diff --git a/tasks.ipynb b/tasks.ipynb index 560ae4d..f04d25c 100644 --- a/tasks.ipynb +++ b/tasks.ipynb @@ -69,7 +69,7 @@ "outputs": [], "source": [ "async def notify_in_profile(item: NotifyItem):\n", - " doc = await notify_collection.update_one(\n", + " result = await notify_collection.update_one(\n", " {\n", " \"user_id\": item.user_id,\n", " \"type\": item.type_item,\n", @@ -90,21 +90,42 @@ " },\n", " upsert=True,\n", " )\n", - " if doc.upserted_id:\n", - " user = await users_collection.find_one({'_id': ObjectId(str(item.user_id))},{'email':1})\n", - " login_link = f'{FULL_DOMAIN}/login/{await token_create(str(user['_id']), url=f'/profile')}'\n", - " if PROD:\n", - " email_task.notify.delay(user['email'], {\n", - " 'text': item.text,\n", - " 'list': [[]],\n", - " 'primary': {\n", - " 'href': login_link,\n", - " 'text': 'Войти и посмотреть'\n", + "\n", + " if result.upserted_id is None:\n", + " return result # Existing notification was updated; no email\n", + "\n", + " user = await users_collection.find_one(\n", + " {\"_id\": ObjectId(str(item.user_id))},\n", + " {\"email\": 1},\n", + " )\n", + "\n", + " if not user:\n", + " return result\n", + "\n", + " token = await token_create(\n", + " str(user[\"_id\"]),\n", + " url=\"/profile\",\n", + " )\n", + " login_link = f\"{FULL_DOMAIN}/login/{token}\"\n", + "\n", + " if PROD:\n", + " email_task.notify.delay(\n", + " user[\"email\"],\n", + " {\n", + " \"text\": item.text,\n", + " \"list\": [[]],\n", + " \"primary\": {\n", + " \"href\": login_link,\n", + " \"text\": \"Войти и посмотреть\",\n", " },\n", - " 'title': item.title,\n", - " 'login_link': login_link,\n", - " }, 'Уведомление', 'main.html')\n", - " return doc\n" + " \"title\": item.title,\n", + " \"login_link\": login_link,\n", + " },\n", + " \"Уведомление\",\n", + " \"main.html\",\n", + " )\n", + "\n", + " return result\n" ], "display_name": ".venv" }, diff --git a/tasks.py b/tasks.py index 252a739..594acca 100644 --- a/tasks.py +++ b/tasks.py @@ -12,7 +12,7 @@ import asyncio async def notify_in_profile(item: NotifyItem): - doc = await notify_collection.update_one( + result = await notify_collection.update_one( { "user_id": item.user_id, "type": item.type_item, @@ -33,21 +33,42 @@ async def notify_in_profile(item: NotifyItem): }, upsert=True, ) - if doc.upserted_id: - user = await users_collection.find_one({'_id': ObjectId(str(item.user_id))},{'email':1}) - login_link = f'{FULL_DOMAIN}/login/{await token_create(str(user['_id']), url=f'/profile')}' - if PROD: - email_task.notify.delay(user['email'], { - 'text': item.text, - 'list': [[]], - 'primary': { - 'href': login_link, - 'text': 'Войти и посмотреть' + + if result.upserted_id is None: + return result # Existing notification was updated; no email + + user = await users_collection.find_one( + {"_id": ObjectId(str(item.user_id))}, + {"email": 1}, + ) + + if not user: + return result + + token = await token_create( + str(user["_id"]), + url="/profile", + ) + login_link = f"{FULL_DOMAIN}/login/{token}" + + if PROD: + email_task.notify.delay( + user["email"], + { + "text": item.text, + "list": [[]], + "primary": { + "href": login_link, + "text": "Войти и посмотреть", }, - 'title': item.title, - 'login_link': login_link, - }, 'Уведомление', 'main.html') - return doc + "title": item.title, + "login_link": login_link, + }, + "Уведомление", + "main.html", + ) + + return result async def email_new_post(new_post: Dict):