Browse Source

Initial commit

master
Chimo 3 years ago
commit
aa565ca348

+ 10
- 0
.jscsrc View File

@@ -0,0 +1,10 @@
{
"preset": "jquery",
"validateLineBreaks": null,
"requireCamelCaseOrUpperCaseIdentifiers": null,
"maximumLineLength": 500,
"requireBlocksOnNewline": true,
"requireSpaceBeforeBlockStatements": true,
"disallowMixedSpacesAndTabs": true,
validateIndentation: 4
}

+ 14
- 0
.jshintrc View File

@@ -0,0 +1,14 @@
{
"boss": true,
"curly": true,
"eqeqeq": true,
"eqnull": true,
"expr": true,
"immed": true,
"noarg": true,
"undef": true,
"unused": true,
"jquery": true,
"browser": true,
"es3": true
}

+ 68
- 0
BrowserNotificationsPlugin.php View File

@@ -0,0 +1,68 @@
<?php

if (!defined('GNUSOCIAL')) {
exit(1);
}

class BrowserNotificationsPlugin extends Plugin
{
const VERSION = '0.0.1';

function onEndAccountSettingsNav($action) {
$action->elementStart('li');
$action->element('a', array('href' => common_local_url('browsernotificationssettings')), 'Browser Notifications');
$action->elementEnd('li');
return true;
}

function onRouterInitialized($m) {
$m->connect(
'settings/browsernotifications', array(
'action' => 'browsernotificationssettings'
)
);

return true;
}

function onEndShowScripts($action) {
$user_settings = BrowserNotificationSettings::getDefaults();

if (common_logged_in()) {
$user = common_current_user();

$bns = BrowserNotificationSettings::getByUserId($user->id);

if (!empty($bns)) {
$user_settings = $bns;
}
}

// Only include the JS if the setting is enabled
if ($user_settings->enabled === true) {
$action->inlineScript('BrowserNotifications = ' . $user_settings->toJSON());

$action->script($this->path('js/browser-notifications.js'));
}

return true;
}

function onCheckSchema() {
$schema = Schema::get();
$schema->ensureTable('browser_notifications', BrowserNotificationSettings::schemaDef());
return true;
}

function onPluginVersion(&$versions)
{
$versions[] = array('name' => 'BrowserNotifications',
'version' => self::VERSION,
'author' => 'chimo',
'homepage' => 'https://github.com/chimo/gs-browserNotifications',
'description' =>
// TRANS: Plugin description.
_m('Receive browser notifications when a new notice and/or mention comes in.'));
return true;
}
}

+ 9
- 0
README.md View File

@@ -0,0 +1,9 @@
GNU social Browser Notifications
===========================

Instructions
---------

1. Make sure the files are in a folder called BrowserNotifications if they're not already
2. Put the folder in your /plugins/ directory
3. Tell /config.php to use it with: addPlugin('BrowserNotifications');

+ 60
- 0
actions/browsernotificationssettings.php View File

@@ -0,0 +1,60 @@
<?php
if (!defined('GNUSOCIAL')) {
exit(1);
}

class BrowserNotificationsSettingsAction extends Action
{
function prepare($args)
{
parent::prepare($args);

if (!common_logged_in()) { // Make sure we're logged in
$this->clientError(_('Not logged in.'));
return;
} else if (!common_is_real_login()) { // Make _really_ sure we're logged in...
common_set_returnto($this->selfUrl());
$user = common_current_user();
if (Event::handle('RedirectToLogin', array($this, $user))) {
common_redirect(common_local_url('login'), 303);
}
} else { // k, I think by now we're logged in. For realz.
$this->user = common_current_user();
}

if ($this->isPost()) {
$this->checkSessionToken();
}

return true;
}

function handle($args)
{
parent::handle($args);

if ($this->isPost()) {
$settings = array(
'enabled' => $this->boolean('enabled', false),
'mentions_only' => $this->boolean('mentions_only', false)
);

BrowserNotificationSettings::save($this->user, $settings);
}

$this->showPage();
}

function title()
{
return _m('Browser Notifications Settings');
}

function showContent()
{
// TODO: Show 'success'/'error' msg after a form submit

$form = new BrowserNotificationsSettingsForm($this);
$form->show();
}
}

+ 72
- 0
classes/BrowserNotificationSettings.php View File

@@ -0,0 +1,72 @@
<?php


if (!defined('GNUSOCIAL')) {
exit(1);
}

