2023-11-26 21:33:46 +01:00
|
|
|
import { Feed } from "feed";
|
|
|
|
import { In, IsNull } from "typeorm";
|
2024-04-19 19:07:08 +02:00
|
|
|
import { config } from "@/config.js";
|
2023-12-05 08:12:10 +01:00
|
|
|
import type { User } from "@/models/entities/user.js";
|
2024-04-14 10:34:33 +02:00
|
|
|
import type { Note } from "@/models/entities/note.js";
|
2023-12-05 08:12:10 +01:00
|
|
|
import { Notes, DriveFiles, UserProfiles, Users } from "@/models/index.js";
|
2024-04-14 10:34:33 +02:00
|
|
|
import getNoteHtml from "@/remote/activitypub/misc/get-note-html.js";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If there is this part in the note, it will cause CDATA to be terminated early.
|
|
|
|
*/
|
|
|
|
function escapeCDATA(str: string) {
|
2024-04-14 10:44:12 +02:00
|
|
|
return str.replaceAll("]]>", "]]]]><![CDATA[>");
|
2024-04-14 10:34:33 +02:00
|
|
|
}
|
2018-12-21 03:54:39 +01:00
|
|
|
|
2023-07-03 08:08:33 +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}`,
|
2024-04-14 10:34:33 +02:00
|
|
|
name: escapeCDATA(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,
|
2023-07-03 08:08:33 +02:00
|
|
|
visibility: In(["public", "home"]),
|
2023-07-02 05:58:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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-03 00:18:30 +02:00
|
|
|
generator: "Firefish",
|
2024-04-14 10:34:33 +02:00
|
|
|
description: escapeCDATA(
|
|
|
|
`${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) {
|
2023-07-03 08:08:33 +02:00
|
|
|
title += "renotes";
|
2023-07-02 05:58:36 +02:00
|
|
|
} else if (note.replyId) {
|
2023-07-03 08:08:33 +02:00
|
|
|
title += "replies";
|
2023-07-02 05:58:36 +02:00
|
|
|
} else {
|
2023-07-03 08:08:33 +02:00
|
|
|
title += "says";
|
2023-07-02 05:58:36 +02:00
|
|
|
}
|
|
|
|
if (noteintitle) {
|
|
|
|
const content = note.cw ?? note.text;
|
|
|
|
if (content) {
|
|
|
|
title += `: ${content}`;
|
|
|
|
} else {
|
2023-07-03 08:08:33 +02:00
|
|
|
title += "something";
|
2023-07-02 05:58:36 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-21 03:54:39 +01:00
|
|
|
|
|
|
|
feed.addItem({
|
2024-04-14 10:34:33 +02:00
|
|
|
title: escapeCDATA(
|
|
|
|
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-03 08:08:33 +02:00
|
|
|
description: note.cw
|
2024-04-14 10:34:33 +02:00
|
|
|
? escapeCDATA(note.cw.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, ""))
|
2023-07-03 08:08:33 +02:00
|
|
|
: undefined,
|
2024-04-14 10:34:33 +02:00
|
|
|
content: escapeCDATA(
|
|
|
|
contentStr.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, ""),
|
|
|
|
),
|
2018-12-21 03:54:39 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-04-14 10:34:33 +02:00
|
|
|
async function noteToString(note: Note, isTheNote = false) {
|
2023-07-03 08:08:33 +02:00
|
|
|
const author = isTheNote
|
|
|
|
? null
|
|
|
|
: await Users.findOneBy({ id: note.userId });
|
|
|
|
let outstr = author
|
|
|
|
? `${author.name}(@${author.username}@${
|
|
|
|
author.host ? author.host : config.host
|
2024-03-28 06:25:33 +01:00
|
|
|
}) ${
|
2023-07-03 08:08:33 +02:00
|
|
|
note.renoteId ? "renotes" : note.replyId ? "replies" : "says"
|
2024-03-28 06:25:33 +01:00
|
|
|
}: <br>`
|
2023-07-03 08:08:33 +02:00
|
|
|
: "";
|
2023-01-13 05:40:33 +01:00
|
|
|
const files =
|
|
|
|
note.fileIds.length > 0
|
|
|
|
? await DriveFiles.findBy({
|
|
|
|
id: In(note.fileIds),
|
2024-03-28 06:25:33 +01:00
|
|
|
})
|
2023-01-13 05:40:33 +01:00
|
|
|
: [];
|
2023-07-03 08:08:33 +02:00
|
|
|
let fileEle = "";
|
2023-07-02 05:58:36 +02:00
|
|
|
for (const file of files) {
|
2023-07-03 08:08:33 +02:00
|
|
|
if (file.type.startsWith("image/")) {
|
2023-07-02 05:58:36 +02:00
|
|
|
fileEle += ` <br><img src="${DriveFiles.getPublicUrl(file)}">`;
|
2023-07-03 08:08:33 +02:00
|
|
|
} 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}">`;
|
2023-07-02 05:58:36 +02:00
|
|
|
} else {
|
2023-07-03 08:08:33 +02:00
|
|
|
fileEle += ` <br><a href="${DriveFiles.getPublicUrl(file)}" download="${
|
|
|
|
file.name
|
|
|
|
}">${file.name}</a>`;
|
2023-07-02 05:58:36 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-14 10:34:33 +02:00
|
|
|
|
|
|
|
outstr += `${note.cw ? note.cw + "<br>" : ""}${
|
|
|
|
getNoteHtml(note) || ""
|
|
|
|
}${fileEle}`;
|
2023-07-02 05:58:36 +02:00
|
|
|
if (isTheNote) {
|
2023-07-03 08:08:33 +02:00
|
|
|
outstr += ` <span class="${
|
|
|
|
note.renoteId ? "renote_note" : note.replyId ? "reply_note" : "new_note"
|
|
|
|
} ${
|
|
|
|
fileEle.indexOf("img src") !== -1 ? "with_img" : "without_img"
|
|
|
|
}"></span>`;
|
2023-07-02 05:58:36 +02:00
|
|
|
}
|
|
|
|
return outstr;
|
|
|
|
}
|
2018-12-21 03:54:39 +01:00
|
|
|
|
2023-07-03 08:08:33 +02:00
|
|
|
async function findById(id) {
|
|
|
|
let text = "";
|
2023-07-02 05:58:36 +02:00
|
|
|
let next = null;
|
2023-07-03 08:08:33 +02:00
|
|
|
const findings = await Notes.findOneBy({
|
|
|
|
id: id,
|
|
|
|
visibility: In(["public", "home"]),
|
2018-12-21 03:54:39 +01:00
|
|
|
});
|
2023-07-02 05:58:36 +02:00
|
|
|
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;
|
|
|
|
}
|