core/resolvers/topics.py

109 lines
3.1 KiB
Python
Raw Normal View History

2021-12-12 13:00:38 +00:00
from orm import Topic, TopicSubscription, TopicStorage, Shout, User
2021-12-13 18:05:00 +00:00
from orm.shout import TopicStat, ShoutAuthorStorage
from orm.user import UserStorage
2021-10-28 10:42:34 +00:00
from orm.base import local_session
from resolvers.base import mutation, query, subscription
2021-11-10 08:42:29 +00:00
from resolvers.zine import ShoutSubscriptions
2021-10-28 10:42:34 +00:00
from auth.authenticate import login_required
import asyncio
2022-01-30 08:50:29 +00:00
from sqlalchemy import func, and_
2021-10-28 10:42:34 +00:00
@query.field("topicsBySlugs")
2021-12-12 13:00:38 +00:00
async def topics_by_slugs(_, info, slugs = None):
2021-10-28 10:42:34 +00:00
with local_session() as session:
2021-12-13 16:51:01 +00:00
topics = await TopicStorage.get_topics(slugs)
all_fields = [node.name.value for node in info.field_nodes[0].selection_set.selections]
if "topicStat" in all_fields:
for topic in topics:
topic.topicStat = await TopicStat.get_stat(topic.slug)
return topics
2021-10-28 10:42:34 +00:00
@query.field("topicsByCommunity")
async def topics_by_community(_, info, community):
with local_session() as session:
2021-12-12 13:00:38 +00:00
return await TopicStorage.get_topics_by_community(community)
2021-10-28 10:42:34 +00:00
@query.field("topicsByAuthor")
async def topics_by_author(_, info, author):
2021-12-12 13:00:38 +00:00
slugs = set()
2021-10-28 10:42:34 +00:00
with local_session() as session:
2021-12-11 12:17:59 +00:00
shouts = session.query(Shout).\
filter(Shout.authors.any(User.slug == author))
for shout in shouts:
2021-12-12 13:00:38 +00:00
slugs.update([topic.slug for topic in shout.topics])
return await TopicStorage.get_topics(slugs)
2021-10-28 10:42:34 +00:00
@mutation.field("createTopic")
@login_required
async def create_topic(_, info, input):
new_topic = Topic.create(**input)
await TopicStorage.add_topic(new_topic)
return { "topic" : new_topic }
@mutation.field("updateTopic")
@login_required
async def update_topic(_, info, input):
slug = input["slug"]
session = local_session()
topic = session.query(Topic).filter(Topic.slug == slug).first()
if not topic:
return { "error" : "topic not found" }
topic.update(input)
session.commit()
session.close()
await TopicStorage.add_topic(topic)
return { "topic" : topic }
2021-10-28 10:42:34 +00:00
@mutation.field("topicSubscribe")
@login_required
async def topic_subscribe(_, info, slug):
2022-01-30 08:50:29 +00:00
user = info.context["request"].user
TopicSubscription.create(
subscriber = user.slug,
topic = slug)
return {}
2021-10-28 10:42:34 +00:00
@mutation.field("topicUnsubscribe")
@login_required
async def topic_unsubscribe(_, info, slug):
2022-01-30 08:50:29 +00:00
user = info.context["request"].user
2021-10-28 10:42:34 +00:00
with local_session() as session:
2022-01-30 08:50:29 +00:00
sub = session.query(TopicSubscription).\
filter(and_(TopicSubscription.subscriber == user.slug, TopicSubscription.topic == slug)).\
first()
if not sub:
return { "error" : "subscription not exist" }
2021-10-28 10:42:34 +00:00
session.delete(sub)
2022-01-30 08:50:29 +00:00
session.commit()
return {}
2021-11-04 16:37:41 +00:00
@subscription.source("topicUpdated")
2022-01-30 08:50:29 +00:00
async def new_shout_generator(obj, info, user_slug):
2021-11-10 08:42:29 +00:00
try:
with local_session() as session:
2022-01-30 08:50:29 +00:00
topics = session.query(TopicSubscription.topic).filter(TopicSubscription.subscriber == user_slug).all()
2021-11-10 08:42:29 +00:00
topics = set([item.topic for item in topics])
shouts_queue = asyncio.Queue()
await ShoutSubscriptions.register_subscription(shouts_queue)
while True:
shout = await shouts_queue.get()
if topics.intersection(set(shout.topic_slugs)):
2021-11-10 08:42:29 +00:00
yield shout
finally:
await ShoutSubscriptions.del_subscription(shouts_queue)
2021-11-04 16:37:41 +00:00
@subscription.field("topicUpdated")
def shout_resolver(shout, info, user_id):
return shout