Skip to content

Commit

Permalink
Use full names for comment authors and their mentions in replies
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiGr committed Dec 4, 2022
1 parent 3d512d5 commit 75d875f
Showing 1 changed file with 31 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,30 @@
public class SoundcloudCommentsInfoItemExtractor implements CommentsInfoItemExtractor {
public static final String USER = "user";
public static final String BODY = "body";
public static final String USER_PERMALINK = "permalink";

private final JsonObject json;
private final int index;
private final JsonObject item;
private final String url;
private final JsonObject superComment;

private int replyCount = CommentsInfoItem.UNKNOWN_REPLY_COUNT;
private Page repliesPage = null;

public SoundcloudCommentsInfoItemExtractor(final JsonObject json, final int index, final JsonObject item, final String url) {
public SoundcloudCommentsInfoItemExtractor(final JsonObject json, final int index,
final JsonObject item, final String url,
@Nullable final JsonObject superComment) {
this.json = json;
this.index = index;
this.item = item;
this.url = url;
this.superComment = superComment;
}

public SoundcloudCommentsInfoItemExtractor(final JsonObject json, final int index,
final JsonObject item, final String url) {
this(json, index, item, url, null);
}

@Override
Expand All @@ -43,12 +53,27 @@ public String getCommentId() {

@Override
public Description getCommentText() {
return new Description(item.getString(BODY), Description.PLAIN_TEXT);
String commentContent = item.getString(BODY);
if (superComment == null) {
return new Description(commentContent, Description.PLAIN_TEXT);
}
// This comment is a reply to another comment.
// Therefore, the comment starts with the mention of the original comment's author.
// The account is automatically linked by the SoundCloud web UI.
// We need to do this manually.
final JsonObject user = superComment.getObject("user");
final String link = "<a href=\"" + user.getString("permalink_url") + "\">"
+ "@" + user.getString("full_name") + "</a>";
commentContent = commentContent
.replace("@" + user.getString(USER_PERMALINK), link)
.replace("@" + superComment.getInt("user_id"), link);

return new Description(commentContent, Description.HTML);
}

@Override
public String getUploaderName() {
return item.getObject(USER).getString("username");
return item.getObject(USER).getString("full_name");
}

@Override
Expand Down Expand Up @@ -105,7 +130,7 @@ public Page getReplies() {
ServiceList.SoundCloud.getServiceId());
final JsonArray jsonArray = new JsonArray();
// Replies start with the mention of the user who created the original comment.
final String mention = "@" + item.getObject(USER).getString("permalink");
final String mention = "@" + item.getObject(USER).getString(USER_PERMALINK);
// Loop through all comments which come after the original comment to find its replies.
final JsonArray allItems = json.getArray(SoundcloudCommentsExtractor.COLLECTION);
for (int i = index + 1; i < allItems.size(); i++) {
Expand All @@ -114,7 +139,8 @@ public Page getReplies() {
if (commentContent.startsWith(mention)) {
replies.add(comment);
jsonArray.add(comment);
collector.commit(new SoundcloudCommentsInfoItemExtractor(json, i, comment, url));
collector.commit(new SoundcloudCommentsInfoItemExtractor(
json, i, comment, url, item));
} else if (!commentContent.startsWith("@") || replies.isEmpty()) {
// Only the comments directly after the original comment
// starting with the mention of the comment's creator
Expand Down

0 comments on commit 75d875f

Please sign in to comment.