Merge pull request '[PR]: Embedded all attachment, renotes and discussion history into rss feed content & improve title, and not generate feed for locked account' (#10388) from cgsama/calckey:feedenhance into develop
Reviewed-on: https://codeberg.org/calckey/calckey/pulls/10388
This commit is contained in:
commit
c0348add7a
2 changed files with 106 additions and 33 deletions
|
@ -4,34 +4,40 @@ import config from "@/config/index.js";
|
|||
import type { User } from "@/models/entities/user.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 = {
|
||||
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 searchCriteria = {
|
||||
userId: user.id,
|
||||
visibility: In(['public', 'home']),
|
||||
};
|
||||
|
||||
if (!renotes) {
|
||||
searchCriteria.renoteId = IsNull();
|
||||
}
|
||||
|
||||
if (!replies) {
|
||||
searchCriteria.replyId = IsNull();
|
||||
}
|
||||
|
||||
const notes = await Notes.find({
|
||||
where: {
|
||||
userId: user.id,
|
||||
renoteId: IsNull(),
|
||||
visibility: In(["public", "home"]),
|
||||
},
|
||||
where: searchCriteria,
|
||||
order: { createdAt: -1 },
|
||||
take: 20,
|
||||
take: history,
|
||||
});
|
||||
|
||||
const feed = new Feed({
|
||||
id: author.link,
|
||||
title: `${author.name} (@${user.username}@${config.host})`,
|
||||
updated: notes[0].createdAt,
|
||||
generator: "Calckey",
|
||||
description: `${user.notesCount} Notes, ${
|
||||
profile.ffVisibility === "public" ? user.followingCount : "?"
|
||||
} Following, ${
|
||||
profile.ffVisibility === "public" ? user.followersCount : "?"
|
||||
} Followers${profile.description ? ` · ${profile.description}` : ""}`,
|
||||
generator: 'Calckey',
|
||||
description: `${user.notesCount} Notes, ${profile.ffVisibility === 'public' ? user.followingCount : '?'} Following, ${profile.ffVisibility === 'public' ? user.followersCount : '?'} Followers${profile.description ? ` · ${profile.description}` : ''}`,
|
||||
link: author.link,
|
||||
image: await Users.getAvatarUrl(user),
|
||||
feedLinks: {
|
||||
|
@ -43,23 +49,78 @@ export default async function (user: User) {
|
|||
});
|
||||
|
||||
for (const note of notes) {
|
||||
const files =
|
||||
note.fileIds.length > 0
|
||||
? await DriveFiles.findBy({
|
||||
id: In(note.fileIds),
|
||||
})
|
||||
: [];
|
||||
const file = files.find((file) => file.type.startsWith("image/"));
|
||||
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';
|
||||
}
|
||||
}
|
||||
|
||||
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}`,
|
||||
date: note.createdAt,
|
||||
description: note.cw || undefined,
|
||||
content: note.text || undefined,
|
||||
image: file ? DriveFiles.getPublicUrl(file) || undefined : undefined,
|
||||
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, '')
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -247,7 +247,7 @@ router.get("/api.json", async (ctx) => {
|
|||
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();
|
||||
if (meta.privateMode) {
|
||||
return;
|
||||
|
@ -257,14 +257,26 @@ const getFeed = async (acct: string) => {
|
|||
usernameLower: username.toLowerCase(),
|
||||
host: host ?? IsNull(),
|
||||
isSuspended: false,
|
||||
isLocked:false,
|
||||
});
|
||||
|
||||
return user && (await packFeed(user));
|
||||
if (!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.
|
||||
const reUser = new RegExp(
|
||||
"^/@(?<user>[^/]+?)(?:.(?<feed>json|rss|atom))?(?:/(?<sub>[^/]+))?$",
|
||||
"^/@(?<user>[^/]+?)(?:.(?<feed>json|rss|atom)(?:\\?[^/]*)?)?(?:/(?<sub>[^/]+))?$",
|
||||
);
|
||||
router.get(reUser, async (ctx, next) => {
|
||||
const groups = reUser.exec(ctx.originalUrl)?.groups;
|
||||
|
@ -275,7 +287,7 @@ router.get(reUser, async (ctx, next) => {
|
|||
|
||||
ctx.params = groups;
|
||||
|
||||
console.log(ctx, ctx.params);
|
||||
//console.log(ctx, ctx.params, ctx.query);
|
||||
if (groups.feed) {
|
||||
if (groups.sub) {
|
||||
await next();
|
||||
|
@ -301,7 +313,7 @@ router.get(reUser, async (ctx, next) => {
|
|||
|
||||
// Atom
|
||||
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) {
|
||||
ctx.set("Content-Type", "application/atom+xml; charset=utf-8");
|
||||
|
@ -313,7 +325,7 @@ const atomFeed: Router.Middleware = async (ctx) => {
|
|||
|
||||
// RSS
|
||||
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) {
|
||||
ctx.set("Content-Type", "application/rss+xml; charset=utf-8");
|
||||
|
@ -325,7 +337,7 @@ const rssFeed: Router.Middleware = async (ctx) => {
|
|||
|
||||
// JSON
|
||||
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) {
|
||||
ctx.set("Content-Type", "application/json; charset=utf-8");
|
||||
|
|
Loading…
Reference in a new issue