Feed

Feed is a great way for users to get relevant updates, communicate and express themselves via algorithmically ranked content.

Each feed consists of a collection of posts. Users will be able to create posts on any group that they are a member of and any user that they can find.

There is also a global feed which is an aggregate of all the posts in a user's feeds.

Feed management methods are all contained in a EkoFeedRepository class.

let feedRepository = EkoFeedRepository(client: client)

Query User Feed

Querying for feed fetches your the posts in your own feed or other user's feed. You can also sort the posts by lastCreated or firstCreated timestamp. EkoUserFeedSortOption enum provides both sort option which you can provide as argument while fetching feed.

EkoFeedRepository class exposes two convenient methods getMyFeedSortedBy(_:) and getUserFeed(_:) to query for your own feed and other user's feed respectively. getUserFeed(_:) method requires an additional parameter userId.

let feedToken: EkoNotificationToken?
...
let feedCollection = feedRepository.getUserFeed(userId, sortBy: .lastUpdated, includeDeleted: false)
feedToken = feedCollection.observe({ (collection, _, error) in
// React to changes here
})

There is also a quick easy method to get your own feed:

let feedCollection = feedRepository.getMyFeedSorted(by: .lastUpdated, includeDeleted: false)
feedToken = feedCollection.observe({ (collection, _, error) in
// React to changes here
})

Query Group Feed

You can get any group's feed by calling the method below with the communityId:

let feedCollection = feedRepository.getCommunityFeed(withCommunityId: "community-id", sortBy: .lastCreated, includeDeleted: false)
feedToken = feedCollection.observe({(collection, change, error) in
// Observer is called once object is received
})

Query Global Feed

You can retreive your global feed by calling the following method:

let feedCollection = feedRepo.getGlobalFeed()
feedToken = feedCollection.observe({(collection, change, error) in
// Observer is called once object is received
})

Posts

A feed is made up of a collection of posts. Users will be able to generate different types of posts as well as be able to react and comment on posts. SDK provides different builder classes to create each type of post. First you need to build the post using one of the builder classes. Then use the instance of EkoFeedRepository to actually create the post. createPost method requires instance of builder, targetId and targetType. TargetType is an enum with two cases. user and community.

If you want to create the post on particular user's feed, provide id of that user for targetId parameter. If you want to create a post on your own feed, set targetId to nil. If you want to post on particular community, then targetType would be community.

Create a Text Post

Use EkoTextPostBuilder to create text post.

// Build your post
let postBuilder = EkoTextPostBuilder()
postBuilder.setText("ABC")
...
feedRepository.createPost(postBuilder, targetId: nil, targetType: .user, completion: { (success, error) in
// React to changes here...
})

Create an Image Post

In order to create image post, images must be uploaded first. SDK provides a convenient class EkoFileRepository to handle upload and download of images. The repository contains uploadImages method which takes array of UIImage and provides you with array of EkoImageData for successful upload and array of UIImage for failed uploads.

A post can have maximum 10 images.

let fileRepository = EkoFileRepository(client: client)
fileRepository.uploadImage(image, progress: nil, completion: { (imageData, error) in
// ... create image post using imageData
})

Now to create the image post, we build the post first using EkoImagePostBuilder. Then use the same createPost method in EkoFeedRepository to create image post.

// Build your post
let postBuilder = EkoImagePostBuilder()
postBuilder.setText("ABC")
postBuilder.setImageData([imageData])
...
feedRepository.createPost(postBuilder, targetId: nil, targetType: .user, completion: { (success, error) in
// React to changes here...
})

Create a File Post

File post follows the same order as image post. First you upload the files using EkoFileRepository, build the post using EkoFilePostBuilder and then finally create the post. To upload the file, EkoFileRepository provides uploadFile method which takes an instance of UploadableFile. It provides you with EkoFileData for successful upload and Error for failed upload. Its okay to call this method inside a loop if you want to upload multiple files.

Note: A post can consist of either a list of images or a list of files but not both.

let file1 = UploadableFile(fileData: #Data#, fileName: "my_file.pdf")
...
let fileRepository = EkoFileRepository(client: client)
fileRepository.uploadFile(file1, progress: nil, completion: { (fileData, error) in
// ... create file post using successful uploads
})

Now to create file post, we build the post using EkoFilePostBuilder.

// Build your post
let postBuilder = EkoFilePostBuilder()
postBuilder.setText("ABC")
postBuilder.setFileData([fileData])
...
feedRepository?.createPost(postBuilder, targetId: nil, targetType: .user, completion: { (success, error) in
// React to changes here...
})

Get Post

You can also query for each individual post based on its postId. EkoFeedRepository provides a convenient method getPostForPostId(_:) which returns EkoObject<EkoPost>.

let postToken: EkoNotificationToken?
...
let postObject = feedRepository.getPostForPostId("post-id")
postToken = postObject.observe({ (object, error) in
// Observer is called once object is received
})

Delete Post

EkoFeedRepository provides another convenient method to delete the post. To delete the post, you just need the post id. If you are deleting child post, you need to pass the parent id too. If its the parent post, just pass nil.

feedRepository?.deletePost(withPostId: "post-id", parentId: nil, completion: { success, error in
// React to changes here
})

Note: Only the post owner or an admin will be able to delete a post.

Accessing Post Information

Each post is represented by EkoPost instance. Each instance of EkoPost holds several informations such as data, reactions, comments, metadata, child posts etc. For text based post, you can access the actual data for the post through data property.

Post with images or files follows Parent - Child relationship. Each images or files uploaded will be a separate child post. Any text that you set while creating image/file post will act as a Parent post. Parent post contains childrenPosts property which gives you array of EkoPost. You can access data for child post through same data property for child post. Alternatively, you can also access more details about uploaded files images through getFileInfo() or getImageInfo() method.

Reactions

EkoPost object provides enough information about the reactions for that post. Use myReactions property to get list of your reactions to the post, reactions property to get list of all reactions to the post and reactionsCount provides the total count of reactions on that post.

To add or remove reaction, please refer to documentation for Reactions. Reactions Docs.

Comments

EkoPost object provides few latest comments through latestComments property. To fetch all comments for the post, please refer to documentation for Comments. Comments Docs.

Flag Post

You can flag a post as inappropriate using EkoPostFlaggerinstance.

flagger?.flagPost(completion: { (isSuccess, error) in
// Do something
})

Check for isFlaggedByMe

flagger?.isPostFlaggedByMe(completion: { (isFlagged) in
// Do something
})

Unflag Post

flagger?.unflagPost(completion: { (isSuccess, error) in
// Do something
})