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 get_clan_battles(clan_id): uid = loggedUserUid() if uid == 0: return [] if rs.sismember("clan:%s:users" % clan_id, uid) == 0: return [] from warg.views.battle import get_battles_by_set return get_battles_by_set("clan:%s:battles" % clan_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 battleFollowUser(battle_id): if rs.exists("battle:" + str(battle_id)) != 1: return -1 uid = loggedUserUid() if uid == 0: return -2 if rs.sismember('battle:%s:invited' % battle_id, uid): create_battle_notification(uid, 0, battle_id, NTFY_INVITE_ACCEPT) rs.srem('battle:%s:invited' % battle_id, uid) rs.zadd('battle:%s:users' % battle_id, uid, 1) rs.sadd('battle:%s:accepted' % battle_id, uid) rs.zadd('user:%s:battles' % uid, battle_id, rs.zscore("battles_ids", battle_id)) return 1 else: create_battle_notification(uid, 0, battle_id, NTFY_BATTLE_FOLLOW) return followBattleByUser(battle_id, uid)
def create_group_battle(group_id): uid = loggedUserUid() if uid == 0: return -2 if rs.sismember("group:%s:users" % group_id, uid) == 0: return -1 try: data = json.loads(request.stream.read()) except: return -3 data["privacy"] = "PRIVATE" from warg.views.battle import do_create_battle battle_id = do_create_battle(uid, data) if int(battle_id) > 0: zscore = rs.zscore("battles_ids", battle_id) rs.zadd("group:%s:battles" % group_id, battle_id, zscore) return battle_id
def on_clan_message(uid, msg): clan_id = int(msg.get('clan_id', 0)) if clan_id == 0 or len(msg.get('text', "")) == 0 or rs.sismember( "clan:%s:users" % clan_id, uid) == 0: return chid = "clan:%s:message:%s:" % (clan_id, uid) mid = rs.incr(chid + "counter") chid = chid + str(mid) score = calendar.timegm(datetime.utcnow().timetuple()) chatm = { "id": mid, "text": msg.get('text'), 'sid': uid, 'clan_id': clan_id, "type": "clan_chat" } chatm['user'] = json.loads(rs.get("users:%s" % uid)) rs.hmset(chid, chatm) rs.zadd("clan:%s:messages" % clan_id, chid, score) chatm["create_date"] = score message = json.dumps({"type": "clan_chat", "content": chatm}) members = rs.smembers("clan:%s:users" % clan_id) for user_id in members: send_message_to_user(user_id, message)
def get_clan_chat_history(clan_id): uid = loggedUserUid() if uid == 0 or rs.sismember("clan:%s:users" % clan_id, uid) == 0: return [] return get_multi_chat_history("clan:%s:messages" % clan_id)
def get_battle_chat_history(battle_id): uid = loggedUserUid() if uid == 0 or rs.sismember("battle:%s:accepted" % battle_id, uid) == 0: return [] return get_multi_chat_history("battle:%s:chat" % battle_id)
def create_battle_notification(from_user, to_user, battle_id, template): battle = get_battle._original(battle_id) to_user = battle['user']["id"] if int(to_user) == 0 else to_user #timedelta = int(rs.get('user:%s:timedelta' % to_user) or 0) #zonedelta = int(rs.get('user:%s:zonedelta' % to_user) or 0) battle_date = datetime.fromtimestamp( int(battle["battle_date"]), tz=pytz.utc).strftime('UTC %H:%M %d/%m') message = None if template == NTFY_BATTLE_INVITE: message = NTFY_BATTLE_INVITE % (battle['user']['nickname'], battle_date, battle["descr"]) if template == NTFY_BATTLE_FOLLOW: usr = user_datail._original(from_user) to_user = battle['user']["id"] message = NTFY_BATTLE_FOLLOW % (usr["nickname"], battle_date, battle["descr"]) if template == NTFY_BATTLE_UFLLOW: usr = user_datail._original(from_user) to_user = battle['user']["id"] message = NTFY_BATTLE_UFLLOW % (usr["nickname"], battle_date, battle["descr"]) if template == NTFY_BATTLE_KICK: message = NTFY_BATTLE_KICK % (battle['user']['nickname'], battle_date, battle["descr"]) if template == NTFY_BATTLE_ACCEPT: message = NTFY_BATTLE_ACCEPT % (battle_date, battle["descr"]) if template == NTFY_BATTLE_REJECT: message = NTFY_BATTLE_REJECT % (battle_date, battle["descr"]) if template == NTFY_INVITE_ACCEPT: usr = user_datail._original(from_user) to_user = battle['user']["id"] message = NTFY_INVITE_ACCEPT % (usr["nickname"], battle_date, battle["descr"]) if template == NTFY_INVITE_REJECT: usr = user_datail._original(from_user) to_user = battle['user']["id"] message = NTFY_INVITE_REJECT % (usr["nickname"], battle_date, battle["descr"]) if message is None: message = template if rs.sismember("users:virtual", to_user): return chid = "chat:message:%s:%s:" % (from_user, to_user) mid = rs.incr(chid + "counter") chid = chid + str(mid) score = calendar.timegm(datetime.utcnow().timetuple()) chatm = { "id": mid, "text": message, 'is_read': 'false', 'sid': from_user, 'rid': to_user, "type": "battle", "battle_id": battle_id } rs.hmset(chid, chatm) rs.zadd("chat:user:%s:unread" % to_user, chid, score) rs.sadd("chat:user:%s:ntfy" % to_user, chid) rs.zadd("battle:%s:unread" % battle_id, chid, score) ucount = rs.zcard("chat:user:%s:unread" % to_user) unread = get_user_unread(to_user) if len(unread) > 0: unread_message = json.dumps({ "type": "unread", "content": { "count": ucount, "message": unread[0] } }) send_message_to_user(to_user, unread_message)