conn.py 32 KB

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