2022-02-27 03:07:39 +01:00
|
|
|
import Router from '@koa/router';
|
|
|
|
import config from '@/config/index.js';
|
|
|
|
import { renderActivity } from '@/remote/activitypub/renderer/index.js';
|
|
|
|
import renderOrderedCollection from '@/remote/activitypub/renderer/ordered-collection.js';
|
|
|
|
import { setResponseType } from '../activitypub.js';
|
|
|
|
import renderNote from '@/remote/activitypub/renderer/note.js';
|
|
|
|
import { Users, Notes, UserNotePinings } from '@/models/index.js';
|
2022-03-20 21:21:37 +01:00
|
|
|
import { userCache } from './cache.js';
|
2018-09-18 06:08:27 +02:00
|
|
|
|
2019-09-26 22:50:34 +02:00
|
|
|
export default async (ctx: Router.RouterContext) => {
|
2019-04-07 14:50:36 +02:00
|
|
|
const userId = ctx.params.user;
|
2018-09-18 06:08:27 +02:00
|
|
|
|
2022-03-20 21:21:37 +01:00
|
|
|
// TODO: typeorm 3.0にしたら .then(x => x || null) は消せる
|
|
|
|
const user = await userCache.fetch(userId, () => Users.findOne({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: userId,
|
2021-12-09 15:58:30 +01:00
|
|
|
host: null,
|
2022-03-20 21:21:37 +01:00
|
|
|
}).then(x => x || null));
|
2018-09-18 06:08:27 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (user == null) {
|
2018-09-18 06:08:27 +02:00
|
|
|
ctx.status = 404;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-12 02:37:00 +02:00
|
|
|
const pinings = await UserNotePinings.find({
|
|
|
|
where: { userId: user.id },
|
2021-12-09 15:58:30 +01:00
|
|
|
order: { id: 'DESC' },
|
2019-05-12 02:37:00 +02:00
|
|
|
});
|
2018-09-18 06:08:27 +02:00
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
const pinnedNotes = await Promise.all(pinings.map(pining =>
|
2021-02-13 07:33:38 +01:00
|
|
|
Notes.findOneOrFail(pining.noteId)));
|
2018-09-18 06:08:27 +02:00
|
|
|
|
|
|
|
const renderedNotes = await Promise.all(pinnedNotes.map(note => renderNote(note)));
|
|
|
|
|
|
|
|
const rendered = renderOrderedCollection(
|
|
|
|
`${config.url}/users/${userId}/collections/featured`,
|
2022-02-03 13:20:25 +01:00
|
|
|
renderedNotes.length, undefined, undefined, renderedNotes,
|
2018-09-18 06:08:27 +02:00
|
|
|
);
|
|
|
|
|
2019-01-30 18:29:36 +01:00
|
|
|
ctx.body = renderActivity(rendered);
|
2020-05-15 14:37:09 +02:00
|
|
|
ctx.set('Cache-Control', 'public, max-age=180');
|
2018-09-18 06:08:27 +02:00
|
|
|
setResponseType(ctx);
|
|
|
|
};
|