Merge branch 'main' of testapi.discours.io:discoursio-api

This commit is contained in:
bniwredyc
2023-01-18 13:30:28 +01:00
13 changed files with 295 additions and 96 deletions

View File

@@ -8,7 +8,8 @@ from resolvers.auth import (
get_current_user,
)
from resolvers.create.collab import remove_coauthor, invite_coauthor
from resolvers.create.collab import load_drafts, create_draft, update_draft, delete_draft,\
accept_coauthor, invite_coauthor
from resolvers.create.migrate import markdown_body
from resolvers.create.editor import create_shout, delete_shout, update_shout
@@ -93,8 +94,12 @@ __all__ = [
# create.migrate
"markdown_body",
# create.collab
"load_drafts",
"create_draft",
"update_draft",
"delete_draft",
"invite_coauthor",
"remove_coauthor",
"accept_coauthor",
# zine.topics
"topics_all",
"topics_by_community",

View File

@@ -3,84 +3,130 @@ from auth.credentials import AuthCredentials
from base.orm import local_session
from base.resolvers import query, mutation
from base.exceptions import ObjectNotExist, BaseHttpException
from orm.collab import Collab, CollabAuthor
from orm.draft import DraftCollab, DraftAuthor, DraftTopic
from orm.shout import Shout
from orm.user import User
@query.field("getCollabs")
@query.field("loadDrafts")
@login_required
async def get_collabs(_, info):
async def load_drafts(_, info):
auth: AuthCredentials = info.context["request"].auth
drafts = []
with local_session() as session:
drafts = session.query(DraftCollab).filter(auth.user_id in DraftCollab.authors)
return drafts
@mutation.field("createDraft") # TODO
@login_required
async def create_draft(_, info, draft_input):
auth: AuthCredentials = info.context["request"].auth
with local_session() as session:
collabs = session.query(Collab).filter(auth.user_id in Collab.authors)
return collabs
collab = DraftCollab.create(**draft_input)
session.add(collab)
session.commit()
@mutation.field("inviteCoauthor")
@login_required
async def invite_coauthor(_, info, author: int = 0, shout: int = 0):
auth: AuthCredentials = info.context["request"].auth
with local_session() as session:
s = session.query(Shout).where(Shout.id == shout).one()
if not s:
raise ObjectNotExist("invalid shout id")
else:
c = session.query(Collab).where(Collab.shout == shout).one()
if auth.user_id not in c.authors:
raise BaseHttpException("you are not in authors list")
else:
invited_user = session.query(User).where(User.id == author).one()
c.invites.append(invited_user)
session.add(c)
session.commit()
# TODO: email notify
# TODO: email notify to all authors
return {}
@mutation.field("removeCoauthor")
@mutation.field("deleteDraft") # TODO
@login_required
async def remove_coauthor(_, info, author: int = 0, shout: int = 0):
async def delete_draft(_, info, draft: int = 0):
auth: AuthCredentials = info.context["request"].auth
with local_session() as session:
s = session.query(Shout).where(Shout.id == shout).one() # raises Error when not found
collab = session.query(DraftCollab).where(DraftCollab.id == draft_input.id).one()
if auth.user_id not in s.authors:
raise BaseHttpException("only owner can remove coauthors")
# raise BaseHttpException("only owner can remove coauthors")
return {
"error": "Only authors can update a draft"
}
elif not collab:
return {
"error": "There is no draft with this id"
}
else:
c = session.query(Collab).where(Collab.shout == shout).one()
ca = session.query(CollabAuthor).join(User).where(c.shout == shout, User.id == author).one()
session.remve(ca)
c.invites = filter(lambda x: x.id == author, c.invites)
c.authors = filter(lambda x: x.id == author, c.authors)
session.add(c)
session.delete(collab)
session.commit()
return {}
@mutation.field("updateDraft") # TODO: draft input type
@login_required
async def update_draft(_, info, draft_input):
auth: AuthCredentials = info.context["request"].auth
with local_session() as session:
collab = session.query(DraftCollab).where(DraftCollab.id == draft_input.id).one() # raises Error when not found
if auth.user_id not in s.authors:
# raise BaseHttpException("only owner can remove coauthors")
return {
"error": "Only authors can update draft"
}
elif not s:
return {
"error": "There is no draft with this id"
}
else:
draft_input["updatedAt"] = datetime.now(tz=timezone.utc)
collab.update(draft_input)
session.commit()
# TODO: email notify
return {}
@mutation.field("acceptCoauthor")
@mutation.field("inviteAuthor")
@login_required
async def accept_coauthor(_, info, shout: int):
async def invite_coauthor(_, info, author: int = 0, draft: int = 0):
auth: AuthCredentials = info.context["request"].auth
with local_session() as session:
s = session.query(Shout).where(Shout.id == shout).one()
if not s:
raise ObjectNotExist("invalid shout id")
c = session.query(DraftCollab).where(DraftCollab.id == draft).one()
if auth.user_id not in c.authors:
# raise BaseHttpException("you are not in authors list")
return {
"error": "You are not in authors list"
}
elif c.id:
invited_user = session.query(User).where(User.id == author).one()
da = DraftAuthor.create({
"accepted": False,
"collab": c.id,
"author": invited_user.id
})
session.add(da)
session.commit()
else:
c = session.query(Collab).where(Collab.shout == shout).one()
accepted = filter(lambda x: x.id == auth.user_id, c.invites).pop()
if accepted:
c.authors.append(accepted)
s.authors.append(accepted)
session.add(s)
session.add(c)
session.commit()
return {}
else:
raise BaseHttpException("only invited can accept")
return {
"error": "Draft is not found"
}
# TODO: email notify
return {}
@mutation.field("inviteAccept")
@login_required
async def accept_coauthor(_, info, draft: int):
auth: AuthCredentials = info.context["request"].auth
with local_session() as session:
# c = session.query(DraftCollab).where(DraftCollab.id == draft).one()
a = session.query(DraftAuthor).where(DraftAuthor.collab == draft).filter(DraftAuthor.author == auth.user_id).one()
if not a.accepted:
a.accepted = True
session.commit()
# TODO: email notify
return {}
elif a.accepted == True:
return {
"error": "You have accepted invite before"
}
else:
# raise BaseHttpException("only invited can accept")
return {
"error": "You don't have an invitation yet"
}

View File

@@ -14,7 +14,7 @@ from resolvers.zine.reactions import reactions_follow, reactions_unfollow
from services.zine.gittask import GitTask
from resolvers.inbox.chats import create_chat
from services.inbox.storage import MessagesStorage
from orm.collab import Collab
from orm.draft import DraftCollab
@mutation.field("createShout")

View File

@@ -218,10 +218,13 @@ def author_unfollow(user_id, slug):
).first()
)
if not flw:
raise Exception("[resolvers.profile] follower not exist, cant unfollow")
return {
"error": "Follower is not exist, cant unfollow"
}
else:
session.delete(flw)
session.commit()
return {}
@query.field("authorsAll")

