Skip to content

Commit 071b54d

Browse files
committed
Don't allow no changes in edit command
1 parent 9488eaa commit 071b54d

File tree

6 files changed

+31
-23
lines changed

6 files changed

+31
-23
lines changed

backend/src/common/general.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ export function todo(): never {
1818

1919
export type ValuesOf<T> = T[keyof T];
2020
export type Awaitable<T> = T | PromiseLike<T>;
21+
export type Nullable<T> = {
22+
[TKey in keyof T]: T[TKey] | null;
23+
};

backend/src/plugin/logging/config/tags.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ export const TagEditEvent = eventConfig(
6060
name: "{{actor.tag}}",
6161
iconURL: "{{actor.avatar}}",
6262
},
63-
description: "{{#nameChanged}}{{oldTag.name}} → {{/nameChanged}}{{newTag.name}}",
63+
description:
64+
"{{#nameChanged}}{{oldTag.name}} → {{/nameChanged}}{{newTag.name}}",
6465
fields: [
6566
{
6667
name: "Old Content",

backend/src/plugin/logging/logger/tags.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Nullable } from "#common/general.ts";
12
import { makeGuildView } from "#common/template/guild.ts";
23
import { makeMemberUserView } from "#common/template/user.ts";
34
import type { SquirrelDiscordContext } from "#discord/index.ts";
@@ -40,15 +41,8 @@ async function handleEdit(
4041
guild: Guild,
4142
actor: Member,
4243
oldTag: Tag,
43-
newTag: Tag,
44+
changes: Nullable<Tag>,
4445
): Promise<void> {
45-
const nameChanged = oldTag.name !== newTag.name;
46-
const contentChanged = oldTag.content !== newTag.content;
47-
48-
if (!(nameChanged || contentChanged)) {
49-
return;
50-
}
51-
5246
await logEvent(ctx, {
5347
guild,
5448
key: "tagEdit",
@@ -57,9 +51,12 @@ async function handleEdit(
5751
guild: makeGuildView(guild),
5852
actor: makeMemberUserView(actor),
5953
oldTag: oldTag,
60-
newTag: newTag,
61-
nameChanged: nameChanged,
62-
contentChanged: contentChanged,
54+
newTag: {
55+
name: changes.name ?? oldTag.name,
56+
content: changes.content ?? oldTag.content,
57+
},
58+
nameChanged: changes.name !== null,
59+
contentChanged: changes.content !== null,
6360
};
6461
},
6562
});

backend/src/plugin/tags/command/tagEdit.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { escapeMarkdown } from "#common/discord/markdown.ts";
2+
import type { Nullable } from "#common/general.ts";
23
import { moduleLogger } from "#common/logger/index.ts";
34
import { OptionType } from "#plugin/core/public/command.ts";
45
import { defineCommand } from "#plugin/core/public/extensionPoints.ts";
@@ -11,6 +12,7 @@ import {
1112
import { autocompleteTags } from "#plugin/tags/helper/autocompletion.ts";
1213
import { tagsConfigStore } from "#plugin/tags/index.ts";
1314
import { onTagEdited } from "#plugin/tags/public/extensionPoints.ts";
15+
import type { Tag } from "#plugin/tags/public/tag.ts";
1416
import { updateTag } from "#plugin/tags/storage/tags.ts";
1517

1618
const logger = moduleLogger();
@@ -52,14 +54,16 @@ export default defineCommand({
5254
(permissions) => permissions.tagEdit,
5355
),
5456
async run(ctx, args) {
57+
const changes = {
58+
name: args.newName,
59+
content: args.content,
60+
};
61+
5562
const oldTag = await updateTag(
5663
ctx.squirrelCtx.db,
5764
ctx.guild.id,
5865
args.name,
59-
{
60-
name: args.newName ?? undefined,
61-
content: args.content ?? undefined,
62-
},
66+
changes,
6367
);
6468

6569
if (oldTag === null) {
@@ -69,13 +73,14 @@ export default defineCommand({
6973
return;
7074
}
7175

72-
const newTag = {
73-
name: args.newName ?? oldTag.name,
74-
content: args.content ?? oldTag.content,
75-
};
76+
// check after we know the tag exists
77+
if (args.newName === null && args.content === null) {
78+
await ctx.respond(`${icons.error} No changes specified!`);
79+
return;
80+
}
7681

7782
onTagEdited
78-
.fire(ctx.squirrelCtx, ctx.guild, ctx.member, oldTag, newTag)
83+
.fire(ctx.squirrelCtx, ctx.guild, ctx.member, oldTag, changes)
7984
.catch((error) => logger.error?.("Error in onTagDeleted", error));
8085

8186
await ctx.respond(

backend/src/plugin/tags/public/extensionPoints.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Nullable } from "#common/general.ts";
12
import type { SquirrelDiscordContext } from "#discord/index.ts";
23
import { makeEventExtensionPoint } from "#extensionPoint.ts";
34
import type { Tag } from "#plugin/tags/public/tag.ts";
@@ -14,7 +15,7 @@ type BinaryEvent = [
1415
guild: Guild,
1516
actor: Member,
1617
oldTag: Tag,
17-
newTag: Tag,
18+
changes: Nullable<Tag>,
1819
];
1920

2021
export const onTagCreated = makeEventExtensionPoint<UnaryEvent>();

backend/src/plugin/tags/storage/tags.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Nullable } from "#common/general.ts";
12
import type { Tag } from "#plugin/tags/public/tag.ts";
23
import { dbParse } from "#storage/index.ts";
34
import type { Pool } from "pg";
@@ -77,7 +78,7 @@ export async function updateTag(
7778
db: Pool,
7879
guildID: string,
7980
name: string,
80-
tag: Partial<Tag>,
81+
tag: Nullable<Tag>,
8182
): Promise<Tag | null> {
8283
const result = await db.query(
8384
`

0 commit comments

Comments
 (0)