Browse Source

Initial commit

Chimo 3 years ago
commit
aa565ca348

+ 10
- 0
.jscsrc View File

@@ -0,0 +1,10 @@
1
+{
2
+	"preset": "jquery",
3
+	"validateLineBreaks": null,
4
+	"requireCamelCaseOrUpperCaseIdentifiers": null,
5
+	"maximumLineLength": 500,
6
+	"requireBlocksOnNewline": true,
7
+	"requireSpaceBeforeBlockStatements": true,
8
+	"disallowMixedSpacesAndTabs": true,
9
+    validateIndentation: 4
10
+}

+ 14
- 0
.jshintrc View File

@@ -0,0 +1,14 @@
1
+{
2
+	"boss": true,
3
+	"curly": true,
4
+	"eqeqeq": true,
5
+	"eqnull": true,
6
+	"expr": true,
7
+	"immed": true,
8
+	"noarg": true,
9
+	"undef": true,
10
+	"unused": true,
11
+	"jquery": true,
12
+	"browser": true,
13
+	"es3": true
14
+}

+ 68
- 0
BrowserNotificationsPlugin.php View File

@@ -0,0 +1,68 @@
1
+<?php
2
+
3
+if (!defined('GNUSOCIAL')) {
4
+    exit(1);
5
+}
6
+
7
+class BrowserNotificationsPlugin extends Plugin
8
+{
9
+    const VERSION = '0.0.1';
10
+
11
+    function onEndAccountSettingsNav($action) {
12
+        $action->elementStart('li');
13
+        $action->element('a', array('href' => common_local_url('browsernotificationssettings')), 'Browser Notifications');
14
+        $action->elementEnd('li');
15
+        return true;
16
+    }
17
+
18
+    function onRouterInitialized($m) {
19
+        $m->connect(
20
+            'settings/browsernotifications', array(
21
+                'action' => 'browsernotificationssettings'
22
+                )
23
+            );
24
+
25
+        return true;
26
+    }
27
+
28
+    function onEndShowScripts($action) {
29
+        $user_settings = BrowserNotificationSettings::getDefaults();
30
+
31
+        if (common_logged_in()) {
32
+            $user = common_current_user();
33
+
34
+            $bns = BrowserNotificationSettings::getByUserId($user->id);
35
+
36
+            if (!empty($bns)) {
37
+                $user_settings = $bns;
38
+            }
39
+        }
40
+
41
+        // Only include the JS if the setting is enabled
42
+        if ($user_settings->enabled === true) {
43
+            $action->inlineScript('BrowserNotifications = ' . $user_settings->toJSON());
44
+
45
+            $action->script($this->path('js/browser-notifications.js'));
46
+        }
47
+
48
+        return true;
49
+    }
50
+
51
+    function onCheckSchema() {
52
+        $schema = Schema::get();
53
+        $schema->ensureTable('browser_notifications', BrowserNotificationSettings::schemaDef());
54
+        return true;
55
+    }
56
+
57
+    function onPluginVersion(&$versions)
58
+    {
59
+        $versions[] = array('name' => 'BrowserNotifications',
60
+                            'version' => self::VERSION,
61
+                            'author' => 'chimo',
62
+                            'homepage' => 'https://github.com/chimo/gs-browserNotifications',
63
+                            'description' =>
64
+                            // TRANS: Plugin description.
65
+                            _m('Receive browser notifications when a new notice and/or mention comes in.'));
66
+        return true;
67
+    }
68
+}

+ 9
- 0
README.md View File

@@ -0,0 +1,9 @@
1
+GNU social Browser Notifications
2
+===========================
3
+
4
+Instructions
5
+---------
6
+
7
+1. Make sure the files are in a folder called BrowserNotifications if they're not already
8
+2. Put the folder in your /plugins/ directory
9
+3. Tell /config.php to use it with: addPlugin('BrowserNotifications');

+ 60
- 0
actions/browsernotificationssettings.php View File

