allow using duplicate notifications to remove queued items

This commit is contained in:
bakatrouble 2023-08-13 22:21:19 +03:00
parent b2b727597b
commit 5bfc531b39
2 changed files with 24 additions and 2 deletions

View File

@ -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=""),
),
]

View File

@ -10,6 +10,7 @@ import imagehash
import requests import requests
from PIL import Image from PIL import Image
from django.db import models from django.db import models
from django.db.models import Q
from telegram import Update, Bot, InputMediaPhoto from telegram import Update, Bot, InputMediaPhoto
from telegram.ext import Dispatcher, CallbackContext, MessageHandler, Filters, CommandHandler from telegram.ext import Dispatcher, CallbackContext, MessageHandler, Filters, CommandHandler
from jsonrpc import Dispatcher as RPCDispatcher 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() duplicate = self.queued_items.filter(image_hash=i.image_hash, type='photo').first()
if duplicate: if duplicate:
try: 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: except:
traceback.print_exc() traceback.print_exc()
update.message.reply_text('Could not send duplicate original', quote=True) update.message.reply_text('Could not send duplicate original', quote=True)
@ -151,7 +154,8 @@ class ChannelHelperBotModuleConfig(TelegramBotModuleConfig):
return return
reply_to_id = update.effective_message.reply_to_message.message_id reply_to_id = update.effective_message.reply_to_message.message_id
try: 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() msg.delete()
update.effective_message.reply_text('Deleted') update.effective_message.reply_text('Deleted')
except QueuedItem.DoesNotExist: except QueuedItem.DoesNotExist:
@ -188,6 +192,7 @@ class QueuedItem(models.Model):
type = models.CharField(max_length=12) type = models.CharField(max_length=12)
args = models.TextField() args = models.TextField()
message_id = models.PositiveBigIntegerField(default=None, db_index=True, null=True, blank=True) 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) image_hash = models.CharField(max_length=64, null=True, blank=True)
processed = models.BooleanField(default=False) processed = models.BooleanField(default=False)
datetime = models.DateTimeField(auto_now_add=True) datetime = models.DateTimeField(auto_now_add=True)