Embedded all attachment, renotes and discussion history into rss feed content & improve title, and not generate feed for locked account

This commit is contained in:
CGsama 2023-07-01 23:58:36 -04:00
parent c553583d02
commit 78a479312c
2 changed files with 106 additions and 33 deletions

View file

@ -4,34 +4,40 @@ import config from "@/config/index.js";
import type { User } from "@/models/entities/user.js"; import type { User } from "@/models/entities/user.js";
import { Notes, DriveFiles, UserProfiles, Users } from "@/models/index.js"; import { Notes, DriveFiles, UserProfiles, Users } from "@/models/index.js";
export default async function (user: User) { export default async function (user: User, threadDepth = 5, history = 20, noteintitle = false, renotes = true, replies = true) {
const author = { const author = {
link: `${config.url}/@${user.username}`, link: `${config.url}/@${user.username}`,
name: user.name || user.username, email: `${user.username}@${config.host}`,
name: user.name || user.username
}; };
const profile = await UserProfiles.findOneByOrFail({ userId: user.id }); const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
const searchCriteria = {
userId: user.id,
visibility: In(['public', 'home']),
};
if (!renotes) {
searchCriteria.renoteId = IsNull();
}
if (!replies) {
searchCriteria.replyId = IsNull();
}
const notes = await Notes.find({ const notes = await Notes.find({
where: { where: searchCriteria,
userId: user.id,
renoteId: IsNull(),
visibility: In(["public", "home"]),
},
order: { createdAt: -1 }, order: { createdAt: -1 },
take: 20, take: history,
}); });
const feed = new Feed({ const feed = new Feed({
id: author.link, id: author.link,
title: `${author.name} (@${user.username}@${config.host})`, title: `${author.name} (@${user.username}@${config.host})`,
updated: notes[0].createdAt, updated: notes[0].createdAt,
generator: "Calckey", generator: 'Calckey',
description: `${user.notesCount} Notes, ${ description: `${user.notesCount} Notes, ${profile.ffVisibility === 'public' ? user.followingCount : '?'} Following, ${profile.ffVisibility === 'public' ? user.followersCount : '?'} Followers${profile.description ? ` · ${profile.description}` : ''}`,
profile.ffVisibility === "public" ? user.followingCount : "?"
} Following, ${
profile.ffVisibility === "public" ? user.followersCount : "?"
} Followers${profile.description ? ` · ${profile.description}` : ""}`,
link: author.link, link: author.link,
image: await Users.getAvatarUrl(user), image: await Users.getAvatarUrl(user),
feedLinks: { feedLinks: {
@ -43,23 +49,78 @@ export default async function (user: User) {
}); });
for (const note of notes) { for (const note of notes) {
const files = let contentStr = await noteToString(note, true);
note.fileIds.length > 0 let next = note.renoteId ? note.renoteId : note.replyId;
? await DriveFiles.findBy({ let depth = threadDepth;
id: In(note.fileIds), while (depth > 0 && next) {
}) const finding = await findById(next);
: []; contentStr += finding.text;
const file = files.find((file) => file.type.startsWith("image/")); 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';
}
}
feed.addItem({ feed.addItem({
title: `New note by ${author.name}`, title: title.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '').substring(0,100),
link: `${config.url}/notes/${note.id}`, link: `${config.url}/notes/${note.id}`,
date: note.createdAt, date: note.createdAt,
description: note.cw || undefined, description: note.cw ? note.cw.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '') : undefined,
content: note.text || undefined, content: contentStr.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '')
image: file ? DriveFiles.getPublicUrl(file) || undefined : undefined,
}); });
} }
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 };
}
return feed; return feed;
} }

View file

