lint wip
This commit is contained in:
@@ -18,12 +18,7 @@ class Following:
|
||||
|
||||
class FollowingManager:
|
||||
lock = asyncio.Lock()
|
||||
data = {
|
||||
'author': [],
|
||||
'topic': [],
|
||||
'shout': [],
|
||||
'chat': []
|
||||
}
|
||||
data = {'author': [], 'topic': [], 'shout': [], 'chat': []}
|
||||
|
||||
@staticmethod
|
||||
async def register(kind, uid):
|
||||
|
@@ -1,6 +1,6 @@
|
||||
from base.orm import local_session
|
||||
from services.search import SearchService
|
||||
from services.stat.viewed import ViewedStorage
|
||||
from base.orm import local_session
|
||||
|
||||
|
||||
async def storages_init():
|
||||
|
@@ -5,26 +5,18 @@ from datetime import datetime, timezone
|
||||
from sqlalchemy import and_
|
||||
|
||||
from base.orm import local_session
|
||||
from orm import Reaction, Shout, Notification, User
|
||||
from orm import Notification, Reaction, Shout, User
|
||||
from orm.notification import NotificationType
|
||||
from orm.reaction import ReactionKind
|
||||
from services.notifications.sse import connection_manager
|
||||
|
||||
|
||||
def shout_to_shout_data(shout):
|
||||
return {
|
||||
"title": shout.title,
|
||||
"slug": shout.slug
|
||||
}
|
||||
return {"title": shout.title, "slug": shout.slug}
|
||||
|
||||
|
||||
def user_to_user_data(user):
|
||||
return {
|
||||
"id": user.id,
|
||||
"name": user.name,
|
||||
"slug": user.slug,
|
||||
"userpic": user.userpic
|
||||
}
|
||||
return {"id": user.id, "name": user.name, "slug": user.slug, "userpic": user.userpic}
|
||||
|
||||
|
||||
def update_prev_notification(notification, user, reaction):
|
||||
@@ -57,34 +49,45 @@ class NewReactionNotificator:
|
||||
if reaction.kind == ReactionKind.COMMENT:
|
||||
parent_reaction = None
|
||||
if reaction.replyTo:
|
||||
parent_reaction = session.query(Reaction).where(Reaction.id == reaction.replyTo).one()
|
||||
parent_reaction = (
|
||||
session.query(Reaction).where(Reaction.id == reaction.replyTo).one()
|
||||
)
|
||||
if parent_reaction.createdBy != reaction.createdBy:
|
||||
prev_new_reply_notification = session.query(Notification).where(
|
||||
and_(
|
||||
Notification.user == shout.createdBy,
|
||||
Notification.type == NotificationType.NEW_REPLY,
|
||||
Notification.shout == shout.id,
|
||||
Notification.reaction == parent_reaction.id,
|
||||
Notification.seen == False
|
||||
prev_new_reply_notification = (
|
||||
session.query(Notification)
|
||||
.where(
|
||||
and_(
|
||||
Notification.user == shout.createdBy,
|
||||
Notification.type == NotificationType.NEW_REPLY,
|
||||
Notification.shout == shout.id,
|
||||
Notification.reaction == parent_reaction.id,
|
||||
Notification.seen == False,
|
||||
)
|
||||
)
|
||||
).first()
|
||||
.first()
|
||||
)
|
||||
|
||||
if prev_new_reply_notification:
|
||||
update_prev_notification(prev_new_reply_notification, user, reaction)
|
||||
else:
|
||||
reply_notification_data = json.dumps({
|
||||
"shout": shout_to_shout_data(shout),
|
||||
"users": [user_to_user_data(user)],
|
||||
"reactionIds": [reaction.id]
|
||||
}, ensure_ascii=False)
|
||||
reply_notification_data = json.dumps(
|
||||
{
|
||||
"shout": shout_to_shout_data(shout),
|
||||
"users": [user_to_user_data(user)],
|
||||
"reactionIds": [reaction.id],
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
|
||||
reply_notification = Notification.create(**{
|
||||
"user": parent_reaction.createdBy,
|
||||
"type": NotificationType.NEW_REPLY,
|
||||
"shout": shout.id,
|
||||
"reaction": parent_reaction.id,
|
||||
"data": reply_notification_data
|
||||
})
|
||||
reply_notification = Notification.create(
|
||||
**{
|
||||
"user": parent_reaction.createdBy,
|
||||
"type": NotificationType.NEW_REPLY,
|
||||
"shout": shout.id,
|
||||
"reaction": parent_reaction.id,
|
||||
"data": reply_notification_data,
|
||||
}
|
||||
)
|
||||
|
||||
session.add(reply_notification)
|
||||
|
||||
@@ -93,30 +96,39 @@ class NewReactionNotificator:
|
||||
if reaction.createdBy != shout.createdBy and (
|
||||
parent_reaction is None or parent_reaction.createdBy != shout.createdBy
|
||||
):
|
||||
prev_new_comment_notification = session.query(Notification).where(
|
||||
and_(
|
||||
Notification.user == shout.createdBy,
|
||||
Notification.type == NotificationType.NEW_COMMENT,
|
||||
Notification.shout == shout.id,
|
||||
Notification.seen == False
|
||||
prev_new_comment_notification = (
|
||||
session.query(Notification)
|
||||
.where(
|
||||
and_(
|
||||
Notification.user == shout.createdBy,
|
||||
Notification.type == NotificationType.NEW_COMMENT,
|
||||
Notification.shout == shout.id,
|
||||
Notification.seen == False,
|
||||
)
|
||||
)
|
||||
).first()
|
||||
.first()
|
||||
)
|
||||
|
||||
if prev_new_comment_notification:
|
||||
update_prev_notification(prev_new_comment_notification, user, reaction)
|
||||
else:
|
||||
notification_data_string = json.dumps({
|
||||
"shout": shout_to_shout_data(shout),
|
||||
"users": [user_to_user_data(user)],
|
||||
"reactionIds": [reaction.id]
|
||||
}, ensure_ascii=False)
|
||||
notification_data_string = json.dumps(
|
||||
{
|
||||
"shout": shout_to_shout_data(shout),
|
||||
"users": [user_to_user_data(user)],
|
||||
"reactionIds": [reaction.id],
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
|
||||
author_notification = Notification.create(**{
|
||||
"user": shout.createdBy,
|
||||
"type": NotificationType.NEW_COMMENT,
|
||||
"shout": shout.id,
|
||||
"data": notification_data_string
|
||||
})
|
||||
author_notification = Notification.create(
|
||||
**{
|
||||
"user": shout.createdBy,
|
||||
"type": NotificationType.NEW_COMMENT,
|
||||
"shout": shout.id,
|
||||
"data": notification_data_string,
|
||||
}
|
||||
)
|
||||
|
||||
session.add(author_notification)
|
||||
|
||||
|
@@ -1,8 +1,8 @@
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from sse_starlette.sse import EventSourceResponse
|
||||
from starlette.requests import Request
|
||||
import asyncio
|
||||
|
||||
|
||||
class ConnectionManager:
|
||||
@@ -28,9 +28,7 @@ class ConnectionManager:
|
||||
return
|
||||
|
||||
for connection in self.connections_by_user_id[user_id]:
|
||||
data = {
|
||||
"type": "newNotifications"
|
||||
}
|
||||
data = {"type": "newNotifications"}
|
||||
data_string = json.dumps(data, ensure_ascii=False)
|
||||
await connection.put(data_string)
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from base.redis import redis
|
||||
from orm.shout import Shout
|
||||
from resolvers.zine.load import load_shouts_by
|
||||
@@ -20,12 +21,7 @@ class SearchService:
|
||||
cached = await redis.execute("GET", text)
|
||||
if not cached:
|
||||
async with SearchService.lock:
|
||||
options = {
|
||||
"title": text,
|
||||
"body": text,
|
||||
"limit": limit,
|
||||
"offset": offset
|
||||
}
|
||||
options = {"title": text, "body": text, "limit": limit, "offset": offset}
|
||||
payload = await load_shouts_by(None, None, options)
|
||||
await redis.execute("SET", text, json.dumps(payload))
|
||||
return payload
|
||||
|
@@ -1,6 +1,6 @@
|
||||
import asyncio
|
||||
import time
|
||||
from datetime import timedelta, timezone, datetime
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from os import environ, path
|
||||
from ssl import create_default_context
|
||||
|
||||
@@ -9,10 +9,11 @@ from gql.transport.aiohttp import AIOHTTPTransport
|
||||
from sqlalchemy import func
|
||||
|
||||
from base.orm import local_session
|
||||
from orm import User, Topic
|
||||
from orm.shout import ShoutTopic, Shout
|
||||
from orm import Topic, User
|
||||
from orm.shout import Shout, ShoutTopic
|
||||
|
||||
load_facts = gql("""
|
||||
load_facts = gql(
|
||||
"""
|
||||
query getDomains {
|
||||
domains {
|
||||
id
|
||||
@@ -25,9 +26,11 @@ query getDomains {
|
||||
}
|
||||
}
|
||||
}
|
||||
""")
|
||||
"""
|
||||
)
|
||||
|
||||
load_pages = gql("""
|
||||
load_pages = gql(
|
||||
"""
|
||||
query getDomains {
|
||||
domains {
|
||||
title
|
||||
@@ -41,7 +44,8 @@ query getDomains {
|
||||
}
|
||||
}
|
||||
}
|
||||
""")
|
||||
"""
|
||||
)
|
||||
schema_str = open(path.dirname(__file__) + '/ackee.graphql').read()
|
||||
token = environ.get("ACKEE_TOKEN", "")
|
||||
|
||||
@@ -50,10 +54,8 @@ def create_client(headers=None, schema=None):
|
||||
return Client(
|
||||
schema=schema,
|
||||
transport=AIOHTTPTransport(
|
||||
url="https://ackee.discours.io/api",
|
||||
ssl=create_default_context(),
|
||||
headers=headers
|
||||
)
|
||||
url="https://ackee.discours.io/api", ssl=create_default_context(), headers=headers
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@@ -71,13 +73,13 @@ class ViewedStorage:
|
||||
|
||||
@staticmethod
|
||||
async def init():
|
||||
""" graphql client connection using permanent token """
|
||||
"""graphql client connection using permanent token"""
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
if token:
|
||||
self.client = create_client({
|
||||
"Authorization": "Bearer %s" % str(token)
|
||||
}, schema=schema_str)
|
||||
self.client = create_client(
|
||||
{"Authorization": "Bearer %s" % str(token)}, schema=schema_str
|
||||
)
|
||||
print("[stat.viewed] * authorized permanentely by ackee.discours.io: %s" % token)
|
||||
else:
|
||||
print("[stat.viewed] * please set ACKEE_TOKEN")
|
||||
@@ -85,7 +87,7 @@ class ViewedStorage:
|
||||
|
||||
@staticmethod
|
||||
async def update_pages():
|
||||
""" query all the pages from ackee sorted by views count """
|
||||
"""query all the pages from ackee sorted by views count"""
|
||||
print("[stat.viewed] ⎧ updating ackee pages data ---")
|
||||
start = time.time()
|
||||
self = ViewedStorage
|
||||
@@ -118,7 +120,7 @@ class ViewedStorage:
|
||||
# unused yet
|
||||
@staticmethod
|
||||
async def get_shout(shout_slug):
|
||||
""" getting shout views metric by slug """
|
||||
"""getting shout views metric by slug"""
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
shout_views = self.by_shouts.get(shout_slug)
|
||||
@@ -136,7 +138,7 @@ class ViewedStorage:
|
||||
|
||||
@staticmethod
|
||||
async def get_topic(topic_slug):
|
||||
""" getting topic views value summed """
|
||||
"""getting topic views value summed"""
|
||||
self = ViewedStorage
|
||||
topic_views = 0
|
||||
async with self.lock:
|
||||
@@ -146,18 +148,22 @@ class ViewedStorage:
|
||||
|
||||
@staticmethod
|
||||
def update_topics(session, shout_slug):
|
||||
""" updates topics counters by shout slug """
|
||||
"""updates topics counters by shout slug"""
|
||||
self = ViewedStorage
|
||||
for [shout_topic, topic] in session.query(ShoutTopic, Topic).join(Topic).join(Shout).where(
|
||||
Shout.slug == shout_slug
|
||||
).all():
|
||||
for [shout_topic, topic] in (
|
||||
session.query(ShoutTopic, Topic)
|
||||
.join(Topic)
|
||||
.join(Shout)
|
||||
.where(Shout.slug == shout_slug)
|
||||
.all()
|
||||
):
|
||||
if not self.by_topics.get(topic.slug):
|
||||
self.by_topics[topic.slug] = {}
|
||||
self.by_topics[topic.slug][shout_slug] = self.by_shouts[shout_slug]
|
||||
|
||||
@staticmethod
|
||||
async def increment(shout_slug, amount=1, viewer='ackee'):
|
||||
""" the only way to change views counter """
|
||||
"""the only way to change views counter"""
|
||||
self = ViewedStorage
|
||||
async with self.lock:
|
||||
# TODO optimize, currenty we execute 1 DB transaction per shout
|
||||
@@ -185,7 +191,7 @@ class ViewedStorage:
|
||||
|
||||
@staticmethod
|
||||
async def worker():
|
||||
""" async task worker """
|
||||
"""async task worker"""
|
||||
failed = 0
|
||||
self = ViewedStorage
|
||||
if self.disabled:
|
||||
@@ -205,9 +211,10 @@ class ViewedStorage:
|
||||
if failed == 0:
|
||||
when = datetime.now(timezone.utc) + timedelta(seconds=self.period)
|
||||
t = format(when.astimezone().isoformat())
|
||||
print("[stat.viewed] ⎩ next update: %s" % (
|
||||
t.split("T")[0] + " " + t.split("T")[1].split(".")[0]
|
||||
))
|
||||
print(
|
||||
"[stat.viewed] ⎩ next update: %s"
|
||||
% (t.split("T")[0] + " " + t.split("T")[1].split(".")[0])
|
||||
)
|
||||
await asyncio.sleep(self.period)
|
||||
else:
|
||||
await asyncio.sleep(10)
|
||||
|
Reference in New Issue
Block a user