always show your own comments at the top

This commit is contained in:
Sean Yesmunt 2020-10-07 16:18:16 -04:00
parent c43eff8587
commit 5d2d6d2a94
3 changed files with 30 additions and 12 deletions

View file

@ -4,8 +4,8 @@ declare type Comment = {
claim_id: string, // id linking to the claim this comment
timestamp: number, // integer representing unix-time
is_hidden: boolean, // claim owner may enable/disable this
channel_id?: string, // claimId of channel signing this comment
channel_name?: string, // name of channel claim
channel_id: string, // claimId of channel signing this comment
channel_name?: string, // name of channel claim
channel_url?: string, // full lbry url to signing channel
signature?: string, // signature of comment by originating channel
signing_ts?: string, // timestamp used when signing this comment
@ -35,4 +35,4 @@ declare type CommentReactParams = {
react_type: string,
clear_types?: string,
remove?: boolean,
}
};

View file

@ -52,10 +52,8 @@ function CommentList(props: Props) {
const [readyToDisplayComments, setReadyToDisplayComments] = React.useState(!ENABLE_COMMENT_REACTIONS);
const linkedCommentId = linkedComment && linkedComment.comment_id;
const hasNoComments = totalComments === 0;
const moreBelow = totalComments - end > 0;
// todo: implement comment_list --mine in SDK so redux can grab with selectCommentIsMine
const isMyComment = (channelId: string) => {
const isMyComment = (channelId: string): boolean => {
if (myChannels != null && channelId != null) {
for (let i = 0; i < myChannels.length; i++) {
if (myChannels[i].claim_id === channelId) {
@ -140,7 +138,9 @@ function CommentList(props: Props) {
}
// Default to newest first for apps that don't have comment reactions
const sortedComments = ENABLE_COMMENT_REACTIONS ? sortComments(comments, reactionsById, sort) : comments;
const sortedComments = ENABLE_COMMENT_REACTIONS
? sortComments({ comments, reactionsById, sort, isMyComment })
: comments;
const displayedComments = readyToDisplayComments
? prepareComments(sortedComments, linkedComment).slice(start, end)
: [];

View file

@ -5,16 +5,34 @@ import { SORT_COMMENTS_NEW, SORT_COMMENTS_BEST, SORT_COMMENTS_CONTROVERSIAL } fr
// Mostly taken from Reddit's sorting functions
// https://github.com/reddit-archive/reddit/blob/master/r2/r2/lib/db/_sorts.pyx
export function sortComments(comments: ?Array<Comment>, reactionsById: {}, method: string): Array<Comment> {
type SortProps = {
comments: ?Array<Comment>,
reactionsById: {},
sort: string,
isMyComment: string => boolean,
};
export function sortComments(sortProps: SortProps): Array<Comment> {
const { comments, reactionsById, sort, isMyComment } = sortProps;
if (!comments) {
return [];
}
return comments.slice().sort((a, b) => {
if (method === SORT_COMMENTS_NEW) {
return comments.slice().sort((a: Comment, b: Comment) => {
if (sort === SORT_COMMENTS_NEW) {
return 0;
}
const aIsMine = isMyComment(a.channel_id);
const bIsMine = isMyComment(b.channel_id);
if (aIsMine) {
return -1;
} else if (bIsMine) {
return 1;
}
const aReactions = reactionsById[a.comment_id];
const bReactions = reactionsById[b.comment_id];
const aLikes = (aReactions && aReactions[REACTION_TYPES.LIKE]) || 0;
@ -22,7 +40,7 @@ export function sortComments(comments: ?Array<Comment>, reactionsById: {}, metho
const bLikes = (bReactions && bReactions[REACTION_TYPES.LIKE]) || 0;
const bDislikes = (bReactions && bReactions[REACTION_TYPES.DISLIKE]) || 0;
if (method === SORT_COMMENTS_CONTROVERSIAL) {
if (sort === SORT_COMMENTS_CONTROVERSIAL) {
if (aLikes === 0 && aDislikes === 0) {
return 1;
} else if (bLikes === 0 && bDislikes === 0) {
@ -38,7 +56,7 @@ export function sortComments(comments: ?Array<Comment>, reactionsById: {}, metho
return bMagnitude ** bBalance - aMagnitude ** aBalance;
}
if (method === SORT_COMMENTS_BEST) {
if (sort === SORT_COMMENTS_BEST) {
const aN = aLikes + aDislikes;
const bN = bLikes + bDislikes;