Add follow notifications and follow request

This commit is contained in:
Jesse Wierzbinski 2024-04-08 16:12:54 -10:00
parent 5afa0c334a
commit 6c60191ab2
No known key found for this signature in database
2 changed files with 111 additions and 41 deletions

View file

@ -11,6 +11,7 @@ import { addEmojiIfNotExists, emojiToAPI, emojiToLysand } from "./Emoji";
import { addInstanceIfNotExists } from "./Instance"; import { addInstanceIfNotExists } from "./Instance";
import { userRelations } from "./relations"; import { userRelations } from "./relations";
import { getUrl } from "./Attachment"; import { getUrl } from "./Attachment";
import { createNewRelationship } from "./Relationship";
export interface AuthData { export interface AuthData {
user: UserWithRelations | null; user: UserWithRelations | null;
@ -59,6 +60,72 @@ export const getFromRequest = async (req: Request): Promise<AuthData> => {
return { user: await retrieveUserFromToken(token), token }; 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) => { export const fetchRemoteUser = async (uri: string) => {
// Check if user not already in database // Check if user not already in database
const foundUser = await client.user.findUnique({ const foundUser = await client.user.findUnique({
@ -267,12 +334,33 @@ export const getRelationshipToOtherUser = async (
user: UserWithRelations, user: UserWithRelations,
other: User, other: User,
) => { ) => {
return await client.relationship.findFirst({ const relationship = await client.relationship.findFirst({
where: { where: {
ownerId: user.id, ownerId: user.id,
subjectId: other.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;
}; };
/** /**

View file

@ -5,7 +5,11 @@ import {
createNewRelationship, createNewRelationship,
relationshipToAPI, relationshipToAPI,
} from "~database/entities/Relationship"; } from "~database/entities/Relationship";
import { getRelationshipToOtherUser } from "~database/entities/User"; import {
followRequestUser,
followUser,
getRelationshipToOtherUser,
} from "~database/entities/User";
export const meta = applyConfig({ export const meta = applyConfig({
allowedMethods: ["POST"], allowedMethods: ["POST"],
@ -53,47 +57,25 @@ export default apiRoute<{
// Check if already following // Check if already following
let relationship = await getRelationshipToOtherUser(self, user); 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) { 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)); return jsonResponse(relationshipToAPI(relationship));
}); });