From 06649bfa22589e0cbbf93c371e4a3cd645515e68 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 23 Aug 2020 23:10:20 +0800 Subject: Post list is now patched. --- Timeline/ClientApp/src/app/data/timeline.ts | 80 +++++++++++++++++++++++++---- Timeline/ClientApp/src/app/http/timeline.ts | 2 +- 2 files changed, 70 insertions(+), 12 deletions(-) (limited to 'Timeline/ClientApp/src') diff --git a/Timeline/ClientApp/src/app/data/timeline.ts b/Timeline/ClientApp/src/app/data/timeline.ts index fc554f7b..216d903c 100644 --- a/Timeline/ClientApp/src/app/data/timeline.ts +++ b/Timeline/ClientApp/src/app/data/timeline.ts @@ -306,20 +306,78 @@ export class TimelineService { } } + const now = new Date(); + + const lastUpdatedTime = await dataStorage.getItem( + `timeline.${timelineName}.lastUpdated` + ); + try { - const httpPosts = await getHttpTimelineClient().listPost( - timelineName, - userService.currentUser?.token - ); + if (lastUpdatedTime == null) { + const httpPosts = await getHttpTimelineClient().listPost( + timelineName, + userService.currentUser?.token + ); + + uniqBy( + httpPosts.map((post) => post.author), + 'username' + ).forEach((user) => void userInfoService.saveUser(user)); + + const posts = this.convertHttpPostToDataList(httpPosts); + await this.savePosts(timelineName, posts); + await dataStorage.setItem( + `timeline.${timelineName}.lastUpdated`, + now + ); + + line.endSyncAndNext({ type: 'synced', posts }); + } else { + const httpPosts = await getHttpTimelineClient().listPost( + timelineName, + userService.currentUser?.token, + { + modifiedSince: lastUpdatedTime, + includeDeleted: true, + } + ); + + const deletedIds = httpPosts.filter((p) => p.deleted).map((p) => p.id); + const changed = httpPosts.filter( + (p): p is HttpTimelinePostInfo => !p.deleted + ); + + uniqBy( + httpPosts + .map((post) => post.author) + .filter((u): u is HttpUser => u != null), + 'username' + ).forEach((user) => void userInfoService.saveUser(user)); + + const cache = (await this.getCachedPosts(timelineName)) ?? []; - uniqBy( - httpPosts.map((post) => post.author), - 'username' - ).forEach((user) => void userInfoService.saveUser(user)); + const posts = cache.filter((p) => !deletedIds.includes(p.id)); - const posts = this.convertHttpPostToDataList(httpPosts); - await this.savePosts(timelineName, posts); - line.endSyncAndNext({ type: 'synced', posts }); + for (const changedPost of changed) { + const savedChangedPostIndex = posts.findIndex( + (p) => p.id === changedPost.id + ); + if (savedChangedPostIndex === -1) { + posts.push(this.convertHttpPostToData(changedPost)); + } else { + posts[savedChangedPostIndex] = this.convertHttpPostToData( + changedPost + ); + } + } + + await this.savePosts(timelineName, posts); + await dataStorage.setItem( + `timeline.${timelineName}.lastUpdated`, + now + ); + line.endSyncAndNext({ type: 'synced', posts }); + } } catch (e) { if (e instanceof HttpTimelineNotExistError) { line.endSyncAndNext({ type: 'notexist', posts: [] }); diff --git a/Timeline/ClientApp/src/app/http/timeline.ts b/Timeline/ClientApp/src/app/http/timeline.ts index d8f0b49c..c4ebdee9 100644 --- a/Timeline/ClientApp/src/app/http/timeline.ts +++ b/Timeline/ClientApp/src/app/http/timeline.ts @@ -66,7 +66,7 @@ export interface HttpTimelineDeletedPostInfo { id: number; time: Date; lastUpdated: Date; - author: HttpUser; + author?: HttpUser; deleted: true; } -- cgit v1.2.3