Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Chatter Gallery
<apex:page showHeader="true" sidebar="true" title="Chatter Gallery" controller="ChatterGalleryPageController">
<apex:includeScript value="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"/>
<apex:includeScript value="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"/>
<apex:stylesheet value="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css"/>
<apex:includeScript value="{!URLFOR($Resource.Galleria,'galleria/galleria-1.3.5.min.js')}"/>
<style>
.galleria{ width: 700px; height: 400px; background: #000 }
</style>
<apex:sectionHeader title="View Chatter Images" subtitle="Chatter Gallery" />
<div class="galleria">
<apex:repeat value="{!images}" var="img">
<img src="{!img.image.downloadUrl}" data-title="{!img.item.Id}"/>
</apex:repeat>
</div>
<div id="dialog" title="Like Info">
</div>
<script>
Galleria.loadTheme('{!URLFOR($Resource.Galleria,'galleria/themes/classic/galleria.classic.min.js')}');
Galleria.run('.galleria', {
extend: function(options) {
this.bind('image', function(e) {
$(e.imageTarget).click(this.proxy(function() {
imageClicked(e);
}));
});
}
});
function imageClicked(e){
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.ChatterGalleryPageController.getLikesForImage}',
e.galleriaData.title,
renderDialog
);
}
function renderDialog(result, event){
var dialog = $('#dialog');
dialog.empty();
setLikeCount(dialog, result);
setWhoLiked(dialog, result);
dialog.dialog();
}
function setLikeCount(dialog, result){
if(result.length == 0){
dialog.append('<p><strong>No likes:</strong></p>');
}else if(result.length == 1){
dialog.append('<p><strong>1 like:</strong></p>');
}else{
dialog.append('<p><strong>' + result.length + ' likes:</strong></p>');
}
}
function setWhoLiked(dialog, result){
if(result.length == 0){
dialog.append('<p>Sorry! No one liked this yet!</p>');
}else{
for (i=0; i<result.length; i++){
dialog.append('<p>' + result[i].user.name + ' liked this</p>');
}
}
}
</script>
</apex:page>
public class ChatterGalleryPageController{
public transient List<ChatterImage> images;
public ChatterGalleryPageController(){
instantiateImages();
ConnectApi.FeedItemPage feedPage = getFirstFeedItemPageOfInternalPosts();
generateImages(feedPage);
}
private void instantiateImages(){
images = new List<ChatterImage>();
}
private ConnectApi.FeedItemPage getFirstFeedItemPageOfInternalPosts(){
return ConnectApi.ChatterFeeds.getFeedItemsFromFeed('internal', ConnectApi.FeedType.Files, UserInfo.getUserId());
}
private void generateImages(ConnectApi.FeedItemPage feedPage){
for(ConnectApi.FeedItem item:feedPage.items){
if(doesFeedItemContainContentAttachment(item)){
ifAttachmentIsImageAddToList(item);
}
}
}
private Boolean doesFeedItemContainContentAttachment(ConnectApi.FeedItem item){
return item.attachment != null && item.attachment instanceOf ConnectApi.ContentAttachment;
}
private void ifAttachmentIsImageAddToList(ConnectApi.FeedItem item){
ConnectApi.ContentAttachment image = (ConnectApi.ContentAttachment)item.attachment;
if(image.mimeType.contains('image')){
images.add(new ChatterImage(item, image));
}
}
public List<ChatterImage> getImages(){
return images;
}
@RemoteAction
public static List<ConnectApi.ChatterLike> getLikesForImage(String feedItemId){
ConnectApi.ChatterLikePage likePage = ConnectApi.ChatterFeeds.getLikesForFeedItem('internal', feedItemId);
return likePage.likes;
}
public class ChatterImage{
public transient ConnectApi.FeedItem item;
public transient ConnectApi.ContentAttachment image;
public ChatterImage(ConnectApi.FeedItem item, ConnectApi.ContentAttachment image){
this.item = item;
this.image = image;
}
public ConnectApi.FeedItem getItem(){
return item;
}
public ConnectApi.ContentAttachment getImage(){
return image;
}
}
}
@isTest
public class ChatterPageControllerTest{
static testMethod void testEmptyFeed(){
instantiateMockFeedItems(false);
Test.startTest();
ChatterGalleryPageController controller = new ChatterGalleryPageController();
Test.stopTest();
System.assertEquals(0, controller.getImages().size());
}
static testMethod void testFeedWithItems(){
instantiateMockFeedItems(true);
Test.startTest();
ChatterGalleryPageController controller = new ChatterGalleryPageController();
Test.stopTest();
System.assertEquals(3, controller.getImages().size());
}
@IsTest(SeeAllData=true)
static void testGetLikesForImage(){
FeedItem item = generateFeedItemWithLikes();
Test.startTest();
List<ConnectApi.ChatterLike> chatterLikes = ChatterGalleryPageController.getLikesForImage(item.Id);
Test.stopTest();
System.assertEquals(1, chatterLikes.size());
}
static testMethod void testGetCallsFromChatterImage(){
ConnectApi.FeedItem mockFeedItem = generateMockFeedItem(true, true);
ConnectApi.ContentAttachment mockContentAttachment = (ConnectApi.ContentAttachment)mockFeedItem.attachment;
Test.startTest();
ChatterGalleryPageController.ChatterImage chatterImage = new ChatterGalleryPageController.ChatterImage(mockFeedItem, mockContentAttachment);
Test.stopTest();
System.assertEquals(mockFeedItem, chatterImage.getItem());
System.assertEquals(mockContentAttachment, chatterImage.getImage());
}
private static void instantiateMockFeedItems(Boolean hasFeedItems){
ConnectApi.FeedItemPage mockFeedItemPage = generateMockFeedItemPage(hasFeedItems);
ConnectApi.ChatterFeeds.setTestGetFeedItemsFromFeed('internal', ConnectApi.FeedType.Files, UserInfo.getUserId(), mockFeedItemPage);
}
private static ConnectApi.FeedItemPage generateMockFeedItemPage(Boolean addFeedItems){
ConnectApi.FeedItemPage mockFeedItemPage = new ConnectApi.FeedItemPage();
mockFeedItemPage.items = new List<ConnectApi.FeedItem>();
if(addFeedItems){
mockFeedItemPage.items = generateMockFeedItems();
}
return mockFeedItemPage;
}
private static List<ConnectApi.FeedItem> generateMockFeedItems(){
List<ConnectApi.FeedItem> mockItems = new List<ConnectApi.FeedItem>();
mockItems.add(generateMockFeedItem(true, true));
mockItems.add(generateMockFeedItem(true, false));
mockItems.add(generateMockFeedItem(false, false));
mockItems.add(generateMockFeedItem(true, true));
mockItems.add(generateMockFeedItem(true, true));
return mockItems;
}
private static ConnectApi.FeedItem generateMockFeedItem(Boolean hasAttachment, Boolean isImageType){
ConnectApi.FeedItem mockFeedItem = new ConnectApi.FeedItem();
if(hasAttachment){
mockFeedItem.attachment = generateMockContentAttachment(isImageType);
}
return mockFeedItem;
}
private static ConnectApi.ContentAttachment generateMockContentAttachment(Boolean isImageType){
ConnectApi.ContentAttachment mockContentAttachment = new ConnectApi.ContentAttachment();
if(isImageType){
mockContentAttachment.mimeType = 'image';
}else{
mockContentAttachment.mimeType = 'pdf';
}
return mockContentAttachment;
}
private static FeedItem generateFeedItemWithLikes(){
FeedItem item = new FeedItem();
item.Body = 'Test';
item.ParentId = UserInfo.getUserId();
insert item;
generateFeedLike(item);
return item;
}
private static void generateFeedLike(FeedItem item){
FeedLike fdLike = new FeedLike();
fdLike.FeedItemId = item.Id;
insert fdLike;
}
}
Owner

jessealtman commented Apr 15, 2014

This code is from my blog article Chatter Image Gallery using ConnectApi

Owner

jessealtman commented Apr 19, 2014

This code was changed with another blog article Extending Chatter Image Gallery using ConnectApi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment