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 all 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ before_script:
# Initiate the generator to build using CLI. We use the --no-insight flag to
# prevent yo from waiting for user input about sending anonymous usgae
# statistics.
- yo hedley --no-insight --project-name=skeleton --github-repo=https://github.com/Foo/skeleton --db=skeleton --db-user=root --db-pass= --drupal-url=http://127.0.0.1
- yo hedley --no-insight --project-name=skeleton --github-repo=https://github.com/Foo/skeleton --db=skeleton --db-user=root --db-pass= --drupal-url=http://127.0.0.1 --pusher-key=0 --pusher-secret=0 --pusher-id=0

# Configure client.
- cd client
Expand Down
98 changes: 97 additions & 1 deletion app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ module.exports = yeoman.generators.Base.extend({
type: String,
required: 'false'
});

this.option('pusher-key', {
desc: 'The pusher key',
type: String,
required: 'false'
});

this.option('pusher-secret', {
desc: 'The pusher secret',
type: String,
required: 'false'
});

this.option('pusher-id', {
desc: 'The pusher app ID',
type: String,
required: 'false'
});
},

askForProjectName: function () {
Expand Down Expand Up @@ -204,6 +222,81 @@ module.exports = yeoman.generators.Base.extend({
}.bind(this));
},

askForPusherKey: function () {
// Allow passing an empty password.
if (this.options['pusher-key'] !== undefined) {
// Get the value from the CLI.
this.pusherKey = this.options['pusher-key'];
this.log('Setting pusher key: ' + this.pusherKey);
return;
}

var done = this.async();

var prompts = [{
name: 'pusherKey',
message: 'What is the Pusher key?',
// Empty by default, as otherwise it might not be able to set to blank.
default: ''
}];

this.prompt(prompts, function (props) {
this.pusherKey = props.pusherKey;

done();
}.bind(this));
},

askForPusherSecret: function () {
// Allow passing an empty password.
if (this.options['pusher-secret'] !== undefined) {
// Get the value from the CLI.
this.pusherSecret = this.options['pusher-secret'];
this.log('Setting pusher secret: ' + this.pusherSecret);
return;
}

var done = this.async();

var prompts = [{
name: 'pusherSecret',
message: 'What is the Pusher Secret?',
// Empty by default, as otherwise it might not be able to set to blank.
default: ''
}];

this.prompt(prompts, function (props) {
this.pusherSecret = props.pusherSecret;

done();
}.bind(this));
},

askForPusherId: function () {
// Allow passing an empty password.
if (this.options['pusher-id'] !== undefined) {
// Get the value from the CLI.
this.pusherId = this.options['pusher-id'];
this.log('Setting pusher app ID: ' + this.pusherId);
return;
}

var done = this.async();

var prompts = [{
name: 'pusherId',
message: 'What is the Pusher App ID?',
// Empty by default, as otherwise it might not be able to set to blank.
default: ''
}];

this.prompt(prompts, function (props) {
this.pusherId = props.pusherId;

done();
}.bind(this));
},

writing: {
app: function() {
var self = this;
Expand Down Expand Up @@ -249,7 +342,10 @@ module.exports = yeoman.generators.Base.extend({
if (fileName === 'config.sh') {
newContents = newContents
.replace(/MYSQL_USERNAME=".*"/g, 'MYSQL_USERNAME="' + self.dbUser + '"')
.replace(/MYSQL_PASSWORD=".*"/g, 'MYSQL_PASSWORD="' + self.dbPass + '"');
.replace(/MYSQL_PASSWORD=".*"/g, 'MYSQL_PASSWORD="' + self.dbPass + '"')
.replace(/<your-app-key>/g, self.pusherKey)
.replace(/<your-app-secret>/g, self.pusherSecret)
.replace(/<your-app-id>/g, self.pusherId);
}

self.fs.write(newFileName, newContents);
Expand Down
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.
angular.extend($scope.events, value);
// 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 companies channeles.
*
* @param array companies
* The user's companies list.
*/
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
88 changes: 88 additions & 0 deletions app/templates/client/app/scripts/services/pusher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
'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 = {};

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
* Company 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];
};

/**
* Get the Pusher object.
*
* @returns {*}
* Returns existing object or creates a new one.
*/
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();
};

/**
* Create a new Pusher object.
*
* @returns {*}
* Return the Pusher object.
*/
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/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,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
}
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
Loading