How to like a post, on behalf of someone, in SharePoint 2013 social

In enterprise social scenarios, you will have use cases where you need to like, or unlike, a SharePoint feed social post on behalf on someone. That can happen, for example, as a integral part of some process, or in various other scenarios.

This can not be achieved through SharePoint Client Object Model, but it can be very much achieved with the Server Object model. Here’s the code:

using (SPSite site = new SPSite(siteName))
{
    SPServiceContext context = SPServiceContext.GetContext(site);

    UserProfileManager profileManager = new UserProfileManager(context);

    if (profileManager.UserExists(username))
    {
        UserProfile userProfile = profileManager.GetUserProfile(username);

        //get the Social Feed Manager of the selected user
        SPSocialFeedManager feedManager = new SPSocialFeedManager(userProfile, context);

        //get the likes for a specific post
        SPSocialActor[] likers = feedManager.GetAllLikers(postid);

        if (likers.Where(co => co.AccountName == userProfile.AccountName).FirstOrDefault() == null)
        {
            feedManager.LikePost(postid);
        }
        else
        {
            feedManager.UnlikePost(postid);
        }
    }
}