thread.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. async def create_group_thread(self, recipient_users: list[int | str]) -> Thread:
  134. return await self.std_http_post(
  135. "/api/v1/direct_v2/create_group_thread/",
  136. data={
  137. "_csrftoken": self.state.cookies.csrf_token,
  138. "_uuid": self.state.device.uuid,
  139. "_uid": self.state.session.ds_user_id,
  140. "recipient_users": json.dumps(
  141. [str(user) for user in recipient_users], separators=(",", ":")
  142. ),
  143. },
  144. response_type=Thread,
  145. )
  146. async def approve_thread(self, thread_id: int | str) -> None:
  147. await self.std_http_post(
  148. f"/api/v1/direct_v2/threads/{thread_id}/approve/",
  149. data={
  150. "filter": "DEFAULT",
  151. "_uuid": self.state.device.uuid,
  152. },
  153. raw=True,
  154. )
  155. async def approve_threads(self, thread_ids: list[int | str]) -> None:
  156. await self.std_http_post(
  157. "/api/v1/direct_v2/threads/approve_multiple/",
  158. data={
  159. "thread_ids": json.dumps(
  160. [str(thread) for thread in thread_ids], separators=(",", ":")
  161. ),
  162. "folder": "",
  163. },
  164. )
  165. async def delete_item(self, thread_id: str, item_id: str) -> None:
  166. await self.std_http_post(
  167. f"/api/v1/direct_v2/threads/{thread_id}/items/{item_id}/delete/",
  168. data={"_csrftoken": self.state.cookies.csrf_token, "_uuid": self.state.device.uuid},
  169. )
  170. async def _broadcast(
  171. self,
  172. thread_id: str,
  173. item_type: str,
  174. response_type: Type[T],
  175. signed: bool = False,
  176. client_context: str | None = None,
  177. **kwargs,
  178. ) -> T:
  179. client_context = client_context or self.state.gen_client_context()
  180. form = {
  181. "action": ThreadAction.SEND_ITEM.value,
  182. "send_attribution": "direct_thread",
  183. "thread_ids": f"[{thread_id}]",
  184. "is_shh_mode": "0",
  185. "client_context": client_context,
  186. "_csrftoken": self.state.cookies.csrf_token,
  187. "device_id": self.state.device.id,
  188. "mutation_token": client_context,
  189. "_uuid": self.state.device.uuid,
  190. **kwargs,
  191. "offline_threading_id": client_context,
  192. }
  193. return await self.std_http_post(
  194. f"/api/v1/direct_v2/threads/broadcast/{item_type}/",
  195. data=form,
  196. raw=not signed,
  197. response_type=response_type,
  198. )
  199. async def broadcast(
  200. self,
  201. thread_id: str,
  202. item_type: ThreadItemType,
  203. signed: bool = False,
  204. client_context: str | None = None,
  205. **kwargs,
  206. ) -> CommandResponse:
  207. return await self._broadcast(
  208. thread_id, item_type.value, CommandResponse, signed, client_context, **kwargs
  209. )
  210. async def fetch_clip(self, media_id: int) -> FetchedClipInfo:
  211. return await self.std_http_get(
  212. f"/api/v1/clips/item/",
  213. query={"clips_media_id": str(media_id)},
  214. response_type=FetchedClipInfo,
  215. )