core/resolvers/reactions.py

159 lines
5.0 KiB
Python
Raw Normal View History

from datetime import datetime
2022-09-22 10:31:44 +00:00
from sqlalchemy import desc, and_
from auth.authenticate import login_required
2022-08-11 05:53:14 +00:00
from base.orm import local_session
from base.resolvers import mutation, query
from orm.reaction import Reaction
2022-08-11 05:53:14 +00:00
from orm.shout import ShoutReactionsFollower
from orm.user import User
from services.auth.users import UserStorage
from services.stat.reacted import ReactedStorage
2022-09-19 13:50:43 +00:00
from services.stat.viewed import ViewedStorage
async def get_reaction_stat(reaction_id):
return {
"viewed": await ViewedStorage.get_reaction(reaction_id),
"reacted": len(await ReactedStorage.get_reaction(reaction_id)),
"rating": await ReactedStorage.get_reaction_rating(reaction_id),
"commented": len(await ReactedStorage.get_reaction_comments(reaction_id)),
}
2022-09-03 10:50:14 +00:00
2022-09-22 10:31:44 +00:00
def reactions_follow(user: User, slug: str, auto=False):
with local_session() as session:
2022-09-18 14:29:21 +00:00
following = (
2022-09-03 10:50:14 +00:00
session.query(ShoutReactionsFollower)
2022-09-22 10:31:44 +00:00
.where(and_(
2022-09-03 10:50:14 +00:00
ShoutReactionsFollower.follower == user.slug,
2022-09-18 14:29:21 +00:00
ShoutReactionsFollower.shout == slug
2022-09-22 10:31:44 +00:00
))
2022-09-03 10:50:14 +00:00
.first()
)
2022-09-18 14:29:21 +00:00
if not following:
following = ShoutReactionsFollower.create(
follower=user.slug,
shout=slug,
auto=auto
)
session.add(following)
session.commit()
def reactions_unfollow(user, slug):
with local_session() as session:
2022-09-03 10:50:14 +00:00
following = (
session.query(ShoutReactionsFollower)
2022-09-22 10:31:44 +00:00
.where(and_(
2022-09-03 10:50:14 +00:00
ShoutReactionsFollower.follower == user.slug,
2022-09-22 10:31:44 +00:00
ShoutReactionsFollower.shout == slug
))
2022-09-03 10:50:14 +00:00
.first()
)
2022-09-18 14:29:21 +00:00
if following:
session.delete(following)
2022-09-18 14:29:21 +00:00
session.commit()
@mutation.field("createReaction")
@login_required
async def create_reaction(_, info, inp):
user = info.context["request"].user
2022-09-01 10:16:22 +00:00
2022-09-07 16:19:06 +00:00
# TODO: filter allowed for post reaction kinds
2022-09-01 10:16:22 +00:00
2022-10-14 09:25:45 +00:00
with local_session() as session:
reaction = Reaction.create(**inp)
session.add(reaction)
session.commit()
2022-09-07 16:19:06 +00:00
ReactedStorage.react(reaction)
try:
2022-09-03 10:50:14 +00:00
reactions_follow(user, inp["shout"], True)
except Exception as e:
print(f"[resolvers.reactions] error on reactions autofollowing: {e}")
2022-09-19 13:50:43 +00:00
reaction.stat = await get_reaction_stat(reaction.id)
return {"reaction": reaction}
@mutation.field("updateReaction")
@login_required
async def update_reaction(_, info, inp):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
2022-09-22 10:31:44 +00:00
user = session.query(User).where(User.id == user_id).first()
2022-09-14 13:12:25 +00:00
reaction = session.query(Reaction).filter(Reaction.id == inp.id).first()
if not reaction:
return {"error": "invalid reaction id"}
if reaction.createdBy != user.slug:
return {"error": "access denied"}
2022-09-03 10:50:14 +00:00
reaction.body = inp["body"]
reaction.updatedAt = datetime.now()
2022-09-03 10:50:14 +00:00
if reaction.kind != inp["kind"]:
2022-08-11 11:22:10 +00:00
# NOTE: change mind detection can be here
pass
2022-09-03 10:50:14 +00:00
if inp.get("range"):
reaction.range = inp.get("range")
session.commit()
2022-09-19 13:50:43 +00:00
reaction.stat = await get_reaction_stat(reaction.id)
return {"reaction": reaction}
@mutation.field("deleteReaction")
@login_required
2022-09-14 13:10:38 +00:00
async def delete_reaction(_, info, rid):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
2022-09-22 10:31:44 +00:00
user = session.query(User).where(User.id == user_id).first()
2022-09-14 13:10:38 +00:00
reaction = session.query(Reaction).filter(Reaction.id == rid).first()
if not reaction:
return {"error": "invalid reaction id"}
if reaction.createdBy != user.slug:
return {"error": "access denied"}
reaction.deletedAt = datetime.now()
session.commit()
return {}
2022-09-03 10:50:14 +00:00
2022-09-04 17:20:38 +00:00
@query.field("reactionsForShouts")
2022-09-14 09:45:31 +00:00
async def get_reactions_for_shouts(_, info, shouts, offset, limit):
2022-09-01 10:16:22 +00:00
reactions = []
with local_session() as session:
2022-09-05 10:18:29 +00:00
for slug in shouts:
2022-09-03 10:50:14 +00:00
reactions += (
session.query(Reaction)
.filter(Reaction.shout == slug)
2022-09-22 10:31:44 +00:00
.where(Reaction.deletedAt.is_not(None))
2022-09-04 17:20:38 +00:00
.order_by(desc("createdAt"))
2022-09-03 10:50:14 +00:00
.offset(offset)
2022-09-14 13:10:38 +00:00
.limit(limit)
2022-09-03 10:50:14 +00:00
.all()
)
2022-09-01 10:16:22 +00:00
for r in reactions:
2022-09-19 13:50:43 +00:00
r.stat = await get_reaction_stat(r.id)
2022-09-03 10:50:14 +00:00
r.createdBy = await UserStorage.get_user(r.createdBy or "discours")
2022-09-01 10:16:22 +00:00
return reactions
@query.field("reactionsByAuthor")
2022-09-14 13:12:25 +00:00
async def get_reactions_by_author(_, info, slug, limit=50, offset=0):
reactions = []
with local_session() as session:
2022-09-03 10:50:14 +00:00
reactions = (
session.query(Reaction)
2022-09-22 10:31:44 +00:00
.where(Reaction.createdBy == slug)
2022-09-14 13:12:25 +00:00
.limit(limit)
2022-09-03 10:50:14 +00:00
.offset(offset)
)
2022-08-18 06:12:46 +00:00
for r in reactions:
2022-09-19 13:50:43 +00:00
r.stat = await get_reaction_stat(r.id)
2022-09-03 10:50:14 +00:00
r.createdBy = await UserStorage.get_user(r.createdBy or "discours")
2022-09-01 10:16:22 +00:00
return reactions