wip-refactoring

This commit is contained in:
tonyrewin 2022-11-16 09:35:51 +03:00
parent db3617c80d
commit 606655ee2c
5 changed files with 20 additions and 15 deletions

View File

@ -32,16 +32,16 @@ async def invite_author(_, info, author, shout):
authors = [a.id for a in shout.authors] authors = [a.id for a in shout.authors]
if user_id not in authors: if user_id not in authors:
return {"error": "access denied"} return {"error": "access denied"}
author = session.query(User).filter(User.slug == author).first() author = session.query(User).filter(User.id == author.id).first()
if author.id in authors: if author:
return {"error": "already added"} if author.id in authors:
shout.authors.append(author) return {"error": "already added"}
shout.authors.append(author)
shout.updated_at = datetime.now() shout.updated_at = datetime.now()
session.add(shout) session.add(shout)
session.commit() session.commit()
# TODO: email notify # TODO: email notify
return {} return {}
@ -59,9 +59,10 @@ async def remove_author(_, info, author, shout):
if user_id not in authors: if user_id not in authors:
return {"error": "access denied"} return {"error": "access denied"}
author = session.query(User).filter(User.slug == author).first() author = session.query(User).filter(User.slug == author).first()
if author.id not in authors: if author:
return {"error": "not in authors"} if author.id not in authors:
shout.authors.remove(author) return {"error": "not in authors"}
shout.authors.remove(author)
shout.updated_at = datetime.now() shout.updated_at = datetime.now()
session.add(shout) session.add(shout)
session.commit() session.commit()

View File

@ -1,13 +1,16 @@
import json import json
from datetime import datetime, timedelta from datetime import datetime, timedelta
from auth.authenticate import login_required
from base.redis import redis from base.redis import redis
from base.resolvers import query from base.resolvers import query
from auth.authenticate import login_required
async def get_unread_counter(chat_id: str, user_slug: str): async def get_unread_counter(chat_id: str, user_slug: str):
try: try:
return int(await redis.execute("LLEN", f"chats/{chat_id}/unread/{user_slug}")) unread = await redis.execute("LLEN", f"chats/{chat_id}/unread/{user_slug}")
if unread:
return unread
except Exception: except Exception:
return 0 return 0
@ -55,7 +58,7 @@ async def load_chats(_, info, offset: int, amount: int):
if not chats: if not chats:
chats = [] chats = []
for c in chats: for c in chats:
c['messages'] = await load_messages(c['id']) c['messages'] = await load_messages(c['id'], offset, amount)
c['unread'] = await get_unread_counter(c['id'], user.slug) c['unread'] = await get_unread_counter(c['id'], user.slug)
return { return {
"chats": chats, "chats": chats,

View File

@ -1,4 +1,5 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from sqlalchemy import and_, desc, select, text from sqlalchemy import and_, desc, select, text
from sqlalchemy.orm import selectinload from sqlalchemy.orm import selectinload

View File

@ -255,8 +255,6 @@ type Query {
userFollowers(slug: String!): [Author]! userFollowers(slug: String!): [Author]!
userFollowedAuthors(slug: String!): [Author]! userFollowedAuthors(slug: String!): [Author]!
userFollowedTopics(slug: String!): [Topic]! userFollowedTopics(slug: String!): [Topic]!
authorsAll: [Author]! authorsAll: [Author]!
getAuthor(slug: String!): User! getAuthor(slug: String!): User!

View File

@ -1,7 +1,9 @@
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
import asyncio import asyncio
import json import json
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
from base.redis import redis from base.redis import redis
from services.zine.topics import TopicStorage from services.zine.topics import TopicStorage