return-error-on-follow

This commit is contained in:
Untone 2024-03-11 16:52:16 +03:00
parent ebf08ea2ed
commit e6f5cfcb8d
2 changed files with 70 additions and 69 deletions

View File

@ -40,8 +40,8 @@ async def follow(_, info, what, slug):
return {"error": "cant find follower"} return {"error": "cant find follower"}
if what == 'AUTHOR': if what == 'AUTHOR':
if not author_follow(follower.id, slug): error = author_follow(follower.id, slug)
return {"error": "cant follow author"} if not error:
logger.debug(f'@{follower.slug} followed @{slug}') logger.debug(f'@{follower.slug} followed @{slug}')
[author] = get_with_stat(select(Author).select_from(Author).where(Author.slug == slug)) [author] = get_with_stat(select(Author).select_from(Author).where(Author.slug == slug))
if not author: if not author:
@ -51,8 +51,8 @@ async def follow(_, info, what, slug):
await notify_follower(follower.dict(), author.id, 'unfollow') await notify_follower(follower.dict(), author.id, 'unfollow')
elif what == 'TOPIC': elif what == 'TOPIC':
if not topic_follow(follower.id, slug): error = topic_follow(follower.id, slug)
return {"error": "cant follow topic"} if not error:
[topic] = get_with_stat(select(Topic).where(Topic.slug == slug)) [topic] = get_with_stat(select(Topic).where(Topic.slug == slug))
if not topic: if not topic:
return {"error": "topic is not found"} return {"error": "topic is not found"}
@ -62,8 +62,8 @@ async def follow(_, info, what, slug):
follows = local_session().execute(select(Community)) follows = local_session().execute(select(Community))
elif what == 'SHOUT': elif what == 'SHOUT':
if not reactions_follow(follower.id, slug): error = reactions_follow(follower.id, slug)
return {"error": "cant follow shout"} if not error:
[shout] = local_session().execute(select(Shout).where(Shout.slug == slug)) [shout] = local_session().execute(select(Shout).where(Shout.slug == slug))
if not shout: if not shout:
return {"error": "cant find shout"} return {"error": "cant find shout"}
@ -86,8 +86,8 @@ async def unfollow(_, info, what, slug):
return {"error": "follower profile is not found"} return {"error": "follower profile is not found"}
if what == 'AUTHOR': if what == 'AUTHOR':
if not author_unfollow(follower.id, slug): error = author_unfollow(follower.id, slug)
return {"error": "cant unfollow author"} if not error:
logger.info(f'@{follower.slug} unfollowing @{slug}') logger.info(f'@{follower.slug} unfollowing @{slug}')
[author] = get_with_stat(select(Author).where(Author.slug == slug)) [author] = get_with_stat(select(Author).where(Author.slug == slug))
if not author: if not author:
@ -97,8 +97,8 @@ async def unfollow(_, info, what, slug):
follows = await update_follows_for_author(follower, 'author', author, False) follows = await update_follows_for_author(follower, 'author', author, False)
elif what == 'TOPIC': elif what == 'TOPIC':
if not topic_unfollow(follower.id, slug): error = topic_unfollow(follower.id, slug)
return {"error": "cant unfollow topic"} if not error:
logger.info(f'@{follower.slug} unfollowing §{slug}') logger.info(f'@{follower.slug} unfollowing §{slug}')
[topic] = get_with_stat(select(Topic).where(Topic.slug == slug)) [topic] = get_with_stat(select(Topic).where(Topic.slug == slug))
if not topic: if not topic:
@ -109,12 +109,13 @@ async def unfollow(_, info, what, slug):
follows = local_session().execute(select(Community)) follows = local_session().execute(select(Community))
elif what == 'SHOUT': elif what == 'SHOUT':
error = reactions_unfollow(follower.id, slug)
if not error:
logger.info(f'@{follower.slug} unfollowing §{slug}') logger.info(f'@{follower.slug} unfollowing §{slug}')
[shout] = local_session().execute(select(Shout).where(Shout.slug == slug)) [shout] = local_session().execute(select(Shout).where(Shout.slug == slug))
if not shout: if not shout:
return {"error": "cant find shout"} return {"error": "cant find shout"}
if not reactions_unfollow(follower.id, slug): if not error:
return {"error": "cant unfollow shout"}
follows = await update_follows_for_author(follower, 'shout', shout, False) follows = await update_follows_for_author(follower, 'shout', shout, False)
return {'error': error, f'{what.lower()}s': follows} return {'error': error, f'{what.lower()}s': follows}
@ -177,9 +178,9 @@ def reactions_follow(author_id, shout_id, auto=False):
) )
session.add(following) session.add(following)
session.commit() session.commit()
return True return None
except Exception: except Exception as exc:
return False return exc
def reactions_unfollow(author_id, shout_id: int): def reactions_unfollow(author_id, shout_id: int):
@ -201,13 +202,12 @@ def reactions_unfollow(author_id, shout_id: int):
if following: if following:
session.delete(following) session.delete(following)
session.commit() session.commit()
return True return None
except Exception as ex: except Exception as ex:
logger.debug(ex)
import traceback import traceback
traceback.print_exc() traceback.print_exc()
return False return ex
# for mutation.field("follow") # for mutation.field("follow")
@ -218,17 +218,17 @@ def author_follow(follower_id, slug):
af = AuthorFollower(follower=follower_id, author=author.id) af = AuthorFollower(follower=follower_id, author=author.id)
session.add(af) session.add(af)
session.commit() session.commit()
return True return None
except Exception as exc: except Exception as exc:
logger.error(exc)
import traceback import traceback
traceback.print_exc() traceback.print_exc()
return False return exc
# for mutation.field("unfollow") # for mutation.field("unfollow")
def author_unfollow(follower_id, slug): def author_unfollow(follower_id, slug):
try:
with local_session() as session: with local_session() as session:
flw = ( flw = (
session.query(AuthorFollower) session.query(AuthorFollower)
@ -239,8 +239,9 @@ def author_unfollow(follower_id, slug):
if flw: if flw:
session.delete(flw) session.delete(flw)
session.commit() session.commit()
return True return None
return False except Exception as exc:
return exc
@query.field('get_topic_followers') @query.field('get_topic_followers')

View File

@ -96,10 +96,10 @@ def topic_follow(follower_id, slug):
with local_session() as session: with local_session() as session:
topic = session.query(Topic).where(Topic.slug == slug).one() topic = session.query(Topic).where(Topic.slug == slug).one()
_following = TopicFollower(topic=topic.id, follower=follower_id) _following = TopicFollower(topic=topic.id, follower=follower_id)
return True return None
except Exception as exc: except Exception as exc:
logger.error(exc) logger.error(exc)
return False return exc
def topic_unfollow(follower_id, slug): def topic_unfollow(follower_id, slug):
@ -114,10 +114,10 @@ def topic_unfollow(follower_id, slug):
if sub: if sub:
session.delete(sub) session.delete(sub)
session.commit() session.commit()
return True return None
except Exception as ex: except Exception as ex:
logger.debug(ex) logger.debug(ex)
return False return ex
@query.field('get_topics_random') @query.field('get_topics_random')