Implement getting number of channels and users

This commit is contained in:
Mark Veidemanis 2022-07-21 13:40:18 +01:00
parent a8d0a7d886
commit 8c9ec3ab9c
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 61 additions and 1 deletions

View File

@ -119,3 +119,41 @@ class API(object):
usermap[user] = False usermap[user] = False
return dumps(usermap) return dumps(usermap)
@app.route("/num_users/", methods=["POST"])
@login_required
def num_users(self, request):
try:
data = loads(request.content.read())
except JSONDecodeError:
return "Invalid JSON"
if "net" not in data:
return "No net provided"
if "query" not in data:
return "No users provided"
if not data["query"]:
warn(f"No chans provided: for online {data}")
return dumps({})
net = data["net"]
results = userinfo.getUserNum(net, data["query"])
return dumps(results)
@app.route("/num_chans/", methods=["POST"])
@login_required
def num_chans(self, request):
try:
data = loads(request.content.read())
except JSONDecodeError:
return "Invalid JSON"
if "net" not in data:
return "No net provided"
if "query" not in data:
return "No users provided"
if not data["query"]:
warn(f"No users provided: for online {data}")
return dumps({})
net = data["net"]
results = userinfo.getChanNum(net, data["query"])
return dumps(results)

View File

@ -23,7 +23,7 @@ def getWho(query):
def getChansSingle(name, nick): def getChansSingle(name, nick):
nick = ["live.chan." + name + "." + i for i in nick] nick = ("live.chan." + name + "." + i for i in nick)
result = main.r.sinter(*nick) result = main.r.sinter(*nick)
if len(result) == 0: if len(result) == 0:
return None return None
@ -38,6 +38,28 @@ def getChanList(name, nick):
return (i.decode() for i in result) return (i.decode() for i in result)
def getUserNum(name, channels):
"""
Get the number of users on a list of channels.
"""
chanspace = ("live.who." + name + "." + i for i in channels)
results = {}
for channel, space in zip(channels, chanspace):
results[channel] = main.r.scard(space)
return results
def getChanNum(name, nicks):
"""
Get the number of channels a list of users are on.
"""
nickspace = ("live.chan." + name + "." + i for i in nicks)
results = {}
for nick, space in zip(nicks, nickspace):
results[nick] = main.r.scard(space)
return results
def getChans(nick): def getChans(nick):
result = {} result = {}
for i in main.network.keys(): for i in main.network.keys():