zulip_data.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. """
  2. The functions here should be specific to how we store
  3. Zulip data, without getting specific about HTML/Markdown
  4. syntax.
  5. The goal here is to have some functions that are resuable
  6. for folks who may want to emit differently structured
  7. HTML or markdown.
  8. """
  9. from .date_helper import format_date1
  10. def sorted_streams(streams):
  11. """
  12. Streams are sorted so that streams with the most topics
  13. go to the top.
  14. """
  15. return sorted(streams, key=lambda s: len(streams[s]["topic_data"]), reverse=True)
  16. def sorted_topics(topic_data):
  17. """
  18. Topics are sorted so that the most recently updated
  19. topic is at the top of the list.
  20. """
  21. return sorted(
  22. topic_data, key=lambda tn: topic_data[tn]["latest_date"], reverse=True
  23. )
  24. def num_topics_string(stream_topic_data):
  25. """
  26. example: "5 topics"
  27. """
  28. num_topics = len(stream_topic_data)
  29. plural = "" if num_topics == 1 else "s"
  30. return f"{num_topics} topic{plural}"
  31. def topic_info_string(message_data):
  32. """
  33. n messages, latest: <date>
  34. """
  35. cnt = message_data["size"]
  36. plural = "" if cnt == 1 else "s"
  37. latest_date = message_data["latest_date"]
  38. date = format_date1(latest_date)
  39. return f"{cnt} message{plural}, latest: {date}"