thread.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2020 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
  17. from uuid import uuid4
  18. from .base import BaseAndroidAPI
  19. from ..types import (DMInboxResponse, DMThreadResponse, Thread, ThreadItem, ThreadAction,
  20. ThreadItemType, CommandResponse)
  21. class ThreadAPI(BaseAndroidAPI):
  22. async def get_inbox(self, cursor: Optional[str] = None, seq_id: Optional[str] = None,
  23. message_limit: int = 10, limit: int = 20, pending: bool = False,
  24. direction: str = "older") -> DMInboxResponse:
  25. query = {
  26. "visual_message_return_type": "unseen",
  27. "cursor": cursor,
  28. "direction": direction if cursor else None,
  29. "seq_id": seq_id,
  30. "thread_message_limit": message_limit,
  31. "persistentBadging": "true",
  32. "limit": limit,
  33. }
  34. inbox_type = "pending_inbox" if pending else "inbox"
  35. return await self.std_http_get(f"/api/v1/direct_v2/{inbox_type}/", query=query,
  36. response_type=DMInboxResponse)
  37. async def iter_inbox(self, cursor: Optional[str] = None, seq_id: Optional[str] = None,
  38. message_limit: int = 10) -> AsyncIterable[Thread]:
  39. has_more = True
  40. while has_more:
  41. resp = await self.get_inbox(message_limit=message_limit, cursor=cursor, seq_id=seq_id)
  42. seq_id = resp.seq_id
  43. cursor = resp.inbox.prev_cursor
  44. has_more = resp.inbox.has_older
  45. for thread in resp.inbox.threads:
  46. yield thread
  47. async def get_thread(self, thread_id: str, cursor: Optional[str] = None, limit: int = 10,
  48. direction: str = "older", seq_id: Optional[int] = None
  49. ) -> DMThreadResponse:
  50. query = {
  51. "visual_message_return_type": "unseen",
  52. "cursor": cursor,
  53. "direction": direction,
  54. "seq_id": seq_id,
  55. "limit": limit,
  56. }
  57. return await self.std_http_get(f"/api/v1/direct_v2/threads/{thread_id}/", query=query,
  58. response_type=DMThreadResponse)
  59. async def iter_thread(self, thread_id: str, seq_id: Optional[int] = None,
  60. cursor: Optional[str] = None) -> AsyncIterable[ThreadItem]:
  61. has_more = True
  62. while has_more:
  63. resp = await self.get_thread(thread_id, seq_id=seq_id, cursor=cursor)
  64. cursor = resp.thread.oldest_cursor
  65. has_more = resp.thread.has_older
  66. for item in resp.thread.items:
  67. yield item
  68. async def delete_item(self, thread_id: str, item_id: str) -> None:
  69. await self.std_http_post(f"/api/v1/direct_v2/threads/{thread_id}/items/{item_id}/delete/",
  70. data={"_csrftoken": self.state.cookies.csrf_token,
  71. "_uuid": self.state.device.uuid})
  72. async def broadcast(self, thread_id: str, item_type: ThreadItemType, signed: bool = False,
  73. client_context: Optional[str] = None, **kwargs) -> CommandResponse:
  74. client_context = client_context or str(uuid4())
  75. form = {
  76. "action": ThreadAction.SEND_ITEM.value,
  77. "send_attribution": "inbox",
  78. "thread_id": thread_id,
  79. "client_context": client_context,
  80. "_csrftoken": self.state.cookies.csrf_token,
  81. "device_id": self.state.device.id,
  82. "mutation_token": client_context,
  83. "_uuid": self.state.device.uuid,
  84. **kwargs,
  85. "offline_threading_id": client_context,
  86. }
  87. return await self.std_http_post(f"/api/v1/direct_v2/threads/broadcast/{item_type.value}/",
  88. data=form, raw=not signed, response_type=CommandResponse)