mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
Add incoming Like federation
This commit is contained in:
parent
17bd81cf46
commit
ae41139ad8
|
|
@ -2,6 +2,9 @@
|
|||
import type { Like as LysandLike } from "~types/lysand/Object";
|
||||
import { getConfig } from "@config";
|
||||
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.
|
||||
|
|
@ -17,3 +20,29 @@ export const toLysand = (like: Like): LysandLike => {
|
|||
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
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { applyConfig } from "@api";
|
|||
import { errorResponse, jsonResponse } from "@response";
|
||||
import type { MatchedRoute } from "bun";
|
||||
import { client } from "~database/datasource";
|
||||
import { createLike } from "~database/entities/Like";
|
||||
import {
|
||||
isViewableByUser,
|
||||
statusAndUserRelations,
|
||||
|
|
@ -54,12 +55,7 @@ export default async (
|
|||
});
|
||||
|
||||
if (!existingLike) {
|
||||
await client.like.create({
|
||||
data: {
|
||||
likedId: status.id,
|
||||
likerId: user.id,
|
||||
},
|
||||
});
|
||||
await createLike(user, status);
|
||||
}
|
||||
|
||||
return jsonResponse({
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import { errorResponse, jsonResponse } from "@response";
|
|||
import type { MatchedRoute } from "bun";
|
||||
import { client } from "~database/datasource";
|
||||
import { parseEmojis } from "~database/entities/Emoji";
|
||||
import { createLike } from "~database/entities/Like";
|
||||
import { createFromObject } from "~database/entities/Object";
|
||||
import {
|
||||
createNewStatus,
|
||||
|
|
@ -16,6 +17,7 @@ import {
|
|||
import { parseMentionsUris, userRelations } from "~database/entities/User";
|
||||
import type {
|
||||
Announce,
|
||||
Like,
|
||||
LysandAction,
|
||||
LysandPublication,
|
||||
Patch,
|
||||
|
|
@ -250,8 +252,23 @@ export default async (
|
|||
break;
|
||||
}
|
||||
case "Like": {
|
||||
const like = body as Like;
|
||||
// Store the object in the LysandObject table
|
||||
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;
|
||||
}
|
||||
case "Dislike": {
|
||||
|
|
|
|||
Loading…
Reference in a new issue