add community subscription; unify subscription interface
This commit is contained in:
parent
f89cb47daf
commit
39070dc359
|
@ -1,5 +1,5 @@
|
|||
from orm.rbac import Operation, Resource, Permission, Role, RoleStorage
|
||||
from orm.community import Community
|
||||
from orm.community import Community, CommunitySubscription
|
||||
from orm.user import User, UserRating, UserRole, UserStorage
|
||||
from orm.topic import Topic, TopicSubscription, TopicStorage
|
||||
from orm.notification import Notification
|
||||
|
|
|
@ -3,6 +3,14 @@ from sqlalchemy import Column, Integer, String, ForeignKey, DateTime
|
|||
from sqlalchemy.orm import relationship, backref
|
||||
from orm.base import Base, local_session
|
||||
|
||||
class CommunitySubscription(Base):
|
||||
__tablename__ = 'community_subscription'
|
||||
|
||||
id = None
|
||||
subscriber = Column(ForeignKey('user.slug'), primary_key = True)
|
||||
community = Column(ForeignKey('community.slug'), primary_key = True)
|
||||
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
||||
|
||||
|
||||
class Community(Base):
|
||||
__tablename__ = 'community'
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
from orm import Community
|
||||
from orm import Community, CommunitySubscription
|
||||
from orm.base import local_session
|
||||
from resolvers.base import mutation, query, subscription
|
||||
from auth.authenticate import login_required
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import and_
|
||||
|
||||
@mutation.field("createCommunity")
|
||||
@login_required
|
||||
async def create_community(_, info, title, desc):
|
||||
|
@ -68,3 +70,19 @@ async def get_communities(_, info):
|
|||
with local_session() as session:
|
||||
communities = session.query(Community)
|
||||
return communities
|
||||
|
||||
def community_subscribe(user, slug):
|
||||
CommunitySubscription.create(
|
||||
subscriber = user.slug,
|
||||
community = slug
|
||||
)
|
||||
|
||||
def community_unsubscribe(user, slug):
|
||||
with local_session() as session:
|
||||
sub = session.query(CommunitySubscription).\
|
||||
filter(and_(CommunitySubscription.subscriber == user.slug, CommunitySubscription.community == slug)).\
|
||||
first()
|
||||
if not sub:
|
||||
raise Exception("subscription not exist")
|
||||
session.delete(sub)
|
||||
session.commit()
|
||||
|
|
|
@ -4,6 +4,8 @@ from orm.comment import Comment
|
|||
from orm.base import local_session
|
||||
from orm.topic import Topic, TopicSubscription
|
||||
from resolvers.base import mutation, query, subscription
|
||||
from resolvers.topics import topic_subscribe, topic_unsubscribe
|
||||
from resolvers.community import community_subscribe, community_unsubscribe
|
||||
from auth.authenticate import login_required
|
||||
|
||||
from inbox_resolvers.inbox import get_total_unread_messages_for_user
|
||||
|
@ -133,30 +135,53 @@ async def rate_user(_, info, slug, value):
|
|||
|
||||
return {}
|
||||
|
||||
@mutation.field("authorSubscribe")
|
||||
@login_required
|
||||
async def author_subscribe(_, info, slug):
|
||||
user = info.context["request"].user
|
||||
|
||||
def author_subscribe(user, slug):
|
||||
AuthorSubscription.create(
|
||||
subscriber = user.slug,
|
||||
author = slug
|
||||
)
|
||||
|
||||
return {}
|
||||
|
||||
@mutation.field("authorUnsubscribe")
|
||||
@login_required
|
||||
async def author_unsubscribe(_, info, slug):
|
||||
user = info.context["request"].user
|
||||
|
||||
def author_unsubscribe(user, slug):
|
||||
with local_session() as session:
|
||||
sub = session.query(AuthorSubscription).\
|
||||
filter(and_(AuthorSubscription.subscriber == user.slug, AuthorSubscription.author == slug)).\
|
||||
first()
|
||||
if not sub:
|
||||
return { "error" : "subscription not exist" }
|
||||
raise Exception("subscription not exist")
|
||||
session.delete(sub)
|
||||
session.commit()
|
||||
|
||||
@mutation.field("subscribe")
|
||||
@login_required
|
||||
async def subscribe(_, info, subscription, slug):
|
||||
user = info.context["request"].user
|
||||
|
||||
try:
|
||||
if subscription == "AUTHOR":
|
||||
author_subscribe(user, slug)
|
||||
elif subscription == "TOPIC":
|
||||
topic_subscribe(user, slug)
|
||||
elif subscription == "COMMUNITY":
|
||||
community_subscribe(user, slug)
|
||||
except Exception as e:
|
||||
return {"error" : e}
|
||||
|
||||
return {}
|
||||
|
||||
@mutation.field("unsubscribe")
|
||||
@login_required
|
||||
async def unsubscribe(_, info, subscription, slug):
|
||||
user = info.context["request"].user
|
||||
|
||||
try:
|
||||
if subscription == "AUTHOR":
|
||||
author_unsubscribe(user, slug)
|
||||
elif subscription == "TOPIC":
|
||||
topic_unsubscribe(user, slug)
|
||||
elif subscription == "COMMUNITY":
|
||||
community_unsubscribe(user, slug)
|
||||
except Exception as e:
|
||||
return {"error" : e}
|
||||
|
||||
return {}
|
||||
|
|
|
@ -60,29 +60,17 @@ async def update_topic(_, info, input):
|
|||
|
||||
return { "topic" : topic }
|
||||
|
||||
@mutation.field("topicSubscribe")
|
||||
@login_required
|
||||
async def topic_subscribe(_, info, slug):
|
||||
user = info.context["request"].user
|
||||
|
||||
def topic_subscribe(user, slug):
|
||||
TopicSubscription.create(
|
||||
subscriber = user.slug,
|
||||
topic = slug)
|
||||
|
||||
return {}
|
||||
|
||||
@mutation.field("topicUnsubscribe")
|
||||
@login_required
|
||||
async def topic_unsubscribe(_, info, slug):
|
||||
user = info.context["request"].user
|
||||
|
||||
def topic_unsubscribe(user, slug):
|
||||
with local_session() as session:
|
||||
sub = session.query(TopicSubscription).\
|
||||
filter(and_(TopicSubscription.subscriber == user.slug, TopicSubscription.topic == slug)).\
|
||||
first()
|
||||
if not sub:
|
||||
return { "error" : "subscription not exist" }
|
||||
raise Exception("subscription not exist")
|
||||
session.delete(sub)
|
||||
session.commit()
|
||||
|
||||
return {}
|
||||
|
|
|
@ -93,6 +93,12 @@ type CommentUpdatedResult {
|
|||
comment: Comment
|
||||
}
|
||||
|
||||
enum SubscriptionType {
|
||||
TOPIC
|
||||
AUTHOR
|
||||
COMMUNITY
|
||||
}
|
||||
|
||||
################################### Mutation
|
||||
|
||||
type Mutation {
|
||||
|
@ -118,8 +124,6 @@ type Mutation {
|
|||
# topics
|
||||
createTopic(input: TopicInput!): TopicResult!
|
||||
updateTopic(input: TopicInput!): TopicResult!
|
||||
topicSubscribe(slug: String!): Result!
|
||||
topicUnsubscribe(slug: String!): Result!
|
||||
|
||||
createComment(body: String!, shout: String!, replyTo: Int): CommentResult!
|
||||
updateComment(id: Int!, body: String!): CommentResult!
|
||||
|
@ -130,8 +134,8 @@ type Mutation {
|
|||
updateCommunity(community: CommunityInput!): Community!
|
||||
deleteCommunity(id: Int!): Result!
|
||||
|
||||
authorSubscribe(slug: String!): Result!
|
||||
authorUnsubscribe(slug: String!): Result!
|
||||
subscribe(subscription : SubscriptionType!, slug: String!): Result!
|
||||
unsubscribe(subscription : SubscriptionType!, slug: String!): Result!
|
||||
}
|
||||
|
||||
################################### Query
|
||||
|
|
Loading…
Reference in New Issue
Block a user