init-following-manager
This commit is contained in:
48
services/following.py
Normal file
48
services/following.py
Normal file
@@ -0,0 +1,48 @@
|
||||
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()
|
||||
data = {
|
||||
'author': [],
|
||||
'topic': [],
|
||||
'shout': [],
|
||||
'chat': []
|
||||
}
|
||||
|
||||
@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):
|
||||
async with FollowingManager.lock:
|
||||
if kind == 'chat':
|
||||
for chat in FollowingManager['chat']:
|
||||
if payload.message["chatId"] == chat.uid:
|
||||
chat.queue.put_nowait(payload)
|
||||
else:
|
||||
for entity in FollowingManager[kind]:
|
||||
if payload.shout['createdBy'] == entity.uid:
|
||||
entity.queue.put_nowait(payload)
|
@@ -1,14 +0,0 @@
|
||||
import asyncio
|
||||
|
||||
|
||||
class MessageResult:
|
||||
def __init__(self, status, message):
|
||||
self.seen = status
|
||||
self.message = message
|
||||
|
||||
|
||||
class ChatFollowing:
|
||||
queue = asyncio.Queue()
|
||||
|
||||
def __init__(self, chat_id):
|
||||
self.chat_id = chat_id
|
@@ -1,23 +0,0 @@
|
||||
import asyncio
|
||||
|
||||
|
||||
class MessagesStorage:
|
||||
lock = asyncio.Lock()
|
||||
chats = []
|
||||
|
||||
@staticmethod
|
||||
async def register_chat(chat):
|
||||
async with MessagesStorage.lock:
|
||||
MessagesStorage.chats.append(chat)
|
||||
|
||||
@staticmethod
|
||||
async def remove_chat(chat):
|
||||
async with MessagesStorage.lock:
|
||||
MessagesStorage.chats.remove(chat)
|
||||
|
||||
@staticmethod
|
||||
async def put(message_result):
|
||||
async with MessagesStorage.lock:
|
||||
for chat in MessagesStorage.chats:
|
||||
if message_result.message["chatId"] == chat.chat_id:
|
||||
chat.queue.put_nowait(message_result)
|
Reference in New Issue
Block a user