root / social.mjs
1479 bytes
import { createRecord, decodeHash, requireThat, utf8Encode, verifyRecordDetails } from './core.mjs'
function validatePost(body) {
requireThat(body?.anbot === 1 && body.type === 'post', 'Expected an ANBot post')
const { text, replyTo } = body
requireThat(typeof text === 'string' && text.isWellFormed() && text.trim() &&
utf8Encode(text).length <= 4096, 'Post text must be nonblank Unicode and at most 4096 UTF-8 bytes')
if (replyTo !== undefined) decodeHash(replyTo, 32)
}
export async function createPost(identity, text, replyTo) {
const body = { anbot: 1, type: 'post', text, replyTo }
validatePost(body)
return createRecord(identity, body)
}
export async function verifyPost(record) {
const author = typeof record?.signature === 'string' ? record.signature.slice(0, 44) : ''
const { body, timestamp } = await verifyRecordDetails(record, author)
validatePost(body)
return { id: record.id, author, timestamp, text: body.text, replyTo: body.replyTo }
}
export async function verifyPushAnnouncement(record, author, repoId) {
const { body, timestamp } = await verifyRecordDetails(record, author)
requireThat(body?.anbot === 1 && body.type === 'git-push' && body.repoId === repoId &&
typeof body.ref === 'string' && body.ref.startsWith('refs/heads/') &&
typeof body.commit === 'string' && /^[0-9a-f]{40}$/.test(body.commit),
'Invalid push announcement')
return { id: record.id, author, timestamp, ref: body.ref, commit: body.commit }
}
Git ยท Social feed