2021-11-24 12:10:34 +00:00
|
|
|
from datetime import datetime
|
2022-07-21 16:11:39 +00:00
|
|
|
from typing import List
|
2022-09-17 18:12:14 +00:00
|
|
|
|
2022-06-12 07:51:22 +00:00
|
|
|
from sqlalchemy import and_
|
|
|
|
|
2022-09-17 18:12:14 +00:00
|
|
|
from auth.authenticate import login_required
|
|
|
|
from base.orm import local_session
|
|
|
|
from base.resolvers import mutation, query
|
|
|
|
from orm.community import Community, CommunityFollower
|
|
|
|
from orm.user import User
|
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
|
2021-11-24 12:10:34 +00:00
|
|
|
@mutation.field("createCommunity")
|
|
|
|
@login_required
|
2022-06-28 19:40:44 +00:00
|
|
|
async def create_community(_, info, input):
|
2022-09-03 10:50:14 +00:00
|
|
|
auth = info.context["request"].auth
|
|
|
|
user_id = auth.user_id
|
2022-09-04 17:20:38 +00:00
|
|
|
with local_session() as session:
|
|
|
|
user = session.query(User).where(User.id == user_id).first()
|
|
|
|
community = Community.create(
|
|
|
|
slug=input.get("slug", ""),
|
|
|
|
title=input.get("title", ""),
|
|
|
|
desc=input.get("desc", ""),
|
|
|
|
pic=input.get("pic", ""),
|
|
|
|
createdBy=user.slug,
|
|
|
|
createdAt=datetime.now(),
|
|
|
|
)
|
2022-09-17 18:12:14 +00:00
|
|
|
session.add(community)
|
|
|
|
session.commit()
|
2021-11-24 12:10:34 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
return {"community": community}
|
2021-11-24 12:10:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@mutation.field("updateCommunity")
|
|
|
|
@login_required
|
2022-06-28 19:40:44 +00:00
|
|
|
async def update_community(_, info, input):
|
2022-09-03 10:50:14 +00:00
|
|
|
auth = info.context["request"].auth
|
|
|
|
user_id = auth.user_id
|
|
|
|
community_slug = input.get("slug", "")
|
|
|
|
|
|
|
|
with local_session() as session:
|
|
|
|
owner = session.query(User).filter(User.id == user_id) # note list here
|
|
|
|
community = (
|
|
|
|
session.query(Community).filter(Community.slug == community_slug).first()
|
|
|
|
)
|
|
|
|
editors = [e.slug for e in community.editors]
|
|
|
|
if not community:
|
|
|
|
return {"error": "invalid community id"}
|
|
|
|
if community.createdBy not in (owner + editors):
|
|
|
|
return {"error": "access denied"}
|
|
|
|
community.title = input.get("title", "")
|
|
|
|
community.desc = input.get("desc", "")
|
|
|
|
community.pic = input.get("pic", "")
|
|
|
|
community.updatedAt = datetime.now()
|
2022-09-17 18:12:14 +00:00
|
|
|
session.add(community)
|
2022-09-03 10:50:14 +00:00
|
|
|
session.commit()
|
|
|
|
|
2021-11-24 12:10:34 +00:00
|
|
|
|
|
|
|
@mutation.field("deleteCommunity")
|
|
|
|
@login_required
|
2022-06-28 19:40:44 +00:00
|
|
|
async def delete_community(_, info, slug):
|
2022-09-03 10:50:14 +00:00
|
|
|
auth = info.context["request"].auth
|
|
|
|
user_id = auth.user_id
|
|
|
|
|
|
|
|
with local_session() as session:
|
|
|
|
community = session.query(Community).filter(Community.slug == slug).first()
|
|
|
|
if not community:
|
|
|
|
return {"error": "invalid community slug"}
|
|
|
|
if community.owner != user_id:
|
|
|
|
return {"error": "access denied"}
|
|
|
|
community.deletedAt = datetime.now()
|
2022-09-17 18:12:14 +00:00
|
|
|
session.add(community)
|
2022-09-03 10:50:14 +00:00
|
|
|
session.commit()
|
2021-11-24 12:10:34 +00:00
|
|
|
|
2022-09-03 10:50:14 +00:00
|
|
|
return {}
|
2021-11-24 12:10:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@query.field("getCommunity")
|
|
|
|
async def get_community(_, info, slug):
|
2022-09-03 10:50:14 +00:00
|
|
|
with local_session() as session:
|
|
|
|
community = session.query(Community).filter(Community.slug == slug).first()
|
|
|
|
if not community:
|
|
|
|
return {"error": "invalid community id"}
|
|
|
|
|
|
|
|
return community
|
2021-11-24 12:10:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
@query.field("getCommunities")
|
2021-11-24 13:34:30 +00:00
|
|
|
async def get_communities(_, info):
|
2022-09-03 10:50:14 +00:00
|
|
|
with local_session() as session:
|
|
|
|
communities = session.query(Community)
|
|
|
|
return communities
|
|
|
|
|
2022-06-12 07:51:22 +00:00
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
def community_follow(user, slug):
|
2022-09-03 10:50:14 +00:00
|
|
|
CommunityFollower.create(follower=user.slug, community=slug)
|
|
|
|
|
2022-06-12 07:51:22 +00:00
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
def community_unfollow(user, slug):
|
2022-09-03 10:50:14 +00:00
|
|
|
with local_session() as session:
|
|
|
|
following = (
|
|
|
|
session.query(CommunityFollower)
|
|
|
|
.filter(
|
|
|
|
and_(
|
|
|
|
CommunityFollower.follower == user.slug,
|
|
|
|
CommunityFollower.community == slug,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.first()
|
|
|
|
)
|
|
|
|
if not following:
|
|
|
|
raise Exception("[orm.community] following was not exist")
|
|
|
|
session.delete(following)
|
|
|
|
session.commit()
|
|
|
|
|
2022-06-12 08:45:08 +00:00
|
|
|
|
2022-07-21 11:58:50 +00:00
|
|
|
@query.field("userFollowedCommunities")
|
2022-07-21 16:11:39 +00:00
|
|
|
def get_followed_communities(_, user_slug) -> List[Community]:
|
2022-09-03 10:50:14 +00:00
|
|
|
ccc = []
|
|
|
|
with local_session() as session:
|
|
|
|
ccc = (
|
|
|
|
session.query(Community.slug)
|
|
|
|
.join(CommunityFollower)
|
|
|
|
.where(CommunityFollower.follower == user_slug)
|
|
|
|
.all()
|
|
|
|
)
|
|
|
|
return ccc
|