thread.py 9.8 KB

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