dbfunctions.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. """
  2. MIT License
  3. Copyright (c) 2021 TheHamkerCat
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import codecs
  21. import pickle
  22. from typing import Dict, List, Union
  23. from wbb import db
  24. """
  25. SOME THINGS ARE FUCKED UP HERE, LIKE TOGGLEABLES HAVE THEIR OWN COLLECTION
  26. (SHOULD FIX IT WITH SOMETHING LIKE TOGGLEDB, BUT I WON'T, AS IT WILL TAKE
  27. TOO MUCH TIME AND WILL BE BAD FOR ALREADY STORED DATA)
  28. """
  29. notesdb = db.notes
  30. filtersdb = db.filters
  31. warnsdb = db.warns
  32. karmadb = db.karma
  33. chatsdb = db.chats
  34. usersdb = db.users
  35. gbansdb = db.gban
  36. coupledb = db.couple
  37. captchadb = db.captcha
  38. antiservicedb = db.antiservice
  39. pmpermitdb = db.pmpermit
  40. welcomedb = db.welcome_text
  41. captcha_cachedb = db.captcha_cache
  42. blacklist_filtersdb = db.blacklistFilters
  43. pipesdb = db.pipes
  44. sudoersdb = db.sudoers
  45. blacklist_chatdb = db.blacklistChat
  46. restart_stagedb = db.restart_stage
  47. flood_toggle_db = db.flood_toggle
  48. rssdb = db.rss
  49. def obj_to_str(obj):
  50. if not obj:
  51. return False
  52. string = codecs.encode(pickle.dumps(obj), "base64").decode()
  53. return string
  54. def str_to_obj(string: str):
  55. obj = pickle.loads(codecs.decode(string.encode(), "base64"))
  56. return obj
  57. async def get_notes_count() -> dict:
  58. chats = notesdb.find({"chat_id": {"$lt": 0}})
  59. if not chats:
  60. return {}
  61. chats_count = 0
  62. notes_count = 0
  63. for chat in await chats.to_list(length=1000000000):
  64. notes_name = await get_note_names(chat["chat_id"])
  65. notes_count += len(notes_name)
  66. chats_count += 1
  67. return {"chats_count": chats_count, "notes_count": notes_count}
  68. async def _get_notes(chat_id: int) -> Dict[str, int]:
  69. _notes = await notesdb.find_one({"chat_id": chat_id})
  70. if not _notes:
  71. return {}
  72. return _notes["notes"]
  73. async def get_note_names(chat_id: int) -> List[str]:
  74. _notes = []
  75. for note in await _get_notes(chat_id):
  76. _notes.append(note)
  77. return _notes
  78. async def get_note(chat_id: int, name: str) -> Union[bool, dict]:
  79. name = name.lower().strip()
  80. _notes = await _get_notes(chat_id)
  81. if name in _notes:
  82. return _notes[name]
  83. return False
  84. async def save_note(chat_id: int, name: str, note: dict):
  85. name = name.lower().strip()
  86. _notes = await _get_notes(chat_id)
  87. _notes[name] = note
  88. await notesdb.update_one(
  89. {"chat_id": chat_id}, {"$set": {"notes": _notes}}, upsert=True
  90. )
  91. async def delete_note(chat_id: int, name: str) -> bool:
  92. notesd = await _get_notes(chat_id)
  93. name = name.lower().strip()
  94. if name in notesd:
  95. del notesd[name]
  96. await notesdb.update_one(
  97. {"chat_id": chat_id},
  98. {"$set": {"notes": notesd}},
  99. upsert=True,
  100. )
  101. return True
  102. return False
  103. async def get_filters_count() -> dict:
  104. chats = filtersdb.find({"chat_id": {"$lt": 0}})
  105. if not chats:
  106. return {}
  107. chats_count = 0
  108. filters_count = 0
  109. for chat in await chats.to_list(length=1000000000):
  110. filters_name = await get_filters_names(chat["chat_id"])
  111. filters_count += len(filters_name)
  112. chats_count += 1
  113. return {
  114. "chats_count": chats_count,
  115. "filters_count": filters_count,
  116. }
  117. async def _get_filters(chat_id: int) -> Dict[str, int]:
  118. _filters = await filtersdb.find_one({"chat_id": chat_id})
  119. if not _filters:
  120. return {}
  121. return _filters["filters"]
  122. async def get_filters_names(chat_id: int) -> List[str]:
  123. _filters = []
  124. for _filter in await _get_filters(chat_id):
  125. _filters.append(_filter)
  126. return _filters
  127. async def get_filter(chat_id: int, name: str) -> Union[bool, dict]:
  128. name = name.lower().strip()
  129. _filters = await _get_filters(chat_id)
  130. if name in _filters:
  131. return _filters[name]
  132. return False
  133. async def save_filter(chat_id: int, name: str, _filter: dict):
  134. name = name.lower().strip()
  135. _filters = await _get_filters(chat_id)
  136. _filters[name] = _filter
  137. await filtersdb.update_one(
  138. {"chat_id": chat_id},
  139. {"$set": {"filters": _filters}},
  140. upsert=True,
  141. )
  142. async def delete_filter(chat_id: int, name: str) -> bool:
  143. filtersd = await _get_filters(chat_id)
  144. name = name.lower().strip()
  145. if name in filtersd:
  146. del filtersd[name]
  147. await filtersdb.update_one(
  148. {"chat_id": chat_id},
  149. {"$set": {"filters": filtersd}},
  150. upsert=True,
  151. )
  152. return True
  153. return False
  154. async def int_to_alpha(user_id: int) -> str:
  155. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  156. text = ""
  157. user_id = str(user_id)
  158. for i in user_id:
  159. text += alphabet[int(i)]
  160. return text
  161. async def alpha_to_int(user_id_alphabet: str) -> int:
  162. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  163. user_id = ""
  164. for i in user_id_alphabet:
  165. index = alphabet.index(i)
  166. user_id += str(index)
  167. user_id = int(user_id)
  168. return user_id
  169. async def get_warns_count() -> dict:
  170. chats = warnsdb.find({"chat_id": {"$lt": 0}})
  171. if not chats:
  172. return {}
  173. chats_count = 0
  174. warns_count = 0
  175. for chat in await chats.to_list(length=100000000):
  176. for user in chat["warns"]:
  177. warns_count += chat["warns"][user]["warns"]
  178. chats_count += 1
  179. return {"chats_count": chats_count, "warns_count": warns_count}
  180. async def get_warns(chat_id: int) -> Dict[str, int]:
  181. warns = await warnsdb.find_one({"chat_id": chat_id})
  182. if not warns:
  183. return {}
  184. return warns["warns"]
  185. async def get_warn(chat_id: int, name: str) -> Union[bool, dict]:
  186. name = name.lower().strip()
  187. warns = await get_warns(chat_id)
  188. if name in warns:
  189. return warns[name]
  190. async def add_warn(chat_id: int, name: str, warn: dict):
  191. name = name.lower().strip()
  192. warns = await get_warns(chat_id)
  193. warns[name] = warn
  194. await warnsdb.update_one(
  195. {"chat_id": chat_id}, {"$set": {"warns": warns}}, upsert=True
  196. )
  197. async def remove_warns(chat_id: int, name: str) -> bool:
  198. warnsd = await get_warns(chat_id)
  199. name = name.lower().strip()
  200. if name in warnsd:
  201. del warnsd[name]
  202. await warnsdb.update_one(
  203. {"chat_id": chat_id},
  204. {"$set": {"warns": warnsd}},
  205. upsert=True,
  206. )
  207. return True
  208. return False
  209. async def get_karmas_count() -> dict:
  210. chats = karmadb.find({"chat_id": {"$lt": 0}})
  211. if not chats:
  212. return {}
  213. chats_count = 0
  214. karmas_count = 0
  215. for chat in await chats.to_list(length=1000000):
  216. for i in chat["karma"]:
  217. karma_ = chat["karma"][i]["karma"]
  218. if karma_ > 0:
  219. karmas_count += karma_
  220. chats_count += 1
  221. return {"chats_count": chats_count, "karmas_count": karmas_count}
  222. async def user_global_karma(user_id) -> int:
  223. chats = karmadb.find({"chat_id": {"$lt": 0}})
  224. if not chats:
  225. return 0
  226. total_karma = 0
  227. for chat in await chats.to_list(length=1000000):
  228. karma = await get_karma(
  229. chat["chat_id"], await int_to_alpha(user_id)
  230. )
  231. if karma and (int(karma["karma"]) > 0):
  232. total_karma += int(karma["karma"])
  233. return total_karma
  234. async def get_karmas(chat_id: int) -> Dict[str, int]:
  235. karma = await karmadb.find_one({"chat_id": chat_id})
  236. if not karma:
  237. return {}
  238. return karma["karma"]
  239. async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
  240. name = name.lower().strip()
  241. karmas = await get_karmas(chat_id)
  242. if name in karmas:
  243. return karmas[name]
  244. async def update_karma(chat_id: int, name: str, karma: dict):
  245. name = name.lower().strip()
  246. karmas = await get_karmas(chat_id)
  247. karmas[name] = karma
  248. await karmadb.update_one(
  249. {"chat_id": chat_id}, {"$set": {"karma": karmas}}, upsert=True
  250. )
  251. async def is_karma_on(chat_id: int) -> bool:
  252. chat = await karmadb.find_one({"chat_id_toggle": chat_id})
  253. if not chat:
  254. return True
  255. return False
  256. async def karma_on(chat_id: int):
  257. is_karma = await is_karma_on(chat_id)
  258. if is_karma:
  259. return
  260. return await karmadb.delete_one({"chat_id_toggle": chat_id})
  261. async def karma_off(chat_id: int):
  262. is_karma = await is_karma_on(chat_id)
  263. if not is_karma:
  264. return
  265. return await karmadb.insert_one({"chat_id_toggle": chat_id})
  266. async def is_served_chat(chat_id: int) -> bool:
  267. chat = await chatsdb.find_one({"chat_id": chat_id})
  268. if not chat:
  269. return False
  270. return True
  271. async def get_served_chats() -> list:
  272. chats = chatsdb.find({"chat_id": {"$lt": 0}})
  273. if not chats:
  274. return []
  275. chats_list = []
  276. for chat in await chats.to_list(length=1000000000):
  277. chats_list.append(chat)
  278. return chats_list
  279. async def add_served_chat(chat_id: int):
  280. is_served = await is_served_chat(chat_id)
  281. if is_served:
  282. return
  283. return await chatsdb.insert_one({"chat_id": chat_id})
  284. async def remove_served_chat(chat_id: int):
  285. is_served = await is_served_chat(chat_id)
  286. if not is_served:
  287. return
  288. return await chatsdb.delete_one({"chat_id": chat_id})
  289. async def is_served_user(user_id: int) -> bool:
  290. user = await usersdb.find_one({"user_id": user_id})
  291. if not user:
  292. return False
  293. return True
  294. async def get_served_users() -> list:
  295. users = usersdb.find({"user_id": {"$gt": 0}})
  296. if not users:
  297. return []
  298. users_list = []
  299. for user in await users.to_list(length=1000000000):
  300. users_list.append(user)
  301. return users_list
  302. async def add_served_user(user_id: int):
  303. is_served = await is_served_user(user_id)
  304. if is_served:
  305. return
  306. return await usersdb.insert_one({"user_id": user_id})
  307. async def get_gbans_count() -> int:
  308. users = gbansdb.find({"user_id": {"$gt": 0}})
  309. users = await users.to_list(length=100000)
  310. return len(users)
  311. async def is_gbanned_user(user_id: int) -> bool:
  312. user = await gbansdb.find_one({"user_id": user_id})
  313. if not user:
  314. return False
  315. return True
  316. async def add_gban_user(user_id: int):
  317. is_gbanned = await is_gbanned_user(user_id)
  318. if is_gbanned:
  319. return
  320. return await gbansdb.insert_one({"user_id": user_id})
  321. async def remove_gban_user(user_id: int):
  322. is_gbanned = await is_gbanned_user(user_id)
  323. if not is_gbanned:
  324. return
  325. return await gbansdb.delete_one({"user_id": user_id})
  326. async def _get_lovers(chat_id: int):
  327. lovers = await coupledb.find_one({"chat_id": chat_id})
  328. if not lovers:
  329. return {}
  330. return lovers["couple"]
  331. async def get_couple(chat_id: int, date: str):
  332. lovers = await _get_lovers(chat_id)
  333. if date in lovers:
  334. return lovers[date]
  335. return False
  336. async def save_couple(chat_id: int, date: str, couple: dict):
  337. lovers = await _get_lovers(chat_id)
  338. lovers[date] = couple
  339. await coupledb.update_one(
  340. {"chat_id": chat_id},
  341. {"$set": {"couple": lovers}},
  342. upsert=True,
  343. )
  344. async def is_captcha_on(chat_id: int) -> bool:
  345. chat = await captchadb.find_one({"chat_id": chat_id})
  346. if not chat:
  347. return True
  348. return False
  349. async def captcha_on(chat_id: int):
  350. is_captcha = await is_captcha_on(chat_id)
  351. if is_captcha:
  352. return
  353. return await captchadb.delete_one({"chat_id": chat_id})
  354. async def captcha_off(chat_id: int):
  355. is_captcha = await is_captcha_on(chat_id)
  356. if not is_captcha:
  357. return
  358. return await captchadb.insert_one({"chat_id": chat_id})
  359. async def is_antiservice_on(chat_id: int) -> bool:
  360. chat = await antiservicedb.find_one({"chat_id": chat_id})
  361. if not chat:
  362. return True
  363. return False
  364. async def antiservice_on(chat_id: int):
  365. is_antiservice = await is_antiservice_on(chat_id)
  366. if is_antiservice:
  367. return
  368. return await antiservicedb.delete_one({"chat_id": chat_id})
  369. async def antiservice_off(chat_id: int):
  370. is_antiservice = await is_antiservice_on(chat_id)
  371. if not is_antiservice:
  372. return
  373. return await antiservicedb.insert_one({"chat_id": chat_id})
  374. async def is_pmpermit_approved(user_id: int) -> bool:
  375. user = await pmpermitdb.find_one({"user_id": user_id})
  376. if not user:
  377. return False
  378. return True
  379. async def approve_pmpermit(user_id: int):
  380. is_pmpermit = await is_pmpermit_approved(user_id)
  381. if is_pmpermit:
  382. return
  383. return await pmpermitdb.insert_one({"user_id": user_id})
  384. async def disapprove_pmpermit(user_id: int):
  385. is_pmpermit = await is_pmpermit_approved(user_id)
  386. if not is_pmpermit:
  387. return
  388. return await pmpermitdb.delete_one({"user_id": user_id})
  389. async def get_welcome(chat_id: int) -> str:
  390. text = await welcomedb.find_one({"chat_id": chat_id})
  391. if not text:
  392. return ""
  393. return text["text"]
  394. async def set_welcome(chat_id: int, text: str):
  395. return await welcomedb.update_one(
  396. {"chat_id": chat_id}, {"$set": {"text": text}}, upsert=True
  397. )
  398. async def del_welcome(chat_id: int):
  399. return await welcomedb.delete_one({"chat_id": chat_id})
  400. async def update_captcha_cache(captcha_dict):
  401. pickle = obj_to_str(captcha_dict)
  402. await captcha_cachedb.delete_one({"captcha": "cache"})
  403. if not pickle:
  404. return
  405. await captcha_cachedb.update_one(
  406. {"captcha": "cache"},
  407. {"$set": {"pickled": pickle}},
  408. upsert=True,
  409. )
  410. async def get_captcha_cache():
  411. cache = await captcha_cachedb.find_one({"captcha": "cache"})
  412. if not cache:
  413. return []
  414. return str_to_obj(cache["pickled"])
  415. async def get_blacklist_filters_count() -> dict:
  416. chats = blacklist_filtersdb.find({"chat_id": {"$lt": 0}})
  417. if not chats:
  418. return {"chats_count": 0, "filters_count": 0}
  419. chats_count = 0
  420. filters_count = 0
  421. for chat in await chats.to_list(length=1000000000):
  422. filters = await get_blacklisted_words(chat["chat_id"])
  423. filters_count += len(filters)
  424. chats_count += 1
  425. return {
  426. "chats_count": chats_count,
  427. "filters_count": filters_count,
  428. }
  429. async def get_blacklisted_words(chat_id: int) -> List[str]:
  430. _filters = await blacklist_filtersdb.find_one(
  431. {"chat_id": chat_id}
  432. )
  433. if not _filters:
  434. return []
  435. return _filters["filters"]
  436. async def save_blacklist_filter(chat_id: int, word: str):
  437. word = word.lower().strip()
  438. _filters = await get_blacklisted_words(chat_id)
  439. _filters.append(word)
  440. await blacklist_filtersdb.update_one(
  441. {"chat_id": chat_id},
  442. {"$set": {"filters": _filters}},
  443. upsert=True,
  444. )
  445. async def delete_blacklist_filter(chat_id: int, word: str) -> bool:
  446. filtersd = await get_blacklisted_words(chat_id)
  447. word = word.lower().strip()
  448. if word in filtersd:
  449. filtersd.remove(word)
  450. await blacklist_filtersdb.update_one(
  451. {"chat_id": chat_id},
  452. {"$set": {"filters": filtersd}},
  453. upsert=True,
  454. )
  455. return True
  456. return False
  457. async def activate_pipe(
  458. from_chat_id: int, to_chat_id: int, fetcher: str
  459. ):
  460. pipes = await show_pipes()
  461. pipe = {
  462. "from_chat_id": from_chat_id,
  463. "to_chat_id": to_chat_id,
  464. "fetcher": fetcher,
  465. }
  466. pipes.append(pipe)
  467. return await pipesdb.update_one(
  468. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  469. )
  470. async def deactivate_pipe(from_chat_id: int, to_chat_id: int):
  471. pipes = await show_pipes()
  472. if not pipes:
  473. return
  474. for pipe in pipes:
  475. if (
  476. pipe["from_chat_id"] == from_chat_id
  477. and pipe["to_chat_id"] == to_chat_id
  478. ):
  479. pipes.remove(pipe)
  480. return await pipesdb.update_one(
  481. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  482. )
  483. async def is_pipe_active(from_chat_id: int, to_chat_id: int) -> bool:
  484. for pipe in await show_pipes():
  485. if (
  486. pipe["from_chat_id"] == from_chat_id
  487. and pipe["to_chat_id"] == to_chat_id
  488. ):
  489. return True
  490. async def show_pipes() -> list:
  491. pipes = await pipesdb.find_one({"pipe": "pipe"})
  492. if not pipes:
  493. return []
  494. return pipes["pipes"]
  495. async def get_sudoers() -> list:
  496. sudoers = await sudoersdb.find_one({"sudo": "sudo"})
  497. if not sudoers:
  498. return []
  499. return sudoers["sudoers"]
  500. async def add_sudo(user_id: int) -> bool:
  501. sudoers = await get_sudoers()
  502. sudoers.append(user_id)
  503. await sudoersdb.update_one(
  504. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  505. )
  506. return True
  507. async def remove_sudo(user_id: int) -> bool:
  508. sudoers = await get_sudoers()
  509. sudoers.remove(user_id)
  510. await sudoersdb.update_one(
  511. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  512. )
  513. return True
  514. async def blacklisted_chats() -> list:
  515. chats = blacklist_chatdb.find({"chat_id": {"$lt": 0}})
  516. return [
  517. chat["chat_id"]
  518. for chat in await chats.to_list(length=1000000000)
  519. ]
  520. async def blacklist_chat(chat_id: int) -> bool:
  521. if not await blacklist_chatdb.find_one({"chat_id": chat_id}):
  522. await blacklist_chatdb.insert_one({"chat_id": chat_id})
  523. return True
  524. return False
  525. async def whitelist_chat(chat_id: int) -> bool:
  526. if await blacklist_chatdb.find_one({"chat_id": chat_id}):
  527. await blacklist_chatdb.delete_one({"chat_id": chat_id})
  528. return True
  529. return False
  530. async def start_restart_stage(chat_id: int, message_id: int):
  531. await restart_stagedb.update_one(
  532. {"something": "something"},
  533. {
  534. "$set": {
  535. "chat_id": chat_id,
  536. "message_id": message_id,
  537. }
  538. },
  539. upsert=True,
  540. )
  541. async def clean_restart_stage() -> dict:
  542. data = await restart_stagedb.find_one({"something": "something"})
  543. if not data:
  544. return {}
  545. await restart_stagedb.delete_one({"something": "something"})
  546. return {
  547. "chat_id": data["chat_id"],
  548. "message_id": data["message_id"],
  549. }
  550. async def is_flood_on(chat_id: int) -> bool:
  551. chat = await flood_toggle_db.find_one({"chat_id": chat_id})
  552. if not chat:
  553. return True
  554. return False
  555. async def flood_on(chat_id: int):
  556. is_flood = await is_flood_on(chat_id)
  557. if is_flood:
  558. return
  559. return await flood_toggle_db.delete_one({"chat_id": chat_id})
  560. async def flood_off(chat_id: int):
  561. is_flood = await is_flood_on(chat_id)
  562. if not is_flood:
  563. return
  564. return await flood_toggle_db.insert_one({"chat_id": chat_id})
  565. async def add_rss_feed(chat_id: int, url: str, last_title: str):
  566. return await rssdb.update_one(
  567. {"chat_id": chat_id},
  568. {"$set": {"url": url, "last_title": last_title}},
  569. upsert=True,
  570. )
  571. async def remove_rss_feed(chat_id: int):
  572. return await rssdb.delete_one({"chat_id": chat_id})
  573. async def update_rss_feed(chat_id: int, last_title: str):
  574. return await rssdb.update_one(
  575. {"chat_id": chat_id},
  576. {"$set": {"last_title": last_title}},
  577. upsert=True,
  578. )
  579. async def is_rss_active(chat_id: int) -> bool:
  580. return await rssdb.find_one({"chat_id": chat_id})
  581. async def get_rss_feeds() -> list:
  582. feeds = rssdb.find({"chat_id": {"$exists": 1}})
  583. feeds = await feeds.to_list(length=10000000)
  584. if not feeds:
  585. return
  586. data = []
  587. for feed in feeds:
  588. data.append(
  589. dict(
  590. chat_id=feed["chat_id"],
  591. url=feed["url"],
  592. last_title=feed["last_title"],
  593. )
  594. )
  595. return data
  596. async def get_rss_feeds_count() -> int:
  597. feeds = rssdb.find({"chat_id": {"$exists": 1}})
  598. feeds = await feeds.to_list(length=10000000)
  599. return len(feeds)