2022-08-13 09:48:07 +00:00
|
|
|
from sqlalchemy import desc
|
2022-08-18 06:12:46 +00:00
|
|
|
from sqlalchemy.orm import joinedload, selectinload
|
2022-07-21 11:58:50 +00:00
|
|
|
from orm.reaction import Reaction
|
2022-08-11 05:53:14 +00:00
|
|
|
from base.orm import local_session
|
|
|
|
from orm.shout import ShoutReactionsFollower
|
2022-07-21 11:58:50 +00:00
|
|
|
from orm.user import User
|
2022-08-11 05:53:14 +00:00
|
|
|
from base.resolvers import mutation, query
|
2022-07-21 11:58:50 +00:00
|
|
|
from auth.authenticate import login_required
|
|
|
|
from datetime import datetime
|
2022-08-14 12:48:35 +00:00
|
|
|
from services.auth.users import UserStorage
|
2022-08-13 09:48:07 +00:00
|
|
|
from services.stat.reacted import ReactedStorage
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
def reactions_follow(user, slug, auto=False):
|
|
|
|
with local_session() as session:
|
2022-09-03 10:50:14 +00:00
|
|
|
fw = (
|
|
|
|
session.query(ShoutReactionsFollower)
|
|
|
|
.filter(
|
|
|
|
ShoutReactionsFollower.follower == user.slug,
|
|
|
|
ShoutReactionsFollower.shout == slug,
|
|
|
|
)
|
|
|
|
.first()
|
|
|
|
)
|
2022-07-21 11:58:50 +00:00
|
|
|
if auto and fw:
|
|
|
|
return
|
|
|
|
elif not auto and fw:
|
|
|
|
if not fw.deletedAt is None:
|
|
|
|
fw.deletedAt = None
|
|
|
|
fw.auto = False
|
|
|
|
session.commit()
|
|
|
|
return
|
|
|
|
# print("[resolvers.reactions] was followed before")
|
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
ShoutReactionsFollower.create(follower=user.slug, shout=slug, auto=auto)
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def reactions_unfollow(user, slug):
|
|
|
|
with local_session() as session:
|
2022-09-03 10:50:14 +00:00
|
|
|
following = (
|
|
|
|
session.query(ShoutReactionsFollower)
|
|
|
|
.filter(
|
|
|
|
ShoutReactionsFollower.follower == user.slug,
|
|
|
|
ShoutReactionsFollower.shout == slug,
|
|
|
|
)
|
|
|
|
.first()
|
|
|
|
)
|
2022-07-21 11:58:50 +00:00
|
|
|
if not following:
|
|
|
|
# print("[resolvers.reactions] was not followed", slug)
|
|
|
|
return
|
|
|
|
if following.auto:
|
|
|
|
following.deletedAt = datetime.now()
|
|
|
|
else:
|
|
|
|
session.delete(following)
|
|
|
|
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-08-13 09:48:07 +00:00
|
|
|
# TODO: filter allowed reaction kinds
|
2022-09-01 10:16:22 +00:00
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
reaction = Reaction.create(**inp)
|
2022-08-13 09:48:07 +00:00
|
|
|
ReactedStorage.increment(reaction.shout, reaction.replyTo)
|
2022-07-21 11:58:50 +00:00
|
|
|
try:
|
2022-09-03 10:50:14 +00:00
|
|
|
reactions_follow(user, inp["shout"], True)
|
2022-07-21 11:58:50 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(f"[resolvers.reactions] error on reactions autofollowing: {e}")
|
|
|
|
|
|
|
|
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:
|
|
|
|
user = session.query(User).filter(User.id == user_id).first()
|
|
|
|
reaction = session.query(Reaction).filter(Reaction.id == 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"]
|
2022-07-21 11:58:50 +00:00
|
|
|
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
|
2022-07-21 11:58:50 +00:00
|
|
|
pass
|
2022-09-03 10:50:14 +00:00
|
|
|
if inp.get("range"):
|
|
|
|
reaction.range = inp.get("range")
|
2022-07-21 11:58:50 +00:00
|
|
|
session.commit()
|
|
|
|
|
|
|
|
return {"reaction": reaction}
|
|
|
|
|
|
|
|
|
|
|
|
@mutation.field("deleteReaction")
|
|
|
|
@login_required
|
|
|
|
async def delete_reaction(_, info, id):
|
|
|
|
auth = info.context["request"].auth
|
|
|
|
user_id = auth.user_id
|
|
|
|
with local_session() as session:
|
|
|
|
user = session.query(User).filter(User.id == user_id).first()
|
|
|
|
reaction = session.query(Reaction).filter(Reaction.id == id).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-07-21 11:58:50 +00:00
|
|
|
@query.field("reactionsByShout")
|
2022-08-14 12:48:35 +00:00
|
|
|
async def get_shout_reactions(_, info, slug, page, size):
|
2022-08-13 09:48:07 +00:00
|
|
|
offset = page * size
|
|
|
|
reactions = []
|
|
|
|
with local_session() as session:
|
2022-09-03 10:50:14 +00:00
|
|
|
reactions = (
|
|
|
|
session.query(Reaction)
|
|
|
|
.filter(Reaction.shout == slug)
|
|
|
|
.limit(size)
|
|
|
|
.offset(offset)
|
|
|
|
.all()
|
|
|
|
)
|
2022-08-14 12:48:35 +00:00
|
|
|
for r in reactions:
|
2022-09-03 10:50:14 +00:00
|
|
|
r.createdBy = await UserStorage.get_user(r.createdBy or "discours")
|
2022-08-13 09:48:07 +00:00
|
|
|
return reactions
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-09-01 10:16:22 +00:00
|
|
|
@query.field("reactionsForSlugs")
|
|
|
|
async def get_shout_reactions(_, info, slugs, page, size):
|
|
|
|
offset = page * size
|
|
|
|
reactions = []
|
|
|
|
with local_session() as session:
|
2022-09-03 10:50:14 +00:00
|
|
|
for slug in slugs:
|
|
|
|
reactions += (
|
|
|
|
session.query(Reaction)
|
|
|
|
.filter(Reaction.shout == slug)
|
|
|
|
.limit(size)
|
|
|
|
.offset(offset)
|
|
|
|
.all()
|
|
|
|
)
|
2022-09-01 10:16:22 +00:00
|
|
|
for r in reactions:
|
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
|
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
|
|
|
|
@query.field("reactionsAll")
|
2022-08-18 06:12:46 +00:00
|
|
|
async def get_all_reactions(_, info, page=1, size=10):
|
2022-08-05 15:55:07 +00:00
|
|
|
offset = page * size
|
2022-08-13 09:48:07 +00:00
|
|
|
reactions = []
|
|
|
|
with local_session() as session:
|
2022-09-03 10:50:14 +00:00
|
|
|
reactions = (
|
|
|
|
session.query(Reaction)
|
|
|
|
.filter(Reaction.deletedAt == None)
|
|
|
|
.order_by(desc("createdAt"))
|
|
|
|
.offset(offset)
|
|
|
|
.limit(size)
|
|
|
|
)
|
2022-08-18 06:12:46 +00:00
|
|
|
for r in reactions:
|
2022-09-03 10:50:14 +00:00
|
|
|
r.createdBy = await UserStorage.get_user(r.createdBy or "discours")
|
2022-08-18 06:12:46 +00:00
|
|
|
reactions = list(reactions)
|
2022-08-13 09:48:07 +00:00
|
|
|
reactions.sort(key=lambda x: x.createdAt, reverse=True)
|
|
|
|
return reactions
|
2022-07-21 11:58:50 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
@query.field("reactionsByAuthor")
|
2022-08-18 06:12:46 +00:00
|
|
|
async def get_reactions_by_author(_, info, slug, page=1, size=50):
|
2022-08-05 15:55:07 +00:00
|
|
|
offset = page * size
|
2022-08-13 09:48:07 +00:00
|
|
|
reactions = []
|
|
|
|
with local_session() as session:
|
2022-09-03 10:50:14 +00:00
|
|
|
reactions = (
|
|
|
|
session.query(Reaction)
|
|
|
|
.filter(Reaction.createdBy == slug)
|
|
|
|
.limit(size)
|
|
|
|
.offset(offset)
|
|
|
|
)
|
2022-08-18 06:12:46 +00:00
|
|
|
for r in reactions:
|
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
|