0.2.21-ga

This commit is contained in:
2024-01-23 16:04:38 +03:00
parent 954e6dabb7
commit 3f65652a5f
9 changed files with 249 additions and 209 deletions

View File

@@ -64,7 +64,7 @@ async def author_followings(author_id: int):
"unread": await get_total_unread_counter(author_id),
"topics": [t.slug for t in await followed_topics(author_id)],
"authors": [a.slug for a in await followed_authors(author_id)],
"reactions": [s.slug for s in followed_reactions(author_id)],
"reactions": [s.slug for s in await followed_reactions(author_id)],
"communities": [c.slug for c in [followed_communities(author_id)] if isinstance(c, Community)],
}

View File

@@ -17,7 +17,7 @@ async def accept_invite(_, info, invite_id: int):
if author:
# Check if the invite exists
invite = session.query(Invite).filter(Invite.id == invite_id).first()
if invite and invite.author_id == author.id and invite.status == InviteStatus.PENDING.value:
if invite and invite.author_id is author.id and invite.status is InviteStatus.PENDING.value:
# Add the user to the shout authors
shout = session.query(Shout).filter(Shout.id == invite.shout_id).first()
if shout:
@@ -46,7 +46,7 @@ async def reject_invite(_, info, invite_id: int):
if author:
# Check if the invite exists
invite = session.query(Invite).filter(Invite.id == invite_id).first()
if invite and invite.author_id == author.id and invite.status == InviteStatus.PENDING.value:
if invite and invite.author_id is author.id and invite.status is InviteStatus.PENDING.value:
# Delete the invite
session.delete(invite)
session.commit()
@@ -59,14 +59,14 @@ async def reject_invite(_, info, invite_id: int):
@mutation.field("create_invite")
@login_required
async def create_invite(_, info, slug: str = "", author_id: int = None):
async def create_invite(_, info, slug: str = "", author_id: int = 0):
user_id = info.context["user_id"]
# Check if the inviter is the owner of the shout
with local_session() as session:
shout = session.query(Shout).filter(Shout.slug == slug).first()
inviter = session.query(Author).filter(Author.user == user_id).first()
if inviter and shout and shout.authors and inviter.id == shout.created_by:
if inviter and shout and shout.authors and inviter.id is shout.created_by:
# Check if the author is a valid author
author = session.query(Author).filter(Author.id == author_id).first()
if author:
@@ -100,14 +100,14 @@ async def create_invite(_, info, slug: str = "", author_id: int = None):
@mutation.field("remove_author")
@login_required
async def remove_author(_, info, slug: str = "", author_id: int = None):
async def remove_author(_, info, slug: str = "", author_id: int = 0):
user_id = info.context["user_id"]
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
if author:
shout = session.query(Shout).filter(Shout.slug == slug).first()
# NOTE: owner should be first in a list
if shout and author.id == shout.created_by:
if shout and author.id is shout.created_by:
shout.authors = [author for author in shout.authors if author.id != author_id]
session.commit()
return {}
@@ -125,14 +125,15 @@ async def remove_invite(_, info, invite_id: int):
if author:
# Check if the invite exists
invite = session.query(Invite).filter(Invite.id == invite_id).first()
shout = session.query(Shout).filter(Shout.id == invite.shout_id).first()
if shout and shout.deleted_at is None and invite:
if invite.inviter_id == author.id or author.id == shout.created_by:
if invite.status == InviteStatus.PENDING.value:
# Delete the invite
session.delete(invite)
session.commit()
return {}
if isinstance(invite, Invite):
shout = session.query(Shout).filter(Shout.id == invite.shout_id).first()
if shout and shout.deleted_at is None and invite:
if invite.inviter_id is author.id or author.id is shout.created_by:
if invite.status is InviteStatus.PENDING.value:
# Delete the invite
session.delete(invite)
session.commit()
return {}
else:
return {"error": "Invalid invite or already accepted/rejected"}
else:

View File

