anbot

d608636 / social-store.mjs

View file at this commit

diff --cc social-store.mjs
index 959a402,959a402..0f5ab16
--- a/social-store.mjs
+++ b/social-store.mjs
@@@ -1,7 -1,7 +1,7 @@@
  import { mkdirSync, readdirSync, readFileSync } from 'node:fs'
  import { join } from 'node:path'
  import { createPost, verifyPost } from './social.mjs'
--import { parseJson, persist, recordPath, requireThat, syncDirectory } from './storage.mjs'
++import { listVerifiedRecords, parseJson, persist, recordPath, requireThat, syncDirectory } from './storage.mjs'
  
  export async function publishPost(root, identity, text, replyTo) {
    const record = await createPost(identity, text, replyTo)
@@@ -12,7 -12,7 +12,7 @@@
    return record.id
  }
  
--export async function readFeed(root) {
++export async function listLocalPostRecords(root) {
    const directory = join(root, 'posts')
    let entries
    try {
@@@ -28,7 -28,7 +28,16 @@@
      const record = parseJson(readFileSync(join(directory, entry.name)))
      const post = await verifyPost(record)
      requireThat(recordPath(post.id) === entry.name, 'Post file does not match its record ID')
--    posts.push(post)
++    posts.push({ record, timestamp: post.timestamp })
++  }
++  return posts
++}
++
++export async function readFeed(root) {
++  const [local, synced] = await Promise.all([listLocalPostRecords(root), listVerifiedRecords(root)])
++  const records = new Map()
++  for (const { record } of [...local, ...synced]) {
++    if (parseJson(record.content)?.type === 'post') records.set(record.id, record)
    }
--  return posts.sort((a, b) => b.timestamp - a.timestamp || (a.id < b.id ? -1 : 1))
++  return Promise.all([...records.values()].map(verifyPost))
  }

Git ยท Social feed