aboutsummaryrefslogtreecommitdiff
path: root/FrontEnd/src/app/services
diff options
context:
space:
mode:
Diffstat (limited to 'FrontEnd/src/app/services')
-rw-r--r--FrontEnd/src/app/services/timeline.ts62
-rw-r--r--FrontEnd/src/app/services/user.ts30
2 files changed, 38 insertions, 54 deletions
diff --git a/FrontEnd/src/app/services/timeline.ts b/FrontEnd/src/app/services/timeline.ts
index c58516fc..3b9a9072 100644
--- a/FrontEnd/src/app/services/timeline.ts
+++ b/FrontEnd/src/app/services/timeline.ts
@@ -28,13 +28,7 @@ export type { TimelineVisibility } from "@/http/timeline";
import { dataStorage, throwIfNotNetworkError, BlobOrStatus } from "./common";
import { DataHub, WithSyncStatus } from "./DataHub";
-import {
- checkLogin,
- userService,
- userInfoService,
- User,
- AuthUser,
-} from "./user";
+import { userInfoService, User, AuthUser } from "./user";
export type TimelineInfo = HttpTimelineInfo;
export type TimelineChangePropertyRequest = HttpTimelinePatchRequest;
@@ -227,14 +221,10 @@ export class TimelineService {
}
createTimeline(timelineName: string): Observable<TimelineInfo> {
- const user = checkLogin();
return from(
- getHttpTimelineClient().postTimeline(
- {
- name: timelineName,
- },
- user.token
- )
+ getHttpTimelineClient().postTimeline({
+ name: timelineName,
+ })
).pipe(
convertError(HttpTimelineNameConflictError, TimelineNameConflictError)
);
@@ -244,10 +234,9 @@ export class TimelineService {
timelineName: string,
req: TimelineChangePropertyRequest
): Observable<TimelineInfo> {
- const user = checkLogin();
return from(
getHttpTimelineClient()
- .patchTimeline(timelineName, req, user.token)
+ .patchTimeline(timelineName, req)
.then((timeline) => {
void this.syncTimeline(timelineName);
return timeline;
@@ -256,17 +245,13 @@ export class TimelineService {
}
deleteTimeline(timelineName: string): Observable<unknown> {
- const user = checkLogin();
- return from(
- getHttpTimelineClient().deleteTimeline(timelineName, user.token)
- );
+ return from(getHttpTimelineClient().deleteTimeline(timelineName));
}
addMember(timelineName: string, username: string): Observable<unknown> {
- const user = checkLogin();
return from(
getHttpTimelineClient()
- .memberPut(timelineName, username, user.token)
+ .memberPut(timelineName, username)
.then(() => {
void this.syncTimeline(timelineName);
})
@@ -274,10 +259,9 @@ export class TimelineService {
}
removeMember(timelineName: string, username: string): Observable<unknown> {
- const user = checkLogin();
return from(
getHttpTimelineClient()
- .memberDelete(timelineName, username, user.token)
+ .memberDelete(timelineName, username)
.then(() => {
void this.syncTimeline(timelineName);
})
@@ -344,10 +328,7 @@ export class TimelineService {
try {
if (lastUpdatedTime == null) {
- const httpPosts = await getHttpTimelineClient().listPost(
- key,
- userService.currentUser?.token
- );
+ const httpPosts = await getHttpTimelineClient().listPost(key);
userInfoService.saveUsers(
uniqBy(
@@ -362,14 +343,10 @@ export class TimelineService {
line.next({ type: "synced", posts });
} else {
- const httpPosts = await getHttpTimelineClient().listPost(
- key,
- userService.currentUser?.token,
- {
- modifiedSince: lastUpdatedTime,
- includeDeleted: true,
- }
- );
+ const httpPosts = await getHttpTimelineClient().listPost(key, {
+ modifiedSince: lastUpdatedTime,
+ includeDeleted: true,
+ });
const deletedIds = httpPosts
.filter((p) => p.deleted)
@@ -582,10 +559,9 @@ export class TimelineService {
timelineName: string,
request: TimelineCreatePostRequest
): Observable<unknown> {
- const user = checkLogin();
return from(
getHttpTimelineClient()
- .postPost(timelineName, request, user.token)
+ .postPost(timelineName, request)
.then(() => {
void this.syncPosts(timelineName);
})
@@ -593,10 +569,9 @@ export class TimelineService {
}
deletePost(timelineName: string, postId: number): Observable<unknown> {
- const user = checkLogin();
return from(
getHttpTimelineClient()
- .deletePost(timelineName, postId, user.token)
+ .deletePost(timelineName, postId)
.then(() => {
void this.syncPosts(timelineName);
})
@@ -681,7 +656,10 @@ export function validateTimelineName(name: string): boolean {
export function useTimelineInfo(
timelineName: string
-): TimelineWithSyncStatus | undefined {
+): [
+ TimelineWithSyncStatus | undefined,
+ React.Dispatch<React.SetStateAction<TimelineWithSyncStatus | undefined>>
+] {
const [state, setState] = React.useState<TimelineWithSyncStatus | undefined>(
undefined
);
@@ -695,7 +673,7 @@ export function useTimelineInfo(
subscription.unsubscribe();
};
}, [timelineName]);
- return state;
+ return [state, setState];
}
export function usePostList(
diff --git a/FrontEnd/src/app/services/user.ts b/FrontEnd/src/app/services/user.ts
index 7a60b474..c3343493 100644
--- a/FrontEnd/src/app/services/user.ts
+++ b/FrontEnd/src/app/services/user.ts
@@ -5,7 +5,12 @@ import { map, filter } from "rxjs/operators";
import { UiLogicError } from "@/common";
import { convertError } from "@/utilities/rxjs";
-import { HttpNetworkError, BlobWithEtag, NotModified } from "@/http/common";
+import {
+ HttpNetworkError,
+ BlobWithEtag,
+ NotModified,
+ tokenSubject,
+} from "@/http/common";
import {
getHttpTokenClient,
HttpCreateTokenBadCredentialError,
@@ -61,6 +66,12 @@ export class BadCredentialError {
const USER_STORAGE_KEY = "currentuser";
export class UserService {
+ constructor() {
+ this.userSubject.subscribe((u) => {
+ tokenSubject.next(u?.token ?? null);
+ });
+ }
+
private userSubject = new BehaviorSubject<AuthUser | null | undefined>(
undefined
);
@@ -167,13 +178,10 @@ export class UserService {
throw new UiLogicError("Not login or checked now, can't log out.");
}
const $ = from(
- getHttpUserClient().changePassword(
- {
- oldPassword,
- newPassword,
- },
- this.currentUser.token
- )
+ getHttpUserClient().changePassword({
+ oldPassword,
+ newPassword,
+ })
);
$.subscribe(() => {
void this.logout();
@@ -378,15 +386,13 @@ export class UserInfoService {
}
async setAvatar(username: string, blob: Blob): Promise<void> {
- const user = checkLogin();
- await getHttpUserClient().putAvatar(username, blob, user.token);
+ await getHttpUserClient().putAvatar(username, blob);
this._avatarHub.getLine(username)?.next({ data: blob, type: "synced" });
}
async setNickname(username: string, nickname: string): Promise<void> {
- const user = checkLogin();
return getHttpUserClient()
- .patch(username, { nickname }, user.token)
+ .patch(username, { nickname })
.then((user) => {
this.saveUser(user);
});