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
|
import React, { useState, useEffect } from "react";
import { BehaviorSubject, Observable, from } from "rxjs";
import { UiLogicError } from "@/common";
import {
HttpNetworkError,
BlobWithEtag,
NotModified,
setHttpToken,
} from "@/http/common";
import {
getHttpTokenClient,
HttpCreateTokenBadCredentialError,
} from "@/http/token";
import {
getHttpUserClient,
HttpUserNotExistError,
HttpUser,
UserPermission,
} from "@/http/user";
import { DataHub2 } from "./DataHub2";
import { dataStorage } from "./common";
import { pushAlert } from "./alert";
export type User = HttpUser;
export class AuthUser implements User {
constructor(user: User, public token: string) {
this.uniqueId = user.uniqueId;
this.username = user.username;
this.permissions = user.permissions;
this.nickname = user.nickname;
}
uniqueId: string;
username: string;
permissions: UserPermission[];
nickname: string;
get hasAdministrationPermission(): boolean {
return this.permissions.length !== 0;
}
get hasAllTimelineAdministrationPermission(): boolean {
return this.permissions.includes("AllTimelineManagement");
}
get hasHighlightTimelineAdministrationPermission(): boolean {
return this.permissions.includes("HighlightTimelineManagement");
}
}
export interface LoginCredentials {
username: string;
password: string;
}
export class BadCredentialError {
message = "login.badCredential";
}
const USER_STORAGE_KEY = "currentuser";
export class UserService {
constructor() {
this.userSubject.subscribe((u) => {
setHttpToken(u?.token ?? null);
});
}
private userSubject = new BehaviorSubject<AuthUser | null | undefined>(
undefined
);
get user$(): Observable<AuthUser | null | undefined> {
return this.userSubject;
}
get currentUser(): AuthUser | null | undefined {
return this.userSubject.value;
}
async checkLoginState(): Promise<AuthUser | null> {
if (this.currentUser !== undefined) {
console.warn("Already checked user. Can't check twice.");
}
const savedUser = await dataStorage.getItem<AuthUser | null>(
USER_STORAGE_KEY
);
if (savedUser == null) {
this.userSubject.next(null);
return null;
}
this.userSubject.next(savedUser);
const savedToken = savedUser.token;
try {
const res = await getHttpTokenClient().verify({ token: savedToken });
const user = new AuthUser(res.user, savedToken);
await dataStorage.setItem<AuthUser>(USER_STORAGE_KEY, user);
this.userSubject.next(user);
pushAlert({
type: "success",
message: {
type: "i18n",
key: "user.welcomeBack",
},
});
return user;
} catch (error) {
if (error instanceof HttpNetworkError) {
pushAlert({
type: "danger",
message: { type: "i18n", key: "user.verifyTokenFailedNetwork" },
});
return savedUser;
} else {
await dataStorage.removeItem(USER_STORAGE_KEY);
this.userSubject.next(null);
pushAlert({
type: "danger",
message: { type: "i18n", key: "user.verifyTokenFailed" },
});
return null;
}
}
}
async login(
credentials: LoginCredentials,
rememberMe: boolean
): Promise<void> {
if (this.currentUser) {
throw new UiLogicError("Already login.");
}
try {
const res = await getHttpTokenClient().create({
...credentials,
expire: 30,
});
const user = new AuthUser(res.user, res.token);
if (rememberMe) {
await dataStorage.setItem<AuthUser>(USER_STORAGE_KEY, user);
}
this.userSubject.next(user);
} catch (e) {
if (e instanceof HttpCreateTokenBadCredentialError) {
throw new BadCredentialError();
} else {
throw e;
}
}
}
async logout(): Promise<void> {
if (this.currentUser === undefined) {
throw new UiLogicError("Please check user first.");
}
if (this.currentUser === null) {
throw new UiLogicError("No login.");
}
await dataStorage.removeItem(USER_STORAGE_KEY);
this.userSubject.next(null);
}
changePassword(
oldPassword: string,
newPassword: string
): Observable<unknown> {
if (this.currentUser == undefined) {
throw new UiLogicError("Not login or checked now, can't log out.");
}
const $ = from(
getHttpUserClient().changePassword({
oldPassword,
newPassword,
})
);
$.subscribe(() => {
void this.logout();
});
return $;
}
}
export const userService = new UserService();
export function useRawUser(): AuthUser | null | undefined {
const [user, setUser] = useState<AuthUser | null | undefined>(
userService.currentUser
);
useEffect(() => {
const subscription = userService.user$.subscribe((u) => setUser(u));
return () => {
subscription.unsubscribe();
};
});
return user;
}
export function useUser(): AuthUser | null {
const [user, setUser] = useState<AuthUser | null>(() => {
const initUser = userService.currentUser;
if (initUser === undefined) {
throw new UiLogicError(
"This is a logic error in user module. Current user can't be undefined in useUser."
);
}
return initUser;
});
useEffect(() => {
const sub = userService.user$.subscribe((u) => {
if (u === undefined) {
throw new UiLogicError(
"This is a logic error in user module. User emitted can't be undefined later."
);
}
setUser(u);
});
return () => {
sub.unsubscribe();
};
});
return user;
}
export function useUserLoggedIn(): AuthUser {
const user = useUser();
if (user == null) {
throw new UiLogicError("You assert user has logged in but actually not.");
}
return user;
}
export function checkLogin(): AuthUser {
const user = userService.currentUser;
if (user == null) {
throw new UiLogicError("You must login to perform the operation.");
}
return user;
}
export class UserNotExistError extends Error {}
export class UserInfoService {
saveUser(user: HttpUser): void {
this.userHub.getLine(user.username).save(user);
}
saveUsers(users: HttpUser[]): void {
return users.forEach((user) => this.saveUser(user));
}
async getCachedUser(username: string): Promise<HttpUser | null> {
const user = await this.userHub.getLine(username).getSavedData();
if (user == null || user === "notexist") return null;
return user;
}
async getCachedUsers(usernames: string[]): Promise<HttpUser[] | null> {
const users = await Promise.all(
usernames.map((username) => this.userHub.getLine(username).getSavedData())
);
for (const u of users) {
if (u == null || u === "notexist") {
return null;
}
}
return users as HttpUser[];
}
private generateUserDataStorageKey(username: string): string {
return `user.${username}`;
}
readonly userHub = new DataHub2<string, HttpUser | "notexist">({
saveData: (username, data) => {
if (typeof data === "string") return Promise.resolve();
return dataStorage
.setItem<HttpUser>(this.generateUserDataStorageKey(username), data)
.then();
},
getSavedData: (username) => {
return dataStorage.getItem<HttpUser | null>(
this.generateUserDataStorageKey(username)
);
},
fetchData: async (username) => {
try {
return await getHttpUserClient().get(username);
} catch (e) {
if (e instanceof HttpUserNotExistError) {
return "notexist";
} else if (e instanceof HttpNetworkError) {
return null;
}
throw e;
}
},
});
private generateAvatarDataStorageKey(username: string): string {
return `user.${username}.avatar`;
}
readonly avatarHub = new DataHub2<string, BlobWithEtag | "notexist">({
saveData: async (username, data) => {
if (typeof data === "string") return;
await dataStorage.setItem<BlobWithEtag>(
this.generateAvatarDataStorageKey(username),
data
);
},
getSavedData: (username) =>
dataStorage.getItem<BlobWithEtag | null>(
this.generateAvatarDataStorageKey(username)
),
fetchData: async (username, savedData) => {
try {
if (savedData == null || savedData === "notexist") {
return await getHttpUserClient().getAvatar(username);
} else {
const res = await getHttpUserClient().getAvatar(
username,
savedData.etag
);
if (res instanceof NotModified) {
return savedData;
} else {
return res;
}
}
} catch (e) {
if (e instanceof HttpUserNotExistError) {
return "notexist";
} else if (e instanceof HttpNetworkError) {
return null;
} else {
throw e;
}
}
},
});
async setAvatar(username: string, blob: Blob): Promise<void> {
const etag = await getHttpUserClient().putAvatar(username, blob);
this.avatarHub.getLine(username).save({ data: blob, etag });
}
async setNickname(username: string, nickname: string): Promise<void> {
return getHttpUserClient()
.patch(username, { nickname })
.then((user) => {
this.saveUser(user);
});
}
}
export const userInfoService = new UserInfoService();
export function useAvatar(username?: string): Blob | undefined {
const [state, setState] = React.useState<Blob | undefined>(undefined);
React.useEffect(() => {
if (username == null) {
setState(undefined);
return;
}
const subscription = userInfoService.avatarHub
.getLine(username)
.getObservalble()
.subscribe((data) => {
if (data.data != null && data.data !== "notexist") {
setState(data.data.data);
} else {
setState(undefined);
}
});
return () => {
subscription.unsubscribe();
};
}, [username]);
return state;
}
|