Пример #1
0
def getUserFollowing(user_id):
    if rs.exists("users:" + str(user_id)) != 1:
        return []
    offset = request.args.get("offset", 0)
    count = request.args.get("count", 20)
    rows = rs.sort('user:'******':following', start=offset, num=count, desc=True, get='users:*')
    lua = """local r1 = redis.call('sort', 'user:'******':following', 'LIMIT', KEYS[3], KEYS[4],
    'GET', 'users:*', 'GET', '#', 'GET', '#', 'GET', '#');
for i = 1, table.getn(r1) do
  if i % 4 == 1 then
    r1[i+1] = redis.call('sismember', 'user:'******':followers', KEYS[2])
    r1[i+2] = redis.call('sismember', 'users:virtual', r1[i+2])
    r1[i+3] = redis.call('sismember', 'users_online', r1[i+3])
  end
end
return r1;"""
    rows = rs.eval(lua, 4, user_id, loggedUserUid(), offset, count)
    users = []
    for i in range(0, len(rows) - 1):
        if i % 4 != 0 or rows[i] is None:
            continue
        u = json.loads(rows[i])
        u['is_follow'] = rows[i + 1]
        u['is_online'] = rows[i + 3]
        u['virtual'] = rows[i + 2]
        users.append(u)
    return users
Пример #2
0
def searchuser():
    q = request.args.get("q", "")  # repr
    offset = int(request.args.get("offset", 0))
    count = int(request.args.get("count", 20))
    with uix.searcher() as searcher:
        query = QueryParser("nickname", uix.schema).parse(
            "nickname:*%s*" %
            q)  # QueryParser("name", ix.schema).parse("tash*")
        #print query
        user_id = sorting.FieldFacet("user_id", reverse=True)
        results = searcher.search_page(query,
                                       max(offset / count, 0) + 1,
                                       pagelen=count,
                                       sortedby=user_id)
        print results.offset, count, offset, max(offset / count, 0) + 1
        if results.offset < offset:
            return "[]"
        tmp = hashlib.md5(str(mktime(datetime.datetime.now().timetuple()))
                          ).hexdigest() + "user_search_tmp"
        lua = """local searched = loadstring('return ' .. KEYS[1])()
for i = 1, table.getn(searched) do
    redis.call('sadd', KEYS[2], tostring(searched[i]))
end
local arr = redis.call('sort', KEYS[2], 'GET', 'users:*')
return arr"""
        res = rs.eval(
            lua, 2,
            '{' + ','.join(str(hit['user_id']) for hit in results) + '}', tmp)
        rs.delete(tmp)
        r = ",".join(res)
    return "[" + r + "]"
