Skip to content

Commit af3f175

Browse files
committed
added unread counter
1 parent 85c61a3 commit af3f175

5 files changed

Lines changed: 67 additions & 56 deletions

File tree

Services/MailboxService.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,5 @@ public async Task<string> GenerateNewMailbox(string publicKey, string password)
199199

200200
return mailAddress;
201201
}
202-
203-
public async Task SetMailReadMark(int mailId, bool isRead)
204-
{
205-
await this.mailRepository.SetReadMark(mailId, isRead);
206-
}
207202
}
208203
}

Web/Pages/Mailbox.razor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ private void MailboxSessionService_MailReceived(object sender, EventArgs e)
4343
protected async Task ShowMail(MailPreviewDto mailPreview)
4444
{
4545
this.CurrentMail = await this.MailboxSessionService.GetMailByIdAsync(mailPreview.Id);
46-
await this.MailboxService.SetMailReadMark(mailPreview.Id, true);
4746
mailPreview.IsRead = true;
47+
await this.MailboxSessionService.SetMailReadMark(mailPreview.Id, true);
4848
}
4949

5050
protected async Task DeleteCurrentMail()

Web/Services/MailboxSessionService.cs

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,20 @@
55

66
namespace Lyralabs.TempMailServer.Web.Services
77
{
8-
public class MailboxSessionService : IRecipient<MailReceivedMessage>, IDisposable
8+
public class MailboxSessionService(
9+
UserState userState,
10+
ILocalStorageService localStorage,
11+
AsymmetricCryptoService cryptoService,
12+
MailboxService mailboxService,
13+
MailRepository mailRepository,
14+
IMessenger messenger,
15+
ILogger<MailboxSessionService> logger) : IRecipient<MailReceivedMessage>, IDisposable
916
{
1017
public event EventHandler MailReceived;
11-
12-
private readonly UserState userState;
13-
private readonly ILocalStorageService localStorage;
14-
private readonly AsymmetricCryptoService cryptoService;
15-
private readonly MailboxService mailboxService;
16-
private readonly IMessenger messenger;
17-
private readonly ILogger<MailboxSessionService> logger;
18+
public event EventHandler MailRead;
1819

1920
public List<MailPreviewDto> Mails { get; private set; } = [];
2021

21-
public MailboxSessionService(
22-
UserState userState,
23-
ILocalStorageService LocalStorage,
24-
AsymmetricCryptoService cryptoService,
25-
MailboxService mailboxService,
26-
IMessenger messenger,
27-
ILogger<MailboxSessionService> logger)
28-
{
29-
this.userState = userState;
30-
this.localStorage = LocalStorage;
31-
this.cryptoService = cryptoService;
32-
this.mailboxService = mailboxService;
33-
this.messenger = messenger;
34-
this.logger = logger;
35-
}
36-
3722
public void TestEmail()
3823
{
3924
try
@@ -42,7 +27,7 @@ public void TestEmail()
4227
client.Timeout = 1000;
4328

4429
var from = new MailAddress("steve@contoso.com", "Steve Ballmer");
45-
var to = new MailAddress(this.userState.CurrentMailbox, "Steve Jobs");
30+
var to = new MailAddress(userState.CurrentMailbox, "Steve Jobs");
4631
var msg = new MailMessage(from, to);
4732
msg.Subject = "Test Mail";
4833
msg.Body = $"Test Mail issued at {DateTime.UtcNow} (UTC)\r\n{Guid.NewGuid()}";
@@ -51,74 +36,80 @@ public void TestEmail()
5136
}
5237
catch (Exception ex)
5338
{
54-
this.logger.LogError(ex, "failed to send test-email");
39+
logger.LogError(ex, "failed to send test-email");
5540
}
5641
}
5742

5843
public async Task<UserSecret> GetOrCreateUserSecret()
5944
{
60-
if (await this.localStorage.ContainKeyAsync("secret") == true)
45+
if (await localStorage.ContainKeyAsync("secret") == true)
6146
{
62-
var secretFromStorage = await this.localStorage.GetItemAsync<UserSecret>("secret");
47+
var secretFromStorage = await localStorage.GetItemAsync<UserSecret>("secret");
6348

6449
if (secretFromStorage.PrivateKey != null && secretFromStorage.PublicKey != null && secretFromStorage.Password != null)
6550
{
6651
return secretFromStorage;
6752
}
6853
}
6954

70-
var secret = this.cryptoService.GenerateUserSecret();
55+
var secret = cryptoService.GenerateUserSecret();
7156

72-
await this.localStorage.SetItemAsync("secret", secret);
57+
await localStorage.SetItemAsync("secret", secret);
7358

7459
return secret;
7560
}
7661

7762
public async Task Refresh()
7863
{
79-
this.Mails = await this.mailboxService.GetDecryptedMailsAsync(this.userState.CurrentMailbox, this.userState.Secret.Value.PrivateKey);
64+
this.Mails = await mailboxService.GetDecryptedMailsAsync(userState.CurrentMailbox, userState.Secret.Value.PrivateKey);
8065
this.MailReceived?.Invoke(this, EventArgs.Empty);
8166
}
8267

8368
public void Receive(MailReceivedMessage message)
8469
{
85-
this.Mails.Insert(0, this.mailboxService.ConvertToPreview(message.Mail));
70+
this.Mails.Insert(0, mailboxService.ConvertToPreview(message.Mail));
8671
this.MailReceived?.Invoke(this, EventArgs.Empty);
8772
}
8873

8974
public async Task DeleteMail(int mailid)
9075
{
91-
await this.mailboxService.DeleteMail(mailid);
76+
await mailboxService.DeleteMail(mailid);
9277
this.Mails.Remove(this.Mails.Single(m => m.Id == mailid));
9378
}
9479

9580
public async Task GetMailbox(bool forceNew = false)
9681
{
97-
var userSecret = this.userState.Secret.Value;
82+
var userSecret = userState.Secret.Value;
9883

9984
if (forceNew == true)
10085
{
101-
this.userState.CurrentMailbox = await this.mailboxService.GenerateNewMailbox(userSecret.PublicKey, userSecret.Password);
86+
userState.CurrentMailbox = await mailboxService.GenerateNewMailbox(userSecret.PublicKey, userSecret.Password);
10287
}
10388
else
10489
{
105-
this.userState.CurrentMailbox = await this.mailboxService.GetOrCreateMailboxAsync(userSecret.PrivateKey, userSecret.Password);
90+
userState.CurrentMailbox = await mailboxService.GetOrCreateMailboxAsync(userSecret.PrivateKey, userSecret.Password);
10691
}
10792

108-
this.messenger.Register(this, this.userState.CurrentMailbox);
93+
messenger.Register(this, userState.CurrentMailbox);
10994

11095
await this.Refresh();
11196
}
11297

11398
internal async Task<MailModel> GetMailByIdAsync(int mailId)
11499
{
115-
var mail = await this.mailboxService.GetDecryptedMailById(this.userState.CurrentMailbox, mailId, this.userState.Secret.Value.PrivateKey);
100+
var mail = await mailboxService.GetDecryptedMailById(userState.CurrentMailbox, mailId, userState.Secret.Value.PrivateKey);
116101
return mail;
117102
}
118103

104+
public async Task SetMailReadMark(int mailId, bool isRead)
105+
{
106+
await mailRepository.SetReadMark(mailId, isRead);
107+
this.MailRead?.Invoke(this, EventArgs.Empty);
108+
}
109+
119110
public void Dispose()
120111
{
121-
this.messenger.UnregisterAll(this);
112+
messenger.UnregisterAll(this);
122113
this.Mails.Clear();
123114
}
124115
}

