def get_activities_by_user_id(user_id, activity_offset=0, activity_start_id=None): user = nomagic._get_entity_by_id(user_id) activity_ids = user.get("activity", []) if user else [] if user: activities = [dict(activity, id=activity_id) for activity_id, activity in nomagic._get_entities_by_ids(activity_ids)] return [dict(activity, comments = [dict(comment, id=comment_id) \ for comment_id, comment in nomagic._get_entities_by_ids(activity["comment_ids"])]) \ for activity in activities] return []
def get_public_feed(item_offset=15, item_start_id=None): if item_start_id: last_post = conn.get("SELECT * FROM index_posts WHERE entity_id = %s", item_start_id) posts = conn.query( "SELECT * FROM index_posts WHERE rank < %s ORDER BY rank DESC LIMIT 0, %s", last_post["rank"], item_offset) else: posts = conn.query( "SELECT * FROM index_posts ORDER BY rank DESC LIMIT 0, %s", item_offset) item_ids = [i["entity_id"] for i in posts] if posts: items = [ dict(item, id=item_id) for item_id, item in nomagic._get_entities_by_ids(item_ids) ] return [ dict( item, like_count=len(item.get("likes", [])), comment_count=len(item.get("comment_ids", [])), url=item.get("url"), ) for item in items ] return []
def get_users_by_ids(ids): users = [ dict(user, id=user_id) for user_id, user in nomagic._get_entities_by_ids(ids) ] for user in users: del user["password"] del user["salt"] return users
def get_friends(user_ids): result = [] for friend_id, friend in nomagic._get_entities_by_ids(user_ids): if "password" in friend: del friend["password"] if "salt" in friend: del friend["salt"] result.append(dict(friend, user_id=friend_id)) return result
def get_comments(entity, user_ids = set()): assert "comment_ids" in entity assert "comments" not in entity entity_comments = entity.get("comments", []) for comment_id, comment in nomagic._get_entities_by_ids(entity.get("comment_ids", [])): comment["id"] = comment_id comment["like_count"] = len(comment.get("likes", [])), comment["comment_count"] = 0, #len(activity.get("comment_ids", [])), user_ids.add(comment["user_id"]) if comment.get("comment_ids"): get_comments(comment, user_ids) entity_comments.append(comment) entity["comments"] = entity_comments return entity_comments, user_ids
def get_public_news_feed(activity_offset=10, activity_start_id=None): if activity_start_id: pass else: posts = conn.query("SELECT * FROM index_posts ORDER BY id DESC LIMIT 0, %s", activity_offset) activity_ids = [i["entity_id"] for i in posts] if posts: activities = [dict(activity, id=activity_id) for activity_id, activity in nomagic._get_entities_by_ids(activity_ids)] return [dict(activity, like_count = len(activity.get("likes", [])), comment_count = len(activity.get("comment_ids", [])), ) for activity in activities] return []
def disconnect_entities(entity_id1, entity_connection_name1, entity_id2, entity_connection_name2): entity1, entity2 = nomagic._get_entities_by_ids([entity_id1, entity_id2]) entity_data1, entity_data2 = entity1[1], entity2[1] entity_connection = set(entity_data1.get(entity_connection_name1, [])) if entity_id2 in entity_connection: entity_connection.remove(entity_id2) entity_data1[entity_connection_name1] = list(entity_connection) nomagic._update_entity_by_id(entity_id1, entity_data1) entity_connection = set(entity_data2.get(entity_connection_name2, [])) if entity_id1 in entity_connection: entity_connection.remove(entity_id1) entity_data2[entity_connection_name2] = list(entity_connection) nomagic._update_entity_by_id(entity_id2, entity_data2)
def get_comments(entity, user_ids=set()): assert "comment_ids" in entity assert "comments" not in entity entity_comments = entity.get("comments", []) for comment_id, comment in nomagic._get_entities_by_ids( entity.get("comment_ids", [])): comment["id"] = comment_id comment["like_count"] = len(comment.get("likes", [])), comment["comment_count"] = 0, #len(activity.get("comment_ids", [])), user_ids.add(comment["user_id"]) if comment.get("comment_ids"): get_comments(comment, user_ids) entity_comments.append(comment) entity["comments"] = entity_comments return entity_comments, user_ids
def get_public_feed(item_offset=15, item_start_id=None): if item_start_id: last_post = conn.get("SELECT * FROM index_posts WHERE entity_id = %s", item_start_id) posts = conn.query("SELECT * FROM index_posts WHERE rank < %s ORDER BY rank DESC LIMIT 0, %s", last_post["rank"], item_offset) else: posts = conn.query("SELECT * FROM index_posts ORDER BY rank DESC LIMIT 0, %s", item_offset) item_ids = [i["entity_id"] for i in posts] if posts: items = [dict(item, id=item_id) for item_id, item in nomagic._get_entities_by_ids(item_ids)] return [dict(item, like_count = len(item.get("likes", [])), comment_count = len(item.get("comment_ids", [])), url = item.get("url"), ) for item in items] return []
def get_entities(cache, entity_ids): missing_entity_ids = [] existing_entities = {} for entity_id in entity_ids: entity = cache.get(entity_id) if entity: existing_entities[entity_id] = entity else: missing_entity_ids.append(entity_id) missing_entities = dict(nomagic._get_entities_by_ids(missing_entity_ids)) result = [] for entity_id in entity_ids: if entity_id in existing_entities: result.append((entity_id, existing_entities[entity_id])) else: entity = missing_entities[entity_id] result.append((entity_id, entity)) cache.set(entity_id, entity) return result
def get_public_news_feed(activity_offset=10, activity_start_id=None): if activity_start_id: pass else: posts = conn.query( "SELECT * FROM index_posts ORDER BY id DESC LIMIT 0, %s", activity_offset) activity_ids = [i["entity_id"] for i in posts] if posts: activities = [ dict(activity, id=activity_id) for activity_id, activity in nomagic._get_entities_by_ids(activity_ids) ] return [ dict( activity, like_count=len(activity.get("likes", [])), comment_count=len(activity.get("comment_ids", [])), ) for activity in activities ] return []
def get_users_by_ids(ids): users = [dict(user, id=user_id) for user_id, user in nomagic._get_entities_by_ids(ids)] for user in users: del user["password"] del user["salt"] return users