mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-25 19:37:34 -07:00
[mastodon-client] GET /v1/conversations
This commit is contained in:
parent
2fef4b1982
commit
3fb3f405ea
3 changed files with 82 additions and 21 deletions
|
@ -8,6 +8,7 @@ import { TimelineHelpers } from "@/server/api/mastodon/helpers/timeline.js";
|
||||||
import { NoteConverter } from "@/server/api/mastodon/converters/note.js";
|
import { NoteConverter } from "@/server/api/mastodon/converters/note.js";
|
||||||
import { UserHelpers } from "@/server/api/mastodon/helpers/user.js";
|
import { UserHelpers } from "@/server/api/mastodon/helpers/user.js";
|
||||||
import { UserLists } from "@/models/index.js";
|
import { UserLists } from "@/models/index.js";
|
||||||
|
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
|
||||||
|
|
||||||
export function limitToInt(q: ParsedUrlQuery, additional: string[] = []) {
|
export function limitToInt(q: ParsedUrlQuery, additional: string[] = []) {
|
||||||
let object: any = q;
|
let object: any = q;
|
||||||
|
@ -180,12 +181,19 @@ export function setupEndpointsTimeline(router: Router): void {
|
||||||
const accessTokens = ctx.headers.authorization;
|
const accessTokens = ctx.headers.authorization;
|
||||||
const client = getClient(BASE_URL, accessTokens);
|
const client = getClient(BASE_URL, accessTokens);
|
||||||
try {
|
try {
|
||||||
const data = await client.getConversationTimeline(
|
const auth = await authenticate(ctx.headers.authorization, null);
|
||||||
convertPaginationArgsIds(limitToInt(ctx.query)),
|
const user = auth[0] ?? undefined;
|
||||||
);
|
|
||||||
ctx.body = data.data.map((conversation) =>
|
if (!user) {
|
||||||
convertConversation(conversation),
|
ctx.status = 401;
|
||||||
);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const args = normalizeUrlQuery(convertPaginationArgsIds(limitToInt(ctx.query)));
|
||||||
|
const res = await TimelineHelpers.getConversations(user, args.max_id, args.since_id, args.min_id, args.limit);
|
||||||
|
|
||||||
|
ctx.body = res.data.map(c => convertConversation(c));
|
||||||
|
PaginationHelpers.appendLinkPaginationHeader(args, ctx, res);
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
console.error(e.response.data);
|
console.error(e.response.data);
|
||||||
|
|
|
@ -8,30 +8,32 @@ export class PaginationHelpers {
|
||||||
sinceId?: string,
|
sinceId?: string,
|
||||||
maxId?: string,
|
maxId?: string,
|
||||||
minId?: string,
|
minId?: string,
|
||||||
idField: string = "id"
|
idField: string = "id",
|
||||||
|
autoPrefix: boolean = true
|
||||||
) {
|
) {
|
||||||
if (sinceId && minId) throw new Error("Can't user both sinceId and minId params");
|
if (sinceId && minId) throw new Error("Can't user both sinceId and minId params");
|
||||||
|
if (autoPrefix) idField = `${q.alias}.${idField}`;
|
||||||
|
|
||||||
if (sinceId && maxId) {
|
if (sinceId && maxId) {
|
||||||
q.andWhere(`${q.alias}.${idField} > :sinceId`, {sinceId: sinceId});
|
q.andWhere(`${idField} > :sinceId`, {sinceId: sinceId});
|
||||||
q.andWhere(`${q.alias}.${idField} < :maxId`, {maxId: maxId});
|
q.andWhere(`${idField} < :maxId`, {maxId: maxId});
|
||||||
q.orderBy(`${q.alias}.${idField}`, "DESC");
|
q.orderBy(`${idField}`, "DESC");
|
||||||
}
|
}
|
||||||
if (minId && maxId) {
|
if (minId && maxId) {
|
||||||
q.andWhere(`${q.alias}.${idField} > :minId`, {minId: minId});
|
q.andWhere(`${idField} > :minId`, {minId: minId});
|
||||||
q.andWhere(`${q.alias}.${idField} < :maxId`, {maxId: maxId});
|
q.andWhere(`${idField} < :maxId`, {maxId: maxId});
|
||||||
q.orderBy(`${q.alias}.${idField}`, "ASC");
|
q.orderBy(`${idField}`, "ASC");
|
||||||
} else if (sinceId) {
|
} else if (sinceId) {
|
||||||
q.andWhere(`${q.alias}.${idField} > :sinceId`, {sinceId: sinceId});
|
q.andWhere(`${idField} > :sinceId`, {sinceId: sinceId});
|
||||||
q.orderBy(`${q.alias}.${idField}`, "DESC");
|
q.orderBy(`${idField}`, "DESC");
|
||||||
} else if (minId) {
|
} else if (minId) {
|
||||||
q.andWhere(`${q.alias}.${idField} > :minId`, {minId: minId});
|
q.andWhere(`${idField} > :minId`, {minId: minId});
|
||||||
q.orderBy(`${q.alias}.${idField}`, "ASC");
|
q.orderBy(`${idField}`, "ASC");
|
||||||
} else if (maxId) {
|
} else if (maxId) {
|
||||||
q.andWhere(`${q.alias}.${idField} < :maxId`, {maxId: maxId});
|
q.andWhere(`${idField} < :maxId`, {maxId: maxId});
|
||||||
q.orderBy(`${q.alias}.${idField}`, "DESC");
|
q.orderBy(`${idField}`, "DESC");
|
||||||
} else {
|
} else {
|
||||||
q.orderBy(`${q.alias}.${idField}`, "DESC");
|
q.orderBy(`${idField}`, "DESC");
|
||||||
}
|
}
|
||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Note } from "@/models/entities/note.js";
|
import { Note } from "@/models/entities/note.js";
|
||||||
import { ILocalUser } from "@/models/entities/user.js";
|
import { ILocalUser, User } from "@/models/entities/user.js";
|
||||||
import { Followings, Notes, UserListJoinings } from "@/models/index.js";
|
import { Followings, Notes, UserListJoinings } from "@/models/index.js";
|
||||||
import { Brackets } from "typeorm";
|
import { Brackets } from "typeorm";
|
||||||
import { generateChannelQuery } from "@/server/api/common/generate-channel-query.js";
|
import { generateChannelQuery } from "@/server/api/common/generate-channel-query.js";
|
||||||
|
@ -12,6 +12,11 @@ import { generateMutedUserRenotesQueryForNotes } from "@/server/api/common/gener
|
||||||
import { fetchMeta } from "@/misc/fetch-meta.js";
|
import { fetchMeta } from "@/misc/fetch-meta.js";
|
||||||
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
|
import { PaginationHelpers } from "@/server/api/mastodon/helpers/pagination.js";
|
||||||
import { UserList } from "@/models/entities/user-list.js";
|
import { UserList } from "@/models/entities/user-list.js";
|
||||||
|
import { LinkPaginationObject, UserHelpers } from "@/server/api/mastodon/helpers/user.js";
|
||||||
|
import { UserConverter } from "@/server/api/mastodon/converters/user.js";
|
||||||
|
import { NoteConverter } from "@/server/api/mastodon/converters/note.js";
|
||||||
|
import { awaitAll } from "@/prelude/await-all.js";
|
||||||
|
import { unique } from "@/prelude/array.js";
|
||||||
|
|
||||||
export class TimelineHelpers {
|
export class TimelineHelpers {
|
||||||
public static async getHomeTimeline(user: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20): Promise<Note[]> {
|
public static async getHomeTimeline(user: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20): Promise<Note[]> {
|
||||||
|
@ -153,4 +158,50 @@ export class TimelineHelpers {
|
||||||
|
|
||||||
return PaginationHelpers.execQuery(query, limit, minId !== undefined);
|
return PaginationHelpers.execQuery(query, limit, minId !== undefined);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async getConversations(user: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20): Promise<LinkPaginationObject<MastodonEntity.Conversation[]>> {
|
||||||
|
if (limit > 40) limit = 40;
|
||||||
|
const query = PaginationHelpers.makePaginationQuery(
|
||||||
|
Notes.createQueryBuilder("note"),
|
||||||
|
sinceId,
|
||||||
|
maxId,
|
||||||
|
minId,
|
||||||
|
"COALESCE(note.threadId, note.id)",
|
||||||
|
false
|
||||||
|
)
|
||||||
|
.distinctOn(["COALESCE(note.threadId, note.id)"])
|
||||||
|
.orderBy({"COALESCE(note.threadId, note.id)": minId ? "ASC" : "DESC", "note.id": "DESC"})
|
||||||
|
.andWhere("note.visibility = 'specified'")
|
||||||
|
.andWhere(
|
||||||
|
new Brackets(qb => {
|
||||||
|
qb.where("note.userId = :userId");
|
||||||
|
qb.orWhere("note.visibleUserIds @> array[:userId]::varchar[]");
|
||||||
|
}))
|
||||||
|
.setParameters({userId: user.id})
|
||||||
|
|
||||||
|
return query.take(limit).getMany().then(p => {
|
||||||
|
if (minId !== undefined) p = p.reverse();
|
||||||
|
const cache = UserHelpers.getFreshAccountCache();
|
||||||
|
const conversations = p.map(c => {
|
||||||
|
// Gather all unique IDs except for the local user
|
||||||
|
const userIds = unique([c.userId].concat(c.visibleUserIds).filter(p => p != user.id));
|
||||||
|
const users = userIds.map(id => UserHelpers.getUserCached(id, cache).catch(_ => null));
|
||||||
|
const accounts = Promise.all(users).then(u => UserConverter.encodeMany(u.filter(u => u) as User[], cache));
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: c.threadId ?? c.id,
|
||||||
|
accounts: accounts.then(u => u.length > 0 ? u : UserConverter.encodeMany([user], cache)), // failsafe to prevent apps from crashing case when all participant users have been deleted
|
||||||
|
last_status: NoteConverter.encode(c, user, cache),
|
||||||
|
unread: false //FIXME implement this (also the /v1/conversations/:id/read endpoint)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
const res = {
|
||||||
|
data: Promise.all(conversations.map(c => awaitAll(c))),
|
||||||
|
maxId: p.map(p => p.threadId ?? p.id).at(-1),
|
||||||
|
minId: p.map(p => p.threadId ?? p.id)[0],
|
||||||
|
};
|
||||||
|
|
||||||
|
return awaitAll(res);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue