topic history: Fix fetching topic history of public streams.

Apparently, we did essentially all the work to support showing full
topic history to newly subscribed users from a data flow perspective,
but didn't actually enable this feature by having the topic history
endpoint grant access to historical topics.  This fixes that gap.

I'm not altogether happy with how the code and tests read for this
feature; the code itself has more duplication than I'd like, and the
tests do too, but it works.
This commit is contained in:
Tim Abbott
2018-03-11 20:59:20 -07:00
parent 9e52a9f879
commit ef92fcbe2b
3 changed files with 65 additions and 5 deletions

View File

@@ -196,8 +196,26 @@ def realm_user_count(realm: Realm) -> int:
return UserProfile.objects.filter(realm=realm, is_active=True, is_bot=False).count()
def get_topic_history_for_stream(user_profile: UserProfile,
recipient: Recipient) -> List[Dict[str, Any]]:
query = '''
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
@@ -213,9 +231,8 @@ def get_topic_history_for_stream(user_profile: UserProfile,
"zerver_message"."subject"
)
ORDER BY max("zerver_message".id) DESC
'''
cursor = connection.cursor()
cursor.execute(query, [user_profile.id, recipient.id])
'''
cursor.execute(query, [user_profile.id, recipient.id])
rows = cursor.fetchall()
cursor.close()