Skip to content

Commit 60164d8

Browse files
authored
Merge pull request #187 from python-discord/jb3/features/content-hash
Store a MD5 hash of content for stored messages
2 parents 6ce69f2 + e433585 commit 60164d8

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Add content_hash field to messages model.
3+
4+
Revision ID: a192a8d3282c
5+
Revises: 01b101590e74
6+
Create Date: 2024-09-10 16:32:46.593911
7+
8+
"""
9+
import sqlalchemy as sa
10+
11+
from alembic import op
12+
13+
# revision identifiers, used by Alembic.
14+
revision = "a192a8d3282c"
15+
down_revision = "01b101590e74"
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
def upgrade() -> None:
21+
"""Apply the current migration."""
22+
op.add_column("messages", sa.Column("content_hash", sa.String(), nullable=True))
23+
24+
25+
def downgrade() -> None:
26+
"""Revert the current migration."""
27+
op.drop_column("messages", "content_hash")

metricity/__main__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ async def main() -> None:
2424
voice_states=False,
2525
presences=False,
2626
messages=True,
27+
message_content=True,
2728
reactions=False,
2829
typing=False,
2930
)

metricity/exts/event_listeners/_syncer_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import binascii
2+
import hashlib
3+
14
import discord
25
from pydis_core.utils import logging
36
from sqlalchemy import update
@@ -30,11 +33,17 @@ async def sync_message(message: discord.Message, sess: AsyncSession, *, from_thr
3033
if await sess.get(models.Message, str(message.id)):
3134
return
3235

36+
hash_ctx = hashlib.md5() # noqa: S324
37+
hash_ctx.update(message.content.encode())
38+
digest = hash_ctx.digest()
39+
digest_encoded = binascii.hexlify(digest).decode()
40+
3341
args = {
3442
"id": str(message.id),
3543
"channel_id": str(message.channel.id),
3644
"author_id": str(message.author.id),
3745
"created_at": message.created_at,
46+
"content_hash": digest_encoded,
3847
}
3948

4049
if from_thread:

metricity/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,4 @@ class Message(Base):
7878
author_id: Mapped[str] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
7979
created_at = mapped_column(TZDateTime())
8080
is_deleted: Mapped[bool] = mapped_column(default=False)
81+
content_hash: Mapped[str] = mapped_column(nullable=True)

0 commit comments

Comments
 (0)