Sharkey/packages/backend/src/server/api/endpoints/page-push.ts

52 lines
1 KiB
TypeScript
Raw Normal View History

import $ from 'cafy';
import define from '../define';
import { ID } from '@/misc/cafy-id';
import { publishMainStream } from '@/services/stream';
import { Users, Pages } from '@/models/index';
import { ApiError } from '../error';
export const meta = {
2020-02-15 14:33:32 +02:00
requireCredential: true as const,
secure: true,
params: {
pageId: {
2021-12-09 16:58:30 +02:00
validator: $.type(ID),
},
event: {
2021-12-09 16:58:30 +02:00
validator: $.str,
},
var: {
2021-12-09 16:58:30 +02:00
validator: $.optional.nullable.any,
},
},
errors: {
noSuchPage: {
message: 'No such page.',
code: 'NO_SUCH_PAGE',
2021-12-09 16:58:30 +02:00
id: '4a13ad31-6729-46b4-b9af-e86b265c2e74',
},
},
};
2022-01-02 19:12:50 +02:00
// eslint-disable-next-line import/no-default-export
export default define(meta, async (ps, user) => {
const page = await Pages.findOne(ps.pageId);
if (page == null) {
throw new ApiError(meta.errors.noSuchPage);
}
2019-07-09 10:55:33 +03:00
publishMainStream(page.userId, 'pageEvent', {
pageId: ps.pageId,
event: ps.event,
var: ps.var,
2019-07-09 10:55:55 +03:00
userId: user.id,
user: await Users.pack(user.id, { id: page.userId }, {
2021-12-09 16:58:30 +02:00
detail: true,
}),
});
});