|
30 | 30 |
|
31 | 31 | from zulipterminal import unicode_emojis
|
32 | 32 | from zulipterminal.api_types import (
|
| 33 | + RESOLVED_TOPIC_PREFIX, |
33 | 34 | Composition,
|
34 | 35 | EditPropagateMode,
|
35 | 36 | Event,
|
@@ -564,6 +565,53 @@ def update_private_message(self, msg_id: int, content: str) -> bool:
|
564 | 565 | display_error_if_present(response, self.controller)
|
565 | 566 | return response["result"] == "success"
|
566 | 567 |
|
| 568 | + def get_latest_message_in_topic( |
| 569 | + self, stream_id: int, topic_name: str |
| 570 | + ) -> Dict[str, Any]: |
| 571 | + request: Dict[str, Any] = { |
| 572 | + "anchor": "newest", |
| 573 | + "num_before": 1, |
| 574 | + "num_after": 0, |
| 575 | + "narrow": [ |
| 576 | + { |
| 577 | + "operator": "stream", |
| 578 | + "operand": self.stream_dict[stream_id]["name"], |
| 579 | + }, |
| 580 | + {"operator": "topic", "operand": topic_name}, |
| 581 | + ], |
| 582 | + } |
| 583 | + response = self.client.get_messages(request) |
| 584 | + display_error_if_present(response, self.controller) |
| 585 | + return response |
| 586 | + |
| 587 | + def toggle_topic_resolved_status(self, stream_id: int, topic_name: str) -> bool: |
| 588 | + response = self.get_latest_message_in_topic(stream_id, topic_name) |
| 589 | + if response["messages"] != []: |
| 590 | + time_since_msg_sent = time.time() - response["messages"][0]["timestamp"] |
| 591 | + edit_time_limit = self.initial_data[ |
| 592 | + "realm_community_topic_editing_limit_seconds" |
| 593 | + ] |
| 594 | + # Don't allow editing topic if time-limit exceeded. |
| 595 | + if time_since_msg_sent <= edit_time_limit: |
| 596 | + topic_msg_id = response["messages"][0]["id"] |
| 597 | + if topic_name.startswith(RESOLVED_TOPIC_PREFIX): |
| 598 | + self.update_stream_message( |
| 599 | + topic=topic_name[2:], |
| 600 | + message_id=topic_msg_id, |
| 601 | + propagate_mode="change_all", |
| 602 | + ) |
| 603 | + else: |
| 604 | + self.update_stream_message( |
| 605 | + topic=RESOLVED_TOPIC_PREFIX + topic_name, |
| 606 | + message_id=topic_msg_id, |
| 607 | + propagate_mode="change_all", |
| 608 | + ) |
| 609 | + else: |
| 610 | + self.controller.report_error( |
| 611 | + " Time limit for editing topic has been exceeded." |
| 612 | + ) |
| 613 | + return response["result"] == "success" |
| 614 | + |
567 | 615 | def update_stream_message(
|
568 | 616 | self,
|
569 | 617 | topic: str,
|
|
0 commit comments