ticket #85 attempt 1
Some checks failed
DevPlace CI / test (pull_request) Failing after 1h15m13s
Some checks failed
DevPlace CI / test (pull_request) Failing after 1h15m13s
This commit is contained in:
parent
1d1efc0dfc
commit
54a7805a90
@ -30,6 +30,26 @@ def migrate_bug_tables_to_issue_tables() -> None:
|
||||
logger.info("Dropped table %s after migration", source_name)
|
||||
|
||||
|
||||
def _add_message_unique_index() -> None:
|
||||
if "messages" not in db.tables:
|
||||
return
|
||||
with db:
|
||||
db.query(
|
||||
"""
|
||||
DELETE FROM messages WHERE id NOT IN (
|
||||
SELECT MIN(id) FROM messages GROUP BY sender_uid, receiver_uid, content
|
||||
)
|
||||
"""
|
||||
)
|
||||
_index(
|
||||
db,
|
||||
"messages",
|
||||
"idx_messages_unique_sender_receiver_content",
|
||||
["sender_uid", "receiver_uid", "content"],
|
||||
unique=True,
|
||||
)
|
||||
|
||||
|
||||
def init_db():
|
||||
tables = db.tables
|
||||
_index(db, "users", "idx_users_username", ["username"])
|
||||
@ -131,6 +151,7 @@ def init_db():
|
||||
"idx_messages_conversation_rev",
|
||||
["receiver_uid", "sender_uid"],
|
||||
)
|
||||
_add_message_unique_index()
|
||||
_index(db, "notifications", "idx_notifications_user", ["user_uid"])
|
||||
_index(db, "notifications", "idx_notifications_user_read", ["user_uid", "read"])
|
||||
_index(db, "push_registration", "idx_push_registration_user", ["user_uid"])
|
||||
|
||||
@ -18,6 +18,8 @@ from devplacepy.utils import (
|
||||
track_action,
|
||||
)
|
||||
from devplacepy.services.audit import record as audit
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from devplacepy.services.correction import schedule_correction
|
||||
from devplacepy.services.ai_modifier import schedule_modification
|
||||
|
||||
@ -110,16 +112,36 @@ def persist_message(
|
||||
messages_table = get_table("messages")
|
||||
msg_uid = generate_uid()
|
||||
created_at = datetime.now(timezone.utc).isoformat()
|
||||
messages_table.insert(
|
||||
{
|
||||
"uid": msg_uid,
|
||||
"sender_uid": sender_uid,
|
||||
"receiver_uid": receiver_uid,
|
||||
"content": content,
|
||||
"read": False,
|
||||
"created_at": created_at,
|
||||
|
||||
try:
|
||||
messages_table.insert(
|
||||
{
|
||||
"uid": msg_uid,
|
||||
"sender_uid": sender_uid,
|
||||
"receiver_uid": receiver_uid,
|
||||
"content": content,
|
||||
"read": False,
|
||||
"created_at": created_at,
|
||||
}
|
||||
)
|
||||
except IntegrityError:
|
||||
existing = messages_table.find_one(
|
||||
sender_uid=sender_uid, receiver_uid=receiver_uid, content=content
|
||||
)
|
||||
if not existing:
|
||||
raise
|
||||
logger.debug(
|
||||
"Dedup via unique constraint for message (uid %s)", existing["uid"]
|
||||
)
|
||||
_content_cache[content_hash] = (time.time(), existing["uid"])
|
||||
return {
|
||||
"uid": existing["uid"],
|
||||
"sender_uid": existing["sender_uid"],
|
||||
"receiver_uid": existing["receiver_uid"],
|
||||
"content": existing["content"],
|
||||
"read": existing.get("read", False),
|
||||
"created_at": existing["created_at"],
|
||||
}
|
||||
)
|
||||
|
||||
link_attachments(attachment_uids, "message", msg_uid)
|
||||
schedule_correction(sender, "messages", msg_uid, request)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user