thread.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, start_at: Optional[DMInboxResponse] = None,
  38. message_limit: int = 10) -> AsyncIterable[Thread]:
  39. if start_at:
  40. cursor = start_at.inbox.prev_cursor
  41. seq_id = start_at.inbox.prev_cursor
  42. has_more = start_at.inbox.has_older
  43. for thread in start_at.inbox.threads:
  44. yield thread
  45. else:
  46. cursor = None
  47. seq_id = None
  48. has_more = True
  49. while has_more:
  50. resp = await self.get_inbox(message_limit=message_limit, cursor=cursor, seq_id=seq_id)
  51. seq_id = resp.seq_id
  52. cursor = resp.inbox.prev_cursor
  53. has_more = resp.inbox.has_older
  54. for thread in resp.inbox.threads:
  55. yield thread
  56. async def get_thread(self, thread_id: str, cursor: Optional[str] = None, limit: int = 10,
  57. direction: str = "older", seq_id: Optional[int] = None
  58. ) -> DMThreadResponse:
  59. query = {
  60. "visual_message_return_type": "unseen",
  61. "cursor": cursor,
  62. "direction": direction,
  63. "seq_id": seq_id,
  64. "limit": limit,
  65. }
  66. return await self.std_http_get(f"/api/v1/direct_v2/threads/{thread_id}/", query=query,
  67. response_type=DMThreadResponse)
  68. async def iter_thread(self, thread_id: str, seq_id: Optional[int] = None,
  69. cursor: Optional[str] = None) -> AsyncIterable[ThreadItem]:
  70. has_more = True
  71. while has_more:
  72. resp = await self.get_thread(thread_id, seq_id=seq_id, cursor=cursor)
  73. cursor = resp.thread.oldest_cursor
  74. has_more = resp.thread.has_older
  75. for item in resp.thread.items:
  76. yield item
  77. async def delete_item(self, thread_id: str, item_id: str) -> None:
  78. await self.std_http_post(f"/api/v1/direct_v2/threads/{thread_id}/items/{item_id}/delete/",
  79. data={"_csrftoken": self.state.cookies.csrf_token,
  80. "_uuid": self.state.device.uuid})
  81. async def broadcast(self, thread_id: str, item_type: ThreadItemType, signed: bool = False,
  82. client_context: Optional[str] = None, **kwargs) -> CommandResponse:
  83. client_context = client_context or str(uuid4())
  84. form = {
  85. "action": ThreadAction.SEND_ITEM.value,
  86. "send_attribution": "inbox",
  87. "thread_id": thread_id,
  88. "client_context": client_context,
  89. "_csrftoken": self.state.cookies.csrf_token,
  90. "device_id": self.state.device.id,
  91. "mutation_token": client_context,
  92. "_uuid": self.state.device.uuid,
  93. **kwargs,
  94. "offline_threading_id": client_context,
  95. }
  96. return await self.std_http_post(f"/api/v1/direct_v2/threads/broadcast/{item_type.value}/",
  97. data=form, raw=not signed, response_type=CommandResponse)