Posts

Here's an overview of posts and how you can get started with integrating them into your applications

A feed is made up of a collection of posts. Users will be able to generate different types of posts as well as to react and comment on posts. SDK provides different builder classes to create each type of post. First, you need to build the post by 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, which is user and community.

If you want to create a post on a particular user's feed, you can provide the 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 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 can build the post first by 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 file 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({ (liveObject, error) in
    
    // Observer block, Do something here
    
})

The observe block can get called multiple times depending upon where the post is being fetched from. We can check the dataStatus property for liveObject to see if the post is being fetched from server or locally. Please refer to Accessing Post Information section.

Delete Post

EkoFeedRepository provides another convenient method to delete 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 it is 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 information such as data, reactions, comments, metadata, child posts etc. For text based post, you can access to the actual data for the post through data property.

let postToken: EkoNotificationToken?

...
postToken = postObject.observe({ (liveObject, error) in
    
    // Here we are interested in data fetched from server only
    guard liveObject.dataStatus == .fresh else { return }
    
    // Then we get the actual post data from liveObject and
    // access its properties
    guard let post = liveObject.object else { return }
    
    let postId = post.postId; // postId
    let postData = post.data; // post data
    let postCommentCount = post.commentsCount; // No of commments in this psot
    ...
})

Post with images or files follow 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 and images through getFileInfo() or getImageInfo() method.

Let's consider our post contains some text & 1 image. This means parent post would be a text post and its child post will be an image post. Here is how we can access it.

let postToken: EkoNotificationToken?

...
postToken = postObject.observe({ (liveObject, error) in
    guard let post = liveObject.object else { return }
        
    // We check if this post has any children i.e file/image
    if let childPostArr = post.childrenPosts, children.count > 0 {
    
        for childPost in childPostArr {
            
            if childPost.dataType == "image" {
                // This post is an image post. So we get image information
                let imageInfo = childPost.getImageInfo() 
                
                ...
            }
            
            if childPost.dataType == "file" {
                // This post is an file post. So we get file information
                let imageInfo = childPost.getFileInfo() 
                
                ...
            }
        }
    }
    
    // Since parent post contains text, we can access it as 
    let postData = post.data; // post data
    ...
})

Add Reaction

You can add any number of reactions to the given post. EkoReactionRepository provides addReaction method which accepts reaction name. Reaction name is case sensitive i.e "like" & "Like" are two different reactions.

reactionRepository.addReaction("reaction-name", referenceId: "reference-id", referenceType: .reference-type) { (isSuccess, error) in
    // React to completion handler here
}

Remove Reaction

You can remove any reactions added to the post. EkoReactionRepository provides removeReaction method which removes reaction name. Reaction name is case sensitive in this case too.

reactionRepository.removeReaction("reaction-name", referenceId: "reference-id", referenceType: .reference-type) { (isSuccess, error) in
    // React to completion handler here
}

Flag a 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
})

Comments

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

Last updated