diff options
author | crupest <crupest@outlook.com> | 2020-08-23 23:32:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-23 23:32:42 +0800 |
commit | e9e0888e9f8a204a29109cb1bb6950c1d72599d6 (patch) | |
tree | 1ca45c1c7f5655d820c4637e561f6a95d68af3bc | |
parent | 07795ebbd2b2a48ab0cebe97b6d591d35ef0cd31 (diff) | |
parent | 06649bfa22589e0cbbf93c371e4a3cd645515e68 (diff) | |
download | timeline-e9e0888e9f8a204a29109cb1bb6950c1d72599d6.tar.gz timeline-e9e0888e9f8a204a29109cb1bb6950c1d72599d6.tar.bz2 timeline-e9e0888e9f8a204a29109cb1bb6950c1d72599d6.zip |
Merge pull request #151 from crupest/patch-posts
Patch post list instead of fetch the full one.
-rw-r--r-- | Timeline/ClientApp/src/app/data/timeline.ts | 80 | ||||
-rw-r--r-- | Timeline/ClientApp/src/app/http/timeline.ts | 2 |
2 files changed, 70 insertions, 12 deletions
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<Date | null>(
+ `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<Date>(
+ `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<Date>(
+ `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;
}
|