From 371da4f6de975f39de906641ecc2fcf197e7abac Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Tue, 10 Sep 2024 16:29:41 +0100 Subject: [PATCH 1/5] Add content_hash field to Message model --- metricity/models.py | 1 + 1 file changed, 1 insertion(+) diff --git a/metricity/models.py b/metricity/models.py index 86bb723..638e442 100644 --- a/metricity/models.py +++ b/metricity/models.py @@ -78,3 +78,4 @@ class Message(Base): author_id: Mapped[str] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True) created_at = mapped_column(TZDateTime()) is_deleted: Mapped[bool] = mapped_column(default=False) + content_hash: Mapped[str] = mapped_column(nullable=True) From 26be8e64d10016444ddebc58d86b622ffdec522a Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Tue, 10 Sep 2024 16:29:52 +0100 Subject: [PATCH 2/5] Generate MD5 hash of content when adding to database --- metricity/exts/event_listeners/_syncer_utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/metricity/exts/event_listeners/_syncer_utils.py b/metricity/exts/event_listeners/_syncer_utils.py index 043f1d1..f6b9f88 100644 --- a/metricity/exts/event_listeners/_syncer_utils.py +++ b/metricity/exts/event_listeners/_syncer_utils.py @@ -1,3 +1,6 @@ +import binascii +import hashlib + import discord from pydis_core.utils import logging from sqlalchemy import update @@ -30,11 +33,17 @@ async def sync_message(message: discord.Message, sess: AsyncSession, *, from_thr if await sess.get(models.Message, str(message.id)): return + hash_ctx = hashlib.md5() # noqa: S324 + hash_ctx.update(message.content.encode()) + digest = hash_ctx.digest() + digest_encoded = binascii.hexlify(digest).decode() + args = { "id": str(message.id), "channel_id": str(message.channel.id), "author_id": str(message.author.id), "created_at": message.created_at, + "content_hash": digest_encoded, } if from_thread: From 62d099d0f3d588bd5e2868cd8fc17818843f673e Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Tue, 10 Sep 2024 16:33:38 +0100 Subject: [PATCH 3/5] Add migration for content_hash field --- .../versions/a192a8d3282c_add_content_hash.py | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 alembic/versions/a192a8d3282c_add_content_hash.py diff --git a/alembic/versions/a192a8d3282c_add_content_hash.py b/alembic/versions/a192a8d3282c_add_content_hash.py new file mode 100644 index 0000000..1d9c7c1 --- /dev/null +++ b/alembic/versions/a192a8d3282c_add_content_hash.py @@ -0,0 +1,27 @@ +""" +Add content_hash field to messages model. + +Revision ID: a192a8d3282c +Revises: 01b101590e74 +Create Date: 2024-09-10 16:32:46.593911 + +""" +import sqlalchemy as sa + +from alembic import op + +# revision identifiers, used by Alembic. +revision = "a192a8d3282c" +down_revision = "01b101590e74" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + """Apply the current migration.""" + op.add_column("messages", sa.Column("content_hash", sa.String(), nullable=True)) + + +def downgrade() -> None: + """Revert the current migration.""" + op.drop_column("messages", "content_hash") From 32d0fe5e7b1ba32d02c38a5e6daa7a3347890e68 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Tue, 10 Sep 2024 17:01:28 +0100 Subject: [PATCH 4/5] Enable message content intent --- metricity/__main__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/metricity/__main__.py b/metricity/__main__.py index 86aba6a..f9c0497 100644 --- a/metricity/__main__.py +++ b/metricity/__main__.py @@ -24,6 +24,7 @@ async def main() -> None: voice_states=False, presences=False, messages=True, + message_content=True, reactions=False, typing=False, ) From e433585bdbc8fc7c89fe113ff9827e64dcff30a7 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Tue, 10 Sep 2024 17:05:25 +0100 Subject: [PATCH 5/5] Fix P1 in comment spacing --- metricity/exts/event_listeners/_syncer_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/metricity/exts/event_listeners/_syncer_utils.py b/metricity/exts/event_listeners/_syncer_utils.py index f6b9f88..067c476 100644 --- a/metricity/exts/event_listeners/_syncer_utils.py +++ b/metricity/exts/event_listeners/_syncer_utils.py @@ -33,7 +33,7 @@ async def sync_message(message: discord.Message, sess: AsyncSession, *, from_thr if await sess.get(models.Message, str(message.id)): return - hash_ctx = hashlib.md5() # noqa: S324 + hash_ctx = hashlib.md5() # noqa: S324 hash_ctx.update(message.content.encode()) digest = hash_ctx.digest() digest_encoded = binascii.hexlify(digest).decode()