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,34 +40,34 @@ 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:
return {"error": "author is not found"} return {"error": "author is not found"}
follows = await update_follows_for_author(follower, 'author', author, True) follows = await update_follows_for_author(follower, 'author', author, True)
_followers = await update_followers_for_author(follower, author, True) _followers = await update_followers_for_author(follower, author, True)
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"}
follows = await update_follows_for_author(follower, 'topic', topic, True) follows = await update_follows_for_author(follower, 'topic', topic, True)
elif what == 'COMMUNITY': elif what == 'COMMUNITY':
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"}
follows = await update_follows_for_author(follower, 'shout', shout, True) follows = await update_follows_for_author(follower, 'shout', shout, True)
return {f'{what.lower()}s': follows, "error": error} return {f'{what.lower()}s': follows, "error": error}
@ -86,36 +86,37 @@ 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:
return {"error": "cant find author"} return {"error": "cant find author"}
_followers = await update_followers_for_author(follower, author, False) _followers = await update_followers_for_author(follower, author, False)
await notify_follower(follower.dict(), author.id, 'unfollow') await notify_follower(follower.dict(), author.id, 'unfollow')
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:
return {"error": "cant find topic"} return {"error": "cant find topic"}
follows = await update_follows_for_author(follower, 'topic', topic, False) follows = await update_follows_for_author(follower, 'topic', topic, False)
elif what == 'COMMUNITY': elif what == 'COMMUNITY':
follows = local_session().execute(select(Community)) follows = local_session().execute(select(Community))
elif what == 'SHOUT': elif what == 'SHOUT':
logger.info(f'@{follower.slug} unfollowing §{slug}') error = reactions_unfollow(follower.id, slug)
[shout] = local_session().execute(select(Shout).where(Shout.slug == slug)) if not error:
if not shout: logger.info(f'@{follower.slug} unfollowing §{slug}')
return {"error": "cant find shout"} [shout] = local_session().execute(select(Shout).where(Shout.slug == slug))
if not reactions_unfollow(follower.id, slug): if not shout:
return {"error": "cant unfollow shout"} return {"error": "cant find shout"}
follows = await update_follows_for_author(follower, 'shout', shout, False) if not error:
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,29 +218,30 @@ 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):
with local_session() as session: try:
flw = ( with local_session() as session:
session.query(AuthorFollower) flw = (
.join(Author, Author.id == AuthorFollower.author) session.query(AuthorFollower)
.filter(and_(AuthorFollower.follower == follower_id, Author.slug == slug)) .join(Author, Author.id == AuthorFollower.author)
.first() .filter(and_(AuthorFollower.follower == follower_id, Author.slug == slug))
) .first()
if flw: )
session.delete(flw) if flw:
session.commit() session.delete(flw)
return True session.commit()
return False return None
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')