new create shout flow

This commit is contained in:
bniwredyc 2023-05-03 17:47:09 +02:00
parent 2f22fd2126
commit 3836674b72
5 changed files with 93 additions and 19 deletions

19
migrate.sh Normal file
View File

@ -0,0 +1,19 @@
database_name="discoursio"
echo "DATABASE MIGRATION STARTED"
echo "Dropping database $database_name"
dropdb $database_name --force
if [ $? -ne 0 ]; then { echo "Failed to drop database, aborting." ; exit 1; } fi
echo "Database $database_name dropped"
echo "Creating database $database_name"
createdb $database_name
if [ $? -ne 0 ]; then { echo "Failed to create database, aborting." ; exit 1; } fi
echo "Database $database_name successfully created"
echo "Start migration"
python3 server.py migrate
if [ $? -ne 0 ]; then { echo "Migration failed, aborting." ; exit 1; } fi
echo 'Done!'

View File

@ -27,11 +27,11 @@ async def create_shout(_, info, inp):
new_shout = Shout.create(**{
"title": inp.get("title"),
"subtitle": inp.get('subtitle'),
"body": inp.get("body"),
"body": inp.get("body", ''),
"authors": inp.get("authors", []),
"slug": inp.get("slug"),
"mainTopic": inp.get("mainTopic"),
"visibility": "community",
"visibility": "owner",
"createdBy": auth.user_id
})
@ -81,21 +81,24 @@ async def create_shout(_, info, inp):
session.commit()
# TODO
# GitTask(inp, user.username, user.email, "new shout %s" % new_shout.slug)
# TODO
# GitTask(inp, user.username, user.email, "new shout %s" % new_shout.slug)
if new_shout.slug is None:
new_shout.slug = f"draft-{new_shout.id}"
session.commit()
return {"shout": new_shout}
@mutation.field("updateShout")
@login_required
async def update_shout(_, info, inp):
async def update_shout(_, info, slug, inp):
auth: AuthCredentials = info.context["request"].auth
slug = inp["slug"]
with local_session() as session:
user = session.query(User).filter(User.id == auth.user_id).first()
shout = session.query(Shout).filter(Shout.slug == slug).first()
if not shout:
return {"error": "shout not found"}
@ -108,18 +111,38 @@ async def update_shout(_, info, inp):
else:
shout.update(inp)
shout.updatedAt = datetime.now(tz=timezone.utc)
session.add(shout)
if inp.get("topics"):
# remove old links
links = session.query(ShoutTopic).where(ShoutTopic.shout == shout.id).all()
for topiclink in links:
session.delete(topiclink)
# add new topic links
for topic in inp.get("topics", []):
ShoutTopic.create(shout=slug, topic=topic)
# for topic_slug in inp.get("topics", []):
# topic = session.query(Topic).filter(Topic.slug == topic_slug).first()
# shout_topic = ShoutTopic.create(shout=shout.id, topic=topic.id)
# session.add(shout_topic)
session.commit()
# GitTask(inp, user.username, user.email, "update shout %s" % slug)
GitTask(inp, user.username, user.email, "update shout %s" % slug)
return {"shout": shout}
@mutation.field("publishShout")
@login_required
async def publish_shout(_, info, slug, inp):
auth: AuthCredentials = info.context["request"].auth
with local_session() as session:
shout = session.query(Shout).filter(Shout.slug == slug).first()
if not shout:
return {"error": "shout not found"}
else:
shout.update(inp)
shout.visibility = "community"
shout.updatedAt = datetime.now(tz=timezone.utc)
session.commit()
return {"shout": shout}

View File

@ -1,11 +1,11 @@
from datetime import datetime, timedelta, timezone
from sqlalchemy.orm import joinedload, aliased
from sqlalchemy.sql.expression import desc, asc, select, func, case
from sqlalchemy.sql.expression import desc, asc, select, func, case, and_
from auth.authenticate import login_required
from auth.credentials import AuthCredentials
from base.exceptions import ObjectNotExist
from base.exceptions import ObjectNotExist, OperationNotAllowed
from base.orm import local_session
from base.resolvers import query
from orm import ViewedEntry, TopicFollower
@ -70,7 +70,6 @@ def apply_filters(q, filters, user_id=None):
return q
@query.field("loadShout")
async def load_shout(_, info, slug):
with local_session() as session:
@ -196,6 +195,38 @@ async def load_shouts_by(_, info, options):
return shouts
@query.field("loadDrafts")
async def get_drafts(_, info, options):
auth: AuthCredentials = info.context["request"].auth
user_id = auth.user_id
q = select(Shout).options(
joinedload(Shout.authors),
joinedload(Shout.topics),
).where(
and_(Shout.deletedAt.is_(None), Shout.createdBy == user_id)
)
q = apply_filters(q, options.get("filters", {}), user_id)
order_by = options.get("order_by", Shout.createdAt)
if order_by == 'reacted':
aliased_reaction = aliased(Reaction)
q.outerjoin(aliased_reaction).add_columns(func.max(aliased_reaction.createdAt).label('reacted'))
query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by)
offset = options.get("offset", 0)
limit = options.get("limit", 10)
q = q.group_by(Shout.id).order_by(query_order_by).limit(limit).offset(offset)
shouts = []
with local_session() as session:
for [shout] in session.execute(q).unique():
shouts.append(shout)
return shouts
@query.field("myFeed")
@login_required

View File

@ -60,7 +60,6 @@ type Author {
type Result {
error: String
uids: [String]
slugs: [String]
chat: Chat
chats: [Chat]
@ -98,7 +97,7 @@ type ReactionUpdating {
input ShoutInput {
slug: String
title: String
body: String!
body: String
authors: [String]
topics: [String]
community: Int
@ -171,8 +170,9 @@ type Mutation {
# shout
createShout(inp: ShoutInput!): Result!
updateShout(inp: ShoutInput!): Result!
updateShout(slug: String!, inp: ShoutInput!): Result!
deleteShout(slug: String!): Result!
publishShout(slug: String!, inp: ShoutInput!): Result!
# user profile
rateUser(slug: String!, value: Int!): Result!
@ -278,6 +278,7 @@ type Query {
loadAuthorsBy(by: AuthorsBy, limit: Int, offset: Int): [Author]!
loadShout(slug: String!): Shout
loadShouts(options: LoadShoutsOptions): [Shout]!
loadDrafts(options: LoadShoutsOptions): [Shout]!
loadReactionsBy(by: ReactionBy!, limit: Int, offset: Int): [Reaction]!
userFollowers(slug: String!): [Author]!
userFollowedAuthors(slug: String!): [Author]!

View File

@ -169,9 +169,9 @@ class ViewedStorage:
viewed = session.query(
ViewedEntry
).join(
Shout
Shout, Shout.id == ViewedEntry.shout
).join(
User
User, User.id == ViewedEntry.viewer
).filter(
User.slug == viewer,
Shout.slug == shout_slug