fix topic subscription
This commit is contained in:
parent
32becea4da
commit
68f7733f91
|
@ -9,8 +9,8 @@ class TopicSubscription(Base):
|
||||||
__tablename__ = "topic_subscription"
|
__tablename__ = "topic_subscription"
|
||||||
|
|
||||||
id = None
|
id = None
|
||||||
|
subscriber = Column(ForeignKey('user.slug'), primary_key = True)
|
||||||
topic = Column(ForeignKey('topic.slug'), primary_key = True)
|
topic = Column(ForeignKey('topic.slug'), primary_key = True)
|
||||||
user = Column(ForeignKey('user.id'), primary_key = True)
|
|
||||||
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
|
||||||
|
|
||||||
class Topic(Base):
|
class Topic(Base):
|
||||||
|
|
|
@ -7,6 +7,8 @@ from resolvers.zine import ShoutSubscriptions
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
from sqlalchemy import func, and_
|
||||||
|
|
||||||
@query.field("topicsBySlugs")
|
@query.field("topicsBySlugs")
|
||||||
async def topics_by_slugs(_, info, slugs = None):
|
async def topics_by_slugs(_, info, slugs = None):
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
|
@ -62,27 +64,35 @@ async def update_topic(_, info, input):
|
||||||
@mutation.field("topicSubscribe")
|
@mutation.field("topicSubscribe")
|
||||||
@login_required
|
@login_required
|
||||||
async def topic_subscribe(_, info, slug):
|
async def topic_subscribe(_, info, slug):
|
||||||
auth = info.context["request"].auth
|
user = info.context["request"].user
|
||||||
user_id = auth.user_id
|
|
||||||
sub = TopicSubscription.create({ user: user_id, topic: slug })
|
TopicSubscription.create(
|
||||||
return {} # type Result
|
subscriber = user.slug,
|
||||||
|
topic = slug)
|
||||||
|
|
||||||
|
return {}
|
||||||
|
|
||||||
@mutation.field("topicUnsubscribe")
|
@mutation.field("topicUnsubscribe")
|
||||||
@login_required
|
@login_required
|
||||||
async def topic_unsubscribe(_, info, slug):
|
async def topic_unsubscribe(_, info, slug):
|
||||||
auth = info.context["request"].auth
|
user = info.context["request"].user
|
||||||
user_id = auth.user_id
|
|
||||||
sub = session.query(TopicSubscription).filter(TopicSubscription.user == user_id and TopicSubscription.topic == slug).first()
|
|
||||||
with local_session() as session:
|
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" }
|
||||||
session.delete(sub)
|
session.delete(sub)
|
||||||
return {} # type Result
|
session.commit()
|
||||||
return { "error": "session error" }
|
|
||||||
|
return {}
|
||||||
|
|
||||||
@subscription.source("topicUpdated")
|
@subscription.source("topicUpdated")
|
||||||
async def new_shout_generator(obj, info, user_id):
|
async def new_shout_generator(obj, info, user_slug):
|
||||||
try:
|
try:
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
topics = session.query(TopicSubscription.topic).filter(TopicSubscription.user == user_id).all()
|
topics = session.query(TopicSubscription.topic).filter(TopicSubscription.subscriber == user_slug).all()
|
||||||
topics = set([item.topic for item in topics])
|
topics = set([item.topic for item in topics])
|
||||||
shouts_queue = asyncio.Queue()
|
shouts_queue = asyncio.Queue()
|
||||||
await ShoutSubscriptions.register_subscription(shouts_queue)
|
await ShoutSubscriptions.register_subscription(shouts_queue)
|
||||||
|
|
|
@ -205,7 +205,7 @@ type Subscription {
|
||||||
onlineUpdated: [User!]!
|
onlineUpdated: [User!]!
|
||||||
shoutUpdated: Shout!
|
shoutUpdated: Shout!
|
||||||
userUpdated: User!
|
userUpdated: User!
|
||||||
topicUpdated(user_id: Int!): Shout!
|
topicUpdated(user_slug: String!): Shout!
|
||||||
commentUpdated(shout: String!): CommentUpdatedResult!
|
commentUpdated(shout: String!): CommentUpdatedResult!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user