dbfunctions.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. # SOME THINGS ARE FUCKED UP HERE, LIKE TOGGLEABLES HAVE THEIR OWN COLLECTION
  25. # (SHOULD FIX IT WITH SOMETHING LIKE TOGGLEDB), MOST OF THE CODE IS BAD AF
  26. # AND NEEDS TO BE REWRITTEN, BUT I WON'T, AS IT WILL TAKE
  27. # TOO MUCH TIME AND WILL BE BAD FOR ALREADY STORED DATA
  28. notesdb = db.notes
  29. filtersdb = db.filters
  30. warnsdb = db.warns
  31. karmadb = db.karma
  32. chatsdb = db.chats
  33. usersdb = db.users
  34. gbansdb = db.gban
  35. coupledb = db.couple
  36. captchadb = db.captcha
  37. solved_captcha_db = db.solved_captcha
  38. captcha_cachedb = db.captcha_cache
  39. antiservicedb = db.antiservice
  40. pmpermitdb = db.pmpermit
  41. welcomedb = db.welcome_text
  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_count = 0
  59. notes_count = 0
  60. async for chat in notesdb.find({"chat_id": {"$exists": 1}}):
  61. notes_name = await get_note_names(chat["chat_id"])
  62. notes_count += len(notes_name)
  63. chats_count += 1
  64. return {"chats_count": chats_count, "notes_count": notes_count}
  65. async def _get_notes(chat_id: int) -> Dict[str, int]:
  66. _notes = await notesdb.find_one({"chat_id": chat_id})
  67. if not _notes:
  68. return {}
  69. return _notes["notes"]
  70. async def get_note_names(chat_id: int) -> List[str]:
  71. _notes = []
  72. for note in await _get_notes(chat_id):
  73. _notes.append(note)
  74. return _notes
  75. async def get_note(chat_id: int, name: str) -> Union[bool, dict]:
  76. name = name.lower().strip()
  77. _notes = await _get_notes(chat_id)
  78. if name in _notes:
  79. return _notes[name]
  80. return False
  81. async def save_note(chat_id: int, name: str, note: dict):
  82. name = name.lower().strip()
  83. _notes = await _get_notes(chat_id)
  84. _notes[name] = note
  85. await notesdb.update_one(
  86. {"chat_id": chat_id}, {"$set": {"notes": _notes}}, upsert=True
  87. )
  88. async def delete_note(chat_id: int, name: str) -> bool:
  89. notesd = await _get_notes(chat_id)
  90. name = name.lower().strip()
  91. if name in notesd:
  92. del notesd[name]
  93. await notesdb.update_one(
  94. {"chat_id": chat_id},
  95. {"$set": {"notes": notesd}},
  96. upsert=True,
  97. )
  98. return True
  99. return False
  100. async def get_filters_count() -> dict:
  101. chats_count = 0
  102. filters_count = 0
  103. async for chat in filtersdb.find({"chat_id": {"$lt": 0}}):
  104. filters_name = await get_filters_names(chat["chat_id"])
  105. filters_count += len(filters_name)
  106. chats_count += 1
  107. return {
  108. "chats_count": chats_count,
  109. "filters_count": filters_count,
  110. }
  111. async def _get_filters(chat_id: int) -> Dict[str, int]:
  112. _filters = await filtersdb.find_one({"chat_id": chat_id})
  113. if not _filters:
  114. return {}
  115. return _filters["filters"]
  116. async def get_filters_names(chat_id: int) -> List[str]:
  117. _filters = []
  118. for _filter in await _get_filters(chat_id):
  119. _filters.append(_filter)
  120. return _filters
  121. async def get_filter(chat_id: int, name: str) -> Union[bool, dict]:
  122. name = name.lower().strip()
  123. _filters = await _get_filters(chat_id)
  124. if name in _filters:
  125. return _filters[name]
  126. return False
  127. async def save_filter(chat_id: int, name: str, _filter: dict):
  128. name = name.lower().strip()
  129. _filters = await _get_filters(chat_id)
  130. _filters[name] = _filter
  131. await filtersdb.update_one(
  132. {"chat_id": chat_id},
  133. {"$set": {"filters": _filters}},
  134. upsert=True,
  135. )
  136. async def delete_filter(chat_id: int, name: str) -> bool:
  137. filtersd = await _get_filters(chat_id)
  138. name = name.lower().strip()
  139. if name in filtersd:
  140. del filtersd[name]
  141. await filtersdb.update_one(
  142. {"chat_id": chat_id},
  143. {"$set": {"filters": filtersd}},
  144. upsert=True,
  145. )
  146. return True
  147. return False
  148. async def int_to_alpha(user_id: int) -> str:
  149. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  150. text = ""
  151. user_id = str(user_id)
  152. for i in user_id:
  153. text += alphabet[int(i)]
  154. return text
  155. async def alpha_to_int(user_id_alphabet: str) -> int:
  156. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  157. user_id = ""
  158. for i in user_id_alphabet:
  159. index = alphabet.index(i)
  160. user_id += str(index)
  161. user_id = int(user_id)
  162. return user_id
  163. async def get_warns_count() -> dict:
  164. chats_count = 0
  165. warns_count = 0
  166. async for chat in warnsdb.find({"chat_id": {"$lt": 0}}):
  167. for user in chat["warns"]:
  168. warns_count += chat["warns"][user]["warns"]
  169. chats_count += 1
  170. return {"chats_count": chats_count, "warns_count": warns_count}
  171. async def get_warns(chat_id: int) -> Dict[str, int]:
  172. warns = await warnsdb.find_one({"chat_id": chat_id})
  173. if not warns:
  174. return {}
  175. return warns["warns"]
  176. async def get_warn(chat_id: int, name: str) -> Union[bool, dict]:
  177. name = name.lower().strip()
  178. warns = await get_warns(chat_id)
  179. if name in warns:
  180. return warns[name]
  181. async def add_warn(chat_id: int, name: str, warn: dict):
  182. name = name.lower().strip()
  183. warns = await get_warns(chat_id)
  184. warns[name] = warn
  185. await warnsdb.update_one(
  186. {"chat_id": chat_id}, {"$set": {"warns": warns}}, upsert=True
  187. )
  188. async def remove_warns(chat_id: int, name: str) -> bool:
  189. warnsd = await get_warns(chat_id)
  190. name = name.lower().strip()
  191. if name in warnsd:
  192. del warnsd[name]
  193. await warnsdb.update_one(
  194. {"chat_id": chat_id},
  195. {"$set": {"warns": warnsd}},
  196. upsert=True,
  197. )
  198. return True
  199. return False
  200. async def get_karmas_count() -> dict:
  201. chats_count = 0
  202. karmas_count = 0
  203. async for chat in karmadb.find({"chat_id": {"$lt": 0}}):
  204. for i in chat["karma"]:
  205. karma_ = chat["karma"][i]["karma"]
  206. if karma_ > 0:
  207. karmas_count += karma_
  208. chats_count += 1
  209. return {"chats_count": chats_count, "karmas_count": karmas_count}
  210. async def user_global_karma(user_id) -> int:
  211. total_karma = 0
  212. async for chat in karmadb.find({"chat_id": {"$lt": 0}}):
  213. karma = await get_karma(chat["chat_id"], await int_to_alpha(user_id))
  214. if karma and (int(karma["karma"]) > 0):
  215. total_karma += int(karma["karma"])
  216. return total_karma
  217. async def get_karmas(chat_id: int) -> Dict[str, int]:
  218. karma = await karmadb.find_one({"chat_id": chat_id})
  219. if not karma:
  220. return {}
  221. return karma["karma"]
  222. async def get_karma(chat_id: int, name: str) -> Union[bool, dict]:
  223. name = name.lower().strip()
  224. karmas = await get_karmas(chat_id)
  225. if name in karmas:
  226. return karmas[name]
  227. async def update_karma(chat_id: int, name: str, karma: dict):
  228. name = name.lower().strip()
  229. karmas = await get_karmas(chat_id)
  230. karmas[name] = karma
  231. await karmadb.update_one(
  232. {"chat_id": chat_id}, {"$set": {"karma": karmas}}, upsert=True
  233. )
  234. async def is_karma_on(chat_id: int) -> bool:
  235. chat = await karmadb.find_one({"chat_id_toggle": chat_id})
  236. if not chat:
  237. return True
  238. return False
  239. async def karma_on(chat_id: int):
  240. is_karma = await is_karma_on(chat_id)
  241. if is_karma:
  242. return
  243. return await karmadb.delete_one({"chat_id_toggle": chat_id})
  244. async def karma_off(chat_id: int):
  245. is_karma = await is_karma_on(chat_id)
  246. if not is_karma:
  247. return
  248. return await karmadb.insert_one({"chat_id_toggle": chat_id})
  249. async def is_served_chat(chat_id: int) -> bool:
  250. chat = await chatsdb.find_one({"chat_id": chat_id})
  251. if not chat:
  252. return False
  253. return True
  254. async def get_served_chats() -> list:
  255. chats_list = []
  256. async for chat in chatsdb.find({"chat_id": {"$lt": 0}}):
  257. chats_list.append(chat)
  258. return chats_list
  259. async def add_served_chat(chat_id: int):
  260. is_served = await is_served_chat(chat_id)
  261. if is_served:
  262. return
  263. return await chatsdb.insert_one({"chat_id": chat_id})
  264. async def remove_served_chat(chat_id: int):
  265. is_served = await is_served_chat(chat_id)
  266. if not is_served:
  267. return
  268. return await chatsdb.delete_one({"chat_id": chat_id})
  269. async def is_served_user(user_id: int) -> bool:
  270. user = await usersdb.find_one({"user_id": user_id})
  271. if not user:
  272. return False
  273. return True
  274. async def get_served_users() -> list:
  275. users_list = []
  276. async for user in usersdb.find({"user_id": {"$gt": 0}}):
  277. users_list.append(user)
  278. return users_list
  279. async def add_served_user(user_id: int):
  280. is_served = await is_served_user(user_id)
  281. if is_served:
  282. return
  283. return await usersdb.insert_one({"user_id": user_id})
  284. async def get_gbans_count() -> int:
  285. return len([i async for i in gbansdb.find({"user_id": {"$gt": 0}})])
  286. async def is_gbanned_user(user_id: int) -> bool:
  287. user = await gbansdb.find_one({"user_id": user_id})
  288. if not user:
  289. return False
  290. return True
  291. async def add_gban_user(user_id: int):
  292. is_gbanned = await is_gbanned_user(user_id)
  293. if is_gbanned:
  294. return
  295. return await gbansdb.insert_one({"user_id": user_id})
  296. async def remove_gban_user(user_id: int):
  297. is_gbanned = await is_gbanned_user(user_id)
  298. if not is_gbanned:
  299. return
  300. return await gbansdb.delete_one({"user_id": user_id})
  301. async def _get_lovers(chat_id: int):
  302. lovers = await coupledb.find_one({"chat_id": chat_id})
  303. if not lovers:
  304. return {}
  305. return lovers["couple"]
  306. async def get_couple(chat_id: int, date: str):
  307. lovers = await _get_lovers(chat_id)
  308. if date in lovers:
  309. return lovers[date]
  310. return False
  311. async def save_couple(chat_id: int, date: str, couple: dict):
  312. lovers = await _get_lovers(chat_id)
  313. lovers[date] = couple
  314. await coupledb.update_one(
  315. {"chat_id": chat_id},
  316. {"$set": {"couple": lovers}},
  317. upsert=True,
  318. )
  319. async def is_captcha_on(chat_id: int) -> bool:
  320. chat = await captchadb.find_one({"chat_id": chat_id})
  321. if not chat:
  322. return True
  323. return False
  324. async def captcha_on(chat_id: int):
  325. is_captcha = await is_captcha_on(chat_id)
  326. if is_captcha:
  327. return
  328. return await captchadb.delete_one({"chat_id": chat_id})
  329. async def captcha_off(chat_id: int):
  330. is_captcha = await is_captcha_on(chat_id)
  331. if not is_captcha:
  332. return
  333. return await captchadb.insert_one({"chat_id": chat_id})
  334. async def has_solved_captcha_once(chat_id: int, user_id: int):
  335. has_solved = await solved_captcha_db.find_one(
  336. {"chat_id": chat_id, "user_id": user_id}
  337. )
  338. return bool(has_solved)
  339. async def save_captcha_solved(chat_id: int, user_id: int):
  340. return await solved_captcha_db.update_one(
  341. {"chat_id": chat_id},
  342. {"$set": {"user_id": user_id}},
  343. upsert=True,
  344. )
  345. async def is_antiservice_on(chat_id: int) -> bool:
  346. chat = await antiservicedb.find_one({"chat_id": chat_id})
  347. if not chat:
  348. return True
  349. return False
  350. async def antiservice_on(chat_id: int):
  351. is_antiservice = await is_antiservice_on(chat_id)
  352. if is_antiservice:
  353. return
  354. return await antiservicedb.delete_one({"chat_id": chat_id})
  355. async def antiservice_off(chat_id: int):
  356. is_antiservice = await is_antiservice_on(chat_id)
  357. if not is_antiservice:
  358. return
  359. return await antiservicedb.insert_one({"chat_id": chat_id})
  360. async def is_pmpermit_approved(user_id: int) -> bool:
  361. user = await pmpermitdb.find_one({"user_id": user_id})
  362. if not user:
  363. return False
  364. return True
  365. async def approve_pmpermit(user_id: int):
  366. is_pmpermit = await is_pmpermit_approved(user_id)
  367. if is_pmpermit:
  368. return
  369. return await pmpermitdb.insert_one({"user_id": user_id})
  370. async def disapprove_pmpermit(user_id: int):
  371. is_pmpermit = await is_pmpermit_approved(user_id)
  372. if not is_pmpermit:
  373. return
  374. return await pmpermitdb.delete_one({"user_id": user_id})
  375. async def get_welcome(chat_id: int) -> str:
  376. text = await welcomedb.find_one({"chat_id": chat_id})
  377. if not text:
  378. return ""
  379. return text["text"]
  380. async def set_welcome(chat_id: int, text: str):
  381. return await welcomedb.update_one(
  382. {"chat_id": chat_id}, {"$set": {"text": text}}, upsert=True
  383. )
  384. async def del_welcome(chat_id: int):
  385. return await welcomedb.delete_one({"chat_id": chat_id})
  386. async def update_captcha_cache(captcha_dict):
  387. pickle = obj_to_str(captcha_dict)
  388. await captcha_cachedb.delete_one({"captcha": "cache"})
  389. if not pickle:
  390. return
  391. await captcha_cachedb.update_one(
  392. {"captcha": "cache"},
  393. {"$set": {"pickled": pickle}},
  394. upsert=True,
  395. )
  396. async def get_captcha_cache():
  397. cache = await captcha_cachedb.find_one({"captcha": "cache"})
  398. if not cache:
  399. return []
  400. return str_to_obj(cache["pickled"])
  401. async def get_blacklist_filters_count() -> dict:
  402. chats_count = 0
  403. filters_count = 0
  404. async for chat in blacklist_filtersdb.find({"chat_id": {"$lt": 0}}):
  405. filters = await get_blacklisted_words(chat["chat_id"])
  406. filters_count += len(filters)
  407. chats_count += 1
  408. return {
  409. "chats_count": chats_count,
  410. "filters_count": filters_count,
  411. }
  412. async def get_blacklisted_words(chat_id: int) -> List[str]:
  413. _filters = await blacklist_filtersdb.find_one({"chat_id": chat_id})
  414. if not _filters:
  415. return []
  416. return _filters["filters"]
  417. async def save_blacklist_filter(chat_id: int, word: str):
  418. word = word.lower().strip()
  419. _filters = await get_blacklisted_words(chat_id)
  420. _filters.append(word)
  421. await blacklist_filtersdb.update_one(
  422. {"chat_id": chat_id},
  423. {"$set": {"filters": _filters}},
  424. upsert=True,
  425. )
  426. async def delete_blacklist_filter(chat_id: int, word: str) -> bool:
  427. filtersd = await get_blacklisted_words(chat_id)
  428. word = word.lower().strip()
  429. if word in filtersd:
  430. filtersd.remove(word)
  431. await blacklist_filtersdb.update_one(
  432. {"chat_id": chat_id},
  433. {"$set": {"filters": filtersd}},
  434. upsert=True,
  435. )
  436. return True
  437. return False
  438. async def activate_pipe(from_chat_id: int, to_chat_id: int, fetcher: str):
  439. pipes = await show_pipes()
  440. pipe = {
  441. "from_chat_id": from_chat_id,
  442. "to_chat_id": to_chat_id,
  443. "fetcher": fetcher,
  444. }
  445. pipes.append(pipe)
  446. return await pipesdb.update_one(
  447. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  448. )
  449. async def deactivate_pipe(from_chat_id: int, to_chat_id: int):
  450. pipes = await show_pipes()
  451. if not pipes:
  452. return
  453. for pipe in pipes:
  454. if (
  455. pipe["from_chat_id"] == from_chat_id
  456. and pipe["to_chat_id"] == to_chat_id
  457. ):
  458. pipes.remove(pipe)
  459. return await pipesdb.update_one(
  460. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  461. )
  462. async def is_pipe_active(from_chat_id: int, to_chat_id: int) -> bool:
  463. for pipe in await show_pipes():
  464. if (
  465. pipe["from_chat_id"] == from_chat_id
  466. and pipe["to_chat_id"] == to_chat_id
  467. ):
  468. return True
  469. async def show_pipes() -> list:
  470. pipes = await pipesdb.find_one({"pipe": "pipe"})
  471. if not pipes:
  472. return []
  473. return pipes["pipes"]
  474. async def get_sudoers() -> list:
  475. sudoers = await sudoersdb.find_one({"sudo": "sudo"})
  476. if not sudoers:
  477. return []
  478. return sudoers["sudoers"]
  479. async def add_sudo(user_id: int) -> bool:
  480. sudoers = await get_sudoers()
  481. sudoers.append(user_id)
  482. await sudoersdb.update_one(
  483. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  484. )
  485. return True
  486. async def remove_sudo(user_id: int) -> bool:
  487. sudoers = await get_sudoers()
  488. sudoers.remove(user_id)
  489. await sudoersdb.update_one(
  490. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  491. )
  492. return True
  493. async def blacklisted_chats() -> list:
  494. blacklist_chat = []
  495. async for chat in blacklist_chatdb.find({"chat_id": {"$lt": 0}}):
  496. blacklist_chat.append(chat["chat_id"])
  497. return blacklist_chat
  498. async def blacklist_chat(chat_id: int) -> bool:
  499. if not await blacklist_chatdb.find_one({"chat_id": chat_id}):
  500. await blacklist_chatdb.insert_one({"chat_id": chat_id})
  501. return True
  502. return False
  503. async def whitelist_chat(chat_id: int) -> bool:
  504. if await blacklist_chatdb.find_one({"chat_id": chat_id}):
  505. await blacklist_chatdb.delete_one({"chat_id": chat_id})
  506. return True
  507. return False
  508. async def start_restart_stage(chat_id: int, message_id: int):
  509. await restart_stagedb.update_one(
  510. {"something": "something"},
  511. {
  512. "$set": {
  513. "chat_id": chat_id,
  514. "message_id": message_id,
  515. }
  516. },
  517. upsert=True,
  518. )
  519. async def clean_restart_stage() -> dict:
  520. data = await restart_stagedb.find_one({"something": "something"})
  521. if not data:
  522. return {}
  523. await restart_stagedb.delete_one({"something": "something"})
  524. return {
  525. "chat_id": data["chat_id"],
  526. "message_id": data["message_id"],
  527. }
  528. async def is_flood_on(chat_id: int) -> bool:
  529. chat = await flood_toggle_db.find_one({"chat_id": chat_id})
  530. if not chat:
  531. return True
  532. return False
  533. async def flood_on(chat_id: int):
  534. is_flood = await is_flood_on(chat_id)
  535. if is_flood:
  536. return
  537. return await flood_toggle_db.delete_one({"chat_id": chat_id})
  538. async def flood_off(chat_id: int):
  539. is_flood = await is_flood_on(chat_id)
  540. if not is_flood:
  541. return
  542. return await flood_toggle_db.insert_one({"chat_id": chat_id})
  543. async def add_rss_feed(chat_id: int, url: str, last_title: str):
  544. return await rssdb.update_one(
  545. {"chat_id": chat_id},
  546. {"$set": {"url": url, "last_title": last_title}},
  547. upsert=True,
  548. )
  549. async def remove_rss_feed(chat_id: int):
  550. return await rssdb.delete_one({"chat_id": chat_id})
  551. async def update_rss_feed(chat_id: int, last_title: str):
  552. return await rssdb.update_one(
  553. {"chat_id": chat_id},
  554. {"$set": {"last_title": last_title}},
  555. upsert=True,
  556. )
  557. async def is_rss_active(chat_id: int) -> bool:
  558. return await rssdb.find_one({"chat_id": chat_id})
  559. async def get_rss_feeds() -> list:
  560. data = []
  561. async for feed in rssdb.find({"chat_id": {"$exists": 1}}):
  562. data.append(
  563. dict(
  564. chat_id=feed["chat_id"],
  565. url=feed["url"],
  566. last_title=feed["last_title"],
  567. )
  568. )
  569. return data
  570. async def get_rss_feeds_count() -> int:
  571. return len([i async for i in rssdb.find({"chat_id": {"$exists": 1}})])