From 3984bfa0a97d1ee5aead27b2d238ee5a1735aeb7 Mon Sep 17 00:00:00 2001 From: gnuxie Date: Fri, 29 Nov 2024 17:51:42 +0000 Subject: [PATCH] Readable message for watch command when list is already watched. https://github.com/the-draupnir-project/Draupnir/issues/630 --- src/commands/WatchUnwatchCommand.ts | 10 +++++++++- test/unit/commands/WatchUnwatchCommandTest.ts | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/commands/WatchUnwatchCommand.ts b/src/commands/WatchUnwatchCommand.ts index 80f0db77..6a221cd8 100644 --- a/src/commands/WatchUnwatchCommand.ts +++ b/src/commands/WatchUnwatchCommand.ts @@ -19,7 +19,7 @@ import { describeCommand, tuple, } from "@the-draupnir-project/interface-manager"; -import { Result } from "@gnuxie/typescript-result"; +import { Result, ResultError } from "@gnuxie/typescript-result"; import { Draupnir } from "../Draupnir"; import { DraupnirContextToCommandContextTranslator, @@ -49,6 +49,14 @@ export const DraupnirWatchPolicyRoomCommand = describeCommand({ if (isError(policyRoom)) { return policyRoom; } + if ( + issuerManager.allWatchedLists.some( + (profile) => + profile.room.toRoomIDOrAlias() === policyRoom.ok.toRoomIDOrAlias() + ) + ) { + return ResultError.Result("We are already watching this list."); + } return await issuerManager.watchList( PropagationType.Direct, policyRoom.ok, diff --git a/test/unit/commands/WatchUnwatchCommandTest.ts b/test/unit/commands/WatchUnwatchCommandTest.ts index b4ca49aa..734c0911 100644 --- a/test/unit/commands/WatchUnwatchCommandTest.ts +++ b/test/unit/commands/WatchUnwatchCommandTest.ts @@ -13,6 +13,7 @@ import { PolicyListConfig, PropagationType, RoomResolver, + isError, isOk, } from "matrix-protection-suite"; import { createMock } from "ts-auto-mock"; @@ -50,6 +51,20 @@ describe("Test the WatchUnwatchCommmands", function () { ); expect(isOk(result)).toBe(true); }); + it("Draupnir watch command should return an error if the room is already being watched", async function () { + const issuerManagerWithWatchedList = createMock({ + allWatchedLists: [ + { room: policyRoom, propagation: PropagationType.Direct, options: {} }, + ], + }); + const result = await CommandExecutorHelper.execute( + DraupnirWatchPolicyRoomCommand, + { issuerManager: issuerManagerWithWatchedList, roomResolver }, + {}, + policyRoom + ); + expect(isError(result)).toBe(true); + }); it("DraupnirUnwatchCommand", async function () { const result = await CommandExecutorHelper.execute( DraupnirUnwatchPolicyRoomCommand,