dbfunctions.py 20 KB

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