Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/BuildFire/sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
abuturla committed Feb 28, 2018
2 parents 198705f + 8d3acd7 commit 02dfdd4
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 5 deletions.
26 changes: 21 additions & 5 deletions pluginTester/scripts/overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,29 @@ postMaster.servicePluginAPIs.service.tag = 'service';
};


///override the authAPI.getCurrentUser to return CP loggedIn user
///override the authAPI.getCurrentUser to return auth
authAPI.secondaryUserLookup = function () {
var user = window.currentUser;
if(!user) {
if(!window.currentUser || !window.currentUser.userToken || !window.currentUser.auth)
return null;
}
return user.authProfile ? user.authProfile : user;

if (!window.currentAuthUser) {
var appId = '';
if (appContext.currentApp && appContext.currentApp.appId)
appId = appContext.currentApp.appId;

var request = new XMLHttpRequest();
request.open('GET', 'https://app.buildfire.com/api/user/auth/' + appId, false); // `false` makes the request synchronous
request.setRequestHeader("userToken", window.currentUser.userToken);
request.setRequestHeader("auth", window.currentUser.auth);
request.send(null);
if (request.status === 200) {
window.currentAuthUser = JSON.parse(request.responseText);
}
}
if (window.currentAuthUser)
window.currentAuthUser._cpUser = window.currentUser;

return window.currentAuthUser;
};
///

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ buildfire.notifications.pushNotification = {
//text: string
//at: Date (optional), representing the time to send the push notification.
//users: array (optional)
//userTags: array (optional)
//groupName: string (optional)
//queryString: string (optional)
schedule: function (options, callback) {
Expand Down
209 changes: 209 additions & 0 deletions scripts/buildfire/services/social/reactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/**
* Created by danielhindi on 2/16/18.
*/

if (typeof (buildfire) == "undefined") throw ("please add buildfire.js first to use BuildFire services");
if (!buildfire.social) buildfire.social = {};

buildfire.social.reactions= (function(){

var endPoint="https://kzq9kqyroa.execute-api.us-east-1.amazonaws.com/dev/social/";

function createReactionDiv(container,articleId,emotion){
var div = document.createElement('div');
div.setAttribute('emotion',emotion);


function handler() {

if (this.parentNode.className.indexOf("activeBar") >= 0) {
console.log(this.getAttribute('emotion'));
this.parentNode.classList.remove("activeBar");
buildfire.social.reactions.toggleEmotion(articleId,emotion,function(err,result){
debugger;
});
}
else {
this.parentNode.classList.add("activeBar");
}

}

if(location.protocol.indexOf("http")==0)
div.onclick=handler;
else
div.ontouch=handler;

container.appendChild(div);
}
function createReactionDivs(container,articleId){
['hate','love','lol','wow','sad','like'].forEach(function(e){
createReactionDiv(container,articleId,e);
});

}

function getUser(callback) {
buildfire.auth.getCurrentUser(function (err, user) {
if (err)
callback(err);
else if (!user) {
buildfire.auth.login({}, function (err, user) {
if (err)
callback(err);
else
callback(null,user);
});
}
else
callback(null,user);

});
}

function addLastReactorsPics(div,participants) {
if (!participants || !div || !participants.length) return;

var picCount=0;
for (var i = participants.length - 1; i >= 0 && picCount < 3; i--){
if (participants[i].user.profilePic) {
var img = document.createElement('img');
img.src = buildfire.imageLib.cropImage(participants[i].user.profilePic, {width: 52, height: 52});
img.className = "reactorAvatar";
div.appendChild(img);
picCount++;
}
}


}

function didIParticipate(userId,participants) {
if(!userId || !participants)return false;
if (stats.participants && participants.length)
for (var i = 0; i < participants.length; i++)
if (participants.userId == userId)
return true;
return false;
}

return {
fetchStats:function(articleIds,currentUserId,callback){
if(!callback)callback=function(){};

var ids = articleIds;
if(!Array.isArray(ids))
ids=[ids];
var url = endPoint + "articles?includeAllReaction=1&articleIds="+ ids.join(",");

fetch(url).then(function(res) {

if(res.status == 200){
res.json().then(function(b){
if(Array.isArray(b) && b.length > 0 )
callback(null,b[0]);
else
callback();
}).catch(function(err){
console.error(err);
});
}
else if(res.status == 400)
console.error(res) ;
} );


}
,toggleEmotion:function(articleId,emotion,callback){
if(!callback)callback=function(){};
getUser(function(err,user){
if(err)
callback(new Error('no logged in user',err));
else {

fetch(endPoint + "reactToggle",{
method: 'post',
body: JSON.stringify({
articleId,articleId
,emotion:emotion
,user: user
})
}).then(function(res) {
debugger;
if(res.status == 200){
res.json().then(function(b){
if(Array.isArray(b) && b.length > 0 )
callback(null,b[0]);
else
callback();
}).catch(function(err){
console.error(err);
});
}
else if(res.status == 400)
console.error(res) ;
} );
}
});




}
,attach:function(element,articleId,currentUserId){

var divTransformer = document.createElement('div');
divTransformer.className = "reactionTransformer";
element.appendChild(divTransformer);


var divReactionBar = document.createElement('div');
divReactionBar.className="reactionBar";
createReactionDivs(divReactionBar,articleId);

divTransformer.appendChild(divReactionBar);

this.fetchStats(articleId,currentUserId,function(err,stats){
if(!stats)return;

var divReactionStats = document.createElement('div');
divReactionStats.className="reactionStats";

addLastReactorsPics(divReactionStats,stats.participants);


if(didIParticipate(currentUserId,stats.participants)){

}


var count = 0 ;
for(p in stats.emotions)
count+= stats.emotions[p];

if(count) {
var divReactionCount = document.createElement('div');
divReactionCount.className = "reactionCount";
divReactionCount.innerHTML = "(+" + count + ")";
divReactionStats.appendChild(divReactionCount);
}
divTransformer.appendChild(divReactionStats);

});



}
}
})();

document.addEventListener("DOMContentLoaded", function() {
buildfire.auth.getCurrentUser(function(err,user){
var elements = document.querySelectorAll(".reactionContainer[articleId]");
elements.forEach(function(e){
var articleId= e.getAttribute("articleId");
buildfire.social.reactions.attach(e,articleId,user?user.id:null);
});
});

});
57 changes: 57 additions & 0 deletions styles/reactions.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

.reactionContainer{
overflow: hidden;
height: 55px;
padding-bottom: 3px;
white-space: nowrap;
}

.reactionContainer > .reactionTransformer {
transform: scale(1);
transform-origin: 0%;
white-space: nowrap;
}

.reactionContainer .reactionBar{
float: left;
background-color: white;
background-image: url('./media/reactions.png');
background-repeat: no-repeat;
height: 52px;
width: 279px;
box-shadow: 0 2px 2px rgba(0, 0, 0, 0.2), 0 0 0 1px rgba(0, 0, 0, 0.05);
border-radius: 50px;
margin-left: -230px;
transition: 250ms ease-out;
}

.reactionContainer .reactionBar.activeBar{
margin-left: 10px ;
}

.reactionContainer .reactionBar > div{

margin-left: 1px;
width:45px;
height:52px;
float: left;
white-space: nowrap;
}

.reactionContainer .reactionStats > .reactorAvatar{
width: 50px;
height:50px;
border-radius: 50%;
border:3px solid white;
}

.reactionContainer .reactionStats > .reactorAvatar:nth-child(n+2){
margin-left: -20px;
}

.reactionContainer .reactionStats > .reactionCount{
font-size: 20px;
display: inline;
padding-top: 15px;

}

0 comments on commit 02dfdd4

Please sign in to comment.