-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationsModule.cs
More file actions
109 lines (102 loc) · 4.45 KB
/
Copy pathNotificationsModule.cs
File metadata and controls
109 lines (102 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SimpleModule.BackgroundJobs.Contracts;
using SimpleModule.Core;
using SimpleModule.Core.Settings;
using SimpleModule.Database;
using SimpleModule.Notifications.Contracts;
using SimpleModule.Notifications.Infrastructure;
using SimpleModule.Notifications.Infrastructure.Channels;
using SimpleModule.Notifications.Infrastructure.Jobs;
namespace SimpleModule.Notifications;
[Module(
NotificationsConstants.ModuleName,
RoutePrefix = NotificationsConstants.RoutePrefix,
ViewPrefix = NotificationsConstants.ViewPrefix
)]
public class NotificationsModule : IModule, IModuleServices
{
public void ConfigureMiddleware(IApplicationBuilder app)
{
// DispatchNotificationJob and the Notifier rely on IBackgroundJobs (whose
// implementation lives in the BackgroundJobs module, not its Contracts assembly).
// Fail fast with a directive message if the implementation isn't installed.
var probe = app.ApplicationServices.GetRequiredService<IServiceProviderIsService>();
if (!probe.IsService(typeof(IBackgroundJobs)))
{
throw new InvalidOperationException(
"SimpleModule.Notifications requires SimpleModule.BackgroundJobs to be installed. "
+ "Add a reference to the SimpleModule.BackgroundJobs project so IBackgroundJobs can be resolved."
);
}
}
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddModuleDbContext<NotificationsDbContext>(
configuration,
NotificationsConstants.ModuleName
);
services.Configure<NotificationsModuleOptions>(configuration.GetSection("Notifications"));
services.AddScoped<INotificationsContracts, NotificationService>();
services.AddScoped<INotifier, Notifier>();
// Channels — registered as INotificationChannel so the registry can enumerate them.
services.AddScoped<INotificationChannel, DatabaseChannel>();
services.AddScoped<INotificationChannel, MailChannel>();
services.AddScoped<INotificationChannel, LogSmsChannel>();
services.AddScoped<INotificationChannelRegistry, NotificationChannelRegistry>();
services.AddModuleJob<DispatchNotificationJob>();
}
public void ConfigureSettings(ISettingsBuilder settings)
{
settings
.Add(
new SettingDefinition
{
Key = NotificationsConstants.SettingsKeys.MailChannelEnabled,
DisplayName = "Mail notifications",
Description = "Receive notifications by email.",
Group = "Notifications",
Scope = SettingScope.User,
DefaultValue = "true",
Type = SettingType.Bool,
}
)
.Add(
new SettingDefinition
{
Key = NotificationsConstants.SettingsKeys.DatabaseChannelEnabled,
DisplayName = "In-app notifications",
Description = "Show notifications in the in-app inbox.",
Group = "Notifications",
Scope = SettingScope.User,
DefaultValue = "true",
Type = SettingType.Bool,
}
)
.Add(
new SettingDefinition
{
Key = NotificationsConstants.SettingsKeys.SmsChannelEnabled,
DisplayName = "SMS notifications",
Description = "Receive notifications by SMS (when a phone number is on file).",
Group = "Notifications",
Scope = SettingScope.User,
DefaultValue = "false",
Type = SettingType.Bool,
}
)
.Add(
new SettingDefinition
{
Key = NotificationsConstants.SettingsKeys.DefaultPageSize,
DisplayName = "Inbox page size",
Description = "Notifications to load per page in the inbox.",
Group = "Notifications",
Scope = SettingScope.Application,
DefaultValue = "20",
Type = SettingType.Number,
}
);
}
}