Skip to content

Commit

Permalink
Merge pull request #3781 from coralproject/release-6.15.4
Browse files Browse the repository at this point in the history
Release 6.15.4
  • Loading branch information
tessalt authored Nov 11, 2021
2 parents 7cfd71a + 4f5f317 commit 1204c46
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM node:12-alpine

# Install build dependancies.
RUN apk --no-cache add git python
RUN apk --update --no-cache add git python3

# Create app directory.
RUN mkdir -p /usr/src/app
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coralproject/talk",
"version": "6.15.3",
"version": "6.15.4",
"author": "The Coral Project",
"homepage": "https://coralproject.net/",
"sideEffects": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ import { CreateCommentReplyMutation as MutationTypes } from "coral-stream/__gene

import {
determineDepthTillAncestor,
determineDepthTillStory,
getFlattenedReplyAncestorID,
incrementStoryCommentCounts,
isPublished,
lookupFlattenReplies,
lookupStoryConnectionKey,
lookupStoryConnectionOrderBy,
lookupStoryConnectionTag,
prependCommentEdgeToProfile,
} from "../../helpers";

Expand Down Expand Up @@ -156,7 +160,16 @@ function addCommentReplyToStory(
const flattenReplies = lookupFlattenReplies(environment);
const singleCommentID = lookup(environment, LOCAL_ID).commentID;
const comment = commentEdge.getLinkedRecord("node")!;
const depth = determineDepthTillAncestor(comment, singleCommentID);
const depth = singleCommentID
? determineDepthTillAncestor(store, comment, singleCommentID)
: determineDepthTillStory(
store,
comment,
input.storyID,
lookupStoryConnectionOrderBy(environment),
lookupStoryConnectionKey(environment),
lookupStoryConnectionTag(environment)
);

if (depth === null) {
// could not trace back to ancestor, that should not happen.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { CommentEnteredSubscription } from "coral-stream/__generated__/CommentEn

import {
determineDepthTillAncestor,
determineDepthTillStory,
getFlattenedReplyAncestorID,
lookupFlattenReplies,
} from "../../helpers";
Expand Down Expand Up @@ -81,12 +82,30 @@ function insertReply(
store: RecordSourceSelectorProxy<unknown>,
liveDirectRepliesInsertion: boolean,
flattenReplies: boolean,
storyID: string,
storyConnectionKey: string,
orderBy?: GQLCOMMENT_SORT_RL,
tag?: string,
ancestorID?: string | null
) {
const comment = store
.getRootField("commentEntered")!
.getLinkedRecord("comment")!;
const depth = determineDepthTillAncestor(comment, ancestorID);

if (!ancestorID && !orderBy) {
throw new Error("orderBy not set");
}

const depth = ancestorID
? determineDepthTillAncestor(store, comment, ancestorID)
: determineDepthTillStory(
store,
comment,
storyID,
orderBy!,
storyConnectionKey,
tag
);

if (depth === null) {
// could not trace back to ancestor, discard.
Expand All @@ -110,12 +129,10 @@ function insertReply(
parentProxy = store.get(parentID)!;
}

const connectionKey = "ReplyList_replies";
const filters = { orderBy: "CREATED_AT_ASC" };
const connection = ConnectionHandler.getConnection(
parentProxy,
connectionKey,
filters
"ReplyList_replies",
{ orderBy: GQLCOMMENT_SORT.CREATED_AT_ASC }
);
if (!connection) {
// If it has no connection, it could not have been
Expand Down Expand Up @@ -230,6 +247,10 @@ const CommentEnteredSubscription = createSubscription(
store,
Boolean(variables.liveDirectRepliesInsertion),
flattenReplies,
variables.storyID,
variables.storyConnectionKey,
variables.orderBy,
variables.tag,
variables.ancestorID
);
return;
Expand Down
179 changes: 179 additions & 0 deletions src/core/client/stream/tabs/Comments/helpers/determineDepthTill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import { LOCAL_ID } from "coral-framework/lib/relay";
import { GQLCOMMENT_SORT } from "coral-framework/schema";
import { MAX_REPLY_INDENT_DEPTH } from "coral-stream/constants";
import {
ConnectionHandler,
RecordProxy,
RecordSourceSelectorProxy,
} from "relay-runtime";

function isNodeIDInConnection(
id: string,
connection: RecordProxy<any>
): boolean {
return connection
.getLinkedRecords("edges")
?.some((edge) => edge.getLinkedRecord("node")?.getDataID() === id);
}

function isNodeIDInRepliesConnection(
id: string,
connection: RecordProxy<any>
): boolean {
return connection
.getLinkedRecords("edges")
?.concat(connection.getLinkedRecords("viewNewEdges") || [])
.some((edge) => edge.getLinkedRecord("node")?.getDataID() === id);
}

function isCommentInsideParentRepliesConnection(comment: RecordProxy) {
const parent = comment.getLinkedRecord("parent");
if (!parent) {
return false;
}

// Check if comment is inside parent replies connection
const repliesConnection = ConnectionHandler.getConnection(
parent,
"ReplyList_replies",
{ orderBy: GQLCOMMENT_SORT.CREATED_AT_ASC }
)!;
if (!repliesConnection) {
return false;
}
return isNodeIDInRepliesConnection(comment.getDataID(), repliesConnection);
}

function findFlattenedParent(comment: RecordProxy, ancestorID?: string | null) {
const maxDepth = MAX_REPLY_INDENT_DEPTH + 1; // +1 to include root level.
const ancestors: RecordProxy[] = [];
let cur = comment;
while (cur) {
ancestors.unshift(cur);
if (cur.getDataID() === ancestorID) {
break;
}
cur = cur.getLinkedRecord("parent");
}
if (ancestors.length > maxDepth) {
return {
flattendParent: ancestors[maxDepth - 1],
exceededDepth: ancestors.length - maxDepth,
};
}
return null;
}

/**
* Returns depth until story or null if could not trace back to
* comments that is loaded inside the stream!
*/
export function determineDepthTillStory(
store: RecordSourceSelectorProxy<unknown>,
comment: RecordProxy,
storyID: string,
orderBy: string,
storyConnectionKey: string,
tag?: string
) {
const story = store.get(storyID)!;
const storyConnection = ConnectionHandler.getConnection(
story,
storyConnectionKey,
{
orderBy,
tag,
}
)!;

// This is a top level comment and part of story, return 0;
if (isNodeIDInConnection(comment.getDataID(), storyConnection)) {
return 0;
}

const firstParent: RecordProxy | null | undefined = comment.getLinkedRecord(
"parent"
);
// When first parent is null and it is not in the story connection: Return null;
if (!firstParent) {
return null;
}

// If flattenReplies start iterating from flattened Parent.
const flattenReplies = store.get(LOCAL_ID)?.getValue("flattenReplies");
const result = flattenReplies ? findFlattenedParent(comment) : null;
let depth = result !== null ? result.exceededDepth : 1;
let cur = result !== null ? result.flattendParent : firstParent;

while (cur) {
const parent: RecordProxy | null = cur.getLinkedRecord("parent");
if (parent === null) {
// Reaching a top level comment.
if (!isNodeIDInConnection(cur.getDataID(), storyConnection)) {
// Not in story, return null.
return null;
}
// Return depth otherwise.
return depth;
} else if (isCommentInsideParentRepliesConnection(cur)) {
// Current comment is inside the replies connection of the parent.
depth++;
} else {
// Current comment is not in the replies connection of the parent.
return null;
}
cur = parent;
}
return depth;
}
/**
* Returns depth until ancestor or null if could not trace back to
* comments that is loaded inside the stream!
*/
export function determineDepthTillAncestor(
store: RecordSourceSelectorProxy<unknown>,
comment: RecordProxy,
ancestorID?: string | null
) {
// Already ancestor, return 0;
if (comment.getDataID() === ancestorID) {
return 0;
}
const firstParent: RecordProxy | null | undefined = comment.getLinkedRecord(
"parent"
);
if (!firstParent) {
// It's a top level comment, so can't determine depth till ancestor.
return null;
}

// Parent is ancestor, return 1.
if (firstParent.getDataID() === ancestorID) {
return 1;
}

// If flattenReplies start iterating from flattened Parent.
const flattenReplies = store.get(LOCAL_ID)?.getValue("flattenReplies");
const result = flattenReplies
? findFlattenedParent(comment, ancestorID)
: null;
let depth = result !== null ? result.exceededDepth : 1;
let cur = result !== null ? result.flattendParent : firstParent;
while (cur) {
const parent: RecordProxy | null = cur.getLinkedRecord("parent");
if (parent !== null) {
if (!isCommentInsideParentRepliesConnection(cur)) {
// Comment is not inside parents replies connection.
return null;
}
depth++;
if (parent.getDataID() === ancestorID) {
// Reaching ancestor, return depth.
return depth;
}
}
cur = parent;
}
// Could not trace back to ancestor.
return null;
}

This file was deleted.

8 changes: 7 additions & 1 deletion src/core/client/stream/tabs/Comments/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ export { default as useStaticFlattenReplies } from "./useStaticFlattenReplies";
export { default as useAMP } from "./useAMP";
export { default as lookupFlattenReplies } from "./lookupFlattenReplies";
export { default as getFlattenedReplyAncestorID } from "./getFlattenedReplyAncestorID";
export { default as determineDepthTillAncestor } from "./determineDepthTillAncestor";
export {
determineDepthTillAncestor,
determineDepthTillStory,
} from "./determineDepthTill";
export { default as lookupStoryConnectionKey } from "./lookupStoryConnectionKey";
export { default as lookupStoryConnectionTag } from "./lookupStoryConnectionTag";
export { default as lookupStoryConnectionOrderBy } from "./lookupStoryConnectionOrderBy";
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Environment } from "react-relay";

import { LOCAL_ID, lookup } from "coral-framework/lib/relay";

export default function lookupStoryConnectionKey(
environment: Environment
): string {
switch (lookup(environment, LOCAL_ID).commentsTab) {
case "UNANSWERED_COMMENTS":
return "UnansweredStream_comments";
}
return "Stream_comments";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Environment } from "react-relay";

import { LOCAL_ID, lookup } from "coral-framework/lib/relay";

export default function lookupStoryConnectionOrderBy(
environment: Environment
): string {
return lookup(environment, LOCAL_ID).commentsOrderBy;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Environment } from "react-relay";

import { LOCAL_ID, lookup } from "coral-framework/lib/relay";
import { GQLTAG } from "coral-framework/schema";

export default function lookupStoryConnectionTag(
environment: Environment
): string | undefined {
switch (lookup(environment, LOCAL_ID).commentsTab) {
case "REVIEWS":
return GQLTAG.REVIEW;
case "QUESTIONS":
return GQLTAG.QUESTION;
case "UNANSWERED_COMMENTS":
return GQLTAG.UNANSWERED;
}
return undefined;
}

0 comments on commit 1204c46

Please sign in to comment.