refactored

This commit is contained in:
Untone 2024-03-12 10:35:33 +03:00
parent 0e1df1e7ca
commit a7944f5176
3 changed files with 35 additions and 39 deletions

View File

@ -12,7 +12,6 @@ from orm.community import Community
from orm.reaction import Reaction from orm.reaction import Reaction
from orm.shout import Shout, ShoutReactionsFollower from orm.shout import Shout, ShoutReactionsFollower
from orm.topic import Topic, TopicFollower from orm.topic import Topic, TopicFollower
from resolvers.topic import topic_unfollow, topic_follow
from resolvers.stat import get_with_stat, author_follows_topics, author_follows_authors from resolvers.stat import get_with_stat, author_follows_topics, author_follows_authors
from services.auth import login_required from services.auth import login_required
from services.db import local_session from services.db import local_session
@ -156,6 +155,35 @@ async def get_follows_by_user_id(user_id: str):
return follows return follows
def topic_follow(follower_id, slug):
try:
with local_session() as session:
topic = session.query(Topic).where(Topic.slug == slug).one()
_following = TopicFollower(topic=topic.id, follower=follower_id)
return None
except Exception as exc:
logger.error(exc)
return exc
def topic_unfollow(follower_id, slug):
try:
with local_session() as session:
sub = (
session.query(TopicFollower)
.join(Topic)
.filter(and_(TopicFollower.follower == follower_id, Topic.slug == slug))
.first()
)
if sub:
session.delete(sub)
session.commit()
return None
except Exception as ex:
logger.debug(ex)
return ex
def reactions_follow(author_id, shout_id, auto=False): def reactions_follow(author_id, shout_id, auto=False):
try: try:
with local_session() as session: with local_session() as session:

View File

@ -1,13 +1,12 @@
from sqlalchemy import and_, distinct, func, select from sqlalchemy import distinct, func, select
from orm.author import Author from orm.author import Author
from orm.shout import ShoutTopic from orm.shout import ShoutTopic
from orm.topic import Topic, TopicFollower from orm.topic import Topic
from resolvers.stat import get_with_stat from resolvers.stat import get_with_stat
from services.auth import login_required from services.auth import login_required
from services.db import local_session from services.db import local_session
from services.schema import mutation, query from services.schema import mutation, query
from services.logger import root_logger as logger
@query.field('get_topics_all') @query.field('get_topics_all')
@ -91,35 +90,6 @@ async def delete_topic(_, info, slug: str):
return {'error': 'access denied'} return {'error': 'access denied'}
def topic_follow(follower_id, slug):
try:
with local_session() as session:
topic = session.query(Topic).where(Topic.slug == slug).one()
_following = TopicFollower(topic=topic.id, follower=follower_id)
return None
except Exception as exc:
logger.error(exc)
return exc
def topic_unfollow(follower_id, slug):
try:
with local_session() as session:
sub = (
session.query(TopicFollower)
.join(Topic)
.filter(and_(TopicFollower.follower == follower_id, Topic.slug == slug))
.first()
)
if sub:
session.delete(sub)
session.commit()
return None
except Exception as ex:
logger.debug(ex)
return ex
@query.field('get_topics_random') @query.field('get_topics_random')
def get_topics_random(_, _info, amount=12): def get_topics_random(_, _info, amount=12):
q = select(Topic) q = select(Topic)

View File

@ -166,21 +166,21 @@ def after_author_update(mapper, connection, author: Author):
@event.listens_for(TopicFollower, 'after_insert') @event.listens_for(TopicFollower, 'after_insert')
def after_topic_follower_insert(mapper, connection, target: TopicFollower): def after_topic_follower_insert(mapper, connection, target: TopicFollower):
asyncio.create_task( asyncio.create_task(
handle_topic_follower_change(connection, target.topic, target.follower, True) handle_topic_follower_change(target.topic, target.follower, True)
) )
@event.listens_for(TopicFollower, 'after_delete') @event.listens_for(TopicFollower, 'after_delete')
def after_topic_follower_delete(mapper, connection, target: TopicFollower): def after_topic_follower_delete(mapper, connection, target: TopicFollower):
asyncio.create_task( asyncio.create_task(
handle_topic_follower_change(connection, target.topic, target.follower, False) handle_topic_follower_change(target.topic, target.follower, False)
) )
@event.listens_for(AuthorFollower, 'after_insert') @event.listens_for(AuthorFollower, 'after_insert')
def after_author_follower_insert(mapper, connection, target: AuthorFollower): def after_author_follower_insert(mapper, connection, target: AuthorFollower):
asyncio.create_task( asyncio.create_task(
handle_author_follower_change(connection, target.author, target.follower, True) handle_author_follower_change(target.author, target.follower, True)
) )
@ -224,9 +224,7 @@ async def handle_author_follower_change(
) )
async def handle_topic_follower_change( async def handle_topic_follower_change(topic_id: int, follower_id: int, is_insert: bool):
connection, topic_id: int, follower_id: int, is_insert: bool
):
topic_query = select(Topic).filter(Topic.id == topic_id) topic_query = select(Topic).filter(Topic.id == topic_id)
[topic] = get_with_stat(topic_query) [topic] = get_with_stat(topic_query)
follower_query = select(Author).filter(Author.id == follower_id) follower_query = select(Author).filter(Author.id == follower_id)