View File

@@ -196,7 +196,7 @@ async def update_reaction(_, info, reaction={}):
if not r:
return {"error": "invalid reaction id"}
if r.createdBy != user.slug:
if r.createdBy != user.id:
return {"error": "access denied"}
r.body = reaction["body"]

48
resolvers/zine/remark.py Normal file
View File

@@ -0,0 +1,48 @@
from datetime import datetime, timedelta, timezone
from sqlalchemy.orm import joinedload, aliased
from sqlalchemy.sql.expression import desc, asc, select, func
from base.orm import local_session
from base.resolvers import query, mutation
from base.exceptions import ObjectNotExist
from orm.remark import Remark
@mutation.field("createRemark")
@login_required
async def create_remark(_, info, slug, body):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
tt = Remark.create(slug=slug, body=body)
session.commit()
return
@mutation.field("updateRemark")
@login_required
async def update_remark(_, info, slug, body = ''):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
rmrk = session.query(Remark).where(Remark.slug == slug).one()
if body:
tt.body = body
session.add(rmrk)
session.commit()
return
@mutation.field("deleteRemark")
@login_required
async def delete_remark(_, info, slug):
auth = info.context["request"].auth
user_id = auth.user_id
with local_session() as session:
rmrk = session.query(Remark).where(Remark.slug == slug).one()
rmrk.remove()
session.commit()
return
@query.field("loadRemark")
@login_required
async def load_remark(_, info, slug):
pass