summary refs log tree commit diff
path: root/syweb/webclient/components/matrix/notification-service.js
blob: 9a911413c3a6abd10a602e9886aa3e55268db6a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Copyright 2014 OpenMarket Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

'use strict';

/*
This service manages notifications: enabling, creating and showing them. This
also contains 'bing word' logic.
*/
angular.module('notificationService', [])
.factory('notificationService', ['$timeout', function($timeout) {

    var getLocalPartFromUserId = function(user_id) {
        if (!user_id) {
            return null;
        }
        var localpartRegex = /@(.*):\w+/i
        var results = localpartRegex.exec(user_id);
        if (results && results.length == 2) {
            return results[1];
        }
        return null;
    };
    
    return {
    
        containsBingWord: function(userId, displayName, bingWords, content) {
            // case-insensitive name check for user_id OR display_name if they exist
            var userRegex = "";
            if (userId) {
                var localpart = getLocalPartFromUserId(userId);
                if (localpart) {
                    localpart = localpart.toLocaleLowerCase();
                    userRegex += "\\b" + localpart + "\\b";
                }
            }
            if (displayName) {
                displayName = displayName.toLocaleLowerCase();
                if (userRegex.length > 0) {
                    userRegex += "|";
                }
                userRegex += "\\b" + displayName + "\\b";
            }

            var regexList = [new RegExp(userRegex, 'i')];
            
            // bing word list check
            if (bingWords && bingWords.length > 0) {
                for (var i=0; i<bingWords.length; i++) {
                    var re = RegExp(bingWords[i], 'i');
                    regexList.push(re);
                }
            }
            return this.hasMatch(regexList, content);
        },
    
        hasMatch: function(regExps, content) {
            if (!content || $.type(content) != "string") {
                return false;
            }
            
            if (regExps && regExps.length > 0) {
                for (var i=0; i<regExps.length; i++) {
                    if (content.search(regExps[i]) != -1) {
                        return true;
                    }
                }
            }
            return false;
        },
        
        showNotification: function(title, body, icon, onclick) {
            var notification = new window.Notification(
                title,
                {
                    "body": body,
                    "icon": icon
                }
            );

            if (onclick) {
                notification.onclick = onclick;
            }

            $timeout(function() {
                notification.close();
            }, 5 * 1000);
        }
    };

}]);