thread.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. Thread,
  24. ThreadAction,
  25. ThreadItem,
  26. ThreadItemType,
  27. )
  28. from .base import BaseAndroidAPI, T
  29. class ThreadAPI(BaseAndroidAPI):
  30. async def get_inbox(
  31. self,
  32. cursor: str | None = None,
  33. seq_id: str | None = None,
  34. message_limit: int = 10,
  35. limit: int = 20,
  36. pending: bool = False,
  37. direction: str = "older",
  38. ) -> DMInboxResponse:
  39. query = {
  40. "visual_message_return_type": "unseen",
  41. "cursor": cursor,
  42. "direction": direction if cursor else None,
  43. "seq_id": seq_id,
  44. "thread_message_limit": message_limit,
  45. "persistentBadging": "true",
  46. "limit": limit,
  47. }
  48. inbox_type = "pending_inbox" if pending else "inbox"
  49. return await self.std_http_get(
  50. f"/api/v1/direct_v2/{inbox_type}/", query=query, response_type=DMInboxResponse
  51. )
  52. async def iter_inbox(
  53. self, start_at: DMInboxResponse | None = None, message_limit: int = 10
  54. ) -> AsyncIterable[Thread]:
  55. if start_at:
  56. cursor = start_at.inbox.oldest_cursor
  57. seq_id = start_at.seq_id
  58. has_more = start_at.inbox.has_older
  59. for thread in start_at.inbox.threads:
  60. yield thread
  61. else:
  62. cursor = None
  63. seq_id = None
  64. has_more = True
  65. while has_more:
  66. resp = await self.get_inbox(message_limit=message_limit, cursor=cursor, seq_id=seq_id)
  67. seq_id = resp.seq_id
  68. cursor = resp.inbox.oldest_cursor
  69. has_more = resp.inbox.has_older
  70. for thread in resp.inbox.threads:
  71. yield thread
  72. async def get_thread(
  73. self,
  74. thread_id: str,
  75. cursor: str | None = None,
  76. limit: int = 10,
  77. direction: str = "older",
  78. seq_id: int | None = None,
  79. ) -> DMThreadResponse:
  80. query = {
  81. "visual_message_return_type": "unseen",
  82. "cursor": cursor,
  83. "direction": direction,
  84. "seq_id": seq_id,
  85. "limit": limit,
  86. }
  87. return await self.std_http_get(
  88. f"/api/v1/direct_v2/threads/{thread_id}/", query=query, response_type=DMThreadResponse
  89. )
  90. async def iter_thread(
  91. self,
  92. thread_id: str,
  93. seq_id: int | None = None,
  94. cursor: str | None = None,
  95. start_at: Thread | None = None,
  96. ) -> AsyncIterable[ThreadItem]:
  97. if start_at:
  98. for item in start_at.items:
  99. yield item
  100. cursor = start_at.oldest_cursor
  101. has_more = start_at.has_older
  102. else:
  103. has_more = True
  104. while has_more:
  105. resp = await self.get_thread(thread_id, seq_id=seq_id, cursor=cursor)
  106. cursor = resp.thread.oldest_cursor
  107. has_more = resp.thread.has_older
  108. for item in resp.thread.items:
  109. yield item
  110. async def create_group_thread(self, recipient_users: list[int | str]) -> Thread:
  111. return await self.std_http_post(
  112. "/api/v1/direct_v2/create_group_thread/",
  113. data={
  114. "_csrftoken": self.state.cookies.csrf_token,
  115. "_uuid": self.state.device.uuid,
  116. "_uid": self.state.cookies.user_id,
  117. "recipient_users": json.dumps(
  118. [str(user) for user in recipient_users], separators=(",", ":")
  119. ),
  120. },
  121. response_type=Thread,
  122. )
  123. async def approve_thread(self, thread_id: int | str) -> None:
  124. await self.std_http_post(
  125. f"/api/v1/direct_v2/threads/{thread_id}/approve/",
  126. data={
  127. "filter": "DEFAULT",
  128. "_uuid": self.state.device.uuid,
  129. },
  130. raw=True,
  131. )
  132. async def approve_threads(self, thread_ids: list[int | str]) -> None:
  133. await self.std_http_post(
  134. "/api/v1/direct_v2/threads/approve_multiple/",
  135. data={
  136. "thread_ids": json.dumps(
  137. [str(thread) for thread in thread_ids], separators=(",", ":")
  138. ),
  139. "folder": "",
  140. },
  141. )
  142. async def delete_item(self, thread_id: str, item_id: str) -> None:
  143. await self.std_http_post(
  144. f"/api/v1/direct_v2/threads/{thread_id}/items/{item_id}/delete/",
  145. data={"_csrftoken": self.state.cookies.csrf_token, "_uuid": self.state.device.uuid},
  146. )
  147. async def _broadcast(
  148. self,
  149. thread_id: str,
  150. item_type: str,
  151. response_type: Type[T],
  152. signed: bool = False,
  153. client_context: str | None = None,
  154. **kwargs,
  155. ) -> T:
  156. client_context = client_context or self.state.gen_client_context()
  157. form = {
  158. "action": ThreadAction.SEND_ITEM.value,
  159. "send_attribution": "direct_thread",
  160. "thread_ids": f"[{thread_id}]",
  161. "is_shh_mode": "0",
  162. "client_context": client_context,
  163. "_csrftoken": self.state.cookies.csrf_token,
  164. "device_id": self.state.device.id,
  165. "mutation_token": client_context,
  166. "_uuid": self.state.device.uuid,
  167. **kwargs,
  168. "offline_threading_id": client_context,
  169. }
  170. return await self.std_http_post(
  171. f"/api/v1/direct_v2/threads/broadcast/{item_type}/",
  172. data=form,
  173. raw=not signed,
  174. response_type=response_type,
  175. )
  176. async def broadcast(
  177. self,
  178. thread_id: str,
  179. item_type: ThreadItemType,
  180. signed: bool = False,
  181. client_context: str | None = None,
  182. **kwargs,
  183. ) -> CommandResponse:
  184. return await self._broadcast(
  185. thread_id, item_type.value, CommandResponse, signed, client_context, **kwargs
  186. )