Add incoming Like federation

This commit is contained in:
Jesse Wierzbinski 2023-11-23 08:43:56 -10:00
parent 17bd81cf46
commit ae41139ad8
No known key found for this signature in database
3 changed files with 48 additions and 6 deletions

View file

@ -2,6 +2,9 @@
import type { Like as LysandLike } from "~types/lysand/Object"; import type { Like as LysandLike } from "~types/lysand/Object";
import { getConfig } from "@config"; import { getConfig } from "@config";
import type { Like } from "@prisma/client"; import type { Like } from "@prisma/client";
import { client } from "~database/datasource";
import type { UserWithRelations } from "./User";
import type { StatusWithRelations } from "./Status";
/** /**
* Represents a Like entity in the database. * Represents a Like entity in the database.
@ -17,3 +20,29 @@ export const toLysand = (like: Like): LysandLike => {
uri: `${getConfig().http.base_url}/actions/${like.id}`, uri: `${getConfig().http.base_url}/actions/${like.id}`,
}; };
}; };
export const createLike = async (
user: UserWithRelations,
status: StatusWithRelations
) => {
await client.like.create({
data: {
likedId: status.id,
likerId: user.id,
},
});
if (status.author.instanceId === user.instanceId) {
// Notify the user that their post has been favourited
await client.notification.create({
data: {
accountId: user.id,
type: "favourite",
notifiedId: status.authorId,
statusId: status.id,
},
});
} else {
// TODO: Add database jobs for federating this
}
};

View file

@ -3,6 +3,7 @@ import { applyConfig } from "@api";
import { errorResponse, jsonResponse } from "@response"; import { errorResponse, jsonResponse } from "@response";
import type { MatchedRoute } from "bun"; import type { MatchedRoute } from "bun";
import { client } from "~database/datasource"; import { client } from "~database/datasource";
import { createLike } from "~database/entities/Like";
import { import {
isViewableByUser, isViewableByUser,
statusAndUserRelations, statusAndUserRelations,
@ -54,12 +55,7 @@ export default async (
}); });
if (!existingLike) { if (!existingLike) {
await client.like.create({ await createLike(user, status);
data: {
likedId: status.id,
likerId: user.id,
},
});
} }
return jsonResponse({ return jsonResponse({

View file

@ -7,6 +7,7 @@ import { errorResponse, jsonResponse } from "@response";
import type { MatchedRoute } from "bun"; import type { MatchedRoute } from "bun";
import { client } from "~database/datasource"; import { client } from "~database/datasource";
import { parseEmojis } from "~database/entities/Emoji"; import { parseEmojis } from "~database/entities/Emoji";
import { createLike } from "~database/entities/Like";
import { createFromObject } from "~database/entities/Object"; import { createFromObject } from "~database/entities/Object";
import { import {
createNewStatus, createNewStatus,
@ -16,6 +17,7 @@ import {
import { parseMentionsUris, userRelations } from "~database/entities/User"; import { parseMentionsUris, userRelations } from "~database/entities/User";
import type { import type {
Announce, Announce,
Like,
LysandAction, LysandAction,
LysandPublication, LysandPublication,
Patch, Patch,
@ -250,8 +252,23 @@ export default async (
break; break;
} }
case "Like": { case "Like": {
const like = body as Like;
// Store the object in the LysandObject table // Store the object in the LysandObject table
await createFromObject(body); await createFromObject(body);
const likedStatus = await client.status.findUnique({
where: {
uri: like.object,
},
include: statusAndUserRelations,
});
if (!likedStatus) {
return errorResponse("Status not found", 404);
}
await createLike(author, likedStatus);
break; break;
} }
case "Dislike": { case "Dislike": {