[mastodon-client] GET /v1/trends/{statuses,hashtags,links}

This commit is contained in:
Laura Hausmann 2023-10-05 02:25:13 +02:00
parent fb7713c424
commit ec55071eb2
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
3 changed files with 43 additions and 46 deletions

View File

@ -75,21 +75,30 @@ export function setupEndpointsMisc(router: Router): void {
},
);
router.get("/v1/trends", async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
// displayed without being logged in
router.get(["/v1/trends/tags", "/v1/trends"], async (ctx) => {
try {
const data = await client.getInstanceTrends();
ctx.body = data.data;
const args = limitToInt(ctx.query);
ctx.body = await MiscHelpers.getTrendingHashtags(args.limit, args.offset);
} catch (e: any) {
console.error(e);
ctx.status = 401;
ctx.body = e.response.data;
ctx.status = 500;
ctx.body = { error: e.message };
}
});
router.get("/v1/trends/statuses", async (ctx) => {
try {
const args = limitToInt(ctx.query);
ctx.body = await MiscHelpers.getTrendingStatuses(args.limit, args.offset);
} catch (e: any) {
ctx.status = 500;
ctx.body = { error: e.message };
}
});
router.get("/v1/trends/links", async (ctx) => {
ctx.body = [];
});
router.get("/v1/preferences", async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.request.headers.authorization;

View File

@ -54,40 +54,4 @@ export function setupEndpointsSearch(router: Router): void {
ctx.body = {error: e.message};
}
});
router.get("/v1/trends/statuses", async (ctx) => {
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
const accessTokens = ctx.headers.authorization;
try {
const data = await getHighlight(
BASE_URL,
ctx.request.hostname,
accessTokens,
);
ctx.body = data.map((status) => convertStatusIds(status));
} catch (e: any) {
console.error(e);
ctx.status = 401;
ctx.body = e.response.data;
}
});
}
async function getHighlight(
BASE_URL: string,
domain: string,
accessTokens: string | undefined,
) {
const accessTokenArr = accessTokens?.split(" ") ?? [null];
const accessToken = accessTokenArr[accessTokenArr.length - 1];
try {
const api = await axios.post(`${BASE_URL}/api/notes/featured`, {
i: accessToken,
});
const data: MisskeyEntity.Note[] = api.data;
return data.map((note) => new Converter(BASE_URL).note(note, domain));
} catch (e: any) {
console.log(e);
console.log(e.response.data);
return [];
}
}

View File

@ -18,6 +18,7 @@ import { generateBlockQueryForUsers } from "@/server/api/common/generate-block-q
import { uniqBy } from "@/prelude/array.js";
import { EmojiConverter } from "@/server/api/mastodon/converters/emoji.js";
import { populateEmojis } from "@/misc/populate-emojis.js";
import { NoteConverter } from "@/server/api/mastodon/converters/note.js";
export class MiscHelpers {
public static async getInstance(): Promise<MastodonEntity.Instance> {
@ -203,4 +204,27 @@ export class MiscHelpers {
)
);
}
public static async getTrendingStatuses(limit: number = 20, offset: number = 0): Promise<MastodonEntity.Status[]> {
if (limit > 40) limit = 40;
const query = Notes.createQueryBuilder("note")
.addSelect("note.score")
.andWhere("note.score > 0")
.andWhere("note.createdAt > :date", { date: new Date(Date.now() - 1000 * 60 * 60 * 24) })
.andWhere("note.visibility = 'public'")
.andWhere("note.userHost IS NULL")
.orderBy("note.score", "DESC");
return query
.skip(offset)
.take(limit)
.getMany()
.then(result => NoteConverter.encodeMany(result, null));
}
public static async getTrendingHashtags(limit: number = 10, offset: number = 0): Promise<MastodonEntity.Tag[]> {
if (limit > 20) limit = 20;
return [];
//FIXME: This was already implemented in api/endpoints/hashtags/trend.ts, but the implementation is sketchy at best. Rewrite from scratch.
}
}