forked from retoor/devplacepy
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
54a7805a90 | ||
|
|
1d1efc0dfc |
File diff suppressed because one or more lines are too long
@@ -30,6 +30,26 @@ def migrate_bug_tables_to_issue_tables() -> None:
|
|||||||
logger.info("Dropped table %s after migration", source_name)
|
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():
|
def init_db():
|
||||||
tables = db.tables
|
tables = db.tables
|
||||||
_index(db, "users", "idx_users_username", ["username"])
|
_index(db, "users", "idx_users_username", ["username"])
|
||||||
@@ -42,7 +62,6 @@ def init_db():
|
|||||||
_index(db, "posts", "idx_posts_created_at", ["created_at"])
|
_index(db, "posts", "idx_posts_created_at", ["created_at"])
|
||||||
_index(db, "posts", "idx_posts_topic", ["topic"])
|
_index(db, "posts", "idx_posts_topic", ["topic"])
|
||||||
_index(db, "posts", "idx_posts_slug", ["slug"])
|
_index(db, "posts", "idx_posts_slug", ["slug"])
|
||||||
_index(db, "posts", "idx_posts_project_uid", ["project_uid"])
|
|
||||||
if "posts" in tables:
|
if "posts" in tables:
|
||||||
posts_table = get_table("posts")
|
posts_table = get_table("posts")
|
||||||
if not posts_table.has_column("tags"):
|
if not posts_table.has_column("tags"):
|
||||||
@@ -132,6 +151,7 @@ def init_db():
|
|||||||
"idx_messages_conversation_rev",
|
"idx_messages_conversation_rev",
|
||||||
["receiver_uid", "sender_uid"],
|
["receiver_uid", "sender_uid"],
|
||||||
)
|
)
|
||||||
|
_add_message_unique_index()
|
||||||
_index(db, "notifications", "idx_notifications_user", ["user_uid"])
|
_index(db, "notifications", "idx_notifications_user", ["user_uid"])
|
||||||
_index(db, "notifications", "idx_notifications_user_read", ["user_uid", "read"])
|
_index(db, "notifications", "idx_notifications_user_read", ["user_uid", "read"])
|
||||||
_index(db, "push_registration", "idx_push_registration_user", ["user_uid"])
|
_index(db, "push_registration", "idx_push_registration_user", ["user_uid"])
|
||||||
|
|||||||
@@ -216,15 +216,6 @@ async def project_detail(request: Request, project_slug: str):
|
|||||||
if parent
|
if parent
|
||||||
else None
|
else None
|
||||||
)
|
)
|
||||||
posts_table = get_table("posts")
|
|
||||||
project_posts = list(
|
|
||||||
posts_table.find(
|
|
||||||
project_uid=project["uid"],
|
|
||||||
deleted_at=None,
|
|
||||||
order_by=["-created_at"],
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
return respond(
|
return respond(
|
||||||
request,
|
request,
|
||||||
"project_detail.html",
|
"project_detail.html",
|
||||||
@@ -244,7 +235,6 @@ async def project_detail(request: Request, project_slug: str):
|
|||||||
"forked_from": forked_from,
|
"forked_from": forked_from,
|
||||||
"fork_count": count_forks(project["uid"]),
|
"fork_count": count_forks(project["uid"]),
|
||||||
"file_count": count_files(project["uid"]),
|
"file_count": count_files(project["uid"]),
|
||||||
"project_posts": project_posts,
|
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
model=ProjectDetailOut,
|
model=ProjectDetailOut,
|
||||||
|
|||||||
@@ -163,7 +163,6 @@ class ProjectDetailOut(_Out):
|
|||||||
forked_from: Optional[dict] = None
|
forked_from: Optional[dict] = None
|
||||||
fork_count: int = 0
|
fork_count: int = 0
|
||||||
file_count: int = 0
|
file_count: int = 0
|
||||||
project_posts: list[PostOut] = []
|
|
||||||
|
|
||||||
|
|
||||||
class GistsOut(_Out):
|
class GistsOut(_Out):
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ def gateway_complete(
|
|||||||
{"role": "system", "content": system},
|
{"role": "system", "content": system},
|
||||||
{"role": "user", "content": text},
|
{"role": "user", "content": text},
|
||||||
],
|
],
|
||||||
"temperature": 0.1,
|
"temperature": 0.0,
|
||||||
}
|
}
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
# retoor <retoor@molodetz.nl>
|
# retoor <retoor@molodetz.nl>
|
||||||
|
|
||||||
|
import hashlib
|
||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
|
from collections import OrderedDict
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
@@ -15,12 +18,16 @@ from devplacepy.utils import (
|
|||||||
track_action,
|
track_action,
|
||||||
)
|
)
|
||||||
from devplacepy.services.audit import record as audit
|
from devplacepy.services.audit import record as audit
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
|
||||||
from devplacepy.services.correction import schedule_correction
|
from devplacepy.services.correction import schedule_correction
|
||||||
from devplacepy.services.ai_modifier import schedule_modification
|
from devplacepy.services.ai_modifier import schedule_modification
|
||||||
|
|
||||||
logger = logging.getLogger("messaging.persist")
|
logger = logging.getLogger("messaging.persist")
|
||||||
|
|
||||||
MAX_CONTENT_LENGTH = 2000
|
MAX_CONTENT_LENGTH = 2000
|
||||||
|
DEDUP_WINDOW_SECONDS = 3
|
||||||
|
_content_cache: dict[str, tuple[float, str]] = OrderedDict()
|
||||||
|
|
||||||
|
|
||||||
def _slim_attachment(attachment: dict[str, Any]) -> dict[str, Any]:
|
def _slim_attachment(attachment: dict[str, Any]) -> dict[str, Any]:
|
||||||
@@ -81,19 +88,60 @@ def persist_message(
|
|||||||
|
|
||||||
sender_uid = sender["uid"]
|
sender_uid = sender["uid"]
|
||||||
sender_username = sender.get("username", "")
|
sender_username = sender.get("username", "")
|
||||||
|
|
||||||
|
content_hash = hashlib.sha256(
|
||||||
|
f"{sender_uid}:{receiver_uid}:{content}".encode()
|
||||||
|
).hexdigest()[:16]
|
||||||
|
now = time.time()
|
||||||
|
last_seen, cached_uid = _content_cache.get(content_hash, (0.0, None))
|
||||||
|
if now - last_seen < DEDUP_WINDOW_SECONDS and cached_uid is not None:
|
||||||
|
logger.debug(
|
||||||
|
"Dedup hit for message hash %s (original uid %s)", content_hash, cached_uid
|
||||||
|
)
|
||||||
|
cached = get_table("messages").find_one(uid=cached_uid)
|
||||||
|
if cached:
|
||||||
|
return {
|
||||||
|
"uid": cached["uid"],
|
||||||
|
"sender_uid": cached["sender_uid"],
|
||||||
|
"receiver_uid": cached["receiver_uid"],
|
||||||
|
"content": cached["content"],
|
||||||
|
"read": cached.get("read", False),
|
||||||
|
"created_at": cached["created_at"],
|
||||||
|
}
|
||||||
|
|
||||||
messages_table = get_table("messages")
|
messages_table = get_table("messages")
|
||||||
msg_uid = generate_uid()
|
msg_uid = generate_uid()
|
||||||
created_at = datetime.now(timezone.utc).isoformat()
|
created_at = datetime.now(timezone.utc).isoformat()
|
||||||
messages_table.insert(
|
|
||||||
{
|
try:
|
||||||
"uid": msg_uid,
|
messages_table.insert(
|
||||||
"sender_uid": sender_uid,
|
{
|
||||||
"receiver_uid": receiver_uid,
|
"uid": msg_uid,
|
||||||
"content": content,
|
"sender_uid": sender_uid,
|
||||||
"read": False,
|
"receiver_uid": receiver_uid,
|
||||||
"created_at": created_at,
|
"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)
|
link_attachments(attachment_uids, "message", msg_uid)
|
||||||
schedule_correction(sender, "messages", msg_uid, request)
|
schedule_correction(sender, "messages", msg_uid, request)
|
||||||
@@ -114,6 +162,8 @@ def persist_message(
|
|||||||
)
|
)
|
||||||
track_action(sender_uid, "message")
|
track_action(sender_uid, "message")
|
||||||
|
|
||||||
|
_content_cache[content_hash] = (time.time(), msg_uid)
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
"Message %s sent from %s to %s via %s",
|
"Message %s sent from %s to %s via %s",
|
||||||
msg_uid,
|
msg_uid,
|
||||||
|
|||||||
@@ -313,50 +313,3 @@
|
|||||||
color: var(--text-muted);
|
color: var(--text-muted);
|
||||||
margin-bottom: 0.5rem;
|
margin-bottom: 0.5rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.project-devlog {
|
|
||||||
margin-top: 2rem;
|
|
||||||
padding-top: 1.5rem;
|
|
||||||
border-top: 1px solid var(--border);
|
|
||||||
}
|
|
||||||
|
|
||||||
.devlog-list {
|
|
||||||
list-style: none;
|
|
||||||
padding: 0;
|
|
||||||
margin: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.devlog-item {
|
|
||||||
display: flex;
|
|
||||||
justify-content: space-between;
|
|
||||||
align-items: center;
|
|
||||||
padding: 0.5rem 0;
|
|
||||||
border-bottom: 1px solid var(--border);
|
|
||||||
gap: 1rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.devlog-item:last-child {
|
|
||||||
border-bottom: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
.devlog-link {
|
|
||||||
color: var(--text-primary);
|
|
||||||
text-decoration: none;
|
|
||||||
font-weight: 500;
|
|
||||||
}
|
|
||||||
|
|
||||||
.devlog-link:hover {
|
|
||||||
color: var(--accent);
|
|
||||||
}
|
|
||||||
|
|
||||||
.devlog-date {
|
|
||||||
font-size: 0.8125rem;
|
|
||||||
color: var(--text-muted);
|
|
||||||
white-space: nowrap;
|
|
||||||
flex-shrink: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.devlog-empty {
|
|
||||||
color: var(--text-muted);
|
|
||||||
font-size: 0.875rem;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -170,22 +170,6 @@
|
|||||||
{% endcall %}
|
{% endcall %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<div class="project-devlog">
|
|
||||||
<h2 class="project-section-label">Devlog</h2>
|
|
||||||
{% if project_posts %}
|
|
||||||
<ul class="devlog-list">
|
|
||||||
{% for post in project_posts %}
|
|
||||||
<li class="devlog-item">
|
|
||||||
<a href="/posts/{{ post['slug'] }}" class="devlog-link">{{ render_title(post['title']) }}</a>
|
|
||||||
<span class="devlog-date">{{ local_dt(post['created_at'], 'date') }}</span>
|
|
||||||
</li>
|
|
||||||
{% endfor %}
|
|
||||||
</ul>
|
|
||||||
{% else %}
|
|
||||||
<p class="devlog-empty">No posts yet for this project.</p>
|
|
||||||
{% endif %}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
{% with target_uid=project['uid'], target_type="project" %}
|
{% with target_uid=project['uid'], target_type="project" %}
|
||||||
{% include "_comment_section.html" %}
|
{% include "_comment_section.html" %}
|
||||||
{% endwith %}
|
{% endwith %}
|
||||||
|
|||||||
@@ -166,3 +166,23 @@ def test_send_attachment_only_empty_content_succeeds(seeded_db):
|
|||||||
refresh_snapshot()
|
refresh_snapshot()
|
||||||
row = get_table("messages").find_one(uid=msg["uid"])
|
row = get_table("messages").find_one(uid=msg["uid"])
|
||||||
assert row["content"] == ""
|
assert row["content"] == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_duplicate_message_returns_same_uid(seeded_db):
|
||||||
|
s, _ = _member()
|
||||||
|
receiver = _db_user("bob_test")["uid"]
|
||||||
|
content = _unique("dupmsg")
|
||||||
|
|
||||||
|
first = s.post(
|
||||||
|
f"{BASE_URL}/messages/send",
|
||||||
|
headers=JSON_audit_log,
|
||||||
|
data={"content": content, "receiver_uid": receiver},
|
||||||
|
).json()["data"]
|
||||||
|
|
||||||
|
second = s.post(
|
||||||
|
f"{BASE_URL}/messages/send",
|
||||||
|
headers=JSON_audit_log,
|
||||||
|
data={"content": content, "receiver_uid": receiver},
|
||||||
|
).json()["data"]
|
||||||
|
|
||||||
|
assert first["uid"] == second["uid"], "duplicate messages should return the same uid"
|
||||||
|
|||||||
Reference in New Issue
Block a user