From 5bfc531b394403486f16ec469d0650622dc38d48 Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Sun, 13 Aug 2023 22:21:19 +0300 Subject: [PATCH] allow using duplicate notifications to remove queued items --- .../0011_queueditem_message_ids_extra.py | 17 +++++++++++++++++ bots/modules/channel_helper.py | 9 +++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 bots/migrations/0011_queueditem_message_ids_extra.py diff --git a/bots/migrations/0011_queueditem_message_ids_extra.py b/bots/migrations/0011_queueditem_message_ids_extra.py new file mode 100644 index 0000000..11bc043 --- /dev/null +++ b/bots/migrations/0011_queueditem_message_ids_extra.py @@ -0,0 +1,17 @@ +# Generated by Django 4.1.1 on 2023-08-13 19:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("bots", "0010_queueditem_datetime"), + ] + + operations = [ + migrations.AddField( + model_name="queueditem", + name="message_ids_extra", + field=models.TextField(db_index=True, default=""), + ), + ] diff --git a/bots/modules/channel_helper.py b/bots/modules/channel_helper.py index 2560449..e8fcd63 100644 --- a/bots/modules/channel_helper.py +++ b/bots/modules/channel_helper.py @@ -10,6 +10,7 @@ import imagehash import requests from PIL import Image from django.db import models +from django.db.models import Q from telegram import Update, Bot, InputMediaPhoto from telegram.ext import Dispatcher, CallbackContext, MessageHandler, Filters, CommandHandler from jsonrpc import Dispatcher as RPCDispatcher @@ -103,7 +104,9 @@ class ChannelHelperBotModuleConfig(TelegramBotModuleConfig): duplicate = self.queued_items.filter(image_hash=i.image_hash, type='photo').first() if duplicate: try: - update.message.reply_photo(json.loads(duplicate.args)[0], f'Duplicate from {duplicate.datetime}', quote=True) + m = update.message.reply_photo(json.loads(duplicate.args)[0], f'Duplicate from {duplicate.datetime}', quote=True) + duplicate.message_ids_extra += f'|{m.message_id}' + duplicate.save() except: traceback.print_exc() update.message.reply_text('Could not send duplicate original', quote=True) @@ -151,7 +154,8 @@ class ChannelHelperBotModuleConfig(TelegramBotModuleConfig): return reply_to_id = update.effective_message.reply_to_message.message_id try: - msg = QueuedItem.objects.get(message_id=reply_to_id, config=self) + msg = QueuedItem.objects.get(Q(message_id=reply_to_id) | Q(message_ids_extra__contains=f'{reply_to_id}'), + config=self) msg.delete() update.effective_message.reply_text('Deleted') except QueuedItem.DoesNotExist: @@ -188,6 +192,7 @@ class QueuedItem(models.Model): type = models.CharField(max_length=12) args = models.TextField() message_id = models.PositiveBigIntegerField(default=None, db_index=True, null=True, blank=True) + message_ids_extra = models.TextField(db_index=True, default='') image_hash = models.CharField(max_length=64, null=True, blank=True) processed = models.BooleanField(default=False) datetime = models.DateTimeField(auto_now_add=True)