aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Timeline/ClientApp/src/app/data/timeline.ts80
-rw-r--r--Timeline/ClientApp/src/app/http/timeline.ts2
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;
}