def new_comment(user_id, entity_id, data): entity = nomagic._get_entity_by_id(entity_id) data["type"] = "comment" data["likes"] = [] data["user_id"] = user_id data["activity_id"] = entity_id data["datetime"] = datetime.datetime.now().isoformat() data["comment_ids"] = [] if entity["type"] == "comment": data["activity_id"] = entity.get("activity_id") else: data["activity_id"] = entity_id #content valid assert data.get("content") new_comment_id = nomagic._new_key() assert nomagic._node(new_comment_id).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)", new_comment_id, nomagic._pack(data)) comment_ids = entity.get("comment_ids", []) comment_ids.append(new_comment_id) entity["comment_ids"] = comment_ids nomagic._update_entity_by_id(entity_id, entity) return comment_ids, dict(data, id=new_comment_id, like_count=0, like=False, user=nomagic._get_entity_by_id(user_id))
def new_comment(user_id, entity_id, data): entity = nomagic._get_entity_by_id(entity_id) data["type"] = "comment" data["likes"] = [] data["user_id"] = user_id data["activity_id"] = entity_id data["datetime"] = datetime.datetime.now().isoformat() data["comment_ids"] = [] if entity["type"] == "comment": data["activity_id"] = entity.get("activity_id") else: data["activity_id"] = entity_id #content valid assert data.get("content") new_comment_id = nomagic._new_key() assert nomagic._node(new_comment_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_comment_id, nomagic._pack(data)) comment_ids = entity.get("comment_ids", []) comment_ids.append(new_comment_id) entity["comment_ids"] = comment_ids nomagic._update_entity_by_id(entity_id, entity) return comment_ids, dict(data, id=new_comment_id, like_count=0, like=False, user=nomagic._get_entity_by_id(user_id))
def unfollow_users(user_id, friend_ids): user = nomagic._get_entity_by_id(user_id) following = user.get("following", []) for friend_id in friend_ids: if friend_id in following: friend = nomagic._get_entity_by_id(friend_id) followed = friend.get("followed", []) if user_id in followed: followed.remove(user_id) friend["followed"] = followed auth.update_user(friend_id, friend) following.remove(friend_id) user["following"] = following auth.update_user(user_id, user)
def update_user(user_id, data): #valid name user = nomagic._get_entity_by_id(user_id) user_json1 = nomagic._pack(user) result = {} if "password0" in data and "password1" in data and data["password0"] != data["password1"] and data["password1"] != "": #normal update password with old password0 and new password1 if user["password"] == hashlib.sha1(data["password0"] + user.get("salt", "")).hexdigest(): user["password"] = hashlib.sha1(data["password1"] + user.get("salt", "")).hexdigest() result["password_updated"] = True del data["password0"] del data["password1"] elif "password" in data and data["password"] != "": #force update password user["password"] = hashlib.sha1(data["password"] + user.get("salt", "")).hexdigest() result["password_updated"] = True del data["password"] if user: user.update(data) user_json2 = nomagic._pack(user) if user_json1 != user_json2: assert nomagic._node(user_id).execute_rowcount("UPDATE entities SET body = %s WHERE id = %s", nomagic._pack(user), nomagic._key(user_id)) return result
def new_status(user_id, data): now = datetime.datetime.now() data["type"] = "status" data["user_id"] = user_id data["datetime"] = now.isoformat() data["likes"] = [] data["comment_ids"] = [] assert data.get("title") new_id = nomagic._new_key() assert nomagic._node(new_id).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(data)) user = nomagic._get_entity_by_id(user_id) activity = user.get("activity", []) activity.append(new_id) user["activity"] = activity nomagic._update_entity_by_id(user_id, user) data["user"] = user data["like_count"] = 0 data["like"] = False data["comment_count"] = 0 assert conn.execute_rowcount("INSERT INTO index_posts (user_id, entity_id) VALUES(%s, %s)", user_id, new_id) return new_id, data
def new_status(user_id, data): now = datetime.datetime.now() data["type"] = "status" data["user_id"] = user_id data["datetime"] = now.isoformat() data["likes"] = [] data["comment_ids"] = [] assert data.get("title") new_id = nomagic._new_key() assert nomagic._node(new_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(data)) user = nomagic._get_entity_by_id(user_id) activity = user.get("activity", []) activity.append(new_id) user["activity"] = activity nomagic._update_entity_by_id(user_id, user) data["user"] = user data["like_count"] = 0 data["like"] = False data["comment_count"] = 0 assert conn.execute_rowcount( "INSERT INTO index_posts (user_id, entity_id) VALUES(%s, %s)", user_id, new_id) return new_id, data
def create_user(user): user["type"] = "user" user["name"] = user.get("name", "") user["salt"] = "".join( random.choice(string.ascii_uppercase + string.digits) for x in range(10)) user["password"] = hashlib.sha1(user["password"] + user["salt"]).hexdigest() user["datetime"] = datetime.datetime.now().isoformat() user["createtime"] = int(time.time()) new_id = nomagic._new_key() while True: new_user = nomagic._get_entity_by_id(new_id) if not new_user: break else: new_id = nomagic._new_key() rowcount = nomagic._node(new_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(user)) assert rowcount #update indexes: email # assert "@" in email # rowcount = conn.execute_rowcount("INSERT INTO index_login (login, entity_id) VALUES(%s, %s)", email, new_id) # assert rowcount return (new_id, user)
def get_fileinfo_by_rev(rev): fileinfo = conn.get("SELECT * FROM index_dropbox WHERE rev=%s", rev) if not fileinfo: return entity = nomagic._get_entity_by_id(fileinfo["entity_id"]) return dict(entity, md5=fileinfo["entity_id"]) if entity else None
def update_user(user_id, data): #valid name user = nomagic._get_entity_by_id(user_id) user_json1 = nomagic._pack(user) result = {} if "password0" in data and "password1" in data and data[ "password0"] != data["password1"] and data["password1"] != "": #normal update password with old password0 and new password1 if user["password"] == hashlib.sha1(data["password0"] + user.get("salt", "")).hexdigest(): user["password"] = hashlib.sha1(data["password1"] + user.get("salt", "")).hexdigest() result["password_updated"] = True del data["password0"] del data["password1"] elif "password" in data and data["password"] != "": #force update password user["password"] = hashlib.sha1(data["password"] + user.get("salt", "")).hexdigest() result["password_updated"] = True del data["password"] if user: user.update(data) user_json2 = nomagic._pack(user) if user_json1 != user_json2: assert nomagic._node(user_id).execute_rowcount( "UPDATE entities SET body = %s WHERE id = %s", nomagic._pack(user), nomagic._key(user_id)) BIG_CACHE.unset(user_id) return result
def check_user(login, password): login_info = conn.get("SELECT entity_id FROM index_login WHERE login = %s", login) if login_info: user_id = login_info["entity_id"] user = nomagic._get_entity_by_id(user_id) if user["password"] == hashlib.sha1(password + user.get("salt", "")).hexdigest(): return (user_id, user) return (None, None)
def like(user_id, entity_id): entity = nomagic._get_entity_by_id(entity_id) likes = entity.get("likes", []) if user_id not in likes: likes.append(user_id) entity["likes"] = likes nomagic._update_entity_by_id(entity_id, entity) return likes
def unlike(user_id, entity_id): entity = nomagic._get_entity_by_id(entity_id) likes = entity.get("likes", []) if user_id in likes: likes.remove(user_id) entity["likes"] = likes nomagic._update_entity_by_id(entity_id, entity) return likes
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 update_user(user_id, data): #valid name user = nomagic._get_entity_by_id(user_id) user_json1 = nomagic._pack(user) if user: user.update(data) user_json2 = nomagic._pack(user) if user_json1 != user_json2: assert ring[nomagic._number(user_id)].execute_rowcount("UPDATE entities SET body = %s WHERE id = %s", nomagic._pack(user), nomagic._key(user_id))
def update_fileinfo(filehash, user_id, id3info = None, title = None, rev = None): if rev: if not conn.get("SELECT * FROM index_dropbox WHERE rev=%s", rev): assert conn.execute_rowcount("INSERT INTO index_dropbox (rev, entity_id) VALUES (%s, %s)", rev, filehash) fileinfo = nomagic._get_entity_by_id(filehash) if fileinfo: changed = False owners = fileinfo.get("owners", []) if user_id not in owners: owners.append(user_id) fileinfo["owners"] = owners changed = True if id3info and id3info != fileinfo.get("id3info"): fileinfo["id3info"] = id3info changed = True if title and title != fileinfo.get("title"): fileinfo["title"] = title changed = True if changed: nomagic._update_entity_by_id(filehash, fileinfo) else: fileinfo = {"owners":[user_id]} if id3info: fileinfo["id3info"] = id3info if title: fileinfo["title"] = title assert nomagic._node(filehash).execute_rowcount("INSERT INTO entities (id, body) VALUES(%s, %s)",\ filehash, nomagic._pack(fileinfo)) user = nomagic._get_entity_by_id(user_id) if user: files = user.get("files", []) if filehash not in files: files.append(filehash) user["files"] = files nomagic._update_entity_by_id(user_id, user) return fileinfo, user
def get_news_by_id(activity_id): activity = nomagic._get_entity_by_id(activity_id) activity["id"] = activity_id comments, user_ids = get_comments(activity) return dict(activity, id = activity_id, like_count = len(activity.get("likes", [])), comment_count = 0, #len(activity.get("comment_ids", [])), comments = comments), user_ids
def get_item_by_id(item_id): item = nomagic._get_entity_by_id(item_id) comments, user_ids = get_comments(item) return dict(item, id = item_id, like_count = len(item.get("likes", [])), like = False, url = item.get("url"), comment_count = 0, #len(activity.get("comment_ids", [])), comments = comments), user_ids
def update_user(user_id, data): #valid name user = nomagic._get_entity_by_id(user_id) user_json1 = nomagic._pack(user) if user: user.update(data) user_json2 = nomagic._pack(user) if user_json1 != user_json2: assert ring[nomagic._number(user_id)].execute_rowcount( "UPDATE entities SET body = %s WHERE id = %s", nomagic._pack(user), nomagic._key(user_id))
def get_news_by_id(activity_id): activity = nomagic._get_entity_by_id(activity_id) activity["id"] = activity_id comments, user_ids = get_comments(activity) return dict( activity, id=activity_id, like_count=len(activity.get("likes", [])), comment_count=0, #len(activity.get("comment_ids", [])), comments=comments), user_ids
def get_item_by_id(item_id): item = nomagic._get_entity_by_id(item_id) comments, user_ids = get_comments(item) return dict( item, id=item_id, like_count=len(item.get("likes", [])), like=False, url=item.get("url"), comment_count=0, #len(activity.get("comment_ids", [])), comments=comments), user_ids
def follow_users(user_id, friend_ids): user = nomagic._get_entity_by_id(user_id) following = user.get("following", []) suggested_friend_ids = user.get("suggested_friend_ids", []) changed = False for friend_id in friend_ids: if friend_id not in following: friend = nomagic._get_entity_by_id(friend_id) followed = friend.get("followed", []) if user_id not in followed: followed.append(user_id) friend["followed"] = followed auth.update_user(friend_id, friend) following.append(friend_id) changed = True if friend_id in suggested_friend_ids: suggested_friend_ids.remove(friend_id) changed = True if changed: user["following"] = following user["suggested_friend_ids"] = suggested_friend_ids auth.update_user(user_id, user)
def disconnect_index_and_entity(table_index, index_name, index_connection_name, entity_id, entity_connection_name): index = conn.get("SELECT * FROM %s WHERE name = %s" % (table_index, "%s"), index_name) index_data = nomagic._unpack(index["data"] or "{}") index_connection = set(index_data.get(index_connection_name, [])) if entity_id in index_connection: index_connection.remove(entity_id) index_data[index_connection_name] = list(index_connection) index_data_updated = nomagic._pack(index_data) conn.execute("UPDATE %s SET data = %s WHERE name = %s" % (table_index, "%s", "%s"), index_data_updated, index_name) entity = nomagic._get_entity_by_id(entity_id) entity_connection = set(entity.get(entity_connection_name, [])) if index_name in entity_connection: entity_connection.remove(index_name) entity[entity_connection_name] = list(entity_connection) nomagic._update_entity_by_id(entity_id, entity)
def create_event(event): order["type"] = "event" order["owner"] = order.get("owner", "") order["datetime"] = datetime.datetime.now().isoformat() order["createtime"] = int(time.time()) new_id = nomagic._new_key() while True: new_order = nomagic._get_entity_by_id(new_id) if not new_order: break else: new_id = nomagic._new_key() rowcount = nomagic._node(new_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(order)) assert rowcount return (new_id, order)
def create_comment(comment): comment["type"] = "comment" comment["owner"] = comment.get("owner", "") comment["owner_type"] = comment.get("owner_type", "entity") comment["datetime"] = datetime.datetime.now().isoformat() comment["createtime"] = int(time.time()) new_id = nomagic._new_key() while True: new_comment = nomagic._get_entity_by_id(new_id) if not new_comment: break else: new_id = nomagic._new_key() rowcount = nomagic._node(new_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(comment)) assert rowcount return (new_id, comment)
def create_chat(chat): chat["type"] = "chat" chat["owner"] = chat.get("owner", "") chat["owner_type"] = chat.get("owner_type", "entity") chat["editors"] = chat.get("editors", []) chat["helpers"] = chat.get("helpers", {}) chat["comment_members"] = chat.get("comment_members", []) chat["notifyers"] = chat.get("notifyers", []) chat["blackers"] = chat.get("blackers", []) chat["datetime"] = datetime.datetime.now().isoformat() chat["createtime"] = int(time.time()) new_id = nomagic._new_key() while True: new_chat = nomagic._get_entity_by_id(new_id) if not new_chat: break else: new_id = nomagic._new_key() rowcount = nomagic._node(new_id).execute_rowcount( "INSERT INTO entities (id, body) VALUES(%s, %s)", new_id, nomagic._pack(chat)) assert rowcount return (new_id, chat)
def get_user_by_login(login): index_login = conn.get("SELECT * FROM index_login WHERE login = %s", login) entity_id = index_login["entity_id"] return nomagic._get_entity_by_id(entity_id)
def get_entity(cache, entity_id): entity = cache.get(entity_id) if not entity: entity = nomagic._get_entity_by_id(entity_id) cache.set(entity_id, entity) return entity