Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store a MD5 hash of content for stored messages #187

Merged
merged 5 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions alembic/versions/a192a8d3282c_add_content_hash.py
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions metricity/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async def main() -> None:
voice_states=False,
presences=False,
messages=True,
message_content=True,
reactions=False,
typing=False,
)
Expand Down
9 changes: 9 additions & 0 deletions metricity/exts/event_listeners/_syncer_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import binascii
import hashlib

import discord
from pydis_core.utils import logging
from sqlalchemy import update
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions metricity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading