def get_nearby(cls, lng, lat, user_id): # get a list of the user's Facebook friends; tags authored by # friends are ranked higher friends = yield UsersModel.get_friends(user_id) # add the user to the friends list; tags created by the user should # also rank higher friends.add(user_id) # Query the database for tags, performing as much of the query # algorithm in the database as possible for efficiency. Returns a list # of the highest scoring tags that appears within a query radius. tags = yield sundowner.data.content.get_nearby( lng, lat, friends, cls._BATCH_SIZE_DB_FRIENDS, cls._BATCH_SIZE_DB_FINAL) # Now perform LIGHTWEIGHT manipulation of the database results before # they're returned. Only manipulations that couldn't be performed in # the database should be done here: # allow the tag's distance from the query location to influence the # sort order tags = _DistanceSorter.sort(lng, lat, tags, cls._BATCH_SIZE_RESULT) # Author data for tags not created by friends is not shown, but author # data for tags created by friends needs to be retrieved from the # `Users` collection. Tags created by the used have a special 'self' # username (eg 'Me'). friend_user_ids = set() usernames = {} for tag in tags: if tag["score"]["friend"]: if tag["user_id"] == user_id: usernames[user_id] = cls._SELF_USERNAME else: friend_user_ids.add(tag["user_id"]) friend_usernames = \ yield sundowner.data.users.get_usernames(friend_user_ids) usernames.update(friend_usernames) # format and return the result result = [] for tag in tags: entry = { "id": str(tag["_id"]), "text": tag["text"], "score": tag["score"], } if tag["url"]: entry["url"] = tag["url"] if tag["score"]["friend"]: entry["username"] = usernames[tag["user_id"]] result.append(entry) raise tornado.gen.Return(result)
def test(): result = yield UsersModel.get_friends(pete) raise tornado.gen.Return(result)