Skip to content
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

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/templates/client/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ <h3>Login with demo / 1234</h3>
<script src="bower_components/angular-ui-router/release/angular-ui-router.js"></script>
<script src="bower_components/angular-local-storage/dist/angular-local-storage.js"></script>
<script src="bower_components/angular-loading-bar/build/loading-bar.js"></script>
<script src="bower_components/pusher-angular/lib/pusher-angular.js"></script>
<script src="bower_components/pusher/dist/pusher.js"></script>
<!-- endbower -->
<!-- endbuild -->

Expand All @@ -87,6 +89,7 @@ <h3>Login with demo / 1234</h3>
<script src="scripts/services/companies.js"></script>
<script src="scripts/services/map.js"></script>
<script src="scripts/services/marker.js"></script>
<script src="scripts/services/pusher.js"></script>
<script src="scripts/controllers/dashboard.js"></script>
<script src="scripts/directives/spinner.js"></script>
<script src="scripts/directives/loadingbartext.js"></script>
Expand Down
1 change: 1 addition & 0 deletions app/templates/client/app/scripts/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ angular
'config',
'leaflet-directive',
'LocalStorageModule',
'pusher-angular',
'ui.router',
'angular-loading-bar'
])
Expand Down
16 changes: 15 additions & 1 deletion app/templates/client/app/scripts/controllers/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Controller of the clientApp
*/
angular.module('clientApp')
.controller('EventsCtrl', function ($scope, events, authors, mapConfig, $state, $stateParams, $log) {
.controller('EventsCtrl', function ($scope, events, authors, mapConfig, Events, $state, $stateParams, channelManager, $log) {

// Initialize values.
$scope.events = events;
Expand Down Expand Up @@ -44,6 +44,20 @@ angular.module('clientApp')
selectedAuthorId($stateParams.userId);
}

// Get list of chanels.
var channels = channelManager.getChannels();
angular.forEach(channels, function(channel, companyId) {
// Listen to event.
channel.bind('new-event', function(data) {
Events.get(companyId, data.userId).then(function(value) {
// Update events list of the company.
$scope.events = angular.extend($scope.events, value);
Copy link
Contributor

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 ?

// Update user's events count.
$scope.authors[data.userId].count = Object.keys(value).length;
});
});
});

// Select marker in the Map.
$scope.$on('leafletDirectiveMarker.click', function(event, args) {
var stateName = $stateParams.userId ? 'dashboard.byCompany.byUser.events.event' : 'dashboard.byCompany.events.event';
Expand Down
23 changes: 22 additions & 1 deletion app/templates/client/app/scripts/services/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -37,6 +37,7 @@ angular.module('clientApp')
transformResponse: prepareResponse
}).success(function(response) {
setCache(response[0]);
setChannels(response[0].companies);
deferred.resolve(response[0]);
});

Expand Down Expand Up @@ -97,4 +98,24 @@ angular.module('clientApp')
cache = {};
});

/**
* Subscribe user to user's Pusher channel.
*
* @param int userId
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=> companies

* 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);
});
};
});
7 changes: 7 additions & 0 deletions app/templates/client/app/scripts/services/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ angular.module('clientApp')
return !!localStorageService.get('access_token');
};

/**
* Returns access token.
*/
this.getAccessToken = function() {
return localStorageService.get('access_token');
};

/**
* Authentication failed, set state to login.
*/
Expand Down
77 changes: 77 additions & 0 deletions app/templates/client/app/scripts/services/pusher.js
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 = {};


Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

companyId is User id ?

*
* @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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add docs.

return this.pusher ? this.pusher : this.createNewPusher();
};

this.createNewPusher = function() {
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
};

});
4 changes: 3 additions & 1 deletion app/templates/client/bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"angular-leaflet-directive": "~0.7.9",
"angular-ui-router": "~0.2.13",
"angular-local-storage": "~0.1.5",
"angular-loading-bar": "~0.6.0"
"angular-loading-bar": "~0.6.0",
"pusher-angular": "~0.1.5",
"pusher": "~2.2.4"
},
"devDependencies": {
"angular-mocks": "~1.3.0",
Expand Down
17 changes: 14 additions & 3 deletions app/templates/default.config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,21 @@ MYSQL_DB_NAME="skeleton"
##

# Post install script.
# function post_install {}
#function post_install {
# chmod 777 www/sites/default/settings.php
#
# # Pusher integration.
# echo "\$conf['skeleton_pusher_app_key'] = '<your-app-key>';" >> www/sites/default/settings.php
# echo "\$conf['skeleton_pusher_app_secret'] = '<your-app-secret>';" >> www/sites/default/settings.php
# echo "\$conf['skeleton_pusher_app_id'] = '<your-app-id>';" >> www/sites/default/settings.php
#}

# Post upgrade script.
# function post_upgrade {}
#function post_upgrade {
# post_install
#}

# Post reset script.
# function post_reset {}
#function post_reset {
# post_install
#}
3 changes: 3 additions & 0 deletions app/templates/install
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ symlink_externals
# Install the Drupal profile as defined in the config.sh file.
install_drupal_profile

# Composer install
composer_install

# Setup the files directory.
create_sites_default_files_directory

Expand Down
12 changes: 12 additions & 0 deletions app/templates/scripts/helper-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ function install_drupal_profile {
cd $ROOT
}

##
# Composer install.
##
function composer_install {
echo -e "${LBLUE}> Composer install${RESTORE}"

cd $ROOT/www/sites/default/files/composer
composer install
echo

cd $ROOT
}

##
# Create (if not exists) and set the proper file permissions
Expand Down
3 changes: 3 additions & 0 deletions app/templates/scripts/reset
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ delete_sites_default_content
# Install the installation profile.
install_drupal_profile

# Composer install
composer_install

# Setup the files directory.
create_sites_default_files_directory

Expand Down
5 changes: 4 additions & 1 deletion app/templates/skeleton/drupal-org.make
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ projects[admin_menu][version] = "3.0-rc5"
projects[admin_views][subdir] = "contrib"
projects[admin_views][version] = "1.4"

projects[composer_manager][subdir] = "contrib"
projects[composer_manager][version] = "1.7"

projects[ctools][subdir] = "contrib"
projects[ctools][version] = "1.7"

Expand Down Expand Up @@ -105,4 +108,4 @@ projects[migrate][subdir] = "development"
projects[migrate][version] = "2.7"

projects[migrate_extras][subdir] = "development"
projects[migrate_extras][version] = 2.5
projects[migrate_extras][version] = 2.5
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,20 @@
*/

include_once 'skeleton_event.features.inc';

/**
* Implement hook_node_insert().
*/
function skeleton_event_node_insert($node) {
if ($node->type != 'event') {
return;
}
$wrapper = entity_metadata_wrapper('node', $node);
$company_id = $wrapper->{OG_AUDIENCE_FIELD}->value(array('identifier' => TRUE));

$data = array(
'userId' => $node->uid,
);

skeleton_pusher_trigger_event($company_id, 'new-event', $data);
}
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);
}
Loading