-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add pusher integration. #73
base: master
Are you sure you want to change the base?
Changes from 5 commits
2ff295a
fc24eba
05b4c9a
dbff1bd
e9797da
85420b3
6bc4ee3
371ef10
864f49e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
* Service in the clientApp. | ||
*/ | ||
angular.module('clientApp') | ||
.service('Account', function ($q, $http, $timeout, Config, $rootScope, $log) { | ||
.service('Account', function ($q, $http, $timeout, Config, $rootScope, channelManager, $log) { | ||
|
||
// A private cache key. | ||
var cache = {}; | ||
|
@@ -37,6 +37,7 @@ angular.module('clientApp') | |
transformResponse: prepareResponse | ||
}).success(function(response) { | ||
setCache(response[0]); | ||
setChannels(response[0].companies); | ||
deferred.resolve(response[0]); | ||
}); | ||
|
||
|
@@ -97,4 +98,24 @@ angular.module('clientApp') | |
cache = {}; | ||
}); | ||
|
||
/** | ||
* Subscribe user to user's Pusher channel. | ||
* | ||
* @param int userId | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. => |
||
* The user id. | ||
*/ | ||
var setChannels = function(companies) { | ||
if (!companies) { | ||
// User doesn't have repositories yet. | ||
return; | ||
} | ||
|
||
companies.forEach(function (company) { | ||
if (!company.id) { | ||
// repoId is null. | ||
return; | ||
} | ||
channelManager.addChannel(company.id); | ||
}); | ||
}; | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @ngdoc service | ||
* @name clientApp.account | ||
* @description | ||
* # account | ||
* Service in the clientApp. | ||
*/ | ||
angular.module('clientApp') | ||
.service('channelManager', function ($q, $http, $timeout, $pusher, $log, Config, Auth) { | ||
|
||
var channels = {}; | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove empty line. |
||
this.pusher = null; | ||
|
||
/** | ||
* Get all pusher channels. | ||
* | ||
* @returns {{}} | ||
* Return list of existing channels. | ||
*/ | ||
this.getChannels = function () { | ||
return channels; | ||
}; | ||
|
||
/** | ||
* Get Company channel. | ||
* | ||
* @param companyId | ||
* Company id. | ||
* | ||
* @returns {*} | ||
* Return company's channel. | ||
*/ | ||
this.getChannel = function (companyId) { | ||
return (companyId in channels) ? channels[companyId] : null; | ||
}; | ||
|
||
/** | ||
* Add new company's chanel. | ||
* | ||
* @param companyId | ||
* User id. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. companyId is |
||
* | ||
* @returns {*} | ||
* Return new company's channel. | ||
*/ | ||
this.addChannel = function (companyId) { | ||
if (!!channels[companyId]) { | ||
// Already subscribed to channel. | ||
return; | ||
} | ||
var pusher = $pusher(this.getClient()); | ||
channels[companyId] = pusher.subscribe('private-company-' + companyId); | ||
return channels[companyId]; | ||
}; | ||
|
||
this.getClient = function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add docs. |
||
return this.pusher ? this.pusher : this.createNewPusher(); | ||
}; | ||
|
||
this.createNewPusher = function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add docs. |
||
var pusherConf = { | ||
authEndpoint: Config.backend + '/api/v1.0/pusher_auth', | ||
auth: { | ||
headers: { | ||
"access-token": Auth.getAccessToken() | ||
} | ||
} | ||
}; | ||
this.pusher = new Pusher(Config.pusherKey, pusherConf); | ||
return this.pusher; | ||
}; | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"require": { | ||
"pusher/pusher-php-server": "2.2" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
name = Skeleton Pusher | ||
core = 7.x | ||
package = Skeleton | ||
dependencies[] = composer_manager |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
/** | ||
* @file | ||
* Code for the Pusher API integration. | ||
*/ | ||
|
||
|
||
/** | ||
* Trigger a Pusher event. | ||
* | ||
* @param $company_id | ||
* Company ID. | ||
* @param $event_name | ||
* Event name. | ||
* @param array $data | ||
* Parameters that should be sent. | ||
* | ||
* @throws \PusherException | ||
* @throws \RestfulServerConfigurationException | ||
* | ||
* @return bool | ||
*/ | ||
function skeleton_pusher_trigger_event($company_id, $event_name, $data = array()) { | ||
$pusher = skeleton_pusher_get_pusher(); | ||
return $pusher->trigger('private-company-' . $company_id, $event_name, $data); | ||
} | ||
|
||
/** | ||
* Get a Pusher instance. | ||
* | ||
* @return \Pusher | ||
* The Pusher object. | ||
* | ||
* @throws \RestfulServerConfigurationException | ||
*/ | ||
function skeleton_pusher_get_pusher() { | ||
$pusher = &drupal_static(__FUNCTION__); | ||
if ($pusher) { | ||
return $pusher; | ||
} | ||
|
||
$app_key = variable_get('skeleton_pusher_app_key'); | ||
$app_secret = variable_get('skeleton_pusher_app_secret'); | ||
$app_id = variable_get('skeleton_pusher_app_id'); | ||
|
||
if (empty($app_key) || empty($app_secret) || empty($app_id)) { | ||
throw new \RestfulServerConfigurationException('Pusher app is not configured properly.'); | ||
} | ||
|
||
return new Pusher($app_key, $app_secret, $app_id); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't
angular.extend($scope.events, value);
enough ?