thread.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2023 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. FetchedClipInfo,
  26. Thread,
  27. ThreadAction,
  28. ThreadItemType,
  29. )
  30. from .base import BaseAndroidAPI, T
  31. class ThreadAPI(BaseAndroidAPI):
  32. async def get_inbox(
  33. self,
  34. cursor: str | None = None,
  35. seq_id: str | None = None,
  36. message_limit: int | None = 10,
  37. limit: int = 20,
  38. pending: bool = False,
  39. spam: bool = False,
  40. direction: str = "older",
  41. ) -> DMInboxResponse:
  42. query = {
  43. "visual_message_return_type": "unseen",
  44. "cursor": cursor,
  45. "direction": direction if cursor else None,
  46. "seq_id": seq_id,
  47. "thread_message_limit": message_limit,
  48. "persistentBadging": "true",
  49. "limit": limit,
  50. "push_disabled": "true",
  51. "is_prefetching": "false",
  52. }
  53. inbox_type = "inbox"
  54. if pending:
  55. inbox_type = "pending_inbox"
  56. if spam:
  57. inbox_type = "spam_inbox"
  58. elif not cursor:
  59. query["fetch_reason"] = "initial_snapshot" # can also be manual_refresh
  60. headers = {
  61. # MainFeedFragment:feed_timeline for limit=0 cold start fetch
  62. "ig-client-endpoint": "DirectInboxFragment:direct_inbox",
  63. }
  64. return await self.std_http_get(
  65. f"/api/v1/direct_v2/{inbox_type}/", query=query, response_type=DMInboxResponse
  66. )
  67. async def iter_inbox(
  68. self,
  69. update_seq_id_and_cursor: Callable[[int, str | None], None],
  70. start_at: DMInboxResponse | None = None,
  71. local_limit: int | None = None,
  72. rate_limit_exceeded_backoff: float = 60.0,
  73. ) -> AsyncIterable[Thread]:
  74. thread_counter = 0
  75. if start_at:
  76. cursor = start_at.inbox.oldest_cursor
  77. seq_id = start_at.seq_id
  78. has_more = start_at.inbox.has_older
  79. for thread in start_at.inbox.threads:
  80. yield thread
  81. thread_counter += 1
  82. if local_limit and thread_counter >= local_limit:
  83. return
  84. update_seq_id_and_cursor(seq_id, cursor)
  85. else:
  86. cursor = None
  87. seq_id = None
  88. has_more = True
  89. while has_more:
  90. try:
  91. resp = await self.get_inbox(cursor=cursor, seq_id=seq_id)
  92. except IGRateLimitError:
  93. self.log.warning(
  94. "Fetching more threads failed due to rate limit. Waiting for "
  95. f"{rate_limit_exceeded_backoff} seconds before resuming."
  96. )
  97. await asyncio.sleep(rate_limit_exceeded_backoff)
  98. continue
  99. except Exception:
  100. self.log.exception("Failed to fetch more threads")
  101. raise
  102. seq_id = resp.seq_id
  103. cursor = resp.inbox.oldest_cursor
  104. has_more = resp.inbox.has_older
  105. for thread in resp.inbox.threads:
  106. yield thread
  107. thread_counter += 1
  108. if local_limit and thread_counter >= local_limit:
  109. return
  110. update_seq_id_and_cursor(seq_id, cursor)
  111. async def get_thread(
  112. self,
  113. thread_id: str,
  114. cursor: str | None = None,
  115. limit: int = 20,
  116. direction: str = "older",
  117. seq_id: int | None = None,
  118. ) -> DMThreadResponse:
  119. query = {
  120. "visual_message_return_type": "unseen",
  121. "cursor": cursor,
  122. "direction": direction,
  123. "seq_id": seq_id,
  124. "limit": limit,
  125. }
  126. headers = {
  127. "ig-client-endpoint": "DirectThreadFragment:direct_thread",
  128. "x-ig-nav-chain": "MainFeedFragment:feed_timeline:1:cold_start::,DirectInboxFragment:direct_inbox:4:on_launch_direct_inbox::",
  129. }
  130. return await self.std_http_get(
  131. f"/api/v1/direct_v2/threads/{thread_id}/", query=query, response_type=DMThreadResponse
  132. )
  133. # /threads/.../get_items/ with urlencoded form body:
  134. # visual_message_return_type: unseen
  135. # _uuid: device uuid
  136. # original_message_client_contexts:["client context"]
  137. # item_ids: [item id]
  138. async def get_thread_participant_requests(self, thread_id: str, page_size: int = 10):
  139. return await self.std_http_get(
  140. f"/api/v1/direct_v2/threads/{thread_id}/participant_requests/",
  141. query={"page_size": str(page_size)},
  142. )
  143. async def mark_seen(
  144. self, thread_id: str, item_id: str, client_context: str | None = None
  145. ) -> None:
  146. if not client_context:
  147. client_context = self.state.gen_client_context()
  148. data = {
  149. "thread_id": thread_id,
  150. "action": "mark_seen",
  151. "client_context": client_context,
  152. "_uuid": self.state.device.uuid,
  153. "offline_threading_id": client_context,
  154. }
  155. await self.std_http_post(
  156. f"/api/v1/direct_v2/threads/{thread_id}/items/{item_id}/seen/", data=data
  157. )
  158. async def create_group_thread(self, recipient_users: list[int | str]) -> Thread:
  159. return await self.std_http_post(
  160. "/api/v1/direct_v2/create_group_thread/",
  161. data={
  162. "_uuid": self.state.device.uuid,
  163. "_uid": self.state.session.ds_user_id,
  164. "recipient_users": json.dumps(
  165. [str(user) for user in recipient_users], separators=(",", ":")
  166. ),
  167. },
  168. response_type=Thread,
  169. )
  170. async def approve_thread(self, thread_id: int | str) -> None:
  171. await self.std_http_post(
  172. f"/api/v1/direct_v2/threads/{thread_id}/approve/",
  173. data={
  174. "filter": "DEFAULT",
  175. "_uuid": self.state.device.uuid,
  176. },
  177. raw=True,
  178. )
  179. async def approve_threads(self, thread_ids: list[int | str]) -> None:
  180. await self.std_http_post(
  181. "/api/v1/direct_v2/threads/approve_multiple/",
  182. data={
  183. "thread_ids": json.dumps(
  184. [str(thread) for thread in thread_ids], separators=(",", ":")
  185. ),
  186. "folder": "",
  187. },
  188. )
  189. async def delete_item(
  190. self, thread_id: str, item_id: str, orig_client_context: str | None = None
  191. ) -> None:
  192. await self.std_http_post(
  193. f"/api/v1/direct_v2/threads/{thread_id}/items/{item_id}/delete/",
  194. data={
  195. "is_shh_mode": "0",
  196. "send_attribution": "direct_thread",
  197. "_uuid": self.state.device.uuid,
  198. "original_message_client_context": orig_client_context,
  199. },
  200. )
  201. async def _broadcast(
  202. self,
  203. thread_id: str,
  204. item_type: str,
  205. response_type: Type[T],
  206. signed: bool = False,
  207. client_context: str | None = None,
  208. **kwargs,
  209. ) -> T:
  210. client_context = client_context or self.state.gen_client_context()
  211. form = {
  212. "action": ThreadAction.SEND_ITEM.value,
  213. "send_attribution": "direct_thread",
  214. "thread_ids": f"[{thread_id}]",
  215. "is_shh_mode": "0",
  216. "client_context": client_context,
  217. "device_id": self.state.device.id,
  218. "mutation_token": client_context,
  219. "_uuid": self.state.device.uuid,
  220. **kwargs,
  221. "offline_threading_id": client_context,
  222. "is_x_transport_forward": "false",
  223. }
  224. return await self.std_http_post(
  225. f"/api/v1/direct_v2/threads/broadcast/{item_type}/",
  226. data=form,
  227. raw=not signed,
  228. response_type=response_type,
  229. )
  230. async def broadcast(
  231. self,
  232. thread_id: str,
  233. item_type: ThreadItemType,
  234. signed: bool = False,
  235. client_context: str | None = None,
  236. **kwargs,
  237. ) -> CommandResponse:
  238. return await self._broadcast(
  239. thread_id, item_type.value, CommandResponse, signed, client_context, **kwargs
  240. )
  241. async def fetch_clip(self, media_id: int) -> FetchedClipInfo:
  242. return await self.std_http_get(
  243. f"/api/v1/clips/item/",
  244. query={"clips_media_id": str(media_id)},
  245. response_type=FetchedClipInfo,
  246. )