2018-12-21 03:54:39 +01:00
|
|
|
import { Feed } from 'feed';
|
2022-04-17 14:18:18 +02:00
|
|
|
import { In, IsNull } from 'typeorm';
|
2022-02-27 03:07:39 +01:00
|
|
|
import config from '@/config/index.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
2022-04-17 14:18:18 +02:00
|
|
|
import { Notes, DriveFiles, UserProfiles, Users } from '@/models/index.js';
|
2018-12-21 03:54:39 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
export default async function(user: User) {
|
2019-07-02 12:20:34 +02:00
|
|
|
const author = {
|
2018-12-21 03:54:39 +01:00
|
|
|
link: `${config.url}/@${user.username}`,
|
2021-12-09 15:58:30 +01:00
|
|
|
name: user.name || user.username,
|
2018-12-21 03:54:39 +01:00
|
|
|
};
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
|
2019-04-10 08:04:27 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
const notes = await Notes.find({
|
|
|
|
where: {
|
|
|
|
userId: user.id,
|
2022-03-26 07:34:00 +01:00
|
|
|
renoteId: IsNull(),
|
2021-12-09 15:58:30 +01:00
|
|
|
visibility: In(['public', 'home']),
|
2019-04-07 14:50:36 +02:00
|
|
|
},
|
|
|
|
order: { createdAt: -1 },
|
2021-12-09 15:58:30 +01:00
|
|
|
take: 20,
|
2018-12-21 03:54:39 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const feed = new Feed({
|
|
|
|
id: author.link,
|
|
|
|
title: `${author.name} (@${user.username}@${config.host})`,
|
|
|
|
updated: notes[0].createdAt,
|
2022-07-19 19:13:17 +02:00
|
|
|
generator: 'Calckey',
|
2021-11-07 10:04:32 +01:00
|
|
|
description: `${user.notesCount} Notes, ${profile.ffVisibility === 'public' ? user.followingCount : '?'} Following, ${profile.ffVisibility === 'public' ? user.followersCount : '?'} Followers${profile.description ? ` · ${profile.description}` : ''}`,
|
2018-12-21 03:54:39 +01:00
|
|
|
link: author.link,
|
2022-04-17 14:18:18 +02:00
|
|
|
image: await Users.getAvatarUrl(user),
|
2018-12-21 03:54:39 +01:00
|
|
|
feedLinks: {
|
|
|
|
json: `${author.link}.json`,
|
|
|
|
atom: `${author.link}.atom`,
|
|
|
|
},
|
2019-07-02 12:20:34 +02:00
|
|
|
author,
|
2021-12-09 15:58:30 +01:00
|
|
|
copyright: user.name || user.username,
|
2019-07-02 12:20:34 +02:00
|
|
|
});
|
2018-12-21 03:54:39 +01:00
|
|
|
|
|
|
|
for (const note of notes) {
|
2022-03-26 07:34:00 +01:00
|
|
|
const files = note.fileIds.length > 0 ? await DriveFiles.findBy({
|
2021-12-09 15:58:30 +01:00
|
|
|
id: In(note.fileIds),
|
2019-04-07 14:50:36 +02:00
|
|
|
}) : [];
|
|
|
|
const file = files.find(file => file.type.startsWith('image/'));
|
2018-12-21 03:54:39 +01:00
|
|
|
|
|
|
|
feed.addItem({
|
|
|
|
title: `New note by ${author.name}`,
|
2019-04-07 14:50:36 +02:00
|
|
|
link: `${config.url}/notes/${note.id}`,
|
2018-12-21 03:54:39 +01:00
|
|
|
date: note.createdAt,
|
2019-04-12 18:43:22 +02:00
|
|
|
description: note.cw || undefined,
|
|
|
|
content: note.text || undefined,
|
2021-12-09 15:58:30 +01:00
|
|
|
image: file ? DriveFiles.getPublicUrl(file) || undefined : undefined,
|
2018-12-21 03:54:39 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return feed;
|
|
|
|
}
|