Last active
December 18, 2015 23:59
-
-
Save joeartsea/5865738 to your computer and use it in GitHub Desktop.
Working Chatter in Apex. http://deferloader.blog.uhuru.co.jp/?p=3896
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="ChatterInApexController"> | |
<apex:form > | |
<apex:selectList value="{!communityId}" size="1"> | |
<apex:selectOptions value="{!communityOptions}"/> | |
<apex:actionSupport event="onchange" rerender="feed"/> | |
</apex:selectList> | |
<apex:commandButton value="Post" action="{!postFeed}" /> | |
</apex:form> | |
<apex:outputPanel id="feed"> | |
<apex:repeat value="{!newsFeedItems}" var="feedItem"> | |
<div> | |
<apex:image style="margin:4px" width="25" url="{!feedItem.photoUrl}"/><br/> | |
User: <b>{!feedItem.actor.name}</b><br/> | |
Text: <b>{!feedItem.body.text}</b><br/> | |
Likes: <b>{!feedItem.likes.total} [ | |
<apex:repeat value="{!feedItem.likes.likes}" var="like"> | |
{!like.user.lastName} {!like.user.FirstName}, | |
</apex:repeat> | |
] </b><br/> | |
<apex:outputPanel > | |
<apex:repeat value="{!feedItem.comments.comments}" var="comment"> | |
<div style="margin-left:25px"> | |
<apex:image style="margin:4px" width="25" url="{!comment.user.photo.smallPhotoUrl}"/><br/> | |
User: <b>{!comment.user.name}</b><br/> | |
Text: <b>{!comment.body.text}</b><br/> | |
Likes: <b>{!comment.likes.total} [ | |
<apex:repeat value="{!comment.likes.likes}" var="like"> | |
{!like.user.lastName} {!like.user.FirstName}, | |
</apex:repeat> | |
]</b> | |
</div> | |
</apex:repeat> | |
</apex:outputPanel> | |
</div> | |
</apex:repeat> | |
</apex:outputPanel> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ChatterInApexController{ | |
public String communityId { | |
get; | |
set { | |
if (value == '') { | |
communityId = null; | |
} else { | |
communityId = value; | |
} | |
} | |
} | |
public static List<SelectOption> getCommunityOptions() { | |
List<SelectOption> options = new List<SelectOption>(); | |
options.add(new SelectOption('', 'Internal')); | |
ConnectApi.CommunityPage communityPage = ConnectApi.Communities.getCommunities(ConnectApi.CommunityStatus.Live); | |
for (ConnectApi.Community community : communityPage.communities) { | |
options.add(new SelectOption(community.id, community.name)); | |
} | |
return options; | |
} | |
public List<ConnectApi.FeedItem> getNewsFeedItems() { | |
return ConnectApi.ChatterFeeds.getFeedItemsFromFeed(communityId, ConnectApi.FeedType.News, 'me').items; | |
} | |
public PageReference postFeed() { | |
ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput(); | |
messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>(); | |
ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput(); | |
ConnectApi.MentionSegmentInput mentionSegment = new ConnectApi.MentionSegmentInput(); | |
ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput(); | |
mentionSegment.id = 'XXXXXXXXXXXXXXX'; | |
messageInput.messageSegments.add(mentionSegment); | |
textSegment.text = 'test'; | |
messageInput.messageSegments.add(textSegment); | |
input.body = messageInput; | |
ConnectApi.ChatterFeeds.postFeedItem(communityId, ConnectApi.FeedType.News, 'me', Input, null); | |
return null; | |
} | |
public PageReference choose() { | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment