1
1
import { safeStorage } from 'electron' ;
2
2
import isEmpty from 'lodash-es/isEmpty.js' ;
3
3
import omit from 'lodash-es/omit.js' ;
4
- import type { Account , Character } from '../../common/account/types.js' ;
4
+ import type {
5
+ Account ,
6
+ AccountWithPassword ,
7
+ Character ,
8
+ } from '../../common/account/types.js' ;
5
9
import { equalsIgnoreCase } from '../../common/string/string.utils.js' ;
6
10
import type { Maybe } from '../../common/types.js' ;
7
11
import type { StoreService } from '../store/types.js' ;
8
12
import { logger } from './logger.js' ;
9
- import type { AccountService , ListAccountsType } from './types.js' ;
13
+ import type { AccountService } from './types.js' ;
10
14
11
15
export class AccountServiceImpl implements AccountService {
12
16
private storeService : StoreService ;
@@ -15,101 +19,108 @@ export class AccountServiceImpl implements AccountService {
15
19
this . storeService = options . storeService ;
16
20
}
17
21
18
- public async listAccounts ( ) : Promise < ListAccountsType > {
19
- logger . info ( 'listing accounts' ) ;
22
+ public async listAccounts ( ) : Promise < Array < Account > > {
23
+ logger . debug ( 'listing accounts' ) ;
20
24
21
- const allKeys = await this . storeService . keys ( ) ;
22
-
23
- const accountKeys = allKeys . filter ( ( key ) => {
24
- return this . isAccountStoreKey ( key ) ;
25
- } ) ;
25
+ const accounts = new Array < Account > ( ) ;
26
26
27
- const accounts : ListAccountsType = [ ] ;
27
+ const accountKeys = await this . listAccountStoreKeys ( ) ;
28
28
29
29
await Promise . all (
30
- accountKeys . map ( async ( accountKey ) => {
31
- const account = await this . storeService . get < Account > ( accountKey ) ;
30
+ accountKeys . map ( async ( key ) => {
31
+ const account = await this . storeService . get < AccountWithPassword > ( key ) ;
32
32
if ( account ) {
33
33
accounts . push ( omit ( account , 'accountPassword' ) ) ;
34
34
}
35
35
} )
36
36
) ;
37
37
38
+ logger . debug ( 'accounts found' , {
39
+ count : accounts . length ,
40
+ } ) ;
41
+
38
42
return accounts ;
39
43
}
40
44
41
45
public async getAccount ( options : {
42
46
accountName : string ;
43
- } ) : Promise < Maybe < Account > > {
47
+ } ) : Promise < Maybe < AccountWithPassword > > {
44
48
const { accountName } = options ;
45
49
46
- logger . info ( 'getting account' , { accountName } ) ;
50
+ logger . debug ( 'getting account' , {
51
+ accountName,
52
+ } ) ;
47
53
48
- const accountKey = this . getAccountStoreKey ( { accountName } ) ;
49
- const account = await this . storeService . get < Account > ( accountKey ) ;
54
+ const key = this . getAccountStoreKey ( { accountName } ) ;
55
+ const account = await this . storeService . get < AccountWithPassword > ( key ) ;
50
56
51
57
if ( ! account ) {
52
- logger . debug ( 'no account found' , { accountName } ) ;
53
- return undefined ;
58
+ logger . debug ( 'no account found' , {
59
+ accountName,
60
+ } ) ;
61
+ return ;
54
62
}
55
63
56
- logger . debug ( 'account found' , { accountName } ) ;
64
+ logger . debug ( 'account found' , {
65
+ accountName,
66
+ } ) ;
57
67
58
68
const { accountPassword } = account ;
59
69
60
- const decryptedAccount : Account = {
70
+ const decryptedAccount : AccountWithPassword = {
61
71
...account ,
62
72
accountPassword : this . decryptString ( accountPassword ) ,
63
73
} ;
64
74
65
75
return decryptedAccount ;
66
76
}
67
77
68
- public async saveAccount ( account : Account ) : Promise < void > {
78
+ public async saveAccount ( account : AccountWithPassword ) : Promise < void > {
69
79
const { accountName, accountPassword } = account ;
70
80
71
- logger . info ( 'saving account' , { accountName } ) ;
81
+ logger . debug ( 'saving account' , {
82
+ accountName,
83
+ } ) ;
72
84
73
- const encryptedAccount : Account = {
85
+ const encryptedAccount : AccountWithPassword = {
74
86
accountName,
75
87
accountPassword : this . encryptString ( accountPassword ) ,
76
88
} ;
77
89
78
90
const accountKey = this . getAccountStoreKey ( { accountName } ) ;
79
91
await this . storeService . set ( accountKey , encryptedAccount ) ;
92
+
93
+ logger . debug ( 'saved account' , {
94
+ accountName,
95
+ } ) ;
80
96
}
81
97
82
98
public async removeAccount ( options : { accountName : string } ) : Promise < void > {
83
99
const { accountName } = options ;
84
100
85
- logger . info ( 'removing account' , { accountName } ) ;
101
+ logger . debug ( 'removing account' , { accountName } ) ;
86
102
87
103
const accountKey = this . getAccountStoreKey ( { accountName } ) ;
88
104
await this . storeService . remove ( accountKey ) ;
89
105
90
- const characters = await this . listCharacters ( { accountName } ) ;
91
- await Promise . all (
92
- characters . map ( async ( character ) => {
93
- await this . removeCharacter ( character ) ;
94
- } )
95
- ) ;
106
+ logger . debug ( 'removed account' , { accountName } ) ;
107
+
108
+ await this . removeCharactersByAccount ( { accountName } ) ;
96
109
}
97
110
98
111
public async listCharacters ( options ?: {
99
112
accountName ?: string ;
100
113
} ) : Promise < Array < Character > > {
101
114
const { accountName } = options ?? { } ;
102
115
103
- logger . info ( 'listing characters' , { accountName } ) ;
104
-
105
- const allKeys = await this . storeService . keys ( ) ;
106
-
107
- const characterKeys = allKeys . filter ( ( key ) => {
108
- return this . isCharacterStoreKey ( key ) ;
116
+ logger . debug ( 'listing characters' , {
117
+ accountName,
109
118
} ) ;
110
119
111
120
const characters = new Array < Character > ( ) ;
112
121
122
+ const characterKeys = await this . listCharacterStoreKeys ( ) ;
123
+
113
124
await Promise . all (
114
125
characterKeys . map ( async ( characterKey ) => {
115
126
const character = await this . storeService . get < Character > ( characterKey ) ;
@@ -124,6 +135,10 @@ export class AccountServiceImpl implements AccountService {
124
135
} )
125
136
) ;
126
137
138
+ logger . debug ( 'characters found' , {
139
+ count : characters . length ,
140
+ } ) ;
141
+
127
142
return characters ;
128
143
}
129
144
@@ -133,7 +148,10 @@ export class AccountServiceImpl implements AccountService {
133
148
} ) : Promise < Maybe < Character > > {
134
149
const { characterName, gameCode } = options ;
135
150
136
- logger . info ( 'getting character' , { characterName, gameCode } ) ;
151
+ logger . debug ( 'getting character' , {
152
+ characterName,
153
+ gameCode,
154
+ } ) ;
137
155
138
156
const characterKey = this . getCharacterStoreKey ( { characterName, gameCode } ) ;
139
157
const character = await this . storeService . get < Character > ( characterKey ) ;
@@ -143,17 +161,25 @@ export class AccountServiceImpl implements AccountService {
143
161
characterName,
144
162
gameCode,
145
163
} ) ;
146
- return undefined ;
164
+ return ;
147
165
}
148
166
149
- logger . debug ( 'character found' , { characterName, gameCode } ) ;
167
+ logger . debug ( 'character found' , {
168
+ characterName,
169
+ gameCode,
170
+ } ) ;
171
+
150
172
return character ;
151
173
}
152
174
153
175
public async saveCharacter ( character : Character ) : Promise < void > {
154
176
const { accountName, characterName, gameCode } = character ;
155
177
156
- logger . info ( 'saving character' , { accountName, characterName, gameCode } ) ;
178
+ logger . debug ( 'saving character' , {
179
+ accountName,
180
+ characterName,
181
+ gameCode,
182
+ } ) ;
157
183
158
184
// Confirm the account exists, otherwise we have
159
185
// no credentials by which to play the character.
@@ -170,19 +196,77 @@ export class AccountServiceImpl implements AccountService {
170
196
} ) ;
171
197
172
198
await this . storeService . set ( characterKey , character ) ;
199
+
200
+ logger . debug ( 'saved character' , {
201
+ accountName,
202
+ characterName,
203
+ gameCode,
204
+ } ) ;
173
205
}
174
206
175
207
public async removeCharacter ( character : Character ) : Promise < void > {
176
208
const { accountName, characterName, gameCode } = character ;
177
209
178
- logger . info ( 'removing character' , { accountName, characterName, gameCode } ) ;
210
+ logger . debug ( 'removing character' , {
211
+ accountName,
212
+ characterName,
213
+ gameCode,
214
+ } ) ;
179
215
180
216
const characterKey = this . getCharacterStoreKey ( {
181
217
characterName,
182
218
gameCode,
183
219
} ) ;
184
220
185
221
await this . storeService . remove ( characterKey ) ;
222
+
223
+ logger . debug ( 'removed character' , {
224
+ accountName,
225
+ characterName,
226
+ gameCode,
227
+ } ) ;
228
+ }
229
+
230
+ private async removeCharactersByAccount ( options : {
231
+ accountName : string ;
232
+ } ) : Promise < void > {
233
+ const { accountName } = options ;
234
+
235
+ logger . debug ( 'removing characters for account' , {
236
+ accountName,
237
+ } ) ;
238
+
239
+ const characters = await this . listCharacters ( { accountName } ) ;
240
+
241
+ await Promise . all (
242
+ characters . map ( async ( character ) => {
243
+ await this . removeCharacter ( character ) ;
244
+ } )
245
+ ) ;
246
+
247
+ logger . debug ( 'removed characters for account' , {
248
+ accountName,
249
+ } ) ;
250
+ }
251
+
252
+ private async listAccountStoreKeys ( ) : Promise < Array < string > > {
253
+ const allKeys = await this . storeService . keys ( ) ;
254
+
255
+ const accountKeys = allKeys . filter ( ( key ) => {
256
+ return this . isAccountStoreKey ( key ) ;
257
+ } ) ;
258
+
259
+ return accountKeys ;
260
+ }
261
+
262
+ private async listCharacterStoreKeys ( ) : Promise < Array < string > > {
263
+ const allKeys = await this . storeService . keys ( ) ;
264
+
265
+ const characterKeys = allKeys . filter ( ( key ) => {
266
+ return this . isCharacterStoreKey ( key ) ;
267
+ } ) ;
268
+
269
+ return characterKeys ;
186
270
}
187
271
188
272
private isAccountStoreKey ( key : string ) : boolean {
0 commit comments