Web/Shared/SideNavigationMenu.razor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
<NavLink class="nav-link" href="inbox" Match="NavLinkMatch.All">
1919
<i class="fas fa-inbox"></i>
2020
Mailbox
21+
@if (this.UnreadCount > 0)
22+
{
23+
<span class="badge bg-primary rounded-pill ms-2">@this.UnreadCount</span>
24+
}
2125
</NavLink>
2226
</li>
2327
</ul>

Web/Shared/SideNavigationMenu.razor.cs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Lyralabs.TempMailServer.Web.Shared
55
{
6-
partial class SideNavigationMenu
6+
partial class SideNavigationMenu : IDisposable
77
{
88
[Inject]
99
protected IJSRuntime JsRuntime { get; set; }
@@ -19,17 +19,19 @@ partial class SideNavigationMenu
1919

2020
public bool IsMailboxActive { get; private set; }
2121

22+
public int UnreadCount { get; private set; }
23+
2224
protected override void OnParametersSet()
2325
{
2426
this.CheckMailboxUrl();
2527
}
2628

27-
protected override async Task OnAfterRenderAsync(bool firstRender)
29+
protected override async Task OnInitializedAsync()
2830
{
29-
if (firstRender == false)
30-
{
31-
return;
32-
}
31+
await base.OnInitializedAsync();
32+
33+
this.MailboxSessionService.MailReceived += this.MailboxSessionService_MailReceived;
34+
this.MailboxSessionService.MailRead += this.MailboxSessionService_MailRead;
3335

3436
if (this.UserState.Secret is null)
3537
{
@@ -44,11 +46,20 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
4446
{
4547
await this.Refresh();
4648
}
49+
50+
this.NavigationManager.LocationChanged += this.NavigationManager_LocationChanged;
4751
}
4852

49-
protected override void OnInitialized()
53+
private void MailboxSessionService_MailRead(object sender, EventArgs e)
5054
{
51-
this.NavigationManager.LocationChanged += this.NavigationManager_LocationChanged;
55+
this.UnreadCount = this.MailboxSessionService.Mails.Count(mail => mail.IsRead == false);
56+
this.InvokeAsync(this.StateHasChanged);
57+
}
58+
59+
private void MailboxSessionService_MailReceived(object sender, EventArgs e)
60+
{
61+
this.UnreadCount = this.MailboxSessionService.Mails.Count(mail => mail.IsRead == false);
62+
_ = this.InvokeAsync(this.StateHasChanged);
5263
}
5364

5465
private void NavigationManager_LocationChanged(object sender, LocationChangedEventArgs e)
@@ -62,13 +73,12 @@ private void CheckMailboxUrl()
6273
this.IsMailboxActive = Uri.TryCreate(this.NavigationManager.Uri, UriKind.Absolute, out var uri) == true
6374
&& uri.LocalPath.Equals("/inbox", StringComparison.OrdinalIgnoreCase) == true;
6475

65-
this.StateHasChanged();
76+
_ = this.InvokeAsync(this.StateHasChanged);
6677
}
6778

6879
protected async Task Refresh()
6980
{
7081
await this.MailboxSessionService.Refresh();
71-
this.StateHasChanged();
7282
}
7383

7484
protected void TestEmail()
@@ -78,7 +88,18 @@ protected void TestEmail()
7888

7989
protected async Task NewAddress()
8090
{
81-
await this.MailboxSessionService.GetMailbox(forceNew: true);
91+
var result = await this.JsRuntime.InvokeAsync<bool?>("confirm", "Are you sure you want to create a new mailbox? This will delete all your current emails.");
92+
93+
if (result == true)
94+
{
95+
await this.MailboxSessionService.GetMailbox(forceNew: true);
96+
}
97+
}
98+
99+
public void Dispose()
100+
{
101+
this.MailboxSessionService.MailReceived -= this.MailboxSessionService_MailReceived;
102+
this.MailboxSessionService.MailRead -= this.MailboxSessionService_MailRead;
82103
}
83104
}
84105
}

0 commit comments

Comments
 (0)