[mastodon-client] POST /follow_requests/:id/authorize, POST /follow_requests/:id/reject

This commit is contained in:
Laura Hausmann 2023-09-28 22:58:01 +02:00
parent 3dc6bf49b2
commit 45d005fa1a
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
2 changed files with 44 additions and 20 deletions

View file

@ -527,14 +527,18 @@ export function apiAccountMastodon(router: Router): void {
router.post<{ Params: { id: string } }>( router.post<{ Params: { id: string } }>(
"/v1/follow_requests/:id/authorize", "/v1/follow_requests/:id/authorize",
async (ctx) => { 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.acceptFollowRequest( const auth = await authenticate(ctx.headers.authorization, null);
convertId(ctx.params.id, IdType.IceshrimpId), const user = auth[0] ?? null;
);
ctx.body = convertRelationship(data.data); if (!user) {
ctx.status = 401;
return;
}
const target = await UserHelpers.getUserCached(convertId(ctx.params.id, IdType.IceshrimpId));
const result = await UserHelpers.acceptFollowRequest(target, user);
ctx.body = convertRelationship(result);
} catch (e: any) { } catch (e: any) {
console.error(e); console.error(e);
console.error(e.response.data); console.error(e.response.data);
@ -546,14 +550,18 @@ export function apiAccountMastodon(router: Router): void {
router.post<{ Params: { id: string } }>( router.post<{ Params: { id: string } }>(
"/v1/follow_requests/:id/reject", "/v1/follow_requests/:id/reject",
async (ctx) => { 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.rejectFollowRequest( const auth = await authenticate(ctx.headers.authorization, null);
convertId(ctx.params.id, IdType.IceshrimpId), const user = auth[0] ?? null;
);
ctx.body = convertRelationship(data.data); if (!user) {
ctx.status = 401;
return;
}
const target = await UserHelpers.getUserCached(convertId(ctx.params.id, IdType.IceshrimpId));
const result = await UserHelpers.rejectFollowRequest(target, user);
ctx.body = convertRelationship(result);
} catch (e: any) { } catch (e: any) {
console.error(e); console.error(e);
console.error(e.response.data); console.error(e.response.data);

View file

@ -31,6 +31,8 @@ import { Muting } from "@/models/entities/muting.js";
import { publishUserEvent } from "@/services/stream.js"; import { publishUserEvent } from "@/services/stream.js";
import { UserConverter } from "@/server/api/mastodon/converters/user.js"; import { UserConverter } from "@/server/api/mastodon/converters/user.js";
import { convertId, IdType } from "@/misc/convert-id.js"; import { convertId, IdType } from "@/misc/convert-id.js";
import acceptFollowRequest from "@/services/following/requests/accept.js";
import { rejectFollowRequest } from "@/services/following/reject.js";
export type AccountCache = { export type AccountCache = {
locks: AsyncLock; locks: AsyncLock;
@ -47,7 +49,7 @@ export type LinkPaginationObject<T> = {
type RelationshipType = 'followers' | 'following'; type RelationshipType = 'followers' | 'following';
export class UserHelpers { export class UserHelpers {
public static async followUser(target: User, localUser: ILocalUser, reblogs: boolean, notify: boolean) { public static async followUser(target: User, localUser: ILocalUser, reblogs: boolean, notify: boolean): Promise<MastodonEntity.Relationship> {
//FIXME: implement reblogs & notify params //FIXME: implement reblogs & notify params
const following = await Followings.exist({where: {followerId: localUser.id, followeeId: target.id}}); const following = await Followings.exist({where: {followerId: localUser.id, followeeId: target.id}});
const requested = await FollowRequests.exist({where: {followerId: localUser.id, followeeId: target.id}}); const requested = await FollowRequests.exist({where: {followerId: localUser.id, followeeId: target.id}});
@ -57,7 +59,7 @@ export class UserHelpers {
return this.getUserRelationshipTo(target.id, localUser.id); return this.getUserRelationshipTo(target.id, localUser.id);
} }
public static async unfollowUser(target: User, localUser: ILocalUser) { public static async unfollowUser(target: User, localUser: ILocalUser): Promise<MastodonEntity.Relationship> {
const following = await Followings.exist({where: {followerId: localUser.id, followeeId: target.id}}); const following = await Followings.exist({where: {followerId: localUser.id, followeeId: target.id}});
const requested = await FollowRequests.exist({where: {followerId: localUser.id, followeeId: target.id}}); const requested = await FollowRequests.exist({where: {followerId: localUser.id, followeeId: target.id}});
if (following) if (following)
@ -68,7 +70,7 @@ export class UserHelpers {
return this.getUserRelationshipTo(target.id, localUser.id); return this.getUserRelationshipTo(target.id, localUser.id);
} }
public static async blockUser(target: User, localUser: ILocalUser) { public static async blockUser(target: User, localUser: ILocalUser): Promise<MastodonEntity.Relationship> {
const blocked = await Blockings.exist({where: {blockerId: localUser.id, blockeeId: target.id}}); const blocked = await Blockings.exist({where: {blockerId: localUser.id, blockeeId: target.id}});
if (!blocked) if (!blocked)
await createBlocking(localUser, target); await createBlocking(localUser, target);
@ -76,7 +78,7 @@ export class UserHelpers {
return this.getUserRelationshipTo(target.id, localUser.id); return this.getUserRelationshipTo(target.id, localUser.id);
} }
public static async unblockUser(target: User, localUser: ILocalUser) { public static async unblockUser(target: User, localUser: ILocalUser): Promise<MastodonEntity.Relationship> {
const blocked = await Blockings.exist({where: {blockerId: localUser.id, blockeeId: target.id}}); const blocked = await Blockings.exist({where: {blockerId: localUser.id, blockeeId: target.id}});
if (blocked) if (blocked)
await deleteBlocking(localUser, target); await deleteBlocking(localUser, target);
@ -84,7 +86,7 @@ export class UserHelpers {
return this.getUserRelationshipTo(target.id, localUser.id); return this.getUserRelationshipTo(target.id, localUser.id);
} }
public static async muteUser(target: User, localUser: ILocalUser, notifications: boolean = true, duration: number = 0) { public static async muteUser(target: User, localUser: ILocalUser, notifications: boolean = true, duration: number = 0): Promise<MastodonEntity.Relationship> {
//FIXME: respect notifications parameter //FIXME: respect notifications parameter
const muted = await Mutings.exist({where: {muterId: localUser.id, muteeId: target.id}}); const muted = await Mutings.exist({where: {muterId: localUser.id, muteeId: target.id}});
if (!muted) { if (!muted) {
@ -107,7 +109,7 @@ export class UserHelpers {
return this.getUserRelationshipTo(target.id, localUser.id); return this.getUserRelationshipTo(target.id, localUser.id);
} }
public static async unmuteUser(target: User, localUser: ILocalUser) { public static async unmuteUser(target: User, localUser: ILocalUser): Promise<MastodonEntity.Relationship> {
const muting = await Mutings.findOneBy({muterId: localUser.id, muteeId: target.id}); const muting = await Mutings.findOneBy({muterId: localUser.id, muteeId: target.id});
if (muting) { if (muting) {
await Mutings.delete({ await Mutings.delete({
@ -120,6 +122,20 @@ export class UserHelpers {
return this.getUserRelationshipTo(target.id, localUser.id); return this.getUserRelationshipTo(target.id, localUser.id);
} }
public static async acceptFollowRequest(target: User, localUser: ILocalUser): Promise<MastodonEntity.Relationship> {
const pending = await FollowRequests.exist({where: {followerId: target.id, followeeId: localUser.id}});
if (pending)
await acceptFollowRequest(localUser, target);
return this.getUserRelationshipTo(target.id, localUser.id);
}
public static async rejectFollowRequest(target: User, localUser: ILocalUser): Promise<MastodonEntity.Relationship> {
const pending = await FollowRequests.exist({where: {followerId: target.id, followeeId: localUser.id}});
if (pending)
await rejectFollowRequest(localUser, target);
return this.getUserRelationshipTo(target.id, localUser.id);
}
public static async getUserMutes(user: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, cache: AccountCache = UserHelpers.getFreshAccountCache()): Promise<LinkPaginationObject<MastodonEntity.MutedAccount[]>> { public static async getUserMutes(user: ILocalUser, maxId: string | undefined, sinceId: string | undefined, minId: string | undefined, limit: number = 40, cache: AccountCache = UserHelpers.getFreshAccountCache()): Promise<LinkPaginationObject<MastodonEntity.MutedAccount[]>> {
if (limit > 80) limit = 80; if (limit > 80) limit = 80;