mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 06:23:38 +00:00
We start by including functions that do custom queries for topic history. The goal of this library is partly to quarantine the legacy "subject" column on Message.
90 lines
2.8 KiB
Python
90 lines
2.8 KiB
Python
from django.db import connection
|
|
|
|
from zerver.models import UserProfile, Recipient
|
|
|
|
from typing import Any, Dict, List, Tuple
|
|
|
|
def generate_topic_history_from_db_rows(rows: List[Tuple[str, int]]) -> List[Dict[str, Any]]:
|
|
canonical_topic_names = {} # type: Dict[str, Tuple[int, str]]
|
|
|
|
# Sort rows by max_message_id so that if a topic
|
|
# has many different casings, we use the most
|
|
# recent row.
|
|
rows = sorted(rows, key=lambda tup: tup[1])
|
|
|
|
for (topic_name, max_message_id) in rows:
|
|
canonical_name = topic_name.lower()
|
|
canonical_topic_names[canonical_name] = (max_message_id, topic_name)
|
|
|
|
history = []
|
|
for canonical_topic, (max_message_id, topic_name) in canonical_topic_names.items():
|
|
history.append(dict(
|
|
name=topic_name,
|
|
max_id=max_message_id)
|
|
)
|
|
return sorted(history, key=lambda x: -x['max_id'])
|
|
|
|
def get_topic_history_for_stream(user_profile: UserProfile,
|
|
recipient: Recipient,
|
|
public_history: bool) -> List[Dict[str, Any]]:
|
|
cursor = connection.cursor()
|
|
if public_history:
|
|
query = '''
|
|
SELECT
|
|
"zerver_message"."subject" as topic,
|
|
max("zerver_message".id) as max_message_id
|
|
FROM "zerver_message"
|
|
WHERE (
|
|
"zerver_message"."recipient_id" = %s
|
|
)
|
|
GROUP BY (
|
|
"zerver_message"."subject"
|
|
)
|
|
ORDER BY max("zerver_message".id) DESC
|
|
'''
|
|
cursor.execute(query, [recipient.id])
|
|
else:
|
|
query = '''
|
|
SELECT
|
|
"zerver_message"."subject" as topic,
|
|
max("zerver_message".id) as max_message_id
|
|
FROM "zerver_message"
|
|
INNER JOIN "zerver_usermessage" ON (
|
|
"zerver_usermessage"."message_id" = "zerver_message"."id"
|
|
)
|
|
WHERE (
|
|
"zerver_usermessage"."user_profile_id" = %s AND
|
|
"zerver_message"."recipient_id" = %s
|
|
)
|
|
GROUP BY (
|
|
"zerver_message"."subject"
|
|
)
|
|
ORDER BY max("zerver_message".id) DESC
|
|
'''
|
|
cursor.execute(query, [user_profile.id, recipient.id])
|
|
rows = cursor.fetchall()
|
|
cursor.close()
|
|
|
|
return generate_topic_history_from_db_rows(rows)
|
|
|
|
def get_topic_history_for_web_public_stream(recipient: Recipient) -> List[Dict[str, Any]]:
|
|
cursor = connection.cursor()
|
|
query = '''
|
|
SELECT
|
|
"zerver_message"."subject" as topic,
|
|
max("zerver_message".id) as max_message_id
|
|
FROM "zerver_message"
|
|
WHERE (
|
|
"zerver_message"."recipient_id" = %s
|
|
)
|
|
GROUP BY (
|
|
"zerver_message"."subject"
|
|
)
|
|
ORDER BY max("zerver_message".id) DESC
|
|
'''
|
|
cursor.execute(query, [recipient.id])
|
|
rows = cursor.fetchall()
|
|
cursor.close()
|
|
|
|
return generate_topic_history_from_db_rows(rows)
|