[mastodon-client] GET /blocks
This commit is contained in:
parent
eb42a21d77
commit
0747e24256
2 changed files with 41 additions and 7 deletions
|
|
@ -479,14 +479,21 @@ export function apiAccountMastodon(router: Router): void {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
router.get("/v1/blocks", async (ctx) => {
|
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 {
|
try {
|
||||||
const data = await client.getBlocks(
|
const auth = await authenticate(ctx.headers.authorization, null);
|
||||||
convertTimelinesArgsId(limitToInt(ctx.query as any)),
|
const user = auth[0] ?? null;
|
||||||
);
|
|
||||||
ctx.body = data.data.map((account) => convertAccount(account));
|
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) {
|
} catch (e: any) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
console.error(e.response.data);
|
console.error(e.response.data);
|
||||||
|
|
|
||||||
|
|
@ -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[]> {
|
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;
|
if (limit > 40) limit = 40;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue