|
8 | 8 | from collections import defaultdict
|
9 | 9 | from concurrent.futures import Future, ThreadPoolExecutor, wait
|
10 | 10 | from copy import deepcopy
|
11 |
| -from datetime import datetime |
| 11 | +from datetime import datetime,timezone |
12 | 12 | from typing import (
|
13 | 13 | Any,
|
14 | 14 | Callable,
|
@@ -116,7 +116,6 @@ def __init__(self, controller: Any) -> None:
|
116 | 116 | self.recipients: FrozenSet[Any] = frozenset()
|
117 | 117 | self.index = initial_index
|
118 | 118 | self.last_unread_pm = None
|
119 |
| - |
120 | 119 | self.user_id = -1
|
121 | 120 | self.user_email = ""
|
122 | 121 | self.user_full_name = ""
|
@@ -315,6 +314,7 @@ def set_narrow(
|
315 | 314 | frozenset(["pm_with"]): [["pm-with", pm_with]],
|
316 | 315 | frozenset(["starred"]): [["is", "starred"]],
|
317 | 316 | frozenset(["mentioned"]): [["is", "mentioned"]],
|
| 317 | + |
318 | 318 | }
|
319 | 319 | for narrow_param, narrow in valid_narrows.items():
|
320 | 320 | if narrow_param == selected_params:
|
@@ -856,6 +856,7 @@ def modernize_message_response(message: Message) -> Message:
|
856 | 856 | message["topic_links"] = topic_links
|
857 | 857 |
|
858 | 858 | return message
|
| 859 | + |
859 | 860 |
|
860 | 861 | def fetch_message_history(
|
861 | 862 | self, message_id: int
|
@@ -1094,7 +1095,56 @@ def get_other_subscribers_in_stream(
|
1094 | 1095 | if stream["name"] == stream_name
|
1095 | 1096 | if sub != self.user_id
|
1096 | 1097 | ]
|
| 1098 | + def group_recent_conversations(self) -> List[Dict[str, Any]]: |
| 1099 | + """Return the 10 most recent stream conversations.""" |
| 1100 | + # Filter for stream messages |
| 1101 | + stream_msgs = [m for m in self.index["messages"].values() if m["type"] == "stream"] |
| 1102 | + if not stream_msgs: |
| 1103 | + return [] |
| 1104 | + |
| 1105 | + # Sort messages by timestamp (most recent first) |
| 1106 | + stream_msgs.sort(key=lambda x: x["timestamp"], reverse=True) |
| 1107 | + |
| 1108 | + # Group messages by stream and topic |
| 1109 | + convos = defaultdict(list) |
| 1110 | + for msg in stream_msgs[:50]: # Limit to 50 recent messages |
| 1111 | + convos[(msg["stream_id"], msg["subject"])].append(msg) |
| 1112 | + |
| 1113 | + # Process conversations into the desired format |
| 1114 | + processed_conversations = [] |
| 1115 | + now = datetime.now(timezone.utc) |
| 1116 | + for (stream_id, topic), msg_list in sorted( |
| 1117 | + convos.items(), key=lambda x: max(m["timestamp"] for m in x[1]), reverse=True |
| 1118 | + )[:10]: |
| 1119 | + # Map stream_id to stream name |
| 1120 | + |
| 1121 | + stream_name=self.stream_name_from_id(stream_id) |
| 1122 | + topic_name = topic if topic else "(no topic)" |
| 1123 | + |
| 1124 | + # Extract participants |
| 1125 | + participants = set() |
| 1126 | + for msg in msg_list: |
| 1127 | + participants.add(msg["sender_full_name"]) |
| 1128 | + |
| 1129 | + # Format timestamp (using the most recent message in the conversation) |
| 1130 | + most_recent_msg = max(msg_list, key=lambda x: x["timestamp"]) |
| 1131 | + timestamp = most_recent_msg["timestamp"] |
| 1132 | + conv_time = datetime.fromtimestamp(timestamp, tz=timezone.utc) |
| 1133 | + delta = now - conv_time |
| 1134 | + if delta.days > 0: |
| 1135 | + time_str = f"{delta.days} days ago" |
| 1136 | + else: |
| 1137 | + hours = delta.seconds // 3600 |
| 1138 | + time_str = f"{hours} hours ago" if hours > 0 else "just now" |
| 1139 | + |
| 1140 | + processed_conversations.append({ |
| 1141 | + "stream": stream_name, |
| 1142 | + "topic": topic_name, |
| 1143 | + "participants": list(participants), |
| 1144 | + "time": time_str, |
| 1145 | + }) |
1097 | 1146 |
|
| 1147 | + return processed_conversations |
1098 | 1148 | def _clean_and_order_custom_profile_data(
|
1099 | 1149 | self, custom_profile_data: Dict[str, CustomFieldValue]
|
1100 | 1150 | ) -> List[CustomProfileData]:
|
@@ -1417,6 +1467,8 @@ def stream_id_from_name(self, stream_name: str) -> int:
|
1417 | 1467 | if stream["name"] == stream_name:
|
1418 | 1468 | return stream_id
|
1419 | 1469 | raise RuntimeError("Invalid stream name.")
|
| 1470 | + def stream_name_from_id(self,stream_id:int)->str: |
| 1471 | + return self.stream_dict[stream_id]["name"] |
1420 | 1472 |
|
1421 | 1473 | def stream_access_type(self, stream_id: int) -> StreamAccessType:
|
1422 | 1474 | if stream_id not in self.stream_dict:
|
|
0 commit comments