dbfunctions.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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 = 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(chat["chat_id"], await int_to_alpha(user_id))
  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 has_solved_captcha_once(chat_id: int, user_id: int):
  358. has_solved = await solved_captcha_db.find_one(
  359. {"chat_id": chat_id, "user_id": user_id}
  360. )
  361. return bool(has_solved)
  362. async def save_captcha_solved(chat_id: int, user_id: int):
  363. return await solved_captcha_db.update_one(
  364. {"chat_id": chat_id},
  365. {"$set": {"user_id": user_id}},
  366. upsert=True,
  367. )
  368. async def is_antiservice_on(chat_id: int) -> bool:
  369. chat = await antiservicedb.find_one({"chat_id": chat_id})
  370. if not chat:
  371. return True
  372. return False
  373. async def antiservice_on(chat_id: int):
  374. is_antiservice = await is_antiservice_on(chat_id)
  375. if is_antiservice:
  376. return
  377. return await antiservicedb.delete_one({"chat_id": chat_id})
  378. async def antiservice_off(chat_id: int):
  379. is_antiservice = await is_antiservice_on(chat_id)
  380. if not is_antiservice:
  381. return
  382. return await antiservicedb.insert_one({"chat_id": chat_id})
  383. async def is_pmpermit_approved(user_id: int) -> bool:
  384. user = await pmpermitdb.find_one({"user_id": user_id})
  385. if not user:
  386. return False
  387. return True
  388. async def approve_pmpermit(user_id: int):
  389. is_pmpermit = await is_pmpermit_approved(user_id)
  390. if is_pmpermit:
  391. return
  392. return await pmpermitdb.insert_one({"user_id": user_id})
  393. async def disapprove_pmpermit(user_id: int):
  394. is_pmpermit = await is_pmpermit_approved(user_id)
  395. if not is_pmpermit:
  396. return
  397. return await pmpermitdb.delete_one({"user_id": user_id})
  398. async def get_welcome(chat_id: int) -> str:
  399. text = await welcomedb.find_one({"chat_id": chat_id})
  400. if not text:
  401. return ""
  402. return text["text"]
  403. async def set_welcome(chat_id: int, text: str):
  404. return await welcomedb.update_one(
  405. {"chat_id": chat_id}, {"$set": {"text": text}}, upsert=True
  406. )
  407. async def del_welcome(chat_id: int):
  408. return await welcomedb.delete_one({"chat_id": chat_id})
  409. async def update_captcha_cache(captcha_dict):
  410. pickle = obj_to_str(captcha_dict)
  411. await captcha_cachedb.delete_one({"captcha": "cache"})
  412. if not pickle:
  413. return
  414. await captcha_cachedb.update_one(
  415. {"captcha": "cache"},
  416. {"$set": {"pickled": pickle}},
  417. upsert=True,
  418. )
  419. async def get_captcha_cache():
  420. cache = await captcha_cachedb.find_one({"captcha": "cache"})
  421. if not cache:
  422. return []
  423. return str_to_obj(cache["pickled"])
  424. async def get_blacklist_filters_count() -> dict:
  425. chats = blacklist_filtersdb.find({"chat_id": {"$lt": 0}})
  426. if not chats:
  427. return {"chats_count": 0, "filters_count": 0}
  428. chats_count = 0
  429. filters_count = 0
  430. for chat in await chats.to_list(length=1000000000):
  431. filters = await get_blacklisted_words(chat["chat_id"])
  432. filters_count += len(filters)
  433. chats_count += 1
  434. return {
  435. "chats_count": chats_count,
  436. "filters_count": filters_count,
  437. }
  438. async def get_blacklisted_words(chat_id: int) -> List[str]:
  439. _filters = await blacklist_filtersdb.find_one({"chat_id": chat_id})
  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. async def activate_pipe(from_chat_id: int, to_chat_id: int, fetcher: str):
  465. pipes = await show_pipes()
  466. pipe = {
  467. "from_chat_id": from_chat_id,
  468. "to_chat_id": to_chat_id,
  469. "fetcher": fetcher,
  470. }
  471. pipes.append(pipe)
  472. return await pipesdb.update_one(
  473. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  474. )
  475. async def deactivate_pipe(from_chat_id: int, to_chat_id: int):
  476. pipes = await show_pipes()
  477. if not pipes:
  478. return
  479. for pipe in pipes:
  480. if (
  481. pipe["from_chat_id"] == from_chat_id
  482. and pipe["to_chat_id"] == to_chat_id
  483. ):
  484. pipes.remove(pipe)
  485. return await pipesdb.update_one(
  486. {"pipe": "pipe"}, {"$set": {"pipes": pipes}}, upsert=True
  487. )
  488. async def is_pipe_active(from_chat_id: int, to_chat_id: int) -> bool:
  489. for pipe in await show_pipes():
  490. if (
  491. pipe["from_chat_id"] == from_chat_id
  492. and pipe["to_chat_id"] == to_chat_id
  493. ):
  494. return True
  495. async def show_pipes() -> list:
  496. pipes = await pipesdb.find_one({"pipe": "pipe"})
  497. if not pipes:
  498. return []
  499. return pipes["pipes"]
  500. async def get_sudoers() -> list:
  501. sudoers = await sudoersdb.find_one({"sudo": "sudo"})
  502. if not sudoers:
  503. return []
  504. return sudoers["sudoers"]
  505. async def add_sudo(user_id: int) -> bool:
  506. sudoers = await get_sudoers()
  507. sudoers.append(user_id)
  508. await sudoersdb.update_one(
  509. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  510. )
  511. return True
  512. async def remove_sudo(user_id: int) -> bool:
  513. sudoers = await get_sudoers()
  514. sudoers.remove(user_id)
  515. await sudoersdb.update_one(
  516. {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
  517. )
  518. return True
  519. async def blacklisted_chats() -> list:
  520. chats = blacklist_chatdb.find({"chat_id": {"$lt": 0}})
  521. return [chat["chat_id"] for chat in await chats.to_list(length=1000000000)]
  522. async def blacklist_chat(chat_id: int) -> bool:
  523. if not await blacklist_chatdb.find_one({"chat_id": chat_id}):
  524. await blacklist_chatdb.insert_one({"chat_id": chat_id})
  525. return True
  526. return False
  527. async def whitelist_chat(chat_id: int) -> bool:
  528. if await blacklist_chatdb.find_one({"chat_id": chat_id}):
  529. await blacklist_chatdb.delete_one({"chat_id": chat_id})
  530. return True
  531. return False
  532. async def start_restart_stage(chat_id: int, message_id: int):
  533. await restart_stagedb.update_one(
  534. {"something": "something"},
  535. {
  536. "$set": {
  537. "chat_id": chat_id,
  538. "message_id": message_id,
  539. }
  540. },
  541. upsert=True,
  542. )
  543. async def clean_restart_stage() -> dict:
  544. data = await restart_stagedb.find_one({"something": "something"})
  545. if not data:
  546. return {}
  547. await restart_stagedb.delete_one({"something": "something"})
  548. return {
  549. "chat_id": data["chat_id"],
  550. "message_id": data["message_id"],
  551. }
  552. async def is_flood_on(chat_id: int) -> bool:
  553. chat = await flood_toggle_db.find_one({"chat_id": chat_id})
  554. if not chat:
  555. return True
  556. return False
  557. async def flood_on(chat_id: int):
  558. is_flood = await is_flood_on(chat_id)
  559. if is_flood:
  560. return
  561. return await flood_toggle_db.delete_one({"chat_id": chat_id})
  562. async def flood_off(chat_id: int):
  563. is_flood = await is_flood_on(chat_id)
  564. if not is_flood:
  565. return
  566. return await flood_toggle_db.insert_one({"chat_id": chat_id})
  567. async def add_rss_feed(chat_id: int, url: str, last_title: str):
  568. return await rssdb.update_one(
  569. {"chat_id": chat_id},
  570. {"$set": {"url": url, "last_title": last_title}},
  571. upsert=True,
  572. )
  573. async def remove_rss_feed(chat_id: int):
  574. return await rssdb.delete_one({"chat_id": chat_id})
  575. async def update_rss_feed(chat_id: int, last_title: str):
  576. return await rssdb.update_one(
  577. {"chat_id": chat_id},
  578. {"$set": {"last_title": last_title}},
  579. upsert=True,
  580. )
  581. async def is_rss_active(chat_id: int) -> bool:
  582. return await rssdb.find_one({"chat_id": chat_id})
  583. async def get_rss_feeds() -> list:
  584. feeds = rssdb.find({"chat_id": {"$exists": 1}})
  585. feeds = await feeds.to_list(length=10000000)
  586. if not feeds:
  587. return
  588. data = []
  589. for feed in feeds:
  590. data.append(
  591. dict(
  592. chat_id=feed["chat_id"],
  593. url=feed["url"],
  594. last_title=feed["last_title"],
  595. )
  596. )
  597. return data
  598. async def get_rss_feeds_count() -> int:
  599. feeds = rssdb.find({"chat_id": {"$exists": 1}})
  600. feeds = await feeds.to_list(length=10000000)
  601. return len(feeds)