conn.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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 Any, Awaitable, Callable, Iterable, Type, TypeVar
  18. from collections import defaultdict
  19. from socket import error as SocketError, socket
  20. import asyncio
  21. import json
  22. import logging
  23. import re
  24. import time
  25. import urllib.request
  26. import zlib
  27. from paho.mqtt.client import MQTTMessage, WebsocketConnectionError
  28. from yarl import URL
  29. import paho.mqtt.client
  30. from mautrix.util.logging import TraceLogger
  31. from ..errors import IrisSubscribeError, MQTTNotConnected, MQTTNotLoggedIn
  32. from ..state import AndroidState
  33. from ..types import (
  34. AppPresenceEventPayload,
  35. ClientConfigUpdatePayload,
  36. CommandResponse,
  37. IrisPayload,
  38. IrisPayloadData,
  39. LiveVideoCommentPayload,
  40. MessageSyncEvent,
  41. MessageSyncMessage,
  42. PubsubEvent,
  43. PubsubPayload,
  44. ReactionStatus,
  45. RealtimeDirectEvent,
  46. RealtimeZeroProvisionPayload,
  47. ThreadAction,
  48. ThreadItemType,
  49. ThreadSyncEvent,
  50. TypingStatus,
  51. )
  52. from .events import Connect, Disconnect
  53. from .otclient import MQTToTClient
  54. from .subscription import GraphQLQueryID, RealtimeTopic, everclear_subscriptions
  55. from .thrift import ForegroundStateConfig, IncomingMessage, RealtimeClientInfo, RealtimeConfig
  56. try:
  57. import socks
  58. except ImportError:
  59. socks = None
  60. T = TypeVar("T")
  61. ACTIVITY_INDICATOR_REGEX = re.compile(
  62. r"/direct_v2/threads/([\w_]+)/activity_indicator_id/([\w_]+)"
  63. )
  64. INBOX_THREAD_REGEX = re.compile(r"/direct_v2/inbox/threads/([\w_]+)")
  65. class AndroidMQTT:
  66. _loop: asyncio.AbstractEventLoop
  67. _client: MQTToTClient
  68. log: TraceLogger
  69. state: AndroidState
  70. _graphql_subs: set[str]
  71. _skywalker_subs: set[str]
  72. _iris_seq_id: int | None
  73. _iris_snapshot_at_ms: int | None
  74. _publish_waiters: dict[int, asyncio.Future]
  75. _response_waiters: dict[RealtimeTopic, asyncio.Future]
  76. _response_waiter_locks: dict[RealtimeTopic, asyncio.Lock]
  77. _message_response_waiters: dict[str, asyncio.Future]
  78. _disconnect_error: Exception | None
  79. _event_handlers: dict[Type[T], list[Callable[[T], Awaitable[None]]]]
  80. # region Initialization
  81. def __init__(
  82. self,
  83. state: AndroidState,
  84. loop: asyncio.AbstractEventLoop | None = None,
  85. log: TraceLogger | None = None,
  86. ) -> None:
  87. self._graphql_subs = set()
  88. self._skywalker_subs = set()
  89. self._iris_seq_id = None
  90. self._iris_snapshot_at_ms = None
  91. self._publish_waiters = {}
  92. self._response_waiters = {}
  93. self._message_response_waiters = {}
  94. self._disconnect_error = None
  95. self._response_waiter_locks = defaultdict(lambda: asyncio.Lock())
  96. self._event_handlers = defaultdict(lambda: [])
  97. self.log = log or logging.getLogger("mauigpapi.mqtt")
  98. self._loop = loop or asyncio.get_event_loop()
  99. self.state = state
  100. self._client = MQTToTClient(
  101. client_id=self._form_client_id(),
  102. clean_session=True,
  103. protocol=paho.mqtt.client.MQTTv31,
  104. transport="tcp",
  105. )
  106. try:
  107. http_proxy = urllib.request.getproxies()["http"]
  108. except KeyError:
  109. http_proxy = None
  110. if http_proxy and socks and URL:
  111. proxy_url = URL(http_proxy)
  112. proxy_type = {
  113. "http": socks.HTTP,
  114. "https": socks.HTTP,
  115. "socks": socks.SOCKS5,
  116. "socks5": socks.SOCKS5,
  117. "socks4": socks.SOCKS4,
  118. }[proxy_url.scheme]
  119. self._client.proxy_set(
  120. proxy_type=proxy_type,
  121. proxy_addr=proxy_url.host,
  122. proxy_port=proxy_url.port,
  123. proxy_username=proxy_url.user,
  124. proxy_password=proxy_url.password,
  125. )
  126. self._client.enable_logger()
  127. self._client.tls_set()
  128. # mqtt.max_inflight_messages_set(20) # The rest will get queued
  129. # mqtt.max_queued_messages_set(0) # Unlimited messages can be queued
  130. # mqtt.message_retry_set(20) # Retry sending for at least 20 seconds
  131. # mqtt.reconnect_delay_set(min_delay=1, max_delay=120)
  132. self._client.connect_async("edge-mqtt.facebook.com", 443, keepalive=60)
  133. self._client.on_message = self._on_message_handler
  134. self._client.on_publish = self._on_publish_handler
  135. self._client.on_connect = self._on_connect_handler
  136. # self._client.on_disconnect = self._on_disconnect_handler
  137. self._client.on_socket_open = self._on_socket_open
  138. self._client.on_socket_close = self._on_socket_close
  139. self._client.on_socket_register_write = self._on_socket_register_write
  140. self._client.on_socket_unregister_write = self._on_socket_unregister_write
  141. def _form_client_id(self) -> bytes:
  142. subscribe_topics = [
  143. RealtimeTopic.PUBSUB,
  144. RealtimeTopic.SUB_IRIS_RESPONSE,
  145. RealtimeTopic.REALTIME_SUB,
  146. RealtimeTopic.REGION_HINT,
  147. RealtimeTopic.SEND_MESSAGE_RESPONSE,
  148. RealtimeTopic.MESSAGE_SYNC,
  149. RealtimeTopic.UNKNOWN_179,
  150. RealtimeTopic.UNKNOWN_PP,
  151. ]
  152. subscribe_topic_ids = [int(topic.encoded) for topic in subscribe_topics]
  153. password = f"sessionid={self.state.cookies['sessionid']}"
  154. cfg = RealtimeConfig(
  155. client_identifier=self.state.device.phone_id[:20],
  156. client_info=RealtimeClientInfo(
  157. user_id=int(self.state.user_id),
  158. user_agent=self.state.user_agent,
  159. client_capabilities=0b10110111,
  160. endpoint_capabilities=0,
  161. publish_format=1,
  162. no_automatic_foreground=True,
  163. make_user_available_in_foreground=False,
  164. device_id=self.state.device.phone_id,
  165. is_initially_foreground=True,
  166. network_type=1,
  167. network_subtype=0,
  168. client_mqtt_session_id=int(time.time() * 1000) & 0xFFFFFFFF,
  169. subscribe_topics=subscribe_topic_ids,
  170. client_type="cookie_auth",
  171. app_id=567067343352427,
  172. region_preference=self.state.session.region_hint or "LLA",
  173. device_secret="",
  174. client_stack=3,
  175. ),
  176. password=password,
  177. app_specific_info={
  178. "app_version": self.state.application.APP_VERSION,
  179. "X-IG-Capabilities": self.state.application.CAPABILITIES,
  180. "everclear_subscriptions": json.dumps(everclear_subscriptions),
  181. "User-Agent": self.state.user_agent,
  182. "Accept-Language": self.state.device.language.replace("_", "-"),
  183. "platform": "android",
  184. "ig_mqtt_route": "django",
  185. "pubsub_msg_type_blacklist": "direct, typing_type",
  186. "auth_cache_enabled": "0",
  187. },
  188. )
  189. return zlib.compress(cfg.to_thrift(), level=9)
  190. # endregion
  191. def _on_socket_open(self, client: MQTToTClient, _: Any, sock: socket) -> None:
  192. self._loop.add_reader(sock, client.loop_read)
  193. def _on_socket_close(self, client: MQTToTClient, _: Any, sock: socket) -> None:
  194. self._loop.remove_reader(sock)
  195. def _on_socket_register_write(self, client: MQTToTClient, _: Any, sock: socket) -> None:
  196. self._loop.add_writer(sock, client.loop_write)
  197. def _on_socket_unregister_write(self, client: MQTToTClient, _: Any, sock: socket) -> None:
  198. self._loop.remove_writer(sock)
  199. def _on_connect_handler(
  200. self, client: MQTToTClient, _: Any, flags: dict[str, Any], rc: int
  201. ) -> None:
  202. if rc != 0:
  203. err = paho.mqtt.client.connack_string(rc)
  204. self.log.error("MQTT Connection Error: %s (%d)", err, rc)
  205. return
  206. self._loop.create_task(self._post_connect())
  207. async def _post_connect(self) -> None:
  208. await self._dispatch(Connect())
  209. self.log.debug("Re-subscribing to things after connect")
  210. if self._graphql_subs:
  211. res = await self.graphql_subscribe(self._graphql_subs)
  212. self.log.trace("GraphQL subscribe response: %s", res)
  213. if self._skywalker_subs:
  214. res = await self.skywalker_subscribe(self._skywalker_subs)
  215. self.log.trace("Skywalker subscribe response: %s", res)
  216. if self._iris_seq_id:
  217. retry = 0
  218. while True:
  219. try:
  220. await self.iris_subscribe(self._iris_seq_id, self._iris_snapshot_at_ms)
  221. break
  222. except (asyncio.TimeoutError, IrisSubscribeError) as e:
  223. self.log.exception("Error requesting iris subscribe")
  224. retry += 1
  225. if retry >= 5:
  226. self._disconnect_error = e
  227. self.disconnect()
  228. break
  229. await asyncio.sleep(5)
  230. self.log.debug("Retrying iris subscribe")
  231. def _on_publish_handler(self, client: MQTToTClient, _: Any, mid: int) -> None:
  232. try:
  233. waiter = self._publish_waiters[mid]
  234. except KeyError:
  235. self.log.trace(f"Got publish confirmation for {mid}, but no waiters")
  236. return
  237. self.log.trace(f"Got publish confirmation for {mid}")
  238. waiter.set_result(None)
  239. # region Incoming event parsing
  240. def _parse_direct_thread_path(self, path: str) -> dict:
  241. try:
  242. blank, direct_v2, threads, thread_id, *rest = path.split("/")
  243. except (ValueError, IndexError) as e:
  244. self.log.debug(f"Got {e!r} while parsing path {path}")
  245. raise
  246. if (blank, direct_v2, threads) != ("", "direct_v2", "threads"):
  247. self.log.debug(f"Got unexpected first parts in direct thread path {path}")
  248. raise ValueError("unexpected first three parts in _parse_direct_thread_path")
  249. additional = {"thread_id": thread_id}
  250. if rest:
  251. subitem_key = rest[0]
  252. if subitem_key == "approval_required_for_new_members":
  253. additional["approval_required_for_new_members"] = True
  254. elif subitem_key == "participants" and len(rest) > 2 and rest[2] == "has_seen":
  255. additional["has_seen"] = int(rest[1])
  256. elif subitem_key == "items":
  257. additional["item_id"] = rest[1]
  258. # TODO wtf is this?
  259. # it has something to do with reactions
  260. if len(rest) > 4:
  261. additional[rest[2]] = {
  262. rest[3]: rest[4],
  263. }
  264. elif subitem_key in "admin_user_ids":
  265. additional["admin_user_id"] = int(rest[1])
  266. elif subitem_key == "activity_indicator_id":
  267. additional["activity_indicator_id"] = rest[1]
  268. self.log.trace("Parsed path %s -> %s", path, additional)
  269. return additional
  270. def _on_messager_sync_item(self, part: IrisPayloadData, parsed_item: IrisPayload) -> None:
  271. if part.path.startswith("/direct_v2/threads/"):
  272. raw_message = {
  273. "path": part.path,
  274. "op": part.op,
  275. **self._parse_direct_thread_path(part.path),
  276. }
  277. try:
  278. raw_message = {
  279. **raw_message,
  280. **json.loads(part.value),
  281. }
  282. except (json.JSONDecodeError, TypeError):
  283. raw_message["value"] = part.value
  284. message = MessageSyncMessage.deserialize(raw_message)
  285. evt = MessageSyncEvent(iris=parsed_item, message=message)
  286. elif part.path.startswith("/direct_v2/inbox/threads/"):
  287. raw_message = {
  288. "path": part.path,
  289. "op": part.op,
  290. **json.loads(part.value),
  291. }
  292. evt = ThreadSyncEvent.deserialize(raw_message)
  293. else:
  294. self.log.warning(f"Unsupported path {part.path}")
  295. return
  296. self._loop.create_task(self._dispatch(evt))
  297. def _on_message_sync(self, payload: bytes) -> None:
  298. parsed = json.loads(payload.decode("utf-8"))
  299. self.log.trace("Got message sync event: %s", parsed)
  300. for sync_item in parsed:
  301. parsed_item = IrisPayload.deserialize(sync_item)
  302. if self._iris_seq_id < parsed_item.seq_id:
  303. self.log.trace(f"Got new seq_id: {parsed_item.seq_id}")
  304. self._iris_seq_id = parsed_item.seq_id
  305. self._iris_snapshot_at_ms = int(time.time() * 1000)
  306. for part in parsed_item.data:
  307. self._on_messager_sync_item(part, parsed_item)
  308. def _on_pubsub(self, payload: bytes) -> None:
  309. parsed_thrift = IncomingMessage.from_thrift(payload)
  310. self.log.trace(f"Got pubsub event {parsed_thrift.topic} / {parsed_thrift.payload}")
  311. message = PubsubPayload.parse_json(parsed_thrift.payload)
  312. for data in message.data:
  313. match = ACTIVITY_INDICATOR_REGEX.match(data.path)
  314. if match:
  315. evt = PubsubEvent(
  316. data=data,
  317. base=message,
  318. thread_id=match.group(1),
  319. activity_indicator_id=match.group(2),
  320. )
  321. self._loop.create_task(self._dispatch(evt))
  322. elif not data.double_publish:
  323. self.log.debug("Pubsub: no activity indicator on data: %s", data)
  324. else:
  325. self.log.debug("Pubsub: double publish: %s", data.path)
  326. def _parse_realtime_sub_item(self, topic: str | GraphQLQueryID, raw: dict) -> Iterable[Any]:
  327. if topic == GraphQLQueryID.APP_PRESENCE:
  328. yield AppPresenceEventPayload.deserialize(raw).presence_event
  329. elif topic == GraphQLQueryID.ZERO_PROVISION:
  330. yield RealtimeZeroProvisionPayload.deserialize(raw).zero_product_provisioning_event
  331. elif topic == GraphQLQueryID.CLIENT_CONFIG_UPDATE:
  332. yield ClientConfigUpdatePayload.deserialize(raw).client_config_update_event
  333. elif topic == GraphQLQueryID.LIVE_REALTIME_COMMENTS:
  334. yield LiveVideoCommentPayload.deserialize(raw).live_video_comment_event
  335. elif topic == "direct":
  336. event = raw["event"]
  337. for item in raw["data"]:
  338. yield RealtimeDirectEvent.deserialize(
  339. {
  340. "event": event,
  341. **self._parse_direct_thread_path(item["path"]),
  342. **item,
  343. }
  344. )
  345. def _on_realtime_sub(self, payload: bytes) -> None:
  346. parsed_thrift = IncomingMessage.from_thrift(payload)
  347. try:
  348. topic = GraphQLQueryID(parsed_thrift.topic)
  349. except ValueError:
  350. topic = parsed_thrift.topic
  351. self.log.trace(f"Got realtime sub event {topic} / {parsed_thrift.payload}")
  352. allowed = (
  353. "direct",
  354. GraphQLQueryID.APP_PRESENCE,
  355. GraphQLQueryID.ZERO_PROVISION,
  356. GraphQLQueryID.CLIENT_CONFIG_UPDATE,
  357. GraphQLQueryID.LIVE_REALTIME_COMMENTS,
  358. )
  359. if topic not in allowed:
  360. return
  361. parsed_json = json.loads(parsed_thrift.payload)
  362. for evt in self._parse_realtime_sub_item(topic, parsed_json):
  363. self._loop.create_task(self._dispatch(evt))
  364. def _on_message_handler(self, client: MQTToTClient, _: Any, message: MQTTMessage) -> None:
  365. try:
  366. topic = RealtimeTopic.decode(message.topic)
  367. # Instagram Android MQTT messages are always compressed
  368. message.payload = zlib.decompress(message.payload)
  369. if topic == RealtimeTopic.MESSAGE_SYNC:
  370. self._on_message_sync(message.payload)
  371. elif topic == RealtimeTopic.PUBSUB:
  372. self._on_pubsub(message.payload)
  373. elif topic == RealtimeTopic.REALTIME_SUB:
  374. self._on_realtime_sub(message.payload)
  375. elif topic == RealtimeTopic.SEND_MESSAGE_RESPONSE:
  376. try:
  377. data = json.loads(message.payload.decode("utf-8"))
  378. ccid = data["payload"]["client_context"]
  379. waiter = self._message_response_waiters.pop(ccid)
  380. except KeyError as e:
  381. self.log.debug(
  382. "No handler (%s) for send message response: %s", e, message.payload
  383. )
  384. else:
  385. self.log.trace("Got response to %s: %s", ccid, message.payload)
  386. waiter.set_result(message)
  387. else:
  388. try:
  389. waiter = self._response_waiters.pop(topic)
  390. except KeyError:
  391. self.log.debug(
  392. "No handler for MQTT message in %s: %s", topic.value, message.payload
  393. )
  394. else:
  395. self.log.trace("Got response %s: %s", topic.value, message.payload)
  396. waiter.set_result(message)
  397. except Exception:
  398. self.log.exception("Error in incoming MQTT message handler")
  399. self.log.trace("Errored MQTT payload: %s", message.payload)
  400. # endregion
  401. async def _reconnect(self) -> None:
  402. try:
  403. self.log.trace("Trying to reconnect to MQTT")
  404. self._client.reconnect()
  405. except (SocketError, OSError, WebsocketConnectionError) as e:
  406. # TODO custom class
  407. raise MQTTNotLoggedIn("MQTT reconnection failed") from e
  408. def add_event_handler(
  409. self, evt_type: Type[T], handler: Callable[[T], Awaitable[None]]
  410. ) -> None:
  411. self._event_handlers[evt_type].append(handler)
  412. async def _dispatch(self, evt: T) -> None:
  413. for handler in self._event_handlers[type(evt)]:
  414. try:
  415. await handler(evt)
  416. except Exception:
  417. self.log.exception(f"Error in {type(evt)} handler")
  418. def disconnect(self) -> None:
  419. self._client.disconnect()
  420. async def listen(
  421. self,
  422. graphql_subs: set[str] | None = None,
  423. skywalker_subs: set[str] | None = None,
  424. seq_id: int = None,
  425. snapshot_at_ms: int = None,
  426. retry_limit: int = 5,
  427. ) -> None:
  428. self._graphql_subs = graphql_subs or set()
  429. self._skywalker_subs = skywalker_subs or set()
  430. self._iris_seq_id = seq_id
  431. self._iris_snapshot_at_ms = snapshot_at_ms
  432. self.log.debug("Connecting to Instagram MQTT")
  433. await self._reconnect()
  434. connection_retries = 0
  435. while True:
  436. try:
  437. await asyncio.sleep(1)
  438. except asyncio.CancelledError:
  439. self.disconnect()
  440. # this might not be necessary
  441. self._client.loop_misc()
  442. break
  443. rc = self._client.loop_misc()
  444. # If disconnect() has been called
  445. # Beware, internal API, may have to change this to something more stable!
  446. if self._client._state == paho.mqtt.client.mqtt_cs_disconnecting:
  447. break # Stop listening
  448. if rc != paho.mqtt.client.MQTT_ERR_SUCCESS:
  449. # If known/expected error
  450. if rc == paho.mqtt.client.MQTT_ERR_CONN_LOST:
  451. await self._dispatch(Disconnect(reason="Connection lost, retrying"))
  452. elif rc == paho.mqtt.client.MQTT_ERR_NOMEM:
  453. # This error is wrongly classified
  454. # See https://github.com/eclipse/paho.mqtt.python/issues/340
  455. await self._dispatch(Disconnect(reason="Connection lost, retrying"))
  456. elif rc == paho.mqtt.client.MQTT_ERR_CONN_REFUSED:
  457. raise MQTTNotLoggedIn("MQTT connection refused")
  458. elif rc == paho.mqtt.client.MQTT_ERR_NO_CONN:
  459. if connection_retries > retry_limit:
  460. raise MQTTNotConnected(f"Connection failed {connection_retries} times")
  461. sleep = connection_retries * 2
  462. await self._dispatch(
  463. Disconnect(
  464. reason="MQTT Error: no connection, retrying "
  465. f"in {connection_retries} seconds"
  466. )
  467. )
  468. await asyncio.sleep(sleep)
  469. else:
  470. err = paho.mqtt.client.error_string(rc)
  471. self.log.error("MQTT Error: %s", err)
  472. await self._dispatch(Disconnect(reason=f"MQTT Error: {err}, retrying"))
  473. await self._reconnect()
  474. connection_retries += 1
  475. else:
  476. connection_retries = 0
  477. if self._disconnect_error:
  478. self.log.info("disconnect_error is set, raising and clearing variable")
  479. err = self._disconnect_error
  480. self._disconnect_error = None
  481. raise err
  482. # region Basic outgoing MQTT
  483. def publish(self, topic: RealtimeTopic, payload: str | bytes | dict) -> asyncio.Future:
  484. if isinstance(payload, dict):
  485. payload = json.dumps(payload)
  486. if isinstance(payload, str):
  487. payload = payload.encode("utf-8")
  488. self.log.trace(f"Publishing message in {topic.value} ({topic.encoded}): {payload}")
  489. payload = zlib.compress(payload, level=9)
  490. info = self._client.publish(topic.encoded, payload, qos=1)
  491. self.log.trace(f"Published message ID: {info.mid}")
  492. fut = asyncio.Future()
  493. self._publish_waiters[info.mid] = fut
  494. return fut
  495. async def request(
  496. self,
  497. topic: RealtimeTopic,
  498. response: RealtimeTopic,
  499. payload: str | bytes | dict,
  500. timeout: int | None = None,
  501. ) -> MQTTMessage:
  502. async with self._response_waiter_locks[response]:
  503. fut = asyncio.Future()
  504. self._response_waiters[response] = fut
  505. await self.publish(topic, payload)
  506. self.log.trace(
  507. f"Request published to {topic.value}, waiting for response {response.name}"
  508. )
  509. return await asyncio.wait_for(fut, timeout)
  510. async def iris_subscribe(self, seq_id: int, snapshot_at_ms: int) -> None:
  511. self.log.debug(f"Requesting iris subscribe {seq_id}/{snapshot_at_ms}")
  512. resp = await self.request(
  513. RealtimeTopic.SUB_IRIS,
  514. RealtimeTopic.SUB_IRIS_RESPONSE,
  515. {"seq_id": seq_id, "snapshot_at_ms": snapshot_at_ms},
  516. timeout=20 * 1000,
  517. )
  518. self.log.debug("Iris subscribe response: %s", resp.payload.decode("utf-8"))
  519. resp_dict = json.loads(resp.payload.decode("utf-8"))
  520. if resp_dict["error_type"] and resp_dict["error_message"]:
  521. raise IrisSubscribeError(resp_dict["error_type"], resp_dict["error_message"])
  522. def graphql_subscribe(self, subs: set[str]) -> asyncio.Future:
  523. self._graphql_subs |= subs
  524. return self.publish(RealtimeTopic.REALTIME_SUB, {"sub": list(subs)})
  525. def graphql_unsubscribe(self, subs: set[str]) -> asyncio.Future:
  526. self._graphql_subs -= subs
  527. return self.publish(RealtimeTopic.REALTIME_SUB, {"unsub": list(subs)})
  528. def skywalker_subscribe(self, subs: set[str]) -> asyncio.Future:
  529. self._skywalker_subs |= subs
  530. return self.publish(RealtimeTopic.PUBSUB, {"sub": list(subs)})
  531. def skywalker_unsubscribe(self, subs: set[str]) -> asyncio.Future:
  532. self._skywalker_subs -= subs
  533. return self.publish(RealtimeTopic.PUBSUB, {"unsub": list(subs)})
  534. # endregion
  535. # region Actually sending messages and stuff
  536. async def send_foreground_state(self, state: ForegroundStateConfig) -> None:
  537. self.log.debug("Updating foreground state: %s", state)
  538. await self.publish(
  539. RealtimeTopic.FOREGROUND_STATE, zlib.compress(state.to_thrift(), level=9)
  540. )
  541. if state.keep_alive_timeout:
  542. self._client._keepalive = state.keep_alive_timeout
  543. async def send_command(
  544. self,
  545. thread_id: str,
  546. action: ThreadAction,
  547. client_context: str | None = None,
  548. **kwargs: Any,
  549. ) -> CommandResponse | None:
  550. client_context = client_context or self.state.gen_client_context()
  551. req = {
  552. "thread_id": thread_id,
  553. "client_context": client_context,
  554. "offline_threading_id": client_context,
  555. "action": action.value,
  556. # "device_id": self.state.cookies["ig_did"],
  557. **kwargs,
  558. }
  559. if action in (ThreadAction.MARK_SEEN,):
  560. # Some commands don't have client_context in the response, so we can't properly match
  561. # them to the requests. We probably don't need the data, so just ignore it.
  562. await self.publish(RealtimeTopic.SEND_MESSAGE, payload=req)
  563. return None
  564. else:
  565. fut = asyncio.Future()
  566. self._message_response_waiters[client_context] = fut
  567. await self.publish(RealtimeTopic.SEND_MESSAGE, req)
  568. self.log.trace(
  569. f"Request published to {RealtimeTopic.SEND_MESSAGE}, "
  570. f"waiting for response {RealtimeTopic.SEND_MESSAGE_RESPONSE}"
  571. )
  572. resp = await fut
  573. return CommandResponse.parse_json(resp.payload.decode("utf-8"))
  574. def send_item(
  575. self,
  576. thread_id: str,
  577. item_type: ThreadItemType,
  578. shh_mode: bool = False,
  579. client_context: str | None = None,
  580. **kwargs: Any,
  581. ) -> Awaitable[CommandResponse]:
  582. return self.send_command(
  583. thread_id,
  584. item_type=item_type.value,
  585. is_shh_mode=str(int(shh_mode)),
  586. action=ThreadAction.SEND_ITEM,
  587. client_context=client_context,
  588. **kwargs,
  589. )
  590. def send_hashtag(
  591. self,
  592. thread_id: str,
  593. hashtag: str,
  594. text: str = "",
  595. shh_mode: bool = False,
  596. client_context: str | None = None,
  597. ) -> Awaitable[CommandResponse]:
  598. return self.send_item(
  599. thread_id,
  600. text=text,
  601. item_id=hashtag,
  602. shh_mode=shh_mode,
  603. item_type=ThreadItemType.HASHTAG,
  604. client_context=client_context,
  605. )
  606. def send_like(
  607. self, thread_id: str, shh_mode: bool = False, client_context: str | None = None
  608. ) -> Awaitable[CommandResponse]:
  609. return self.send_item(
  610. thread_id,
  611. shh_mode=shh_mode,
  612. item_type=ThreadItemType.LIKE,
  613. client_context=client_context,
  614. )
  615. def send_location(
  616. self,
  617. thread_id: str,
  618. venue_id: str,
  619. text: str = "",
  620. shh_mode: bool = False,
  621. client_context: str | None = None,
  622. ) -> Awaitable[CommandResponse]:
  623. return self.send_item(
  624. thread_id,
  625. text=text,
  626. item_id=venue_id,
  627. shh_mode=shh_mode,
  628. item_type=ThreadItemType.LOCATION,
  629. client_context=client_context,
  630. )
  631. def send_media(
  632. self,
  633. thread_id: str,
  634. media_id: str,
  635. text: str = "",
  636. shh_mode: bool = False,
  637. client_context: str | None = None,
  638. ) -> Awaitable[CommandResponse]:
  639. return self.send_item(
  640. thread_id,
  641. text=text,
  642. media_id=media_id,
  643. shh_mode=shh_mode,
  644. item_type=ThreadItemType.MEDIA_SHARE,
  645. client_context=client_context,
  646. )
  647. def send_profile(
  648. self,
  649. thread_id: str,
  650. user_id: str,
  651. text: str = "",
  652. shh_mode: bool = False,
  653. client_context: str | None = None,
  654. ) -> Awaitable[CommandResponse]:
  655. return self.send_item(
  656. thread_id,
  657. text=text,
  658. item_id=user_id,
  659. shh_mode=shh_mode,
  660. item_type=ThreadItemType.PROFILE,
  661. client_context=client_context,
  662. )
  663. def send_reaction(
  664. self,
  665. thread_id: str,
  666. emoji: str,
  667. item_id: str,
  668. reaction_status: ReactionStatus = ReactionStatus.CREATED,
  669. target_item_type: ThreadItemType = ThreadItemType.TEXT,
  670. shh_mode: bool = False,
  671. client_context: str | None = None,
  672. ) -> Awaitable[CommandResponse]:
  673. return self.send_item(
  674. thread_id,
  675. reaction_status=reaction_status.value,
  676. node_type="item",
  677. reaction_type="like",
  678. target_item_type=target_item_type.value,
  679. emoji=emoji,
  680. item_id=item_id,
  681. reaction_action_source="double_tap",
  682. shh_mode=shh_mode,
  683. item_type=ThreadItemType.REACTION,
  684. client_context=client_context,
  685. )
  686. def send_user_story(
  687. self,
  688. thread_id: str,
  689. media_id: str,
  690. text: str = "",
  691. shh_mode: bool = False,
  692. client_context: str | None = None,
  693. ) -> Awaitable[CommandResponse]:
  694. return self.send_item(
  695. thread_id,
  696. text=text,
  697. item_id=media_id,
  698. shh_mode=shh_mode,
  699. item_type=ThreadItemType.REEL_SHARE,
  700. client_context=client_context,
  701. )
  702. def send_text(
  703. self,
  704. thread_id: str,
  705. text: str = "",
  706. shh_mode: bool = False,
  707. client_context: str | None = None,
  708. ) -> Awaitable[CommandResponse]:
  709. return self.send_item(
  710. thread_id,
  711. text=text,
  712. shh_mode=shh_mode,
  713. item_type=ThreadItemType.TEXT,
  714. client_context=client_context,
  715. )
  716. def mark_seen(
  717. self, thread_id: str, item_id: str, client_context: str | None = None
  718. ) -> Awaitable[None]:
  719. return self.send_command(
  720. thread_id,
  721. item_id=item_id,
  722. action=ThreadAction.MARK_SEEN,
  723. client_context=client_context,
  724. )
  725. def mark_visual_item_seen(
  726. self, thread_id: str, item_id: str, client_context: str | None = None
  727. ) -> Awaitable[CommandResponse]:
  728. return self.send_command(
  729. thread_id,
  730. item_id=item_id,
  731. action=ThreadAction.MARK_VISUAL_ITEM_SEEN,
  732. client_context=client_context,
  733. )
  734. def indicate_activity(
  735. self,
  736. thread_id: str,
  737. activity_status: TypingStatus = TypingStatus.TEXT,
  738. client_context: str | None = None,
  739. ) -> Awaitable[CommandResponse]:
  740. return self.send_command(
  741. thread_id,
  742. activity_status=activity_status.value,
  743. action=ThreadAction.INDICATE_ACTIVITY,
  744. client_context=client_context,
  745. )
  746. # endregion