Spaces:
Paused
Paused
Download apps/api/src/modules/social.js from nenbuj/we-superapp: direct link, hf CLI and curl.
- Browser
- Download file 1.41 kB
-
https://huggingface.co/spaces/nenbuj/we-superapp/resolve/main/apps/api/src/modules/social.js
- Command line
-
hf download hf://spaces/nenbuj/we-superapp/apps/api/src/modules/social.js
-
curl -L -o social.js https://huggingface.co/spaces/nenbuj/we-superapp/resolve/main/apps/api/src/modules/social.js
1.41 kB
| import crypto from 'node:crypto'; | |
| import { db } from '../core/db.js'; | |
| import { bus } from '../core/bus.js'; | |
| import { requireMember } from '../core/guards.js'; | |
| function sendError(reply, status, code, message) { | |
| return reply.code(status).send({ error: { code, message } }); | |
| } | |
| export default async function socialRoutes(fastify) { | |
| fastify.get('/spaces/:id/posts', async (request) => { | |
| await requireMember(request, request.params.id); | |
| const posts = db | |
| .prepare('SELECT * FROM posts WHERE space_id = ? ORDER BY created_at DESC') | |
| .all(request.params.id); | |
| return { posts }; | |
| }); | |
| fastify.post('/spaces/:id/posts', async (request, reply) => { | |
| await requireMember(request, request.params.id); | |
| const spaceId = request.params.id; | |
| const { text } = request.body || {}; | |
| if (!text || typeof text !== 'string') { | |
| return sendError(reply, 400, 'VALIDATION', 'text обязателен'); | |
| } | |
| const post = { | |
| id: crypto.randomUUID(), | |
| space_id: spaceId, | |
| author_id: request.user.id, | |
| text, | |
| created_at: Date.now(), | |
| }; | |
| db.prepare('INSERT INTO posts (id, space_id, author_id, text, created_at) VALUES (?,?,?,?,?)') | |
| .run(post.id, post.space_id, post.author_id, post.text, post.created_at); | |
| bus.emit(spaceId, { type: 'post.created', space_id: spaceId, payload: post }); | |
| return reply.code(201).send({ post }); | |
| }); | |
| } | |