2021-08-18 16:53:55 +00:00
|
|
|
from orm import Shout, User, Organization, Resource
|
2021-08-07 16:14:20 +00:00
|
|
|
from orm.base import local_session
|
2021-07-27 04:58:06 +00:00
|
|
|
|
2021-08-07 16:14:20 +00:00
|
|
|
from resolvers.base import mutation, query
|
|
|
|
|
|
|
|
from auth.authenticate import login_required
|
|
|
|
from settings import SHOUTS_REPO
|
|
|
|
|
|
|
|
import subprocess
|
2021-08-08 09:49:15 +00:00
|
|
|
import asyncio
|
|
|
|
|
2021-08-08 12:23:12 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2021-08-08 09:49:15 +00:00
|
|
|
class GitTask:
|
|
|
|
|
|
|
|
queue = asyncio.Queue()
|
|
|
|
|
2021-08-17 09:14:26 +00:00
|
|
|
def __init__(self, input, org, username, user_email, comment):
|
2021-08-08 12:23:12 +00:00
|
|
|
self.slug = input["slug"];
|
|
|
|
self.shout_body = input["body"];
|
2021-08-17 09:14:26 +00:00
|
|
|
self.org = org;
|
2021-08-08 09:49:15 +00:00
|
|
|
self.username = username;
|
|
|
|
self.user_email = user_email;
|
|
|
|
self.comment = comment;
|
|
|
|
|
|
|
|
GitTask.queue.put_nowait(self)
|
2021-08-08 12:23:12 +00:00
|
|
|
|
|
|
|
def init_repo(self):
|
|
|
|
repo_path = "%s/%s" % (SHOUTS_REPO, self.org)
|
|
|
|
|
|
|
|
Path(repo_path).mkdir()
|
|
|
|
|
|
|
|
cmd = "cd %s && git init && touch initial && git add initial && git commit -m 'init repo'" % (repo_path)
|
|
|
|
output = subprocess.check_output(cmd, shell=True)
|
|
|
|
print(output)
|
2021-08-08 09:49:15 +00:00
|
|
|
|
|
|
|
def execute(self):
|
2021-08-08 12:23:12 +00:00
|
|
|
repo_path = "%s/%s" % (SHOUTS_REPO, self.org)
|
|
|
|
|
|
|
|
if not Path(repo_path).exists():
|
|
|
|
self.init_repo()
|
|
|
|
|
|
|
|
cmd = "cd %s && git checkout master" % (repo_path)
|
2021-08-08 09:49:15 +00:00
|
|
|
output = subprocess.check_output(cmd, shell=True)
|
|
|
|
print(output)
|
|
|
|
|
2021-08-08 12:23:12 +00:00
|
|
|
shout_filename = "%s.md" % (self.slug)
|
|
|
|
shout_full_filename = "%s/%s" % (repo_path, shout_filename)
|
2021-08-08 09:49:15 +00:00
|
|
|
with open(shout_full_filename, mode='w', encoding='utf-8') as shout_file:
|
|
|
|
shout_file.write(self.shout_body)
|
|
|
|
|
|
|
|
author = "%s <%s>" % (self.username, self.user_email)
|
2021-08-08 12:23:12 +00:00
|
|
|
cmd = "cd %s && git add %s && git commit -m '%s' --author='%s'" % \
|
|
|
|
(repo_path, shout_filename, self.comment, author)
|
2021-08-08 09:49:15 +00:00
|
|
|
output = subprocess.check_output(cmd, shell=True)
|
|
|
|
print(output)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def git_task_worker():
|
|
|
|
print("git task worker start")
|
|
|
|
while True:
|
|
|
|
task = await GitTask.queue.get()
|
|
|
|
try:
|
|
|
|
task.execute()
|
|
|
|
except Exception as err:
|
|
|
|
print("git task worker error = %s" % (err))
|
|
|
|
|
2021-07-27 04:58:06 +00:00
|
|
|
|
2021-07-27 05:41:45 +00:00
|
|
|
@query.field("topShouts")
|
2021-08-07 16:14:20 +00:00
|
|
|
async def top_shouts(_, info):
|
2021-07-27 05:41:45 +00:00
|
|
|
# TODO: implement top shouts
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@query.field("topAuthors")
|
2021-08-07 16:14:20 +00:00
|
|
|
async def top_shouts(_, info):
|
2021-07-27 05:41:45 +00:00
|
|
|
# TODO: implement top authors
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-07-27 04:58:06 +00:00
|
|
|
@mutation.field("createShout")
|
|
|
|
@login_required
|
2021-08-08 12:23:12 +00:00
|
|
|
async def create_shout(_, info, input):
|
2021-07-27 04:58:06 +00:00
|
|
|
auth = info.context["request"].auth
|
|
|
|
user_id = auth.user_id
|
|
|
|
|
2021-08-17 09:14:26 +00:00
|
|
|
org_id = org = input["org_id"]
|
2021-08-08 09:49:15 +00:00
|
|
|
with local_session() as session:
|
|
|
|
user = session.query(User).filter(User.id == user_id).first()
|
2021-08-17 09:14:26 +00:00
|
|
|
org = session.query(Organization).filter(Organization.id == org_id).first()
|
|
|
|
|
|
|
|
if not org:
|
|
|
|
return {
|
|
|
|
"error" : "invalid organization"
|
|
|
|
}
|
2021-08-08 09:49:15 +00:00
|
|
|
|
2021-07-27 04:58:06 +00:00
|
|
|
new_shout = Shout.create(
|
2021-08-08 12:23:12 +00:00
|
|
|
slug = input["slug"],
|
2021-08-17 09:14:26 +00:00
|
|
|
org_id = org_id,
|
2021-08-20 08:08:32 +00:00
|
|
|
authors = [user_id, ],
|
2021-08-08 12:23:12 +00:00
|
|
|
body = input["body"],
|
|
|
|
replyTo = input.get("replyTo"),
|
|
|
|
versionOf = input.get("versionOf"),
|
|
|
|
tags = input.get("tags"),
|
|
|
|
topics = input.get("topics")
|
2021-07-27 04:58:06 +00:00
|
|
|
)
|
2021-08-08 09:49:15 +00:00
|
|
|
|
|
|
|
task = GitTask(
|
2021-08-08 12:23:12 +00:00
|
|
|
input,
|
2021-08-17 09:14:26 +00:00
|
|
|
org.name,
|
2021-08-08 09:49:15 +00:00
|
|
|
user.username,
|
|
|
|
user.email,
|
2021-08-08 12:23:12 +00:00
|
|
|
"new shout %s" % (new_shout.slug)
|
2021-08-08 09:49:15 +00:00
|
|
|
)
|
|
|
|
|
2021-07-27 04:58:06 +00:00
|
|
|
return {
|
|
|
|
"shout" : new_shout
|
|
|
|
}
|
|
|
|
|
2021-08-17 09:14:26 +00:00
|
|
|
@mutation.field("updateShout")
|
|
|
|
@login_required
|
2021-08-18 16:53:55 +00:00
|
|
|
async def update_shout(_, info, input):
|
2021-08-17 09:14:26 +00:00
|
|
|
auth = info.context["request"].auth
|
|
|
|
user_id = auth.user_id
|
|
|
|
|
2021-08-18 16:53:55 +00:00
|
|
|
slug = input["slug"]
|
|
|
|
org_id = org = input["org_id"]
|
2021-08-17 09:14:26 +00:00
|
|
|
with local_session() as session:
|
|
|
|
user = session.query(User).filter(User.id == user_id).first()
|
2021-08-18 16:53:55 +00:00
|
|
|
shout = session.query(Shout).filter(Shout.slug == slug).first()
|
|
|
|
org = session.query(Organization).filter(Organization.id == org_id).first()
|
2021-08-17 09:14:26 +00:00
|
|
|
|
|
|
|
if not shout:
|
|
|
|
return {
|
|
|
|
"error" : "shout not found"
|
|
|
|
}
|
|
|
|
|
2021-08-20 08:08:32 +00:00
|
|
|
if shout.authors[0] != user_id:
|
2021-08-18 16:53:55 +00:00
|
|
|
scopes = auth.scopes
|
|
|
|
print(scopes)
|
|
|
|
if not Resource.shout_id in scopes:
|
2021-08-17 09:14:26 +00:00
|
|
|
return {
|
|
|
|
"error" : "access denied"
|
|
|
|
}
|
|
|
|
|
2021-08-18 16:53:55 +00:00
|
|
|
shout.body = input["body"],
|
|
|
|
shout.replyTo = input.get("replyTo"),
|
|
|
|
shout.versionOf = input.get("versionOf"),
|
|
|
|
shout.tags = input.get("tags"),
|
|
|
|
shout.topics = input.get("topics")
|
|
|
|
|
|
|
|
with local_session() as session:
|
|
|
|
session.commit()
|
|
|
|
|
|
|
|
task = GitTask(
|
|
|
|
input,
|
|
|
|
org.name,
|
|
|
|
user.username,
|
|
|
|
user.email,
|
|
|
|
"update shout %s" % (shout.slug)
|
|
|
|
)
|
|
|
|
|
2021-08-17 09:14:26 +00:00
|
|
|
return {
|
|
|
|
"shout" : shout
|
|
|
|
}
|
2021-07-27 04:58:06 +00:00
|
|
|
|
2021-08-07 16:14:20 +00:00
|
|
|
# TODO: paginate, get, update, delete
|