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 delete_battle(battle_id, admin=0): if rs.exists("battle:%s" % battle_id) == 1 and (admin == 1 or str( loggedUserUid()) == rs.hget("battle:%s" % battle_id, 'uid')): uid = rs.hget("battle:%s" % battle_id, 'uid') rs.zrem("user_battles:" + uid, battle_id) rs.zrem("battles_ids", battle_id) battle = json.loads(rs.hget("battle:%s" % battle_id, 'data')) rs.zrem("battles:%s" % battle['type'], battle_id) rs.zrem("privacy:%s" % battle['privacy'], battle_id) tanks = rs.smembers("battle:%s:tanks" % battle_id) for tank_id in tanks: rs.srem("tank:%s:battles" % tank_id, battle_id) users = rs.zrange('battle:%s:users' % battle_id, 0, -1) from warg.views.battle_followers import unFollowBattleByUser from warg.views.notifications import create_battle_notification, NTFY_BATTLE_KICK for user_id in users: unFollowBattleByUser(battle_id, int(user_id)) if user_id != uid: create_battle_notification(uid, user_id, battle_id, NTFY_BATTLE_KICK) rs.delete("battle:%s:tanks" % battle_id) from warg.views.jobs.battle_reminder import remove_battle_reminders remove_battle_reminders(battle_id) rs.delete("battle:%s:reminders" % battle_id) rs.delete("battle:%s" % battle_id) rs.sadd("whoosh:battles:deleted", battle_id) return 1 return 0
def send_battle_reminder(battle_id, delta): battle = json.loads(rs.hget("battle:%s" % battle_id, 'data')) user_id = rs.hget("battle:%s" % battle_id, 'uid') accepted = rs.smembers('battle:%s:accepted' % battle_id) for member in accepted: create_battle_notification(user_id, member, battle_id, REMINDER_BTL_START % (battle['descr'], delta_to_left(delta))) hours, minutes = delta.seconds // 3600, delta.seconds % 3600 / 60 key = "battle:%s:job:%s" % (battle_id, hours * 60 + minutes) reminders[key] = None
def update_battle(battle_id): uid = loggedUserUid() if uid == 0: return -2 if str(uid) != rs.hget("battle:%s" % battle_id, 'uid'): return -1 try: data = json.loads(request.stream.read()) except: return -3 battle = json.loads(rs.hget("battle:%s" % battle_id, 'data')) battle_old = battle.copy() fulldata = True for k in battle_model: if data.get(k, None) is None: fulldata = False break else: battle[k] = data[k] if not fulldata: return "Few data" if data['type'] not in battle_types: return "Wrong battle type" if battle_old['type'] != data['type']: rs.zrem("battles:%s" % battle_old['type'], battle_id) if battle_old['privacy'] != data['privacy']: rs.zrem("privacy:%s" % battle_old['privacy'], battle_id) tanks = rs.smembers("battle:%s:tanks" % battle_id) for tank_id in tanks: rs.srem("tank:%s:battles" % tank_id, battle_id) rs.delete("battle:%s:tanks" % battle_id) from warg.views.jobs.battle_reminder import remove_battle_reminders remove_battle_reminders(battle_id) rs.delete("battle:%s:reminders" % battle_id) process_battle_db(battle_id, uid, battle, data.get("reminders", default_reminders)) users = rs.zrange('battle:%s:users' % battle_id, 0, -1) for user_id in users: rs.zadd('user:%s:battles' % user_id, battle_id, battle['battle_date']) #from uhelp.views.full_text import storeLookInIndex #storeLookInIndex(look, None, True) rs.sadd("whoosh:battles:updated", battle_id) return battle_id
def update_group(group_id): uid = loggedUserUid() if uid == 0: return -2 if str(uid) != rs.hget("group:%s" % group_id, 'uid'): return -1 try: data = json.loads(request.stream.read()) except: return -3 group = json.loads(rs.hget("group:%s" % group_id, 'data')) for k in group_model: group[k] = data[k] rs.hset("group:%s" % group_id, "data", json.dumps(group)) rs.sadd("whoosh:groups:updated", group_id) return group_id
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
def battleDelUser(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 create_battle_notification(uid, user_id, battle_id, NTFY_BATTLE_KICK) return unFollowBattleByUser(battle_id, user_id)
def loggedUserUid(): #print "uSSID", request.cookies.get('uSSID') try: if request.cookies.get('uSSID') and rs.exists('ussid:' + request.cookies.get('uSSID')) == 1: uid = rs.hget('ussid:' + request.cookies.get('uSSID'), 'uid') return int(uid) except: pass return 0
def battleAddUser(battle_id, user_id): if rs.exists("battle:" + str(battle_id)) != 1: return -1 if rs.hget("battle:%s" % battle_id, "uid") != str(loggedUserUid()): return -2 rs.sadd('battle:%s:invited' % battle_id, user_id) create_battle_notification(loggedUserUid(), user_id, battle_id, NTFY_BATTLE_INVITE) return followBattleByUser(battle_id, user_id)
def group_unfollow(group_id): if rs.exists("group:%s" % group_id) != 1: return -1 uid = loggedUserUid() if uid == 0: return -2 #user can't leave own group if rs.hget("group:%s" % group_id, "uid") == str(uid): return -3 return group_del_user(group_id, uid)
def rebuildGroupsIndex(): createGroupSchema() gix = open_dir(groupsindex_dir) writer = AsyncWriter(gix) groups = rs.zrange("group_ids", 0, -1) for gid in groups: g = json.loads(rs.hget("group:%s" % gid, 'data')) storeGroupInIndex(g, writer) writer.commit()
def register_wot(): access_token = request.args.get("access_token", None) if access_token is None: return json.dumps("No access token") wotuid = 'wot_user:'******'account_id') print "wotuid", wotuid, access_token if rs.exists(wotuid) != 1: data = account_info(access_token, request.args.get('account_id')) if data['status'] == 'ok': if data['data'][request.args.get('account_id')] is not None: insert_wot_user(data['data'][request.args.get('account_id')]) else: return json.dumps("Error: Null profile") else: return json.dumps("Error: " + data['error']['message']) if rs.hget(wotuid, 'virtual') == '1': rs.hset(wotuid, 'virtual', 0) uid = rs.hget(wotuid, 'uid') rs.srem("users:virtual", uid) return make_login_response(uid, False)
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 unfollowBattle(battle_id): if rs.exists("battle:" + str(battle_id)) != 1: return -1 uid = loggedUserUid() if uid == 0: return -2 if rs.hget("battle:%s" % battle_id, "uid") == str(uid): return -3 if rs.sismember('battle:%s:invited' % battle_id, uid): create_battle_notification(uid, 0, battle_id, NTFY_INVITE_REJECT) else: create_battle_notification(uid, 0, battle_id, NTFY_BATTLE_UFLLOW) return unFollowBattleByUser(battle_id, uid)
def battleAddExternalUser(battle_id, account_id): if rs.exists("battle:" + str(battle_id)) != 1: return -1 if rs.hget("battle:%s" % battle_id, "uid") != str(loggedUserUid()): return -2 access_token = request.args.get("access_token", None) if access_token is None: return json.dumps("No access token") wotuid = 'wot_user:%s' % account_id if rs.exists(wotuid) != 1: from warg.views.users import insert_wot_user, account_info data = account_info(access_token, account_id) if data['status'] == 'ok': uid = insert_wot_user(data['data'][str(account_id)], 1) else: return json.dumps("Error: " + data['error']['message']) else: uid = rs.hget(wotuid, 'uid') create_battle_notification(loggedUserUid(), int(uid), battle_id, NTFY_BATTLE_INVITE) rs.sadd('battle:%s:invited' % battle_id, uid) return followBattleByUser(battle_id, uid)
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 detail(user_id): #return request.headers.get('Cookie') u = None if (user_id is not None) and rs.exists('users:' + str(user_id)) == 1: u = json.loads(rs.get("users:" + str(user_id))) u['soc_links'] = list(rs.smembers('user_soc_links:' + str(user_id))) u['is_online'] = int(rs.sismember('users_online', user_id)) u['is_follow'] = int(rs.sismember('user:%s:followers' % user_id, loggedUserUid())) u['virtual'] = int(rs.sismember('users:virtual', user_id)) u['memo'] = rs.get('user:%s:memo:%s' % (user_id, loggedUserUid())) clan_id = int(rs.get("user:%s:clan" % user_id) or 0) if clan_id > 0: u['clan'] = json.loads(rs.hget("clan:%s" % clan_id, 'data')) #u = json.dumps(u) return u or {}
def followExternalUser(account_id): uid = loggedUserUid() if uid == 0: return -2 access_token = request.args.get("access_token", None) if access_token is None: return json.dumps("No access token") wotuid = 'wot_user:%s' % account_id if rs.exists(wotuid) != 1: from warg.views.users import insert_wot_user, account_info data = account_info(access_token, account_id) if data['status'] == 'ok': user_id = insert_wot_user(data['data'][str(account_id)], 1) else: return json.dumps("Error: " + data['error']['message']) else: user_id = rs.hget(wotuid, 'uid') return followUserByUser(user_id, str(uid))
def insert_wot_user(profile, virtual=0): uid = rs.incr('users_counter') wotuid = 'wot_user:'******'account_id']) user_data = {'id': uid, 'avatar': None, 'create_date': int(mktime(datetime.now().timetuple()))} for k in profile: if k != 'private': user_data[k] = profile[k] from warg.views.followers import followUserByUser if profile['private'] is not None: for fid in profile['private']['friends']: wotfid = 'wot_user:%s' % fid if rs.exists(wotfid) == 1: followUserByUser(rs.hget(wotfid, 'uid'), str(uid)) rs.set("users:" + str(uid), json.dumps(user_data)) rs.sadd("user_soc_links:" + str(uid), wotuid) rs.hmset(wotuid, {'uid': str(uid), 'profile': json.dumps(profile), "virtual": virtual}) if virtual == 1: rs.sadd("users:virtual", uid) from warg.views.full_text import storeUserInIndex storeUserInIndex(user_data) rs.sadd("user_ids", uid) return uid
def delete_group(group_id, admin=0): if rs.exists("group:%s" % group_id) == 1 and (admin == 1 or str(loggedUserUid()) == rs.hget("group:%s" % group_id, 'uid')): users = rs.smembers("group:%s:users" % group_id) for user_id in users: rs.srem("user:%s:groups" % user_id, group_id) rs.delete("group:%s:users" % group_id) rs.delete("group:%s" % group_id) return 1 else: return 0