Web notifications. Issue #156.

This commit is contained in:
Mark Amoin 2018-04-03 15:43:11 +08:00 committed by Dmitry Khomutov
commit 3f56e4cc54
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
10 changed files with 512 additions and 135 deletions

View file

@ -1,6 +1,18 @@
var PHPCensor = {
intervals: {},
widgets: {},
webNotifiedBuilds: [],
/*
@var STATUS Refer to \PHPCensor\Model\Build.php constants.
TODO: Transfer this variable to Build JS class so
Build JS itself can use it as well.
*/
STATUS: [
'Pending',
'Running',
'Success',
'Failed'
],
init: function () {
$(document).ready(function () {
@ -19,6 +31,128 @@ var PHPCensor = {
});
},
/**
* Shallow comparison that determines that the build
* has been shown as at least once as a web notification.
* Also adds the build to a list of shown web notifications
* if it's not found in the list.
* @param object build
* @return boolean
*/
isWebNotifiedBuild: function (build) {
var o = PHPCensor.webNotifiedBuilds;
for (var i = 0; i < o.length; i++) {
var webNotifiedBuild = o[i];
var b =
webNotifiedBuild.projectTitle === build.projectTitle &&
webNotifiedBuild.branch === build.branch &&
webNotifiedBuild.status === build.status &&
webNotifiedBuild.datePerformed === build.datePerformed &&
webNotifiedBuild.dateFinished === build.dateFinished;
if (b) {
return true;
}
}
/*
It's impossible to remember or use all previously shown
builds. So let's clear them out once they reach 1000.
@var 1000 Estimated.
*/
if (PHPCensor.webNotifiedBuilds.length > 1000) {
PHPCensor.webNotifiedBuilds = [];
}
PHPCensor.webNotifiedBuilds.push(build);
return false;
},
/**
* Web notification.
* Chrome doesn't allow insecure protocols.
* Enable HTTPS even on localhost in order for
* web notifications to work properly.
* @param object data Contains an array of builds.
* @return void
*/
showWebNotification: function (data) {
var pending = data.pending;
var running = data.running;
var success = data.success;
var failed = data.failed;
var notification = null;
//Determine which notification to show.
//TODO: Refactor. Use foreach.
if (pending && pending.count > 0) {
notification = pending;
}
else if (running && running.count > 0) {
notification = running;
}
else if (success && success.count > 0) {
notification = success;
}
else if (failed && failed.count > 0) {
notification = failed;
}
if (notification) {
var msg = '';
if (!Notify.needsPermission) {
var items = notification.items;
for (var item in items) {
var build = items[item].build;
var projTitle = build.project_title;
var branch = build.branch;
var status = PHPCensor.STATUS[build.status];
var datePerformed = build.date_performed;
var dateFinished = build.date_finished;
var rn = "\r\n";
var build = {
projectTitle: projTitle,
branch: branch,
status: status,
datePerformed: datePerformed,
dateFinished: dateFinished
};
//Ignore if the last displayed notification is
//similar to what we're again about to display.
if (!PHPCensor.isWebNotifiedBuild(build)) {
msg +=
'Project title: ' + projTitle + rn +
'Git branch: ' + branch + rn +
'Status: ' + status + rn;
//Build details is empty during
//widget-all-projects-update.
if (datePerformed.length > 0) {
msg += datePerformed + rn;
}
if (dateFinished.length > 0) {
msg += dateFinished;
}
new Notify(
'PHP Censor Web Notification',
{body: msg}
).show();
}
}
}
else if (Notify.isSupported()) {
Notify.requestPermission(null, function(){
msg = 'Web notifications permission ' +
'has been denied by the user.'
console.warn(msg);
});
}
}
},
getBuilds: function () {
$.ajax({
url: APP_URL + 'build/ajax-queue',
@ -29,6 +163,14 @@ var PHPCensor = {
error: PHPCensor.handleFailedAjax
});
$.ajax({
url: APP_URL + 'web-notifications/builds-updated',
success: function (data) {
PHPCensor.showWebNotification(data);
},
error: PHPCensor.handleFailedAjax
});
},
getProjectBuilds: function () {

View file

@ -33,6 +33,18 @@ PHPCensor.widgets.allProjects = {
error: PHPCensor.handleFailedAjax
});
//Let's build another mechanism for web notification
//since the above feature is tightly coupled to the view.
$.ajax({
url: APP_URL +
'web-notifications/widgets-all-projects-update/' +
projectId,
success: function (data) {
PHPCensor.showWebNotification(data);
},
error: PHPCensor.handleFailedAjax
});
});
}
};