thread.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. from ..types import (
  19. CommandResponse,
  20. DMInboxResponse,
  21. DMThreadResponse,
  22. ShareVoiceResponse,
  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, thread_id: str, seq_id: int | None = None, cursor: str | None = None
  92. ) -> AsyncIterable[ThreadItem]:
  93. has_more = True
  94. while has_more:
  95. resp = await self.get_thread(thread_id, seq_id=seq_id, cursor=cursor)
  96. cursor = resp.thread.oldest_cursor
  97. has_more = resp.thread.has_older
  98. for item in resp.thread.items:
  99. yield item
  100. async def delete_item(self, thread_id: str, item_id: str) -> None:
  101. await self.std_http_post(
  102. f"/api/v1/direct_v2/threads/{thread_id}/items/{item_id}/delete/",
  103. data={"_csrftoken": self.state.cookies.csrf_token, "_uuid": self.state.device.uuid},
  104. )
  105. async def _broadcast(
  106. self,
  107. thread_id: str,
  108. item_type: str,
  109. response_type: Type[T],
  110. signed: bool = False,
  111. client_context: str | None = None,
  112. **kwargs,
  113. ) -> T:
  114. client_context = client_context or self.state.gen_client_context()
  115. form = {
  116. "action": ThreadAction.SEND_ITEM.value,
  117. "send_attribution": "direct_thread",
  118. "thread_ids": f"[{thread_id}]",
  119. "is_shh_mode": "0",
  120. "client_context": client_context,
  121. "_csrftoken": self.state.cookies.csrf_token,
  122. "device_id": self.state.device.id,
  123. "mutation_token": client_context,
  124. "_uuid": self.state.device.uuid,
  125. **kwargs,
  126. "offline_threading_id": client_context,
  127. }
  128. return await self.std_http_post(
  129. f"/api/v1/direct_v2/threads/broadcast/{item_type}/",
  130. data=form,
  131. raw=not signed,
  132. response_type=response_type,
  133. )
  134. async def broadcast(
  135. self,
  136. thread_id: str,
  137. item_type: ThreadItemType,
  138. signed: bool = False,
  139. client_context: str | None = None,
  140. **kwargs,
  141. ) -> CommandResponse:
  142. return await self._broadcast(
  143. thread_id, item_type.value, CommandResponse, signed, client_context, **kwargs
  144. )
  145. async def broadcast_audio(
  146. self, thread_id: str, is_direct: bool, client_context: str | None = None, **kwargs
  147. ) -> ShareVoiceResponse | CommandResponse:
  148. response_type = ShareVoiceResponse if is_direct else CommandResponse
  149. return await self._broadcast(
  150. thread_id, "share_voice", response_type, False, client_context, **kwargs
  151. )