subscription.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 __future__ import annotations
  17. from typing import Any
  18. from enum import Enum
  19. from uuid import uuid4
  20. import json
  21. class SkywalkerSubscription:
  22. @staticmethod
  23. def direct_sub(user_id: str | int) -> str:
  24. return f"ig/u/v1/{user_id}"
  25. @staticmethod
  26. def live_sub(user_id: str | int) -> str:
  27. return f"ig/live_notification_subscribe/{user_id}"
  28. class GraphQLQueryID(Enum):
  29. APP_PRESENCE = "17846944882223835"
  30. ASYNC_AD_SUB = "17911191835112000"
  31. CLIENT_CONFIG_UPDATE = "17849856529644700"
  32. DIRECT_STATUS = "17854499065530643"
  33. DIRECT_TYPING = "17867973967082385"
  34. LIVE_WAVE = "17882305414154951"
  35. INTERACTIVITY_ACTIVATE_QUESTION = "18005526940184517"
  36. INTERACTIVITY_REALTIME_QUESTION_SUBMISSION_STATUS = "18027779584026952"
  37. INTERACTIVITY_SUB = "17907616480241689"
  38. LIVE_REALTIME_COMMENTS = "17855344750227125"
  39. LIVE_TYPING_INDICATOR = "17926314067024917"
  40. MEDIA_FEEDBACK = "17877917527113814"
  41. REACT_NATIVE_OTA = "17861494672288167"
  42. VIDEO_CALL_CO_WATCH_CONTROL = "17878679623388956"
  43. VIDEO_CALL_IN_ALERT = "17878679623388956"
  44. VIDEO_CALL_PROTOTYPE_PUBLISH = "18031704190010162"
  45. VIDEO_CALL_PARTICIPANT_DELIVERY = "17977239895057311"
  46. ZERO_PROVISION = "17913953740109069"
  47. INAPP_NOTIFICATION = "17899377895239777"
  48. BUSINESS_DELIVERY = "17940467278199720"
  49. everclear_subscriptions = {
  50. "async_ads_subscribe": GraphQLQueryID.ASYNC_AD_SUB.value,
  51. "inapp_notification_subscribe_default": GraphQLQueryID.INAPP_NOTIFICATION.value,
  52. "inapp_notification_subscribe_comment": GraphQLQueryID.INAPP_NOTIFICATION.value,
  53. "inapp_notification_subscribe_comment_mention_and_reply": GraphQLQueryID.INAPP_NOTIFICATION.value,
  54. "business_import_page_media_delivery_subscribe": GraphQLQueryID.BUSINESS_DELIVERY.value,
  55. "video_call_participant_state_delivery": GraphQLQueryID.VIDEO_CALL_PARTICIPANT_DELIVERY.value,
  56. }
  57. class GraphQLSubscription:
  58. @staticmethod
  59. def _fmt(
  60. query_id: GraphQLQueryID, input_params: Any, client_logged: bool | None = None
  61. ) -> str:
  62. params = {
  63. "input_data": input_params,
  64. **(
  65. {"%options": {"client_logged": client_logged}} if client_logged is not None else {}
  66. ),
  67. }
  68. return f"1/graphqlsubscriptions/{query_id.value}/{json.dumps(params)}"
  69. @classmethod
  70. def app_presence(
  71. cls, subscription_id: str | None = None, client_logged: bool | None = None
  72. ) -> str:
  73. return cls._fmt(
  74. GraphQLQueryID.APP_PRESENCE,
  75. input_params={"client_subscription_id": subscription_id or str(uuid4())},
  76. client_logged=client_logged,
  77. )
  78. @classmethod
  79. def async_ad(
  80. cls,
  81. user_id: str,
  82. subscription_id: str | None = None,
  83. client_logged: bool | None = None,
  84. ) -> str:
  85. return cls._fmt(
  86. GraphQLQueryID.ASYNC_AD_SUB,
  87. input_params={
  88. "client_subscription_id": subscription_id or str(uuid4()),
  89. "user_id": user_id,
  90. },
  91. client_logged=client_logged,
  92. )
  93. @classmethod
  94. def client_config_update(
  95. cls, subscription_id: str | None = None, client_logged: bool | None = None
  96. ) -> str:
  97. return cls._fmt(
  98. GraphQLQueryID.CLIENT_CONFIG_UPDATE,
  99. input_params={"client_subscription_id": subscription_id or str(uuid4())},
  100. client_logged=client_logged,
  101. )
  102. @classmethod
  103. def direct_status(
  104. cls, subscription_id: str | None = None, client_logged: bool | None = None
  105. ) -> str:
  106. return cls._fmt(
  107. GraphQLQueryID.DIRECT_STATUS,
  108. input_params={"client_subscription_id": subscription_id or str(uuid4())},
  109. client_logged=client_logged,
  110. )
  111. @classmethod
  112. def direct_typing(cls, user_id: str, client_logged: bool | None = None) -> str:
  113. return cls._fmt(
  114. GraphQLQueryID.DIRECT_TYPING,
  115. input_params={"user_id": user_id},
  116. client_logged=client_logged,
  117. )
  118. @classmethod
  119. def ig_live_wave(
  120. cls,
  121. broadcast_id: str,
  122. receiver_id: str,
  123. subscription_id: str | None = None,
  124. client_logged: bool | None = None,
  125. ) -> str:
  126. return cls._fmt(
  127. GraphQLQueryID.LIVE_WAVE,
  128. input_params={
  129. "client_subscription_id": subscription_id or str(uuid4()),
  130. "broadcast_id": broadcast_id,
  131. "receiver_id": receiver_id,
  132. },
  133. client_logged=client_logged,
  134. )
  135. @classmethod
  136. def interactivity_activate_question(
  137. cls,
  138. broadcast_id: str,
  139. subscription_id: str | None = None,
  140. client_logged: bool | None = None,
  141. ) -> str:
  142. return cls._fmt(
  143. GraphQLQueryID.INTERACTIVITY_ACTIVATE_QUESTION,
  144. input_params={
  145. "client_subscription_id": subscription_id or str(uuid4()),
  146. "broadcast_id": broadcast_id,
  147. },
  148. client_logged=client_logged,
  149. )
  150. @classmethod
  151. def interactivity_realtime_question_submissions_status(
  152. cls,
  153. broadcast_id: str,
  154. subscription_id: str | None = None,
  155. client_logged: bool | None = None,
  156. ) -> str:
  157. return cls._fmt(
  158. GraphQLQueryID.INTERACTIVITY_REALTIME_QUESTION_SUBMISSION_STATUS,
  159. input_params={
  160. "client_subscription_id": subscription_id or str(uuid4()),
  161. "broadcast_id": broadcast_id,
  162. },
  163. client_logged=client_logged,
  164. )
  165. @classmethod
  166. def interactivity(
  167. cls,
  168. broadcast_id: str,
  169. subscription_id: str | None = None,
  170. client_logged: bool | None = None,
  171. ) -> str:
  172. return cls._fmt(
  173. GraphQLQueryID.INTERACTIVITY_SUB,
  174. input_params={
  175. "client_subscription_id": subscription_id or str(uuid4()),
  176. "broadcast_id": broadcast_id,
  177. },
  178. client_logged=client_logged,
  179. )
  180. @classmethod
  181. def live_realtime_comments(
  182. cls,
  183. broadcast_id: str,
  184. subscription_id: str | None = None,
  185. client_logged: bool | None = None,
  186. ) -> str:
  187. return cls._fmt(
  188. GraphQLQueryID.LIVE_REALTIME_COMMENTS,
  189. input_params={
  190. "client_subscription_id": subscription_id or str(uuid4()),
  191. "broadcast_id": broadcast_id,
  192. },
  193. client_logged=client_logged,
  194. )
  195. @classmethod
  196. def live_realtime_typing_indicator(
  197. cls,
  198. broadcast_id: str,
  199. subscription_id: str | None = None,
  200. client_logged: bool | None = None,
  201. ) -> str:
  202. return cls._fmt(
  203. GraphQLQueryID.LIVE_TYPING_INDICATOR,
  204. input_params={
  205. "client_subscription_id": subscription_id or str(uuid4()),
  206. "broadcast_id": broadcast_id,
  207. },
  208. client_logged=client_logged,
  209. )
  210. @classmethod
  211. def media_feedback(
  212. cls,
  213. feedback_id: str,
  214. subscription_id: str | None = None,
  215. client_logged: bool | None = None,
  216. ) -> str:
  217. return cls._fmt(
  218. GraphQLQueryID.MEDIA_FEEDBACK,
  219. input_params={
  220. "client_subscription_id": subscription_id or str(uuid4()),
  221. "feedback_id": feedback_id,
  222. },
  223. client_logged=client_logged,
  224. )
  225. @classmethod
  226. def react_native_ota_update(
  227. cls,
  228. build_number: str,
  229. subscription_id: str | None = None,
  230. client_logged: bool | None = None,
  231. ) -> str:
  232. return cls._fmt(
  233. GraphQLQueryID.REACT_NATIVE_OTA,
  234. input_params={
  235. "client_subscription_id": subscription_id or str(uuid4()),
  236. "build_number": build_number,
  237. },
  238. client_logged=client_logged,
  239. )
  240. @classmethod
  241. def video_call_co_watch_control(
  242. cls,
  243. video_call_id: str,
  244. subscription_id: str | None = None,
  245. client_logged: bool | None = None,
  246. ) -> str:
  247. return cls._fmt(
  248. GraphQLQueryID.VIDEO_CALL_CO_WATCH_CONTROL,
  249. input_params={
  250. "client_subscription_id": subscription_id or str(uuid4()),
  251. "video_call_id": video_call_id,
  252. },
  253. client_logged=client_logged,
  254. )
  255. @classmethod
  256. def video_call_in_call_alert(
  257. cls,
  258. video_call_id: str,
  259. subscription_id: str | None = None,
  260. client_logged: bool | None = None,
  261. ) -> str:
  262. return cls._fmt(
  263. GraphQLQueryID.VIDEO_CALL_IN_ALERT,
  264. input_params={
  265. "client_subscription_id": subscription_id or str(uuid4()),
  266. "video_call_id": video_call_id,
  267. },
  268. client_logged=client_logged,
  269. )
  270. @classmethod
  271. def video_call_prototype_publish(
  272. cls,
  273. video_call_id: str,
  274. subscription_id: str | None = None,
  275. client_logged: bool | None = None,
  276. ) -> str:
  277. return cls._fmt(
  278. GraphQLQueryID.VIDEO_CALL_PROTOTYPE_PUBLISH,
  279. input_params={
  280. "client_subscription_id": subscription_id or str(uuid4()),
  281. "video_call_id": video_call_id,
  282. },
  283. client_logged=client_logged,
  284. )
  285. @classmethod
  286. def zero_provision(
  287. cls,
  288. device_id: str,
  289. subscription_id: str | None = None,
  290. client_logged: bool | None = None,
  291. ) -> str:
  292. return cls._fmt(
  293. GraphQLQueryID.ZERO_PROVISION,
  294. input_params={
  295. "client_subscription_id": subscription_id or str(uuid4()),
  296. "device_id": device_id,
  297. },
  298. client_logged=client_logged,
  299. )
  300. _topic_map: dict[str, str] = {
  301. "/pp": "34", # unknown
  302. "/ig_sub_iris": "134",
  303. "/ig_sub_iris_response": "135",
  304. "/ig_message_sync": "146",
  305. "/ig_send_message": "132",
  306. "/ig_send_message_response": "133",
  307. "/ig_realtime_sub": "149",
  308. "/pubsub": "88",
  309. "/t_fs": "102", # Foreground state
  310. "/graphql": "9",
  311. "/t_region_hint": "150",
  312. "/mqtt_health_stats": "/mqtt_health_stats",
  313. "179": "179", # also unknown
  314. }
  315. _reverse_topic_map: dict[str, str] = {value: key for key, value in _topic_map.items()}
  316. class RealtimeTopic(Enum):
  317. SUB_IRIS = "/ig_sub_iris"
  318. SUB_IRIS_RESPONSE = "/ig_sub_iris_response"
  319. MESSAGE_SYNC = "/ig_message_sync"
  320. SEND_MESSAGE = "/ig_send_message"
  321. SEND_MESSAGE_RESPONSE = "/ig_send_message_response"
  322. REALTIME_SUB = "/ig_realtime_sub"
  323. PUBSUB = "/pubsub"
  324. FOREGROUND_STATE = "/t_fs"
  325. GRAPHQL = "/graphql"
  326. REGION_HINT = "/t_region_hint"
  327. MQTT_HEALTH_STATS = "/mqtt_health_stats"
  328. UNKNOWN_PP = "/pp"
  329. UNKNOWN_179 = "179"
  330. @property
  331. def encoded(self) -> str:
  332. return _topic_map[self.value]
  333. @staticmethod
  334. def decode(val: str) -> RealtimeTopic:
  335. return RealtimeTopic(_reverse_topic_map[val])