Added blocked number model and collection.

Added blocked number controller.
Hooked up BlockedNumberController loading.
pull/47/head
Mikunj 6 years ago
parent 78d39ac177
commit 8cc9b7b54b

@ -614,6 +614,7 @@
<script type='text/javascript' src='js/registration.js'></script>
<script type='text/javascript' src='js/expire.js'></script>
<script type='text/javascript' src='js/conversation_controller.js'></script>
<script type='text/javascript' src='js/blocked_number_controller.js'></script>
<script type='text/javascript' src='js/views/react_wrapper_view.js'></script>
<script type='text/javascript' src='js/views/whisper_view.js'></script>

@ -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:',

@ -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);
},
};
})();

@ -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);
},
});
})();

@ -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();

@ -345,6 +345,7 @@
<script type="text/javascript" src="../js/models/conversations.js" data-cover></script>
<script type="text/javascript" src="../js/models/blockedNumbers.js" data-cover></script>
<script type="text/javascript" src="../js/conversation_controller.js" data-cover></script>
<script type='text/javascript' src='../js/blocked_number_controller.js'></script>
<script type="text/javascript" src="../js/keychange_listener.js" data-cover></script>
<script type='text/javascript' src='../js/expiring_messages.js' data-cover></script>
<script type='text/javascript' src='../js/notifications.js' data-cover></script>

Loading…
Cancel
Save