feat(api): Implement duration controls on mutes

This commit is contained in:
Jesse Wierzbinski 2025-03-30 20:54:47 +02:00
parent 9d1d56bd08
commit c9a1581932
No known key found for this signature in database
17 changed files with 652 additions and 572 deletions

View file

@ -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);
});
});

View file

@ -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);
},
),