def like(self, liking_user : User): if liking_user._id not in self.liked_by: chat = spawn_chat(self.user, liking_user._id) if chat.accept(liking_user._id): from app.notifications import Notification, UserNotifications note = Notification(User.get({"_id" : self.user}), User.get({"_id" : liking_user._id}), Notification.ACTION_LINKED) UserNotifications.notify(note) note = Notification(liking_user, User.get({"_id" : self.user}), Notification.ACTION_LINKED) UserNotifications.notify(note) pass #mutual like chat.save() self.liked_by.append(liking_user._id)
def action_thing(uid, action): if ObjectId(uid) == current_user._id: raise APIException(message="You cant Like, Block or report yourself") if action == "like": tel = Telemetry.get({"user" : ObjectId(uid)}) tel.like(current_user) tel.save() note = Notification(current_user, User.get({"_id" : ObjectId(uid)}), Notification.ACTION_LIKE) UserNotifications.notify(note) return APISuccessMessage(displayMessage={"message" : "Liked"}, update={"action" : "replace", "subject" : "#like", "fn" : "has_been_liked"}).messageSend() elif action == "block": from app import resolve_user blocked = resolve_user(ObjectId(uid)) ttl = Telemetry.get({"user" : current_user._id}) ttl.block(blocked) ttl.save() return APISuccessMessage(displayMessage={ "message" : "This user is now %s" % ( "blocked" if blocked._id in ttl.blocked else "unblocked")}, update={ "action" : "change", "subject" : "#block", "fn" : "blocking", "data": "%s" %("Block" if not blocked._id in ttl.blocked else "Unblock") }).messageSend() elif action == "report": #Something or the other, need to think about this pass else: raise APIException(message="Invalid option") return "OK"
def current_user(self): from app.users import User from bson import ObjectId if "token" not in session: raise Exception(message="Need token") token = decode_token(session["token"]) return User.get({"_id" : ObjectId(token["identity"]['id'])}, {"hash" : 0})
def public_profile(uid): id = ObjectId(uid) profile_user = User.get({"_id" : id}) profile_telemetry = Telemetry.get({"user" : id}) view_tel = Telemetry.get({"user" : current_user._id}) account = Account.get({"user" : id}) if not current_user._id == id and current_user._id not in profile_telemetry.viewed_by: profile_telemetry.view(current_user) profile_telemetry.save() UserNotifications.notify(Notification(current_user, profile_user, Notification.ACTION_VIEW)) return render_template("account/pages/profile.html", user=profile_user, viewer=current_user, account=account, telemetry=profile_telemetry, showMeta=current_user._id == id, viewer_telemetry=view_tel)
def message(self): auth = User.get({"_id": self.author}, {"uname": 1}) if self.action == self.ACTION_LIKE: return "%s liked your page" % auth["uname"] if self.action == self.ACTION_LINKED: return "%s and your account is now linked, you can chat" % auth[ "uname"] if self.action == self.ACTION_VIEW: return "%s looked at your profile" % auth["uname"] if self.action == self.ACTION_MESSAGE: return "%s left you a message" % auth["uname"]
def get_users(): users = User.get({ "active": True, "login_location.region_name": { "$ne": None } }) print("Sorting by region") for i in users[:5]: print(i.login_location) users.sort(key=lambda x: x.login_location["region_name"] == current_user. login_location["region_name"]) print("Got users") return users
def search(uname=None, age_gap=-1, fame=-1, location_region=None, location_city=None, sort_by=None, tags=None): selector = {"active": True} if uname: selector["uname"] = {"$regex": ".*" + uname + ".*", '$options': 'i'} if location_region: selector["login_location.region_name"] = location_region if location_city: selector["login_location.city"] = location_city ret = User.get(selector, {"uname": 1, "class": 1, "login_location": 1}) if not ret: return [] if not isinstance(ret, list): ret = [ret] if sort_by == "Location": print("Location sort") ret.sort(key=lambda x: (x.login_location["regeion_name"] if hasattr( x, "login_location") and x.login_location else False)) if not isinstance(ret, list): ret = [ret] ret = [x._id for x in ret] ret = filter_byAgeGap(int(age_gap), ret, sort_by == "Age gap", (tags.split(", ") if tags else [])) ret = filter_by_fame(int(fame), ret, sort_by == "Fame") ret = User.get({"_id": {"$in": ret}}) if not ret: return [] if not isinstance(ret, list): ret = [ret] if sort_by == "Name": ret.sort(key=lambda x: x.uname) return ret
def load_user(user_id): return User.get(user_id)