handle-no-userid

This commit is contained in:
Untone 2024-03-11 11:56:14 +03:00
parent bf1068d070
commit 6243c27390

View File

@ -77,6 +77,8 @@ async def unfollow(_, info, what, slug):
follows = None follows = None
try: try:
user_id = info.context.get('user_id') user_id = info.context.get('user_id')
if not user_id:
return {"error": "unauthorized"}
follower_query = select(Author).filter(Author.user == user_id) follower_query = select(Author).filter(Author.user == user_id)
[follower] = get_with_stat(follower_query) [follower] = get_with_stat(follower_query)
if follower: if follower:
@ -111,28 +113,38 @@ async def unfollow(_, info, what, slug):
async def get_follows_by_user_id(user_id: str): async def get_follows_by_user_id(user_id: str):
if user_id: if not user_id:
author = await redis.execute('GET', f'user:{user_id}:author') return {"error": "unauthorized"}
follows = DEFAULT_FOLLOWS author = await redis.execute('GET', f'user:{user_id}:author')
day_old = int(time.time()) - author.get('last_seen', 0) > 24 * 60 * 60 if isinstance(author, str):
if day_old: author = json.loads(author)
author_id = json.loads(str(author)).get('id') if not author:
if author_id: with local_session() as session:
topics = author_follows_topics(author_id) author = session.query(Author).filter(Author.user == user_id).first()
authors = author_follows_authors(author_id) if not author:
follows = { return {"error": "cant find author"}
'topics': topics, author = author.dict()
'authors': authors, last_seen = author.get('last_seen', 0) if isinstance(author, dict) else 0
'communities': [ follows = DEFAULT_FOLLOWS
{'id': 1, 'name': 'Дискурс', 'slug': 'discours', 'pic': ''} day_old = int(time.time()) - last_seen > 24 * 60 * 60
], if day_old:
} author_id = json.loads(str(author)).get('id')
else: if author_id:
logger.debug(f'getting follows for {user_id} from redis') topics = author_follows_topics(author_id)
res = await redis.execute('GET', f'user:{user_id}:follows') authors = author_follows_authors(author_id)
if isinstance(res, str): follows = {
follows = json.loads(res) 'topics': topics,
return follows 'authors': authors,
'communities': [
{'id': 1, 'name': 'Дискурс', 'slug': 'discours', 'pic': ''}
],
}
else:
logger.debug(f'getting follows for {user_id} from redis')
res = await redis.execute('GET', f'user:{user_id}:follows')
if isinstance(res, str):
follows = json.loads(res)
return follows
def reactions_follow(author_id, shout_id, auto=False): def reactions_follow(author_id, shout_id, auto=False):