From 8cc9b7b54b2438134eaa1b300ec8c22f2a2789f8 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Fri, 16 Nov 2018 13:29:31 +1100 Subject: [PATCH] Added blocked number model and collection. Added blocked number controller. Hooked up BlockedNumberController loading. --- background.html | 1 + js/background.js | 2 + js/blocked_number_controller.js | 72 +++++++++++++++++++++++++++++++++ js/models/blockedNumbers.js | 38 ++++++++++++++++- js/signal_protocol_store.js | 4 +- test/index.html | 1 + 6 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 js/blocked_number_controller.js diff --git a/background.html b/background.html index d17fcf7e4..a37e90fe1 100644 --- a/background.html +++ b/background.html @@ -614,6 +614,7 @@ + diff --git a/js/background.js b/js/background.js index 1c1afbc9d..b5a1df155 100644 --- a/js/background.js +++ b/js/background.js @@ -9,6 +9,7 @@ textsecure, WebAPI Whisper, + BlockedNumberController */ // eslint-disable-next-line func-names @@ -418,6 +419,7 @@ try { await ConversationController.load(); + BlockedNumberController.load(); } catch (error) { window.log.error( 'background.js: ConversationController failed to load:', diff --git a/js/blocked_number_controller.js b/js/blocked_number_controller.js new file mode 100644 index 000000000..24cfedd76 --- /dev/null +++ b/js/blocked_number_controller.js @@ -0,0 +1,72 @@ +/* global , Whisper, storage */ +/* global textsecure: false */ + +/* eslint-disable more/no-then */ + +// eslint-disable-next-line func-names +(function() { + 'use strict'; + + window.Whisper = window.Whisper || {}; + + const blockedNumbers = new Whisper.BlockedNumberCollection(); + window.getBlockedNumbers = () => blockedNumbers; + + window.BlockedNumberController = { + reset() { + blockedNumbers.reset([]); + }, + load() { + window.log.info('BlockedNumberController: starting initial fetch'); + + if (blockedNumbers.length) { + throw new Error('BlockedNumberController: Already loaded!'); + } + + if (!storage) { + throw new Error('BlockedNumberController: Could not load blocked numbers'); + } + + // Add the numbers to the collection + const numbers = storage.getBlockedNumbers(); + blockedNumbers.add( + numbers.map(number => ({ number })) + ); + }, + block(number) { + const ourNumber = textsecure.storage.user.getNumber(); + + // Make sure we don't block ourselves + if (ourNumber === number) { + window.log.info('BlockedNumberController: Cannot block yourself!'); + return null; + } + + storage.addBlockedNumber(number); + + // Make sure we don't add duplicates + const exists = blockedNumbers.getNumber(number); + if (exists) + return exists; + + return blockedNumbers.add({ number }); + }, + unblock(number) { + storage.removeBlockedNumber(number); + + // Make sure we don't add duplicates + const exists = blockedNumbers.getNumber(number); + if (exists) { + blockedNumbers.remove(exists); + return exists; + } + + return null; + }, + isBlocked(number) { + return storage.isBlocked(number); + }, + + }; + })(); + \ No newline at end of file diff --git a/js/models/blockedNumbers.js b/js/models/blockedNumbers.js index 10ef67f0f..7e1f79914 100644 --- a/js/models/blockedNumbers.js +++ b/js/models/blockedNumbers.js @@ -1,9 +1,19 @@ /* global storage, _ */ +/* global _: false */ +/* global Backbone: false */ + +/* global BlockedNumberController: false */ +/* global storage: false */ +/* global Whisper: false */ + +/* eslint-disable more/no-then */ // eslint-disable-next-line func-names (function() { 'use strict'; + window.Whisper = window.Whisper || {}; + const BLOCKED_NUMBERS_ID = 'blocked'; const BLOCKED_GROUPS_ID = 'blocked-groups'; @@ -12,6 +22,7 @@ return _.include(numbers, number); }; + storage.getBlockedNumbers = () => storage.get(BLOCKED_NUMBERS_ID, []); storage.addBlockedNumber = number => { const numbers = storage.get(BLOCKED_NUMBERS_ID, []); if (_.include(numbers, number)) { @@ -54,4 +65,29 @@ window.log.info(`removing group(${groupId} from blocked list`); storage.put(BLOCKED_GROUPS_ID, _.without(groupIds, groupId)); }; -})(); + + Whisper.BlockedNumber = Backbone.Model.extend({ + defaults() { + return { + number: '', + }; + }, + block() { + return BlockedNumberController.block(this.number); + }, + unblock() { + return BlockedNumberController.unblock(this.number); + }, + }); + + Whisper.BlockedNumberCollection = Backbone.Collection.extend({ + model: Whisper.BlockedNumber, + comparator(m) { + return m.get('number'); + }, + getNumber(number) { + return this.model.find(m => m.number === number); + }, + }); + +})(); \ No newline at end of file diff --git a/js/signal_protocol_store.js b/js/signal_protocol_store.js index 760f9c99e..80185d238 100644 --- a/js/signal_protocol_store.js +++ b/js/signal_protocol_store.js @@ -1,5 +1,5 @@ /* global - dcodeIO, Backbone, _, libsignal, textsecure, ConversationController, stringObject */ + dcodeIO, Backbone, _, libsignal, textsecure, ConversationController, stringObject, BlockedNumberController */ /* eslint-disable no-proto */ @@ -955,7 +955,9 @@ await window.storage.fetch(); ConversationController.reset(); + BlockedNumberController.reset(); await ConversationController.load(); + BlockedNumberController.load(); }, async removeAllConfiguration() { await window.Signal.Data.removeAllConfiguration(); diff --git a/test/index.html b/test/index.html index 64964f896..e03ce96cf 100644 --- a/test/index.html +++ b/test/index.html @@ -345,6 +345,7 @@ +