// Adding a comment to an activity
let comment = try await feed.addComment(
request: .init(
comment: "So great!",
custom: ["sentiment": "positive"],
objectId: "activity_123",
objectType: "activity",
)
)
// Adding a reply to a comment
let reply = try await feed.addComment(
request: .init(
comment: "I agree!",
objectId: "activity_123",
objectType: "activity",
parentId: "comment_456"
)
)
Activity Feeds V3 is in closed alpha — do not use it in production (just yet).
Comments
Overview
Comments support voting, ranking, threading, images, URL previews, mentions and notifications.
Adding Comments
// Adding a comment to an activity
await client.addComment({
comment: "So great!",
object_id: "activity_123",
object_type: "activity",
custom: {
sentiment: "positive",
},
});
// Adding a reply to a comment
await client.addComment({
comment: "I agree!",
object_id: "activity_123",
object_type: "activity",
parent_id: "comment_456",
});
// Adding a comment to an activity
await serverClient.feeds.addComment({
comment: "So great!",
object_id: "activity_123",
object_type: "activity",
user_id: "<user_id>",
custom: {
sentiment: "positive",
},
});
// Adding a reply to a comment
await serverClient.feeds.addComment({
comment: "I agree!",
object_id: "activity_123",
object_type: "activity",
parent_id: "comment_456",
user_id: "<user_id>",
});
Updating Comments
try await feed.updateComment(
commentId: "comment_123",
request: .init(
comment: "Not so great",
custom: ["edited": true]
)
)
// Update a comment
await client.updateComment({
comment_id: "comment_123",
comment: "Updated comment",
custom: {
edited: true,
},
});
// Update a comment
await client.feeds.updateComment({
comment_id: "comment_123",
comment: "Updated comment",
custom: {
edited: true,
},
});
Removing Comments
try await feed.removeComment(
commentId: "comment_123"
)
client.deleteComment({
comment_id: "comment_123",
});
client.feeds.deleteComment({
comment_id: "comment_123",
});
Reading Comments
You’ll also want to show/return these comments. The most important is when reading the feed.
try await feed.getOrCreate()
print(feed.state.activities[0].comments)
// or
let activity = client.activity(
for: "activity_123",
in: FeedId(group: "user", id: "john")
)
try await activity.get()
print(activity.state.comments)
await feed.getOrCreate();
const activity = feed.state.getLatestValue().activities?.[0]!;
// Supported values for sort: first, last, top, controversial, best
// Loads the first page
await feed.loadNextPageComments(activity, {sort: 'best'});
const comments = feed.state.getLatestValue()?.comments_by_entity_id[activityId]
// Loads the next page
await feed.loadNextPageComments(activity, {sort: 'best'});
const response = feed.getOrCreate({
user_id: "<user id>",
});
console.log(response.activities[0].comments);
Querying Comments
You can also query the comments so you can show all comments for a given activity or user:
// All comments for an activity
let activityCommentList = client.commentList(
for: .init(
filter: [.activityIds: ["activity_123"]]
)
)
let activityComments = try await activityCommentList.get()
// All replies to a parent comment
let replyList = client.commentList(
for: .init(
filter: [.parentIds: ["comment_456"]]
)
)
let replies = try await replyList.get()
// All comments by a certain user
let userCommentList = client.commentList(
for: .init(
filter: [.userIds: ["john"]],
limit: 100
)
)
let userComments = try await userCommentList.get()
// Search in comment texts
await client.feeds.queryComments({
filter: { comment_text: { $q: "oat" } },
});
// All comments for an activity
await client.feeds.queryComments({
filter: { object_id: "activity_123", object_type: "activity" },
});
// Replies to a parent acitivity
await client.feeds.queryComments({
filter: { parent_id: "<parent id>" },
});
await client.feeds.queryComments({
filter: {
user_id: jane.id,
},
limit: 20,
});
// Search in comment texts
await client.feeds.queryComments({
filter: { comment_text: { $q: "oat" } },
});
// All comments for an activity
await client.feeds.queryComments({
filter: { object_id: "activity_123", object_type: "activity" },
});
// Replies to a parent acitivity
await client.feeds.queryComments({
filter: { parent_id: "<parent id>" },
});
await client.feeds.queryComments({
filter: {
user_id: jane.id,
},
limit: 20,
});
Comment Reactions
// Add a reaction to a comment
try await feed.addCommentReaction(
commentId: "comment_123",
request: .init(type: "like")
)
// Remove a reaction from a comment
try await feed.removeCommentReaction(
commentId: "comment_123",
type: "like"
)
// Add a reaction to a comment
const response = await client.addCommentReaction({
comment_id: "comment_123",
type: "like",
});
await client.removeCommentReaction({
comment_id: "comment_123",
type: "like",
});
// Add a reaction to a comment
const response = await client.feeds.addCommentReaction({
comment_id: "comment_123",
type: "like",
user_id: "<user id>",
});
await client.feeds.removeCommentReaction({
comment_id: "comment_123",
type: "like",
user_id: "<user id>",
});
Comment Threading
let comments = try await feed.getComments(
objectId: "activity_123",
objectType: "activity",
// Depth of the threaded comments
depth: 3,
limit: 20,
)
// Get replies of a specific parent comment
let replies = try await feed.getCommentReplies(
commentId: "parent_123"
)
await feed.getOrCreate();
const activity = feed.state.getLatestValue().activities?.[0]!;
await feed.loadNextPageComments(activity, { depth: 3, limit: 20 });
const comments = feed.state.getLatestValue()?.comments_by_entity_id[activityId]
const parentComment = comments[0];
const firstPageOfReplies = feed.state.getLatestValue()?.comments_by_entity_id[parentComment.id]
// Load next page of replies (or first, if replies aren't yet initialized)
feed.loadNextPageCommentReplies(parentComment);
const response = client.feeds.getComments({
object_id: 'activity_123,
object_type: 'activity',
// Depth of the threaded comments
depth: 3,
limit: 20,
});
// Get replies of a specific parent comment
const response = client.feeds.getCommentReplies({
comment_id: '<parent id>',
});