conn.py 28 KB

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