55
66namespace 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 }
0 commit comments