topicUpdated
This commit is contained in:
parent
e5aa568933
commit
7f54db8939
|
@ -15,7 +15,7 @@ class TopicSubscription(Base):
|
||||||
__tablename__ = "topic_subscription"
|
__tablename__ = "topic_subscription"
|
||||||
|
|
||||||
id = None
|
id = None
|
||||||
topic = Column(ForeignKey('topic.slug'), primary_key = True)
|
topic = Column(ForeignKey('topic.id'), primary_key = True)
|
||||||
user = Column(ForeignKey('user.id'), 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")
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from orm import Topic, TopicSubscription
|
from orm import Topic, TopicSubscription
|
||||||
from orm.base import local_session
|
from orm.base import local_session
|
||||||
from resolvers.base import mutation, query, subscription
|
from resolvers.base import mutation, query, subscription
|
||||||
|
from resolvers.zine import ShoutSubscriptions
|
||||||
from auth.authenticate import login_required
|
from auth.authenticate import login_required
|
||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
|
@ -48,14 +49,18 @@ async def topic_unsubscribe(_, info, slug):
|
||||||
|
|
||||||
@subscription.source("topicUpdated")
|
@subscription.source("topicUpdated")
|
||||||
async def new_shout_generator(obj, info, user_id):
|
async def new_shout_generator(obj, info, user_id):
|
||||||
with local_session() as session:
|
try:
|
||||||
topics = session.query(TopicSubscription.topic).filter(TopicSubscription.user == user_id).all()
|
with local_session() as session:
|
||||||
#TODO filter new shouts
|
topics = session.query(TopicSubscription.topic).filter(TopicSubscription.user == user_id).all()
|
||||||
while True:
|
topics = set([item.topic for item in topics])
|
||||||
new_shout = {"slug": "slug", "body" : "body"}
|
shouts_queue = asyncio.Queue()
|
||||||
yield new_shout
|
await ShoutSubscriptions.register_subscription(shouts_queue)
|
||||||
await asyncio.sleep(30)
|
while True:
|
||||||
print("end")
|
shout = await shouts_queue.get()
|
||||||
|
if topics.intersection(set(shout.topic_ids)):
|
||||||
|
yield shout
|
||||||
|
finally:
|
||||||
|
await ShoutSubscriptions.del_subscription(shouts_queue)
|
||||||
|
|
||||||
@subscription.field("topicUpdated")
|
@subscription.field("topicUpdated")
|
||||||
def shout_resolver(shout, info, user_id):
|
def shout_resolver(shout, info, user_id):
|
||||||
|
|
|
@ -188,7 +188,27 @@ class ShoutsCache:
|
||||||
print("shouts cache worker error = %s" % (err))
|
print("shouts cache worker error = %s" % (err))
|
||||||
await asyncio.sleep(ShoutsCache.period)
|
await asyncio.sleep(ShoutsCache.period)
|
||||||
|
|
||||||
|
class ShoutSubscriptions:
|
||||||
|
lock = asyncio.Lock()
|
||||||
|
subscriptions = []
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def register_subscription(subs):
|
||||||
|
async with ShoutSubscriptions.lock:
|
||||||
|
ShoutSubscriptions.subscriptions.append(subs)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def del_subscription(subs):
|
||||||
|
async with ShoutSubscriptions.lock:
|
||||||
|
ShoutSubscriptions.subscriptions.remove(subs)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def send_shout(shout):
|
||||||
|
async with ShoutSubscriptions.lock:
|
||||||
|
for subs in ShoutSubscriptions.subscriptions:
|
||||||
|
subs.put_nowait(shout)
|
||||||
|
|
||||||
|
|
||||||
@query.field("topViewed")
|
@query.field("topViewed")
|
||||||
async def top_viewed(_, info, limit):
|
async def top_viewed(_, info, limit):
|
||||||
async with ShoutsCache.lock:
|
async with ShoutsCache.lock:
|
||||||
|
@ -224,11 +244,20 @@ async def create_shout(_, info, input):
|
||||||
|
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
user = session.query(User).filter(User.id == user_id).first()
|
user = session.query(User).filter(User.id == user_id).first()
|
||||||
|
|
||||||
|
topic_ids = input.get("topic_ids")
|
||||||
|
del input["topic_ids"]
|
||||||
|
|
||||||
new_shout = Shout.create(**input)
|
new_shout = Shout.create(**input)
|
||||||
ShoutAuthor.create(
|
ShoutAuthor.create(
|
||||||
shout = new_shout.id,
|
shout = new_shout.id,
|
||||||
user = user_id)
|
user = user_id)
|
||||||
|
|
||||||
|
for id in topic_ids:
|
||||||
|
topic = ShoutTopic.create(
|
||||||
|
shout = new_shout.id,
|
||||||
|
topic = id)
|
||||||
|
new_shout.topic_ids = topic_ids
|
||||||
|
|
||||||
task = GitTask(
|
task = GitTask(
|
||||||
input,
|
input,
|
||||||
|
@ -236,6 +265,8 @@ async def create_shout(_, info, input):
|
||||||
user.email,
|
user.email,
|
||||||
"new shout %s" % (new_shout.slug)
|
"new shout %s" % (new_shout.slug)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
await ShoutSubscriptions.send_shout(new_shout)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"shout" : new_shout
|
"shout" : new_shout
|
||||||
|
|
|
@ -27,7 +27,7 @@ input ShoutInput {
|
||||||
body: String!
|
body: String!
|
||||||
# replyTo: String # another shout
|
# replyTo: String # another shout
|
||||||
# tags: [String] # actual values
|
# tags: [String] # actual values
|
||||||
topics: [String]
|
topic_ids: [Int]
|
||||||
title: String
|
title: String
|
||||||
subtitle: String
|
subtitle: String
|
||||||
versionOf: String
|
versionOf: String
|
||||||
|
@ -239,7 +239,7 @@ type Shout {
|
||||||
# replyTo: Shout
|
# replyTo: Shout
|
||||||
versionOf: Shout
|
versionOf: Shout
|
||||||
tags: [String] # actual values
|
tags: [String] # actual values
|
||||||
topics: [String] # topic-slugs, order has matter
|
topics: [Topic]
|
||||||
title: String
|
title: String
|
||||||
subtitle: String
|
subtitle: String
|
||||||
updatedAt: DateTime
|
updatedAt: DateTime
|
||||||
|
|
Loading…
Reference in New Issue
Block a user