thread.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 create_group_thread(self, recipient_users: list[int | str]) -> Thread:
  91. return await self.std_http_post(
  92. "/api/v1/direct_v2/create_group_thread/",
  93. data={
  94. "_csrftoken": self.state.cookies.csrf_token,
  95. "_uuid": self.state.device.uuid,
  96. "_uid": self.state.session.ds_user_id,
  97. "recipient_users": json.dumps(
  98. [str(user) for user in recipient_users], separators=(",", ":")
  99. ),
  100. },
  101. response_type=Thread,
  102. )
  103. async def approve_thread(self, thread_id: int | str) -> None:
  104. await self.std_http_post(
  105. f"/api/v1/direct_v2/threads/{thread_id}/approve/",
  106. data={
  107. "filter": "DEFAULT",
  108. "_uuid": self.state.device.uuid,
  109. },
  110. raw=True,
  111. )
  112. async def approve_threads(self, thread_ids: list[int | str]) -> None:
  113. await self.std_http_post(
  114. "/api/v1/direct_v2/threads/approve_multiple/",
  115. data={
  116. "thread_ids": json.dumps(
  117. [str(thread) for thread in thread_ids], separators=(",", ":")
  118. ),
  119. "folder": "",
  120. },
  121. )
  122. async def delete_item(self, thread_id: str, item_id: str) -> None:
  123. await self.std_http_post(
  124. f"/api/v1/direct_v2/threads/{thread_id}/items/{item_id}/delete/",
  125. data={"_csrftoken": self.state.cookies.csrf_token, "_uuid": self.state.device.uuid},
  126. )
  127. async def _broadcast(
  128. self,
  129. thread_id: str,
  130. item_type: str,
  131. response_type: Type[T],
  132. signed: bool = False,
  133. client_context: str | None = None,
  134. **kwargs,
  135. ) -> T:
  136. client_context = client_context or self.state.gen_client_context()
  137. form = {
  138. "action": ThreadAction.SEND_ITEM.value,
  139. "send_attribution": "direct_thread",
  140. "thread_ids": f"[{thread_id}]",
  141. "is_shh_mode": "0",
  142. "client_context": client_context,
  143. "_csrftoken": self.state.cookies.csrf_token,
  144. "device_id": self.state.device.id,
  145. "mutation_token": client_context,
  146. "_uuid": self.state.device.uuid,
  147. **kwargs,
  148. "offline_threading_id": client_context,
  149. }
  150. return await self.std_http_post(
  151. f"/api/v1/direct_v2/threads/broadcast/{item_type}/",
  152. data=form,
  153. raw=not signed,
  154. response_type=response_type,
  155. )
  156. async def broadcast(
  157. self,
  158. thread_id: str,
  159. item_type: ThreadItemType,
  160. signed: bool = False,
  161. client_context: str | None = None,
  162. **kwargs,
  163. ) -> CommandResponse:
  164. return await self._broadcast(
  165. thread_id, item_type.value, CommandResponse, signed, client_context, **kwargs
  166. )