def create_group(): uid = loggedUserUid() if uid == 0: return -2 try: data = json.loads(request.stream.read()) except: return -3 gdata = {} fulldata = True for k in group_model: if data.get(k, None) is None: fulldata = False break else: gdata[k] = data[k] if not fulldata: return "Few data" gid = rs.incr('group_counter') gdata['id'] = gid gdata['create_date'] = int(calendar.timegm(datetime.utcnow().timetuple())) rs.hmset("group:%s" % gid, {'data': json.dumps(gdata), 'id': gid, 'uid': uid}) group_add_user(gid, uid) rs.sadd("whoosh:groups:added", gid) return gid
def add_user_avatar(): uid = loggedUserUid() if uid == 0: return -2 outfile = user_directory(uid) for f in request.files: _file = request.files.get(f) ensure_dir(app.config['UPLOAD_FOLDER'] + outfile) print "File: " + f, _file.filename if allowed_file(_file.filename): ext = _file.filename.rsplit('.', 1)[1] outfile = "%s%s_%s" % (outfile, uid, rs.incr('user:%s:avatar' % uid)) _file.save(app.config['UPLOAD_FOLDER'] + outfile + "_orig." + ext) process_user_image(outfile, ext, uid) u = json.loads(rs.get("users:%s" % uid)) u["avatar"] = outfile rs.set("users:%s" % uid, json.dumps(u)) break return 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 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 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 do_create_battle(uid, data): bdata = {} fulldata = True for k in battle_model: if data.get(k, None) is None: fulldata = False break else: bdata[k] = data[k] if not fulldata: return -4 # "Few data" if data['type'] not in battle_types: return -5 # "Wrong battle type" bid = rs.incr('battle_counter') bdata['id'] = bid bdata['create_date'] = int(calendar.timegm(datetime.utcnow().timetuple())) process_battle_db(bid, uid, bdata, data.get("reminders", default_reminders)) from warg.views.battle_followers import battleAcceptUser battleAcceptUser(bid, uid, True) #from warg.views.full_text import storeBattleInIndex #storeBattleInIndex(bdata, None) rs.sadd("whoosh:battles:added", bid) return bid
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)