Skip to content

Commit

Permalink
Updates that fix chatter posts
Browse files Browse the repository at this point in the history
  • Loading branch information
wadewegner committed Mar 8, 2018
1 parent d448cdc commit cf2c69c
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 85 deletions.
5 changes: 3 additions & 2 deletions force-app/main/default/aura/SearchGIPHY/SearchGIPHY.cmp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<aura:attribute name="selectedGif" type="String" default="http://placehold.it/480x276" />
<aura:attribute name="selectedGifWidth" type="String" default="480" />
<aura:attribute name="selectedGifHeight" type="String" default="360" />
<aura:attribute name="chatterText" type="String" />

<aura:handler name="init" value="{!this}" action="{!c.doInit}" />

Expand All @@ -31,13 +32,13 @@
</div>
<br />
<p>
<lightning:textarea aura:id="chatterText" label="Anything you want to say to go with this GIF?" value="" />
<lightning:textarea aura:id="chatterText" label="Anything you want to say to go with this GIF?" value="{!v.chatterText}" />
</p>

</div>
<footer class="slds-modal__footer">
<button class="slds-button slds-button_neutral" onclick="{!c.closeModal}">Cancel</button>
<button class="slds-button slds-button_brand">Post</button>
<button class="slds-button slds-button_brand" onclick="{!c.postToChatter}">Post</button>
</footer>
</div>
</section>
Expand Down
50 changes: 37 additions & 13 deletions force-app/main/default/aura/SearchGIPHY/SearchGIPHYController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,7 @@
doInit: function (component, event, helper) {
console.log('doInit called successfully');

var action = component.get("c.getChatterGroups");
$A.enqueueAction(action);

console.log('about to set callback');

action.setCallback(this, function (response) {
console.log('i am back');
// var groups = JSON.parse(response.getReturnValue());
var output = response.getReturnValue();
// console.log('groups');
console.log(output);
});

},

keyCheck: function (component, event, helper) {
Expand Down Expand Up @@ -77,12 +66,47 @@

component.set("v.selectedGifWidth", width);
component.set("v.selectedGifHeight", height);
component.set("v.selectedGif", "https://media0.giphy.com/media/" + id + "/giphy.mp4");
component.set("v.selectedGif", "https://media0.giphy.com/media/" + id + "/giphy.gif");

},

closeModal: function (component) {
component.set("v.showModal", false);
},

