user.py 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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, Callable, cast
  18. from datetime import datetime, timedelta
  19. from functools import partial
  20. import asyncio
  21. import logging
  22. import time
  23. from aiohttp import ClientConnectionError
  24. from mauigpapi import AndroidAPI, AndroidMQTT, AndroidState, ProxyHandler
  25. from mauigpapi.errors import (
  26. IGChallengeError,
  27. IGCheckpointError,
  28. IGConsentRequiredError,
  29. IGLoginRequiredError,
  30. IGNotLoggedInError,
  31. IGRateLimitError,
  32. IGUserIDNotFoundError,
  33. IrisSubscribeError,
  34. MQTTConnectionUnauthorized,
  35. MQTTNotConnected,
  36. MQTTNotLoggedIn,
  37. )
  38. from mauigpapi.mqtt import (
  39. Connect,
  40. Disconnect,
  41. GraphQLSubscription,
  42. NewSequenceID,
  43. ProxyUpdate,
  44. SkywalkerSubscription,
  45. )
  46. from mauigpapi.types import (
  47. ActivityIndicatorData,
  48. CurrentUser,
  49. MessageSyncEvent,
  50. Operation,
  51. RealtimeDirectEvent,
  52. Thread,
  53. ThreadRemoveEvent,
  54. ThreadSyncEvent,
  55. TypingStatus,
  56. )
  57. from mauigpapi.types.direct_inbox import DMInbox, DMInboxResponse
  58. from mautrix.appservice import AppService
  59. from mautrix.bridge import BaseUser, async_getter_lock
  60. from mautrix.types import EventID, MessageType, RoomID, TextMessageEventContent, UserID
  61. from mautrix.util.bridge_state import BridgeState, BridgeStateEvent
  62. from mautrix.util.logging import TraceLogger
  63. from mautrix.util.opt_prometheus import Gauge, Summary, async_time
  64. from mautrix.util.simple_lock import SimpleLock
  65. from . import portal as po, puppet as pu
  66. from .config import Config
  67. from .db import Backfill, Message as DBMessage, Portal as DBPortal, User as DBUser
  68. if TYPE_CHECKING:
  69. from .__main__ import InstagramBridge
  70. try:
  71. from aiohttp_socks import ProxyConnectionError, ProxyError, ProxyTimeoutError
  72. except ImportError:
  73. class ProxyError(Exception):
  74. pass
  75. ProxyConnectionError = ProxyTimeoutError = ProxyError
  76. METRIC_MESSAGE = Summary("bridge_on_message", "calls to handle_message")
  77. METRIC_THREAD_SYNC = Summary("bridge_on_thread_sync", "calls to handle_thread_sync")
  78. METRIC_RTD = Summary("bridge_on_rtd", "calls to handle_rtd")
  79. METRIC_LOGGED_IN = Gauge("bridge_logged_in", "Users logged into the bridge")
  80. METRIC_CONNECTED = Gauge("bridge_connected", "Bridged users connected to Instagram")
  81. BridgeState.human_readable_errors.update(
  82. {
  83. "ig-connection-error": "Instagram disconnected unexpectedly",
  84. "ig-refresh-connection-error": "Reconnecting failed again after refresh: {message}",
  85. "ig-connection-fatal-error": "Instagram disconnected unexpectedly",
  86. "ig-auth-error": "Authentication error from Instagram: {message}",
  87. "ig-checkpoint": "Instagram checkpoint error. Please check the Instagram website.",
  88. "ig-consent-required": "Instagram requires a consent update. Please check the Instagram website.",
  89. "ig-checkpoint-locked": "Instagram checkpoint error. Please check the Instagram website.",
  90. "ig-rate-limit": "Got Instagram ratelimit error, waiting a few minutes before retrying...",
  91. "ig-disconnected": None,
  92. "ig-no-mqtt": "You're not connected to Instagram",
  93. "logged-out": "You're not logged into Instagram",
  94. }
  95. )
  96. class User(DBUser, BaseUser):
  97. ig_base_log: TraceLogger = logging.getLogger("mau.instagram")
  98. _activity_indicator_ids: dict[str, int] = {}
  99. by_mxid: dict[UserID, User] = {}
  100. by_igpk: dict[int, User] = {}
  101. config: Config
  102. az: AppService
  103. loop: asyncio.AbstractEventLoop
  104. client: AndroidAPI | None
  105. mqtt: AndroidMQTT | None
  106. _listen_task: asyncio.Task | None = None
  107. _sync_lock: SimpleLock
  108. _backfill_loop_task: asyncio.Task | None
  109. _thread_sync_task: asyncio.Task | None
  110. _seq_id_save_task: asyncio.Task | None
  111. permission_level: str
  112. username: str | None
  113. _notice_room_lock: asyncio.Lock
  114. _notice_send_lock: asyncio.Lock
  115. _is_logged_in: bool
  116. _is_connected: bool
  117. shutdown: bool
  118. remote_typing_status: TypingStatus | None
  119. def __init__(
  120. self,
  121. mxid: UserID,
  122. igpk: int | None = None,
  123. state: AndroidState | None = None,
  124. notice_room: RoomID | None = None,
  125. seq_id: int | None = None,
  126. snapshot_at_ms: int | None = None,
  127. oldest_cursor: str | None = None,
  128. total_backfilled_portals: int | None = None,
  129. thread_sync_completed: bool = False,
  130. ) -> None:
  131. super().__init__(
  132. mxid=mxid,
  133. igpk=igpk,
  134. state=state,
  135. notice_room=notice_room,
  136. seq_id=seq_id,
  137. snapshot_at_ms=snapshot_at_ms,
  138. oldest_cursor=oldest_cursor,
  139. total_backfilled_portals=total_backfilled_portals,
  140. thread_sync_completed=thread_sync_completed,
  141. )
  142. BaseUser.__init__(self)
  143. self._notice_room_lock = asyncio.Lock()
  144. self._notice_send_lock = asyncio.Lock()
  145. perms = self.config.get_permissions(mxid)
  146. self.relay_whitelisted, self.is_whitelisted, self.is_admin, self.permission_level = perms
  147. self.client = None
  148. self.mqtt = None
  149. self.username = None
  150. self._is_logged_in = False
  151. self._is_connected = False
  152. self._is_refreshing = False
  153. self.shutdown = False
  154. self._sync_lock = SimpleLock(
  155. "Waiting for thread sync to finish before handling %s", log=self.log
  156. )
  157. self._listen_task = None
  158. self._thread_sync_task = None
  159. self._backfill_loop_task = None
  160. self.remote_typing_status = None
  161. self._seq_id_save_task = None
  162. self.proxy_handler = ProxyHandler(
  163. api_url=self.config["bridge.get_proxy_api_url"],
  164. )
  165. @classmethod
  166. def init_cls(cls, bridge: "InstagramBridge") -> AsyncIterable[Awaitable[None]]:
  167. cls.bridge = bridge
  168. cls.config = bridge.config
  169. cls.az = bridge.az
  170. cls.loop = bridge.loop
  171. return (user.try_connect() async for user in cls.all_logged_in())
  172. # region Connection management
  173. async def is_logged_in(self) -> bool:
  174. return bool(self.client) and self._is_logged_in
  175. async def get_puppet(self) -> pu.Puppet | None:
  176. if not self.igpk:
  177. return None
  178. return await pu.Puppet.get_by_pk(self.igpk)
  179. async def get_portal_with(self, puppet: pu.Puppet, create: bool = True) -> po.Portal | None:
  180. if not self.igpk:
  181. return None
  182. portal = await po.Portal.find_private_chat(self.igpk, puppet.pk)
  183. if portal:
  184. return portal
  185. if create:
  186. # TODO add error handling somewhere
  187. thread = await self.client.create_group_thread([puppet.pk])
  188. portal = await po.Portal.get_by_thread(thread, self.igpk)
  189. await portal.update_info(thread, self)
  190. return portal
  191. return None
  192. async def try_connect(self) -> None:
  193. try:
  194. await self.connect()
  195. except Exception as e:
  196. self.log.exception("Error while connecting to Instagram")
  197. await self.push_bridge_state(
  198. BridgeStateEvent.UNKNOWN_ERROR, info={"python_error": str(e)}
  199. )
  200. @property
  201. def api_log(self) -> TraceLogger:
  202. return self.ig_base_log.getChild("http").getChild(self.mxid)
  203. @property
  204. def is_connected(self) -> bool:
  205. return bool(self.client) and bool(self.mqtt) and self._is_connected
  206. async def connect(self, user: CurrentUser | None = None) -> None:
  207. if not self.state:
  208. await self.push_bridge_state(BridgeStateEvent.BAD_CREDENTIALS, error="logged-out")
  209. return
  210. client = AndroidAPI(
  211. self.state,
  212. log=self.api_log,
  213. proxy_handler=self.proxy_handler,
  214. )
  215. if not user:
  216. try:
  217. resp = await client.current_user()
  218. user = resp.user
  219. except IGNotLoggedInError as e:
  220. self.log.warning(f"Failed to connect to Instagram: {e}, logging out")
  221. await self.logout(error=e)
  222. return
  223. except IGCheckpointError as e:
  224. self.log.debug("Checkpoint error content: %s", e.body)
  225. raise
  226. except (IGChallengeError, IGConsentRequiredError) as e:
  227. await self._handle_checkpoint(e, on="connect", client=client)
  228. return
  229. self.client = client
  230. self._is_logged_in = True
  231. self.igpk = user.pk
  232. self.username = user.username
  233. await self.push_bridge_state(BridgeStateEvent.CONNECTING)
  234. self._track_metric(METRIC_LOGGED_IN, True)
  235. self.by_igpk[self.igpk] = self
  236. self.mqtt = AndroidMQTT(
  237. self.state,
  238. loop=self.loop,
  239. log=self.ig_base_log.getChild("mqtt").getChild(self.mxid),
  240. proxy_handler=self.proxy_handler,
  241. )
  242. self.mqtt.add_event_handler(Connect, self.on_connect)
  243. self.mqtt.add_event_handler(Disconnect, self.on_disconnect)
  244. self.mqtt.add_event_handler(NewSequenceID, self.update_seq_id)
  245. self.mqtt.add_event_handler(MessageSyncEvent, self.handle_message)
  246. self.mqtt.add_event_handler(ThreadSyncEvent, self.handle_thread_sync)
  247. self.mqtt.add_event_handler(ThreadRemoveEvent, self.handle_thread_remove)
  248. self.mqtt.add_event_handler(RealtimeDirectEvent, self.handle_rtd)
  249. self.mqtt.add_event_handler(ProxyUpdate, self.on_proxy_update)
  250. await self.update()
  251. self.loop.create_task(self._try_sync_puppet(user))
  252. self.loop.create_task(self._post_connect())
  253. async def _post_connect(self):
  254. # Backfill requests are handled synchronously so as not to overload the homeserver.
  255. # Users can configure their backfill stages to be more or less aggressive with backfilling
  256. # to try and avoid getting banned.
  257. if not self._backfill_loop_task or self._backfill_loop_task.done():
  258. self._backfill_loop_task = asyncio.create_task(self._handle_backfill_requests_loop())
  259. if not self.seq_id:
  260. await self._try_sync()
  261. else:
  262. self.log.debug("Connecting to MQTT directly as resync_on_startup is false")
  263. self.start_listen()
  264. if self.config["bridge.backfill.enable"]:
  265. if self._thread_sync_task and not self._thread_sync_task.done():
  266. self.log.warning("Cancelling existing background thread sync task")
  267. self._thread_sync_task.cancel()
  268. self._thread_sync_task = asyncio.create_task(self.backfill_threads())
  269. async def _handle_backfill_requests_loop(self) -> None:
  270. if not self.config["bridge.backfill.enable"] or not self.config["bridge.backfill.msc2716"]:
  271. return
  272. while True:
  273. await self._sync_lock.wait("backfill request")
  274. req = await Backfill.get_next(self.mxid)
  275. if not req:
  276. await asyncio.sleep(30)
  277. continue
  278. self.log.info("Backfill request %s", req)
  279. try:
  280. portal = await po.Portal.get_by_thread_id(
  281. req.portal_thread_id, receiver=req.portal_receiver
  282. )
  283. await req.mark_dispatched()
  284. await portal.backfill(self, req)
  285. await req.mark_done()
  286. except IGLoginRequiredError as e:
  287. self.log.exception(
  288. "User is logged out. Stopping backfill requests loop and forcing refresh."
  289. )
  290. await self.refresh(resync=False)
  291. break
  292. except IGChallengeError as e:
  293. self.log.exception(
  294. "User received a challenge. Stopping backfill requests loop and "
  295. "forcing refresh."
  296. )
  297. await self.refresh(resync=False)
  298. break
  299. except Exception as e:
  300. self.log.exception("Failed to backfill portal %s: %s", req.portal_thread_id, e)
  301. # Don't try again to backfill this portal for a minute.
  302. await req.set_cooldown_timeout(60)
  303. self._backfill_loop_task = None
  304. async def on_connect(self, evt: Connect) -> None:
  305. self.log.debug("Connected to Instagram")
  306. self._track_metric(METRIC_CONNECTED, True)
  307. self._is_connected = True
  308. await self.send_bridge_notice("Connected to Instagram")
  309. await self.push_bridge_state(BridgeStateEvent.CONNECTED)
  310. async def on_disconnect(self, evt: Disconnect) -> None:
  311. self.log.debug("Disconnected from Instagram")
  312. self._track_metric(METRIC_CONNECTED, False)
  313. self._is_connected = False
  314. async def on_proxy_update(self, evt: ProxyUpdate | None = None) -> None:
  315. if self.client:
  316. self.client.setup_http(self.state.cookies.jar)
  317. # TODO this stuff could probably be moved to mautrix-python
  318. async def get_notice_room(self) -> RoomID:
  319. if not self.notice_room:
  320. async with self._notice_room_lock:
  321. # If someone already created the room while this call was waiting,
  322. # don't make a new room
  323. if self.notice_room:
  324. return self.notice_room
  325. creation_content = {}
  326. if not self.config["bridge.federate_rooms"]:
  327. creation_content["m.federate"] = False
  328. self.notice_room = await self.az.intent.create_room(
  329. is_direct=True,
  330. invitees=[self.mxid],
  331. topic="Instagram bridge notices",
  332. creation_content=creation_content,
  333. )
  334. await self.update()
  335. return self.notice_room
  336. async def fill_bridge_state(self, state: BridgeState) -> None:
  337. await super().fill_bridge_state(state)
  338. if not state.remote_id:
  339. if self.igpk:
  340. state.remote_id = str(self.igpk)
  341. else:
  342. try:
  343. state.remote_id = self.state.user_id
  344. except IGUserIDNotFoundError:
  345. state.remote_id = None
  346. if self.username:
  347. state.remote_name = f"@{self.username}"
  348. async def get_bridge_states(self) -> list[BridgeState]:
  349. if not self.state:
  350. return []
  351. state = BridgeState(state_event=BridgeStateEvent.UNKNOWN_ERROR)
  352. if self.is_connected:
  353. state.state_event = BridgeStateEvent.CONNECTED
  354. elif self._is_refreshing or self.mqtt:
  355. state.state_event = BridgeStateEvent.TRANSIENT_DISCONNECT
  356. return [state]
  357. async def send_bridge_notice(
  358. self,
  359. text: str,
  360. edit: EventID | None = None,
  361. state_event: BridgeStateEvent | None = None,
  362. important: bool = False,
  363. error_code: str | None = None,
  364. error_message: str | None = None,
  365. info: dict | None = None,
  366. ) -> EventID | None:
  367. if state_event:
  368. await self.push_bridge_state(
  369. state_event,
  370. error=error_code,
  371. message=error_message if error_code else text,
  372. info=info,
  373. )
  374. if self.config["bridge.disable_bridge_notices"]:
  375. return None
  376. if not important and not self.config["bridge.unimportant_bridge_notices"]:
  377. self.log.debug("Not sending unimportant bridge notice: %s", text)
  378. return None
  379. event_id = None
  380. try:
  381. self.log.debug("Sending bridge notice: %s", text)
  382. content = TextMessageEventContent(
  383. body=text, msgtype=(MessageType.TEXT if important else MessageType.NOTICE)
  384. )
  385. if edit:
  386. content.set_edit(edit)
  387. # This is locked to prevent notices going out in the wrong order
  388. async with self._notice_send_lock:
  389. event_id = await self.az.intent.send_message(await self.get_notice_room(), content)
  390. except Exception:
  391. self.log.warning("Failed to send bridge notice", exc_info=True)
  392. return edit or event_id
  393. async def _try_sync_puppet(self, user_info: CurrentUser) -> None:
  394. puppet = await pu.Puppet.get_by_pk(self.igpk)
  395. try:
  396. await puppet.update_info(user_info, self)
  397. except Exception:
  398. self.log.exception("Failed to update own puppet info")
  399. try:
  400. if puppet.custom_mxid != self.mxid and puppet.can_auto_login(self.mxid):
  401. self.log.info(f"Automatically enabling custom puppet")
  402. await puppet.switch_mxid(access_token="auto", mxid=self.mxid)
  403. except Exception:
  404. self.log.exception("Failed to automatically enable custom puppet")
  405. async def _try_sync(self) -> None:
  406. try:
  407. await self.sync()
  408. except Exception as e:
  409. self.log.exception("Exception while syncing")
  410. if isinstance(e, IGCheckpointError):
  411. self.log.debug("Checkpoint error content: %s", e.body)
  412. await self.push_bridge_state(
  413. BridgeStateEvent.UNKNOWN_ERROR, info={"python_error": str(e)}
  414. )
  415. async def get_direct_chats(self) -> dict[UserID, list[RoomID]]:
  416. return {
  417. pu.Puppet.get_mxid_from_id(portal.other_user_pk): [portal.mxid]
  418. for portal in await DBPortal.find_private_chats_of(self.igpk)
  419. if portal.mxid
  420. }
  421. async def refresh(self, resync: bool = True) -> None:
  422. self._is_refreshing = True
  423. try:
  424. await self.stop_listen()
  425. if resync:
  426. retry_count = 0
  427. minutes = 1
  428. while True:
  429. try:
  430. await self.sync()
  431. return
  432. except Exception as e:
  433. if retry_count >= 4 and minutes < 10:
  434. minutes += 1
  435. retry_count += 1
  436. s = "s" if minutes != 1 else ""
  437. self.log.exception(
  438. f"Error while syncing for refresh, retrying in {minutes} minute{s}"
  439. )
  440. if isinstance(e, IGCheckpointError):
  441. self.log.debug("Checkpoint error content: %s", e.body)
  442. await self.push_bridge_state(
  443. BridgeStateEvent.UNKNOWN_ERROR,
  444. error="unknown-error",
  445. message="An unknown error occurred while connecting to Instagram",
  446. info={"python_error": str(e)},
  447. )
  448. await asyncio.sleep(minutes * 60)
  449. else:
  450. self.start_listen()
  451. finally:
  452. self._is_refreshing = False
  453. self.proxy_handler.update_proxy_url()
  454. async def _handle_checkpoint(
  455. self,
  456. e: IGChallengeError | IGConsentRequiredError,
  457. on: str,
  458. client: AndroidAPI | None = None,
  459. ) -> None:
  460. self.log.warning(f"Got checkpoint error on {on}: {e.body.serialize()}")
  461. client = client or self.client
  462. self.client = None
  463. self.mqtt = None
  464. if isinstance(e, IGConsentRequiredError):
  465. await self.push_bridge_state(
  466. BridgeStateEvent.BAD_CREDENTIALS,
  467. error="ig-consent-required",
  468. info=e.body.serialize(),
  469. )
  470. return
  471. error_code = "ig-checkpoint"
  472. try:
  473. resp = await client.challenge_reset()
  474. info = {
  475. "challenge_context": (
  476. resp.challenge_context.serialize() if resp.challenge_context_str else None
  477. ),
  478. "step_name": resp.step_name,
  479. "step_data": resp.step_data.serialize() if resp.step_data else None,
  480. "user_id": resp.user_id,
  481. "action": resp.action,
  482. "status": resp.status,
  483. "challenge": e.body.challenge.serialize() if e.body.challenge else None,
  484. }
  485. self.log.debug(f"Challenge state: {resp.serialize()}")
  486. if resp.challenge_context.challenge_type_enum == "HACKED_LOCK":
  487. error_code = "ig-checkpoint-locked"
  488. except Exception:
  489. self.log.exception("Error resetting challenge state")
  490. info = {"challenge": e.body.challenge.serialize() if e.body.challenge else None}
  491. await self.push_bridge_state(BridgeStateEvent.BAD_CREDENTIALS, error=error_code, info=info)
  492. async def _sync_thread(self, thread: Thread) -> bool:
  493. """
  494. Sync a specific thread. Returns whether the thread had messages after the last message in
  495. the database before the sync.
  496. """
  497. self.log.debug(f"Syncing thread {thread.thread_id}")
  498. forward_messages = thread.items
  499. assert self.client
  500. portal = await po.Portal.get_by_thread(thread, self.igpk)
  501. assert portal
  502. # Create or update the Matrix room
  503. if not portal.mxid:
  504. await portal.create_matrix_room(self, thread)
  505. else:
  506. await portal.update_matrix_room(self, thread)
  507. last_message = await DBMessage.get_last(portal.mxid)
  508. cursor = thread.oldest_cursor
  509. if last_message:
  510. original_number_of_messages = len(thread.items)
  511. new_messages = [
  512. m for m in thread.items if last_message.ig_timestamp_ms < m.timestamp_ms
  513. ]
  514. forward_messages = new_messages
  515. portal.log.debug(
  516. f"{len(new_messages)}/{original_number_of_messages} messages are after most recent"
  517. " message."
  518. )
  519. # Fetch more messages until we get back to messages that have been bridged already.
  520. while len(new_messages) > 0 and len(new_messages) == original_number_of_messages:
  521. await asyncio.sleep(self.config["bridge.backfill.incremental.page_delay"])
  522. portal.log.debug("Fetching more messages for forward backfill")
  523. resp = await self.client.get_thread(portal.thread_id, cursor=cursor)
  524. if len(resp.thread.items) == 0:
  525. break
  526. original_number_of_messages = len(resp.thread.items)
  527. new_messages = [
  528. m for m in resp.thread.items if last_message.ig_timestamp_ms < m.timestamp_ms
  529. ]
  530. forward_messages = new_messages + forward_messages
  531. cursor = resp.thread.oldest_cursor
  532. portal.log.debug(
  533. f"{len(new_messages)}/{original_number_of_messages} messages are after most "
  534. "recent message."
  535. )
  536. elif not portal.first_event_id:
  537. self.log.debug(
  538. f"Skipping backfilling {portal.thread_id} as the first event ID is not known"
  539. )
  540. return False
  541. if forward_messages:
  542. portal.cursor = cursor
  543. await portal.update()
  544. mark_read = thread.read_state == 0 or (
  545. (hours := self.config["bridge.backfill.unread_hours_threshold"]) > 0
  546. and (
  547. datetime.fromtimestamp(forward_messages[0].timestamp_ms / 1000)
  548. < datetime.now() - timedelta(hours=hours)
  549. )
  550. )
  551. base_insertion_event_id = await portal.backfill_message_page(
  552. self,
  553. list(reversed(forward_messages)),
  554. forward=True,
  555. last_message=last_message,
  556. mark_read=mark_read,
  557. )
  558. if (
  559. not self.bridge.homeserver_software.is_hungry
  560. and self.config["bridge.backfill.msc2716"]
  561. ):
  562. await portal.send_post_backfill_dummy(
  563. forward_messages[0].timestamp, base_insertion_event_id=base_insertion_event_id
  564. )
  565. if (
  566. mark_read
  567. and not self.bridge.homeserver_software.is_hungry
  568. and (puppet := await self.get_puppet())
  569. ):
  570. last_message = await DBMessage.get_last(portal.mxid)
  571. if last_message:
  572. await puppet.intent_for(portal).mark_read(portal.mxid, last_message.mxid)
  573. await portal._update_read_receipts(thread.last_seen_at)
  574. if self.config["bridge.backfill.msc2716"]:
  575. await portal.enqueue_immediate_backfill(self, 1)
  576. return len(forward_messages) > 0
  577. async def _maybe_update_proxy(self, source: str) -> None:
  578. if not self._listen_task:
  579. self.proxy_handler.update_proxy_url()
  580. await self.on_proxy_update()
  581. else:
  582. self.log.debug(f"Not updating proxy: listen_task is still running? (caller: {source})")
  583. async def sync(self, increment_total_backfilled_portals: bool = False) -> None:
  584. await self.run_with_sync_lock(partial(self._sync, increment_total_backfilled_portals))
  585. async def _sync(self, increment_total_backfilled_portals: bool = False) -> None:
  586. sleep_minutes = 2
  587. errors = 0
  588. while True:
  589. try:
  590. resp = await self.client.get_inbox()
  591. break
  592. except (
  593. ProxyError,
  594. ProxyTimeoutError,
  595. ProxyConnectionError,
  596. ClientConnectionError,
  597. ConnectionError,
  598. asyncio.TimeoutError,
  599. ) as e:
  600. errors += 1
  601. wait = min(errors * 10, 60)
  602. self.log.warning(
  603. f"{e.__class__.__name__} while trying to sync, retrying in {wait} seconds: {e}"
  604. )
  605. await asyncio.sleep(wait)
  606. await self._maybe_update_proxy("sync error")
  607. except IGNotLoggedInError as e:
  608. self.log.exception("Got not logged in error while syncing")
  609. await self.logout(error=e)
  610. return
  611. except IGRateLimitError as e:
  612. self.log.error(
  613. "Got ratelimit error while trying to get inbox (%s), retrying in %d minutes",
  614. e.body,
  615. sleep_minutes,
  616. )
  617. await self.push_bridge_state(
  618. BridgeStateEvent.TRANSIENT_DISCONNECT, error="ig-rate-limit"
  619. )
  620. await asyncio.sleep(sleep_minutes * 60)
  621. sleep_minutes += 2
  622. except IGCheckpointError as e:
  623. self.log.debug("Checkpoint error content: %s", e.body)
  624. raise
  625. except (IGChallengeError, IGConsentRequiredError) as e:
  626. await self._handle_checkpoint(e, on="sync")
  627. return
  628. self.seq_id = resp.seq_id
  629. self.snapshot_at_ms = resp.snapshot_at_ms
  630. await self.save_seq_id()
  631. if not self._listen_task:
  632. self.start_listen(is_after_sync=True)
  633. sync_count = min(
  634. self.config["bridge.backfill.max_conversations"],
  635. self.config["bridge.max_startup_thread_sync_count"],
  636. )
  637. self.log.debug(f"Fetching {sync_count} threads, 20 at a time...")
  638. local_limit: int | None = sync_count
  639. if sync_count == 0:
  640. return
  641. elif sync_count < 0:
  642. local_limit = None
  643. await self._sync_threads_with_delay(
  644. self.client.iter_inbox(
  645. self._update_seq_id_and_cursor, start_at=resp, local_limit=local_limit
  646. ),
  647. stop_when_threads_have_no_messages_to_backfill=True,
  648. increment_total_backfilled_portals=increment_total_backfilled_portals,
  649. local_limit=local_limit,
  650. )
  651. try:
  652. await self.update_direct_chats()
  653. except Exception:
  654. self.log.exception("Error updating direct chat list")
  655. async def backfill_threads(self):
  656. try:
  657. await self.run_with_sync_lock(self._backfill_threads)
  658. except Exception:
  659. self.log.exception("Error in thread backfill loop")
  660. async def _backfill_threads(self):
  661. assert self.client
  662. if not self.config["bridge.backfill.enable"]:
  663. return
  664. max_conversations = self.config["bridge.backfill.max_conversations"] or 0
  665. if 0 <= max_conversations <= (self.total_backfilled_portals or 0):
  666. self.log.info("Backfill max_conversations count reached, not syncing any more portals")
  667. return
  668. elif self.thread_sync_completed:
  669. self.log.debug("Thread backfill is marked as completed, not syncing more portals")
  670. return
  671. local_limit = (
  672. max_conversations - (self.total_backfilled_portals or 0)
  673. if max_conversations >= 0
  674. else None
  675. )
  676. start_at = None
  677. if self.oldest_cursor:
  678. start_at = DMInboxResponse(
  679. status="",
  680. seq_id=self.seq_id,
  681. snapshot_at_ms=0,
  682. pending_requests_total=0,
  683. has_pending_top_requests=False,
  684. viewer=None,
  685. inbox=DMInbox(
  686. threads=[],
  687. has_older=True,
  688. unseen_count=0,
  689. unseen_count_ts=0,
  690. blended_inbox_enabled=False,
  691. oldest_cursor=self.oldest_cursor,
  692. ),
  693. )
  694. backoff = self.config.get("bridge.backfill.backoff.thread_list", 300)
  695. await self._sync_threads_with_delay(
  696. self.client.iter_inbox(
  697. self._update_seq_id_and_cursor,
  698. start_at=start_at,
  699. local_limit=local_limit,
  700. rate_limit_exceeded_backoff=backoff,
  701. ),
  702. increment_total_backfilled_portals=True,
  703. local_limit=local_limit,
  704. )
  705. await self.update_direct_chats()
  706. def _update_seq_id_and_cursor(self, seq_id: int, cursor: str | None):
  707. self.seq_id = seq_id
  708. if cursor:
  709. self.oldest_cursor = cursor
  710. async def _sync_threads_with_delay(
  711. self,
  712. threads: AsyncIterable[Thread],
  713. increment_total_backfilled_portals: bool = False,
  714. stop_when_threads_have_no_messages_to_backfill: bool = False,
  715. local_limit: int | None = None,
  716. ):
  717. sync_delay = self.config["bridge.backfill.min_sync_thread_delay"]
  718. last_thread_sync_ts = 0.0
  719. found_thread_count = 0
  720. async for thread in threads:
  721. found_thread_count += 1
  722. now = time.monotonic()
  723. if now < last_thread_sync_ts + sync_delay:
  724. delay = last_thread_sync_ts + sync_delay - now
  725. self.log.debug("Thread sync is happening too quickly. Waiting for %ds", delay)
  726. await asyncio.sleep(delay)
  727. last_thread_sync_ts = time.monotonic()
  728. had_new_messages = await self._sync_thread(thread)
  729. if not had_new_messages and stop_when_threads_have_no_messages_to_backfill:
  730. self.log.debug("Got to threads with no new messages. Stopping sync.")
  731. return
  732. if increment_total_backfilled_portals:
  733. self.total_backfilled_portals = (self.total_backfilled_portals or 0) + 1
  734. await self.update()
  735. if local_limit is None or found_thread_count < local_limit:
  736. if local_limit is None:
  737. self.log.info(
  738. "Reached end of thread list with no limit, marking thread sync as completed"
  739. )
  740. else:
  741. self.log.info(
  742. f"Reached end of thread list (got {found_thread_count} with "
  743. f"limit {local_limit}), marking thread sync as completed"
  744. )
  745. self.thread_sync_completed = True
  746. await self.update()
  747. async def run_with_sync_lock(self, func: Callable[[], Awaitable]):
  748. with self._sync_lock:
  749. retry_count = 0
  750. while retry_count < 5:
  751. try:
  752. retry_count += 1
  753. await func()
  754. # The sync was successful. Exit the loop.
  755. return
  756. except IGNotLoggedInError as e:
  757. await self.logout(error=e)
  758. return
  759. except Exception:
  760. self.log.exception(
  761. "Failed to sync threads. Waiting 30 seconds before retrying sync."
  762. )
  763. await asyncio.sleep(30)
  764. # If we get here, it means that the sync has failed five times. If this happens, most
  765. # likely something very bad has happened.
  766. self.log.error("Failed to sync threads five times. Will not retry.")
  767. def start_listen(self, is_after_sync: bool = False) -> None:
  768. self.shutdown = False
  769. task = self._listen(
  770. seq_id=self.seq_id, snapshot_at_ms=self.snapshot_at_ms, is_after_sync=is_after_sync
  771. )
  772. self._listen_task = self.loop.create_task(task)
  773. async def fetch_user_and_reconnect(self) -> None:
  774. self.log.debug("Refetching current user after disconnection")
  775. errors = 0
  776. while True:
  777. try:
  778. resp = await self.client.current_user()
  779. except (
  780. ProxyError,
  781. ProxyTimeoutError,
  782. ProxyConnectionError,
  783. ClientConnectionError,
  784. ConnectionError,
  785. asyncio.TimeoutError,
  786. ) as e:
  787. errors += 1
  788. wait = min(errors * 10, 60)
  789. self.log.warning(
  790. f"{e.__class__.__name__} while trying to check user for reconnection, "
  791. f"retrying in {wait} seconds: {e}"
  792. )
  793. await asyncio.sleep(wait)
  794. await self._maybe_update_proxy("fetch_user_and_reconnect error")
  795. except IGNotLoggedInError as e:
  796. self.log.warning(f"Failed to reconnect to Instagram: {e}, logging out")
  797. await self.logout(error=e)
  798. return
  799. except (IGChallengeError, IGConsentRequiredError) as e:
  800. await self._handle_checkpoint(e, on="reconnect")
  801. return
  802. except Exception as e:
  803. self.log.exception("Error while reconnecting to Instagram")
  804. if isinstance(e, IGCheckpointError):
  805. self.log.debug("Checkpoint error content: %s", e.body)
  806. await self.push_bridge_state(
  807. BridgeStateEvent.UNKNOWN_ERROR, info={"python_error": str(e)}
  808. )
  809. return
  810. else:
  811. self.log.debug(f"Confirmed current user {resp.user.pk}")
  812. self.start_listen()
  813. return
  814. async def _listen(self, seq_id: int, snapshot_at_ms: int, is_after_sync: bool) -> None:
  815. try:
  816. await self.mqtt.listen(
  817. graphql_subs={
  818. GraphQLSubscription.app_presence(),
  819. GraphQLSubscription.direct_typing(self.state.user_id),
  820. GraphQLSubscription.direct_status(),
  821. },
  822. skywalker_subs={
  823. SkywalkerSubscription.direct_sub(self.state.user_id),
  824. SkywalkerSubscription.live_sub(self.state.user_id),
  825. },
  826. seq_id=seq_id,
  827. snapshot_at_ms=snapshot_at_ms,
  828. )
  829. except IrisSubscribeError as e:
  830. if is_after_sync:
  831. self.log.exception("Got IrisSubscribeError right after refresh")
  832. await self.send_bridge_notice(
  833. f"Reconnecting failed again after refresh: {e}",
  834. important=True,
  835. state_event=BridgeStateEvent.UNKNOWN_ERROR,
  836. error_code="ig-refresh-connection-error",
  837. error_message=str(e),
  838. info={"python_error": str(e)},
  839. )
  840. else:
  841. self.log.warning(f"Got IrisSubscribeError {e}, refreshing...")
  842. asyncio.create_task(self.refresh())
  843. except (MQTTNotConnected, MQTTNotLoggedIn, MQTTConnectionUnauthorized) as e:
  844. self.log.warning(
  845. f"Unexpected connection error: {e}", exc_info="MQTT reconnection failed" in str(e)
  846. )
  847. await self.send_bridge_notice(
  848. f"Error in listener: {e}",
  849. important=True,
  850. state_event=BridgeStateEvent.UNKNOWN_ERROR,
  851. error_code="ig-connection-error",
  852. )
  853. self.mqtt.disconnect()
  854. asyncio.create_task(self.fetch_user_and_reconnect())
  855. except Exception as e:
  856. self.log.exception("Fatal error in listener")
  857. await self.send_bridge_notice(
  858. "Fatal error in listener (see logs for more info)",
  859. state_event=BridgeStateEvent.UNKNOWN_ERROR,
  860. important=True,
  861. error_code="ig-unknown-connection-error",
  862. info={"python_error": str(e)},
  863. )
  864. self.mqtt.disconnect()
  865. else:
  866. if not self.shutdown:
  867. await self.send_bridge_notice(
  868. "Instagram connection closed without error",
  869. state_event=BridgeStateEvent.UNKNOWN_ERROR,
  870. error_code="ig-disconnected",
  871. )
  872. finally:
  873. self._listen_task = None
  874. self._is_connected = False
  875. self._track_metric(METRIC_CONNECTED, False)
  876. async def stop_listen(self) -> None:
  877. if self.mqtt:
  878. self.shutdown = True
  879. self.mqtt.disconnect()
  880. if self._listen_task:
  881. await self._listen_task
  882. self.shutdown = False
  883. self._track_metric(METRIC_CONNECTED, False)
  884. self._is_connected = False
  885. await self.update()
  886. def stop_backfill_tasks(self) -> None:
  887. if self._backfill_loop_task:
  888. self._backfill_loop_task.cancel()
  889. self._backfill_loop_task = None
  890. if self._thread_sync_task:
  891. self._thread_sync_task.cancel()
  892. self._thread_sync_task = None
  893. async def logout(self, error: IGNotLoggedInError | None = None) -> None:
  894. await self.stop_listen()
  895. self.stop_backfill_tasks()
  896. if self.client and error is None:
  897. try:
  898. await self.client.logout(one_tap_app_login=False)
  899. except Exception:
  900. self.log.debug("Exception logging out", exc_info=True)
  901. if self.mqtt:
  902. self.mqtt.disconnect()
  903. self._track_metric(METRIC_CONNECTED, False)
  904. self._track_metric(METRIC_LOGGED_IN, False)
  905. if error is None:
  906. await self.push_bridge_state(BridgeStateEvent.LOGGED_OUT)
  907. puppet = await pu.Puppet.get_by_pk(self.igpk, create=False)
  908. if puppet and puppet.is_real_user:
  909. await puppet.switch_mxid(None, None)
  910. try:
  911. del self.by_igpk[self.igpk]
  912. except KeyError:
  913. pass
  914. self.igpk = None
  915. else:
  916. self.log.debug("Auth error body: %s", error.body.serialize())
  917. await self.send_bridge_notice(
  918. f"You have been logged out of Instagram: {error.proper_message}",
  919. important=True,
  920. state_event=BridgeStateEvent.BAD_CREDENTIALS,
  921. error_code="ig-auth-error",
  922. error_message=error.proper_message,
  923. )
  924. self.client = None
  925. self.mqtt = None
  926. self.state = None
  927. self.seq_id = None
  928. self.snapshot_at_ms = None
  929. self.thread_sync_completed = False
  930. self._is_logged_in = False
  931. await self.update()
  932. # endregion
  933. # region Event handlers
  934. async def _save_seq_id_after_sleep(self) -> None:
  935. await asyncio.sleep(120)
  936. self._seq_id_save_task = None
  937. self.log.trace("Saving sequence ID %d/%d", self.seq_id, self.snapshot_at_ms)
  938. try:
  939. await self.save_seq_id()
  940. except Exception:
  941. self.log.exception("Error saving sequence ID")
  942. async def update_seq_id(self, evt: NewSequenceID) -> None:
  943. self.seq_id = evt.seq_id
  944. self.snapshot_at_ms = evt.snapshot_at_ms
  945. if not self._seq_id_save_task or self._seq_id_save_task.done():
  946. self.log.trace("Starting seq id save task (%d/%d)", evt.seq_id, evt.snapshot_at_ms)
  947. self._seq_id_save_task = asyncio.create_task(self._save_seq_id_after_sleep())
  948. else:
  949. self.log.trace("Not starting seq id save task (%d/%d)", evt.seq_id, evt.snapshot_at_ms)
  950. @async_time(METRIC_MESSAGE)
  951. async def handle_message(self, evt: MessageSyncEvent) -> None:
  952. portal = await po.Portal.get_by_thread_id(evt.message.thread_id, receiver=self.igpk)
  953. if not portal or not portal.mxid:
  954. self.log.debug("Got message in thread with no portal, getting info...")
  955. resp = await self.client.get_thread(evt.message.thread_id)
  956. portal = await po.Portal.get_by_thread(resp.thread, self.igpk)
  957. self.log.debug("Got info for unknown portal, creating room")
  958. await portal.create_matrix_room(self, resp.thread)
  959. if not portal.mxid:
  960. self.log.warning(
  961. "Room creation appears to have failed, "
  962. f"dropping message in {evt.message.thread_id}"
  963. )
  964. return
  965. self.log.trace(f"Received message sync event {evt.message}")
  966. if evt.message.new_reaction:
  967. await portal.handle_instagram_reaction(
  968. evt.message, remove=evt.message.op == Operation.REMOVE
  969. )
  970. return
  971. sender = await pu.Puppet.get_by_pk(evt.message.user_id) if evt.message.user_id else None
  972. if evt.message.op == Operation.ADD:
  973. if not sender:
  974. # I don't think we care about adds with no sender
  975. return
  976. await portal.handle_instagram_item(self, sender, evt.message)
  977. elif evt.message.op == Operation.REMOVE:
  978. # Removes don't have a sender, only the message sender can unsend messages anyway
  979. await portal.handle_instagram_remove(evt.message.item_id)
  980. elif evt.message.op == Operation.REPLACE:
  981. await portal.handle_instagram_update(evt.message)
  982. @async_time(METRIC_THREAD_SYNC)
  983. async def handle_thread_sync(self, evt: ThreadSyncEvent) -> None:
  984. self.log.trace("Thread sync event content: %s", evt)
  985. portal = await po.Portal.get_by_thread(evt, receiver=self.igpk)
  986. if portal.mxid:
  987. self.log.debug("Got thread sync event for %s with existing portal", portal.thread_id)
  988. await portal.update_matrix_room(self, evt)
  989. elif evt.is_group:
  990. self.log.debug(
  991. "Got thread sync event for group %s without existing portal, creating room",
  992. portal.thread_id,
  993. )
  994. await portal.create_matrix_room(self, evt)
  995. else:
  996. self.log.debug(
  997. "Got thread sync event for DM %s without existing portal, ignoring",
  998. portal.thread_id,
  999. )
  1000. async def handle_thread_remove(self, evt: ThreadRemoveEvent) -> None:
  1001. self.log.debug("Got thread remove event: %s", evt.serialize())
  1002. @async_time(METRIC_RTD)
  1003. async def handle_rtd(self, evt: RealtimeDirectEvent) -> None:
  1004. if not isinstance(evt.value, ActivityIndicatorData):
  1005. return
  1006. now = int(time.time() * 1000)
  1007. date = evt.value.timestamp_ms
  1008. expiry = date + evt.value.ttl
  1009. if expiry < now:
  1010. return
  1011. if evt.activity_indicator_id in self._activity_indicator_ids:
  1012. return
  1013. # TODO clear expired items from this dict
  1014. self._activity_indicator_ids[evt.activity_indicator_id] = expiry
  1015. puppet = await pu.Puppet.get_by_pk(int(evt.value.sender_id))
  1016. portal = await po.Portal.get_by_thread_id(evt.thread_id, receiver=self.igpk)
  1017. if not puppet or not portal or not portal.mxid:
  1018. return
  1019. is_typing = evt.value.activity_status != TypingStatus.OFF
  1020. if puppet.pk == self.igpk:
  1021. self.remote_typing_status = TypingStatus.TEXT if is_typing else TypingStatus.OFF
  1022. await puppet.intent_for(portal).set_typing(
  1023. portal.mxid, is_typing=is_typing, timeout=evt.value.ttl
  1024. )
  1025. # endregion
  1026. # region Database getters
  1027. def _add_to_cache(self) -> None:
  1028. self.by_mxid[self.mxid] = self
  1029. if self.igpk:
  1030. self.by_igpk[self.igpk] = self
  1031. @classmethod
  1032. @async_getter_lock
  1033. async def get_by_mxid(cls, mxid: UserID, *, create: bool = True) -> User | None:
  1034. # Never allow ghosts to be users
  1035. if pu.Puppet.get_id_from_mxid(mxid):
  1036. return None
  1037. try:
  1038. return cls.by_mxid[mxid]
  1039. except KeyError:
  1040. pass
  1041. user = cast(cls, await super().get_by_mxid(mxid))
  1042. if user is not None:
  1043. user._add_to_cache()
  1044. return user
  1045. if create:
  1046. user = cls(mxid)
  1047. await user.insert()
  1048. user._add_to_cache()
  1049. return user
  1050. return None
  1051. @classmethod
  1052. @async_getter_lock
  1053. async def get_by_igpk(cls, igpk: int) -> User | None:
  1054. try:
  1055. return cls.by_igpk[igpk]
  1056. except KeyError:
  1057. pass
  1058. user = cast(cls, await super().get_by_igpk(igpk))
  1059. if user is not None:
  1060. user._add_to_cache()
  1061. return user
  1062. return None
  1063. @classmethod
  1064. async def all_logged_in(cls) -> AsyncGenerator[User, None]:
  1065. users = await super().all_logged_in()
  1066. user: cls
  1067. for index, user in enumerate(users):
  1068. try:
  1069. yield cls.by_mxid[user.mxid]
  1070. except KeyError:
  1071. user._add_to_cache()
  1072. yield user
  1073. # endregion