Compare commits
6 commits
master
...
comment-re
Author | SHA1 | Date | |
---|---|---|---|
|
8c350a18a1 | ||
|
7b4fccaf20 | ||
|
4d772d875c | ||
|
711d64bde0 | ||
|
624ef0c9b7 | ||
|
40cea617ee |
7 changed files with 254 additions and 72 deletions
147
dist/bundle.es.js
vendored
147
dist/bundle.es.js
vendored
|
@ -1260,6 +1260,18 @@ function buildURI(UrlObj, includeProto = true, protoDefault = 'lbry://') {
|
||||||
deprecatedParts = _objectWithoutProperties(UrlObj, ['streamName', 'streamClaimId', 'channelName', 'channelClaimId', 'primaryClaimSequence', 'primaryBidPosition', 'secondaryClaimSequence', 'secondaryBidPosition']);
|
deprecatedParts = _objectWithoutProperties(UrlObj, ['streamName', 'streamClaimId', 'channelName', 'channelClaimId', 'primaryClaimSequence', 'primaryBidPosition', 'secondaryClaimSequence', 'secondaryBidPosition']);
|
||||||
const { claimId, claimName, contentName } = deprecatedParts;
|
const { claimId, claimName, contentName } = deprecatedParts;
|
||||||
|
|
||||||
|
{
|
||||||
|
if (claimId) {
|
||||||
|
console.error(__("'claimId' should no longer be used. Use 'streamClaimId' or 'channelClaimId' instead"));
|
||||||
|
}
|
||||||
|
if (claimName) {
|
||||||
|
console.error(__("'claimName' should no longer be used. Use 'streamClaimName' or 'channelClaimName' instead"));
|
||||||
|
}
|
||||||
|
if (contentName) {
|
||||||
|
console.error(__("'contentName' should no longer be used. Use 'streamName' instead"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!claimName && !channelName && !streamName) {
|
if (!claimName && !channelName && !streamName) {
|
||||||
console.error(__("'claimName', 'channelName', and 'streamName' are all empty. One must be present to build a url."));
|
console.error(__("'claimName', 'channelName', and 'streamName' are all empty. One must be present to build a url."));
|
||||||
}
|
}
|
||||||
|
@ -4239,7 +4251,9 @@ const doDeleteTag = name => ({
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
function doCommentList(uri, page = 1, pageSize = 99999) {
|
// if parentId is provided, `uri` is ignored
|
||||||
|
function doCommentList(uri, page = 1, pageSize = 99999, // while the desktop app doesn't paginate
|
||||||
|
parentId) {
|
||||||
return (dispatch, getState) => {
|
return (dispatch, getState) => {
|
||||||
const state = getState();
|
const state = getState();
|
||||||
const claim = selectClaimsByUri(state)[uri];
|
const claim = selectClaimsByUri(state)[uri];
|
||||||
|
@ -4251,7 +4265,8 @@ function doCommentList(uri, page = 1, pageSize = 99999) {
|
||||||
lbryProxy.comment_list({
|
lbryProxy.comment_list({
|
||||||
claim_id: claimId,
|
claim_id: claimId,
|
||||||
page,
|
page,
|
||||||
page_size: pageSize
|
page_size: pageSize,
|
||||||
|
parentId: parentId
|
||||||
}).then(result => {
|
}).then(result => {
|
||||||
const { items: comments } = result;
|
const { items: comments } = result;
|
||||||
dispatch({
|
dispatch({
|
||||||
|
@ -4259,7 +4274,8 @@ function doCommentList(uri, page = 1, pageSize = 99999) {
|
||||||
data: {
|
data: {
|
||||||
comments,
|
comments,
|
||||||
claimId: claimId,
|
claimId: claimId,
|
||||||
uri: uri
|
uri: uri,
|
||||||
|
parentId: parentId
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
|
@ -4291,7 +4307,8 @@ function doCommentCreate(comment = '', claim_id = '', channel, parent_id) {
|
||||||
type: COMMENT_CREATE_COMPLETED,
|
type: COMMENT_CREATE_COMPLETED,
|
||||||
data: {
|
data: {
|
||||||
comment: result,
|
comment: result,
|
||||||
claimId: claim_id
|
claimId: claim_id,
|
||||||
|
parentId: parent_id
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}).catch(error => {
|
}).catch(error => {
|
||||||
|
@ -4835,6 +4852,7 @@ const defaultState$1 = {
|
||||||
byId: {}, // ClaimID -> list of comments
|
byId: {}, // ClaimID -> list of comments
|
||||||
commentsByUri: {}, // URI -> claimId
|
commentsByUri: {}, // URI -> claimId
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
|
repliesByCommentId: {}, // commentId -> list of commentIds
|
||||||
myComments: undefined
|
myComments: undefined
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -4848,9 +4866,10 @@ const commentReducer = handleActions({
|
||||||
}),
|
}),
|
||||||
|
|
||||||
[COMMENT_CREATE_COMPLETED]: (state, action) => {
|
[COMMENT_CREATE_COMPLETED]: (state, action) => {
|
||||||
const { comment, claimId } = action.data;
|
const { comment, claimId, parentId } = action.data;
|
||||||
const commentById = Object.assign({}, state.commentById);
|
const commentById = Object.assign({}, state.commentById);
|
||||||
const byId = Object.assign({}, state.byId);
|
const byId = Object.assign({}, state.byId);
|
||||||
|
const repliesByCommentId = Object.assign({}, state.repliesByCommentId);
|
||||||
const comments = byId[claimId];
|
const comments = byId[claimId];
|
||||||
const newCommentIds = comments.slice();
|
const newCommentIds = comments.slice();
|
||||||
|
|
||||||
|
@ -4861,6 +4880,13 @@ const commentReducer = handleActions({
|
||||||
newCommentIds.unshift(comment.comment_id);
|
newCommentIds.unshift(comment.comment_id);
|
||||||
byId[claimId] = newCommentIds;
|
byId[claimId] = newCommentIds;
|
||||||
|
|
||||||
|
if (parentId) {
|
||||||
|
const newReplies = repliesByCommentId[parentId] || [];
|
||||||
|
// unlike regular comments, newest replies should be at the bottom of list
|
||||||
|
newReplies.push(comment.comment_id);
|
||||||
|
repliesByCommentId[parentId] = newReplies;
|
||||||
|
}
|
||||||
|
|
||||||
return _extends$a({}, state, {
|
return _extends$a({}, state, {
|
||||||
commentById,
|
commentById,
|
||||||
byId,
|
byId,
|
||||||
|
@ -4871,31 +4897,49 @@ const commentReducer = handleActions({
|
||||||
[COMMENT_LIST_STARTED]: state => _extends$a({}, state, { isLoading: true }),
|
[COMMENT_LIST_STARTED]: state => _extends$a({}, state, { isLoading: true }),
|
||||||
|
|
||||||
[COMMENT_LIST_COMPLETED]: (state, action) => {
|
[COMMENT_LIST_COMPLETED]: (state, action) => {
|
||||||
const { comments, claimId, uri } = action.data;
|
const { comments, claimId, uri, parentId } = action.data;
|
||||||
|
|
||||||
const commentById = Object.assign({}, state.commentById);
|
const commentById = Object.assign({}, state.commentById);
|
||||||
const byId = Object.assign({}, state.byId);
|
const byId = Object.assign({}, state.byId);
|
||||||
const commentsByUri = Object.assign({}, state.commentsByUri);
|
const commentsByUri = Object.assign({}, state.commentsByUri);
|
||||||
|
const repliesByCommentId = Object.assign({}, state.repliesByCommentId);
|
||||||
|
|
||||||
if (comments) {
|
if (comments) {
|
||||||
// we use an Array to preserve order of listing
|
// we use an Array to preserve order of listing
|
||||||
// in reality this doesn't matter and we can just
|
// in reality this doesn't matter and we can just
|
||||||
// sort comments by their timestamp
|
// sort comments by their timestamp
|
||||||
const commentIds = Array(comments.length);
|
const commentIds = Array(comments.length);
|
||||||
|
const replyThreads = {};
|
||||||
|
|
||||||
// map the comment_ids to the new comments
|
// map the comment_ids to the new comments
|
||||||
for (let i = 0; i < comments.length; i++) {
|
for (let i = 0; i < comments.length; i++) {
|
||||||
commentIds[i] = comments[i].comment_id;
|
const comment = comments[i];
|
||||||
commentById[commentIds[i]] = comments[i];
|
commentIds[i] = comment.comment_id;
|
||||||
|
commentById[commentIds[i]] = comment;
|
||||||
|
|
||||||
|
if (comment.parent_id) {
|
||||||
|
if (!(comment.parent_id in replyThreads)) {
|
||||||
|
replyThreads[comment.parent_id] = [];
|
||||||
|
}
|
||||||
|
replyThreads[comment.parent_id].push(comment.comment_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
byId[claimId] = commentIds;
|
Object.entries(replyThreads).forEach((parent_id, replyIds) => {
|
||||||
|
repliesByCommentId[parent_id] = replyIds;
|
||||||
|
});
|
||||||
|
|
||||||
commentsByUri[uri] = claimId;
|
commentsByUri[uri] = claimId;
|
||||||
|
// don't override the entire list with the replies to one comment
|
||||||
|
if (parentId == null) {
|
||||||
|
byId[claimId] = commentIds;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return _extends$a({}, state, {
|
return _extends$a({}, state, {
|
||||||
byId,
|
byId,
|
||||||
commentById,
|
commentById,
|
||||||
commentsByUri,
|
commentsByUri,
|
||||||
|
repliesByCommentId,
|
||||||
isLoading: false
|
isLoading: false
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
@ -4910,20 +4954,36 @@ const commentReducer = handleActions({
|
||||||
const { comment_id } = action.data;
|
const { comment_id } = action.data;
|
||||||
const commentById = Object.assign({}, state.commentById);
|
const commentById = Object.assign({}, state.commentById);
|
||||||
const byId = Object.assign({}, state.byId);
|
const byId = Object.assign({}, state.byId);
|
||||||
|
const repliesByCommentId = Object.assign({}, state.repliesByCommentId);
|
||||||
|
|
||||||
// to remove the comment and its references
|
const comment = commentById[comment_id];
|
||||||
const claimId = commentById[comment_id].claim_id;
|
|
||||||
for (let i = 0; i < byId[claimId].length; i++) {
|
// keep record of comment's existence if it has replies
|
||||||
if (byId[claimId][i] === comment_id) {
|
if (!(comment.comment_id in repliesByCommentId)) {
|
||||||
byId[claimId].splice(i, 1);
|
const claimId = commentById[comment_id].claim_id;
|
||||||
break;
|
for (let i = 0; i < byId[claimId].length; i++) {
|
||||||
|
if (byId[claimId][i] === comment_id) {
|
||||||
|
byId[claimId].splice(i, 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (comment.parent_id) {
|
||||||
|
for (let i = 0; i < repliesByCommentId[comment.parent_id]; i++) {
|
||||||
|
if (repliesByCommentId[comment.parent_id][i] === comment.comment_id) {
|
||||||
|
repliesByCommentId[comment.parent_id].splice(i, 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
delete commentById[comment_id];
|
delete commentById[comment_id];
|
||||||
|
|
||||||
return _extends$a({}, state, {
|
return _extends$a({}, state, {
|
||||||
commentById,
|
commentById,
|
||||||
byId,
|
byId,
|
||||||
|
repliesByCommentId,
|
||||||
isLoading: false
|
isLoading: false
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
@ -5875,17 +5935,46 @@ const selectState$8 = state => state.comments || {};
|
||||||
|
|
||||||
const selectCommentsById = reselect.createSelector(selectState$8, state => state.commentById || {});
|
const selectCommentsById = reselect.createSelector(selectState$8, state => state.commentById || {});
|
||||||
|
|
||||||
const selectCommentsByClaimId = reselect.createSelector(selectState$8, selectCommentsById, (state, byId) => {
|
const selectReplyIdsById = reselect.createSelector(selectState$8, state => state.repliesByCommentId || {});
|
||||||
const byClaimId = state.byId || {};
|
|
||||||
|
const selectRepliesById = reselect.createSelector(selectState$8, selectReplyIdsById, (state, repliesById) => {
|
||||||
|
const byCommentId = state.commentById || {};
|
||||||
const comments = {};
|
const comments = {};
|
||||||
|
|
||||||
|
Object.keys(repliesById).forEach(parentId => {
|
||||||
|
comments[parentId] = [];
|
||||||
|
for (const commentId of repliesById[parentId]) {
|
||||||
|
comments[parentId].push(byCommentId[commentId]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return comments;
|
||||||
|
});
|
||||||
|
|
||||||
|
const selectRepliesByClaimId = reselect.createSelector(selectState$8, selectRepliesById, (state, repliesById) => {
|
||||||
|
const byClaimId = state.byId || {};
|
||||||
|
const claimThreads = {};
|
||||||
|
|
||||||
|
Object.keys(byClaimId).forEach(claimId => {
|
||||||
|
claimThreads[claimId] = {};
|
||||||
|
for (const commentId of byClaimId[claimId]) {
|
||||||
|
if (repliesById[commentId]) {
|
||||||
|
claimThreads[claimId][commentId] = repliesById[commentId];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return claimThreads;
|
||||||
|
});
|
||||||
|
|
||||||
|
const selectAllCommentsByClaimId = reselect.createSelector(selectState$8, selectCommentsById, (state, commentById) => {
|
||||||
|
const byClaimId = state.byId || {};
|
||||||
|
const comments = {};
|
||||||
// replace every comment_id in the list with the actual comment object
|
// replace every comment_id in the list with the actual comment object
|
||||||
Object.keys(byClaimId).forEach(claimId => {
|
Object.keys(byClaimId).forEach(claimId => {
|
||||||
const commentIds = byClaimId[claimId];
|
const commentIds = byClaimId[claimId] || [];
|
||||||
|
|
||||||
comments[claimId] = Array(commentIds === null ? 0 : commentIds.length);
|
comments[claimId] = [];
|
||||||
for (let i = 0; i < commentIds.length; i++) {
|
for (const commentId of commentIds) {
|
||||||
comments[claimId][i] = byId[commentIds[i]];
|
comments[claimId].push(commentById[commentId]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -5897,28 +5986,18 @@ const selectCommentsByClaimId = reselect.createSelector(selectState$8, selectCom
|
||||||
selectState,
|
selectState,
|
||||||
state => state.byId || {}
|
state => state.byId || {}
|
||||||
); */
|
); */
|
||||||
const selectCommentsByUri = reselect.createSelector(selectState$8, state => {
|
const selectCommentsByUri = reselect.createSelector(selectState$8, selectAllCommentsByClaimId, (state, commentsByClaimId) => {
|
||||||
const byUri = state.commentsByUri || {};
|
const byUri = state.commentsByUri || {};
|
||||||
const comments = {};
|
const comments = {};
|
||||||
Object.keys(byUri).forEach(uri => {
|
Object.keys(byUri).forEach(uri => {
|
||||||
const claimId = byUri[uri];
|
const claimId = byUri[uri];
|
||||||
if (claimId === null) {
|
comments[uri] = commentsByClaimId[claimId];
|
||||||
comments[uri] = null;
|
|
||||||
} else {
|
|
||||||
comments[uri] = claimId;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return comments;
|
return comments;
|
||||||
});
|
});
|
||||||
|
|
||||||
const makeSelectCommentsForUri = uri => reselect.createSelector(selectCommentsByClaimId, selectCommentsByUri, (byClaimId, byUri) => {
|
const makeSelectCommentsForUri = uri => reselect.createSelector(selectCommentsByUri, byUri => byUri[uri]);
|
||||||
const claimId = byUri[uri];
|
|
||||||
return byClaimId && byClaimId[claimId];
|
|
||||||
});
|
|
||||||
|
|
||||||
// todo: allow SDK to retrieve user comments through comment_list
|
|
||||||
// todo: implement selectors for selecting comments owned by user
|
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
6
dist/flow-typed/Comment.js
vendored
6
dist/flow-typed/Comment.js
vendored
|
@ -10,12 +10,14 @@ declare type Comment = {
|
||||||
signature?: string, // signature of comment by originating channel
|
signature?: string, // signature of comment by originating channel
|
||||||
signing_ts?: string, // timestamp used when signing this comment
|
signing_ts?: string, // timestamp used when signing this comment
|
||||||
is_channel_signature_valid?: boolean, // whether or not the signature could be validated
|
is_channel_signature_valid?: boolean, // whether or not the signature could be validated
|
||||||
parent_id?: number, // comment_id of comment this is in reply to
|
parent_id?: string, // if present, the comment is a reply
|
||||||
};
|
};
|
||||||
|
|
||||||
// todo: relate individual comments to their commentId
|
// todo: implement --is_mine for comment_list
|
||||||
|
// todo: rename byId to commentsByClaimId
|
||||||
declare type CommentsState = {
|
declare type CommentsState = {
|
||||||
commentsByUri: { [string]: string },
|
commentsByUri: { [string]: string },
|
||||||
|
repliesByCommentId: { [string]: Array<string> },
|
||||||
byId: { [string]: Array<string> },
|
byId: { [string]: Array<string> },
|
||||||
commentById: { [string]: Comment },
|
commentById: { [string]: Comment },
|
||||||
isLoading: boolean,
|
isLoading: boolean,
|
||||||
|
|
6
flow-typed/Comment.js
vendored
6
flow-typed/Comment.js
vendored
|
@ -10,12 +10,14 @@ declare type Comment = {
|
||||||
signature?: string, // signature of comment by originating channel
|
signature?: string, // signature of comment by originating channel
|
||||||
signing_ts?: string, // timestamp used when signing this comment
|
signing_ts?: string, // timestamp used when signing this comment
|
||||||
is_channel_signature_valid?: boolean, // whether or not the signature could be validated
|
is_channel_signature_valid?: boolean, // whether or not the signature could be validated
|
||||||
parent_id?: number, // comment_id of comment this is in reply to
|
parent_id?: string, // if present, the comment is a reply
|
||||||
};
|
};
|
||||||
|
|
||||||
// todo: relate individual comments to their commentId
|
// todo: implement --is_mine for comment_list
|
||||||
|
// todo: rename byId to commentsByClaimId
|
||||||
declare type CommentsState = {
|
declare type CommentsState = {
|
||||||
commentsByUri: { [string]: string },
|
commentsByUri: { [string]: string },
|
||||||
|
repliesByCommentId: { [string]: Array<string> },
|
||||||
byId: { [string]: Array<string> },
|
byId: { [string]: Array<string> },
|
||||||
commentById: { [string]: Comment },
|
commentById: { [string]: Comment },
|
||||||
isLoading: boolean,
|
isLoading: boolean,
|
||||||
|
|
|
@ -234,7 +234,11 @@ export {
|
||||||
selectMyStreamUrlsCount,
|
selectMyStreamUrlsCount,
|
||||||
} from 'redux/selectors/claims';
|
} from 'redux/selectors/claims';
|
||||||
|
|
||||||
export { makeSelectCommentsForUri } from 'redux/selectors/comments';
|
export {
|
||||||
|
makeSelectCommentsForUri,
|
||||||
|
makeSelectCommentReplyCount,
|
||||||
|
makeSelectCommentReplyList,
|
||||||
|
} from 'redux/selectors/comments';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
makeSelectFileInfoForUri,
|
makeSelectFileInfoForUri,
|
||||||
|
|
|
@ -4,7 +4,13 @@ import Lbry from 'lbry';
|
||||||
import { selectClaimsByUri, selectMyChannelClaims } from 'redux/selectors/claims';
|
import { selectClaimsByUri, selectMyChannelClaims } from 'redux/selectors/claims';
|
||||||
import { doToast } from 'redux/actions/notifications';
|
import { doToast } from 'redux/actions/notifications';
|
||||||
|
|
||||||
export function doCommentList(uri: string, page: number = 1, pageSize: number = 99999) {
|
// if parentId is provided, `uri` is ignored
|
||||||
|
export function doCommentList(
|
||||||
|
uri: string,
|
||||||
|
page: number = 1,
|
||||||
|
pageSize: number = 99999, // while the desktop app doesn't paginate
|
||||||
|
parentId?: string
|
||||||
|
) {
|
||||||
return (dispatch: Dispatch, getState: GetState) => {
|
return (dispatch: Dispatch, getState: GetState) => {
|
||||||
const state = getState();
|
const state = getState();
|
||||||
const claim = selectClaimsByUri(state)[uri];
|
const claim = selectClaimsByUri(state)[uri];
|
||||||
|
@ -17,6 +23,7 @@ export function doCommentList(uri: string, page: number = 1, pageSize: number =
|
||||||
claim_id: claimId,
|
claim_id: claimId,
|
||||||
page,
|
page,
|
||||||
page_size: pageSize,
|
page_size: pageSize,
|
||||||
|
parentId: parentId,
|
||||||
})
|
})
|
||||||
.then((result: CommentListResponse) => {
|
.then((result: CommentListResponse) => {
|
||||||
const { items: comments } = result;
|
const { items: comments } = result;
|
||||||
|
@ -26,6 +33,7 @@ export function doCommentList(uri: string, page: number = 1, pageSize: number =
|
||||||
comments,
|
comments,
|
||||||
claimId: claimId,
|
claimId: claimId,
|
||||||
uri: uri,
|
uri: uri,
|
||||||
|
parentId: parentId,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
@ -66,6 +74,7 @@ export function doCommentCreate(
|
||||||
data: {
|
data: {
|
||||||
comment: result,
|
comment: result,
|
||||||
claimId: claim_id,
|
claimId: claim_id,
|
||||||
|
parentId: parent_id,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
|
@ -7,6 +7,7 @@ const defaultState: CommentsState = {
|
||||||
byId: {}, // ClaimID -> list of comments
|
byId: {}, // ClaimID -> list of comments
|
||||||
commentsByUri: {}, // URI -> claimId
|
commentsByUri: {}, // URI -> claimId
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
|
repliesByCommentId: {}, // commentId -> list of commentIds
|
||||||
myComments: undefined,
|
myComments: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -23,9 +24,10 @@ export const commentReducer = handleActions(
|
||||||
}),
|
}),
|
||||||
|
|
||||||
[ACTIONS.COMMENT_CREATE_COMPLETED]: (state: CommentsState, action: any): CommentsState => {
|
[ACTIONS.COMMENT_CREATE_COMPLETED]: (state: CommentsState, action: any): CommentsState => {
|
||||||
const { comment, claimId }: { comment: Comment, claimId: string } = action.data;
|
const { comment, claimId, parentId } = action.data;
|
||||||
const commentById = Object.assign({}, state.commentById);
|
const commentById = Object.assign({}, state.commentById);
|
||||||
const byId = Object.assign({}, state.byId);
|
const byId = Object.assign({}, state.byId);
|
||||||
|
const repliesByCommentId = Object.assign({}, state.repliesByCommentId);
|
||||||
const comments = byId[claimId];
|
const comments = byId[claimId];
|
||||||
const newCommentIds = comments.slice();
|
const newCommentIds = comments.slice();
|
||||||
|
|
||||||
|
@ -36,6 +38,13 @@ export const commentReducer = handleActions(
|
||||||
newCommentIds.unshift(comment.comment_id);
|
newCommentIds.unshift(comment.comment_id);
|
||||||
byId[claimId] = newCommentIds;
|
byId[claimId] = newCommentIds;
|
||||||
|
|
||||||
|
if (parentId) {
|
||||||
|
const newReplies = repliesByCommentId[parentId] || [];
|
||||||
|
// unlike regular comments, newest replies should be at the bottom of list
|
||||||
|
newReplies.push(comment.comment_id);
|
||||||
|
repliesByCommentId[parentId] = newReplies;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
commentById,
|
commentById,
|
||||||
|
@ -47,32 +56,50 @@ export const commentReducer = handleActions(
|
||||||
[ACTIONS.COMMENT_LIST_STARTED]: state => ({ ...state, isLoading: true }),
|
[ACTIONS.COMMENT_LIST_STARTED]: state => ({ ...state, isLoading: true }),
|
||||||
|
|
||||||
[ACTIONS.COMMENT_LIST_COMPLETED]: (state: CommentsState, action: any) => {
|
[ACTIONS.COMMENT_LIST_COMPLETED]: (state: CommentsState, action: any) => {
|
||||||
const { comments, claimId, uri } = action.data;
|
const { comments, claimId, uri, parentId } = action.data;
|
||||||
|
|
||||||
const commentById = Object.assign({}, state.commentById);
|
const commentById = Object.assign({}, state.commentById);
|
||||||
const byId = Object.assign({}, state.byId);
|
const byId = Object.assign({}, state.byId);
|
||||||
const commentsByUri = Object.assign({}, state.commentsByUri);
|
const commentsByUri = Object.assign({}, state.commentsByUri);
|
||||||
|
const repliesByCommentId = Object.assign({}, state.repliesByCommentId);
|
||||||
|
|
||||||
if (comments) {
|
if (comments) {
|
||||||
// we use an Array to preserve order of listing
|
// we use an Array to preserve order of listing
|
||||||
// in reality this doesn't matter and we can just
|
// in reality this doesn't matter and we can just
|
||||||
// sort comments by their timestamp
|
// sort comments by their timestamp
|
||||||
const commentIds = Array(comments.length);
|
const commentIds = Array(comments.length);
|
||||||
|
const replyThreads = {};
|
||||||
|
|
||||||
// map the comment_ids to the new comments
|
// map the comment_ids to the new comments
|
||||||
for (let i = 0; i < comments.length; i++) {
|
for (let i = 0; i < comments.length; i++) {
|
||||||
commentIds[i] = comments[i].comment_id;
|
const comment = comments[i];
|
||||||
commentById[commentIds[i]] = comments[i];
|
commentIds[i] = comment.comment_id;
|
||||||
|
commentById[commentIds[i]] = comment;
|
||||||
|
|
||||||
|
if (comment.parent_id) {
|
||||||
|
if (!(comment.parent_id in replyThreads)) {
|
||||||
|
replyThreads[comment.parent_id] = [];
|
||||||
|
}
|
||||||
|
replyThreads[comment.parent_id].push(comment.comment_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
byId[claimId] = commentIds;
|
Object.entries(replyThreads).forEach((parent_id, replyIds) => {
|
||||||
|
repliesByCommentId[parent_id] = replyIds;
|
||||||
|
});
|
||||||
|
|
||||||
commentsByUri[uri] = claimId;
|
commentsByUri[uri] = claimId;
|
||||||
|
// don't override the entire list with the replies to one comment
|
||||||
|
if (parentId == null) {
|
||||||
|
byId[claimId] = commentIds;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
byId,
|
byId,
|
||||||
commentById,
|
commentById,
|
||||||
commentsByUri,
|
commentsByUri,
|
||||||
|
repliesByCommentId,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
@ -89,21 +116,37 @@ export const commentReducer = handleActions(
|
||||||
const { comment_id } = action.data;
|
const { comment_id } = action.data;
|
||||||
const commentById = Object.assign({}, state.commentById);
|
const commentById = Object.assign({}, state.commentById);
|
||||||
const byId = Object.assign({}, state.byId);
|
const byId = Object.assign({}, state.byId);
|
||||||
|
const repliesByCommentId = Object.assign({}, state.repliesByCommentId);
|
||||||
|
|
||||||
// to remove the comment and its references
|
const comment: Comment = commentById[comment_id];
|
||||||
const claimId = commentById[comment_id].claim_id;
|
|
||||||
for (let i = 0; i < byId[claimId].length; i++) {
|
// keep record of comment's existence if it has replies
|
||||||
if (byId[claimId][i] === comment_id) {
|
if (!(comment.comment_id in repliesByCommentId)) {
|
||||||
byId[claimId].splice(i, 1);
|
const claimId = commentById[comment_id].claim_id;
|
||||||
break;
|
for (let i = 0; i < byId[claimId].length; i++) {
|
||||||
|
if (byId[claimId][i] === comment_id) {
|
||||||
|
byId[claimId].splice(i, 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (comment.parent_id) {
|
||||||
|
for (let i = 0; i < repliesByCommentId[comment.parent_id]; i++) {
|
||||||
|
if (repliesByCommentId[comment.parent_id][i] === comment.comment_id) {
|
||||||
|
repliesByCommentId[comment.parent_id].splice(i, 1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
delete commentById[comment_id];
|
delete commentById[comment_id];
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
commentById,
|
commentById,
|
||||||
byId,
|
byId,
|
||||||
|
repliesByCommentId,
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
|
@ -8,20 +8,60 @@ export const selectCommentsById = createSelector(
|
||||||
state => state.commentById || {}
|
state => state.commentById || {}
|
||||||
);
|
);
|
||||||
|
|
||||||
export const selectCommentsByClaimId = createSelector(
|
export const selectReplyIdsById = createSelector(
|
||||||
selectState,
|
selectState,
|
||||||
selectCommentsById,
|
state => state.repliesByCommentId || {}
|
||||||
(state, byId) => {
|
);
|
||||||
const byClaimId = state.byId || {};
|
|
||||||
|
export const selectRepliesById = createSelector(
|
||||||
|
selectState,
|
||||||
|
selectReplyIdsById,
|
||||||
|
(state, repliesById) => {
|
||||||
|
const byCommentId = state.commentById || {};
|
||||||
const comments = {};
|
const comments = {};
|
||||||
|
|
||||||
|
Object.keys(repliesById).forEach(parentId => {
|
||||||
|
comments[parentId] = [];
|
||||||
|
for (const commentId of repliesById[parentId]) {
|
||||||
|
comments[parentId].push(byCommentId[commentId]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return comments;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
export const selectRepliesByClaimId = createSelector(
|
||||||
|
selectState,
|
||||||
|
selectRepliesById,
|
||||||
|
(state, repliesById) => {
|
||||||
|
const byClaimId = state.byId || {};
|
||||||
|
const claimThreads = {};
|
||||||
|
|
||||||
|
Object.keys(byClaimId).forEach(claimId => {
|
||||||
|
claimThreads[claimId] = {};
|
||||||
|
for (const commentId of byClaimId[claimId]) {
|
||||||
|
if (repliesById[commentId]) {
|
||||||
|
claimThreads[claimId][commentId] = repliesById[commentId];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return claimThreads;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
export const selectAllCommentsByClaimId = createSelector(
|
||||||
|
selectState,
|
||||||
|
selectCommentsById,
|
||||||
|
(state, commentById) => {
|
||||||
|
const byClaimId = state.byId || {};
|
||||||
|
const comments = {};
|
||||||
// replace every comment_id in the list with the actual comment object
|
// replace every comment_id in the list with the actual comment object
|
||||||
Object.keys(byClaimId).forEach(claimId => {
|
Object.keys(byClaimId).forEach(claimId => {
|
||||||
const commentIds = byClaimId[claimId];
|
const commentIds = byClaimId[claimId] || [];
|
||||||
|
|
||||||
comments[claimId] = Array(commentIds === null ? 0 : commentIds.length);
|
comments[claimId] = [];
|
||||||
for (let i = 0; i < commentIds.length; i++) {
|
for (const commentId of commentIds) {
|
||||||
comments[claimId][i] = byId[commentIds[i]];
|
comments[claimId].push(commentById[commentId]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -36,16 +76,13 @@ export const selectCommentsByClaimId = createSelector(
|
||||||
); */
|
); */
|
||||||
export const selectCommentsByUri = createSelector(
|
export const selectCommentsByUri = createSelector(
|
||||||
selectState,
|
selectState,
|
||||||
state => {
|
selectAllCommentsByClaimId,
|
||||||
|
(state, commentsByClaimId) => {
|
||||||
const byUri = state.commentsByUri || {};
|
const byUri = state.commentsByUri || {};
|
||||||
const comments = {};
|
const comments = {};
|
||||||
Object.keys(byUri).forEach(uri => {
|
Object.keys(byUri).forEach(uri => {
|
||||||
const claimId = byUri[uri];
|
const claimId = byUri[uri];
|
||||||
if (claimId === null) {
|
comments[uri] = commentsByClaimId[claimId];
|
||||||
comments[uri] = null;
|
|
||||||
} else {
|
|
||||||
comments[uri] = claimId;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return comments;
|
return comments;
|
||||||
|
@ -54,13 +91,19 @@ export const selectCommentsByUri = createSelector(
|
||||||
|
|
||||||
export const makeSelectCommentsForUri = (uri: string) =>
|
export const makeSelectCommentsForUri = (uri: string) =>
|
||||||
createSelector(
|
createSelector(
|
||||||
selectCommentsByClaimId,
|
|
||||||
selectCommentsByUri,
|
selectCommentsByUri,
|
||||||
(byClaimId, byUri) => {
|
byUri => byUri[uri]
|
||||||
const claimId = byUri[uri];
|
|
||||||
return byClaimId && byClaimId[claimId];
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// todo: allow SDK to retrieve user comments through comment_list
|
export const makeSelectCommentReplyCount = (commentId: string) =>
|
||||||
// todo: implement selectors for selecting comments owned by user
|
createSelector(
|
||||||
|
selectReplyIdsById,
|
||||||
|
repliesById => (repliesById[commentId] ? repliesById[commentId].length : 0)
|
||||||
|
);
|
||||||
|
|
||||||
|
export const makeSelectCommentReplyList = (commentId: string) => {
|
||||||
|
createSelector(
|
||||||
|
selectRepliesById,
|
||||||
|
repliesById => repliesById[commentId]
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in a new issue