thread.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 typing import Optional, AsyncIterable, Type, Union
  17. from .base import BaseAndroidAPI, T
  18. from ..types import (DMInboxResponse, DMThreadResponse, Thread, ThreadItem, ThreadAction,
  19. ThreadItemType, CommandResponse, ShareVoiceResponse)
  20. class ThreadAPI(BaseAndroidAPI):
  21. async def get_inbox(self, cursor: Optional[str] = None, seq_id: Optional[str] = None,
  22. message_limit: int = 10, limit: int = 20, pending: bool = False,
  23. direction: str = "older") -> DMInboxResponse:
  24. query = {
  25. "visual_message_return_type": "unseen",
  26. "cursor": cursor,
  27. "direction": direction if cursor else None,
  28. "seq_id": seq_id,
  29. "thread_message_limit": message_limit,
  30. "persistentBadging": "true",
  31. "limit": limit,
  32. }
  33. inbox_type = "pending_inbox" if pending else "inbox"
  34. return await self.std_http_get(f"/api/v1/direct_v2/{inbox_type}/", query=query,
  35. response_type=DMInboxResponse)
  36. async def iter_inbox(self, start_at: Optional[DMInboxResponse] = None,
  37. message_limit: int = 10) -> AsyncIterable[Thread]:
  38. if start_at:
  39. cursor = start_at.inbox.oldest_cursor
  40. seq_id = start_at.seq_id
  41. has_more = start_at.inbox.has_older
  42. for thread in start_at.inbox.threads:
  43. yield thread
  44. else:
  45. cursor = None
  46. seq_id = None
  47. has_more = True
  48. while has_more:
  49. resp = await self.get_inbox(message_limit=message_limit, cursor=cursor, seq_id=seq_id)
  50. seq_id = resp.seq_id
  51. cursor = resp.inbox.oldest_cursor
  52. has_more = resp.inbox.has_older
  53. for thread in resp.inbox.threads:
  54. yield thread
  55. async def get_thread(self, thread_id: str, cursor: Optional[str] = None, limit: int = 10,
  56. direction: str = "older", seq_id: Optional[int] = None
  57. ) -> DMThreadResponse:
  58. query = {
  59. "visual_message_return_type": "unseen",
  60. "cursor": cursor,
  61. "direction": direction,
  62. "seq_id": seq_id,
  63. "limit": limit,
  64. }
  65. return await self.std_http_get(f"/api/v1/direct_v2/threads/{thread_id}/", query=query,
  66. response_type=DMThreadResponse)
  67. async def iter_thread(self, thread_id: str, seq_id: Optional[int] = None,
  68. cursor: Optional[str] = None) -> AsyncIterable[ThreadItem]:
  69. has_more = True
  70. while has_more:
  71. resp = await self.get_thread(thread_id, seq_id=seq_id, cursor=cursor)
  72. cursor = resp.thread.oldest_cursor
  73. has_more = resp.thread.has_older
  74. for item in resp.thread.items:
  75. yield item
  76. async def delete_item(self, thread_id: str, item_id: str) -> None:
  77. await self.std_http_post(f"/api/v1/direct_v2/threads/{thread_id}/items/{item_id}/delete/",
  78. data={"_csrftoken": self.state.cookies.csrf_token,
  79. "_uuid": self.state.device.uuid})
  80. async def _broadcast(self, thread_id: str, item_type: str, response_type: Type[T],
  81. signed: bool = False, client_context: Optional[str] = None, **kwargs
  82. ) -> T:
  83. client_context = client_context or self.state.gen_client_context()
  84. form = {
  85. "action": ThreadAction.SEND_ITEM.value,
  86. "send_attribution": "direct_thread",
  87. "thread_ids": f"[{thread_id}]",
  88. "is_shh_mode": "0",
  89. "client_context": client_context,
  90. "_csrftoken": self.state.cookies.csrf_token,
  91. "device_id": self.state.device.id,
  92. "mutation_token": client_context,
  93. "_uuid": self.state.device.uuid,
  94. **kwargs,
  95. "offline_threading_id": client_context,
  96. }
  97. return await self.std_http_post(f"/api/v1/direct_v2/threads/broadcast/{item_type}/",
  98. data=form, raw=not signed, response_type=response_type)
  99. async def broadcast(self, thread_id: str, item_type: ThreadItemType, signed: bool = False,
  100. client_context: Optional[str] = None, **kwargs) -> CommandResponse:
  101. return await self._broadcast(thread_id, item_type.value, CommandResponse, signed,
  102. client_context, **kwargs)
  103. async def broadcast_audio(self, thread_id: str, is_direct: bool,
  104. client_context: Optional[str] = None, **kwargs
  105. ) -> Union[ShareVoiceResponse, CommandResponse]:
  106. response_type = ShareVoiceResponse if is_direct else CommandResponse
  107. return await self._broadcast(thread_id, "share_voice", response_type, False,
  108. client_context, **kwargs)