@ -247,7 +247,7 @@ router.get("/api.json", async (ctx) => {
ctx.body = genOpenapiSpec(); ctx.body = genOpenapiSpec();
}); });
const getFeed = async (acct: string) => { const getFeed = async (acct: string, threadDepth:string, historyCount:string, noteInTitle:string, noRenotes:string, noReplies:string) => {
const meta = await fetchMeta(); const meta = await fetchMeta();
if (meta.privateMode) { if (meta.privateMode) {
return; return;
@ -257,14 +257,26 @@ const getFeed = async (acct: string) => {
usernameLower: username.toLowerCase(), usernameLower: username.toLowerCase(),
host: host ?? IsNull(), host: host ?? IsNull(),
isSuspended: false, isSuspended: false,
isLocked:false,
}); });
if (!user) {
return user && (await packFeed(user)); return;
}
let thread = parseInt(threadDepth, 10);
if (isNaN(thread) || thread < 0 || thread > 30) {
thread = 3;
}
let history = parseInt(historyCount, 10);
//cant be 0 here or it will get all posts
if (isNaN(history) || history <= 0 || history > 30) {
history = 20;
}
return user && await packFeed(user, thread, history, !isNaN(noteInTitle), isNaN(noRenotes), isNaN(noReplies));
}; };
// As the /@user[.json|.rss|.atom]/sub endpoint is complicated, we will use a regex to switch between them. // As the /@user[.json|.rss|.atom]/sub endpoint is complicated, we will use a regex to switch between them.
const reUser = new RegExp( const reUser = new RegExp(
"^/@(?<user>[^/]+?)(?:.(?<feed>json|rss|atom))?(?:/(?<sub>[^/]+))?$", "^/@(?<user>[^/]+?)(?:.(?<feed>json|rss|atom)(?:\\?[^/]*)?)?(?:/(?<sub>[^/]+))?$",
); );
router.get(reUser, async (ctx, next) => { router.get(reUser, async (ctx, next) => {
const groups = reUser.exec(ctx.originalUrl)?.groups; const groups = reUser.exec(ctx.originalUrl)?.groups;
@ -275,7 +287,7 @@ router.get(reUser, async (ctx, next) => {
ctx.params = groups; ctx.params = groups;
console.log(ctx, ctx.params); //console.log(ctx, ctx.params, ctx.query);
if (groups.feed) { if (groups.feed) {
if (groups.sub) { if (groups.sub) {
await next(); await next();
@ -301,7 +313,7 @@ router.get(reUser, async (ctx, next) => {
// Atom // Atom
const atomFeed: Router.Middleware = async (ctx) => { const atomFeed: Router.Middleware = async (ctx) => {
const feed = await getFeed(ctx.params.user); const feed = await getFeed(ctx.params.user, ctx.query.thread, ctx.query.history, ctx.query.noteintitle, ctx.query.norenotes, ctx.query.noreplies);
if (feed) { if (feed) {
ctx.set("Content-Type", "application/atom+xml; charset=utf-8"); ctx.set("Content-Type", "application/atom+xml; charset=utf-8");
@ -313,7 +325,7 @@ const atomFeed: Router.Middleware = async (ctx) => {
// RSS // RSS
const rssFeed: Router.Middleware = async (ctx) => { const rssFeed: Router.Middleware = async (ctx) => {
const feed = await getFeed(ctx.params.user); const feed = await getFeed(ctx.params.user, ctx.query.thread, ctx.query.history, ctx.query.noteintitle, ctx.query.norenotes, ctx.query.noreplies);
if (feed) { if (feed) {
ctx.set("Content-Type", "application/rss+xml; charset=utf-8"); ctx.set("Content-Type", "application/rss+xml; charset=utf-8");
@ -325,7 +337,7 @@ const rssFeed: Router.Middleware = async (ctx) => {
// JSON // JSON
const jsonFeed: Router.Middleware = async (ctx) => { const jsonFeed: Router.Middleware = async (ctx) => {
const feed = await getFeed(ctx.params.user); const feed = await getFeed(ctx.params.user, ctx.query.thread, ctx.query.history, ctx.query.noteintitle, ctx.query.norenotes, ctx.query.noreplies);
if (feed) { if (feed) {
ctx.set("Content-Type", "application/json; charset=utf-8"); ctx.set("Content-Type", "application/json; charset=utf-8");