[mastodon-client] GET /blocks

This commit is contained in:
Laura Hausmann 2023-09-28 22:23:17 +02:00
parent eb42a21d77
commit 0747e24256
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 41 additions and 7 deletions

View file

@ -479,14 +479,21 @@ export function apiAccountMastodon(router: Router): void {
}
});
router.get("/v1/blocks", async (ctx) => {
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens);
try {
const data = await client.getBlocks(
convertTimelinesArgsId(limitToInt(ctx.query as any)),
);
ctx.body = data.data.map((account) => convertAccount(account));
const auth = await authenticate(ctx.headers.authorization, null);
const user = auth[0] ?? null;
if (!user) {
ctx.status = 401;
return;
}
const cache = UserHelpers.getFreshAccountCache();
const args = normalizeUrlQuery(convertTimelinesArgsId(limitToInt(ctx.query as any)));
const res = await UserHelpers.getUserBlocks(user, args.max_id, args.since_id, args.min_id, args.limit);
const blocks = await UserConverter.encodeMany(res.data, cache);
ctx.body = blocks.map(b => convertAccount(b));
PaginationHelpers.appendLinkPaginationHeader(args, ctx, res);
} catch (e: any) {
console.error(e);
console.error(e.response.data);

View file

@ -156,6 +156,33 @@ export class UserHelpers {
});
}
public static async getUserBlocks(user: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40): Promise<LinkPaginationObject<User[]>> {
if (limit > 80) limit = 80;
const query = PaginationHelpers.makePaginationQuery(
Blockings.createQueryBuilder("blocking"),
sinceId,
maxId,
minId
);
query.andWhere("blocking.blockerId = :userId", {userId: user.id})
.innerJoinAndSelect("blocking.blockee", "blockee");
return query.take(limit).getMany().then(async p => {
if (minId !== undefined) p = p.reverse();
const users = p
.map(p => p.blockee)
.filter(p => p) as User[];
return {
data: users,
maxId: p.map(p => p.id).at(-1),
minId: p.map(p => p.id)[0],
};
});
}
public static async getUserStatuses(user: User, localUser: ILocalUser | null, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 20, onlyMedia: boolean = false, excludeReplies: boolean = false, excludeReblogs: boolean = false, pinned: boolean = false, tagged: string | undefined): Promise<Note[]> {
if (limit > 40) limit = 40;