postToChatter: function (component, event, helper) {

var chatterText = component.get("v.chatterText");
var imageUrl = component.get("v.selectedGif");

console.log('chatterText: ' + chatterText);

var action = component.get("c.getChatterGroups");

action.setParams({
imageUrl : imageUrl,
chatterText : chatterText
});

$A.enqueueAction(action);

console.log('about to set callback');

action.setCallback(this, function (response) {
console.log('i am back');

var feedItemId = response.getReturnValue();
console.log('feedItemId');
console.log(feedItemId);

var urlEvent = $A.get("e.force:navigateToURL");
urlEvent.setParams({
"url": "one/one.app#/sObject/"+ feedItemId + "/view"
});

urlEvent.fire();

});
},

})
115 changes: 47 additions & 68 deletions force-app/main/default/classes/ChatterHelper.cls
Original file line number Diff line number Diff line change
@@ -1,90 +1,69 @@
public with sharing class ChatterHelper {

@AuraEnabled
public static String getChatterGroups() {
public static String getChatterGroups(String imageUrl, String chatterText) {

// System.debug('getChatterGroups');
System.debug('getChatterGroups');

// ConnectApi.ContentHubRepositoryCollection repositoryCollection = ConnectApi.ContentHub.getRepositories();

// System.debug('repository');

// for(ConnectApi.ContentHubRepository repository : repositoryCollection.repositories){

// System.debug(repository);

// }





// String userId = UserInfo.getUserId();
// System.debug('userId: ' + userId);
// String imageUrl = 'https://media0.giphy.com/media/xTiTncVep2khPGhK1i/giphy.gif';
String fileName = 'me.gif';
String body = chatterText;

// String communityId = null;
// String imageId = '069D00000001INA'; // need to replace
// // String mentionedUserId = '005D0000001QNpr';
// String targetUserOrGroupOrRecordId = 'me'; // is this right?

// ConnectApi.FeedItemInput input = new ConnectApi.FeedItemInput();
// input.subjectId = targetUserOrGroupOrRecordId;
// input.feedElementType = ConnectApi.FeedElementType.FeedItem;
Blob imageData = getImageData(imageUrl);

// ConnectApi.MessageBodyInput messageInput = new ConnectApi.MessageBodyInput();
// ConnectApi.TextSegmentInput textSegment;
// ConnectApi.MentionSegmentInput mentionSegment;
// ConnectApi.MarkupBeginSegmentInput markupBeginSegment;
// ConnectApi.MarkupEndSegmentInput markupEndSegment;
// ConnectApi.InlineImageSegmentInput inlineImageSegment;
ID fileId = saveFile(fileName, imageData);
ID postParentId = UserInfo.getUserId();

// System.debug('here 1');
FeedItem post = new FeedItem(
ParentId = postParentId,
Body = body,
IsRichText = true
);

// messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
insert post;

// markupBeginSegment = new ConnectApi.MarkupBeginSegmentInput();
// markupBeginSegment.markupType = ConnectApi.MarkupType.Bold;
// messageInput.messageSegments.add(markupBeginSegment);
String feedItemId = post.Id;

// System.debug('here 2');
// Add image(s) to the chatter post
// Requires 'Allow users to edit posts and comments' in Setup | Chatter | Chatter Settings
// because adding the feed attachments edits the original post.
// http://salesforce.stackexchange.com/questions/156588/feedattachment-invalid-operation-cannot-create-update-or-delete-feed-attachme
List<FeedAttachment> feedAttachments = new List<FeedAttachment>();
feedAttachments.add( new FeedAttachment(
feedEntityId = feedItemId,
recordId = fileId,
type = 'Content'
));

// textSegment = new ConnectApi.TextSegmentInput();
// textSegment.text = 'Hello ';
// messageInput.messageSegments.add(textSegment);
insert feedAttachments;

// System.debug('here 3');

// // mentionSegment = new ConnectApi.MentionSegmentInput();
// // mentionSegment.id = mentionedUserId;
// // messageInput.messageSegments.add(mentionSegment);

// System.debug('here 4');

// textSegment = new ConnectApi.TextSegmentInput();
// textSegment.text = '!';
// messageInput.messageSegments.add(textSegment);

// System.debug('here 5');

// markupEndSegment = new ConnectApi.MarkupEndSegmentInput();
// markupEndSegment.markupType = ConnectApi.MarkupType.Bold;
// messageInput.messageSegments.add(markupEndSegment);
return feedItemId;
}

// System.debug('here 6');
private static ID saveFile(String fileNameWithExt, Blob fileData) {

// inlineImageSegment = new ConnectApi.InlineImageSegmentInput();
// inlineImageSegment.altText = 'image one';
// inlineImageSegment.fileId = imageId;
// messageInput.messageSegments.add(inlineImageSegment);
ContentVersion file = new ContentVersion(
versionData = fileData,
title = fileNameWithExt,
pathOnClient = '/' + fileNameWithExt
);

// input.body = messageInput;
insert file;

// System.debug('here 7');
return file.Id;
}

// ConnectApi.ChatterFeeds.postFeedElement(communityId, input, null);
private static Blob getImageData(String url) {

// System.debug('here 8');
HttpRequest req = new HttpRequest();
req.setEndpoint( url );
req.setHeader('accept', 'image/*');

req.setMethod('GET');
req.setCompressed( false );

return 'finished';
HttpResponse res = new Http().send( req );
return res.getBodyAsBlob();
}

}
4 changes: 2 additions & 2 deletions force-app/main/default/classes/ChatterHelper.cls-meta.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="urn:metadata.tooling.soap.sforce.com" fqn="ChatterHelper">
<apiVersion>35.0</apiVersion>
<apiVersion>42.0</apiVersion>
<status>Active</status>
</ApexClass>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<RemoteSiteSetting xmlns="http://soap.sforce.com/2006/04/metadata">
<disableProtocolSecurity>false</disableProtocolSecurity>
<isActive>true</isActive>
<url>https://media0.giphy.com</url>
</RemoteSiteSetting>

0 comments on commit cf2c69c

Please sign in to comment.