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):
|
2023-10-25 18:33:53 +00:00
|
|
|
self.kind = kind # author topic shout community
|
2023-02-06 13:09:26 +00:00
|
|
|
self.uid = uid
|
|
|
|
|
|
|
|
|
|
|
|
class FollowingManager:
|
|
|
|
lock = asyncio.Lock()
|
2024-01-22 23:37:18 +00:00
|
|
|
followers_by_kind = {}
|
2023-11-28 07:53:48 +00:00
|
|
|
data = {"author": [], "topic": [], "shout": [], "community": []}
|
2023-02-06 13:09:26 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def register(kind, uid):
|
|
|
|
async with FollowingManager.lock:
|
2024-01-22 23:37:18 +00:00
|
|
|
FollowingManager.followers_by_kind[kind] = FollowingManager.followers_by_kind.get(kind, [])
|
|
|
|
FollowingManager.followers_by_kind[kind].append(uid)
|
2023-02-06 13:09:26 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def remove(kind, uid):
|
|
|
|
async with FollowingManager.lock:
|
2024-01-22 23:37:18 +00:00
|
|
|
followings = FollowingManager.followers_by_kind.get(kind)
|
|
|
|
if followings:
|
|
|
|
followings.remove(uid)
|
|
|
|
FollowingManager.followers_by_kind[kind] = followings
|
2023-02-06 13:09:26 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
async def push(kind, payload):
|
2023-02-20 17:38:20 +00:00
|
|
|
try:
|
|
|
|
async with FollowingManager.lock:
|
2024-01-22 23:41:37 +00:00
|
|
|
entities = FollowingManager.followers_by_kind.get(kind, [])
|
|
|
|
for entity in entities[:]: # Use a copy to iterate
|
2023-11-28 07:53:48 +00:00
|
|
|
if payload.shout["created_by"] == entity.uid:
|
2023-10-16 14:50:40 +00:00
|
|
|
entity.queue.put_nowait(payload)
|
2023-02-20 17:38:20 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(Exception(e))
|