thread.py 6.3 KB

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