Пример #3
0
def get_battle_unread(battle_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 rs.exists("battle:%s:unread" % battle_id) != 1:
        return []
    offset = int(request.args.get("offset", 0))
    count = int(request.args.get("count", 10))
    rows = []
    lua = """local r1 = redis.call('ZREVRANGE', KEYS[1], KEYS[2], KEYS[3]);
for i = 1, table.getn(r1) do
  local chid = r1[i]
  r1[i] = {}
  r1[i][1] = redis.call('hget', chid, 'id')
  r1[i][2] = redis.call('hget', chid, 'text')
  r1[i][3] = redis.call('zscore', KEYS[1], chid)
  local uid = redis.call('hget', chid, 'rid')
  r1[i][4] = redis.call('get', 'users:' .. tostring(uid))
  r1[i][5] = redis.call('sismember', 'users_online', uid)
end
return r1;"""
    ids = rs.eval(lua, 3, "battle:%s:unread" % battle_id, offset,
                  offset + count - 1)
    for cmid in ids:
        cmnt = {
            'id': int(cmid[0]),
            'text': cmid[1],
            'create_date': int(cmid[2])
        }
        cmnt['user'] = json.loads(cmid[3])
        cmnt['user']['is_online'] = cmid[4]
        rows.append(cmnt)
    return rows
Пример #4
0
def get_group_users(group_id):
    #if rs.exists("group:%s" % group_id) != 1:
    #    return []
    offset = request.args.get("offset", 0)
    count = request.args.get("count", 20)
    lua = """local r1 = redis.call('sort', 'group:'..tostring(KEYS[1])..':users', 'DESC', 'LIMIT', KEYS[3], KEYS[4],
    'GET', 'users:*', 'GET', '#', 'GET', '#', 'GET', '#');
for i = 1, table.getn(r1) do
  if i % 4 == 1 then
    r1[i+1] = redis.call('sismember', 'user:'******':followers', KEYS[2])
    r1[i+2] = redis.call('sismember', 'users:virtual', r1[i+2])
    r1[i+3] = redis.call('sismember', 'users_online', r1[i+3])
  end
end
return r1;"""
    rows = rs.eval(lua, 4, group_id, loggedUserUid(), offset, count)
    users = []
    for i in range(0, len(rows) - 1):
        if i % 4 != 0 or rows[i] is None:
            continue
        u = json.loads(rows[i])
        u['is_follow'] = rows[i + 1]
        u['is_online'] = rows[i + 3]
        u['virtual'] = rows[i + 2]
        users.append(u)
    return users
Пример #5
0
def get_multi_chat_history(dialog_key):
    if rs.exists(dialog_key) != 1:
        return []
    offset = int(request.args.get("offset", 0))
    count = int(request.args.get("count", 10))
    rows = []
    lua = """local r1 = redis.call('ZREVRANGE', KEYS[1], KEYS[2], KEYS[3]);
for i = 1, table.getn(r1) do
  local chid = r1[i]
  r1[i] = {}
  r1[i][1] = redis.call('hget', chid, 'id')
  r1[i][2] = redis.call('hget', chid, 'text')
  r1[i][3] = redis.call('zscore', KEYS[1], chid)
  r1[i][4] = redis.call('hget', chid, 'sid')
  r1[i][5] = redis.call('get', 'users:'..r1[i][4])
end
return r1;"""
    ids = rs.eval(lua, 3, dialog_key, offset, offset + count - 1)
    for cmid in ids:
        cmnt = {
            'id': int(cmid[0]),
            'text': cmid[1],
            'create_date': int(cmid[2]),
            'sid': int(cmid[3]),
            'user': json.loads(cmid[4])
        }
        rows.append(cmnt)
    return rows
Пример #6
0
def get_allowed_battles():
    uid = loggedUserUid()
    if uid == 0:
        return []
    lua = """local r1 = redis.call('sort', 'user:'******':followers', 'GET', 'user:*->battles');
for i = 1, table.getn(r1) do
  redis.call('zadd', KEYS[1], redis.call('hget', 'battle:'..tostring(r1[i]), 'battle_date'), r1[i]);
end
redis.call('zinterstore', KEYS[1], 2, KEYS[1], 'privacy:PRIVATE', 'AGGREGATE', 'MIN');
redis.call('zunionstore', KEYS[1], 4, KEYS[1], 'user:'******':battles','privacy:ALL', 'user_battles:'..tostring(KEYS[2]), 'AGGREGATE', 'MIN');
return 1;"""
    tmp = hashlib.md5(str(mktime(datetime.now().timetuple()))).hexdigest(
    ) + "allowed_battle_" + str(uid)
    #rs.sort('user:%s:followers' % uid, get=["user:*->battles"], store=tmp)
    #rs.zinterstore(tmp, tmp, "privacy:PRIVATE", aggregate="MIN")
    #rs.zinterstore(tmp, tmp, 'user:%s:battles' % uid, "privacy:ALL", "user_battles:%s" % uid, aggregate="MIN")
    rs.eval(lua, 2, tmp, uid)
    res = get_battles_by_set(tmp)
    rs.delete(tmp)
    return res
Пример #7
0
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
Пример #8
0
def get_dialogs():
    uid = loggedUserUid()
    if uid == 0:
        return []
    if rs.exists("chat:user:%s:dialogs" % uid) != 1:
        return []
    offset = int(request.args.get("offset", 0))
    count = int(request.args.get("count", 10))
    rows = []
    lua = """local r1 = redis.call('ZREVRANGE', KEYS[1], KEYS[2], KEYS[3]);
for i = 1, table.getn(r1) do
  local dialog = r1[i]
  local chid = redis.call('ZREVRANGE', dialog, 0, 0)[1];
  r1[i] = {}
  r1[i][1] = redis.call('hget', chid, 'id')
  r1[i][2] = redis.call('hget', chid, 'text')
  r1[i][3] = redis.call('zscore', dialog, chid)
  local uid = redis.call('hget', chid, 'sid')
  r1[i][4] = redis.call('get', 'users:' .. tostring(uid))
  local uid = redis.call('hget', chid, 'rid')
  r1[i][5] = redis.call('get', 'users:' .. tostring(uid))
  r1[i][6] = redis.call('sismember', 'users_online', uid)
  r1[i][7] = redis.call('hget', chid, 'is_read')
end
return r1;"""
    ids = rs.eval(lua, 3, "chat:user:%s:dialogs" % uid, offset,
                  offset + count - 1)
    #ids = rs.sort('look:' + str(look_id) + ':comments', start=offset, num=count, get='#')
    for cmid in ids:
        is_read = False if cmid[6] == 'False' else json.loads(cmid[6])
        cmnt = {
            'id': int(cmid[0]),
            'text': cmid[1],
            'create_date': int(cmid[2]),
            'is_read': is_read
        }
        cmnt['user'] = json.loads(cmid[3])
        cmnt['user']['is_online'] = cmid[5]
        cmnt['companion'] = json.loads(
            cmid[3]) if uid != cmnt['user']['id'] else json.loads(cmid[4])
        rows.append(cmnt)
    return rows
Пример #9
0
def get_battles_arr_by_set(key, by2=None, offset=0, count=20):
    #start_time = time.time()
    uid = loggedUserUid()
    battles = []
    lua = """local r1 = redis.call('ZRANGEBYSCORE', KEYS[1], KEYS[2], '+inf', 'LIMIT', KEYS[4], KEYS[5]);
for i = 1, table.getn(r1) do
  local bid = r1[i];
  r1[i] = {}
  r1[i][1] = redis.call('HGET', 'battle:'..bid, 'data');
  local uid = redis.call('HGET', 'battle:'..bid, 'uid');
  r1[i][2] = redis.call('get', 'users:' .. uid);
  r1[i][3] = redis.call('sismember', 'users_online', uid);
--    r1[i+3] = redis.call('smembers', 'battle:' .. tostring(r1[i+3]) .. ':tanks')
  r1[i][4] = redis.call('sismember', 'battle:' .. bid .. ':accepted', KEYS[3])
  r1[i][5] = redis.call('zrank', 'battle:' .. bid .. ':users', KEYS[3])
  r1[i][6] = redis.call('scard', 'battle:' .. bid .. ':accepted')
  r1[i][7] = redis.call('zcard', 'battle:' .. bid .. ':users')
  r1[i][8] = redis.call('sismember', 'battle:' .. bid .. ':invited', KEYS[3])
  r1[i][9] = redis.call('smembers', 'battle:' .. bid .. ':reminders')
end
return r1;"""
    #
    # str(by2) if by2 is not None else
    rows = rs.eval(lua, 5, key,
                   int(calendar.timegm(datetime.utcnow().timetuple())), uid,
                   offset, count)
    for b in rows:
        l = json.loads(b[0])
        l['user'] = json.loads(b[1])
        l['user']['is_online'] = b[2]
        l['tanks'] = []  # rows[i + 3]
        l['reminders'] = b[8]
        l['is_accepted'] = b[3]
        l['is_invited'] = b[7]
        l['is_follow'] = 1 if b[4] >= 0 else 0
        l['accepted'] = b[5]
        l['followers'] = b[6]
        battles.append(l)

    #print time.time() - start_time, "seconds"
    return battles
Пример #10
0
def get_user_unread(uid):
    if rs.exists("chat:user:%s:unread" % uid) != 1:
        return []
    try:
        offset = int(request.args.get("offset", 0))
        count = int(request.args.get("count", 10))
    except:
        offset = 0
        count = 10
    rows = []
    lua = """local r1 = redis.call('ZREVRANGE', KEYS[1], KEYS[2], KEYS[3]);
for i = 1, table.getn(r1) do
  local chid = r1[i]
  r1[i] = {}
  r1[i][1] = redis.call('hget', chid, 'id')
  r1[i][2] = redis.call('hget', chid, 'text')
  r1[i][3] = redis.call('zscore', KEYS[1], chid)
  local uid = redis.call('hget', chid, 'sid')
  r1[i][4] = redis.call('get', 'users:' .. tostring(uid))
  r1[i][5] = redis.call('hget', chid, 'type')
  r1[i][6] = redis.call('hget', chid, 'battle_id')
  r1[i][7] = redis.call('sismember', 'users_online', uid)
end
return r1;"""
    ids = rs.eval(lua, 3, "chat:user:%s:unread" % uid, offset,
                  offset + count - 1)
    #ids = rs.sort('look:' + str(look_id) + ':comments', start=offset, num=count, get='#')
    for cmid in ids:
        cmnt = {
            'id': int(cmid[0]),
            'text': cmid[1],
            'create_date': int(cmid[2]),
            "type": cmid[4]
        }
        if cmid[5] is not None:
            cmnt["battle_id"] = cmid[5]
        cmnt['user'] = json.loads(cmid[3])
        cmnt['user']['is_online'] = cmid[6]
        rows.append(cmnt)
    return rows
Пример #11
0
def followDiff():
    uid = loggedUserUid()
    if uid == 0:
        return '-2'
    app_friends = request.stream.read()
    try:
        app_friends = json.loads(app_friends)
    except:
        return '-3'
    lua = """local app_friends = loadstring('return ' .. KEYS[1])()
for i = 1, table.getn(app_friends) do
    redis.call('sadd', KEYS[2], redis.call('hget', 'wot_user:'******'uid'))
end
redis.call('sunionstore', KEYS[2], KEYS[2], 'user:'******':following')
redis.call('sdiffstore', KEYS[2], KEYS[2], 'user:'******':following')
local r = redis.call('sort', KEYS[2], 'get', 'users:*')
redis.call('del', KEYS[2])
return r"""
    """
    """
    tmp = hashlib.md5(str(mktime(datetime.now().timetuple())) + str(uid)).hexdigest() + "following_tmp"
    diff = rs.eval(lua, 3, '{' + ','.join(str(x) for x in app_friends) + '}', tmp, uid)
    return '[' + ','.join(diff) + ']'
Пример #12
0
def get_groups_arr_by_set(key, offset=0, count=20):
    uid = loggedUserUid()
    groups = []
    lua = """local r1 = redis.call('ZREVRANGEBYSCORE', KEYS[1], '+inf', '-inf', 'LIMIT', KEYS[3], KEYS[4]);
for i = 1, table.getn(r1) do
  local gid = r1[i];
  r1[i] = {}
  r1[i][1] = redis.call('HGET', 'group:'..gid, 'data');
  local uid = redis.call('HGET', 'group:'..gid, 'uid');
  r1[i][2] = redis.call('get', 'users:' .. uid);
  r1[i][3] = redis.call('sismember', 'users_online', uid);
  r1[i][4] = redis.call('sismember', 'group:' .. gid .. ':users', KEYS[2])
  r1[i][5] = redis.call('scard', 'group:' .. gid .. ':users')
end
return r1;"""
    rows = rs.eval(lua, 4, key, uid, offset, count)
    for b in rows:
        l = json.loads(b[0])
        l['user'] = json.loads(b[1])
        l['user']['is_online'] = b[2]
        l['is_follow'] = b[3]
        l['followers'] = b[4]
        groups.append(l)
    return groups
Пример #13
0
def get_chat_history(sid):
    uid = loggedUserUid()
    if uid == 0:
        return []
    dialog = "chat:dialog:%s:%s" % (min(int(uid), sid), max(int(uid), sid))
    if rs.exists(dialog) != 1:
        return []
    offset = int(request.args.get("offset", 0))
    count = int(request.args.get("count", 10))
    rows = []
    lua = """local r1 = redis.call('ZREVRANGE', KEYS[1], KEYS[2], KEYS[3]);
for i = 1, table.getn(r1) do
  local chid = r1[i]
  r1[i] = {}
  r1[i][1] = redis.call('hget', chid, 'id')
  r1[i][2] = redis.call('hget', chid, 'text')
  r1[i][3] = redis.call('zscore', KEYS[1], chid)
  r1[i][4] = redis.call('hget', chid, 'sid')
  r1[i][5] = redis.call('hget', chid, 'rid')
  r1[i][6] = redis.call('hget', chid, 'is_read')
end
return r1;"""
    ids = rs.eval(lua, 3, dialog, offset, offset + count - 1)
    #ids = rs.sort('look:' + str(look_id) + ':comments', start=offset, num=count, get='#')
    for cmid in ids:
        is_read = False if cmid[5] == 'False' else json.loads(cmid[5])
        cmnt = {
            'id': int(cmid[0]),
            'text': cmid[1],
            'create_date': int(cmid[2]),
            'is_read': is_read,
            'sid': int(cmid[3]),
            'rid': int(cmid[4])
        }
        rows.append(cmnt)
    return rows