mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
Add more federation support with Undo objects
This commit is contained in:
parent
ae41139ad8
commit
1db82202e0
|
|
@ -46,3 +46,30 @@ export const createLike = async (
|
||||||
// TODO: Add database jobs for federating this
|
// TODO: Add database jobs for federating this
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const deleteLike = async (
|
||||||
|
user: UserWithRelations,
|
||||||
|
status: StatusWithRelations
|
||||||
|
) => {
|
||||||
|
await client.like.deleteMany({
|
||||||
|
where: {
|
||||||
|
likedId: status.id,
|
||||||
|
likerId: user.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Notify the user that their post has been favourited
|
||||||
|
await client.notification.deleteMany({
|
||||||
|
where: {
|
||||||
|
accountId: user.id,
|
||||||
|
type: "favourite",
|
||||||
|
notifiedId: status.authorId,
|
||||||
|
statusId: status.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (user.instanceId === null) {
|
||||||
|
// User is local, federate the delete
|
||||||
|
// TODO: Federate this
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -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 { deleteLike } from "~database/entities/Like";
|
||||||
import {
|
import {
|
||||||
isViewableByUser,
|
isViewableByUser,
|
||||||
statusAndUserRelations,
|
statusAndUserRelations,
|
||||||
|
|
@ -46,12 +47,7 @@ export default async (
|
||||||
if (!status || !isViewableByUser(status, user))
|
if (!status || !isViewableByUser(status, user))
|
||||||
return errorResponse("Record not found", 404);
|
return errorResponse("Record not found", 404);
|
||||||
|
|
||||||
await client.like.deleteMany({
|
await deleteLike(user, status);
|
||||||
where: {
|
|
||||||
likedId: status.id,
|
|
||||||
likerId: user.id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return jsonResponse({
|
return jsonResponse({
|
||||||
...(await statusToAPI(status, user)),
|
...(await statusToAPI(status, user)),
|
||||||
|
|
|
||||||
|
|
@ -7,7 +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 { createLike, deleteLike } from "~database/entities/Like";
|
||||||
import { createFromObject } from "~database/entities/Object";
|
import { createFromObject } from "~database/entities/Object";
|
||||||
import {
|
import {
|
||||||
createNewStatus,
|
createNewStatus,
|
||||||
|
|
@ -21,6 +21,7 @@ import type {
|
||||||
LysandAction,
|
LysandAction,
|
||||||
LysandPublication,
|
LysandPublication,
|
||||||
Patch,
|
Patch,
|
||||||
|
Undo,
|
||||||
} from "~types/lysand/Object";
|
} from "~types/lysand/Object";
|
||||||
|
|
||||||
export const meta = applyConfig({
|
export const meta = applyConfig({
|
||||||
|
|
@ -274,6 +275,10 @@ export default async (
|
||||||
case "Dislike": {
|
case "Dislike": {
|
||||||
// Store the object in the LysandObject table
|
// Store the object in the LysandObject table
|
||||||
await createFromObject(body);
|
await createFromObject(body);
|
||||||
|
|
||||||
|
return jsonResponse({
|
||||||
|
info: "Dislikes are not supported by this software",
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "Follow": {
|
case "Follow": {
|
||||||
|
|
@ -332,8 +337,61 @@ export default async (
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "Undo": {
|
case "Undo": {
|
||||||
|
const undo = body as Undo;
|
||||||
// Store the object in the LysandObject table
|
// Store the object in the LysandObject table
|
||||||
await createFromObject(body);
|
await createFromObject(body);
|
||||||
|
|
||||||
|
const object = await client.lysandObject.findUnique({
|
||||||
|
where: {
|
||||||
|
uri: undo.object,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!object) {
|
||||||
|
return errorResponse("Object not found", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (object.type) {
|
||||||
|
case "Like": {
|
||||||
|
const status = await client.status.findUnique({
|
||||||
|
where: {
|
||||||
|
uri: undo.object,
|
||||||
|
authorId: author.id,
|
||||||
|
},
|
||||||
|
include: statusAndUserRelations,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!status) {
|
||||||
|
return errorResponse("Status not found", 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
await deleteLike(author, status);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "Announce": {
|
||||||
|
await client.status.delete({
|
||||||
|
where: {
|
||||||
|
uri: undo.object,
|
||||||
|
authorId: author.id,
|
||||||
|
},
|
||||||
|
include: statusAndUserRelations,
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "Note": {
|
||||||
|
await client.status.delete({
|
||||||
|
where: {
|
||||||
|
uri: undo.object,
|
||||||
|
authorId: author.id,
|
||||||
|
},
|
||||||
|
include: statusAndUserRelations,
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
return errorResponse("Invalid object type", 400);
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "Extension": {
|
case "Extension": {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue