Load Activity Feed content

Activity Feed consists of posts and announcements. Each post and announcement have a list of comments and likes.

In the following guide, we’ll show how to query all sorts of Activity Feed content.

Save time with prebuilt Activity Feed UI

Activity Feed UI Features
Let your users post, comment and like on your Activity Feed with one line of code with prebuilt UI. Check Activity Feed UI guide for more details.

Prerequisite

Load posts

To get a list of posts on the Activity Feed, you have to create a query and call API to get the results:

1
2
3
4
5
6
7
8
9
val query: ActivitiesQuery = ... // Create query
val pagingQuery = PagingQuery(query)

Communities.getActivities(pagingQuery, { result: PagingResult<GetSocialActivity> ->
    val activities = result.entries
    Log.d("Communities", "Activities: $activities")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get activities: $error")
})
1
2
3
4
5
6
7
8
let query: ActivitiesQuery = ... // Create query
let pagingQuery = ActivitiesPagingQuery(query)

Communities.activities(pagingQuery, success: { result in
    print("Activities: \(result.activities)")
}, failure: { error in
    print("Failed to get activities: \(error)")
})
1
2
3
4
5
6
7
8
9
ActivitiesQuery query = ... // Create query
var pagingQuery = new PagingQuery<ActivitiesQuery>(query);
Communities.GetActivities(pagingQuery,
    (result) => {
        Debug.Log("Activities: " + result.Entries);
    },
    (error) => {
        Debug.Log("Failed to get activities: " + error);
    });

This method uses the PagingQuery concept that is used across our SDK. Read more about this.

Create a query

To create query you have to specify for which feed you want to load posts.

Activities in Topics Feed

To get all activities in topic "cats":

1
val query = ActivitiesQuery.activitiesInTopic("cats")
1
let query = ActivitiesQuery.inTopic("cats")
1
var query = ActivitiesQuery.ActivitiesInTopic("cats");

Activities from All Topics

To get activities from all topics:

1
val query = ActivitiesQuery.inAllTopics()
1
let query = ActivitiesQuery.inAllTopics()
1
var query = ActivitiesQuery.InAllTopics();

Comments to Post

To get all comments to a certain activity with id activityId:

1
val query = ActivitiesQuery.commentsToActivity(activityId)
1
let query = ActivitiesQuery.commentsToActivity("activityId")
1
var query = ActivitiesQuery.CommentsToActivity("activityId");

User Feed

To get the feed of the user with id 42:

1
val query = ActivitiesQuery.feedOf(UserId.create("42"));
1
let query = ActivitiesQuery.feedOf(UserId.create("42"))
1
var query = ActivitiesQuery.FeedOf(UserId.Create("42"));

Activities from All Feeds

To get activities from all existing feeds, including topics and user feeds:

1
val query = ActivitiesQuery.everywhere()
1
let query = ActivitiesQuery.everywhere()
1
var query = ActivitiesQuery.Everywhere();

Filter Activities by Tag

To get activities from all feeds with a tag "cat":

1
val query = ActivitiesQuery.everywhere().withTag("cat")
1
let query = ActivitiesQuery.everywhere().withTag("cat")
1
var query = ActivitiesQuery.Everywhere().WithTag("cat");

Filter Activities by Author

To get activities from all feeds created by a user with id 42:

1
val query = ActivitiesQuery.everywhere().byUser(UserId.create("42"))
1
let query = ActivitiesQuery.everywhere().byUser(UserId.create("42"))
1
var query = ActivitiesQuery.Everywhere().ByUser(UserId.Create("42"));

Load announcements

To get a list of announcements that are active now:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// To get announcements for topic
val query = AnnouncementsQuery.topic("cats")
// Or to get announcements only posted to all feeds
val query = AnnouncementsQuery.timeline()

Communities.getAnnouncements(query, { announcements: List<GetSocialActivity> ->
    Log.d("Communities", "Announcements: $announcements")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get announcements: $error")
})
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// To get announcements for topic
let query = AnnouncementsQuery.inTopic("cats")
// Or to get announcements only posted to all feeds
let query = AnnouncementsQuery.timeline()

Communities.announcements(query, success: { announcements in
    print("Announcements: \(announcements)")
}, failure: { error in
    print("Failed to get announcements: \(error)")
})
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// To get announcements for topic
var query = AnnouncementsQuery.InTopic("cats");

// Or to get announcements only posted to all feeds
var query = AnnouncementsQuery.Timeline();
Communities.GetAnnouncements(query,
    (result) => {
        Debug.Log("Announcements: " + result);
    },
    (error) => {
        Debug.Log("Failed to get announcements: " + error);
    });

Load Reactions