@@ -0,0 +1,60 @@
1
+<?php
2
+if (!defined('GNUSOCIAL')) {
3
+    exit(1);
4
+}
5
+
6
+class BrowserNotificationsSettingsAction extends Action
7
+{
8
+    function prepare($args)
9
+    {
10
+        parent::prepare($args);
11
+
12
+        if (!common_logged_in()) { // Make sure we're logged in
13
+            $this->clientError(_('Not logged in.'));
14
+            return;
15
+        } else if (!common_is_real_login()) { // Make _really_ sure we're logged in...
16
+            common_set_returnto($this->selfUrl());
17
+            $user = common_current_user();
18
+            if (Event::handle('RedirectToLogin', array($this, $user))) {
19
+                common_redirect(common_local_url('login'), 303);
20
+            }
21
+        } else { // k, I think by now we're logged in. For realz.
22
+            $this->user = common_current_user();
23
+        }
24
+
25
+        if ($this->isPost()) {
26
+            $this->checkSessionToken();
27
+        }
28
+
29
+        return true;
30
+    }
31
+
32
+    function handle($args)
33
+    {
34
+        parent::handle($args);
35
+
36
+        if ($this->isPost()) {
37
+            $settings = array(
38
+                'enabled' => $this->boolean('enabled', false),
39
+                'mentions_only' => $this->boolean('mentions_only', false)
40
+            );
41
+
42
+            BrowserNotificationSettings::save($this->user, $settings);
43
+        }
44
+
45
+        $this->showPage();
46
+    }
47
+
48
+    function title()
49
+    {
50
+        return _m('Browser Notifications Settings');
51
+    }
52
+
53
+    function showContent()
54
+    {
55
+        // TODO: Show 'success'/'error' msg after a form submit
56
+
57
+        $form = new BrowserNotificationsSettingsForm($this);
58
+        $form->show();
59
+    }
60
+}

+ 72
- 0
classes/BrowserNotificationSettings.php View File

@@ -0,0 +1,72 @@
1
+<?php
2
+
3
+
4
+if (!defined('GNUSOCIAL')) {
5
+    exit(1);
6
+}
7
+
8
+class BrowserNotificationSettings extends Managed_DataObject
9
+{
10
+    public $__table = 'browser_notifications'; // table name
11
+
12
+    public $user_id;         // int(10)
13
+    public $enabled;         // boolean
14
+    public $mentions_only;   // boolean
15
+
16
+    public static function save($user, $settings)
17
+    {
18
+        $bns = new BrowserNotificationSettings();
19
+
20
+        $bns->user_id = $user->id;
21
+        $bns->enabled = $settings['enabled'];
22
+        $bns->mentions_only = $settings['mentions_only'];
23
+
24
+        // First time saving settings
25
+        if (empty(self::getByUserId($user->id))) {
26
+            $bns->insert();
27
+        } else { // Updating existing settings
28
+            $bns->update();
29
+        }
30
+    }
31
+
32
+    public static function getDefaults()
33
+    {
34
+        return array(
35
+            'enabled' => true,
36
+            'mentions_only' => false
37
+        );
38
+    }
39
+
40
+    public function toJSON()
41
+    {
42
+        return json_encode(array(
43
+            'enabled' => $this->enabled,
44
+            'mentions_only' => $this->mentions_only
45
+        ));
46
+    }
47
+
48
+    public static function getByUserId($userid)
49
+    {
50
+        $user_settings = self::getKV('user_id', $userid);
51
+
52
+        $user_settings->enabled = (boolean)$user_settings->enabled;
53
+        $user_settings->mentions_only = (boolean)$user_settings->mentions_only;
54
+
55
+        return $user_settings;
56
+    }
57
+
58
+    public static function schemaDef()
59
+    {
60
+        return array(
61
+            'fields' => array(
62
+                'user_id' => array('type' => 'int(10)', 'not null' => true),
63
+                'enabled' => array('type' => 'int', 'size' => 'tiny', 'default' => 1),
64
+                'mentions_only' => array('type' => 'int', 'size' => 'tiny', 'default' => 0),
65
+            ),
66
+            'primary key' => array('user_id'),
67
+            'foreign keys' => array(
68
+                'browsernotifications_user_id_fkey' => array('user', array('user_id' => 'id'))
69
+            )
70
+        );
71
+    }
72
+}

+ 72
- 0
forms/browsernotificationssettings.php View File

