thread.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 .base import BaseAndroidAPI
  18. from ..types import DMInboxResponse, DMThreadResponse, Thread, ThreadItem
  19. class ThreadAPI(BaseAndroidAPI):
  20. async def get_inbox(self, cursor: Optional[str] = None, seq_id: Optional[str] = None,
  21. message_limit: int = 10, limit: int = 20, pending: bool = False,
  22. direction: str = "older") -> DMInboxResponse:
  23. query = {
  24. "visual_message_return_type": "unseen",
  25. "cursor": cursor,
  26. "direction": direction if cursor else None,
  27. "seq_id": seq_id,
  28. "thread_message_limit": message_limit,
  29. "persistentBadging": "true",
  30. "limit": limit,
  31. }
  32. inbox_type = "pending_inbox" if pending else "inbox"
  33. return await self.std_http_get(f"/api/v1/direct_v2/{inbox_type}/", query=query,
  34. response_type=DMInboxResponse)
  35. async def iter_inbox(self, cursor: Optional[str] = None, seq_id: Optional[str] = None,
  36. message_limit: int = 10) -> AsyncIterable[Thread]:
  37. has_more = True
  38. while has_more:
  39. resp = await self.get_inbox(message_limit=message_limit, cursor=cursor, seq_id=seq_id)
  40. seq_id = resp.seq_id
  41. cursor = resp.inbox.prev_cursor
  42. has_more = resp.inbox.has_older
  43. for thread in resp.inbox.threads:
  44. yield thread
  45. async def get_thread(self, thread_id: str, cursor: Optional[str] = None, limit: int = 10,
  46. direction: str = "older", seq_id: Optional[int] = None
  47. ) -> DMThreadResponse:
  48. query = {
  49. "visual_message_return_type": "unseen",
  50. "cursor": cursor,
  51. "direction": direction,
  52. "seq_id": seq_id,
  53. "limit": limit,
  54. }
  55. return await self.std_http_get(f"/api/v1/direct_v2/threads/{thread_id}/", query=query,
  56. response_type=DMThreadResponse)
  57. async def iter_thread(self, thread_id: str, seq_id: Optional[int] = None,
  58. cursor: Optional[str] = None) -> AsyncIterable[ThreadItem]:
  59. has_more = True
  60. while has_more:
  61. resp = await self.get_thread(thread_id, seq_id=seq_id, cursor=cursor)
  62. cursor = resp.thread.oldest_cursor
  63. has_more = resp.thread.has_older
  64. for item in resp.thread.items:
  65. yield item