For each activity post, announcement and comment you can check the reactions count, reactions of the current user and get the list of users who reacted to the activity:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
val activityPost: GetSocialActivity = ... // your activity

// to check a particular reaction, e.g. "like"
val isLikedByMe = activityPost.myReactions.contains(Reactions.LIKE)
val likesCount = activityPost.reactionsCount[Reactions.LIKE]

val query = ReactionsQuery.forActivity(activityPost.id)

val pagingQuery = PagingQuery(query)
Communities.getReactions(pagingQuery, { result: PagingResult<UserReaction> ->
    val reactions = result.entries
    Log.d("Communities", "Reactions: $reactions")
}, { error: GetSocialError ->
    Log.d("Communities", "Failed to get reactions: $error")
})
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
let activity: Activity = ... // your activity
// to check a particular reaction, e.g. "like"
let isLikedByMe = activity.myReactions.contains(Reactions.like)
let likesCount = activity.reactionsCount[Reactions.like]

let query = ReactionsQuery.forActivity(activity.id)
let pagingQuery = ReactionsPagingQuery(query)

Communities.reactions(pagingQuery, success: { result in
    print("Reactions: \(result.reactions)")
}, failure: { error in
    print("Failed to get reactions: \(error)")
})
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
Activity activity = ... // your activity
var isLikedByMe = activity.MyReactions.Contains(Reactions.Like);
var likesCount = activity.GetReactionsCount(Reactions.Like);

var query = ReactionsQuery.ForActivity(activity.Id);
var pagingQuery = new PagingQuery<ReactionsQuery>(query);
Communities.GetReactions(pagingQuery,
    (result) => {
        Debug.Log("Reactions: " + result.Entries);
    },
    (error) => {
        Debug.Log("Failed to get reactions: " + error);
    });

This method uses the PagingQuery concept that is used across our SDK. Read more about this.

Working with activity

The section below explains how you can display Activity after loading it from GetSocial API.
Learn what can you do with activity object in Android and iOS.

Post Author and Creation Date

Each post, announcement, and comment contains a set of metadata that you can use on your UI:

1
2
3
4
var activity: GetSocialActivity

val createdAt: Long = activity.createdAt // Creation date, UNIX timestamp in seconds
val author: User = activity.author
1
2
3
4
let activity: Activity = ... // your activity

let createdAt = activity.createdAt // Creation date, UNIX timestamp in seconds
let author = activity.author
1
2
3
Activity activity = ... // your activity
var createdAt = activity.CreatedAt;  // Creation date, UNIX timestamp in seconds
var author = activity.Author;

Text

Text content can contain text itself, mentions, tags, and URLs.

User ids from mentions in the text are replaced by API to user display names. If you posted text like Hello @0128309218! where @0128309218 is user id, API returns text like Hello Ben!.

