@@ -1554,6 +1554,83 @@ def set_typing_status(self, request: Dict[str, Any]) -> Dict[str, Any]:
1554
1554
request = request
1555
1555
)
1556
1556
1557
+ def move_topic (
1558
+ self ,
1559
+ stream : str ,
1560
+ new_stream : str ,
1561
+ topic : str ,
1562
+ new_topic : Optional [str ] = None ,
1563
+ message_id : Optional [int ] = None ,
1564
+ propagate_mode : str = 'change_all' ,
1565
+ notify_old_topic : bool = True ,
1566
+ notify_new_topic : bool = True
1567
+ ) -> Dict [str , Any ]:
1568
+ '''
1569
+ Move a topic from ``stream`` to ``new_stream``
1570
+
1571
+ The topic will be renamed if ``new_topic`` is provided.
1572
+ message_id and propagation_mode let you control which messages
1573
+ should be moved. The default behavior moves all messages in topic.
1574
+
1575
+ propagation_mode must be one of: `change_one`, `change_later`,
1576
+ `change_all`. Defaults to `change_all`.
1577
+
1578
+ Example usage:
1579
+
1580
+ >>> client.move_topic('stream_a', 'stream_b', 'my_topic')
1581
+ {'result': 'success', 'msg': ''}
1582
+ '''
1583
+ # get IDs for source and target streams
1584
+ result = self .get_stream_id (stream )
1585
+ if result ['result' ] != 'success' :
1586
+ return result
1587
+ stream = result ['stream_id' ]
1588
+
1589
+ result = self .get_stream_id (new_stream )
1590
+ if result ['result' ] != 'success' :
1591
+ return result
1592
+ new_stream = result ['stream_id' ]
1593
+
1594
+ if message_id is None :
1595
+ if propagate_mode != 'change_all' :
1596
+ raise AttributeError ('A message_id must be provided if '
1597
+ 'propagate_mode isn\' t "change_all"' )
1598
+
1599
+ # ask the server for the latest message ID in the topic.
1600
+ result = self .get_messages ({
1601
+ 'anchor' : 'newest' ,
1602
+ 'narrow' : [{'operator' : 'stream' , 'operand' : stream },
1603
+ {'operator' : 'topic' , 'operand' : topic }],
1604
+ 'num_before' : 1 ,
1605
+ 'num_after' : 0 ,
1606
+ })
1607
+
1608
+ if result ['result' ] != 'success' :
1609
+ return result
1610
+
1611
+ if len (result ['messages' ]) <= 0 :
1612
+ return {
1613
+ 'result' : 'error' ,
1614
+ 'msg' : 'No messages found in topic: "{}"' .format (topic )
1615
+ }
1616
+
1617
+ message_id = result ['messages' ][0 ]['id' ]
1618
+
1619
+ # move topic containing message to new stream
1620
+ request = {
1621
+ 'stream_id' : new_stream ,
1622
+ 'propagate_mode' : propagate_mode ,
1623
+ 'topic' : new_topic ,
1624
+ 'send_notification_to_old_thread' : notify_old_topic ,
1625
+ 'send_notification_to_new_thread' : notify_new_topic
1626
+ }
1627
+ return self .call_endpoint (
1628
+ url = 'messages/{}' .format (message_id ),
1629
+ method = 'PATCH' ,
1630
+ request = request ,
1631
+ )
1632
+
1633
+
1557
1634
class ZulipStream :
1558
1635
"""
1559
1636
A Zulip stream-like object
0 commit comments