Created
August 21, 2012 10:21
-
-
Save soe/3414191 to your computer and use it in GitHub Desktop.
WP2Chatter Kynetx
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
ruleset a1000x9 { | |
meta { | |
name 'WP2Chatter' | |
description << | |
WP2Chatter | |
>> | |
author "soe soe" | |
logging on | |
key wp2chatter { | |
'sf_consumer_key': '3MVG9y6x0357HlefOUZYRoC6WlirjXHuUcS_tZaaCqH8IlRtei16TZYG.ygxO9Vb76DH1E9Bt7xgC5qY.iVbm', | |
'sf_consumer_secret': '2985057897405945646', | |
'sf_redirect_uri': 'https://login.salesforce.com/services/oauth2/success', | |
'char_limit': 997 | |
} | |
} | |
dispatch { | |
domain "login.salesforce.com" | |
domain "wordpress.com" | |
} | |
global { | |
} | |
// get acess token from url hash after use has authorized via Salesforce | |
rule get_access_token_ruleset { | |
select when pageview re#\#access_token=(.*)&refresh_token=(.*)&instance_url=(.*)&id=# setting(sf_access_token, sf_refresh_token, sf_instance_url) | |
pre { | |
url = event:param('url'); | |
ok = url.match('access_token=([^&]+)'); | |
} | |
{ | |
emit << | |
console.log(sf_access_token); | |
console.log(sf_refresh_token); | |
console.log(sf_instance_url); | |
if(ok) alert('Authorization successful. You may close this window.'); | |
else alert('Authorization unsuccessful. You may close this window.'); | |
>> | |
} | |
fired { | |
// save Salesforce auth info into persistent entities | |
set ent:sf_access_token uri:unescape(sf_access_token); | |
set ent:sf_refresh_token uri:unescape(sf_refresh_token); | |
set ent:sf_instance_url uri:unescape(sf_instance_url); | |
} | |
} | |
rule do_refresh_token_ruleset { | |
select when web do_refresh_token | |
pre { | |
appRID = meta:rid(); | |
url = 'https://login.salesforce.com/services/oauth2/token?grant_type=refresh_token' | |
+'&client_id='+ keys:wp2chatter('sf_consumer_key') | |
+'&refresh_token='+ ent:sf_refresh_token; | |
// now call the REST api | |
r = http:get(url); | |
} | |
{ | |
emit << | |
console.log('do refresh token'); | |
console.log(url); | |
console.log(r); | |
if(r.status_code == 400) { // refresh token invalid | |
$K('.msg').fadeOut('fast'); | |
$K('#chatter-box-msg-auth').fadeIn('fast'); | |
$K('#chatter-box-unauth-btn').hide(); | |
} else { | |
// refresh token ok | |
// update the persistent entities with new auth info | |
// todo - test | |
// now post_to_chatter | |
var app = KOBJ.get_application(appRID); | |
app.raise_event('post_to_chatter'); | |
} | |
>>; | |
} | |
} | |
// remove auth info from the plugin | |
rule clear_access_token_ruleset { | |
select when click '#chatter-box-unauth-btn' | |
pre { } | |
{ | |
emit << | |
$K('#chatter-box-unauth-btn').hide(); | |
>> | |
} | |
fired { | |
// clear Salesforce auth info from persistent entities | |
clear ent:sf_access_token; | |
clear ent:sf_refresh_token; | |
clear ent:sf_instance_url; | |
} | |
} | |
// post to Salesforce chatter | |
rule post_to_chatter_ruleset { | |
select when click '#chatter-box-post-btn' or web post_to_chatter | |
pre { | |
// get Salesforce auth info from Kynetx's persistence entities | |
sf_access_token = ent:sf_access_token; | |
sf_refresh_token = ent:sf_refresh_token; | |
sf_instance_url = ent:sf_instance_url; | |
// get from chatter-box textarea | |
text = 'testing from wp2chatter'; | |
url = sf_instance_url + '/services/data/v23.0/chatter/feeds/news/me/feed-items'; | |
} | |
{ | |
emit << | |
console.log('post to chatter'); | |
console.log(sf_access_token); | |
console.log(url); | |
$K('.msg').fadeOut('fast'); | |
$K('#chatter-box-msg-posting').fadeIn('fast'); | |
// disable the post button | |
$K('#chatter-box-button').attr('disabled', 'disabled'); | |
>>; | |
// do http post to Salesforce REST api | |
http:post(url) | |
with params = { | |
'text': text | |
} and headers = { | |
'Authorization': 'OAuth #{sf_access_token}' | |
} and autoraise = 'post_to_chatter_response'; | |
} | |
} | |
// handle response from post_to_chatter | |
rule post_to_chatter_response_ruleset { | |
select when http post label "post_to_chatter_response" | |
pre { | |
appRID = meta:rid(); | |
content = event:param("content"); | |
status_code = event:param("status_code"); | |
} | |
{ | |
emit << | |
console.log('post to chatter response'); | |
console.log(status_code); | |
console.log(content); | |
if(status_code == 201) { | |
$K('.msg').fadeOut('fast'); | |
$K('#chatter-box-msg-posted').fadeIn('fast'); | |
$K('#chatter-box-unauth-btn').show(); | |
// enable the post button | |
$K('#chatter-box-post-btn').removeAttr('disabled'); | |
} else if(status_code == 401) { | |
if(content.indexOf('INVALID_SESSION_ID') > -1) { | |
// do refresh token | |
var app = KOBJ.get_application(appRID); | |
app.raise_event('do_refresh_token'); | |
} else { | |
// ask for authorization | |
$K('.msg').fadeOut('fast'); | |
$K('#chatter-box-msg-auth').fadeIn('fast'); | |
$K('#chatter-box-unauth-btn').hide(); | |
} | |
} else { | |
// ask for authorization | |
$K('.msg').fadeOut('fast'); | |
$K('#chatter-box-msg-auth').fadeIn('fast'); | |
$K('#chatter-box-unauth-btn').hide(); | |
} | |
>>; | |
} | |
} | |
// insert chatter button in WordPress' sharedaddy widget area | |
rule insert_chatter_button_ruleset { | |
select when pageview '.*' setting () | |
pre { | |
chatter_button = << | |
<li class="share-twitter" style="min-width: 80px;"> | |
<a rel="nofollow" class="sd-button share-icon share-chatter" href="#" title="Click to share on Chatter" id="sharing-chatter"> | |
<span style="background-image: url('http://i.imgur.com/T717k.png');">Chatter</span> | |
</a> | |
</li> | |
>>; | |
} | |
{ | |
before('li.share-end', chatter_button); | |
} | |
} | |
// insert chatter box (chatter post form) | |
rule insert_chatter_box_ruleset { | |
select when pageview '.*' setting () | |
pre { | |
appRID = meta:rid(); | |
char_limit = keys:wp2chatter('char_limit'); | |
authorization_url = 'https://login.salesforce.com/services/oauth2/authorize?response_type=token' | |
+'&client_id='+ keys:wp2chatter('sf_consumer_key') | |
+'&redirect_uri='+ keys:wp2chatter('sf_redirect_uri') | |
+'&display=touch'; | |
// chatter box html & css | |
chatter_box = << | |
<style> | |
#chatter-box { | |
width: 360px; | |
height: 240px; | |
margin-top: -120px; | |
margin-left: -180px; | |
border-radius: 6px; | |
border: 5px solid rgb(204, 204, 204); | |
border: 5px solid rgba(204, 204, 204, .5); | |
-webkit-background-clip: padding-box; /* for Safari */ | |
background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */ | |
box-shadow: 3px 3px 9px #888; | |
position: fixed; | |
z-index: 999999; | |
padding: 10px; | |
background-color: #fff; | |
top: 50%; | |
left: 50%; | |
display: none; | |
} | |
#chatter-box > h3 { | |
float: left; | |
font-size: 16px; | |
line-height: 170%: | |
} | |
#chatter-box > textarea { | |
width: 340px; | |
padding: 8px; | |
max-width: 340px; | |
font-size: 13px; | |
min-height: 90px; | |
max-height: 160px; | |
margin-top: 8px; | |
} | |
#chatter-box > button { | |
padding: 8px 16px; | |
font-size: 14px; | |
margin-right: 10px; | |
box-shadow: 1px 1px 3px #999; | |
cursor: hand; cursor: pointer; | |
} | |
#chatter-box-msges { | |
margin-top: 8px; | |
height: 54px; | |
overflow: hidden; | |
} | |
#chatter-box .msg { | |
width: 340px; | |
max-width: 340px; | |
padding: 6px; | |
font-size: 12px; | |
border-radius: 3px; | |
display: none; | |
background-color: #f6f6f6; | |
font-family: Tahoma, Arial, sans-serif; | |
} | |
#chatter-box-close-btn { | |
float: right; | |
height: 16px; | |
width: 16px; | |
border-radius: 50%; | |
font-size: 14px; | |
line-height: 14px; | |
text-align: center; | |
border: 1px solid #ccc; | |
box-shadow: 1px 1px 2px #eee; | |
text-decoration: none; | |
color: #777; | |
background-color: #f6f6f6; | |
font-family: Tahoma, Arial, sans-serif; | |
cursor: hand; cursor: pointer; | |
} | |
#chatter-box-counter { | |
float: right; | |
color: #777; | |
font-family: consolas, monospace; | |
font-size: 13px; | |
} | |
#chatter-box-auth-btn { | |
color: #06C432; | |
cursor: hand; cursor: pointer; | |
} | |
#chatter-box-unauth-btn { | |
position: absolute; | |
right: 8px; | |
bottom: 6px; | |
color: #D61E49; | |
cursor: hand; cursor: pointer; | |
} | |
</style> | |
<div id="chatter-box" style=""> | |
<h3>Share on Chatter</h3> | |
<a href="#" id="chatter-box-close-btn">x</a> | |
<textarea id="chatter-box-text"></textarea> | |
<div id="chatter-box-counter"><span></span> of #{char_limit}</div> | |
<button id="chatter-box-post-btn">Share on Chatter</button><br /> | |
<div id="chatter-box-msges"> | |
<div id="chatter-box-msg-auth" class="msg" > | |
Please authorize WP2Chatter to access your Salesforce Chatter API. <a href="#" id="chatter-box-auth-btn">>> Authorize Now</a> | |
</div> | |
<div id="chatter-box-msg-authing" class="msg" > | |
Authorization in process... <a href="#" id="chatter-box-authed-btn">I have already authorized...</a> | |
</div> | |
<div id="chatter-box-msg-authed-maybe" class="msg" > | |
Authorization might have been successful. Please try posting to Chatter. | |
</div> | |
<div id="chatter-box-msg-authed-success" class="msg" > | |
Authorization is successful. You can post to your Chatter now. | |
</div> | |
<div id="chatter-box-msg-posting" class="msg" > | |
Posting to chatter... | |
</div> | |
<div id="chatter-box-msg-posted" class="msg" > | |
Your message is successfully posted to Chatter! | |
</div> | |
</div> | |
<div id="chatter-box-unauth-btn">>> unauthorize</div> | |
</div> | |
>> | |
} | |
{ | |
// now append the chatter box to the HTML body | |
append('body', chatter_box); | |
// post_to_chatter ruleset is called when the button is clicked | |
watch('#chatter-box-post-btn', 'click'); | |
// clear the auth info, clear_access_token ruleset is called | |
watch('#chatter-box-unauth-btn', 'click'); | |
emit << | |
// Chatter share button is clicked | |
$K('a.share-chatter').on('click', function(e){ | |
// get post title and link from meta tags | |
var post_title = $K('meta[property="og:title"]').attr('content');; | |
var post_link = $K('meta[property="og:url"]').attr('content'); | |
$K('#chatter-box-text').val('"'+ post_title +'" '+ post_link); | |
$K('#chatter-box-counter > span').text($K('#chatter-box-text').val().length); | |
// show chatter_box | |
$K('#chatter-box').toggle(); | |
e.stopPropagation(); | |
e.preventDefault(); | |
}); | |
// character limit in chatter post form | |
$K('#chatter-box-text').on('keyup', function(e) { | |
var len = $K(this).val().length; | |
$K('#chatter-box-counter > span').text(len); | |
if(len >= char_limit && e.keyCode != 8) { | |
$K(this).val($K(this).val().substring(0, char_limit - 1)); | |
$K('#chatter-box-counter > span').text(char_limit); | |
e.preventDefault(); | |
} | |
}); | |
// close the chatter box | |
$K('#chatter-box-close-btn').on('click', function(e){ | |
// hide chatter_box | |
$K('#chatter-box').hide(); | |
e.stopPropagation(); | |
e.preventDefault(); | |
}); | |
// when user click on authorization link - show a popup to authorize | |
$K('#chatter-box-auth-btn').on('click', function(e){ | |
// open popup window for authorization | |
var auth_window = window.open(authorization_url, "WP2ChatterAuthWindow", "toolbar=no, location=yes, directories=no, status=no, menubar=no, scrollbars=no, menubar=no, resizable=no, width=560, height=420"); | |
// show that autorization is being done | |
$K('.msg').fadeOut('fast'); | |
$K('#chatter-box-msg-authing').fadeIn('fast'); | |
// disable the post button | |
$K('#chatter-box-post-btn').attr('disabled', 'disabled'); | |
e.stopPropagation(); | |
e.preventDefault(); | |
}); | |
// when user manually says that he/she has authorized, enable the post button | |
$K('#chatter-box-authed-btn').on('click', function(e){ | |
// remove all msg | |
$K('.msg').fadeOut('fast'); | |
// enable the post button | |
$K('#chatter-box-post-btn').removeAttr('disabled'); | |
e.stopPropagation(); | |
e.preventDefault(); | |
}); | |
>>; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment