def systemInfo(): info = rs.info() lua = """local r1 = redis.call('keys', 'users:*'); return table.getn(r1);""" stat = {"users": rs.eval(lua, 0) - rs.scard("users:virtual")} #users = rs.keys("users:*") stat["battles"] = rs.zcard("battles_ids") stat["groups"] = rs.zcard("group_ids") stat["clans"] = rs.zcard("clan_ids") stat["online"] = rs.scard("users_online") stat["max_online"] = rs.get("stat:max_online") stat["disk_usage"] = getFolderSize(app.config['UPLOAD_FOLDER']) stat["redis_usage"] = info['used_memory_human'] #print "SYSTEM: ", os.popen("du -hs " + app.config['UPLOAD_FOLDER']).read().split()[0] return stat
def read_message(sid, mid): uid = loggedUserUid() if uid == 0: return -2 chid = "chat:message:%s:%s:%s" % (sid, uid, mid) if rs.exists(chid) != 1: return -1 if rs.hget(chid, "is_read") == "true": return 0 rs.hset(chid, "is_read", 'true') rs.zrem("chat:user:%s:unread" % uid, chid) battle_id = rs.hget(chid, "battle_id") if battle_id is not None: rs.zrem("battle:%s:unread" % battle_id, chid) rs.delete(chid) unread = get_unread._original() ntfs = rs.scard("chat:user:%s:ntfy" % uid) message = {"type": "unread", "content": {"count": len(unread)}} if ntfs > 0 and len(unread) > 0: message["content"]["message"] = unread[0] send_message_to_user(uid, json.dumps(message)) send_message_to_user( sid, json.dumps({ "type": "read_chat", "content": { "mid": mid, "rid": int(uid) } })) return 1
def unfollowUser(user_id): if rs.exists("users:" + str(user_id)) != 1: return -1 uid = loggedUserUid() if uid == 0: return -2 rs.srem('user:%s:following' % uid, user_id) rs.srem('user:%s:followers' % user_id, uid) rs.sdiffstore('user:%s:follownig_looks' % uid, 'user:%s:follownig_looks' % uid, 'user_looks:' + str(user_id)) return rs.scard('user:%s:followers' % user_id)
def battleRejectUser(battle_id, user_id): if rs.exists("battle:" + str(battle_id)) != 1: return -1 uid = loggedUserUid() if rs.hget("battle:%s" % battle_id, "uid") != str(uid): return -2 if uid == user_id: return -3 rs.zadd('battle:%s:users' % battle_id, user_id, 0) rs.srem('battle:%s:accepted' % battle_id, user_id) create_battle_notification(uid, user_id, battle_id, NTFY_BATTLE_REJECT) return rs.scard('battle:%s:accepted' % battle_id)
def battleAcceptUser(battle_id, user_id, admin=False): if rs.exists("battle:" + str(battle_id)) != 1: return -1 uid = loggedUserUid() if rs.hget("battle:%s" % battle_id, "uid") != str(uid) and not admin: return -2 rs.zadd('battle:%s:users' % battle_id, user_id, 1) rs.sadd('battle:%s:accepted' % battle_id, user_id) rs.zadd('user:%s:battles' % user_id, battle_id, rs.zscore("battles_ids", battle_id)) if uid != user_id: create_battle_notification(loggedUserUid(), user_id, battle_id, NTFY_BATTLE_ACCEPT) return rs.scard('battle:%s:accepted' % battle_id)
def on_chat_message(uid, msg): rid = int(msg.get('rid', 0)) if rid == 0 or len(msg.get('text', "")) == 0: return chid = "chat:message:%s:%s:" % (uid, rid) mid = rs.incr(chid + "counter") chid = chid + str(mid) score = calendar.timegm(datetime.utcnow().timetuple()) chatm = { "id": mid, "text": msg.get('text'), 'is_read': 'false', 'sid': uid, 'rid': rid, "type": "chat" } rs.hmset(chid, chatm) rs.zadd("chat:user:%s:unread" % rid, chid, score) rs.sadd("chat:user:%s:ntfy" % rid, chid) dialog = "chat:dialog:%s:%s" % (min(int(uid), rid), max(int(uid), rid)) rs.zadd(dialog, chid, score) rs.zadd("chat:user:%s:dialogs" % uid, dialog, score) rs.zadd("chat:user:%s:dialogs" % rid, dialog, score) chatm["create_date"] = score message = json.dumps({"type": "chat", "content": chatm}) send_message_to_user(uid, message) send_message_to_user(rid, message) ntfs = rs.scard("chat:user:%s:ntfy" % rid) unread = get_unread._original() unread_message = "" if ntfs > 0 and len(unread) > 0: unread_message = json.dumps({ "type": "unread", "content": { "count": len(unread), "message": unread[0] } }) send_message_to_user(rid, unread_message)
def followUserByUser(user_id, by_user_id): rs.sadd('user:%s:following' % by_user_id, user_id) rs.sadd('user:%s:followers' % user_id, by_user_id) rs.sunionstore('user:%s:follownig_looks' % by_user_id, 'user:%s:follownig_looks' % by_user_id, 'user_looks:' + str(user_id)) return rs.scard('user:%s:followers' % user_id)
def group_del_user(group_id, user_id): rs.srem("group:%s:users" % group_id, user_id) rs.zrem("user:%s:groups" % user_id, group_id) user_cnt = rs.scard('group:%s:users' % group_id) rs.zadd("group_ids", group_id, user_cnt) return user_cnt
def group_add_user(group_id, user_id): rs.sadd("group:%s:users" % group_id, user_id) rs.zadd("user:%s:groups" % user_id, group_id, rs.zcard("user:%s:groups" % user_id) + 1) user_cnt = rs.scard('group:%s:users' % group_id) rs.zadd("group_ids", group_id, user_cnt) return user_cnt
def websocket_api(): uid = loggedUserUid() if uid == 0: return "Not authorized" if request.environ.get('wsgi.websocket'): ws = request.environ['wsgi.websocket'] print "New user ", uid, "joined" if participants.get(str(uid)) is None: participants[str(uid)] = set() participants[str(uid)].add(ws) participants[str(uid)].add(ws) rs.sadd("users_online", uid) online_cnt = rs.scard("users_online") if online_cnt > int(rs.get("stat:max_online")): rs.set("stat:max_online", online_cnt) notify_online_status(uid, True) ntfs = rs.scard("chat:user:%s:ntfy" % uid) unread = get_unread._original() if ntfs > 0 and len(unread) > 0: ws.send( json.dumps({ "type": "unread", "content": { "count": len(unread), "message": unread[0] } })) rs.delete("chat:user:%s:ntfy" % uid) while True: try: message = ws.receive() if message is None: break try: evt = json.loads(message) # .encode('utf-8') #print "type =", evt.get("type") if evt.get("type") == "chat": on_chat_message(uid, evt.get("content")) if evt.get("type") == "battle_chat": on_battle_message(uid, evt.get("content")) if evt.get("type") == "group_chat": on_group_message(uid, evt.get("content")) if evt.get("type") == "clan_chat": on_clan_message(uid, evt.get("content")) if evt.get("type") == "read_chat": rm = evt.get("content") read_message._original(rm.get('sid'), rm.get('mid')) except: traceback.print_exc() print "Bad json", message, uid except: traceback.print_exc() break ws.close() print "User ", uid, "exit" rs.srem("users_online", uid) notify_online_status(uid, False) if participants.get(str(uid)) is not None: participants[str(uid)].remove(ws) if len(participants[str(uid)]) == 0: del participants[str(uid)] else: print "NO wsgi.websocket:(" return "OK"
def clan_del_user(clan_id, user_id): rs.srem("clan:%s:users" % clan_id, user_id) rs.delete("user:%s:clan" % user_id) user_cnt = rs.scard('clan:%s:users' % clan_id) rs.zadd("clan_ids", clan_id, user_cnt) return user_cnt
def clan_add_user(clan_id, user_id): rs.sadd("clan:%s:users" % clan_id, user_id) rs.set("user:%s:clan" % user_id, clan_id) user_cnt = rs.scard('clan:%s:users' % clan_id) rs.zadd("clan_ids", clan_id, user_cnt) return user_cnt