static ArrayList<Comments> handleSubcommenting(ArrayList<Comments> comments) { Map<String, Integer> quickIndex = new HashMap<>(); for (int i = 0; i < comments.size(); i++) quickIndex.put(comments.get(i).id, i); // Create adjacency list of comments to parents (math!) Map<Comments, ArrayList<Comments>> cGraph = new HashMap<>(); for (Comments c : comments) { cGraph.put(c, new ArrayList<Comments>()); if (!c.parent.isEmpty()) { int parent = quickIndex.get(c.parent); cGraph.get(comments.get(parent)).add(c); } } // Depth-first search for ordering Set<Comments> unvisited = new HashSet<>(); // For keeping track of which are not sorted yet unvisited.addAll(comments); Stack<Comments> toVisit = new Stack<>(); ArrayList<Comments> visited = new ArrayList<>(); // will be returned for (Comments c : comments) { if (unvisited.contains(c)) toVisit.push(c); // a root node (comment w/ no indent) // Visit all children of root node (all comment w/ indents under root comment) while (!toVisit.empty()) { Comments curr = toVisit.pop(); // deepest children pushed last in stack unvisited.remove(curr); // not to be considered again after while loop visited.add(curr); // added to list of comments in order if (!curr.parent.isEmpty()) { // set indent int parent = quickIndex.get(curr.parent); curr.indent = comments.get(parent).indent + 1; } // Push backwards so earliest post is pushed last in stack (maintain date of posting) List<Comments> children = cGraph.get(curr); for (int i = children.size() - 1; i != -1; i--) toVisit.push(children.get(i)); } } return visited; }
// LIST_POSTS Disqus: two internet requests to load comments // TEMP: Until we don't need a separate call to get thread id (b/c we already have it from using // primarily JSON for articles, return both comments and thread id. // Used only by DisqusGetComments in background TempCommentsAndThreadId getComments(String s, boolean justRefresh) { String threadId = s; // Request 1 if (!justRefresh) { HttpDetails.ThreadIdPair threadIdPair = httpMethods.getMsoThreadId(s); // If Request 1 fails, either error 1 or 2 if (threadIdPair.code != 0) return new TempCommentsAndThreadId(null, null, threadIdPair.code); threadId = threadIdPair.threadId; } // Request 2 (Pure http request) String commentsJson = disqusMethods.getCommentsJson(threadId); if (commentsJson != null) return new TempCommentsAndThreadId(Comments.parseCommentsArray(commentsJson), threadId, 0); else return new TempCommentsAndThreadId(null, threadId, 3); }