diff --git a/database/entities/User.ts b/database/entities/User.ts index abb93125..1b660d3a 100644 --- a/database/entities/User.ts +++ b/database/entities/User.ts @@ -11,6 +11,7 @@ import { addEmojiIfNotExists, emojiToAPI, emojiToLysand } from "./Emoji"; import { addInstanceIfNotExists } from "./Instance"; import { userRelations } from "./relations"; import { getUrl } from "./Attachment"; +import { createNewRelationship } from "./Relationship"; export interface AuthData { user: UserWithRelations | null; @@ -59,6 +60,72 @@ export const getFromRequest = async (req: Request): Promise => { return { user: await retrieveUserFromToken(token), token }; }; +export const followUser = async ( + follower: User, + followee: User, + reblogs = false, + notify = false, + languages: string[] = [], +) => { + const relationship = await client.relationship.update({ + where: { id: follower.id }, + data: { + following: true, + showingReblogs: reblogs, + notifying: notify, + languages: languages, + }, + }); + + if (follower.instanceId === followee.instanceId) { + // Notify the user that their post has been favourited + await client.notification.create({ + data: { + accountId: follower.id, + type: "follow", + notifiedId: followee.id, + }, + }); + } else { + // TODO: Add database jobs for federating this + } + + return relationship; +}; + +export const followRequestUser = async ( + follower: User, + followee: User, + reblogs = false, + notify = false, + languages: string[] = [], +) => { + const relationship = await client.relationship.update({ + where: { id: follower.id }, + data: { + requested: true, + showingReblogs: reblogs, + notifying: notify, + languages: languages, + }, + }); + + if (follower.instanceId === followee.instanceId) { + // Notify the user that their post has been favourited + await client.notification.create({ + data: { + accountId: follower.id, + type: "follow_request", + notifiedId: followee.id, + }, + }); + } else { + // TODO: Add database jobs for federating this + } + + return relationship; +}; + export const fetchRemoteUser = async (uri: string) => { // Check if user not already in database const foundUser = await client.user.findUnique({ @@ -267,12 +334,33 @@ export const getRelationshipToOtherUser = async ( user: UserWithRelations, other: User, ) => { - return await client.relationship.findFirst({ + const relationship = await client.relationship.findFirst({ where: { ownerId: user.id, subjectId: other.id, }, }); + + if (!relationship) { + // Create new relationship + + const newRelationship = await createNewRelationship(user, other); + + await client.user.update({ + where: { id: user.id }, + data: { + relationships: { + connect: { + id: newRelationship.id, + }, + }, + }, + }); + + return newRelationship; + } + + return relationship; }; /** diff --git a/server/api/api/v1/accounts/[id]/follow.ts b/server/api/api/v1/accounts/[id]/follow.ts index 174d30fa..61fb4af6 100644 --- a/server/api/api/v1/accounts/[id]/follow.ts +++ b/server/api/api/v1/accounts/[id]/follow.ts @@ -5,7 +5,11 @@ import { createNewRelationship, relationshipToAPI, } from "~database/entities/Relationship"; -import { getRelationshipToOtherUser } from "~database/entities/User"; +import { + followRequestUser, + followUser, + getRelationshipToOtherUser, +} from "~database/entities/User"; export const meta = applyConfig({ allowedMethods: ["POST"], @@ -53,47 +57,25 @@ export default apiRoute<{ // Check if already following let relationship = await getRelationshipToOtherUser(self, user); - if (!relationship) { - // Create new relationship - - const newRelationship = await createNewRelationship(self, user); - - await client.user.update({ - where: { id: self.id }, - data: { - relationships: { - connect: { - id: newRelationship.id, - }, - }, - }, - }); - - relationship = newRelationship; - } - if (!relationship.following) { - relationship.following = true; + if (user.isLocked) { + relationship = await followRequestUser( + self, + user, + reblogs, + notify, + languages, + ); + } else { + relationship = await followUser( + self, + user, + reblogs, + notify, + languages, + ); + } } - if (reblogs) { - relationship.showingReblogs = true; - } - if (notify) { - relationship.notifying = true; - } - if (languages) { - relationship.languages = languages; - } - - await client.relationship.update({ - where: { id: relationship.id }, - data: { - following: true, - showingReblogs: reblogs ?? false, - notifying: notify ?? false, - languages: languages ?? [], - }, - }); return jsonResponse(relationshipToAPI(relationship)); });