@@ -104,7 +104,7 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
)
if not shout:
return {"error": "shout not found"}
if shout.created_by != author.id and author.id not in shout.authors:
if shout.created_by is not author.id and author.id not in shout.authors:
return {"error": "access denied"}
if shout_input is not None:
topics_input = shout_input["topics"]
@@ -157,16 +157,18 @@ async def update_shout(_, info, shout_id, shout_input=None, publish=False):
.first()
)
main_topic = session.query(Topic).filter(Topic.slug == shout_input["main_topic"]).first()
new_main_topic = (
session.query(ShoutTopic)
.filter(and_(ShoutTopic.shout == shout.id, ShoutTopic.topic == main_topic.id))
.first()
)
if old_main_topic is not new_main_topic:
old_main_topic.main = False
new_main_topic.main = True
session.add(old_main_topic)
session.add(new_main_topic)
if isinstance(main_topic, Topic):
new_main_topic = (
session.query(ShoutTopic)
.filter(and_(ShoutTopic.shout == shout.id, ShoutTopic.topic == main_topic.id))
.first()
)
if isinstance(old_main_topic, ShoutTopic) and isinstance(new_main_topic, ShoutTopic) \
and old_main_topic is not new_main_topic:
ShoutTopic.update(old_main_topic, {"main": False})
session.add(old_main_topic)
ShoutTopic.update(new_main_topic, {"main": True})
session.add(new_main_topic)
session.commit()
@@ -194,15 +196,15 @@ async def delete_shout(_, info, shout_id):
shout = session.query(Shout).filter(Shout.id == shout_id).first()
if not shout:
return {"error": "invalid shout id"}
if author:
if shout.created_by != author.id and author.id not in shout.authors:
if isinstance(author, Author) and isinstance(shout, Shout):
# TODO: add editor role allowed here
if shout.created_by is not author.id and author.id not in shout.authors:
return {"error": "access denied"}
for author_id in shout.authors:
reactions_unfollow(author_id, shout_id)
# Replace datetime with Unix timestamp
current_time = int(time.time())
shout_dict = shout.dict()
shout_dict["deleted_at"] = current_time # Set deleted_at as Unix timestamp
shout_dict["deleted_at"] = int(time.time())
Shout.update(shout, shout_dict)
session.add(shout)
session.commit()

View File

