dbfunctions.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. from typing import Dict, List, Union
  21. from wbb import db
  22. from wbb.utils.functions import obj_to_str, str_to_obj
  23. """
  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. """
  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. antiservicedb = db.antiservice
  38. pmpermitdb = db.pmpermit
  39. welcomedb = db.welcome_text
  40. captcha_cachedb = db.captcha_cache
  41. blacklist_filtersdb = db.blacklistFilters
  42. pipesdb = db.pipes
  43. sudoersdb = db.sudoers
  44. blacklist_chatdb = db.blacklistChat
  45. restart_stagedb = db.restart_stage
  46. flood_toggle_db = db.flood_toggle
  47. rssdb = db.rss
  48. """ Notes functions """
  49. async def get_notes_count() -> dict:
  50. chats = notesdb.find({"chat_id": {"$lt": 0}})
  51. if not chats:
  52. return {}
  53. chats_count = 0
  54. notes_count = 0
  55. for chat in await chats.to_list(length=1000000000):
  56. notes_name = await get_note_names(chat["chat_id"])
  57. notes_count += len(notes_name)
  58. chats_count += 1
  59. return {"chats_count": chats_count, "notes_count": notes_count}
  60. async def _get_notes(chat_id: int) -> Dict[str, int]:
  61. _notes = await notesdb.find_one({"chat_id": chat_id})
  62. if not _notes:
  63. return {}
  64. return _notes["notes"]
  65. async def get_note_names(chat_id: int) -> List[str]:
  66. _notes = []
  67. for note in await _get_notes(chat_id):
  68. _notes.append(note)
  69. return _notes
  70. async def get_note(chat_id: int, name: str) -> Union[bool, dict]:
  71. name = name.lower().strip()
  72. _notes = await _get_notes(chat_id)
  73. if name in _notes:
  74. return _notes[name]
  75. else:
  76. return False
  77. async def save_note(chat_id: int, name: str, note: dict):
  78. name = name.lower().strip()
  79. _notes = await _get_notes(chat_id)
  80. _notes[name] = note
  81. await notesdb.update_one(
  82. {"chat_id": chat_id}, {"$set": {"notes": _notes}}, upsert=True
  83. )
  84. async def delete_note(chat_id: int, name: str) -> bool:
  85. notesd = await _get_notes(chat_id)
  86. name = name.lower().strip()
  87. if name in notesd:
  88. del notesd[name]
  89. await notesdb.update_one(
  90. {"chat_id": chat_id},
  91. {"$set": {"notes": notesd}},
  92. upsert=True,
  93. )
  94. return True
  95. return False
  96. """ Filters funcions """
  97. async def get_filters_count() -> dict:
  98. chats = filtersdb.find({"chat_id": {"$lt": 0}})
  99. if not chats:
  100. return {}
  101. chats_count = 0
  102. filters_count = 0
  103. for chat in await chats.to_list(length=1000000000):
  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. else:
  127. return False
  128. async def save_filter(chat_id: int, name: str, _filter: dict):
  129. name = name.lower().strip()
  130. _filters = await _get_filters(chat_id)
  131. _filters[name] = _filter
  132. await filtersdb.update_one(
  133. {"chat_id": chat_id},
  134. {"$set": {"filters": _filters}},
  135. upsert=True,
  136. )
  137. async def delete_filter(chat_id: int, name: str) -> bool:
  138. filtersd = await _get_filters(chat_id)
  139. name = name.lower().strip()
  140. if name in filtersd:
  141. del filtersd[name]
  142. await filtersdb.update_one(
  143. {"chat_id": chat_id},
  144. {"$set": {"filters": filtersd}},
  145. upsert=True,
  146. )
  147. return True
  148. return False
  149. """ Warn functions """
  150. async def int_to_alpha(user_id: int) -> str:
  151. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  152. text = ""
  153. user_id = str(user_id)
  154. for i in user_id:
  155. text += alphabet[int(i)]
  156. return text
  157. async def alpha_to_int(user_id_alphabet: str) -> int:
  158. alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
  159. user_id = ""
  160. for i in user_id_alphabet:
  161. index = alphabet.index(i)
  162. user_id += str(index)
  163. user_id = int(user_id)
  164. return user_id
  165. async def get_warns_count() -> dict:
  166. chats = warnsdb.find({"chat_id": {"$lt": 0}})
  167. if not chats:
  168. return {}
  169. chats_count = 0
  170. warns_count = 0
  171. for chat in await chats.to_list(length=100000000):
  172. for user in chat["warns"]:
  173. warns_count += chat["warns"][user]["warns"]
  174. chats_count += 1
  175. return {"chats_count": chats_count, "warns_count": warns_count}
  176. async def get_warns(chat_id: int) -> Dict[str, int]:
  177. warns = await warnsdb.find_one({"chat_id": chat_id})
  178. if not warns:
  179. return {}
  180. return warns["warns"]
  181. async def get_warn(chat_id: int, name: str) -> Union[bool, dict]:
  182. name = name.lower().strip()
  183. warns = await get_warns(chat_id)
  184. if name in warns:
  185. return warns[name]
  186. async def add_warn(chat_id: int, name: str, warn: dict):
  187. name = name.lower().strip()
  188. warns = await get_warns(chat_id)
  189. warns[name] = warn
  190. await warnsdb.update_one(
  191. {"chat_id": chat_id}, {"$set": {"warns": warns}}, upsert=True
  192. )
  193. async def remove_warns(chat_id: int, name: str) -> bool:
  194. warnsd = await get_warns(chat_id)
  195. name = name.lower().strip()
  196. if name in warnsd:
  197. del warnsd[name]
  198. await warnsdb.update_one(
  199. {"chat_id": chat_id},
  200. {"$set": {"warns": warnsd}},
  201. upsert=True,
  202. )
  203. return True
  204. return False
  205. """ Karma functions """
  206. async def get_karmas_count() -> dict:
  207. chats = karmadb.find({"chat_id": {"$lt": 0}})
  208. if not chats:
  209. return {}
  210. chats_count = 0
  211. karmas_count = 0
  212. for chat in await chats.to_list(length=1000000):
  213. for i in chat["karma"]:
  214. karma_ = chat["karma"][i]["karma"]
  215. if karma_ > 0:
  216. karmas_count += karma_
  217. chats_count += 1
  218. return {"chats_count": chats_count, "karmas_count": karmas_count}
  219. async def user_global_karma(user_id) -> int:
  220. chats = karmadb.find({"chat_id": {"$lt": 0}})
  221. if not chats:
  222. return 0
  223. total_karma = 0
  224. for chat in await chats.to_list(length=1000000):
  225. karma = await get_karma(
  226. chat["chat_id"], await int_to_alpha(user_id)
  227. )
  228. if karma:
  229. if 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. """ Chats log functions """
  265. async def is_served_chat(chat_id: int) -> bool:
  266. chat = await chatsdb.find_one({"chat_id": chat_id})
  267. if not chat:
  268. return False
  269. return True
  270. async def get_served_chats() -> list:
  271. chats = chatsdb.find({"chat_id": {"$lt": 0}})
  272. if not chats:
  273. return []
  274. chats_list = []
  275. for chat in await chats.to_list(length=1000000000):
  276. chats_list.append(chat)
  277. return chats_list
  278. async def add_served_chat(chat_id: int):
  279. is_served = await is_served_chat(chat_id)
  280. if is_served:
  281. return
  282. return await chatsdb.insert_one({"chat_id": chat_id})
  283. async def remove_served_chat(chat_id: int):
  284. is_served = await is_served_chat(chat_id)
  285. if not is_served:
  286. return
  287. return await chatsdb.delete_one({"chat_id": chat_id})
  288. """ USER LOG FUNCTIONS """
  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. """ Gban functions """
  308. async def get_gbans_count() -> int:
  309. users = gbansdb.find({"user_id": {"$gt": 0}})
  310. users = await users.to_list(length=100000)
  311. return len(users)
  312. async def is_gbanned_user(user_id: int) -> bool:
  313. user = await gbansdb.find_one({"user_id": user_id})
  314. if not user:
  315. return False
  316. return True
  317. async def add_gban_user(user_id: int):
  318. is_gbanned = await is_gbanned_user(user_id)
  319. if is_gbanned:
  320. return
  321. return await gbansdb.insert_one({"user_id": user_id})
  322. async def remove_gban_user(user_id: int):
  323. is_gbanned = await is_gbanned_user(user_id)
  324. if not is_gbanned:
  325. return
  326. return await gbansdb.delete_one({"user_id": user_id})
  327. # Couple Chooser
  328. async def _get_lovers(chat_id: int):
  329. lovers = await coupledb.find_one({"chat_id": chat_id})
  330. if not lovers:
  331. return {}
  332. return lovers["couple"]
  333. async def get_couple(chat_id: int, date: str):
  334. lovers = await _get_lovers(chat_id)
  335. if date in lovers:
  336. return lovers[date]
  337. else:
  338. return False
  339. async def save_couple(chat_id: int, date: str, couple: dict):
  340. lovers = await _get_lovers(chat_id)
  341. lovers[date] = couple
  342. await coupledb.update_one(
  343. {"chat_id": chat_id},
  344. {"$set": {"couple": lovers}},
  345. upsert=True,
  346. )
  347. # Captcha
  348. async def is_captcha_on(chat_id: int) -> bool:
  349. chat = await captchadb.find_one({"chat_id": chat_id})
  350. if not chat:
  351. return True
  352. return False
  353. async def captcha_on(chat_id: int):
  354. is_captcha = await is_captcha_on(chat_id)
  355. if is_captcha:
  356. return
  357. return await captchadb.delete_one({"chat_id": chat_id})
  358. async def captcha_off(chat_id: int):
  359. is_captcha = await is_captcha_on(chat_id)
  360. if not is_captcha:
  361. return
  362. return await captchadb.insert_one({"chat_id": chat_id})
  363. """Anti Service System"""
  364. async def is_antiservice_on(chat_id: int) -> bool:
  365. chat = await antiservicedb.find_one({"chat_id": chat_id})
  366. if not chat:
  367. return True
  368. return False
  369. async def antiservice_on(chat_id: int):
  370. is_antiservice = await is_antiservice_on(chat_id)
  371. if is_antiservice:
  372. return
  373. return await antiservicedb.delete_one({"chat_id": chat_id})
  374. async def antiservice_off(chat_id: int):
  375. is_antiservice = await is_antiservice_on(chat_id)
  376. if not is_antiservice:
  377. return
  378. return await antiservicedb.insert_one({"chat_id": chat_id})
  379. """ PM PERMIT """
  380. async def is_pmpermit_approved(user_id: int) -> bool:
  381. user = await pmpermitdb.find_one({"user_id": user_id})
  382. if not user:
  383. return False
  384. return True
  385. async def approve_pmpermit(user_id: int):
  386. is_pmpermit = await is_pmpermit_approved(user_id)
  387. if is_pmpermit:
  388. return
  389. return await pmpermitdb.insert_one({"user_id": user_id})
  390. async def disapprove_pmpermit(user_id: int):
  391. is_pmpermit = await is_pmpermit_approved(user_id)
  392. if not is_pmpermit:
  393. return
  394. return await pmpermitdb.delete_one({"user_id": user_id})
  395. """ WELCOME FUNCTIONS """
  396. async def get_welcome(chat_id: int) -> str:
  397. text = await welcomedb.find_one({"chat_id": chat_id})
  398. return text["text"]
  399. async def set_welcome(chat_id: int, text: str):
  400. return await welcomedb.update_one(
  401. {"chat_id": chat_id}, {"$set": {"text": text}}, upsert=True
  402. )
  403. async def del_welcome(chat_id: int):
  404. return await welcomedb.delete_one({"chat_id": chat_id})
  405. """ CAPTCHA CACHE SYSTEM """
  406. async def update_captcha_cache(captcha_dict):
  407. pickle = obj_to_str(captcha_dict)
  408. await captcha_cachedb.delete_one({"captcha": "cache"})
  409. if not pickle:
  410. return
  411. await captcha_cachedb.update_one(
  412. {"captcha": "cache"},
  413. {"$set": {"pickled": pickle}},
  414. upsert=True,
  415. )
  416. async def get_captcha_cache():
  417. cache = await captcha_cachedb.find_one({"captcha": "cache"})
  418. if not cache:
  419. return []
  420. return str_to_obj(cache["pickled"])
  421. """ BLACKLIST FILTERS SYSTEM """
  422. async def get_blacklist_filters_count() -> dict:
  423. chats = blacklist_filtersdb.find({"chat_id": {"$lt": 0}})
  424. if not chats:
  425. return {"chats_count": 0, "filters_count": 0}
  426. chats_count = 0
  427. filters_count = 0
  428. for chat in await chats.to_list(length=1000000000):
  429. filters = await get_blacklisted_words(chat["chat_id"])
  430. filters_count += len(filters)
  431. chats_count += 1
  432. return {
  433. "chats_count": chats_count,
  434. "filters_count": filters_count,
  435. }
  436. async def get_blacklisted_words(chat_id: int) -> List[str]:
  437. _filters = await blacklist_filtersdb.find_one(
  438. {"chat_id": chat_id}
  439. )
  440. if not _filters:
  441. return []
  442. return _filters["filters"]
  443. async def save_blacklist_filter(chat_id: int, word: str):
  444. word = word.lower().strip()
  445. _filters = await get_blacklisted_words(chat_id)
  446. _filters.append(word)
  447. await blacklist_filtersdb.update_one(
  448. {"chat_id": chat_id},
  449. {"$set": {"filters": _filters}},
  450. upsert=True,
  451. )
  452. async def delete_blacklist_filter(chat_id: int, word: str) -> bool:
  453. filtersd = await get_blacklisted_words(chat_id)
  454. word = word.lower().strip()
  455. if word in filtersd:
  456. filtersd.remove(word)
  457. await blacklist_filtersdb.update_one(
  458. {"chat_id": chat_id},
  459. {"$set": {"filters": filtersd}},
  460. upsert=True,
  461. )
  462. return True
  463. return False
  464. """ PIPES SYSTEM """
  465. async def activate_pipe(
  466. from_chat_id: int, to_chat_id: int, fetcher: str
  467. ):
  468. pipes = await show_pipes()
  469. pipe = {
  470. "from_chat_id": from_chat_id,
  471. "to_chat_id": to_chat_id,
  472. "fetcher": fetcher,
  473. }
  474. pipes.append(pipe)
  475. return await pipesdb.update_one(
  476. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  477. )
  478. async def deactivate_pipe(from_chat_id: int, to_chat_id: int):
  479. pipes = await show_pipes()
  480. if not pipes:
  481. return
  482. for pipe in pipes:
  483. if (
  484. pipe["from_chat_id"] == from_chat_id
  485. and pipe["to_chat_id"] == to_chat_id
  486. ):
  487. pipes.remove(pipe)
  488. return await pipesdb.update_one(
  489. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  490. )
  491. async def is_pipe_active(from_chat_id: int, to_chat_id: int) -> bool:
  492. for pipe in await show_pipes():
  493. if (
  494. pipe["from_chat_id"] == from_chat_id
  495. and pipe["to_chat_id"] == to_chat_id
  496. ):
  497. return True
  498. async def show_pipes() -> list:
  499. pipes = await pipesdb.find_one({"pipe": "pipe"})
  500. if not pipes:
  501. return []
  502. return pipes["pipes"]
  503. """ SUDOERS FUNCTIONS """
  504. async def get_sudoers() -> list:
  505. sudoers = await sudoersdb.find_one({"sudo": "sudo"})
  506. if not sudoers:
  507. return []
  508. return sudoers["sudoers"]
  509. async def add_sudo(user_id: int) -> bool:
  510. sudoers = await get_sudoers()
  511. sudoers.append(user_id)
  512. await sudoersdb.update_one(
  513. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  514. )
  515. return True
  516. async def remove_sudo(user_id: int) -> bool:
  517. sudoers = await get_sudoers()
  518. sudoers.remove(user_id)
  519. await sudoersdb.update_one(
  520. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  521. )
  522. return True
  523. """ BLACKLISTED CHATS """
  524. async def blacklisted_chats() -> list:
  525. chats = blacklist_chatdb.find({"chat_id": {"$lt": 0}})
  526. return [
  527. chat["chat_id"]
  528. for chat in await chats.to_list(length=1000000000)
  529. ]
  530. async def blacklist_chat(chat_id: int) -> bool:
  531. if not await blacklist_chatdb.find_one({"chat_id": chat_id}):
  532. await blacklist_chatdb.insert_one({"chat_id": chat_id})
  533. return True
  534. return False
  535. async def whitelist_chat(chat_id: int) -> bool:
  536. if await blacklist_chatdb.find_one({"chat_id": chat_id}):
  537. await blacklist_chatdb.delete_one({"chat_id": chat_id})
  538. return True
  539. return False
  540. """ Restart stage """
  541. async def start_restart_stage(chat_id: int, message_id: int):
  542. await restart_stagedb.update_one(
  543. {"something": "something"},
  544. {
  545. "$set": {
  546. "chat_id": chat_id,
  547. "message_id": message_id,
  548. }
  549. },
  550. upsert=True,
  551. )
  552. async def clean_restart_stage() -> dict:
  553. data = await restart_stagedb.find_one({"something": "something"})
  554. if not data:
  555. return {}
  556. await restart_stagedb.delete_one({"something": "something"})
  557. return {
  558. "chat_id": data["chat_id"],
  559. "message_id": data["message_id"],
  560. }
  561. """FLOOD System"""
  562. async def is_flood_on(chat_id: int) -> bool:
  563. chat = await flood_toggle_db.find_one({"chat_id": chat_id})
  564. if not chat:
  565. return True
  566. return False
  567. async def flood_on(chat_id: int):
  568. is_flood = await is_flood_on(chat_id)
  569. if is_flood:
  570. return
  571. return await flood_toggle_db.delete_one({"chat_id": chat_id})
  572. async def flood_off(chat_id: int):
  573. is_flood = await is_flood_on(chat_id)
  574. if not is_flood:
  575. return
  576. return await flood_toggle_db.insert_one({"chat_id": chat_id})
  577. """ RSS DB """
  578. async def add_rss_feed(chat_id: int, url: str, last_title: str):
  579. return await rssdb.update_one(
  580. {"chat_id": chat_id},
  581. {"$set": {"url": url, "last_title": last_title}},
  582. upsert=True,
  583. )
  584. async def remove_rss_feed(chat_id: int):
  585. return await rssdb.delete_one({"chat_id": chat_id})
  586. async def update_rss_feed(chat_id: int, last_title: str):
  587. return await rssdb.update_one(
  588. {"chat_id": chat_id},
  589. {"$set": {"last_title": last_title}},
  590. upsert=True,
  591. )
  592. async def is_rss_active(chat_id: int) -> bool:
  593. return await rssdb.find_one({"chat_id": chat_id})
  594. async def get_rss_feeds() -> list:
  595. feeds = rssdb.find({"chat_id": {"$exists": 1}})
  596. feeds = await feeds.to_list(length=10000000)
  597. if not feeds:
  598. return
  599. data = []
  600. for feed in feeds:
  601. data.append(
  602. dict(
  603. chat_id=feed["chat_id"],
  604. url=feed["url"],
  605. last_title=feed["last_title"],
  606. )
  607. )
  608. return data
  609. async def get_rss_feeds_count() -> int:
  610. feeds = rssdb.find({"chat_id": {"$exists": 1}})
  611. feeds = await feeds.to_list(length=10000000)
  612. return len(feeds)