Optionally you can make URLs on your UI clickable.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
val contentText: TextView = ... // Get instance of text view
val post : GetSocialActivity = ... // Instance of activity post, announcement or comment
if (post.text != null {
    val textContent = post.text
    contentText.text = textContent

    // Don't forget to make text views accessible
    contentText.contentDescription = textContent

    // Optionally, make links clickable if post was made from Dashboard
    if (post.author.isVerified) {
        Linkify.addLinks(contentText, Linkify.WEB_URLS)
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
let contentText: UITextView = ... // Get the instance of text view
let activity: Activity = ... // Instance of activity post, announcement or comment

guard let activityText = activity.text else {
    return
}
contentText.text = activityText.text

// Don't forget to make text views accessible
contentText.accessibilityValue = activityText

// Optionally, make links clickable if post was made from Dashboard
if activity.author.isVerified {
    contentText.dataDetectorTypes = .link
}
1
2
Activity activity = ... // your activity
var activityText = activity.Text;

Mentions

To highlight mentions in the text and make them clickable:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
val spannable = SpannableString(post.text)
post.mentions.forEach { mention ->
    val startIndex = mention.startIndex
    val endIndex = mention.endIndex

    spannable.setSpan(BackgroundColorSpan(Color.YELLOW), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    spannable.setSpan(ForegroundColorSpan(Color.BLUE), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

    spannable.setSpan({ widget: View ->
        // handle click
    }, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}

contentText.text = spannable
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
let mutableAttributedString = NSMutableAttributedString(string: activity.text!)
activity.mentions.forEach {
    var effectiveRange = NSRange(location: Int($0.startIndex), length: Int($0.endIndex - $0.startIndex))
    mutableAttributedString.addAttributes([
        NSAttributedString.Key.backgroundColor: UIColor.yellow,
        NSAttributedString.Key.foregroundColor: UIColor.blue
        ], range: effectiveRange)
}

contentText.attributedText = mutableAttributedString
1
2
3
4
5
Activity activity = ... // your activity
var mentions = activity.Mentions;
mentions.ForEach(mention => {
    Debug.Log("Mention: " + mention);
});

Tags

To highlight tags in the text and make them clickable, you may use code like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
val spannable = SpannableString(post.text)
val matcher = Pattern.compile("(#[\\p{L}\\d_]*\\p{L}[\\p{L}\\d_]*)").matcher(spannable)
while (matcher.find()) {
    val startIndex = mention.startIndex
    val endIndex = mention.endIndex

    spannable.setSpan(BackgroundColorSpan(Color.YELLOW), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    spannable.setSpan(ForegroundColorSpan(Color.BLUE), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

    spannable.setSpan({ widget: View ->
        // handle click
    }, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
let mutableAttributedString = NSMutableAttributedString(string: activity.text!)
let pattern = "(#[\\p{L}\\d_]*\\p{L}[\\p{L}\\d_]*)"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
regex.enumerateMatches(in: activity.text!, options: [], range: NSRange(location: 0, length: activity.text!.count), using: { result, flags, stop in
    if let effectiveRange = result?.range {
        mutableAttributedString.addAttributes([
            NSAttributedString.Key.backgroundColor: UIColor.yellow,
            NSAttributedString.Key.foregroundColor: UIColor.blue
            ], range: effectiveRange)
    }
})

contentText.attributedText = mutableAttributedString

Media attachments

Activity Post can contain images and videos. GIFs are converted to videos for optimization. All videos are encoded in MP4.
Check Media Attachments guide for more details.

You can get a list of media attachments, which is empty if you haven’t attached any media to the post. Attachments have the same order as they were posted with.

Action buttons

The code below shows how to display the button and implement a combination of custom action handling with the one provided by GetSocial.

Check GetSocial Actions guide for Android, iOS or Unity for more information.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
val button : Button = ... // An instance of button for the activity post
val actionButton = post.button
if (actionButton != null) {
    button.text = actionButton.title

    // Don't forget to make view accessible
    button.setContentDescription(actionButton.title)

    button.setOnClickListener { view ->
        val action = actionButton.action
        if ("my-custom-type".equals(action.getType())) {
            // Do the custom action handling
        } else {
            GetSocial.handle(action) // Let GetSocial handle the action
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
let actionButton: UIButton = ... // An instance of button for the activity post
if let buttonTitle = activity.button?.title {
    actionButton.setTitle(buttonTitle, for: .normal)

    // Don't forget to make view accessible
    actionButton.accessibilityValue = buttonTitle

    actionButton.tag = activities.index(of: activity)
    let actionButtonTap = UITapGestureRecognizer(target: self, action: #selector(self.didClickActionButton(_:)))
    actionButtonTap.numberOfTapsRequired = 1
    actionButton.addGestureRecognizer(actionButtonTap)
}

@objc 
func didClickActionButton(_ sender: UITapGestureRecognizer?) {
    guard let index = sender?.view?.tag else {
        return
    }
    let activity: Activity = activities[index]
    if activity.button?.action.type == "my-custom-type" {
        // Do the custom action handling
    } else {
        GetSocial.handle(activity.button?.action)
    }
}
1
2
3
4
5
6
7
8
Activity activity = ...;
if(activity.Button.Action.Type.Equals("my-custom-type"))
{
    // do the custom action handling
} else
{
    GetSocial.Handle(activity.Button.Action);
}

Source

You can check where the activity was posted by checking their source property:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
val activity: GetSocialActivity

val source = activity.source
when(source.type) {
    CommunitiesEntityType.TOPIC -> {
        // posted to topic with id `source.id`
    }
    CommunitiesEntityType.ACTIVITY -> {
        // activity is a comment to another activity with id `source.id`
    }
    CommunitiesEntityType.USER -> {
        // activity is posted into user's feed og the user with id `source.id.
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
let activity: GetSocialActivity

let source = activity.source
switch source.type {
    case .topic:
        // posted to topic with id `source.id`
    case .activity:
        // activity is a comment to another activity with id `source.id`
    case .user:
        // activity is posted into feed of the user with id `source.id`
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
var source = activity.Source;
switch(source.Type)
{
    case CommunitiesEntityType.Topic:
        // posted to topic with id `source.id`
        break;
    case CommunitiesEntityType.Activity:
        // activity is a comment posted to activity with id `source.id`
        break;
    case CommunitiesEntityType.User:
        // activity is posted to feed of user with id `source.id`
        break;
}

Next steps

Give us your feedback! Was this article helpful?

😀 🙁