thread.py 6.5 KB

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