Skip to content

Commit 3630208

Browse files
authored
api: Add binding for moving topics between streams.
Adds the method move_topic to the class Client that allows moving topics between streams. - The topic can be renamed if the new_topic argument is given - Partial topic can be moved givent the proper message_id and propagate_mode arguments. - notification to old/new stream can be silenced (active by default)
1 parent 1df19c0 commit 3630208

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

zulip/zulip/__init__.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,6 +1554,83 @@ def set_typing_status(self, request: Dict[str, Any]) -> Dict[str, Any]:
15541554
request=request
15551555
)
15561556

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+
15571634
class ZulipStream:
15581635
"""
15591636
A Zulip stream-like object

0 commit comments

Comments
 (0)