user.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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 TYPE_CHECKING, AsyncGenerator, AsyncIterable, Awaitable, cast
  18. import asyncio
  19. import logging
  20. import time
  21. from mauigpapi import AndroidAPI, AndroidMQTT, AndroidState
  22. from mauigpapi.errors import (
  23. IGChallengeError,
  24. IGCheckpointError,
  25. IGConsentRequiredError,
  26. IGNotLoggedInError,
  27. IGRateLimitError,
  28. IGUserIDNotFoundError,
  29. IrisSubscribeError,
  30. MQTTNotConnected,
  31. MQTTNotLoggedIn,
  32. )
  33. from mauigpapi.mqtt import Connect, Disconnect, GraphQLSubscription, SkywalkerSubscription
  34. from mauigpapi.types import (
  35. ActivityIndicatorData,
  36. CurrentUser,
  37. MessageSyncEvent,
  38. Operation,
  39. RealtimeDirectEvent,
  40. Thread,
  41. ThreadSyncEvent,
  42. TypingStatus,
  43. )
  44. from mautrix.appservice import AppService
  45. from mautrix.bridge import BaseUser, async_getter_lock
  46. from mautrix.types import EventID, MessageType, RoomID, TextMessageEventContent, UserID
  47. from mautrix.util.bridge_state import BridgeState, BridgeStateEvent
  48. from mautrix.util.logging import TraceLogger
  49. from mautrix.util.opt_prometheus import Gauge, Summary, async_time
  50. from . import portal as po, puppet as pu
  51. from .config import Config
  52. from .db import Portal as DBPortal, User as DBUser
  53. if TYPE_CHECKING:
  54. from .__main__ import InstagramBridge
  55. METRIC_MESSAGE = Summary("bridge_on_message", "calls to handle_message")
  56. METRIC_THREAD_SYNC = Summary("bridge_on_thread_sync", "calls to handle_thread_sync")
  57. METRIC_RTD = Summary("bridge_on_rtd", "calls to handle_rtd")
  58. METRIC_LOGGED_IN = Gauge("bridge_logged_in", "Users logged into the bridge")
  59. METRIC_CONNECTED = Gauge("bridge_connected", "Bridged users connected to Instagram")
  60. BridgeState.human_readable_errors.update(
  61. {
  62. "ig-connection-error": "Instagram disconnected unexpectedly",
  63. "ig-auth-error": "Authentication error from Instagram: {message}",
  64. "ig-checkpoint": "Instagram checkpoint error. Please check the Instagram website.",
  65. "ig-consent-required": "Instagram requires a consent update. Please check the Instagram website.",
  66. "ig-checkpoint-locked": "Instagram checkpoint error. Please check the Instagram website.",
  67. "ig-rate-limit": "Got Instagram ratelimit error, waiting a few minutes before retrying...",
  68. "ig-disconnected": None,
  69. "ig-no-mqtt": "You're not connected to Instagram",
  70. "logged-out": "You're not logged into Instagram",
  71. }
  72. )
  73. class User(DBUser, BaseUser):
  74. ig_base_log: TraceLogger = logging.getLogger("mau.instagram")
  75. _activity_indicator_ids: dict[str, int] = {}
  76. by_mxid: dict[UserID, User] = {}
  77. by_igpk: dict[int, User] = {}
  78. config: Config
  79. az: AppService
  80. loop: asyncio.AbstractEventLoop
  81. client: AndroidAPI | None
  82. mqtt: AndroidMQTT | None
  83. _listen_task: asyncio.Task | None = None
  84. permission_level: str
  85. username: str | None
  86. _notice_room_lock: asyncio.Lock
  87. _notice_send_lock: asyncio.Lock
  88. _is_logged_in: bool
  89. _is_connected: bool
  90. shutdown: bool
  91. remote_typing_status: TypingStatus | None
  92. def __init__(
  93. self,
  94. mxid: UserID,
  95. igpk: int | None = None,
  96. state: AndroidState | None = None,
  97. notice_room: RoomID | None = None,
  98. ) -> None:
  99. super().__init__(mxid=mxid, igpk=igpk, state=state, notice_room=notice_room)
  100. BaseUser.__init__(self)
  101. self._notice_room_lock = asyncio.Lock()
  102. self._notice_send_lock = asyncio.Lock()
  103. perms = self.config.get_permissions(mxid)
  104. self.relay_whitelisted, self.is_whitelisted, self.is_admin, self.permission_level = perms
  105. self.client = None
  106. self.mqtt = None
  107. self.username = None
  108. self._is_logged_in = False
  109. self._is_connected = False
  110. self._is_refreshing = False
  111. self.shutdown = False
  112. self._listen_task = None
  113. self.remote_typing_status = None
  114. @classmethod
  115. def init_cls(cls, bridge: "InstagramBridge") -> AsyncIterable[Awaitable[None]]:
  116. cls.bridge = bridge
  117. cls.config = bridge.config
  118. cls.az = bridge.az
  119. cls.loop = bridge.loop
  120. return (user.try_connect() async for user in cls.all_logged_in())
  121. # region Connection management
  122. async def is_logged_in(self) -> bool:
  123. return bool(self.client) and self._is_logged_in
  124. async def get_puppet(self) -> pu.Puppet | None:
  125. if not self.igpk:
  126. return None
  127. return await pu.Puppet.get_by_pk(self.igpk)
  128. async def get_portal_with(self, puppet: pu.Puppet, create: bool = True) -> po.Portal | None:
  129. if not self.igpk:
  130. return None
  131. portal = await po.Portal.find_private_chat(self.igpk, puppet.pk)
  132. if portal:
  133. return portal
  134. if create:
  135. # TODO add error handling somewhere
  136. thread = await self.client.create_group_thread([puppet.pk])
  137. portal = await po.Portal.get_by_thread(thread, self.igpk)
  138. await portal.update_info(thread, self)
  139. return portal
  140. return None
  141. async def try_connect(self) -> None:
  142. try:
  143. await self.connect()
  144. except Exception as e:
  145. self.log.exception("Error while connecting to Instagram")
  146. await self.push_bridge_state(
  147. BridgeStateEvent.UNKNOWN_ERROR, info={"python_error": str(e)}
  148. )
  149. @property
  150. def api_log(self) -> TraceLogger:
  151. return self.ig_base_log.getChild("http").getChild(self.mxid)
  152. @property
  153. def is_connected(self) -> bool:
  154. return bool(self.client) and bool(self.mqtt) and self._is_connected
  155. async def connect(self, user: CurrentUser | None = None) -> None:
  156. if not self.state:
  157. await self.push_bridge_state(BridgeStateEvent.BAD_CREDENTIALS, error="logged-out")
  158. return
  159. client = AndroidAPI(self.state, log=self.api_log)
  160. if not user:
  161. try:
  162. resp = await client.current_user()
  163. user = resp.user
  164. except IGNotLoggedInError as e:
  165. self.log.warning(f"Failed to connect to Instagram: {e}, logging out")
  166. await self.logout(error=e)
  167. return
  168. except (IGChallengeError, IGConsentRequiredError) as e:
  169. await self._handle_checkpoint(e, on="connect", client=client)
  170. return
  171. self.client = client
  172. self._is_logged_in = True
  173. self.igpk = user.pk
  174. self.username = user.username
  175. await self.push_bridge_state(BridgeStateEvent.CONNECTING)
  176. self._track_metric(METRIC_LOGGED_IN, True)
  177. self.by_igpk[self.igpk] = self
  178. self.mqtt = AndroidMQTT(
  179. self.state, loop=self.loop, log=self.ig_base_log.getChild("mqtt").getChild(self.mxid)
  180. )
  181. self.mqtt.add_event_handler(Connect, self.on_connect)
  182. self.mqtt.add_event_handler(Disconnect, self.on_disconnect)
  183. self.mqtt.add_event_handler(MessageSyncEvent, self.handle_message)
  184. self.mqtt.add_event_handler(ThreadSyncEvent, self.handle_thread_sync)
  185. self.mqtt.add_event_handler(RealtimeDirectEvent, self.handle_rtd)
  186. await self.update()
  187. self.loop.create_task(self._try_sync_puppet(user))
  188. self.loop.create_task(self._try_sync())
  189. async def on_connect(self, evt: Connect) -> None:
  190. self.log.debug("Connected to Instagram")
  191. self._track_metric(METRIC_CONNECTED, True)
  192. self._is_connected = True
  193. await self.send_bridge_notice("Connected to Instagram")
  194. await self.push_bridge_state(BridgeStateEvent.CONNECTED)
  195. async def on_disconnect(self, evt: Disconnect) -> None:
  196. self.log.debug("Disconnected from Instagram")
  197. self._track_metric(METRIC_CONNECTED, False)
  198. self._is_connected = False
  199. # TODO this stuff could probably be moved to mautrix-python
  200. async def get_notice_room(self) -> RoomID:
  201. if not self.notice_room:
  202. async with self._notice_room_lock:
  203. # If someone already created the room while this call was waiting,
  204. # don't make a new room
  205. if self.notice_room:
  206. return self.notice_room
  207. creation_content = {}
  208. if not self.config["bridge.federate_rooms"]:
  209. creation_content["m.federate"] = False
  210. self.notice_room = await self.az.intent.create_room(
  211. is_direct=True,
  212. invitees=[self.mxid],
  213. topic="Instagram bridge notices",
  214. creation_content=creation_content,
  215. )
  216. await self.update()
  217. return self.notice_room
  218. async def fill_bridge_state(self, state: BridgeState) -> None:
  219. await super().fill_bridge_state(state)
  220. if not state.remote_id:
  221. if self.igpk:
  222. state.remote_id = str(self.igpk)
  223. else:
  224. try:
  225. state.remote_id = self.state.user_id
  226. except IGUserIDNotFoundError:
  227. state.remote_id = None
  228. if self.username:
  229. state.remote_name = f"@{self.username}"
  230. async def get_bridge_states(self) -> list[BridgeState]:
  231. if not self.state:
  232. return []
  233. state = BridgeState(state_event=BridgeStateEvent.UNKNOWN_ERROR)
  234. if self.is_connected:
  235. state.state_event = BridgeStateEvent.CONNECTED
  236. elif self._is_refreshing or self.mqtt:
  237. state.state_event = BridgeStateEvent.TRANSIENT_DISCONNECT
  238. return [state]
  239. async def send_bridge_notice(
  240. self,
  241. text: str,
  242. edit: EventID | None = None,
  243. state_event: BridgeStateEvent | None = None,
  244. important: bool = False,
  245. error_code: str | None = None,
  246. error_message: str | None = None,
  247. info: dict | None = None,
  248. ) -> EventID | None:
  249. if state_event:
  250. await self.push_bridge_state(
  251. state_event,
  252. error=error_code,
  253. message=error_message if error_code else text,
  254. info=info,
  255. )
  256. if self.config["bridge.disable_bridge_notices"]:
  257. return None
  258. if not important and not self.config["bridge.unimportant_bridge_notices"]:
  259. self.log.debug("Not sending unimportant bridge notice: %s", text)
  260. return None
  261. event_id = None
  262. try:
  263. self.log.debug("Sending bridge notice: %s", text)
  264. content = TextMessageEventContent(
  265. body=text, msgtype=(MessageType.TEXT if important else MessageType.NOTICE)
  266. )
  267. if edit:
  268. content.set_edit(edit)
  269. # This is locked to prevent notices going out in the wrong order
  270. async with self._notice_send_lock:
  271. event_id = await self.az.intent.send_message(await self.get_notice_room(), content)
  272. except Exception:
  273. self.log.warning("Failed to send bridge notice", exc_info=True)
  274. return edit or event_id
  275. async def _try_sync_puppet(self, user_info: CurrentUser) -> None:
  276. puppet = await pu.Puppet.get_by_pk(self.igpk)
  277. try:
  278. await puppet.update_info(user_info, self)
  279. except Exception:
  280. self.log.exception("Failed to update own puppet info")
  281. try:
  282. if puppet.custom_mxid != self.mxid and puppet.can_auto_login(self.mxid):
  283. self.log.info(f"Automatically enabling custom puppet")
  284. await puppet.switch_mxid(access_token="auto", mxid=self.mxid)
  285. except Exception:
  286. self.log.exception("Failed to automatically enable custom puppet")
  287. async def _try_sync(self) -> None:
  288. try:
  289. await self.sync()
  290. except Exception as e:
  291. self.log.exception("Exception while syncing")
  292. if isinstance(e, IGCheckpointError):
  293. self.log.debug("Checkpoint error content: %s", e.body)
  294. await self.push_bridge_state(
  295. BridgeStateEvent.UNKNOWN_ERROR, info={"python_error": str(e)}
  296. )
  297. async def get_direct_chats(self) -> dict[UserID, list[RoomID]]:
  298. return {
  299. pu.Puppet.get_mxid_from_id(portal.other_user_pk): [portal.mxid]
  300. for portal in await DBPortal.find_private_chats_of(self.igpk)
  301. if portal.mxid
  302. }
  303. async def refresh(self, resync: bool = True) -> None:
  304. self._is_refreshing = True
  305. try:
  306. await self.stop_listen()
  307. if resync:
  308. retry_count = 0
  309. minutes = 1
  310. while True:
  311. try:
  312. await self.sync()
  313. return
  314. except Exception as e:
  315. if retry_count >= 4 and minutes < 10:
  316. minutes += 1
  317. retry_count += 1
  318. s = "s" if minutes != 1 else ""
  319. self.log.exception(
  320. f"Error while syncing for refresh, retrying in {minutes} minute{s}"
  321. )
  322. if isinstance(e, IGCheckpointError):
  323. self.log.debug("Checkpoint error content: %s", e.body)
  324. await self.push_bridge_state(
  325. BridgeStateEvent.UNKNOWN_ERROR,
  326. error="unknown-error",
  327. message="An unknown error occurred while connecting to Instagram",
  328. info={"python_error": str(e)},
  329. )
  330. await asyncio.sleep(minutes * 60)
  331. else:
  332. await self.start_listen()
  333. finally:
  334. self._is_refreshing = False
  335. async def _handle_checkpoint(
  336. self,
  337. e: IGChallengeError | IGConsentRequiredError,
  338. on: str,
  339. client: AndroidAPI | None = None,
  340. ) -> None:
  341. self.log.warning(f"Got checkpoint error on {on}: {e.body.serialize()}")
  342. client = client or self.client
  343. self.client = None
  344. self.mqtt = None
  345. if isinstance(e, IGConsentRequiredError):
  346. await self.push_bridge_state(
  347. BridgeStateEvent.BAD_CREDENTIALS,
  348. error="ig-consent-required",
  349. info=e.body.serialize(),
  350. )
  351. return
  352. error_code = "ig-checkpoint"
  353. try:
  354. resp = await client.challenge_reset()
  355. info = {
  356. "challenge_context": (
  357. resp.challenge_context.serialize() if resp.challenge_context_str else None
  358. ),
  359. "step_name": resp.step_name,
  360. "step_data": resp.step_data.serialize() if resp.step_data else None,
  361. "user_id": resp.user_id,
  362. "action": resp.action,
  363. "status": resp.status,
  364. "challenge": e.body.challenge.serialize() if e.body.challenge else None,
  365. }
  366. self.log.debug(f"Challenge state: {resp.serialize()}")
  367. if resp.challenge_context.challenge_type_enum == "HACKED_LOCK":
  368. error_code = "ig-checkpoint-locked"
  369. except Exception:
  370. self.log.exception("Error resetting challenge state")
  371. info = {"challenge": e.body.challenge.serialize() if e.body.challenge else None}
  372. await self.push_bridge_state(BridgeStateEvent.BAD_CREDENTIALS, error=error_code, info=info)
  373. async def _sync_thread(self, thread: Thread, min_active_at: int) -> None:
  374. portal = await po.Portal.get_by_thread(thread, self.igpk)
  375. if portal.mxid:
  376. self.log.debug(f"{thread.thread_id} has a portal, syncing and backfilling...")
  377. await portal.update_matrix_room(self, thread, backfill=True)
  378. elif thread.last_activity_at > min_active_at:
  379. self.log.debug(f"{thread.thread_id} has been active recently, creating portal...")
  380. await portal.create_matrix_room(self, thread)
  381. else:
  382. self.log.debug(f"{thread.thread_id} is not active and doesn't have a portal")
  383. async def sync(self) -> None:
  384. sleep_minutes = 2
  385. while True:
  386. try:
  387. resp = await self.client.get_inbox()
  388. break
  389. except IGNotLoggedInError as e:
  390. self.log.exception("Got not logged in error while syncing")
  391. await self.logout(error=e)
  392. return
  393. except IGRateLimitError as e:
  394. self.log.error(
  395. "Got ratelimit error while trying to get inbox (%s), retrying in %d minutes",
  396. e.body,
  397. sleep_minutes,
  398. )
  399. await self.push_bridge_state(
  400. BridgeStateEvent.TRANSIENT_DISCONNECT, error="ig-rate-limit"
  401. )
  402. await asyncio.sleep(sleep_minutes * 60)
  403. sleep_minutes += 2
  404. except (IGChallengeError, IGConsentRequiredError) as e:
  405. await self._handle_checkpoint(e, on="sync")
  406. return
  407. if not self._listen_task:
  408. await self.start_listen(resp.seq_id, resp.snapshot_at_ms)
  409. max_age = self.config["bridge.portal_create_max_age"] * 1_000_000
  410. limit = self.config["bridge.chat_sync_limit"]
  411. min_active_at = (time.time() * 1_000_000) - max_age
  412. i = 0
  413. await self.push_bridge_state(BridgeStateEvent.BACKFILLING)
  414. async for thread in self.client.iter_inbox(start_at=resp):
  415. try:
  416. await self._sync_thread(thread, min_active_at)
  417. except Exception:
  418. self.log.exception(f"Error syncing thread {thread.thread_id}")
  419. i += 1
  420. if i >= limit:
  421. break
  422. try:
  423. await self.update_direct_chats()
  424. except Exception:
  425. self.log.exception("Error updating direct chat list")
  426. async def start_listen(
  427. self, seq_id: int | None = None, snapshot_at_ms: int | None = None
  428. ) -> None:
  429. self.shutdown = False
  430. if not seq_id:
  431. resp = await self.client.get_inbox(limit=1)
  432. seq_id, snapshot_at_ms = resp.seq_id, resp.snapshot_at_ms
  433. task = self.listen(seq_id=seq_id, snapshot_at_ms=snapshot_at_ms)
  434. self._listen_task = self.loop.create_task(task)
  435. async def listen(self, seq_id: int, snapshot_at_ms: int) -> None:
  436. try:
  437. await self.mqtt.listen(
  438. graphql_subs={
  439. GraphQLSubscription.app_presence(),
  440. GraphQLSubscription.direct_typing(self.state.user_id),
  441. GraphQLSubscription.direct_status(),
  442. },
  443. skywalker_subs={
  444. SkywalkerSubscription.direct_sub(self.state.user_id),
  445. SkywalkerSubscription.live_sub(self.state.user_id),
  446. },
  447. seq_id=seq_id,
  448. snapshot_at_ms=snapshot_at_ms,
  449. )
  450. except IrisSubscribeError as e:
  451. self.log.warning(f"Got IrisSubscribeError {e}, refreshing...")
  452. await self.refresh()
  453. except (MQTTNotConnected, MQTTNotLoggedIn) as e:
  454. await self.send_bridge_notice(
  455. f"Error in listener: {e}",
  456. important=True,
  457. state_event=BridgeStateEvent.UNKNOWN_ERROR,
  458. error_code="ig-connection-error",
  459. )
  460. self.mqtt.disconnect()
  461. except Exception as e:
  462. self.log.exception("Fatal error in listener")
  463. await self.send_bridge_notice(
  464. "Fatal error in listener (see logs for more info)",
  465. state_event=BridgeStateEvent.UNKNOWN_ERROR,
  466. important=True,
  467. error_code="ig-connection-error",
  468. info={"python_error": str(e)},
  469. )
  470. self.mqtt.disconnect()
  471. else:
  472. if not self.shutdown:
  473. await self.send_bridge_notice(
  474. "Instagram connection closed without error",
  475. state_event=BridgeStateEvent.UNKNOWN_ERROR,
  476. error_code="ig-disconnected",
  477. )
  478. finally:
  479. self._listen_task = None
  480. self._is_connected = False
  481. self._track_metric(METRIC_CONNECTED, False)
  482. async def stop_listen(self) -> None:
  483. if self.mqtt:
  484. self.shutdown = True
  485. self.mqtt.disconnect()
  486. if self._listen_task:
  487. await self._listen_task
  488. self.shutdown = False
  489. self._track_metric(METRIC_CONNECTED, False)
  490. self._is_connected = False
  491. await self.update()
  492. async def logout(self, error: IGNotLoggedInError | None = None) -> None:
  493. if self.client and error is None:
  494. try:
  495. await self.client.logout(one_tap_app_login=False)
  496. except Exception:
  497. self.log.debug("Exception logging out", exc_info=True)
  498. if self.mqtt:
  499. self.mqtt.disconnect()
  500. self._track_metric(METRIC_CONNECTED, False)
  501. self._track_metric(METRIC_LOGGED_IN, False)
  502. if error is None:
  503. await self.push_bridge_state(BridgeStateEvent.LOGGED_OUT)
  504. puppet = await pu.Puppet.get_by_pk(self.igpk, create=False)
  505. if puppet and puppet.is_real_user:
  506. await puppet.switch_mxid(None, None)
  507. try:
  508. del self.by_igpk[self.igpk]
  509. except KeyError:
  510. pass
  511. self.igpk = None
  512. else:
  513. self.log.debug("Auth error body: %s", error.body.serialize())
  514. await self.send_bridge_notice(
  515. f"You have been logged out of Instagram: {error.proper_message}",
  516. important=True,
  517. state_event=BridgeStateEvent.BAD_CREDENTIALS,
  518. error_code="ig-auth-error",
  519. error_message=error.proper_message,
  520. )
  521. self.client = None
  522. self.mqtt = None
  523. self.state = None
  524. self._is_logged_in = False
  525. await self.update()
  526. # endregion
  527. # region Event handlers
  528. @async_time(METRIC_MESSAGE)
  529. async def handle_message(self, evt: MessageSyncEvent) -> None:
  530. portal = await po.Portal.get_by_thread_id(evt.message.thread_id, receiver=self.igpk)
  531. if not portal or not portal.mxid:
  532. self.log.debug("Got message in thread with no portal, getting info...")
  533. resp = await self.client.get_thread(evt.message.thread_id)
  534. portal = await po.Portal.get_by_thread(resp.thread, self.igpk)
  535. self.log.debug("Got info for unknown portal, creating room")
  536. await portal.create_matrix_room(self, resp.thread)
  537. if not portal.mxid:
  538. self.log.warning(
  539. "Room creation appears to have failed, "
  540. f"dropping message in {evt.message.thread_id}"
  541. )
  542. return
  543. self.log.trace(f"Received message sync event {evt.message}")
  544. sender = await pu.Puppet.get_by_pk(evt.message.user_id) if evt.message.user_id else None
  545. if evt.message.op == Operation.ADD:
  546. if not sender:
  547. # I don't think we care about adds with no sender
  548. return
  549. await portal.handle_instagram_item(self, sender, evt.message)
  550. elif evt.message.op == Operation.REMOVE:
  551. # Removes don't have a sender, only the message sender can unsend messages anyway
  552. await portal.handle_instagram_remove(evt.message.item_id)
  553. elif evt.message.op == Operation.REPLACE:
  554. await portal.handle_instagram_update(evt.message)
  555. @async_time(METRIC_THREAD_SYNC)
  556. async def handle_thread_sync(self, evt: ThreadSyncEvent) -> None:
  557. self.log.trace("Received thread sync event %s", evt)
  558. portal = await po.Portal.get_by_thread(evt, receiver=self.igpk)
  559. await portal.create_matrix_room(self, evt)
  560. @async_time(METRIC_RTD)
  561. async def handle_rtd(self, evt: RealtimeDirectEvent) -> None:
  562. if not isinstance(evt.value, ActivityIndicatorData):
  563. return
  564. now = int(time.time() * 1000)
  565. date = evt.value.timestamp_ms
  566. expiry = date + evt.value.ttl
  567. if expiry < now:
  568. return
  569. if evt.activity_indicator_id in self._activity_indicator_ids:
  570. return
  571. # TODO clear expired items from this dict
  572. self._activity_indicator_ids[evt.activity_indicator_id] = expiry
  573. puppet = await pu.Puppet.get_by_pk(int(evt.value.sender_id))
  574. portal = await po.Portal.get_by_thread_id(evt.thread_id, receiver=self.igpk)
  575. if not puppet or not portal or not portal.mxid:
  576. return
  577. is_typing = evt.value.activity_status != TypingStatus.OFF
  578. if puppet.pk == self.igpk:
  579. self.remote_typing_status = TypingStatus.TEXT if is_typing else TypingStatus.OFF
  580. await puppet.intent_for(portal).set_typing(
  581. portal.mxid, is_typing=is_typing, timeout=evt.value.ttl
  582. )
  583. # endregion
  584. # region Database getters
  585. def _add_to_cache(self) -> None:
  586. self.by_mxid[self.mxid] = self
  587. if self.igpk:
  588. self.by_igpk[self.igpk] = self
  589. @classmethod
  590. @async_getter_lock
  591. async def get_by_mxid(cls, mxid: UserID, *, create: bool = True) -> User | None:
  592. # Never allow ghosts to be users
  593. if pu.Puppet.get_id_from_mxid(mxid):
  594. return None
  595. try:
  596. return cls.by_mxid[mxid]
  597. except KeyError:
  598. pass
  599. user = cast(cls, await super().get_by_mxid(mxid))
  600. if user is not None:
  601. user._add_to_cache()
  602. return user
  603. if create:
  604. user = cls(mxid)
  605. await user.insert()
  606. user._add_to_cache()
  607. return user
  608. return None
  609. @classmethod
  610. @async_getter_lock
  611. async def get_by_igpk(cls, igpk: int) -> User | None:
  612. try:
  613. return cls.by_igpk[igpk]
  614. except KeyError:
  615. pass
  616. user = cast(cls, await super().get_by_igpk(igpk))
  617. if user is not None:
  618. user._add_to_cache()
  619. return user
  620. return None
  621. @classmethod
  622. async def all_logged_in(cls) -> AsyncGenerator[User, None]:
  623. users = await super().all_logged_in()
  624. user: cls
  625. for index, user in enumerate(users):
  626. try:
  627. yield cls.by_mxid[user.mxid]
  628. except KeyError:
  629. user._add_to_cache()
  630. yield user
  631. # endregion