core/services/following.py

47 lines
1.3 KiB
Python
Raw Permalink Normal View History

2023-02-06 13:09:26 +00:00
import asyncio
class FollowingResult:
def __init__(self, event, kind, payload):
self.event = event
self.kind = kind
self.payload = payload
class Following:
queue = asyncio.Queue()
def __init__(self, kind, uid):
self.kind = kind # author topic shout chat
self.uid = uid
class FollowingManager:
lock = asyncio.Lock()
2023-10-30 21:00:55 +00:00
data = {"author": [], "topic": [], "shout": [], "chat": []}
2023-02-06 13:09:26 +00:00
@staticmethod
async def register(kind, uid):
async with FollowingManager.lock:
FollowingManager[kind].append(uid)
@staticmethod
async def remove(kind, uid):
async with FollowingManager.lock:
FollowingManager[kind].remove(uid)
@staticmethod
async def push(kind, payload):
2023-02-20 17:38:20 +00:00
try:
async with FollowingManager.lock:
2023-10-30 21:00:55 +00:00
if kind == "chat":
for chat in FollowingManager["chat"]:
2023-02-20 17:38:20 +00:00
if payload.message["chatId"] == chat.uid:
chat.queue.put_nowait(payload)
else:
for entity in FollowingManager[kind]:
2023-10-30 21:00:55 +00:00
if payload.shout["createdBy"] == entity.uid:
2023-02-20 17:38:20 +00:00
entity.queue.put_nowait(payload)
except Exception as e:
print(Exception(e))