forked from countersoft/App-EmailNotificationEngine
/
EmailNotificationEngine.cs
722 lines (499 loc) · 32 KB
/
EmailNotificationEngine.cs
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Countersoft.Foundation.Commons.Extensions;
using Countersoft.Gemini.Commons;
using Countersoft.Gemini.Commons.Dto;
using Countersoft.Gemini.Commons.Entity;
using Countersoft.Gemini.Commons.Permissions;
using Countersoft.Gemini.Commons.System;
using Countersoft.Gemini.Contracts;
using Countersoft.Gemini.Contracts.Caching;
using Countersoft.Gemini.Contracts.Business;
using Countersoft.Gemini.Infrastructure.Helpers;
using Countersoft.Gemini.Infrastructure.Managers;
using Countersoft.Gemini.Infrastructure.TimerJobs;
using Countersoft.Gemini.Mailer;
using Microsoft.Practices.Unity;
using Countersoft.Gemini.Extensibility.Apps;
using Countersoft.Gemini;
using Countersoft.Gemini.Commons.Entity.Security;
namespace EmailAlerts
{
[AppType(AppTypeEnum.Timer),
AppGuid("840D6D86-C74A-43D2-8D3A-3CF9985DB1D4"),
AppName("Email Notification Engine"),
AppDescription("Sends email notifications to users")]
public class EmailNotificationEngine : TimerJob
{
private List<AlertTemplate> _templates;
private List<IssueTypeDto> _types;
private List<PermissionSetDto> _permissionSets;
private List<Organization> _organizations;
private IssueManager _issueManager;
public override bool Run(IssueManager issueManager)
{
if (!issueManager.UserContext.Config.EmailAlertsEnabled) return true;
issueManager.UserContext.User.Entity = new User(issueManager.UserContext.User.Entity);
issueManager.UserContext.User.Entity.Language = "en-US";
_issueManager = issueManager;
_templates = GeminiApp.Container.Resolve<IAlertTemplates>().FindWhere(c => c.AlertType != AlertTemplateType.Breeze).ToList();
_types = new MetaManager(issueManager).TypeGetAll();
_permissionSets = new PermissionSetManager(issueManager).GetAll();
_organizations = new OrganizationManager(issueManager).GetAll();
ProcessAppNavCardAlerts();
ProcessWatcherAlerts();
return true;
}
private List<UserDto> GetCardSubscribers(NavigationCard card, NavigationCardsManager cardManager, UserManager userManager, UserDto owner)
{
if (!owner.Entity.Active) return new List<UserDto>();
Dictionary<int, UserDto> subscribers = new Dictionary<int, UserDto>();
subscribers.Add(owner.Entity.Id, owner);
foreach (var user in card.CardData.Subscription.Users)
{
var userDto = userManager.Get(user);
if(user != owner.Entity.Id && userDto != null && userDto.Entity.Active) subscribers.Add(user, userDto);
}
var groupUsers = cardManager.GetUsersFromGroups(card, card.CardData.Subscription.Groups);
foreach (var user in groupUsers)
{
if (!subscribers.ContainsKey(user.Entity.Id)) subscribers.Add(user.Entity.Id, user);
}
return new List<UserDto>(subscribers.Values);
}
private void ProcessAppNavCardAlerts()
{
var navigationCardsManager = new NavigationCardsManager(_issueManager);
List<NavigationCard> cards = navigationCardsManager.GetPendingAlertCards();
LogDebugMessage("Email templates loaded: " + _templates.Count);
LogDebugMessage("Pending card alerts: " + cards.Count);
// ? We need to store user id and issue id for every email we send out -- avoids dupes?
List<string> issuesEmailedToUsers = new List<string>(50);
List<string> individualIssuesEmailedToUsers = new List<string>(50);
AlertsTemplateHelper alerts = new AlertsTemplateHelper(_templates, GetUrl(_issueManager));
UserManager userManager = new UserManager(_issueManager);
bool refreshCache = false;
var allOptOuts = navigationCardsManager.GetOptOuts();
foreach (NavigationCard card in cards)
{
List<IssueDto> individualIssues = new List<IssueDto>();
// Safety checks
if (!card.UserId.HasValue) continue;
if (card.CardData.Alerts.Count == 0) continue;
UserDto recepient = userManager.Get(card.UserId.Value);
// Safety check
if (!recepient.Entity.EmailMe || recepient.Entity.Email.IsEmpty()) continue;
DateTime lastChecked = card.CardData.AlertsLastSent.HasValue ? card.CardData.AlertsLastSent.Value : card.Created;
DateTime lastCheckedLocal = lastChecked.ToLocal(_issueManager.UserContext.User.TimeZone);
AlertTypeAppNavAlertsTemplateModel model = new AlertTypeAppNavAlertsTemplateModel();
model.TheRecipient = recepient;
model.Version = GeminiVersion.Version;
model.GeminiUrl = alerts.GeminiUrl;
List<int> issuesToAlert = new List<int>(card.CardData.Alerts);
foreach (int issueId in issuesToAlert)
{
IssueDto issue = _issueManager.Get(issueId);
// Safety check
if (issue == null || issue.Entity.IsNew) continue;
// Dupe check
string dupeKey = string.Format("{0}-{1}-{2}", recepient.Entity.Id, issueId, card.Id);
if (issuesEmailedToUsers.Contains(dupeKey)) continue;
var permissionManager = new PermissionsManager(recepient, _types, _permissionSets, _organizations, _issueManager.UserContext.Config.HelpDeskModeGroup, false);
if (!permissionManager.CanSeeItem(issue.Project, issue)) continue;
foreach (var comment in issue.Comments)
{
if (!permissionManager.CanSeeComment(issue, comment))
{
issue.Comments.RemoveAll(c => !permissionManager.CanSeeComment(issue, c));
break;
}
}
issue.ChangeLog = _issueManager.GetChangeLog(issue, _issueManager.UserContext.User, recepient, lastCheckedLocal);
// Populate model for email template
if (card.CardData.Subscription.IndividualAlert) individualIssues.Add(issue);
if (card.CardData.Subscription.Created && issue.Created.ToUtc(_issueManager.UserContext.User.TimeZone) >= lastChecked)
{
model.TheItemsCreated.Add(issue);
}
else
{
List<IssueAuditDto> allChanges = issue.History.FindAll(h => h.Entity.Created.ToUtc(_issueManager.UserContext.User.TimeZone) >= lastChecked);
List<IssueAuditDto> commentChanges = allChanges.FindAll(a => !a.Entity.IsCustom && a.Entity.AttributeChanged == ItemAttributeVisibility.AssociatedComments);
List<IssueAuditDto> nonCommentChanges = allChanges.FindAll(a => a.Entity.IsCustom || a.Entity.AttributeChanged != ItemAttributeVisibility.AssociatedComments);
// Add comments and updates
if (card.CardData.Subscription.Updated && nonCommentChanges.Count > 0 || card.CardData.Subscription.Commented && commentChanges.Count > 0 && issue.Comments.Count > 0)
{
model.TheItemsUpdated.Add(issue);
}
if (card.CardData.Subscription.Commented && commentChanges.Count > 0 && issue.Comments.Count > 0)
{
model.TheItemsCommented.Add(issue);
}
}
// Record the fact that we have processed this issue for this recepient (to prevent dupes)
issuesEmailedToUsers.Add(dupeKey);
}
model.CardTitle = string.Format("{0} {1}", card.Key, card.Title);
model.CardKey = card.Key;
model.CardDescription = card.Title;
model.CardComment = card.CardData.Comment;
model.CardUrl = string.Concat(model.GeminiUrl, "workspace/", card.Id, '/', card.Url);
// Safety check!
if (model.ChangeCount > 0)
{
List<UserDto> subscribers = GetCardSubscribers(card, navigationCardsManager, userManager, recepient);
//if (!subscribers.Contains(recepient) && subscribers.Find(u => u.Entity.Id == recepient.Entity.Id) == null) subscribers.Insert(0, recepient);
if (card.CardData.Subscription.IndividualAlert)
{
foreach (var user in subscribers)
{
if (allOptOuts.Any(s => s.UserId == user.Entity.Id && s.CardId == card.Id && s.OptOutType == OptOutEmails.OptOutTypes.Alert)) continue;
foreach (var issue in individualIssues)
{
string individualDupeKey = string.Format("{0}-{1}", user.Entity.Id, issue.Entity.Id);
if (individualIssuesEmailedToUsers.Contains(individualDupeKey)) continue;
if (user != recepient)
{
var permissionManager = new PermissionsManager(user, _types, _permissionSets, _organizations, _issueManager.UserContext.Config.HelpDeskModeGroup, false);
if (!permissionManager.CanSeeItem(issue.Project, issue)) continue;
issue.ChangeLog = _issueManager.GetChangeLog(issue, _issueManager.UserContext.User, user, lastCheckedLocal);
}
var indModel = new AlertTypeIndividualTemplateModel();
indModel.GeminiUrl = model.GeminiUrl;
indModel.LinkViewItem = NavigationHelper.GetIssueUrl(_issueManager.UserContext, issue.Entity.ProjectId, issue.EscapedProjectCode, issue.Entity.Id);
indModel.TheItem = issue;
indModel.TheRecipient = user;
indModel.Version = GeminiVersion.Version;
indModel.IsNewItem = model.TheItemsCreated.Contains(issue);
indModel.CardKey = model.CardKey;
indModel.CardDescription = model.CardDescription;
indModel.CardComment = model.CardComment;
indModel.CardUrl = model.CardUrl;
if (!indModel.IsNewItem && issue.ChangeLog.Count == 0) continue;
var template = alerts.FindTemplateForProject(indModel.IsNewItem ? AlertTemplateType.Created : AlertTemplateType.Updated, issue.Entity.ProjectId);
string html = alerts.GenerateHtml(template, indModel);
if (GeminiApp.GeminiLicense.IsFree) html = alerts.AddSignature(html);
// Send email
string log;
string subject = template.Options.Subject.HasValue() ? alerts.GenerateHtml(template, indModel, true) : string.Format("[{0}] {1} {2} ({3})", issue.IssueKey, issue.Type, model.TheItemsCreated.Contains(issue) ? "Created" : "Updated", issue.Title, issue.IsClosed ? "Closed" : string.Empty);
EmailHelper.Send(_issueManager.UserContext.Config, subject, html, user.Entity.Email, user.Fullname, true, out log);
individualIssuesEmailedToUsers.Add(individualDupeKey);
}
}
}
else
{
var cloneCreated = new List<IssueDto>(model.TheItemsCreated);
var cloneUpdated = new List<IssueDto>(model.TheItemsUpdated);
var cloneCommented = new List<IssueDto>(model.TheItemsCommented);
// Find email template to use (for this project or fall back to default template)
AlertTemplate template = alerts.FindTemplateForProject(AlertTemplateType.AppNavAlerts, 0);
foreach (var user in subscribers)
{
if (allOptOuts.Any(s => s.UserId == user.Entity.Id && s.CardId == card.Id && s.OptOutType == OptOutEmails.OptOutTypes.Alert)) continue;
model.TheItemsCreated = new List<IssueDto>(cloneCreated);
model.TheItemsUpdated = new List<IssueDto>(cloneUpdated);
model.TheItemsCommented = new List<IssueDto>(cloneCommented);
if (user != recepient)
{
var permissionManager = new PermissionsManager(user, _types, _permissionSets, _organizations, _issueManager.UserContext.Config.HelpDeskModeGroup, false);
model.TheItemsCreated.RemoveAll(i => !permissionManager.CanSeeItem(i.Project, i));
model.TheItemsUpdated.RemoveAll(i => !permissionManager.CanSeeItem(i.Project, i));
model.TheItemsCommented.RemoveAll(i => !permissionManager.CanSeeItem(i.Project, i));
foreach (var issue in model.TheItemsCreated.Concat(model.TheItemsUpdated).Concat(model.TheItemsCommented))
{
issue.ChangeLog = _issueManager.GetChangeLog(issue, _issueManager.UserContext.User, user, lastCheckedLocal);
}
}
//model.TheItemsCreated.RemoveAll(i => i.ChangeLog.Count == 0);
model.TheItemsUpdated.RemoveAll(i => i.ChangeLog.Count == 0);
model.TheItemsCommented.RemoveAll(i => i.ChangeLog.Count == 0);
if (model.ChangeCount == 0) continue;
// Generate email template
string html = alerts.GenerateHtml(template, model);
if (GeminiApp.GeminiLicense.IsFree) html = alerts.AddSignature(html);
string subject = template.Options.Subject.HasValue() ? alerts.GenerateHtml(template, model, true) : string.Format("{0} {1}", card.Key, card.Title);
// Send email
string log;
EmailHelper.Send(_issueManager.UserContext.Config, subject, html, user.Entity.Email, user.Fullname, true, out log);
}
}
}
// Remove the alert notifications and update the database
lock (card.CardData.Alerts)
{
card.CardData.Alerts.RemoveAll(a => issuesToAlert.Contains(a));
}
card.CardData.AlertsLastSent = DateTime.UtcNow;
refreshCache = true;
navigationCardsManager.Update(card, false, false);
}
if (refreshCache)
{
navigationCardsManager.Cache.NavigationCards.Invalidate();
var webNodes = GeminiApp.Container.Resolve<IWebNodes>();
webNodes.AddDataOnAllNodesButMe(new WebNodeData() { NodeGuid = GeminiApp.GUID, Key = "cache", Value = navigationCardsManager.Cache.NavigationCards.CacheKey });
}
}
private void ProcessWatcherAlerts()
{
SchedulerSettings settings = _issueManager.UserContext.Config.SchedulerSettings.HasValue() ? _issueManager.UserContext.Config.SchedulerSettings.FromJson<SchedulerSettings>() : new SchedulerSettings();
DateTime lastChecked = settings.LastCheckedWatchers.HasValue ? settings.LastCheckedWatchers.Value : DateTime.UtcNow;
IssuesFilter filter = new IssuesFilter();
filter.RevisedAfter = lastChecked.ToString();
filter.IncludeClosed = true;
LogDebugMessage("Last checked for watched item alerts: " + lastChecked);
List<IssueDto> issues = _issueManager.GetFiltered(filter);
LogDebugMessage("Item that have changed: " + issues.Count);
if (issues.Count > 0) ProcessWatchers(issues, lastChecked);
settings.LastCheckedWatchers = DateTime.UtcNow;
/*serviceManager.Admin.UpdateSchedulerSettings(settings);*/
IConfiguration configuration = GeminiApp.Container.Resolve<IConfiguration>();
GeminiConfiguration config = configuration.Get();
config.SchedulerSettings = settings.ToJson();
ConfigurationItem item = new ConfigurationItem();
item.SettingId = GeminiConfigurationOption.SchedulerSettings.ToString();
item.SettingValue = config.SchedulerSettings;
configuration.Update(item);
GeminiApp.RefreshConfig(config);
}
private bool IsUserOnlyChange(List<IssueAuditDto> history, int userId)
{
return history.Find(h => h.Entity.UserId.GetValueOrDefault() != userId) == null;
}
private void ProcessWatchers(List<IssueDto> issues, DateTime lastChecked)
{
var lastCheckedLocal = lastChecked.ToLocal(_issueManager.UserContext.User.TimeZone);
Dictionary<int, WatcherData> targets = new Dictionary<int, WatcherData>();
Dictionary<string, WatcherData> emailTargets = new Dictionary<string, WatcherData>();
var userManager = new UserManager(_issueManager);
List<int> projectsMissingFollowerTemplate = new List<int>();
int emailWatchers = -3;
// Build array of users that are watching issues
foreach (var issue in issues)
{
//Safety check
if (issue.Watchers.Count == 0) continue;
if (issue.Revised == issue.Created) continue;
if (issue.Revised.ToUtc(_issueManager.UserContext.User.TimeZone) <= lastChecked) continue;
var history = _issueManager.GetHistory(issue);
issue.History = new List<IssueAuditDto>(history);
history.RemoveAll(h => h.Entity.Created <= lastCheckedLocal);
foreach (var watcher in issue.Watchers)
{
if (watcher.Entity.UserId != null)
{
if (targets.ContainsKey(watcher.Entity.UserId.Value))
{
WatcherData data = targets[watcher.Entity.UserId.Value];
var permissionManager = new PermissionsManager(data.User, _types, _permissionSets, _organizations, _issueManager.UserContext.Config.HelpDeskModeGroup, false);
if (!permissionManager.CanSeeItem(issue.Project, issue)) continue;
if (!data.User.Entity.EmailMeMyChanges && IsUserOnlyChange(history, data.User.Entity.Id)) continue;
data.IssueId.Add(issue.Entity.Id);
}
else
{
WatcherData data = new WatcherData();
data.User = userManager.Get(watcher.Entity.UserId.Value);
if (data.User.Entity.Active)
{
var permissionManager = new PermissionsManager(data.User, _types, _permissionSets, _organizations, _issueManager.UserContext.Config.HelpDeskModeGroup, false);
if (!permissionManager.CanSeeItem(issue.Project, issue)) continue;
if (!data.User.Entity.EmailMeMyChanges && IsUserOnlyChange(history, data.User.Entity.Id)) continue;
data.IssueId.Add(issue.Entity.Id);
targets.Add(watcher.Entity.UserId.Value, data);
}
}
}
else
{
if (emailTargets.ContainsKey(watcher.Entity.Email.ToLower()))
{
WatcherData data = emailTargets[watcher.Entity.Email.ToLower()];
data = targets[data.User.Entity.Id];
data.IssueId.Add(issue.Entity.Id);
}
else
{
WatcherData data = new WatcherData();
data.User = new UserDto(new User());
data.User.Entity.Id = emailWatchers--;
data.User.Entity.Email = watcher.Entity.Email;
data.User.Entity.EmailMe = true;
data.User.Entity.EmailMeMyChanges = true;
data.User.Entity.ProjectGroups.Add(new ProjectGroupMembership() { ProjectGroupId = Constants.GlobalGroupEveryone, UserId = data.User.Entity.Id });
UserSettings settings = new UserSettings();
settings.IndividualFollowerAlerts = true;
data.User.Entity.Settings = settings.ToJson();
var group = new ProjectGroup() { Id = Constants.GlobalGroupEveryone, Members = new List<ProjectGroupMembership>() };
group.Members2.Add(new ProjectGroupMembership() { UserId = data.User.Entity.Id, ProjectGroupId = Constants.GlobalGroupEveryone });
data.User.ProjectGroups.Add(group);
data.IssueId.Add(issue.Entity.Id);
emailTargets.Add(watcher.Entity.Email.ToLower(), data);
targets.Add(data.User.Entity.Id, data);
}
}
}
}
AlertsTemplateHelper alerts = new AlertsTemplateHelper(_templates, GetUrl(_issueManager));
// Now loop through users sending them watcher summary email
Dictionary<int, List<IssueCommentDto>> originalComments = new Dictionary<int, List<IssueCommentDto>>();
List<int> processedProjects;
foreach (var target in targets)
{
processedProjects = new List<int>();
if (originalComments.Count > 0)
{
foreach (var kv in originalComments)
{
IssueDto issue = issues.Find(i => i.Entity.Id == kv.Key);
// Safety check
if (issue == null || issue.Entity.IsNew) continue;
issue.Comments = kv.Value;
}
originalComments = new Dictionary<int, List<IssueCommentDto>>();
}
var recipient = target.Value;
// Safety check
if (!recipient.User.Entity.EmailMe || recipient.User.Entity.Email.IsEmpty()) continue;
AlertTypeWatchersTemplateModel model = new AlertTypeWatchersTemplateModel();
model.TheRecipient = recipient.User;
model.Version = GeminiVersion.Version;
model.GeminiUrl = alerts.GeminiUrl;
foreach (int issueId in recipient.IssueId)
{
IssueDto issue = issues.Find(i => i.Entity.Id == issueId);
// Safety check
if (issue == null || issue.Entity.IsNew) continue;
issue.ChangeLog = _issueManager.GetChangeLog(issue, _issueManager.UserContext.User, recipient.User, lastCheckedLocal);
var permissionManager = new PermissionsManager(recipient.User, _types, _permissionSets, _organizations, _issueManager.UserContext.Config.HelpDeskModeGroup, false);
foreach (var comment in issue.Comments)
{
if (!permissionManager.CanSeeComment(issue, comment))
{
originalComments.Add(issueId, issue.Comments);
List<IssueCommentDto> comments = new List<IssueCommentDto>(issue.Comments);
comments.RemoveAll(c => !permissionManager.CanSeeComment(issue, c));
issue.Comments = comments;
break;
}
}
if (issue.ChangeLog.Count == 0) continue;
if (recipient.User.GetSettings().IndividualFollowerAlerts)
{
var template = alerts.FindTemplateForProject(AlertTemplateType.Updated, issue.Entity.ProjectId);
if (template == null)
{
LogDebugMessage("No update notification template found");
continue;
}
var indModel = new AlertTypeIndividualTemplateModel();
indModel.GeminiUrl = model.GeminiUrl;
indModel.LinkViewItem = NavigationHelper.GetIssueUrl(_issueManager.UserContext, issue.Entity.ProjectId, issue.EscapedProjectCode, issue.Entity.Id);
indModel.TheItem = issue;
indModel.TheRecipient = recipient.User;
indModel.Version = GeminiVersion.Version;
indModel.IsNewItem = false;
string html = alerts.GenerateHtml(template, indModel);
if (GeminiApp.GeminiLicense.IsFree) html = alerts.AddSignature(html);
string log;
string subject = template.Options.Subject.HasValue() ? alerts.GenerateHtml(template, indModel, true) : string.Format("[{0}] {1} {2} ({3})", issue.IssueKey, issue.Type, "Updated", issue.Title, issue.IsClosed ? "Closed" : string.Empty);
EmailHelper.Send(_issueManager.UserContext.Config, subject, html, recipient.User.Entity.Email, recipient.User.Fullname, true, out log);
}
else
{
model.TheItemsUpdated.Add(issue);
}
}
if (recipient.User.GetSettings().IndividualFollowerAlerts) continue;
// Safety check!
if (model.ChangeCount > 0)
{
var watcherAlertTemplates = alerts.Templates.FindAll(s => s.AlertType == AlertTemplateType.Watchers);
if (watcherAlertTemplates.Count == 0)
{
LogDebugMessage("No follower notification template found");
continue;
}
if (!watcherAlertTemplates.Any(p => p.GetAssociatedProjectValue().IsEmpty()))
{
List<Project> allItemProjects = model.TheItemsUpdated.Select(s => s.Project).ToList();
allItemProjects = allItemProjects.Where(s => !watcherAlertTemplates.Any(a => a.GetAssociatedProjects().Contains(s.Id))).ToList();
if (projectsMissingFollowerTemplate.Count > 0)
{
allItemProjects = allItemProjects.Where(s => !projectsMissingFollowerTemplate.Contains(s.Id)).ToList();
}
if (allItemProjects.Count > 0)
{
LogDebugMessage(string.Concat("No follower notification template found for project ", string.Join(", ", allItemProjects.Select(s => s.Name).Distinct())));
projectsMissingFollowerTemplate.AddRange(allItemProjects.Select(s => s.Id).Distinct());
}
}
watcherAlertTemplates.Sort((x, y) => y.GetAssociatedProjectValue().CompareTo(x.GetAssociatedProjectValue()));
foreach(var watcherTemplate in watcherAlertTemplates)
{
var allTemplateProjects = watcherTemplate.GetAssociatedProjects();
var issuesTemplate = allTemplateProjects.Count == 0 ? model.TheItemsUpdated : model.TheItemsUpdated.FindAll(s => allTemplateProjects.Contains(s.Entity.ProjectId));
if (issuesTemplate.Count == 0) continue;
var projectIds = issuesTemplate.Select(s => s.Entity.ProjectId).Distinct();
if (processedProjects.Count > 0)
{
projectIds = projectIds.Where(s => !processedProjects.Contains(s));
issuesTemplate = issuesTemplate.Where(s => !processedProjects.Contains(s.Entity.ProjectId)).ToList();
}
if (processedProjects.Contains(0) || projectIds.Count() == 0 || issuesTemplate.Count == 0) continue;
AlertTypeWatchersTemplateModel projectTemplateModel = new AlertTypeWatchersTemplateModel();
projectTemplateModel.TheItemsUpdated.AddRange(issuesTemplate);
projectTemplateModel.TheRecipient = model.TheRecipient;
projectTemplateModel.Version = model.Version;
projectTemplateModel.GeminiUrl = model.GeminiUrl;
AlertTemplate template = alerts.FindTemplateForProject(AlertTemplateType.Watchers, issuesTemplate.First().Entity.ProjectId);
if (template.Id == 0)
{
continue;
}
// Generate email template
string html = alerts.GenerateHtml(template, projectTemplateModel);
if (GeminiApp.GeminiLicense.IsFree) html = alerts.AddSignature(html);
string subject = template.Options.Subject.HasValue() ? alerts.GenerateHtml(template, projectTemplateModel, true) : string.Format("{0} {1}", projectTemplateModel.ChangeCount, "Gemini Updates");
// Send email
string log;
EmailHelper.Send(_issueManager.UserContext.Config, subject, html, recipient.User.Entity.Email, recipient.User.Entity.Fullname, true, out log);
if (allTemplateProjects.Count == 0)
{
processedProjects.Add(0);
}
else
{
processedProjects.AddRange(projectIds);
}
}
}
}
}
public override void Shutdown()
{
}
public override TimerJobSchedule GetInterval(IGlobalConfigurationWidgetStore dataStore)
{
var data = dataStore.Get<TimerJobSchedule>(AppGuid);
if (data == null || data.Value == null || (data.Value.Cron.IsEmpty() && data.Value.IntervalInHours.GetValueOrDefault() == 0 && data.Value.IntervalInMinutes.GetValueOrDefault() == 0))
{
int interval = GeminiApp.Config.EmailAlertsPollInterval.ToInt();
return new TimerJobSchedule(interval == 0 ? 5 : interval);
}
return data.Value;
}
}
public class WatcherData
{
public WatcherData()
{
IssueId = new List<int>();
}
public UserDto User { get; set; }
public List<int> IssueId { get; set; }
}
}