2023-01-13 05:40:33 +01:00
|
|
|
import { Feed } from "feed";
|
|
|
|
import { In, IsNull } from "typeorm";
|
|
|
|
import config from "@/config/index.js";
|
|
|
|
import type { User } from "@/models/entities/user.js";
|
|
|
|
import { Notes, DriveFiles, UserProfiles, Users } from "@/models/index.js";
|
2018-12-21 03:54:39 +01:00
|
|
|
|
2023-07-02 05:58:36 +02:00
|
|
|
export default async function (user: User, threadDepth = 5, history = 20, noteintitle = false, renotes = true, replies = true) {
|
2019-07-02 12:20:34 +02:00
|
|
|
const author = {
|
2018-12-21 03:54:39 +01:00
|
|
|
link: `${config.url}/@${user.username}`,
|
2023-07-02 05:58:36 +02:00
|
|
|
email: `${user.username}@${config.host}`,
|
|
|
|
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
|
|
|
|
2023-07-02 05:58:36 +02:00
|
|
|
const searchCriteria = {
|
|
|
|
userId: user.id,
|
|
|
|
visibility: In(['public', 'home']),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!renotes) {
|
|
|
|
searchCriteria.renoteId = IsNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!replies) {
|
|
|
|
searchCriteria.replyId = IsNull();
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
const notes = await Notes.find({
|
2023-07-02 05:58:36 +02:00
|
|
|
where: searchCriteria,
|
2019-04-07 14:50:36 +02:00
|
|
|
order: { createdAt: -1 },
|
2023-07-02 05:58:36 +02:00
|
|
|
take: history,
|
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,
|
2023-07-02 05:58:36 +02:00
|
|
|
generator: 'Calckey',
|
|
|
|
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) {
|
2023-07-02 05:58:36 +02:00
|
|
|
let contentStr = await noteToString(note, true);
|
|
|
|
let next = note.renoteId ? note.renoteId : note.replyId;
|
|
|
|
let depth = threadDepth;
|
|
|
|
while (depth > 0 && next) {
|
|
|
|
const finding = await findById(next);
|
|
|
|
contentStr += finding.text;
|
|
|
|
next = finding.next;
|
|
|
|
depth -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let title = `${author.name} `;
|
|
|
|
if (note.renoteId) {
|
|
|
|
title += 'renotes';
|
|
|
|
} else if (note.replyId) {
|
|
|
|
title += 'replies';
|
|
|
|
} else {
|
|
|
|
title += 'says';
|
|
|
|
}
|
|
|
|
if (noteintitle) {
|
|
|
|
const content = note.cw ?? note.text;
|
|
|
|
if (content) {
|
|
|
|
title += `: ${content}`;
|
|
|
|
} else {
|
|
|
|
title += 'something';
|
|
|
|
}
|
|
|
|
}
|
2018-12-21 03:54:39 +01:00
|
|
|
|
|
|
|
feed.addItem({
|
2023-07-02 05:58:36 +02:00
|
|
|
title: title.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '').substring(0,100),
|
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,
|
2023-07-02 05:58:36 +02:00
|
|
|
description: note.cw ? note.cw.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '') : undefined,
|
|
|
|
content: contentStr.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '')
|
2018-12-21 03:54:39 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-07-02 05:58:36 +02:00
|
|
|
async function noteToString (note, isTheNote = false) {
|
|
|
|
const author = isTheNote ? null : await Users.findOneBy({ id: note.userId });
|
|
|
|
let outstr = author ? `${author.name}(@${author.username}@${author.host ? author.host : config.host}) ${(note.renoteId ? 'renotes' : (note.replyId ? 'replies' : 'says'))}: <br>` : '';
|
|
|
|
const files = note.fileIds.length > 0 ? await DriveFiles.findBy({
|
|
|
|
id: In(note.fileIds),
|
|
|
|
}) : [];
|
|
|
|
let fileEle = '';
|
|
|
|
for (const file of files) {
|
|
|
|
if (file.type.startsWith('image/')) {
|
|
|
|
fileEle += ` <br><img src="${DriveFiles.getPublicUrl(file)}">`;
|
|
|
|
} else if (file.type.startsWith('audio/')) {
|
|
|
|
fileEle += ` <br><audio controls src="${DriveFiles.getPublicUrl(file)}" type="${file.type}">`;
|
|
|
|
} else if (file.type.startsWith('video/')) {
|
|
|
|
fileEle += ` <br><video controls src="${DriveFiles.getPublicUrl(file)}" type="${file.type}">`;
|
|
|
|
} else {
|
|
|
|
fileEle += ` <br><a href="${DriveFiles.getPublicUrl(file)}" download="${file.name}">${file.name}</a>`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
outstr += `${note.cw ? note.cw + '<br>' : ''}${note.text || ''}${fileEle}`;
|
|
|
|
if (isTheNote) {
|
|
|
|
outstr += ` <span class="${(note.renoteId ? 'renote_note' : (note.replyId ? 'reply_note' : 'new_note'))} ${(fileEle.indexOf('img src') !== -1 ? 'with_img' : 'without_img')}"></span>`;
|
|
|
|
}
|
|
|
|
return outstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function findById (id) {
|
|
|
|
let text = '';
|
|
|
|
let next = null;
|
|
|
|
const findings = await Notes.findOneBy({ id: id, visibility: In(['public', 'home']) });
|
|
|
|
if (findings) {
|
|
|
|
text += `<hr>`;
|
|
|
|
text += await noteToString(findings);
|
|
|
|
next = findings.renoteId ? findings.renoteId : findings.replyId;
|
|
|
|
}
|
|
|
|
return { text, next };
|
|
|
|
}
|
|
|
|
|
2018-12-21 03:54:39 +01:00
|
|
|
return feed;
|
|
|
|
}
|