diff --git a/src/VahterBanBot.Tests/ContainerTestBase.fs b/src/VahterBanBot.Tests/ContainerTestBase.fs index 0861c48..0e8df2a 100644 --- a/src/VahterBanBot.Tests/ContainerTestBase.fs +++ b/src/VahterBanBot.Tests/ContainerTestBase.fs @@ -228,10 +228,9 @@ WHERE data ->> 'Case' = @caseName //language=postgresql let sql = """ SELECT COUNT(*) FROM false_positive_messages -WHERE chat_id = @chatId - AND message_id = @messageId +WHERE text = @text """ - let! result = conn.QuerySingleAsync(sql, {| chatId = msg.Chat.Id; messageId = msg.MessageId |}) + let! result = conn.QuerySingleAsync(sql, {| text = msg.Text |}) return result > 0 } diff --git a/src/VahterBanBot.Tests/MLBanTests.fs b/src/VahterBanBot.Tests/MLBanTests.fs index 9cadd33..944416f 100644 --- a/src/VahterBanBot.Tests/MLBanTests.fs +++ b/src/VahterBanBot.Tests/MLBanTests.fs @@ -71,7 +71,7 @@ type MLBanTests(fixture: VahterTestContainers, _unused: MlAwaitFixture) = let ``If message got auto-deleted we can mark it as false-positive with a button click`` () = task { // record a message, where 2 is in a training set as spam word // ChatsToMonitor[0] doesn't have stopwords - let msgUpdate = Tg.quickMsg(chat = fixture.ChatsToMonitor[0], text = "2") + let msgUpdate = Tg.quickMsg(chat = fixture.ChatsToMonitor[0], text = "7") let! _ = fixture.SendMessage msgUpdate // assert that the message got auto banned @@ -105,7 +105,7 @@ type MLBanTests(fixture: VahterTestContainers, _unused: MlAwaitFixture) = // send a callback to mark it as false-positive // we are sending this as a usual user let! callbackId = fixture.GetCallbackId msgUpdate.Message (nameof CallbackMessage.NotASpam) - let msgCallback = Tg.callback(string callbackId) + let msgCallback = Tg.callback(string callbackId, from = msgUpdate.Message.From) let! _ = fixture.SendMessage msgCallback // assert it is still NOT a false-positive diff --git a/src/VahterBanBot.Tests/test_seed.sql b/src/VahterBanBot.Tests/test_seed.sql index 62efad3..c3af0fa 100644 --- a/src/VahterBanBot.Tests/test_seed.sql +++ b/src/VahterBanBot.Tests/test_seed.sql @@ -341,8 +341,8 @@ VALUES (100001, 10001, 'a', 1001, '2021-01-01 00:00:00', -666, 'pro.hell', 34), INSERT INTO public.false_positive_users(user_id) VALUES (1001); -INSERT INTO public.false_positive_messages(chat_id, message_id) -VALUES (-666, 10008); +INSERT INTO public.false_positive_messages(text) +VALUES ('a'); INSERT INTO public.false_negative_messages(chat_id, message_id) VALUES (-42, 10008), diff --git a/src/VahterBanBot/DB.fs b/src/VahterBanBot/DB.fs index 09326b5..a2f7c6f 100644 --- a/src/VahterBanBot/DB.fs +++ b/src/VahterBanBot/DB.fs @@ -166,7 +166,7 @@ let getUserById (userId: int64): Task = use conn = new NpgsqlConnection(connString) //language=postgresql - let sql = "SELECT * FROM \"user\" WHERE id = @userId" + let sql = """SELECT * FROM "user" WHERE id = @userId""" let! users = conn.QueryAsync(sql, {| userId = userId |}) return users |> Seq.tryHead } @@ -190,8 +190,7 @@ WITH really_banned AS (SELECT * WHERE NOT EXISTS(SELECT 1 FROM false_positive_users fpu WHERE fpu.user_id = b.banned_user_id) AND NOT EXISTS(SELECT 1 FROM false_positive_messages fpm - WHERE fpm.chat_id = b.banned_in_chat_id - AND fpm.message_id = b.message_id) + WHERE fpm.text = b.message_text) AND b.message_text IS NOT NULL AND b.banned_at <= @criticalDate), spam_or_ham AS (SELECT DISTINCT COALESCE(m.text, re_id.message_text) AS text, @@ -241,8 +240,8 @@ let markMessageAsFalsePositive (message: DbMessage): Task = //language=postgresql let sql = """ -INSERT INTO false_positive_messages (chat_id, message_id) -VALUES (@chat_id, @message_id) +INSERT INTO false_positive_messages (text) +VALUES (@text) ON CONFLICT DO NOTHING; """ diff --git a/src/migrations/V9__callbacks.sql b/src/migrations/V9__callbacks.sql index 7563158..44a988b 100644 --- a/src/migrations/V9__callbacks.sql +++ b/src/migrations/V9__callbacks.sql @@ -12,23 +12,20 @@ CREATE INDEX idx_callback_data ON callback USING GIN (data); CREATE UNIQUE INDEX idx_callback_id ON callback (id); ALTER TABLE false_positive_messages - ADD COLUMN message_id INTEGER NULL, - ADD COLUMN chat_id BIGINT NULL; + ADD COLUMN text TEXT NULL; UPDATE false_positive_messages -SET message_id=b.message_id, - chat_id=b.banned_in_chat_id -FROM (SELECT b.message_id, b.banned_in_chat_id, b.id +SET text = b.message_text +FROM (SELECT b.message_text, b.id FROM false_positive_messages fpm JOIN public.banned b ON b.id = fpm.id) AS b WHERE false_positive_messages.id = b.id; ALTER TABLE false_positive_messages - ALTER COLUMN message_id SET NOT NULL, - ALTER COLUMN chat_id SET NOT NULL; + ALTER COLUMN text SET NOT NULL; -CREATE UNIQUE INDEX idx_false_positive_messages_chat_id_message_id - ON false_positive_messages (chat_id, message_id); +CREATE UNIQUE INDEX idx_false_positive_messages_text + ON false_positive_messages (text); ALTER TABLE false_positive_messages DROP COLUMN id;