def insert(cls, op, date): """Inserts new post records.""" sql = """INSERT INTO hive_posts (is_valid, is_muted, parent_id, author, permlink, category, community_id, depth, created_at) VALUES (:is_valid, :is_muted, :parent_id, :author, :permlink, :category, :community_id, :depth, :date)""" sql += ";SELECT currval(pg_get_serial_sequence('hive_posts','id'))" post = cls._build_post(op, date) result = DB.query(sql, **post) post['id'] = int(list(result)[0][0]) cls._set_id(op['author'] + '/' + op['permlink'], post['id']) if not DbState.is_initial_sync(): if post['error']: author_id = Accounts.get_id(post['author']) Notify('error', dst_id=author_id, when=date, post_id=post['id'], payload=post['error']).write() CachedPost.insert(op['author'], op['permlink'], post['id']) if op['parent_author']: # update parent's child count CachedPost.recount(op['parent_author'], op['parent_permlink'], post['parent_id']) cls._insert_feed_cache(post)
def delete(cls, op): """Marks a post record as being deleted.""" pid, depth = cls.get_id_and_depth(op['author'], op['permlink']) DB.query("UPDATE hive_posts SET is_deleted = '1' WHERE id = :id", id=pid) if not DbState.is_initial_sync(): CachedPost.delete(pid, op['author'], op['permlink']) if depth == 0: # TODO: delete from hive_reblogs -- otherwise feed cache gets populated with deleted posts somwrimas FeedCache.delete(pid) else: # force parent child recount when child is deleted prnt = cls._get_parent_by_child_id(pid) CachedPost.recount(prnt['author'], prnt['permlink'], prnt['id'])