class BrowserNotificationSettings extends Managed_DataObject
{
public $__table = 'browser_notifications'; // table name

public $user_id; // int(10)
public $enabled; // boolean
public $mentions_only; // boolean

public static function save($user, $settings)
{
$bns = new BrowserNotificationSettings();

$bns->user_id = $user->id;
$bns->enabled = $settings['enabled'];
$bns->mentions_only = $settings['mentions_only'];

// First time saving settings
if (empty(self::getByUserId($user->id))) {
$bns->insert();
} else { // Updating existing settings
$bns->update();
}
}

public static function getDefaults()
{
return array(
'enabled' => true,
'mentions_only' => false
);
}

public function toJSON()
{
return json_encode(array(
'enabled' => $this->enabled,
'mentions_only' => $this->mentions_only
));
}

public static function getByUserId($userid)
{
$user_settings = self::getKV('user_id', $userid);

$user_settings->enabled = (boolean)$user_settings->enabled;
$user_settings->mentions_only = (boolean)$user_settings->mentions_only;

return $user_settings;
}

public static function schemaDef()
{
return array(
'fields' => array(
'user_id' => array('type' => 'int(10)', 'not null' => true),
'enabled' => array('type' => 'int', 'size' => 'tiny', 'default' => 1),
'mentions_only' => array('type' => 'int', 'size' => 'tiny', 'default' => 0),
),
'primary key' => array('user_id'),
'foreign keys' => array(
'browsernotifications_user_id_fkey' => array('user', array('user_id' => 'id'))
)
);
}
}

+ 72
- 0
forms/browsernotificationssettings.php View File

@@ -0,0 +1,72 @@
<?php
if (!defined('GNUSOCIAL')) {
exit(1);
}

class BrowserNotificationsSettingsForm extends Form
{
function __construct($out=null)
{
parent::__construct($out);
}

function id()
{
return 'browser_notifications_settings';
}

function formClass()
{
return 'form_settings';
}

function action()
{
return common_local_url('browsernotificationssettings');
}

function formData()
{
$this->out->elementStart('fieldset');

$this->out->elementStart('ul', 'form_data');

// Start with default settings
$user_settings = BrowserNotificationSettings::getDefaults();

// Overwrite defaults with user settings if available
$bns = BrowserNotificationSettings::getByUserId($this->out->user->id);

if(!empty($bns)) {
$user_settings['enabled'] = $bns->enabled;
$user_settings['mentions_only'] = $bns->mentions_only;
}

// Enabled?
$this->li();
$this->out->checkbox(
'enabled', // id
'Enabled', // label
$user_settings['enabled'] // checked
);
$this->unli();

// Mentions only?
// TODO
/* $this->li();
$this->out->checkbox(
'mentions_only',
'Only show notifications for notices that mention me',
$user_settings['mentions_only']
);
$this->unli(); */

$this->elementEnd('ul');
$this->elementEnd('fieldset');
}

function formActions()
{
$this->out->submit('browser-notifications-settings-submit', _m('BUTTON', 'Save'), 'submit', 'submit');
}
}

+ 55
- 0
js/browser-notifications.js View File

@@ -0,0 +1,55 @@
( function() {
/*global RealtimeUpdate: false, BrowserNotifications: false*/
"use strict";

if ( !( "Notification" in window ) || typeof( RealtimeUpdate ) === "undefined" || BrowserNotifications.enabled === false ) {
return;
}

var gs_makeNoticeItem = RealtimeUpdate.makeNoticeItem, // Monkey-patch
showNotification,
isMention;

// Ask for notification permissions on page load
Notification.requestPermission();

showNotification = function( node ) {
/* Request permission again in case previous ones were missed
If request was previouly granted, it doesn't ask again; it just shows
If request was previouly denied, nothing happens */
Notification.requestPermission( function( permission ) {
if ( permission === "granted" ) {
var $node = $( node ),
noticeText = $.trim( $node.find( ".e-content" ).text() ),
author = $.trim( $node.find( ".notice-headers .p-author" ).text() );

new Notification( "New notice from " + author, { body: noticeText } );
}
} );
};

isMention = function( /* node */ ) {
/* TODO */

return true;
};

RealtimeUpdate.makeNoticeItem = function( data, callback ) {
/* Monkey-patch */
var gs_callback = callback;

// Call the original "makeNoticeItem", but with our own callback
gs_makeNoticeItem( data, function( node ) {
/* Call the original callback */
gs_callback( node );

if ( BrowserNotifications.mentions_only === true ) {
if ( isMention( node ) === true ) {
showNotification( node );
}
} else {
showNotification( node );
}
} );
};
}() );

Loading…
Cancel
Save