thread.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2022 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from __future__ import annotations
  17. from typing import AsyncIterable, Type
  18. import json
  19. from ..types import (
  20. CommandResponse,
  21. DMInboxResponse,
  22. DMThreadResponse,
  23. ShareVoiceResponse,
  24. Thread,
  25. ThreadAction,
  26. ThreadItem,
  27. ThreadItemType,
  28. )
  29. from .base import BaseAndroidAPI, T
  30. class ThreadAPI(BaseAndroidAPI):
  31. async def get_inbox(
  32. self,
  33. cursor: str | None = None,
  34. seq_id: str | None = None,
  35. message_limit: int = 10,
  36. limit: int = 20,
  37. pending: bool = False,
  38. direction: str = "older",
  39. ) -> DMInboxResponse:
  40. query = {
  41. "visual_message_return_type": "unseen",
  42. "cursor": cursor,
  43. "direction": direction if cursor else None,
  44. "seq_id": seq_id,
  45. "thread_message_limit": message_limit,
  46. "persistentBadging": "true",
  47. "limit": limit,
  48. }
  49. inbox_type = "pending_inbox" if pending else "inbox"
  50. return await self.std_http_get(
  51. f"/api/v1/direct_v2/{inbox_type}/", query=query, response_type=DMInboxResponse
  52. )
  53. async def iter_inbox(
  54. self, start_at: DMInboxResponse | None = None, message_limit: int = 10
  55. ) -> AsyncIterable[Thread]:
  56. if start_at:
  57. cursor = start_at.inbox.oldest_cursor
  58. seq_id = start_at.seq_id
  59. has_more = start_at.inbox.has_older
  60. for thread in start_at.inbox.threads:
  61. yield thread
  62. else:
  63. cursor = None
  64. seq_id = None
  65. has_more = True
  66. while has_more:
  67. resp = await self.get_inbox(message_limit=message_limit, cursor=cursor, seq_id=seq_id)
  68. seq_id = resp.seq_id
  69. cursor = resp.inbox.oldest_cursor
  70. has_more = resp.inbox.has_older
  71. for thread in resp.inbox.threads:
  72. yield thread
  73. async def get_thread(
  74. self,
  75. thread_id: str,
  76. cursor: str | None = None,
  77. limit: int = 10,
  78. direction: str = "older",
  79. seq_id: int | None = None,
  80. ) -> DMThreadResponse:
  81. query = {
  82. "visual_message_return_type": "unseen",
  83. "cursor": cursor,
  84. "direction": direction,
  85. "seq_id": seq_id,
  86. "limit": limit,
  87. }
  88. return await self.std_http_get(
  89. f"/api/v1/direct_v2/threads/{thread_id}/", query=query, response_type=DMThreadResponse
  90. )
  91. async def iter_thread(
  92. self,
  93. thread_id: str,
  94. seq_id: int | None = None,
  95. cursor: str | None = None,
  96. start_at: Thread | None = None,
  97. ) -> AsyncIterable[ThreadItem]:
  98. if start_at:
  99. for item in start_at.items:
  100. yield item
  101. cursor = start_at.oldest_cursor
  102. has_more = start_at.has_older
  103. else:
  104. has_more = True
  105. while has_more:
  106. resp = await self.get_thread(thread_id, seq_id=seq_id, cursor=cursor)
  107. cursor = resp.thread.oldest_cursor
  108. has_more = resp.thread.has_older
  109. for item in resp.thread.items:
  110. yield item
  111. async def create_group_thread(self, recipient_users: list[int | str]) -> Thread:
  112. return await self.std_http_post(
  113. "/api/v1/direct_v2/create_group_thread/",
  114. data={
  115. "_csrftoken": self.state.cookies.csrf_token,
  116. "_uuid": self.state.device.uuid,
  117. "_uid": self.state.cookies.user_id,
  118. "recipient_users": json.dumps(
  119. [str(user) for user in recipient_users], separators=(",", ":")
  120. ),
  121. },
  122. response_type=Thread,
  123. )
  124. async def approve_thread(self, thread_id: int | str) -> None:
  125. await self.std_http_post(
  126. f"/api/v1/direct_v2/threads/{thread_id}/approve/",
  127. data={
  128. "filter": "DEFAULT",
  129. "_uuid": self.state.device.uuid,
  130. },
  131. raw=True,
  132. )
  133. async def approve_threads(self, thread_ids: list[int | str]) -> None:
  134. await self.std_http_post(
  135. "/api/v1/direct_v2/threads/approve_multiple/",
  136. data={
  137. "thread_ids": json.dumps(
  138. [str(thread) for thread in thread_ids], separators=(",", ":")
  139. ),
  140. "folder": "",
  141. },
  142. )
  143. async def delete_item(self, thread_id: str, item_id: str) -> None:
  144. await self.std_http_post(
  145. f"/api/v1/direct_v2/threads/{thread_id}/items/{item_id}/delete/",
  146. data={"_csrftoken": self.state.cookies.csrf_token, "_uuid": self.state.device.uuid},
  147. )
  148. async def _broadcast(
  149. self,
  150. thread_id: str,
  151. item_type: str,
  152. response_type: Type[T],
  153. signed: bool = False,
  154. client_context: str | None = None,
  155. **kwargs,
  156. ) -> T:
  157. client_context = client_context or self.state.gen_client_context()
  158. form = {
  159. "action": ThreadAction.SEND_ITEM.value,
  160. "send_attribution": "direct_thread",
  161. "thread_ids": f"[{thread_id}]",
  162. "is_shh_mode": "0",
  163. "client_context": client_context,
  164. "_csrftoken": self.state.cookies.csrf_token,
  165. "device_id": self.state.device.id,
  166. "mutation_token": client_context,
  167. "_uuid": self.state.device.uuid,
  168. **kwargs,
  169. "offline_threading_id": client_context,
  170. }
  171. return await self.std_http_post(
  172. f"/api/v1/direct_v2/threads/broadcast/{item_type}/",
  173. data=form,
  174. raw=not signed,
  175. response_type=response_type,
  176. )
  177. async def broadcast(
  178. self,
  179. thread_id: str,
  180. item_type: ThreadItemType,
  181. signed: bool = False,
  182. client_context: str | None = None,
  183. **kwargs,
  184. ) -> CommandResponse:
  185. return await self._broadcast(
  186. thread_id, item_type.value, CommandResponse, signed, client_context, **kwargs
  187. )
  188. async def broadcast_audio(
  189. self, thread_id: str, is_direct: bool, client_context: str | None = None, **kwargs
  190. ) -> ShareVoiceResponse | CommandResponse:
  191. response_type = ShareVoiceResponse if is_direct else CommandResponse
  192. return await self._broadcast(
  193. thread_id, "share_voice", response_type, False, client_context, **kwargs
  194. )