Adds Hide Comment Functionality + Updates #7

Merged
osilkin98 merged 21 commits from hide-comments into master 2019-08-09 09:13:38 +02:00
3 changed files with 42 additions and 33 deletions
Showing only changes of commit 87150d9470 - Show all commits

View file

@ -9,12 +9,13 @@ CREATE TABLE IF NOT EXISTS COMMENT
( (
CommentId TEXT NOT NULL, CommentId TEXT NOT NULL,
LbryClaimId TEXT NOT NULL, LbryClaimId TEXT NOT NULL,
ChannelId TEXT DEFAULT NULL, ChannelId TEXT DEFAULT (NULL),
Body TEXT NOT NULL, Body TEXT NOT NULL,
ParentId TEXT DEFAULT NULL, ParentId TEXT DEFAULT (NULL),
Signature TEXT DEFAULT NULL, Signature TEXT DEFAULT (NULL),
Timestamp INTEGER NOT NULL, Timestamp INTEGER NOT NULL,
SigningTs TEXT DEFAULT NULL, SigningTs TEXT DEFAULT (NULL),
IsHidden BOOLEAN NOT NULL DEFAULT (FALSE),
CONSTRAINT COMMENT_PRIMARY_KEY PRIMARY KEY (CommentId) ON CONFLICT IGNORE, CONSTRAINT COMMENT_PRIMARY_KEY PRIMARY KEY (CommentId) ON CONFLICT IGNORE,
CONSTRAINT COMMENT_SIGNATURE_SK UNIQUE (Signature) ON CONFLICT ABORT, CONSTRAINT COMMENT_SIGNATURE_SK UNIQUE (Signature) ON CONFLICT ABORT,
CONSTRAINT COMMENT_CHANNEL_FK FOREIGN KEY (ChannelId) REFERENCES CHANNEL (ClaimId) CONSTRAINT COMMENT_CHANNEL_FK FOREIGN KEY (ChannelId) REFERENCES CHANNEL (ClaimId)
@ -23,6 +24,7 @@ CREATE TABLE IF NOT EXISTS COMMENT
ON UPDATE CASCADE ON DELETE NO ACTION -- setting null implies comment is top level ON UPDATE CASCADE ON DELETE NO ACTION -- setting null implies comment is top level
); );
-- ALTER TABLE COMMENT ADD COLUMN IsHidden BOOLEAN DEFAULT (FALSE);
-- ALTER TABLE COMMENT ADD COLUMN SigningTs TEXT DEFAULT NULL; -- ALTER TABLE COMMENT ADD COLUMN SigningTs TEXT DEFAULT NULL;
-- DROP TABLE IF EXISTS CHANNEL; -- DROP TABLE IF EXISTS CHANNEL;
@ -37,27 +39,26 @@ CREATE TABLE IF NOT EXISTS CHANNEL
-- indexes -- indexes
-- DROP INDEX IF EXISTS COMMENT_CLAIM_INDEX; -- DROP INDEX IF EXISTS COMMENT_CLAIM_INDEX;
CREATE INDEX IF NOT EXISTS CLAIM_COMMENT_INDEX ON COMMENT (LbryClaimId, CommentId); -- CREATE INDEX IF NOT EXISTS CLAIM_COMMENT_INDEX ON COMMENT (LbryClaimId, CommentId);
CREATE INDEX IF NOT EXISTS CHANNEL_COMMENT_INDEX ON COMMENT (ChannelId, CommentId); -- CREATE INDEX IF NOT EXISTS CHANNEL_COMMENT_INDEX ON COMMENT (ChannelId, CommentId);
-- VIEWS -- VIEWS
CREATE VIEW IF NOT EXISTS COMMENTS_ON_CLAIMS (comment_id, claim_id, timestamp, channel_name, channel_id, channel_url, CREATE VIEW IF NOT EXISTS COMMENTS_ON_CLAIMS AS SELECT
signature, signing_ts, parent_id, comment) AS C.CommentId AS comment_id,
SELECT C.CommentId, C.Body AS comment,
C.LbryClaimId, C.LbryClaimId AS claim_id,
C.Timestamp, C.Timestamp AS timestamp,
CHAN.Name, CHAN.Name AS channel_name,
CHAN.ClaimId, CHAN.ClaimId AS channel_id,
'lbry://' || CHAN.Name || '#' || CHAN.ClaimId, ('lbry://' || CHAN.Name || '#' || CHAN.ClaimId) AS channel_url,
C.Signature, C.Signature AS signature,
C.SigningTs, C.SigningTs AS signing_ts,
C.ParentId, C.ParentId AS parent_id,
C.Body C.IsHidden AS is_hidden
FROM COMMENT AS C FROM COMMENT AS C
LEFT OUTER JOIN CHANNEL CHAN on C.ChannelId = CHAN.ClaimId LEFT OUTER JOIN CHANNEL CHAN ON C.ChannelId = CHAN.ClaimId
ORDER BY C.Timestamp DESC; ORDER BY C.Timestamp DESC;
DROP VIEW IF EXISTS COMMENT_REPLIES; DROP VIEW IF EXISTS COMMENT_REPLIES;

