From 7030b2db45e399e9f6a793951451d6cd320e4ab1 Mon Sep 17 00:00:00 2001 From: eliot-chang-lb Date: Sat, 6 Jan 2024 17:49:35 +0800 Subject: [PATCH 1/5] feat: add replacedText param for spammer's reply replacement --- src/scripts/__tests__/removeArticleReply.js | 60 +++++++++++++++++++++ src/scripts/removeArticleReply.js | 20 ++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/scripts/__tests__/removeArticleReply.js b/src/scripts/__tests__/removeArticleReply.js index de824bc2..586b3929 100644 --- a/src/scripts/__tests__/removeArticleReply.js +++ b/src/scripts/__tests__/removeArticleReply.js @@ -93,4 +93,64 @@ Array [ id: 'article1', }); expect(_source.normalArticleReplyCount).toBe(2); + + it('successfully deletes article-reply with replacedText and updates normalArticleReplyCount', async () => { + MockDate.set(FIXED_DATE); + + // Replace 'replacedTextValue' with the actual replaced text you want to test + const replacedTextValue = 'This is the replaced text'; + + const result = await removeArticleReply({ + userId: 'current-user', + articleId: 'article1', + replyId: 'foo', + replacedText: replacedTextValue, + }); + + MockDate.reset(); + + expect(result).toMatchInlineSnapshot(` + Array [ + Object { + "appId": "WEBSITE", + "replyId": "foo", + "status": "DELETED", + "updatedAt": "1989-06-04T00:00:00.000Z", + "userId": "current-user", + }, + Object { + "appId": "WEBSITE", + "replyId": "bar", + "status": "NORMAL", + "userId": "current-user", + }, + Object { + "appId": "WEBSITE", + "replyId": "foo2", + "status": "NORMAL", + "userId": "other-user", + }, + ] + `); + + const { + body: { _source }, + } = await client.get({ + index: 'articles', + type: 'doc', + id: 'article1', + }); + + expect(_source.normalArticleReplyCount).toBe(2); + + const { + body: { doc }, + } = await client.get({ + index: 'replies', + type: 'doc', + id: 'foo', // Replace with the actual replyId + }); + + expect(doc.text).toBe(replacedTextValue); + }); }); diff --git a/src/scripts/removeArticleReply.js b/src/scripts/removeArticleReply.js index e5a06efc..704c805e 100644 --- a/src/scripts/removeArticleReply.js +++ b/src/scripts/removeArticleReply.js @@ -3,13 +3,25 @@ A script that deletes an article reply */ +import client from 'util/client'; import { updateArticleReplyStatus } from 'graphql/mutations/UpdateArticleReplyStatus'; import yargs from 'yargs'; -async function main({ articleId, replyId, userId } = {}) { +async function main({ articleId, replyId, userId, replacedText } = {}) { if (!articleId || !replyId || !userId) throw new Error('Please provide all of articleId, replyId and userId'); + if (replacedText) { + const replyBody = { + text: replacedText, + }; + + await client.update({ + index: 'replies', + type: 'doc', + body: replyBody, + }); + } return updateArticleReplyStatus({ articleId, replyId, @@ -43,6 +55,12 @@ if (require.main === module) { type: 'string', demandOption: true, }, + replacedText: { + alias: 'rt', + description: 'Replaced text for spammer reply', + type: 'string', + demandOption: false, + }, }) .help('help').argv; From 787e0b9818d9dd117c5f5582b070fce4f6303201 Mon Sep 17 00:00:00 2001 From: eliot-chang-lb Date: Sat, 6 Jan 2024 19:59:39 +0800 Subject: [PATCH 2/5] test: separate the test case --- src/scripts/__tests__/removeArticleReply.js | 102 ++++++++++---------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/src/scripts/__tests__/removeArticleReply.js b/src/scripts/__tests__/removeArticleReply.js index 586b3929..9a56268a 100644 --- a/src/scripts/__tests__/removeArticleReply.js +++ b/src/scripts/__tests__/removeArticleReply.js @@ -93,64 +93,64 @@ Array [ id: 'article1', }); expect(_source.normalArticleReplyCount).toBe(2); +}); - it('successfully deletes article-reply with replacedText and updates normalArticleReplyCount', async () => { - MockDate.set(FIXED_DATE); +it('successfully deletes article-reply with replacedText and updates normalArticleReplyCount', async () => { + MockDate.set(FIXED_DATE); - // Replace 'replacedTextValue' with the actual replaced text you want to test - const replacedTextValue = 'This is the replaced text'; + // Replace 'replacedTextValue' with the actual replaced text you want to test + const replacedTextValue = 'This is the replaced text'; - const result = await removeArticleReply({ - userId: 'current-user', - articleId: 'article1', - replyId: 'foo', - replacedText: replacedTextValue, - }); - - MockDate.reset(); + const result = await removeArticleReply({ + userId: 'current-user', + articleId: 'article1', + replyId: 'foo', + replacedText: replacedTextValue, + }); - expect(result).toMatchInlineSnapshot(` - Array [ - Object { - "appId": "WEBSITE", - "replyId": "foo", - "status": "DELETED", - "updatedAt": "1989-06-04T00:00:00.000Z", - "userId": "current-user", - }, - Object { - "appId": "WEBSITE", - "replyId": "bar", - "status": "NORMAL", - "userId": "current-user", - }, - Object { - "appId": "WEBSITE", - "replyId": "foo2", - "status": "NORMAL", - "userId": "other-user", - }, - ] - `); + MockDate.reset(); - const { - body: { _source }, - } = await client.get({ - index: 'articles', - type: 'doc', - id: 'article1', - }); + expect(result).toMatchInlineSnapshot(` + Array [ + Object { + "appId": "WEBSITE", + "replyId": "foo", + "status": "DELETED", + "updatedAt": "1989-06-04T00:00:00.000Z", + "userId": "current-user", + }, + Object { + "appId": "WEBSITE", + "replyId": "bar", + "status": "NORMAL", + "userId": "current-user", + }, + Object { + "appId": "WEBSITE", + "replyId": "foo2", + "status": "NORMAL", + "userId": "other-user", + }, + ] + `); - expect(_source.normalArticleReplyCount).toBe(2); + const { + body: { _source }, + } = await client.get({ + index: 'articles', + type: 'doc', + id: 'article1', + }); - const { - body: { doc }, - } = await client.get({ - index: 'replies', - type: 'doc', - id: 'foo', // Replace with the actual replyId - }); + expect(_source.normalArticleReplyCount).toBe(2); - expect(doc.text).toBe(replacedTextValue); + const { + body: { doc }, + } = await client.get({ + index: 'replies', + type: 'doc', + id: 'foo', }); + + expect(doc.text).toBe(replacedTextValue); }); From 40240bda8f8f4229a57b96ecb5312e4aa47e7579 Mon Sep 17 00:00:00 2001 From: eliot-chang-lb Date: Sat, 6 Jan 2024 20:22:47 +0800 Subject: [PATCH 3/5] test: modify test case --- src/scripts/__tests__/removeArticleReply.js | 56 ++------------------- 1 file changed, 5 insertions(+), 51 deletions(-) diff --git a/src/scripts/__tests__/removeArticleReply.js b/src/scripts/__tests__/removeArticleReply.js index 9a56268a..5fed5082 100644 --- a/src/scripts/__tests__/removeArticleReply.js +++ b/src/scripts/__tests__/removeArticleReply.js @@ -55,11 +55,16 @@ it('should abort when article or reply ID is wrong', async () => { it('successfully deletes article-reply and updates normalArticleReplyCount', async () => { MockDate.set(FIXED_DATE); + + const replacedTextValue = 'This is the replaced text'; + const result = await removeArticleReply({ userId: 'current-user', articleId: 'article1', replyId: 'foo', + replacedText: replacedTextValue, }); + MockDate.reset(); expect(result).toMatchInlineSnapshot(` @@ -93,56 +98,6 @@ Array [ id: 'article1', }); expect(_source.normalArticleReplyCount).toBe(2); -}); - -it('successfully deletes article-reply with replacedText and updates normalArticleReplyCount', async () => { - MockDate.set(FIXED_DATE); - - // Replace 'replacedTextValue' with the actual replaced text you want to test - const replacedTextValue = 'This is the replaced text'; - - const result = await removeArticleReply({ - userId: 'current-user', - articleId: 'article1', - replyId: 'foo', - replacedText: replacedTextValue, - }); - - MockDate.reset(); - - expect(result).toMatchInlineSnapshot(` - Array [ - Object { - "appId": "WEBSITE", - "replyId": "foo", - "status": "DELETED", - "updatedAt": "1989-06-04T00:00:00.000Z", - "userId": "current-user", - }, - Object { - "appId": "WEBSITE", - "replyId": "bar", - "status": "NORMAL", - "userId": "current-user", - }, - Object { - "appId": "WEBSITE", - "replyId": "foo2", - "status": "NORMAL", - "userId": "other-user", - }, - ] - `); - - const { - body: { _source }, - } = await client.get({ - index: 'articles', - type: 'doc', - id: 'article1', - }); - - expect(_source.normalArticleReplyCount).toBe(2); const { body: { doc }, @@ -151,6 +106,5 @@ it('successfully deletes article-reply with replacedText and updates normalArtic type: 'doc', id: 'foo', }); - expect(doc.text).toBe(replacedTextValue); }); From 7c3739d02fc60ef9e3b0a698ac387a96de307c3a Mon Sep 17 00:00:00 2001 From: eliot-chang-lb Date: Sat, 6 Jan 2024 20:39:11 +0800 Subject: [PATCH 4/5] test: remove test case --- src/scripts/__tests__/removeArticleReply.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/scripts/__tests__/removeArticleReply.js b/src/scripts/__tests__/removeArticleReply.js index 5fed5082..a1e0d493 100644 --- a/src/scripts/__tests__/removeArticleReply.js +++ b/src/scripts/__tests__/removeArticleReply.js @@ -55,14 +55,10 @@ it('should abort when article or reply ID is wrong', async () => { it('successfully deletes article-reply and updates normalArticleReplyCount', async () => { MockDate.set(FIXED_DATE); - - const replacedTextValue = 'This is the replaced text'; - const result = await removeArticleReply({ userId: 'current-user', articleId: 'article1', replyId: 'foo', - replacedText: replacedTextValue, }); MockDate.reset(); @@ -98,13 +94,4 @@ Array [ id: 'article1', }); expect(_source.normalArticleReplyCount).toBe(2); - - const { - body: { doc }, - } = await client.get({ - index: 'replies', - type: 'doc', - id: 'foo', - }); - expect(doc.text).toBe(replacedTextValue); }); From fb90e2a271663d5920d82705060ec33ebd4b138e Mon Sep 17 00:00:00 2001 From: eliot-chang-lb Date: Sat, 6 Jan 2024 20:40:17 +0800 Subject: [PATCH 5/5] style: remove indent --- src/scripts/__tests__/removeArticleReply.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/scripts/__tests__/removeArticleReply.js b/src/scripts/__tests__/removeArticleReply.js index a1e0d493..de824bc2 100644 --- a/src/scripts/__tests__/removeArticleReply.js +++ b/src/scripts/__tests__/removeArticleReply.js @@ -60,7 +60,6 @@ it('successfully deletes article-reply and updates normalArticleReplyCount', asy articleId: 'article1', replyId: 'foo', }); - MockDate.reset(); expect(result).toMatchInlineSnapshot(`