2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-11-13 22:35:37 +00:00
|
|
|
*/
|
2014-05-17 04:48:46 +00:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
2014-11-13 05:46:57 +00:00
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
2015-02-08 02:18:53 +00:00
|
|
|
// TODO: Factor out private and group subclasses of Conversation
|
|
|
|
|
2015-09-10 07:46:50 +00:00
|
|
|
var COLORS = [
|
2016-08-29 06:57:33 +00:00
|
|
|
'red',
|
|
|
|
'pink',
|
|
|
|
'purple',
|
|
|
|
'deep_purple',
|
|
|
|
'indigo',
|
|
|
|
'blue',
|
|
|
|
'light_blue',
|
|
|
|
'cyan',
|
|
|
|
'teal',
|
|
|
|
'green',
|
|
|
|
'light_green',
|
|
|
|
'orange',
|
|
|
|
'deep_orange',
|
|
|
|
'amber',
|
|
|
|
'blue_grey',
|
2015-09-10 07:46:50 +00:00
|
|
|
];
|
|
|
|
|
2015-02-08 02:18:53 +00:00
|
|
|
Whisper.Conversation = Backbone.Model.extend({
|
2014-11-13 22:35:37 +00:00
|
|
|
database: Whisper.Database,
|
|
|
|
storeName: 'conversations',
|
2014-05-17 04:48:46 +00:00
|
|
|
defaults: function() {
|
2016-02-24 20:03:25 +00:00
|
|
|
return { unreadCount : 0 };
|
2014-05-17 04:48:46 +00:00
|
|
|
},
|
|
|
|
|
2017-01-04 03:37:56 +00:00
|
|
|
handleMessageError: function(message, errors) {
|
|
|
|
this.trigger('messageError', message, errors);
|
|
|
|
},
|
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
initialize: function() {
|
2015-09-08 00:22:59 +00:00
|
|
|
this.contactCollection = new Backbone.Collection();
|
2015-03-12 00:49:01 +00:00
|
|
|
this.messageCollection = new Whisper.MessageCollection([], {
|
|
|
|
conversation: this
|
|
|
|
});
|
2015-03-17 22:06:21 +00:00
|
|
|
|
2017-01-04 03:37:56 +00:00
|
|
|
this.messageCollection.on('change:errors', this.handleMessageError, this);
|
|
|
|
|
2015-03-17 22:06:21 +00:00
|
|
|
this.on('change:avatar', this.updateAvatarUrl);
|
2015-03-18 00:10:18 +00:00
|
|
|
this.on('destroy', this.revokeAvatarUrl);
|
2016-09-18 06:55:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
addKeyChange: function(id) {
|
2016-11-17 17:50:51 +00:00
|
|
|
console.log('adding key change advisory for', this.id, this.get('timestamp'));
|
2017-01-23 04:26:10 +00:00
|
|
|
var timestamp = Date.now();
|
|
|
|
var message = new Whisper.Message({
|
2016-09-18 06:55:05 +00:00
|
|
|
conversationId : this.id,
|
|
|
|
type : 'keychange',
|
2016-10-12 02:10:21 +00:00
|
|
|
sent_at : this.get('timestamp'),
|
2017-01-23 04:26:10 +00:00
|
|
|
received_at : timestamp,
|
2016-09-18 06:55:05 +00:00
|
|
|
key_changed : id
|
|
|
|
});
|
2017-01-23 04:26:10 +00:00
|
|
|
message.save().then(this.trigger.bind(this,'newmessage', message));
|
2016-02-22 05:37:47 +00:00
|
|
|
},
|
|
|
|
|
2016-09-21 23:26:42 +00:00
|
|
|
onReadMessage: function(message) {
|
|
|
|
if (this.messageCollection.get(message.id)) {
|
|
|
|
this.messageCollection.get(message.id).fetch();
|
|
|
|
}
|
|
|
|
|
2017-06-07 18:24:21 +00:00
|
|
|
// We mark as read everything older than this message - to clean up old stuff
|
|
|
|
// still marked unread in the database. If the user generally doesn't read in
|
|
|
|
// the desktop app, so the desktop app only gets read receipts, we can very
|
|
|
|
// easily end up with messages never marked as read (our previous early read
|
|
|
|
// receipt handling, read receipts never sent because app was offline)
|
2017-06-07 19:06:23 +00:00
|
|
|
|
|
|
|
// We queue it because we often get a whole lot of read receipts at once, and
|
|
|
|
// their markRead calls could very easily overlap given the async pull from DB.
|
|
|
|
|
|
|
|
// Lastly, we don't send read receipts for any message marked read due to a read
|
|
|
|
// receipt. That's a notification explosion we don't need.
|
|
|
|
this.queueJob(function() {
|
|
|
|
return this.markRead(message.get('received_at'), {sendReadReceipts: false});
|
|
|
|
}.bind(this));
|
2016-02-22 05:37:47 +00:00
|
|
|
},
|
|
|
|
|
2017-06-01 22:47:55 +00:00
|
|
|
getUnread: function() {
|
2016-02-22 05:37:47 +00:00
|
|
|
var conversationId = this.id;
|
|
|
|
var unreadMessages = new Whisper.MessageCollection();
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
return unreadMessages.fetch({
|
|
|
|
index: {
|
|
|
|
// 'unread' index
|
2016-02-26 20:25:32 +00:00
|
|
|
name : 'unread',
|
|
|
|
lower : [conversationId],
|
|
|
|
upper : [conversationId, Number.MAX_VALUE],
|
2016-02-22 05:37:47 +00:00
|
|
|
}
|
|
|
|
}).always(function() {
|
2017-06-01 22:47:55 +00:00
|
|
|
resolve(unreadMessages);
|
2016-02-22 05:37:47 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
},
|
|
|
|
|
2014-05-17 04:48:46 +00:00
|
|
|
validate: function(attributes, options) {
|
2015-06-03 17:29:20 +00:00
|
|
|
var required = ['id', 'type'];
|
2015-01-28 12:19:58 +00:00
|
|
|
var missing = _.filter(required, function(attr) { return !attributes[attr]; });
|
|
|
|
if (missing.length) { return "Conversation must have " + missing; }
|
|
|
|
|
2015-02-08 00:24:56 +00:00
|
|
|
if (attributes.type !== 'private' && attributes.type !== 'group') {
|
|
|
|
return "Invalid conversation type: " + attributes.type;
|
|
|
|
}
|
2015-10-16 20:00:38 +00:00
|
|
|
|
2015-12-05 01:38:41 +00:00
|
|
|
var error = this.validateNumber();
|
|
|
|
if (error) { return error; }
|
|
|
|
|
|
|
|
this.updateTokens();
|
2015-12-01 23:29:39 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
validateNumber: function() {
|
2015-12-05 01:38:41 +00:00
|
|
|
if (this.isPrivate()) {
|
|
|
|
var regionCode = storage.get('regionCode');
|
|
|
|
var number = libphonenumber.util.parseNumber(this.id, regionCode);
|
|
|
|
if (number.isValidNumber) {
|
|
|
|
this.set({ id: number.e164 });
|
2015-12-01 23:29:39 +00:00
|
|
|
} else {
|
2015-12-05 01:38:41 +00:00
|
|
|
return number.error || "Invalid phone number";
|
2015-12-01 23:29:39 +00:00
|
|
|
}
|
2015-10-16 20:00:38 +00:00
|
|
|
}
|
2015-10-15 19:10:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
updateTokens: function() {
|
|
|
|
var tokens = [];
|
|
|
|
var name = this.get('name');
|
|
|
|
if (typeof name === 'string') {
|
2015-12-05 01:38:41 +00:00
|
|
|
tokens.push(name.toLowerCase());
|
|
|
|
tokens = tokens.concat(name.trim().toLowerCase().split(/[\s\-_\(\)\+]+/));
|
2015-10-15 19:10:03 +00:00
|
|
|
}
|
2015-09-10 00:47:45 +00:00
|
|
|
if (this.isPrivate()) {
|
2015-12-05 01:38:41 +00:00
|
|
|
var regionCode = storage.get('regionCode');
|
|
|
|
var number = libphonenumber.util.parseNumber(this.id, regionCode);
|
|
|
|
tokens.push(
|
|
|
|
number.nationalNumber,
|
|
|
|
number.countryCode + number.nationalNumber
|
|
|
|
);
|
2015-01-28 12:19:58 +00:00
|
|
|
}
|
2015-10-15 19:10:03 +00:00
|
|
|
this.set({tokens: tokens});
|
2014-05-17 04:48:46 +00:00
|
|
|
},
|
|
|
|
|
2016-03-15 20:09:06 +00:00
|
|
|
queueJob: function(callback) {
|
|
|
|
var previous = this.pending || Promise.resolve();
|
|
|
|
var current = this.pending = previous.then(callback, callback);
|
|
|
|
|
|
|
|
current.then(function() {
|
|
|
|
if (this.pending === current) {
|
|
|
|
delete this.pending;
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
return current;
|
|
|
|
},
|
|
|
|
|
2014-12-20 08:36:44 +00:00
|
|
|
sendMessage: function(body, attachments) {
|
2016-03-15 20:09:06 +00:00
|
|
|
this.queueJob(function() {
|
|
|
|
var now = Date.now();
|
|
|
|
var message = this.messageCollection.add({
|
|
|
|
body : body,
|
|
|
|
conversationId : this.id,
|
|
|
|
type : 'outgoing',
|
|
|
|
attachments : attachments,
|
|
|
|
sent_at : now,
|
2016-09-28 23:54:05 +00:00
|
|
|
received_at : now,
|
|
|
|
expireTimer : this.get('expireTimer')
|
2016-03-15 20:09:06 +00:00
|
|
|
});
|
|
|
|
if (this.isPrivate()) {
|
|
|
|
message.set({destination: this.id});
|
|
|
|
}
|
|
|
|
message.save();
|
2014-10-26 07:29:01 +00:00
|
|
|
|
2016-03-15 20:09:06 +00:00
|
|
|
this.save({
|
|
|
|
active_at : now,
|
|
|
|
timestamp : now,
|
|
|
|
lastMessage : message.getNotificationText()
|
|
|
|
});
|
2014-08-11 06:34:29 +00:00
|
|
|
|
2016-03-15 20:09:06 +00:00
|
|
|
var sendFunc;
|
|
|
|
if (this.get('type') == 'private') {
|
|
|
|
sendFunc = textsecure.messaging.sendMessageToNumber;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendFunc = textsecure.messaging.sendMessageToGroup;
|
|
|
|
}
|
2016-09-28 23:54:05 +00:00
|
|
|
message.send(sendFunc(this.get('id'), body, attachments, now, this.get('expireTimer')));
|
2016-03-15 20:09:06 +00:00
|
|
|
}.bind(this));
|
2014-05-17 04:48:46 +00:00
|
|
|
},
|
|
|
|
|
2016-09-29 22:26:11 +00:00
|
|
|
updateLastMessage: function() {
|
2017-01-25 08:42:06 +00:00
|
|
|
var collection = new Whisper.MessageCollection();
|
|
|
|
return collection.fetchConversation(this.id, 1).then(function() {
|
|
|
|
var lastMessage = collection.at(0);
|
|
|
|
if (lastMessage) {
|
2017-02-22 03:51:33 +00:00
|
|
|
this.set({
|
2017-01-25 08:42:06 +00:00
|
|
|
lastMessage : lastMessage.getNotificationText(),
|
|
|
|
timestamp : lastMessage.get('sent_at')
|
|
|
|
});
|
|
|
|
} else {
|
2017-02-22 03:51:33 +00:00
|
|
|
this.set({ lastMessage: '', timestamp: null });
|
|
|
|
}
|
|
|
|
if (this.hasChanged('lastMessage') || this.hasChanged('timestamp')) {
|
|
|
|
this.save();
|
2017-01-25 08:42:06 +00:00
|
|
|
}
|
|
|
|
}.bind(this));
|
2016-09-29 22:26:11 +00:00
|
|
|
},
|
|
|
|
|
2017-01-03 12:52:29 +00:00
|
|
|
updateExpirationTimer: function(expireTimer, source, received_at) {
|
2017-02-21 03:12:53 +00:00
|
|
|
if (!expireTimer) { expireTimer = null; }
|
2017-01-03 12:52:29 +00:00
|
|
|
source = source || textsecure.storage.user.getNumber();
|
|
|
|
var timestamp = received_at || Date.now();
|
2016-10-04 08:43:11 +00:00
|
|
|
this.save({ expireTimer: expireTimer });
|
2016-09-27 06:15:20 +00:00
|
|
|
var message = this.messageCollection.add({
|
2016-09-28 23:47:57 +00:00
|
|
|
conversationId : this.id,
|
2017-01-03 13:14:55 +00:00
|
|
|
type : received_at ? 'incoming' : 'outgoing',
|
2017-01-03 12:52:29 +00:00
|
|
|
sent_at : timestamp,
|
|
|
|
received_at : timestamp,
|
2016-09-28 23:47:57 +00:00
|
|
|
flags : textsecure.protobuf.DataMessage.Flags.EXPIRATION_TIMER_UPDATE,
|
|
|
|
expirationTimerUpdate : {
|
2016-10-04 08:43:11 +00:00
|
|
|
expireTimer : expireTimer,
|
2016-09-27 06:15:20 +00:00
|
|
|
source : source
|
|
|
|
}
|
|
|
|
});
|
2016-10-02 23:42:46 +00:00
|
|
|
if (this.isPrivate()) {
|
|
|
|
message.set({destination: this.id});
|
|
|
|
}
|
2016-09-27 06:15:20 +00:00
|
|
|
message.save();
|
2017-01-03 13:14:55 +00:00
|
|
|
if (message.isOutgoing()) { // outgoing update, send it to the number/group
|
2017-01-03 12:52:29 +00:00
|
|
|
var sendFunc;
|
|
|
|
if (this.get('type') == 'private') {
|
|
|
|
sendFunc = textsecure.messaging.sendExpirationTimerUpdateToNumber;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sendFunc = textsecure.messaging.sendExpirationTimerUpdateToGroup;
|
|
|
|
}
|
|
|
|
message.send(sendFunc(this.get('id'), this.get('expireTimer'), message.get('sent_at')));
|
2016-09-29 00:10:39 +00:00
|
|
|
}
|
2017-01-03 12:52:29 +00:00
|
|
|
return message;
|
2016-09-28 23:47:57 +00:00
|
|
|
},
|
2016-09-27 06:15:20 +00:00
|
|
|
|
2016-02-19 01:27:57 +00:00
|
|
|
isSearchable: function() {
|
|
|
|
return !this.get('left') || !!this.get('lastMessage');
|
|
|
|
},
|
|
|
|
|
2015-02-13 04:36:44 +00:00
|
|
|
endSession: function() {
|
2015-09-10 00:47:45 +00:00
|
|
|
if (this.isPrivate()) {
|
2015-03-24 02:08:05 +00:00
|
|
|
var now = Date.now();
|
2015-09-28 20:33:26 +00:00
|
|
|
var message = this.messageCollection.create({
|
2015-03-24 02:08:05 +00:00
|
|
|
conversationId : this.id,
|
|
|
|
type : 'outgoing',
|
|
|
|
sent_at : now,
|
|
|
|
received_at : now,
|
2016-02-23 20:27:49 +00:00
|
|
|
destination : this.id,
|
2015-06-01 21:08:21 +00:00
|
|
|
flags : textsecure.protobuf.DataMessage.Flags.END_SESSION
|
2015-07-16 18:05:47 +00:00
|
|
|
});
|
2015-12-11 20:05:50 +00:00
|
|
|
message.send(textsecure.messaging.closeSession(this.id, now));
|
2015-02-13 04:36:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2015-09-22 02:12:06 +00:00
|
|
|
updateGroup: function(group_update) {
|
|
|
|
if (this.isPrivate()) {
|
|
|
|
throw new Error("Called update group on private conversation");
|
|
|
|
}
|
|
|
|
if (group_update === undefined) {
|
|
|
|
group_update = this.pick(['name', 'avatar', 'members']);
|
|
|
|
}
|
|
|
|
var now = Date.now();
|
2015-09-28 20:33:26 +00:00
|
|
|
var message = this.messageCollection.create({
|
2015-09-22 02:12:06 +00:00
|
|
|
conversationId : this.id,
|
|
|
|
type : 'outgoing',
|
|
|
|
sent_at : now,
|
|
|
|
received_at : now,
|
|
|
|
group_update : group_update
|
|
|
|
});
|
2015-09-28 20:33:26 +00:00
|
|
|
message.send(textsecure.messaging.updateGroup(
|
2015-09-22 02:12:06 +00:00
|
|
|
this.id,
|
|
|
|
this.get('name'),
|
|
|
|
this.get('avatar'),
|
|
|
|
this.get('members')
|
2015-09-28 20:33:26 +00:00
|
|
|
));
|
2015-09-22 02:12:06 +00:00
|
|
|
},
|
|
|
|
|
2015-02-13 04:36:44 +00:00
|
|
|
leaveGroup: function() {
|
2015-03-24 02:08:05 +00:00
|
|
|
var now = Date.now();
|
2015-02-13 04:36:44 +00:00
|
|
|
if (this.get('type') === 'group') {
|
2016-02-11 22:12:09 +00:00
|
|
|
this.save({left: true});
|
2015-09-28 20:33:26 +00:00
|
|
|
var message = this.messageCollection.create({
|
2015-03-24 02:08:05 +00:00
|
|
|
group_update: { left: 'You' },
|
|
|
|
conversationId : this.id,
|
|
|
|
type : 'outgoing',
|
|
|
|
sent_at : now,
|
|
|
|
received_at : now
|
2015-09-18 20:39:22 +00:00
|
|
|
});
|
2015-09-28 20:33:26 +00:00
|
|
|
message.send(textsecure.messaging.leaveGroup(this.id));
|
2015-02-13 04:36:44 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-06-07 19:06:23 +00:00
|
|
|
markRead: function(newestUnreadDate, options) {
|
|
|
|
options = options || {};
|
|
|
|
_.defaults(options, {sendReadReceipts: true});
|
|
|
|
|
2017-06-07 19:20:25 +00:00
|
|
|
var conversationId = this.id;
|
|
|
|
Whisper.Notifications.remove(Whisper.Notifications.where({
|
|
|
|
conversationId: conversationId
|
|
|
|
}));
|
|
|
|
|
2017-06-07 22:16:01 +00:00
|
|
|
return this.getUnread().then(function(unreadMessages) {
|
2017-06-07 19:20:25 +00:00
|
|
|
var oldUnread = unreadMessages.filter(function(message) {
|
|
|
|
return message.get('received_at') <= newestUnreadDate;
|
|
|
|
});
|
|
|
|
|
|
|
|
var read = _.map(oldUnread, function(m) {
|
|
|
|
if (this.messageCollection.get(m.id)) {
|
|
|
|
m = this.messageCollection.get(m.id);
|
|
|
|
} else {
|
|
|
|
console.log('Marked a message as read in the database, but ' +
|
|
|
|
'it was not in messageCollection.');
|
2016-02-26 20:35:02 +00:00
|
|
|
}
|
2017-06-07 19:20:25 +00:00
|
|
|
m.markRead();
|
|
|
|
return {
|
|
|
|
sender : m.get('source'),
|
|
|
|
timestamp : m.get('sent_at')
|
|
|
|
};
|
2016-09-21 00:19:51 +00:00
|
|
|
}.bind(this));
|
2017-06-07 19:20:25 +00:00
|
|
|
|
|
|
|
var unreadCount = unreadMessages.length - read.length;
|
|
|
|
this.save({ unreadCount: unreadCount });
|
|
|
|
|
|
|
|
if (read.length && options.sendReadReceipts) {
|
|
|
|
console.log('Sending', read.length, 'read receipts');
|
|
|
|
textsecure.messaging.syncReadMessages(read);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2015-03-11 19:06:19 +00:00
|
|
|
},
|
|
|
|
|
2015-07-07 23:03:12 +00:00
|
|
|
fetchMessages: function() {
|
2015-09-08 00:22:20 +00:00
|
|
|
if (!this.id) { return false; }
|
2017-05-19 21:06:56 +00:00
|
|
|
return this.messageCollection.fetchConversation(this.id, null, this.get('unreadCount'));
|
2014-12-12 03:41:40 +00:00
|
|
|
},
|
|
|
|
|
2015-02-25 00:02:33 +00:00
|
|
|
fetchContacts: function(options) {
|
2015-09-14 03:59:51 +00:00
|
|
|
return new Promise(function(resolve) {
|
|
|
|
if (this.isPrivate()) {
|
|
|
|
this.contactCollection.reset([this]);
|
|
|
|
resolve();
|
|
|
|
} else {
|
|
|
|
var promises = [];
|
|
|
|
var members = this.get('members') || [];
|
2016-11-16 21:25:36 +00:00
|
|
|
this.contactCollection.reset(
|
2015-09-14 03:59:51 +00:00
|
|
|
members.map(function(number) {
|
|
|
|
var c = ConversationController.create({
|
|
|
|
id : number,
|
|
|
|
type : 'private'
|
|
|
|
});
|
|
|
|
promises.push(new Promise(function(resolve) {
|
|
|
|
c.fetch().always(resolve);
|
|
|
|
}));
|
|
|
|
return c;
|
|
|
|
}.bind(this))
|
|
|
|
);
|
|
|
|
resolve(Promise.all(promises));
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2015-02-25 00:02:33 +00:00
|
|
|
},
|
|
|
|
|
2014-12-02 23:47:28 +00:00
|
|
|
destroyMessages: function() {
|
2016-01-27 20:42:27 +00:00
|
|
|
this.messageCollection.fetch({
|
|
|
|
index: {
|
|
|
|
// 'conversation' index on [conversationId, received_at]
|
|
|
|
name : 'conversation',
|
|
|
|
lower : [this.id],
|
|
|
|
upper : [this.id, Number.MAX_VALUE],
|
|
|
|
}
|
|
|
|
}).then(function() {
|
|
|
|
var models = this.messageCollection.models;
|
|
|
|
this.messageCollection.reset([]);
|
|
|
|
_.each(models, function(message) { message.destroy(); });
|
2016-03-25 17:39:36 +00:00
|
|
|
this.save({lastMessage: null, timestamp: null}); // archive
|
2016-01-27 20:42:27 +00:00
|
|
|
}.bind(this));
|
2015-01-24 20:36:04 +00:00
|
|
|
},
|
|
|
|
|
2016-03-18 20:09:45 +00:00
|
|
|
getName: function() {
|
|
|
|
if (this.isPrivate()) {
|
|
|
|
return this.get('name');
|
|
|
|
} else {
|
|
|
|
return this.get('name') || 'Unknown group';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-01-24 20:36:04 +00:00
|
|
|
getTitle: function() {
|
2015-03-19 20:49:09 +00:00
|
|
|
if (this.isPrivate()) {
|
2015-12-04 23:09:53 +00:00
|
|
|
return this.get('name') || this.getNumber();
|
2015-03-19 20:49:09 +00:00
|
|
|
} else {
|
|
|
|
return this.get('name') || 'Unknown group';
|
|
|
|
}
|
2015-02-04 19:23:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getNumber: function() {
|
2015-12-04 23:09:53 +00:00
|
|
|
if (!this.isPrivate()) {
|
2015-02-04 19:23:00 +00:00
|
|
|
return '';
|
|
|
|
}
|
2015-12-04 23:09:53 +00:00
|
|
|
var number = this.id;
|
|
|
|
try {
|
|
|
|
var parsedNumber = libphonenumber.parse(number);
|
|
|
|
var regionCode = libphonenumber.getRegionCodeForNumber(parsedNumber);
|
|
|
|
if (regionCode === storage.get('regionCode')) {
|
|
|
|
return libphonenumber.format(parsedNumber, libphonenumber.PhoneNumberFormat.NATIONAL);
|
|
|
|
} else {
|
|
|
|
return libphonenumber.format(parsedNumber, libphonenumber.PhoneNumberFormat.INTERNATIONAL);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
return number;
|
|
|
|
}
|
2015-02-25 00:02:33 +00:00
|
|
|
},
|
2015-02-24 00:23:22 +00:00
|
|
|
|
2015-02-25 00:02:33 +00:00
|
|
|
isPrivate: function() {
|
|
|
|
return this.get('type') === 'private';
|
2015-03-17 22:06:21 +00:00
|
|
|
},
|
|
|
|
|
2015-03-18 00:10:18 +00:00
|
|
|
revokeAvatarUrl: function() {
|
2015-03-17 22:06:21 +00:00
|
|
|
if (this.avatarUrl) {
|
|
|
|
URL.revokeObjectURL(this.avatarUrl);
|
|
|
|
this.avatarUrl = null;
|
|
|
|
}
|
2015-03-18 00:10:18 +00:00
|
|
|
},
|
|
|
|
|
2015-06-09 19:03:28 +00:00
|
|
|
updateAvatarUrl: function(silent) {
|
2015-03-18 00:10:18 +00:00
|
|
|
this.revokeAvatarUrl();
|
2015-03-17 22:06:21 +00:00
|
|
|
var avatar = this.get('avatar');
|
|
|
|
if (avatar) {
|
|
|
|
this.avatarUrl = URL.createObjectURL(
|
|
|
|
new Blob([avatar.data], {type: avatar.contentType})
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.avatarUrl = null;
|
|
|
|
}
|
2015-06-09 19:03:28 +00:00
|
|
|
if (!silent) {
|
|
|
|
this.trigger('change');
|
|
|
|
}
|
2015-03-17 22:06:21 +00:00
|
|
|
},
|
2016-09-11 22:03:05 +00:00
|
|
|
getColor: function() {
|
2016-03-18 20:09:45 +00:00
|
|
|
var title = this.get('name');
|
2016-09-03 21:36:56 +00:00
|
|
|
var color = this.get('color');
|
|
|
|
if (!color) {
|
|
|
|
if (this.isPrivate()) {
|
|
|
|
if (title) {
|
|
|
|
color = COLORS[Math.abs(this.hashCode()) % 15];
|
|
|
|
} else {
|
|
|
|
color = 'grey';
|
|
|
|
}
|
2016-03-21 20:02:34 +00:00
|
|
|
} else {
|
2016-09-03 21:36:56 +00:00
|
|
|
color = 'default';
|
2016-03-21 20:02:34 +00:00
|
|
|
}
|
2016-03-18 20:09:45 +00:00
|
|
|
}
|
2016-09-11 22:03:05 +00:00
|
|
|
return color;
|
|
|
|
},
|
|
|
|
getAvatar: function() {
|
|
|
|
if (this.avatarUrl === undefined) {
|
|
|
|
this.updateAvatarUrl(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
var title = this.get('name');
|
|
|
|
var color = this.getColor();
|
2016-03-18 20:09:45 +00:00
|
|
|
|
2015-06-19 00:05:00 +00:00
|
|
|
if (this.avatarUrl) {
|
2016-03-18 20:09:45 +00:00
|
|
|
return { url: this.avatarUrl, color: color };
|
2015-06-19 00:05:00 +00:00
|
|
|
} else if (this.isPrivate()) {
|
2016-03-21 22:37:53 +00:00
|
|
|
return {
|
|
|
|
color: color,
|
|
|
|
content: title ? title.trim()[0] : '#'
|
|
|
|
};
|
2015-06-19 00:05:00 +00:00
|
|
|
} else {
|
2016-03-18 20:09:45 +00:00
|
|
|
return { url: '/images/group_default.png', color: color };
|
2015-06-19 00:05:00 +00:00
|
|
|
}
|
2015-02-24 00:23:22 +00:00
|
|
|
},
|
|
|
|
|
2015-09-22 22:52:33 +00:00
|
|
|
getNotificationIcon: function() {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
var avatar = this.getAvatar();
|
|
|
|
if (avatar.url) {
|
|
|
|
resolve(avatar.url);
|
|
|
|
} else {
|
|
|
|
resolve(new Whisper.IdenticonSVGView(avatar).getDataUrl());
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2015-07-31 18:14:43 +00:00
|
|
|
resolveConflicts: function(conflict) {
|
|
|
|
var number = conflict.number;
|
|
|
|
var identityKey = conflict.identityKey;
|
2015-02-18 02:03:05 +00:00
|
|
|
if (this.isPrivate()) {
|
|
|
|
number = this.id;
|
2015-03-24 01:36:38 +00:00
|
|
|
} else if (!_.include(this.get('members'), number)) {
|
2015-02-18 02:03:05 +00:00
|
|
|
throw 'Tried to resolve conflicts for a unknown group member';
|
2015-02-24 00:23:22 +00:00
|
|
|
}
|
|
|
|
|
2015-02-18 02:03:05 +00:00
|
|
|
if (!this.messageCollection.hasKeyConflicts()) {
|
|
|
|
throw 'No conflicts to resolve';
|
2015-02-24 00:23:22 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 21:50:52 +00:00
|
|
|
return textsecure.storage.protocol.saveIdentity(number, identityKey).then(function() {
|
|
|
|
var promise = Promise.resolve();
|
|
|
|
var conflicts = this.messageCollection.filter(function(message) {
|
|
|
|
return message.hasKeyConflict(number);
|
|
|
|
});
|
|
|
|
// group incoming & outgoing
|
|
|
|
conflicts = _.groupBy(conflicts, function(m) { return m.get('type'); });
|
|
|
|
// sort each group by date and concatenate outgoing after incoming
|
|
|
|
conflicts = _.flatten([
|
|
|
|
_.sortBy(conflicts.incoming, function(m) { return m.get('received_at'); }),
|
|
|
|
_.sortBy(conflicts.outgoing, function(m) { return m.get('received_at'); }),
|
|
|
|
]).forEach(function(message) {
|
|
|
|
var resolveConflict = function() {
|
|
|
|
return message.resolveConflict(number);
|
|
|
|
};
|
|
|
|
promise = promise.then(resolveConflict, resolveConflict);
|
|
|
|
});
|
|
|
|
return promise;
|
2015-04-09 21:58:26 +00:00
|
|
|
}.bind(this));
|
2015-06-19 00:05:00 +00:00
|
|
|
},
|
2015-11-07 22:11:13 +00:00
|
|
|
notify: function(message) {
|
|
|
|
if (!message.isIncoming()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (window.isOpen() && window.isFocused()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
window.drawAttention();
|
|
|
|
var sender = ConversationController.create({
|
|
|
|
id: message.get('source'), type: 'private'
|
|
|
|
});
|
|
|
|
var conversationId = this.id;
|
|
|
|
sender.fetch().then(function() {
|
|
|
|
sender.getNotificationIcon().then(function(iconUrl) {
|
2016-07-29 01:09:09 +00:00
|
|
|
console.log('adding notification');
|
2015-11-07 22:11:13 +00:00
|
|
|
Whisper.Notifications.add({
|
|
|
|
title : sender.getTitle(),
|
|
|
|
message : message.getNotificationText(),
|
|
|
|
iconUrl : iconUrl,
|
|
|
|
imageUrl : message.getImageUrl(),
|
2016-03-08 20:02:47 +00:00
|
|
|
conversationId : conversationId,
|
|
|
|
messageId : message.id
|
2015-11-07 22:11:13 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2015-06-19 00:05:00 +00:00
|
|
|
hashCode: function() {
|
|
|
|
if (this.hash === undefined) {
|
|
|
|
var string = this.getTitle() || '';
|
|
|
|
if (string.length === 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
var hash = 0;
|
|
|
|
for (var i = 0; i < string.length; i++) {
|
|
|
|
hash = ((hash<<5)-hash) + string.charCodeAt(i);
|
|
|
|
hash = hash & hash; // Convert to 32bit integer
|
|
|
|
}
|
|
|
|
|
|
|
|
this.hash = hash;
|
|
|
|
}
|
|
|
|
return this.hash;
|
2014-11-16 23:30:40 +00:00
|
|
|
}
|
2014-05-17 04:48:46 +00:00
|
|
|
});
|
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
Whisper.ConversationCollection = Backbone.Collection.extend({
|
|
|
|
database: Whisper.Database,
|
|
|
|
storeName: 'conversations',
|
2015-02-08 02:18:53 +00:00
|
|
|
model: Whisper.Conversation,
|
2014-10-18 14:08:57 +00:00
|
|
|
|
|
|
|
comparator: function(m) {
|
|
|
|
return -m.get('timestamp');
|
|
|
|
},
|
|
|
|
|
2014-11-13 22:35:37 +00:00
|
|
|
destroyAll: function () {
|
|
|
|
return Promise.all(this.models.map(function(m) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
m.destroy().then(resolve).fail(reject);
|
|
|
|
});
|
|
|
|
}));
|
2014-12-20 01:15:57 +00:00
|
|
|
},
|
|
|
|
|
2015-10-15 19:10:03 +00:00
|
|
|
search: function(query) {
|
|
|
|
query = query.trim().toLowerCase();
|
|
|
|
if (query.length > 0) {
|
2015-12-09 19:43:26 +00:00
|
|
|
query = query.replace(/[-.\(\)]*/g,'').replace(/^\+(\d*)$/, '$1');
|
2015-10-15 19:10:03 +00:00
|
|
|
var lastCharCode = query.charCodeAt(query.length - 1);
|
|
|
|
var nextChar = String.fromCharCode(lastCharCode + 1);
|
|
|
|
var upper = query.slice(0, -1) + nextChar;
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
this.fetch({
|
|
|
|
index: {
|
|
|
|
name: 'search', // 'search' index on tokens array
|
|
|
|
lower: query,
|
2015-12-31 22:05:41 +00:00
|
|
|
upper: upper,
|
|
|
|
excludeUpper: true
|
2015-10-15 19:10:03 +00:00
|
|
|
}
|
|
|
|
}).always(resolve);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-28 00:11:42 +00:00
|
|
|
fetchAlphabetical: function() {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
this.fetch({
|
|
|
|
index: {
|
|
|
|
name: 'search', // 'search' index on tokens array
|
|
|
|
},
|
|
|
|
limit: 100
|
|
|
|
}).always(resolve);
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2014-12-20 01:15:57 +00:00
|
|
|
fetchGroups: function(number) {
|
2016-04-11 22:06:06 +00:00
|
|
|
return new Promise(function(resolve) {
|
|
|
|
this.fetch({
|
|
|
|
index: {
|
|
|
|
name: 'group',
|
|
|
|
only: number
|
|
|
|
}
|
|
|
|
}).always(resolve);
|
|
|
|
}.bind(this));
|
2015-05-26 20:28:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fetchActive: function() {
|
|
|
|
// Ensures all active conversations are included in this collection,
|
|
|
|
// and updates their attributes, but removes nothing.
|
|
|
|
return this.fetch({
|
|
|
|
index: {
|
|
|
|
name: 'inbox', // 'inbox' index on active_at
|
|
|
|
order: 'desc' // ORDER timestamp DESC
|
|
|
|
// TODO pagination/infinite scroll
|
|
|
|
// limit: 10, offset: page*10,
|
|
|
|
},
|
|
|
|
remove: false
|
|
|
|
});
|
2014-11-13 00:48:28 +00:00
|
|
|
}
|
2014-11-13 22:35:37 +00:00
|
|
|
});
|
2016-09-11 22:03:05 +00:00
|
|
|
|
|
|
|
Whisper.Conversation.COLORS = COLORS.concat(['grey', 'default']).join(' ');
|
2017-05-07 21:18:22 +00:00
|
|
|
|
|
|
|
// Special collection for fetching all the groups a certain number appears in
|
|
|
|
Whisper.GroupCollection = Backbone.Collection.extend({
|
|
|
|
database: Whisper.Database,
|
|
|
|
storeName: 'conversations',
|
|
|
|
model: Whisper.Conversation,
|
|
|
|
fetchGroups: function(number) {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
this.fetch({
|
|
|
|
index: {
|
|
|
|
name: 'group',
|
|
|
|
only: number
|
|
|
|
}
|
|
|
|
}).always(resolve);
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
});
|
2014-05-17 04:48:46 +00:00
|
|
|
})();
|