thread.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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, Callable, Type
  18. import asyncio
  19. import json
  20. from mauigpapi.errors.response import IGRateLimitError
  21. from ..types import (
  22. CommandResponse,
  23. DMInboxResponse,
  24. DMThreadResponse,
  25. Thread,
  26. ThreadAction,
  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,
  55. update_seq_id_and_cursor: Callable[[int, str | None], None],
  56. start_at: DMInboxResponse | None = None,
  57. local_limit: int | None = None,
  58. rate_limit_exceeded_backoff: float = 60.0,
  59. ) -> AsyncIterable[Thread]:
  60. thread_counter = 0
  61. if start_at:
  62. cursor = start_at.inbox.oldest_cursor
  63. seq_id = start_at.seq_id
  64. has_more = start_at.inbox.has_older
  65. for thread in start_at.inbox.threads:
  66. yield thread
  67. thread_counter += 1
  68. if local_limit and thread_counter >= local_limit:
  69. return
  70. update_seq_id_and_cursor(seq_id, cursor)
  71. else:
  72. cursor = None
  73. seq_id = None
  74. has_more = True
  75. while has_more:
  76. try:
  77. resp = await self.get_inbox(message_limit=10, cursor=cursor, seq_id=seq_id)
  78. except IGRateLimitError:
  79. self.log.warning(
  80. "Fetching more threads failed due to rate limit. Waiting for "
  81. f"{rate_limit_exceeded_backoff} seconds before resuming."
  82. )
  83. await asyncio.sleep(rate_limit_exceeded_backoff)
  84. continue
  85. except Exception:
  86. self.log.exception("Failed to fetch more threads")
  87. raise
  88. seq_id = resp.seq_id
  89. cursor = resp.inbox.oldest_cursor
  90. has_more = resp.inbox.has_older
  91. for thread in resp.inbox.threads:
  92. yield thread
  93. thread_counter += 1
  94. if local_limit and thread_counter >= local_limit:
  95. return
  96. update_seq_id_and_cursor(seq_id, cursor)
  97. async def get_thread(
  98. self,
  99. thread_id: str,
  100. cursor: str | None = None,
  101. limit: int = 10,
  102. direction: str = "older",
  103. seq_id: int | None = None,
  104. ) -> DMThreadResponse:
  105. query = {
  106. "visual_message_return_type": "unseen",
  107. "cursor": cursor,
  108. "direction": direction,
  109. "seq_id": seq_id,
  110. "limit": limit,
  111. }
  112. return await self.std_http_get(
  113. f"/api/v1/direct_v2/threads/{thread_id}/", query=query, response_type=DMThreadResponse
  114. )
  115. async def create_group_thread(self, recipient_users: list[int | str]) -> Thread:
  116. return await self.std_http_post(
  117. "/api/v1/direct_v2/create_group_thread/",
  118. data={
  119. "_csrftoken": self.state.cookies.csrf_token,
  120. "_uuid": self.state.device.uuid,
  121. "_uid": self.state.session.ds_user_id,
  122. "recipient_users": json.dumps(
  123. [str(user) for user in recipient_users], separators=(",", ":")
  124. ),
  125. },
  126. response_type=Thread,
  127. )
  128. async def approve_thread(self, thread_id: int | str) -> None:
  129. await self.std_http_post(
  130. f"/api/v1/direct_v2/threads/{thread_id}/approve/",
  131. data={
  132. "filter": "DEFAULT",
  133. "_uuid": self.state.device.uuid,
  134. },
  135. raw=True,
  136. )
  137. async def approve_threads(self, thread_ids: list[int | str]) -> None:
  138. await self.std_http_post(
  139. "/api/v1/direct_v2/threads/approve_multiple/",
  140. data={
  141. "thread_ids": json.dumps(
  142. [str(thread) for thread in thread_ids], separators=(",", ":")
  143. ),
  144. "folder": "",
  145. },
  146. )
  147. async def delete_item(self, thread_id: str, item_id: str) -> None:
  148. await self.std_http_post(
  149. f"/api/v1/direct_v2/threads/{thread_id}/items/{item_id}/delete/",
  150. data={"_csrftoken": self.state.cookies.csrf_token, "_uuid": self.state.device.uuid},
  151. )
  152. async def _broadcast(
  153. self,
  154. thread_id: str,
  155. item_type: str,
  156. response_type: Type[T],
  157. signed: bool = False,
  158. client_context: str | None = None,
  159. **kwargs,
  160. ) -> T:
  161. client_context = client_context or self.state.gen_client_context()
  162. form = {
  163. "action": ThreadAction.SEND_ITEM.value,
  164. "send_attribution": "direct_thread",
  165. "thread_ids": f"[{thread_id}]",
  166. "is_shh_mode": "0",
  167. "client_context": client_context,
  168. "_csrftoken": self.state.cookies.csrf_token,
  169. "device_id": self.state.device.id,
  170. "mutation_token": client_context,
  171. "_uuid": self.state.device.uuid,
  172. **kwargs,
  173. "offline_threading_id": client_context,
  174. }
  175. return await self.std_http_post(
  176. f"/api/v1/direct_v2/threads/broadcast/{item_type}/",
  177. data=form,
  178. raw=not signed,
  179. response_type=response_type,
  180. )
  181. async def broadcast(
  182. self,
  183. thread_id: str,
  184. item_type: ThreadItemType,
  185. signed: bool = False,
  186. client_context: str | None = None,
  187. **kwargs,
  188. ) -> CommandResponse:
  189. return await self._broadcast(
  190. thread_id, item_type.value, CommandResponse, signed, client_context, **kwargs
  191. )