Feature/notifications (#77)
feature - notifications Co-authored-by: Igor Lobanov <igor.lobanov@onetwotrip.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import asyncio
|
||||
from base.orm import local_session
|
||||
from base.resolvers import mutation, subscription
|
||||
from base.resolvers import mutation
|
||||
from auth.authenticate import login_required
|
||||
from auth.credentials import AuthCredentials
|
||||
# from resolvers.community import community_follow, community_unfollow
|
||||
@@ -69,79 +69,3 @@ async def unfollow(_, info, what, slug):
|
||||
return {"error": str(e)}
|
||||
|
||||
return {}
|
||||
|
||||
|
||||
# by author and by topic
|
||||
@subscription.source("newShout")
|
||||
@login_required
|
||||
async def shout_generator(_, info: GraphQLResolveInfo):
|
||||
print(f"[resolvers.zine] shouts generator {info}")
|
||||
auth: AuthCredentials = info.context["request"].auth
|
||||
user_id = auth.user_id
|
||||
try:
|
||||
tasks = []
|
||||
|
||||
with local_session() as session:
|
||||
|
||||
# notify new shout by followed authors
|
||||
following_topics = session.query(TopicFollower).where(TopicFollower.follower == user_id).all()
|
||||
|
||||
for topic_id in following_topics:
|
||||
following_topic = Following('topic', topic_id)
|
||||
await FollowingManager.register('topic', following_topic)
|
||||
following_topic_task = following_topic.queue.get()
|
||||
tasks.append(following_topic_task)
|
||||
|
||||
# by followed topics
|
||||
following_authors = session.query(AuthorFollower).where(
|
||||
AuthorFollower.follower == user_id).all()
|
||||
|
||||
for author_id in following_authors:
|
||||
following_author = Following('author', author_id)
|
||||
await FollowingManager.register('author', following_author)
|
||||
following_author_task = following_author.queue.get()
|
||||
tasks.append(following_author_task)
|
||||
|
||||
# TODO: use communities
|
||||
# by followed communities
|
||||
# following_communities = session.query(CommunityFollower).where(
|
||||
# CommunityFollower.follower == user_id).all()
|
||||
|
||||
# for community_id in following_communities:
|
||||
# following_community = Following('community', author_id)
|
||||
# await FollowingManager.register('community', following_community)
|
||||
# following_community_task = following_community.queue.get()
|
||||
# tasks.append(following_community_task)
|
||||
|
||||
while True:
|
||||
shout = await asyncio.gather(*tasks)
|
||||
yield shout
|
||||
finally:
|
||||
pass
|
||||
|
||||
|
||||
@subscription.source("newReaction")
|
||||
@login_required
|
||||
async def reaction_generator(_, info):
|
||||
print(f"[resolvers.zine] reactions generator {info}")
|
||||
auth: AuthCredentials = info.context["request"].auth
|
||||
user_id = auth.user_id
|
||||
try:
|
||||
with local_session() as session:
|
||||
followings = session.query(ShoutReactionsFollower.shout).where(
|
||||
ShoutReactionsFollower.follower == user_id).unique()
|
||||
|
||||
# notify new reaction
|
||||
|
||||
tasks = []
|
||||
for shout_id in followings:
|
||||
following_shout = Following('shout', shout_id)
|
||||
await FollowingManager.register('shout', following_shout)
|
||||
following_author_task = following_shout.queue.get()
|
||||
tasks.append(following_author_task)
|
||||
|
||||
while True:
|
||||
reaction = await asyncio.gather(*tasks)
|
||||
yield reaction
|
||||
finally:
|
||||
pass
|
||||
|
@@ -183,6 +183,7 @@ async def load_shouts_by(_, info, options):
|
||||
|
||||
|
||||
@query.field("loadDrafts")
|
||||
@login_required
|
||||
async def get_drafts(_, info):
|
||||
auth: AuthCredentials = info.context["request"].auth
|
||||
user_id = auth.user_id
|
||||
|
@@ -10,6 +10,7 @@ from base.resolvers import mutation, query
|
||||
from orm.reaction import Reaction, ReactionKind
|
||||
from orm.shout import Shout, ShoutReactionsFollower
|
||||
from orm.user import User
|
||||
from services.notifications.notification_service import notification_service
|
||||
|
||||
|
||||
def add_reaction_stat_columns(q):
|
||||
@@ -198,29 +199,32 @@ async def create_reaction(_, info, reaction):
|
||||
|
||||
r = Reaction.create(**reaction)
|
||||
|
||||
# Proposal accepting logix
|
||||
if r.replyTo is not None and \
|
||||
r.kind == ReactionKind.ACCEPT and \
|
||||
auth.user_id in shout.dict()['authors']:
|
||||
replied_reaction = session.query(Reaction).where(Reaction.id == r.replyTo).first()
|
||||
if replied_reaction and replied_reaction.kind == ReactionKind.PROPOSE:
|
||||
if replied_reaction.range:
|
||||
old_body = shout.body
|
||||
start, end = replied_reaction.range.split(':')
|
||||
start = int(start)
|
||||
end = int(end)
|
||||
new_body = old_body[:start] + replied_reaction.body + old_body[end:]
|
||||
shout.body = new_body
|
||||
# TODO: update git version control
|
||||
# # Proposal accepting logix
|
||||
# FIXME: will break if there will be 2 proposals, will break if shout will be changed
|
||||
# if r.replyTo is not None and \
|
||||
# r.kind == ReactionKind.ACCEPT and \
|
||||
# auth.user_id in shout.dict()['authors']:
|
||||
# replied_reaction = session.query(Reaction).where(Reaction.id == r.replyTo).first()
|
||||
# if replied_reaction and replied_reaction.kind == ReactionKind.PROPOSE:
|
||||
# if replied_reaction.range:
|
||||
# old_body = shout.body
|
||||
# start, end = replied_reaction.range.split(':')
|
||||
# start = int(start)
|
||||
# end = int(end)
|
||||
# new_body = old_body[:start] + replied_reaction.body + old_body[end:]
|
||||
# shout.body = new_body
|
||||
# # TODO: update git version control
|
||||
|
||||
session.add(r)
|
||||
session.commit()
|
||||
|
||||
await notification_service.handle_new_reaction(r.id)
|
||||
|
||||
rdict = r.dict()
|
||||
rdict['shout'] = shout.dict()
|
||||
rdict['createdBy'] = author.dict()
|
||||
|
||||
# self-regulation mechanics
|
||||
|
||||
if check_to_hide(session, auth.user_id, r):
|
||||
set_hidden(session, r.shout)
|
||||
elif check_to_publish(session, auth.user_id, r):
|
||||
|
Reference in New Issue
Block a user