mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
feat(api): ✨ Implement duration controls on mutes
This commit is contained in:
parent
9d1d56bd08
commit
c9a1581932
17 changed files with 652 additions and 572 deletions
|
|
@ -32,10 +32,9 @@ describe("/api/v1/accounts/:id/mute", () => {
|
|||
test("should mute user", async () => {
|
||||
await using client = await generateClient(users[0]);
|
||||
|
||||
const { data, ok, raw } = await client.muteAccount(users[1].id);
|
||||
const { data, ok } = await client.muteAccount(users[1].id);
|
||||
|
||||
expect(ok).toBe(true);
|
||||
expect(raw.status).toBe(200);
|
||||
|
||||
expect(data.muting).toBe(true);
|
||||
});
|
||||
|
|
@ -43,11 +42,31 @@ describe("/api/v1/accounts/:id/mute", () => {
|
|||
test("should return 200 if user already muted", async () => {
|
||||
await using client = await generateClient(users[0]);
|
||||
|
||||
const { data, ok, raw } = await client.muteAccount(users[1].id);
|
||||
const { data, ok } = await client.muteAccount(users[1].id);
|
||||
|
||||
expect(ok).toBe(true);
|
||||
expect(raw.status).toBe(200);
|
||||
|
||||
expect(data.muting).toBe(true);
|
||||
});
|
||||
|
||||
test("should unmute user after duration", async () => {
|
||||
await using client = await generateClient(users[0]);
|
||||
|
||||
const { data, ok } = await client.muteAccount(users[1].id, {
|
||||
duration: 1,
|
||||
});
|
||||
|
||||
expect(ok).toBe(true);
|
||||
|
||||
expect(data.muting).toBe(true);
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve, 1500));
|
||||
|
||||
const { data: data2, ok: ok2 } = await client.getRelationship(
|
||||
users[1].id,
|
||||
);
|
||||
|
||||
expect(ok2).toBe(true);
|
||||
expect(data2.muting).toBe(false);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ import { describeRoute } from "hono-openapi";
|
|||
import { resolver, validator } from "hono-openapi/zod";
|
||||
import { z } from "zod";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { RelationshipJobType } from "~/classes/queues/relationships";
|
||||
import { relationshipQueue } from "~/classes/queues/relationships";
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.post(
|
||||
|
|
@ -56,15 +58,14 @@ export default apiRoute((app) =>
|
|||
.default(0)
|
||||
.openapi({
|
||||
description:
|
||||
"How long the mute should last, in seconds.",
|
||||
"How long the mute should last, in seconds. 0 means indefinite.",
|
||||
}),
|
||||
}),
|
||||
handleZodError,
|
||||
),
|
||||
async (context) => {
|
||||
const { user } = context.get("auth");
|
||||
// TODO: Add duration support
|
||||
const { notifications } = context.req.valid("json");
|
||||
const { notifications, duration } = context.req.valid("json");
|
||||
const otherUser = context.get("user");
|
||||
|
||||
const foundRelationship = await Relationship.fromOwnerAndSubject(
|
||||
|
|
@ -72,12 +73,24 @@ export default apiRoute((app) =>
|
|||
otherUser,
|
||||
);
|
||||
|
||||
// TODO: Implement duration
|
||||
await foundRelationship.update({
|
||||
muting: true,
|
||||
mutingNotifications: notifications,
|
||||
});
|
||||
|
||||
if (duration > 0) {
|
||||
await relationshipQueue.add(
|
||||
RelationshipJobType.Unmute,
|
||||
{
|
||||
ownerId: user.id,
|
||||
subjectId: otherUser.id,
|
||||
},
|
||||
{
|
||||
delay: duration * 1000,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return context.json(foundRelationship.toApi(), 200);
|
||||
},
|
||||
),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue