<commit_before>/*global $, Whisper, Backbone, textsecure, extension*/
/*
 * vim: ts=4:sw=4:expandtab
 */

// This script should only be included in background.html
(function () {
    'use strict';

    window.Whisper = window.Whisper || {};

    var conversations = new Whisper.ConversationCollection();
    var inboxCollection = new (Backbone.Collection.extend({
        initialize: function() {
            this.on('change:active_at', this.sort);
            this.on('change:unreadCount', this.updateUnreadCount);

            this.listenTo(conversations, 'add change:active_at', this.addActive);
        },
        comparator: function(model) {
            return -model.get('active_at');
        },
        addActive: function(model) {
            if (model.get('active_at')) {
                this.add(model);
            }
        },
        updateUnreadCount: function(model, count) {
            var prev = model.previous('unreadCount') || 0;
            if (count < prev) { // decreased
                var newUnreadCount = storage.get("unreadCount", 0) - (prev - count);
                setUnreadCount(newUnreadCount);
                storage.put("unreadCount", newUnreadCount);
            }
        }
    }))();

    window.getInboxCollection = function() {
        return inboxCollection;
    };

    window.ConversationController = {
        get: function(id) {
            return conversations.get(id);
        },
        create: function(attrs) {
            var conversation = conversations.add(attrs);
            return conversation;
        },
        findOrCreatePrivateById: function(id) {
            var conversation = conversations.add({ id: id, type: 'private' });
            return new Promise(function(resolve, reject) {
                conversation.fetch().then(function() {
                    resolve(conversation);
                }).fail(function() {
                    var saved = conversation.save(); // false or indexedDBRequest
                    if (saved) {
                        saved.then(function() {
                            resolve(conversation);
                        }).fail(reject);
                    }
                    reject();
                });
            });
        },
        updateInbox: function() {
            conversations.fetchActive();
        }
    };

    ConversationController.updateInbox();
    setUnreadCount(storage.get("unreadCount", 0));

    function setUnreadCount(count) {
        if (count > 0) {
            extension.navigator.setBadgeText(count);
        } else {
            extension.navigator.setBadgeText("");
        }
    }

    window.notifyConversation = function(message) {
        var conversationId = message.get('conversationId');
        var conversation = ConversationController.get(conversationId);
        if (!conversation) {
            conversation = conversations.create({id: conversationId});
            conversation.fetch();
        }
        if (inboxOpened) {
            conversation.fetchMessages();
            extension.windows.drawAttention(inboxWindowId);
        } else if (Whisper.Notifications.isEnabled()) {
            var sender = ConversationController.create({id: message.get('source')});
            conversation.fetch().then(function() {
                sender.fetch().then(function() {
                    sender.getNotificationIcon().then(function(iconUrl) {
                        Whisper.Notifications.add({
                            title    : sender.getTitle(),
                            message  : message.getNotificationText(),
                            iconUrl  : iconUrl,
                            imageUrl : message.getImageUrl(),
                            conversationId: conversation.id
                        });
                    });
                });
            });
        } else {
            openConversation(conversation);
            ConversationController.updateInbox();
        }
    };

    /* Inbox window controller */
    var inboxOpened = false;
    var inboxWindowId = 'inbox';
    window.openInbox = function() {
        if (inboxOpened === false) {
            inboxOpened = true;
            extension.windows.open({
                id: 'inbox',
                url: 'index.html',
                type: 'panel',
                frame: 'none',
                focused: true,
                width: 580,
                height: 440,
                minWidth: 230,
                minHeight: 150
            }, function (windowInfo) {
                inboxWindowId = windowInfo.id;

                windowInfo.onClosed.addListener(function () {
                    inboxWindowId = 0;
                    inboxOpened = false;
                });

                // close the panel if background.html is refreshed
                extension.windows.beforeUnload(function() {<commit_msg>Don't reset inboxWindowId<commit_after>/*global $, Whisper, Backbone, textsecure, extension*/
/*
 * vim: ts=4:sw=4:expandtab
 */

// This script should only be included in background.html
(function () {
    'use strict';

    window.Whisper = window.Whisper || {};

    var conversations = new Whisper.ConversationCollection();
    var inboxCollection = new (Backbone.Collection.extend({
        initialize: function() {
            this.on('change:active_at', this.sort);
            this.on('change:unreadCount', this.updateUnreadCount);

            this.listenTo(conversations, 'add change:active_at', this.addActive);
        },
        comparator: function(model) {
            return -model.get('active_at');
        },
        addActive: function(model) {
            if (model.get('active_at')) {
                this.add(model);
            }
        },
        updateUnreadCount: function(model, count) {
            var prev = model.previous('unreadCount') || 0;
            if (count < prev) { // decreased
                var newUnreadCount = storage.get("unreadCount", 0) - (prev - count);
                setUnreadCount(newUnreadCount);
                storage.put("unreadCount", newUnreadCount);
            }
        }
    }))();

    window.getInboxCollection = function() {
        return inboxCollection;
    };

    window.ConversationController = {
        get: function(id) {
            return conversations.get(id);
        },
        create: function(attrs) {
            var conversation = conversations.add(attrs);
            return conversation;
        },
        findOrCreatePrivateById: function(id) {
            var conversation = conversations.add({ id: id, type: 'private' });
            return new Promise(function(resolve, reject) {
                conversation.fetch().then(function() {
                    resolve(conversation);
                }).fail(function() {
                    var saved = conversation.save(); // false or indexedDBRequest
                    if (saved) {
                        saved.then(function() {
                            resolve(conversation);
                        }).fail(reject);
                    }
                    reject();
                });
            });
        },
        updateInbox: function() {
            conversations.fetchActive();
        }
    };

    ConversationController.updateInbox();
    setUnreadCount(storage.get("unreadCount", 0));

    function setUnreadCount(count) {
        if (count > 0) {
            extension.navigator.setBadgeText(count);
        } else {
            extension.navigator.setBadgeText("");
        }
    }

    window.notifyConversation = function(message) {
        var conversationId = message.get('conversationId');
        var conversation = ConversationController.get(conversationId);
        if (!conversation) {
            conversation = conversations.create({id: conversationId});
            conversation.fetch();
        }
        if (inboxOpened) {
            conversation.fetchMessages();
            extension.windows.drawAttention(inboxWindowId);
        } else if (Whisper.Notifications.isEnabled()) {
            var sender = ConversationController.create({id: message.get('source')});
            conversation.fetch().then(function() {
                sender.fetch().then(function() {
                    sender.getNotificationIcon().then(function(iconUrl) {
                        Whisper.Notifications.add({
                            title    : sender.getTitle(),
                            message  : message.getNotificationText(),
                            iconUrl  : iconUrl,
                            imageUrl : message.getImageUrl(),
                            conversationId: conversation.id
                        });
                    });
                });
            });
        } else {
            openConversation(conversation);
            ConversationController.updateInbox();
        }
    };

    /* Inbox window controller */
    var inboxOpened = false;
    var inboxWindowId = 'inbox';
    window.openInbox = function() {
        if (inboxOpened === false) {
            inboxOpened = true;
            extension.windows.open({
                id: 'inbox',
                url: 'index.html',
                type: 'panel',
                frame: 'none',
                focused: true,
                width: 580,
                height: 440,
                minWidth: 230,
                minHeight: 150
            }, function (windowInfo) {
                inboxWindowId = windowInfo.id;

                windowInfo.onClosed.addListener(function () {
                    inboxOpened = false;
                });

                // close the panel if background.html is refreshed
                extension.windows.beforeUnload(function() {
{'fraction_non_alphanumeric': 0.11182108626198083, 'fraction_numerical': 0.003813253632897042, 'mean_word_length': 1.545645330535152, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1166860', 'n_tokens_mistral': 2459, 'n_tokens_neox': 2336, 'n_words': 438}