@@ -0,0 +1,72 @@
1
+<?php
2
+if (!defined('GNUSOCIAL')) {
3
+    exit(1);
4
+}
5
+
6
+class BrowserNotificationsSettingsForm extends Form
7
+{
8
+    function __construct($out=null)
9
+    {
10
+        parent::__construct($out);
11
+    }
12
+
13
+    function id()
14
+    {
15
+        return 'browser_notifications_settings';
16
+    }
17
+
18
+    function formClass()
19
+    {
20
+        return 'form_settings';
21
+    }
22
+
23
+    function action()
24
+    {
25
+        return common_local_url('browsernotificationssettings');
26
+    }
27
+
28
+    function formData()
29
+    {
30
+        $this->out->elementStart('fieldset');
31
+
32
+        $this->out->elementStart('ul', 'form_data');
33
+
34
+        // Start with default settings
35
+        $user_settings = BrowserNotificationSettings::getDefaults();
36
+
37
+        // Overwrite defaults with user settings if available
38
+        $bns = BrowserNotificationSettings::getByUserId($this->out->user->id);
39
+
40
+        if(!empty($bns)) {
41
+            $user_settings['enabled'] = $bns->enabled;
42
+            $user_settings['mentions_only'] = $bns->mentions_only;
43
+        }
44
+
45
+        // Enabled?
46
+        $this->li();
47
+        $this->out->checkbox(
48
+            'enabled',  // id
49
+            'Enabled',  // label
50
+            $user_settings['enabled'] // checked
51
+        );
52
+        $this->unli();
53
+
54
+        // Mentions only?
55
+        // TODO
56
+        /* $this->li();
57
+        $this->out->checkbox(
58
+            'mentions_only',
59
+            'Only show notifications for notices that mention me',
60
+            $user_settings['mentions_only']
61
+        );
62
+        $this->unli(); */
63
+
64
+        $this->elementEnd('ul');
65
+        $this->elementEnd('fieldset');
66
+    }
67
+
68
+    function formActions()
69
+    {
70
+        $this->out->submit('browser-notifications-settings-submit', _m('BUTTON', 'Save'), 'submit', 'submit');
71
+    }
72
+}

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

@@ -0,0 +1,55 @@
1
+( function() {
2
+    /*global RealtimeUpdate: false, BrowserNotifications: false*/
3
+    "use strict";
4
+
5
+    if ( !( "Notification" in window ) || typeof( RealtimeUpdate ) === "undefined" || BrowserNotifications.enabled === false ) {
6
+        return;
7
+    }
8
+
9
+    var gs_makeNoticeItem = RealtimeUpdate.makeNoticeItem, // Monkey-patch
10
+        showNotification,
11
+        isMention;
12
+
13
+    // Ask for notification permissions on page load
14
+    Notification.requestPermission();
15
+
16
+    showNotification = function( node ) {
17
+        /* Request permission again in case previous ones were missed
18
+           If request was previouly granted, it doesn't ask again; it just shows
19
+           If request was previouly denied, nothing happens */
20
+        Notification.requestPermission( function( permission ) {
21
+            if ( permission === "granted" ) {
22
+                var $node = $( node ),
23
+                    noticeText = $.trim( $node.find( ".e-content" ).text() ),
24
+                    author = $.trim( $node.find( ".notice-headers .p-author" ).text() );
25
+
26
+                new Notification( "New notice from " + author, { body: noticeText } );
27
+            }
28
+        } );
29
+    };
30
+
31
+    isMention = function( /* node */ ) {
32
+        /* TODO */
33
+
34
+        return true;
35
+    };
36
+
37
+    RealtimeUpdate.makeNoticeItem = function( data, callback ) {
38
+        /* Monkey-patch */
39
+        var gs_callback = callback;
40
+
41
+        // Call the original "makeNoticeItem", but with our own callback
42
+        gs_makeNoticeItem( data, function( node ) {
43
+            /* Call the original callback */
44
+            gs_callback( node );
45
+
46
+            if ( BrowserNotifications.mentions_only === true ) {
47
+                if ( isMention( node ) === true ) {
48
+                    showNotification( node );
49
+                }
50
+            } else {
51
+                showNotification( node );
52
+            }
53
+        } );
54
+    };
55
+}() );

Loading…
Cancel
Save