Skip to content

Commit

Permalink
post-info route
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Apr 12, 2022
1 parent f469d99 commit 8292db3
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
27 changes: 26 additions & 1 deletion common/src/microblog/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import axios, { AxiosResponse } from 'axios'
import TID from '../repo/tid.js'

import { z } from 'zod'
import { Post, Like, schema, AccountInfo, Timeline } from './types.js'
import {
Post,
Like,
schema,
AccountInfo,
Timeline,
TimelinePost,
} from './types.js'
import { schema as repoSchema } from '../repo/types.js'
import * as check from '../common/check.js'
import { assureAxiosError } from '../network/util.js'
Expand Down Expand Up @@ -81,6 +88,24 @@ export class MicroblogReader {
}
}

async getPostInfo(username: string, tid: TID): Promise<TimelinePost | null> {
const { hostUrl } = this.normalizeUsername(username)
const did = await this.resolveDid(username)
const params = { did, namespace: this.namespace, tid: tid.toString() }
try {
const res = await axios.get(`${hostUrl}/indexer/post-info`, {
params,
})
return check.assure(schema.timelinePost, res.data)
} catch (e) {
const err = assureAxiosError(e)
if (err.response?.status === 404) {
return null
}
throw new Error(err.message)
}
}

async getPostFromUser(username: string, tid: TID): Promise<Post | null> {
const { hostUrl } = this.normalizeUsername(username)
const did = await this.resolveDid(username)
Expand Down
31 changes: 29 additions & 2 deletions server/src/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Like, Post, Timeline, AccountInfo } from '@bluesky/common'
import {
Like,
Post,
Timeline,
AccountInfo,
TimelinePost,
} from '@bluesky/common'
import { Follow } from '@bluesky/common/dist/repo/types'
import knex from 'knex'
import { CID } from 'multiformats'
Expand Down Expand Up @@ -248,6 +254,27 @@ export class Database {
// INDEXER
// -----------

async retrievePostInfo(
tid: string,
author: string,
namespace: string,
): Promise<TimelinePost | null> {
const row = await this.db('posts')
.join('user_dids', 'posts.author', '=', 'user_dids.did')
.select('*')
.where({ tid, author, namespace })
if (row.length < 1) return null
const p = row[0]
return {
tid: p.tid,
author: p.author,
author_name: `${p.username}@${p.host}`,
text: p.text,
time: p.time,
likes: await this.likeCount(p.author, p.namespace, p.tid),
}
}

async retrieveFeed(
user: string,
count: number,
Expand All @@ -270,7 +297,7 @@ export class Database {
feed.map(async (p) => ({
tid: p.tid,
author: p.author,
author_name: username,
author_name: `${p.username}@${p.host}`,
text: p.text,
time: p.time,
likes: await this.likeCount(p.author, p.namespace, p.tid),
Expand Down
2 changes: 2 additions & 0 deletions server/src/routes/indexer/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express'
import Timeline from './timeline.js'
import Feed from './feed.js'
import PostInfo from './post-info.js'
import Count from './count.js'
import Followers from './followers.js'
import AccountInfo from './account-info.js'
Expand All @@ -9,6 +10,7 @@ const router = express.Router()

router.use('/timeline', Timeline)
router.use('/feed', Feed)
router.use('/post-info', PostInfo)
router.use('/count', Count)
router.use('/followers', Followers)
router.use('/account-info', AccountInfo)
Expand Down
26 changes: 26 additions & 0 deletions server/src/routes/indexer/post-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import express from 'express'
import { z } from 'zod'
import { schema } from '@bluesky/common'
import * as util from '../../util.js'
import { ServerError } from '../../error.js'

const router = express.Router()

export const getPostInfoReq = z.object({
did: z.string(),
namespace: z.string(),
tid: schema.repo.strToTid,
})
export type getPostInfoReq = z.infer<typeof getPostInfoReq>

router.get('/', async (req, res) => {
const { did, namespace, tid } = util.checkReqBody(req.query, getPostInfoReq)
const db = util.getDB(res)
const post = await db.retrievePostInfo(tid.toString(), did, namespace)
if (!post) {
throw new ServerError(404, 'Could not find post')
}
res.status(200).send(post)
})

export default router

0 comments on commit 8292db3

Please sign in to comment.