about summary refs log tree commit diff
path: root/src/state/models/cache/posts.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/state/models/cache/posts.ts')
-rw-r--r--src/state/models/cache/posts.ts31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/state/models/cache/posts.ts b/src/state/models/cache/posts.ts
new file mode 100644
index 000000000..48621226a
--- /dev/null
+++ b/src/state/models/cache/posts.ts
@@ -0,0 +1,31 @@
+import {LRUMap} from 'lru_map'
+import {RootStoreModel} from '../root-store'
+import {AppBskyFeedDefs} from '@atproto/api'
+
+type PostView = AppBskyFeedDefs.PostView
+
+export class PostsCache {
+  cache: LRUMap<string, PostView> = new LRUMap(500)
+
+  constructor(public rootStore: RootStoreModel) {}
+
+  set(uri: string, postView: PostView) {
+    this.cache.set(uri, postView)
+    if (postView.author.handle) {
+      this.rootStore.handleResolutions.cache.set(
+        postView.author.handle,
+        postView.author.did,
+      )
+    }
+  }
+
+  fromFeedItem(feedItem: AppBskyFeedDefs.FeedViewPost) {
+    this.set(feedItem.post.uri, feedItem.post)
+    if (
+      feedItem.reply?.parent &&
+      AppBskyFeedDefs.isPostView(feedItem.reply?.parent)
+    ) {
+      this.set(feedItem.reply.parent.uri, feedItem.reply.parent)
+    }
+  }
+}