parent
6e3014895b
commit
bc5dea62c3
3 changed files with 69 additions and 55 deletions
|
@ -21,12 +21,13 @@ textsecure.registerOnLoadFunction(function() {
|
||||||
} else {
|
} else {
|
||||||
if (isRegistrationDone()) {
|
if (isRegistrationDone()) {
|
||||||
textsecure.subscribeToPush(function(message) {
|
textsecure.subscribeToPush(function(message) {
|
||||||
Whisper.Messages.addIncomingMessage(message);
|
Whisper.Messages.addIncomingMessage(message).then(function() {
|
||||||
console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice +
|
console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice +
|
||||||
': "' + getString(message.message.body) + '"');
|
': "' + getString(message.message.body) + '"');
|
||||||
var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1;
|
var newUnreadCount = textsecure.storage.getUnencrypted("unreadCount", 0) + 1;
|
||||||
textsecure.storage.putUnencrypted("unreadCount", newUnreadCount);
|
textsecure.storage.putUnencrypted("unreadCount", newUnreadCount);
|
||||||
extension.navigator.setBadgeText(newUnreadCount);
|
extension.navigator.setBadgeText(newUnreadCount);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,27 +30,36 @@ var Whisper = Whisper || {};
|
||||||
|
|
||||||
addIncomingMessage: function(decrypted) {
|
addIncomingMessage: function(decrypted) {
|
||||||
//TODO: The data in decrypted (from subscribeToPush) should already be cleaned up
|
//TODO: The data in decrypted (from subscribeToPush) should already be cleaned up
|
||||||
var attachments = [];
|
return Promise.all(decrypted.message.attachments.map(function(a) {
|
||||||
for (var i = 0; i < decrypted.message.attachments.length; i++)
|
return new Promise(function(resolve, reject) {
|
||||||
attachments[i] = "data:" + decrypted.message.attachments[i].contentType + ";base64," + btoa(getString(decrypted.message.attachments[i].decrypted));
|
var dataView = new DataView(a.decrypted);
|
||||||
|
var blob = new Blob([dataView], { type: a.contentType });
|
||||||
|
var FR = new FileReader();
|
||||||
|
FR.onload = function(e) {
|
||||||
|
resolve(e.target.result);
|
||||||
|
};
|
||||||
|
FR.onerror = reject;
|
||||||
|
FR.readAsDataURL(blob);
|
||||||
|
});
|
||||||
|
})).then(function(base64_attachments) {
|
||||||
|
var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted);
|
||||||
|
var timestamp = decrypted.pushMessage.timestamp.toNumber();
|
||||||
|
var m = thread.messages().add({
|
||||||
|
person: decrypted.pushMessage.source,
|
||||||
|
threadId: thread.id,
|
||||||
|
body: decrypted.message.body,
|
||||||
|
attachments: base64_attachments,
|
||||||
|
type: 'incoming',
|
||||||
|
timestamp: timestamp
|
||||||
|
});
|
||||||
|
m.save();
|
||||||
|
|
||||||
var thread = Whisper.Threads.findOrCreateForIncomingMessage(decrypted);
|
if (timestamp > thread.get('timestamp')) {
|
||||||
var timestamp = decrypted.pushMessage.timestamp.toNumber();
|
thread.set('timestamp', timestamp);
|
||||||
var m = thread.messages().add({
|
}
|
||||||
person: decrypted.pushMessage.source,
|
thread.save({unreadCount: thread.get('unreadCount') + 1, active: true});
|
||||||
threadId: thread.id,
|
return m;
|
||||||
body: decrypted.message.body,
|
|
||||||
attachments: attachments,
|
|
||||||
type: 'incoming',
|
|
||||||
timestamp: timestamp
|
|
||||||
});
|
});
|
||||||
m.save();
|
|
||||||
|
|
||||||
if (timestamp > thread.get('timestamp')) {
|
|
||||||
thread.set('timestamp', timestamp);
|
|
||||||
}
|
|
||||||
thread.save({unreadCount: thread.get('unreadCount') + 1, active: true});
|
|
||||||
return m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}))();
|
}))();
|
||||||
|
|
|
@ -22,37 +22,41 @@ var Whisper = Whisper || {};
|
||||||
},
|
},
|
||||||
|
|
||||||
sendMessage: function(message, attachments) {
|
sendMessage: function(message, attachments) {
|
||||||
var timestamp = Date.now();
|
return Promise.all(attachments.map(function(a) {
|
||||||
var base64_attachments = _.map(attachments, function(a) {
|
return new Promise(function(resolve, reject) {
|
||||||
return ['data:', a.contentType, ';base64,', btoa(getString(a.data))].join('');
|
var dataView = new DataView(a.data);
|
||||||
|
var blob = new Blob([dataView], { type: a.contentType });
|
||||||
|
var FR = new FileReader();
|
||||||
|
FR.onload = function(e) {
|
||||||
|
resolve(e.target.result);
|
||||||
|
};
|
||||||
|
FR.onerror = reject;
|
||||||
|
FR.readAsDataURL(blob);
|
||||||
|
});
|
||||||
|
})).then(function(base64_attachments) {
|
||||||
|
var timestamp = Date.now();
|
||||||
|
|
||||||
|
this.messages().add({ type: 'outgoing',
|
||||||
|
body: message,
|
||||||
|
threadId: this.id,
|
||||||
|
attachments: base64_attachments,
|
||||||
|
timestamp: timestamp }).save();
|
||||||
|
|
||||||
|
this.save({ timestamp: timestamp,
|
||||||
|
unreadCount: 0,
|
||||||
|
active: true});
|
||||||
|
|
||||||
|
if (this.get('type') == 'private') {
|
||||||
|
return textsecure.messaging.sendMessageToNumber(this.get('id'), message, attachments);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return textsecure.messaging.sendMessageToGroup(this.get('groupId'), message, attachments);
|
||||||
|
}
|
||||||
|
}.bind(this)).then(function(result) {
|
||||||
|
console.log(result);
|
||||||
|
}).catch(function(error) {
|
||||||
|
console.log(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.messages().add({ type: 'outgoing',
|
|
||||||
body: message,
|
|
||||||
threadId: this.id,
|
|
||||||
attachments: base64_attachments,
|
|
||||||
timestamp: timestamp }).save();
|
|
||||||
|
|
||||||
|
|
||||||
this.save({ timestamp: timestamp,
|
|
||||||
unreadCount: 0,
|
|
||||||
active: true});
|
|
||||||
|
|
||||||
if (this.get('type') == 'private') {
|
|
||||||
var promise = textsecure.messaging.sendMessageToNumber(this.get('id'), message, attachments)
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var promise = textsecure.messaging.sendMessageToGroup(this.get('groupId'), message, attachments);
|
|
||||||
}
|
|
||||||
promise.then(
|
|
||||||
function(result) {
|
|
||||||
console.log(result);
|
|
||||||
}
|
|
||||||
).catch(
|
|
||||||
function(error) {
|
|
||||||
console.log(error);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
messages: function() {
|
messages: function() {
|
||||||
|
|
Loading…
Reference in a new issue