fmt2
This commit is contained in:
@@ -1,27 +1,36 @@
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
from typing import Dict, List
|
||||
|
||||
import strawberry
|
||||
from sqlalchemy import and_, select
|
||||
from sqlalchemy.orm import aliased
|
||||
from sqlalchemy.sql import not_
|
||||
from services.db import local_session
|
||||
|
||||
from orm.notification import (
|
||||
Notification,
|
||||
NotificationAction,
|
||||
NotificationEntity,
|
||||
NotificationSeen,
|
||||
)
|
||||
from resolvers.model import (
|
||||
NotificationReaction,
|
||||
NotificationGroup,
|
||||
NotificationShout,
|
||||
NotificationAuthor,
|
||||
NotificationGroup,
|
||||
NotificationReaction,
|
||||
NotificationShout,
|
||||
NotificationsResult,
|
||||
)
|
||||
from orm.notification import NotificationAction, NotificationEntity, NotificationSeen, Notification
|
||||
from typing import Dict, List
|
||||
import time, json
|
||||
import strawberry
|
||||
from sqlalchemy.orm import aliased
|
||||
from sqlalchemy.sql.expression import or_
|
||||
from sqlalchemy import select, and_
|
||||
import logging
|
||||
from services.db import local_session
|
||||
|
||||
logger = logging.getLogger("[resolvers.schema] ")
|
||||
|
||||
logger = logging.getLogger('[resolvers.schema] ')
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
async def get_notifications_grouped(author_id: int, after: int = 0, limit: int = 10, offset: int = 0):
|
||||
async def get_notifications_grouped( # noqa: C901
|
||||
author_id: int, after: int = 0, limit: int = 10, offset: int = 0
|
||||
):
|
||||
"""
|
||||
Retrieves notifications for a given author.
|
||||
|
||||
@@ -47,10 +56,13 @@ async def get_notifications_grouped(author_id: int, after: int = 0, limit: int =
|
||||
authors: List[NotificationAuthor], # List of authors involved in the thread.
|
||||
}
|
||||
"""
|
||||
NotificationSeenAlias = aliased(NotificationSeen)
|
||||
query = select(Notification, NotificationSeenAlias.viewer.label("seen")).outerjoin(
|
||||
seen_alias = aliased(NotificationSeen)
|
||||
query = select(Notification, seen_alias.viewer.label('seen')).outerjoin(
|
||||
NotificationSeen,
|
||||
and_(NotificationSeen.viewer == author_id, NotificationSeen.notification == Notification.id),
|
||||
and_(
|
||||
NotificationSeen.viewer == author_id,
|
||||
NotificationSeen.notification == Notification.id,
|
||||
),
|
||||
)
|
||||
if after:
|
||||
query = query.filter(Notification.created_at > after)
|
||||
@@ -62,23 +74,36 @@ async def get_notifications_grouped(author_id: int, after: int = 0, limit: int =
|
||||
notifications_by_thread: Dict[str, List[Notification]] = {}
|
||||
groups_by_thread: Dict[str, NotificationGroup] = {}
|
||||
with local_session() as session:
|
||||
total = session.query(Notification).filter(and_(Notification.action == NotificationAction.CREATE.value, Notification.created_at > after)).count()
|
||||
unread = session.query(Notification).filter(
|
||||
and_(
|
||||
Notification.action == NotificationAction.CREATE.value,
|
||||
Notification.created_at > after,
|
||||
not_(Notification.seen)
|
||||
total = (
|
||||
session.query(Notification)
|
||||
.filter(
|
||||
and_(
|
||||
Notification.action == NotificationAction.CREATE.value,
|
||||
Notification.created_at > after,
|
||||
)
|
||||
)
|
||||
).count()
|
||||
.count()
|
||||
)
|
||||
unread = (
|
||||
session.query(Notification)
|
||||
.filter(
|
||||
and_(
|
||||
Notification.action == NotificationAction.CREATE.value,
|
||||
Notification.created_at > after,
|
||||
not_(Notification.seen),
|
||||
)
|
||||
)
|
||||
.count()
|
||||
)
|
||||
notifications_result = session.execute(query)
|
||||
for n, seen in notifications_result:
|
||||
thread_id = ""
|
||||
for n, _seen in notifications_result:
|
||||
thread_id = ''
|
||||
payload = json.loads(n.payload)
|
||||
logger.debug(f"[resolvers.schema] {n.action} {n.entity}: {payload}")
|
||||
if n.entity == "shout" and n.action == "create":
|
||||
logger.debug(f'[resolvers.schema] {n.action} {n.entity}: {payload}')
|
||||
if n.entity == 'shout' and n.action == 'create':
|
||||
shout: NotificationShout = payload
|
||||
thread_id += f"{shout.id}"
|
||||
logger.debug(f"create shout: {shout}")
|
||||
thread_id += f'{shout.id}'
|
||||
logger.debug(f'create shout: {shout}')
|
||||
group = groups_by_thread.get(thread_id) or NotificationGroup(
|
||||
id=thread_id,
|
||||
entity=n.entity,
|
||||
@@ -86,8 +111,8 @@ async def get_notifications_grouped(author_id: int, after: int = 0, limit: int =
|
||||
authors=shout.authors,
|
||||
updated_at=shout.created_at,
|
||||
reactions=[],
|
||||
action="create",
|
||||
seen=author_id in n.seen
|
||||
action='create',
|
||||
seen=author_id in n.seen,
|
||||
)
|
||||
# store group in result
|
||||
groups_by_thread[thread_id] = group
|
||||
@@ -99,11 +124,11 @@ async def get_notifications_grouped(author_id: int, after: int = 0, limit: int =
|
||||
elif n.entity == NotificationEntity.REACTION.value and n.action == NotificationAction.CREATE.value:
|
||||
reaction: NotificationReaction = payload
|
||||
shout: NotificationShout = reaction.shout
|
||||
thread_id += f"{reaction.shout}"
|
||||
if reaction.kind == "LIKE" or reaction.kind == "DISLIKE":
|
||||
thread_id += f'{reaction.shout}'
|
||||
if not bool(reaction.reply_to) and (reaction.kind == 'LIKE' or reaction.kind == 'DISLIKE'):
|
||||
# TODO: making published reaction vote announce
|
||||
pass
|
||||
elif reaction.kind == "COMMENT":
|
||||
elif reaction.kind == 'COMMENT':
|
||||
if reaction.reply_to:
|
||||
thread_id += f"{'::' + str(reaction.reply_to)}"
|
||||
group: NotificationGroup | None = groups_by_thread.get(thread_id)
|
||||
@@ -128,8 +153,9 @@ async def get_notifications_grouped(author_id: int, after: int = 0, limit: int =
|
||||
break
|
||||
else:
|
||||
# init notification group
|
||||
reactions = []
|
||||
reactions.append(reaction.id)
|
||||
reactions = [
|
||||
reaction.id,
|
||||
]
|
||||
group = NotificationGroup(
|
||||
id=thread_id,
|
||||
action=n.action,
|
||||
@@ -140,7 +166,7 @@ async def get_notifications_grouped(author_id: int, after: int = 0, limit: int =
|
||||
authors=[
|
||||
reaction.created_by,
|
||||
],
|
||||
seen=author_id in n.seen
|
||||
seen=author_id in n.seen,
|
||||
)
|
||||
# store group in result
|
||||
groups_by_thread[thread_id] = group
|
||||
@@ -149,20 +175,22 @@ async def get_notifications_grouped(author_id: int, after: int = 0, limit: int =
|
||||
notifications.append(n)
|
||||
notifications_by_thread[thread_id] = notifications
|
||||
|
||||
elif n.entity == "follower":
|
||||
thread_id = "followers"
|
||||
elif n.entity == 'follower':
|
||||
thread_id = 'followers'
|
||||
follower: NotificationAuthor = payload
|
||||
group = groups_by_thread.get(thread_id) or NotificationGroup(
|
||||
id=thread_id,
|
||||
authors=[follower],
|
||||
updated_at=int(time.time()),
|
||||
shout=None,
|
||||
reactions=[],
|
||||
entity="follower",
|
||||
action="follow",
|
||||
seen=author_id in n.seen
|
||||
)
|
||||
group.authors = [follower, ]
|
||||
id=thread_id,
|
||||
authors=[follower],
|
||||
updated_at=int(time.time()),
|
||||
shout=None,
|
||||
reactions=[],
|
||||
entity='follower',
|
||||
action='follow',
|
||||
seen=author_id in n.seen,
|
||||
)
|
||||
group.authors = [
|
||||
follower,
|
||||
]
|
||||
group.updated_at = int(time.time())
|
||||
# store group in result
|
||||
groups_by_thread[thread_id] = group
|
||||
@@ -182,7 +210,7 @@ async def get_notifications_grouped(author_id: int, after: int = 0, limit: int =
|
||||
class Query:
|
||||
@strawberry.field
|
||||
async def load_notifications(self, info, after: int, limit: int = 50, offset: int = 0) -> NotificationsResult:
|
||||
author_id = info.context.get("author_id")
|
||||
author_id = info.context.get('author_id')
|
||||
groups: Dict[str, NotificationGroup] = {}
|
||||
if author_id:
|
||||
groups, notifications, total, unread = await get_notifications_grouped(author_id, after, limit, offset)
|
||||
|
Reference in New Issue
Block a user