refactor: Use recipient_id to get topic history.

This commit is contained in:
Steve Howell
2020-10-16 15:45:21 +00:00
committed by Tim Abbott
parent 3685fcc701
commit bfd6e2b1fd
3 changed files with 9 additions and 9 deletions

View File

@@ -5,7 +5,7 @@ from django.db.models.query import Q, QuerySet
from sqlalchemy.sql import column, func, literal from sqlalchemy.sql import column, func, literal
from zerver.lib.request import REQ from zerver.lib.request import REQ
from zerver.models import Message, Recipient, Stream, UserMessage, UserProfile from zerver.models import Message, Stream, UserMessage, UserProfile
# Only use these constants for events. # Only use these constants for events.
ORIG_TOPIC = "orig_subject" ORIG_TOPIC = "orig_subject"
@@ -158,7 +158,7 @@ def generate_topic_history_from_db_rows(rows: List[Tuple[str, int]]) -> List[Dic
) )
return sorted(history, key=lambda x: -x['max_id']) return sorted(history, key=lambda x: -x['max_id'])
def get_topic_history_for_public_stream(recipient: Recipient) -> List[Dict[str, Any]]: def get_topic_history_for_public_stream(recipient_id: int) -> List[Dict[str, Any]]:
cursor = connection.cursor() cursor = connection.cursor()
query = ''' query = '''
SELECT SELECT
@@ -173,17 +173,17 @@ def get_topic_history_for_public_stream(recipient: Recipient) -> List[Dict[str,
) )
ORDER BY max("zerver_message".id) DESC ORDER BY max("zerver_message".id) DESC
''' '''
cursor.execute(query, [recipient.id]) cursor.execute(query, [recipient_id])
rows = cursor.fetchall() rows = cursor.fetchall()
cursor.close() cursor.close()
return generate_topic_history_from_db_rows(rows) return generate_topic_history_from_db_rows(rows)
def get_topic_history_for_stream(user_profile: UserProfile, def get_topic_history_for_stream(user_profile: UserProfile,
recipient: Recipient, recipient_id: int,
public_history: bool) -> List[Dict[str, Any]]: public_history: bool) -> List[Dict[str, Any]]:
if public_history: if public_history:
return get_topic_history_for_public_stream(recipient) return get_topic_history_for_public_stream(recipient_id)
cursor = connection.cursor() cursor = connection.cursor()
query = ''' query = '''
@@ -203,7 +203,7 @@ def get_topic_history_for_stream(user_profile: UserProfile,
) )
ORDER BY max("zerver_message".id) DESC ORDER BY max("zerver_message".id) DESC
''' '''
cursor.execute(query, [user_profile.id, recipient.id]) cursor.execute(query, [user_profile.id, recipient_id])
rows = cursor.fetchall() rows = cursor.fetchall()
cursor.close() cursor.close()

View File

@@ -85,6 +85,6 @@ def get_web_public_topics_backend(request: HttpRequest, stream_id: int) -> HttpR
if not stream.is_web_public: if not stream.is_web_public:
return json_success(dict(topics=[])) return json_success(dict(topics=[]))
result = get_topic_history_for_public_stream(recipient=stream.recipient) result = get_topic_history_for_public_stream(recipient_id=stream.recipient_id)
return json_success(dict(topics=result)) return json_success(dict(topics=result))

View File

@@ -666,7 +666,7 @@ def get_topics_backend(
if is_web_public_query: if is_web_public_query:
realm = get_valid_realm_from_request(request) realm = get_valid_realm_from_request(request)
stream = access_web_public_stream(stream_id, realm) stream = access_web_public_stream(stream_id, realm)
result = get_topic_history_for_public_stream(recipient=stream.recipient) result = get_topic_history_for_public_stream(recipient_id=stream.recipient_id)
else: else:
assert user_profile is not None assert user_profile is not None
@@ -675,7 +675,7 @@ def get_topics_backend(
result = get_topic_history_for_stream( result = get_topic_history_for_stream(
user_profile=user_profile, user_profile=user_profile,
recipient=recipient, recipient_id=stream.recipient_id,
public_history=stream.is_history_public_to_subscribers(), public_history=stream.is_history_public_to_subscribers(),
) )