Skip to content

Commit

Permalink
Move mention updates from i18n to dispatcher
Browse files Browse the repository at this point in the history
Signed-off-by: alfred richardsn <rchrdsn@protonmail.ch>
  • Loading branch information
r4rdsn committed Jul 3, 2020
1 parent 19d505d commit f959d0d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
20 changes: 17 additions & 3 deletions src/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from aiogram.contrib.middlewares.logging import LoggingMiddleware
from aiogram.dispatcher import Dispatcher
from aiogram.dispatcher.middlewares import BaseMiddleware
from pymongo import ReturnDocument

from src.config import config
from src.database import database
Expand Down Expand Up @@ -99,8 +100,21 @@ async def process_update(self, update: types.Update):
user = update.callback_query.from_user
chat = update.callback_query.message.chat
if user:
db_user = await database.users.find_one({"id": user.id, "chat": chat.id})
if db_user is None:
await database.users.update_many(
{"id": {"$ne": user.id}, "mention": user.mention},
{"$set": {"has_username": False}},
)
document = await database.users.find_one_and_update(
{"id": user.id, "chat": chat.id},
{
"$set": {
"mention": user.mention,
"has_username": bool(user.username),
}
},
return_document=ReturnDocument.AFTER,
)
if document is None:
if update.message:
update.message.text = "/start"
elif update.callback_query:
Expand All @@ -115,7 +129,7 @@ async def process_update(self, update: types.Update):
"text": "/start",
},
)
database_user.set(db_user)
database_user.set(document)
return await super().process_update(update)


Expand Down
13 changes: 2 additions & 11 deletions src/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

from aiogram import types
from aiogram.contrib.middlewares.i18n import I18nMiddleware
from pymongo import ReturnDocument

from src.database import database
from src.database import database_user


class I18nMiddlewareManual(I18nMiddleware):
Expand Down Expand Up @@ -56,15 +55,7 @@ async def get_user_locale(
return None

user: types.User = types.User.get_current()
await database.users.update_many(
{"id": {"$ne": user.id}, "mention": user.mention},
{"$set": {"has_username": False}},
)
document = await database.users.find_one_and_update(
{"id": user.id},
{"$set": {"mention": user.mention, "has_username": bool(user.username)}},
return_document=ReturnDocument.AFTER,
)
document = database_user.get()
if document:
locale = document.get("locale", user.language_code)
else:
Expand Down

0 comments on commit f959d0d

Please sign in to comment.