forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscription.py
More file actions
147 lines (134 loc) ยท 4.66 KB
/
Copy pathsubscription.py
File metadata and controls
147 lines (134 loc) ยท 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
Subscription Manager - v3.0 ่ฎข้
ๅน้
็ณป็ป
่็นๆ้ขๅ่ฎข้
๏ผHub ็ฒพๅๆจ้่้ๅนฟๆญ
"""
import sqlite3
import os
from datetime import datetime
from typing import Optional
from dataclasses import dataclass
@dataclass
class Subscription:
"""่็น่ฎข้
"""
id: int
agent_id: str
domain: str
created_at: str
active: bool = True
class SubscriptionManager:
"""
่ฎข้
็ฎก็ๅจ
- ่็น่ฎข้
็นๅฎ้ขๅ
- ๆจ้ๆถๅน้
่ฎข้
่
่้ๅนฟๆญ
"""
def __init__(self, db_path: str = "./storage/subscriptions.db"):
self.db_path = db_path
self._init_db()
def _init_db(self):
"""ๅๅงๅ่ฎข้
ๆฐๆฎๅบ"""
os.makedirs(os.path.dirname(self.db_path), exist_ok=True)
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS subscriptions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
agent_id TEXT NOT NULL,
domain TEXT NOT NULL,
created_at TEXT NOT NULL,
active INTEGER DEFAULT 1,
UNIQUE(agent_id, domain)
)
""")
conn.commit()
conn.close()
def subscribe(self, agent_id: str, domain: str) -> bool:
"""่ฎข้
้ขๅ"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
try:
cursor.execute("""
INSERT OR REPLACE INTO subscriptions (agent_id, domain, created_at, active)
VALUES (?, ?, ?, 1)
""", (agent_id, domain, datetime.now().isoformat()))
conn.commit()
conn.close()
print(f"[Subscription] {agent_id} ่ฎข้
ไบ {domain}")
return True
except Exception as e:
conn.close()
return False
def unsubscribe(self, agent_id: str, domain: str) -> bool:
"""ๅๆถ่ฎข้
"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""
UPDATE subscriptions SET active = 0 WHERE agent_id = ? AND domain = ?
""", (agent_id, domain))
affected = cursor.rowcount
conn.commit()
conn.close()
return affected > 0
def get_subscribers(self, domain: str) -> list[str]:
"""่ทๅๆ้ขๅ็ๆๆ่ฎข้
่
"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""
SELECT agent_id FROM subscriptions
WHERE domain = ? AND active = 1
""", (domain,))
rows = cursor.fetchall()
conn.close()
return [row[0] for row in rows]
def get_subscriptions(self, agent_id: str) -> list[str]:
"""่ทๅๆ่็น็ๆๆ่ฎข้
้ขๅ"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""
SELECT domain FROM subscriptions
WHERE agent_id = ? AND active = 1
""", (agent_id,))
rows = cursor.fetchall()
conn.close()
return [row[0] for row in rows]
def get_all_subscriptions(self) -> list[Subscription]:
"""่ทๅๆๆ่ฎข้
่ฎฐๅฝ"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""
SELECT id, agent_id, domain, created_at, active FROM subscriptions
WHERE active = 1 ORDER BY created_at DESC
""")
rows = cursor.fetchall()
conn.close()
return [
Subscription(id=row[0], agent_id=row[1], domain=row[2],
created_at=row[3], active=bool(row[4]))
for row in rows
]
def match_subscribers(self, skill_domain: str) -> list[str]:
"""
ๅน้
่ฎข้
่
ๆฏๆ้้
็ฌฆ * (ๅ
จ้จ่ฎข้
)
"""
subscribers = self.get_subscribers(skill_domain)
# ไนๅ ๅ
ฅ้้
่ฎข้
่
wildcard_subscribers = self.get_subscribers("*")
return list(set(subscribers + wildcard_subscribers))
def stats(self) -> dict:
"""่ทๅ่ฎข้
็ป่ฎก"""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute("""
SELECT COUNT(*) FROM subscriptions WHERE active = 1
""")
total = cursor.fetchone()[0]
cursor.execute("""
SELECT COUNT(DISTINCT agent_id) FROM subscriptions WHERE active = 1
""")
agents = cursor.fetchone()[0]
cursor.execute("""
SELECT COUNT(DISTINCT domain) FROM subscriptions WHERE active = 1
""")
domains = cursor.fetchone()[0]
conn.close()
return {"total": total, "agents": agents, "domains": domains}