View file

@ -13,6 +13,8 @@ logger = logging.getLogger(__name__)
def clean(thing: dict) -> dict: def clean(thing: dict) -> dict:
if 'is_hidden' in thing:
thing.update({'is_hidden': bool(thing['is_hidden'])})
return {k: v for k, v in thing.items() if v} return {k: v for k, v in thing.items() if v}
@ -29,7 +31,7 @@ def get_claim_comments(conn: sqlite3.Connection, claim_id: str, parent_id: str =
if top_level: if top_level:
results = [clean(dict(row)) for row in conn.execute( results = [clean(dict(row)) for row in conn.execute(
""" SELECT comment, comment_id, channel_name, channel_id, """ SELECT comment, comment_id, channel_name, channel_id,
channel_url, timestamp, signature, signing_ts, parent_id channel_url, timestamp, signature, signing_ts, parent_id, is_hidden
FROM COMMENTS_ON_CLAIMS FROM COMMENTS_ON_CLAIMS
WHERE claim_id = ? AND parent_id IS NULL WHERE claim_id = ? AND parent_id IS NULL
LIMIT ? OFFSET ? """, LIMIT ? OFFSET ? """,
@ -45,7 +47,7 @@ def get_claim_comments(conn: sqlite3.Connection, claim_id: str, parent_id: str =
elif parent_id is None: elif parent_id is None:
results = [clean(dict(row)) for row in conn.execute( results = [clean(dict(row)) for row in conn.execute(
""" SELECT comment, comment_id, channel_name, channel_id, """ SELECT comment, comment_id, channel_name, channel_id,
channel_url, timestamp, signature, signing_ts, parent_id channel_url, timestamp, signature, signing_ts, parent_id, is_hidden
FROM COMMENTS_ON_CLAIMS FROM COMMENTS_ON_CLAIMS
WHERE claim_id = ? WHERE claim_id = ?
LIMIT ? OFFSET ? """, LIMIT ? OFFSET ? """,
@ -61,7 +63,7 @@ def get_claim_comments(conn: sqlite3.Connection, claim_id: str, parent_id: str =
else: else:
results = [clean(dict(row)) for row in conn.execute( results = [clean(dict(row)) for row in conn.execute(
""" SELECT comment, comment_id, channel_name, channel_id, """ SELECT comment, comment_id, channel_name, channel_id,
channel_url, timestamp, signature, signing_ts, parent_id channel_url, timestamp, signature, signing_ts, parent_id, is_hidden
FROM COMMENTS_ON_CLAIMS FROM COMMENTS_ON_CLAIMS
WHERE claim_id = ? AND parent_id = ? WHERE claim_id = ? AND parent_id = ?
LIMIT ? OFFSET ? """, LIMIT ? OFFSET ? """,
@ -105,7 +107,7 @@ def get_comment_or_none(conn: sqlite3.Connection, comment_id: str) -> dict:
with conn: with conn:
curry = conn.execute( curry = conn.execute(
""" """
SELECT comment, comment_id, channel_name, channel_id, channel_url, timestamp, signature, signing_ts, parent_id SELECT comment, claim_id, comment_id, channel_name, channel_id, channel_url, timestamp, signature, signing_ts, parent_id, is_hidden
FROM COMMENTS_ON_CLAIMS WHERE comment_id = ? FROM COMMENTS_ON_CLAIMS WHERE comment_id = ?
""", """,
(comment_id,) (comment_id,)
@ -144,7 +146,11 @@ def get_comments_by_id(conn, comment_ids: list) -> typing.Union[list, None]:
placeholders = ', '.join('?' for _ in comment_ids) placeholders = ', '.join('?' for _ in comment_ids)
with conn: with conn:
return [clean(dict(row)) for row in conn.execute( return [clean(dict(row)) for row in conn.execute(
f'SELECT * FROM COMMENTS_ON_CLAIMS WHERE comment_id IN ({placeholders})', """
SELECT comment, claim_id, comment_id, channel_name, channel_id,
channel_url, timestamp, signature, signing_ts, parent_id, is_hidden
FROM COMMENTS_ON_CLAIMS
""" + f' WHERE comment_id IN ({placeholders})',
tuple(comment_ids) tuple(comment_ids)
)] )]
@ -178,7 +184,7 @@ def get_channel_id_from_comment_id(conn: sqlite3.Connection, comment_id: str):
SELECT channel_id, channel_name FROM COMMENTS_ON_CLAIMS WHERE comment_id = ? SELECT channel_id, channel_name FROM COMMENTS_ON_CLAIMS WHERE comment_id = ?
""", (comment_id,) """, (comment_id,)
).fetchone() ).fetchone()
return dict(channel) if channel else dict() return dict(channel) if channel else {}
class DatabaseWriter(object): class DatabaseWriter(object):

View file

@ -6,12 +6,13 @@ CREATE_COMMENT_TABLE = """
CREATE TABLE IF NOT EXISTS COMMENT ( CREATE TABLE IF NOT EXISTS COMMENT (
CommentId TEXT NOT NULL, CommentId TEXT NOT NULL,
LbryClaimId TEXT NOT NULL, LbryClaimId TEXT NOT NULL,
ChannelId TEXT DEFAULT NULL, ChannelId TEXT DEFAULT NULL,
Body TEXT NOT NULL, Body TEXT NOT NULL,
ParentId TEXT DEFAULT NULL, ParentId TEXT DEFAULT NULL,
Signature TEXT DEFAULT NULL, Signature TEXT DEFAULT NULL,
Timestamp INTEGER NOT NULL, Timestamp INTEGER NOT NULL,
SigningTs TEXT DEFAULT NULL, SigningTs TEXT DEFAULT NULL,
IsHidden BOOLEAN NOT NULL DEFAULT (FALSE),
CONSTRAINT COMMENT_PRIMARY_KEY PRIMARY KEY (CommentId) ON CONFLICT IGNORE, CONSTRAINT COMMENT_PRIMARY_KEY PRIMARY KEY (CommentId) ON CONFLICT IGNORE,
CONSTRAINT COMMENT_SIGNATURE_SK UNIQUE (Signature) ON CONFLICT ABORT, CONSTRAINT COMMENT_SIGNATURE_SK UNIQUE (Signature) ON CONFLICT ABORT,
CONSTRAINT COMMENT_CHANNEL_FK FOREIGN KEY (ChannelId) REFERENCES CHANNEL (ClaimId) CONSTRAINT COMMENT_CHANNEL_FK FOREIGN KEY (ChannelId) REFERENCES CHANNEL (ClaimId)
@ -46,7 +47,8 @@ CREATE_COMMENTS_ON_CLAIMS_VIEW = """
('lbry://' || CHAN.Name || '#' || CHAN.ClaimId) AS channel_url, ('lbry://' || CHAN.Name || '#' || CHAN.ClaimId) AS channel_url,
C.Signature AS signature, C.Signature AS signature,
C.SigningTs AS signing_ts, C.SigningTs AS signing_ts,
C.ParentId AS parent_id C.ParentId AS parent_id,
C.IsHidden as is_hidden
FROM COMMENT AS C FROM COMMENT AS C
LEFT OUTER JOIN CHANNEL CHAN ON C.ChannelId = CHAN.ClaimId LEFT OUTER JOIN CHANNEL CHAN ON C.ChannelId = CHAN.ClaimId
ORDER BY C.Timestamp DESC; ORDER BY C.Timestamp DESC;