@@ -92,14 +92,23 @@ def is_published_author(session, author_id):
> 0
)
def check_to_publish(session, approver_id, reaction):
"""set shout to public if publicated approvers amount > 4"""
if not reaction.reply_to and reaction.kind in [
def is_negative(x):
return x in [
ReactionKind.ACCEPT.value,
ReactionKind.LIKE.value,
ReactionKind.PROOF.value,
]:
]
def is_positive(x):
return x in [
ReactionKind.ACCEPT.value,
ReactionKind.LIKE.value,
ReactionKind.PROOF.value,
]
def check_to_publish(session, approver_id, reaction):
"""set shout to public if publicated approvers amount > 4"""
if not reaction.reply_to and is_positive(reaction.kind):
if is_published_author(session, approver_id):
# now count how many approvers are voted already
approvers_reactions = session.query(Reaction).where(Reaction.shout == reaction.shout).all()
@@ -117,20 +126,12 @@ def check_to_publish(session, approver_id, reaction):
def check_to_hide(session, reaction):
"""hides any shout if 20% of reactions are negative"""
if not reaction.reply_to and reaction.kind in [
ReactionKind.REJECT.value,
ReactionKind.DISLIKE.value,
ReactionKind.DISPROOF.value,
]:
if not reaction.reply_to and is_negative(reaction.kind):
# if is_published_author(author_id):
approvers_reactions = session.query(Reaction).where(Reaction.shout == reaction.shout).all()
rejects = 0
for r in approvers_reactions:
if r.kind in [
ReactionKind.REJECT.value,
ReactionKind.DISLIKE.value,
ReactionKind.DISPROOF.value,
]:
if is_negative(r.kind):
rejects += 1
if len(approvers_reactions) / rejects < 5:
return True
@@ -155,6 +156,49 @@ def set_hidden(session, shout_id):
session.add(s)
session.commit()
async def _create_reaction(session, shout, author, reaction):
r = Reaction(**reaction)
rdict = r.dict()
session.add(r)
session.commit()
# Proposal accepting logic
if rdict.get("reply_to"):
if r.kind in ["LIKE", "APPROVE"] and author.id in shout.authors:
replied_reaction = session.query(Reaction).filter(Reaction.id == r.reply_to).first()
if replied_reaction:
if replied_reaction.kind is ReactionKind.PROPOSE.value:
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_dict = shout.dict()
shout_dict["body"] = new_body
Shout.update(shout, shout_dict)
session.add(shout)
session.commit()
# Self-regulation mechanics
if check_to_hide(session, r):
set_hidden(session, shout.id)
elif check_to_publish(session, author.id, r):
await set_published(session, shout.id, author.id)
# Reactions auto-following
reactions_follow(author.id, reaction["shout"], True)
rdict["shout"] = shout.dict()
rdict["created_by"] = author.dict()
rdict["stat"] = {"commented": 0, "reacted": 0, "rating": 0}
# Notifications call
await notify_reaction(rdict, "create")
return rdict
@mutation.field("create_reaction")
@login_required
async def create_reaction(_, info, reaction):
@@ -169,92 +213,64 @@ async def create_reaction(_, info, reaction):
with local_session() as session:
shout = session.query(Shout).filter(Shout.id == shout_id).one()
author = session.query(Author).filter(Author.user == user_id).first()
dont_create_new = False
if shout and author:
reaction["created_by"] = author.id
kind = reaction.get("kind")
shout_id = shout.id
if not kind and reaction.get("body"):
kind = ReactionKind.COMMENT.value
if not kind:
return { "error": "cannot create reaction with this kind"}
existing_reaction = (
session.query(Reaction)
.filter(
and_(
Reaction.shout == shout_id,
Reaction.created_by == author.id,
Reaction.kind == kind,
Reaction.reply_to == reaction.get("reply_to"),
if kind in ["LIKE", "DISLIKE", "AGREE", "DISAGREE"]:
same_reaction = (
session.query(Reaction)
.filter(
and_(
Reaction.shout == shout_id,
Reaction.created_by == author.id,
Reaction.kind == kind,
Reaction.reply_to == reaction.get("reply_to"),
)
)
.first()
)
.first()
)
if existing_reaction is not None:
return {"error": "You can't vote twice"}
if same_reaction is not None:
return {"error": "You can't vote twice"}
opposite_reaction_kind = (
ReactionKind.DISLIKE.value
if reaction["kind"] == ReactionKind.LIKE.value
else ReactionKind.LIKE.value
)
opposite_reaction = (
session.query(Reaction)
.filter(
and_(
Reaction.shout == reaction["shout"],
Reaction.created_by == author.id,
Reaction.kind == opposite_reaction_kind,
Reaction.reply_to == reaction.get("reply_to"),
opposite_reaction_kind = (
ReactionKind.DISLIKE.value
if reaction["kind"] == ReactionKind.LIKE.value
else ReactionKind.LIKE.value
)
opposite_reaction = (
session.query(Reaction)
.filter(
and_(
Reaction.shout == reaction["shout"],
Reaction.created_by == author.id,
Reaction.kind == opposite_reaction_kind,
Reaction.reply_to == reaction.get("reply_to"),
)
)
.first()
)
.first()
)
if opposite_reaction is not None:
session.delete(opposite_reaction)
if opposite_reaction is not None:
await notify_reaction(opposite_reaction, "delete")
session.delete(opposite_reaction)
r = Reaction(**reaction)
rdict = r.dict()
return {"reaction": reaction}
else:
# Proposal accepting logic
if rdict.get("reply_to"):
if r.kind is ReactionKind.ACCEPT.value and author.id in shout.authors:
replied_reaction = session.query(Reaction).filter(Reaction.id == r.reply_to).first()
if replied_reaction:
if replied_reaction.kind is ReactionKind.PROPOSE.value:
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_dict = shout.dict()
shout_dict["body"] = new_body
Shout.update(shout, shout_dict)
rdict = await _create_reaction(session, shout, author, reaction)
session.add(r)
session.commit()
logger.debug(r)
rdict = r.dict()
return {"reaction": rdict}
# Self-regulation mechanics
if check_to_hide(session, r):
set_hidden(session, r.shout)
elif check_to_publish(session, author.id, r):
await set_published(session, r.shout, author.id)
# Reactions auto-following
reactions_follow(author.id, reaction["shout"], True)
rdict["shout"] = shout.dict()
rdict["created_by"] = author.dict()
rdict["stat"] = {"commented": 0, "reacted": 0, "rating": 0}
# Notifications call
await notify_reaction(rdict, "create")
return {"reaction": rdict}
except Exception as e:
import traceback

View File

@@ -342,12 +342,12 @@ async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0):
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
if author:
return get_shouts_from_query(q, author.id)
return await get_shouts_from_query(q, author.id)
else:
return get_shouts_from_query(q)
return await get_shouts_from_query(q)
def get_shouts_from_query(q, author_id=None):
async def get_shouts_from_query(q, author_id=None):
shouts = []
with local_session() as session:
for [shout,commented_stat, likes_stat, dislikes_stat, last_comment] in session.execute(
@@ -355,7 +355,7 @@ def get_shouts_from_query(q, author_id=None):
).unique():
shouts.append(shout)
shout.stat = {
"viewed": shout.views,
"viewed": await ViewedStorage.get_shout(shout_slug=shout.slug),
"commented": commented_stat,
"rating": int(likes_stat or 0) - int(dislikes_stat or 0),
}
@@ -384,7 +384,18 @@ async def load_shouts_random_top(_, _info, options):
subquery = select(Shout.id).outerjoin(aliased_reaction).where(Shout.deleted_at.is_(None))
subquery = apply_filters(subquery, options.get("filters", {}))
subquery = subquery.group_by(Shout.id).order_by(desc(get_rating_func(aliased_reaction)))
subquery = subquery.group_by(Shout.id).order_by(desc(
func.sum(
case(
(Reaction.kind == ReactionKind.LIKE.value, 1),
(Reaction.kind == ReactionKind.AGREE.value, 1),
(Reaction.kind == ReactionKind.DISLIKE.value, -1),
(Reaction.kind == ReactionKind.DISAGREE.value, -1),
else_=0
)
)
)
)
random_limit = options.get("random_limit")
if random_limit:
@@ -406,7 +417,7 @@ async def load_shouts_random_top(_, _info, options):
# print(q.compile(compile_kwargs={"literal_binds": True}))
return get_shouts_from_query(q)
return await get_shouts_from_query(q)
@query.field("load_shouts_random_topic")