2018-03-19 21:19:09 +00:00
|
|
|
|
/* global Whisper: false */
|
2018-04-09 16:43:03 +00:00
|
|
|
|
/* global i18n: false */
|
|
|
|
|
/* global textsecure: false */
|
|
|
|
|
/* global _: false */
|
|
|
|
|
/* global Mustache: false */
|
2018-04-18 23:10:14 +00:00
|
|
|
|
/* global $: false */
|
2018-05-03 02:43:23 +00:00
|
|
|
|
/* global storage: false */
|
2018-05-05 01:19:54 +00:00
|
|
|
|
/* global Signal: false */
|
2018-03-19 21:19:09 +00:00
|
|
|
|
|
2018-04-09 16:43:03 +00:00
|
|
|
|
// eslint-disable-next-line func-names
|
2018-04-27 21:25:04 +00:00
|
|
|
|
(function() {
|
2018-04-09 16:43:03 +00:00
|
|
|
|
'use strict';
|
|
|
|
|
|
2018-05-05 01:19:54 +00:00
|
|
|
|
const {
|
|
|
|
|
loadAttachmentData,
|
|
|
|
|
getAbsoluteAttachmentPath,
|
|
|
|
|
} = window.Signal.Migrations;
|
2018-04-09 16:43:03 +00:00
|
|
|
|
|
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
|
|
|
|
|
|
const ErrorIconView = Whisper.View.extend({
|
|
|
|
|
templateName: 'error-icon',
|
|
|
|
|
className: 'error-icon-container',
|
|
|
|
|
initialize() {
|
|
|
|
|
if (this.model.name === 'UnregisteredUserError') {
|
|
|
|
|
this.$el.addClass('unregistered-user-error');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const NetworkErrorView = Whisper.View.extend({
|
|
|
|
|
tagName: 'span',
|
|
|
|
|
className: 'hasRetry',
|
|
|
|
|
templateName: 'hasRetry',
|
|
|
|
|
render_attributes() {
|
|
|
|
|
let messageNotSent;
|
|
|
|
|
|
|
|
|
|
if (!this.model.someRecipientsFailed()) {
|
|
|
|
|
messageNotSent = i18n('messageNotSent');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
messageNotSent,
|
|
|
|
|
resend: i18n('resend'),
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const SomeFailedView = Whisper.View.extend({
|
|
|
|
|
tagName: 'span',
|
|
|
|
|
className: 'some-failed',
|
|
|
|
|
templateName: 'some-failed',
|
2018-04-17 01:17:38 +00:00
|
|
|
|
render_attributes() {
|
|
|
|
|
return {
|
|
|
|
|
someFailed: i18n('someRecipientsFailed'),
|
|
|
|
|
};
|
2018-04-09 16:43:03 +00:00
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const TimerView = Whisper.View.extend({
|
|
|
|
|
templateName: 'hourglass',
|
|
|
|
|
initialize() {
|
|
|
|
|
this.listenTo(this.model, 'unload', this.remove);
|
|
|
|
|
},
|
|
|
|
|
update() {
|
|
|
|
|
if (this.timeout) {
|
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
|
this.timeout = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.model.isExpired()) {
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
if (this.model.isExpiring()) {
|
|
|
|
|
this.render();
|
|
|
|
|
const totalTime = this.model.get('expireTimer') * 1000;
|
|
|
|
|
const remainingTime = this.model.msTilExpire();
|
|
|
|
|
const elapsed = (totalTime - remainingTime) / totalTime;
|
|
|
|
|
this.$('.sand').css('transform', `translateY(${elapsed * 100}%)`);
|
|
|
|
|
this.$el.css('display', 'inline-block');
|
2018-04-27 21:25:04 +00:00
|
|
|
|
this.timeout = setTimeout(
|
|
|
|
|
this.update.bind(this),
|
|
|
|
|
Math.max(totalTime / 100, 500)
|
|
|
|
|
);
|
2018-04-09 16:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Whisper.ExpirationTimerUpdateView = Whisper.View.extend({
|
|
|
|
|
tagName: 'li',
|
|
|
|
|
className: 'expirationTimerUpdate advisory',
|
|
|
|
|
templateName: 'expirationTimerUpdate',
|
|
|
|
|
id() {
|
|
|
|
|
return this.model.id;
|
|
|
|
|
},
|
|
|
|
|
initialize() {
|
|
|
|
|
this.conversation = this.model.getExpirationTimerUpdateSource();
|
|
|
|
|
this.listenTo(this.conversation, 'change', this.render);
|
|
|
|
|
this.listenTo(this.model, 'unload', this.remove);
|
2018-06-08 22:51:00 +00:00
|
|
|
|
this.listenTo(this.model, 'change', this.onChange);
|
2018-04-09 16:43:03 +00:00
|
|
|
|
},
|
|
|
|
|
render_attributes() {
|
|
|
|
|
const seconds = this.model.get('expirationTimerUpdate').expireTimer;
|
|
|
|
|
let timerMessage;
|
|
|
|
|
|
|
|
|
|
const timerUpdate = this.model.get('expirationTimerUpdate');
|
|
|
|
|
const prettySeconds = Whisper.ExpirationTimerOptions.getName(seconds);
|
|
|
|
|
|
|
|
|
|
if (timerUpdate && timerUpdate.fromSync) {
|
|
|
|
|
timerMessage = i18n('timerSetOnSync', prettySeconds);
|
|
|
|
|
} else if (this.conversation.id === textsecure.storage.user.getNumber()) {
|
|
|
|
|
timerMessage = i18n('youChangedTheTimer', prettySeconds);
|
|
|
|
|
} else {
|
|
|
|
|
timerMessage = i18n('theyChangedTheTimer', [
|
|
|
|
|
this.conversation.getTitle(),
|
|
|
|
|
prettySeconds,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
return { content: timerMessage };
|
|
|
|
|
},
|
2018-06-08 22:51:00 +00:00
|
|
|
|
onChange() {
|
|
|
|
|
this.addId();
|
|
|
|
|
},
|
|
|
|
|
addId() {
|
|
|
|
|
// This is important to enable the lastSeenIndicator when it's just been added.
|
|
|
|
|
this.$el.attr('id', this.id());
|
|
|
|
|
},
|
2018-04-09 16:43:03 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Whisper.KeyChangeView = Whisper.View.extend({
|
|
|
|
|
tagName: 'li',
|
|
|
|
|
className: 'keychange advisory',
|
|
|
|
|
templateName: 'keychange',
|
|
|
|
|
id() {
|
|
|
|
|
return this.model.id;
|
|
|
|
|
},
|
|
|
|
|
initialize() {
|
|
|
|
|
this.conversation = this.model.getModelForKeyChange();
|
|
|
|
|
this.listenTo(this.conversation, 'change', this.render);
|
|
|
|
|
this.listenTo(this.model, 'unload', this.remove);
|
|
|
|
|
},
|
|
|
|
|
events: {
|
|
|
|
|
'click .content': 'showIdentity',
|
|
|
|
|
},
|
|
|
|
|
render_attributes() {
|
|
|
|
|
return {
|
|
|
|
|
content: this.model.getNotificationText(),
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
showIdentity() {
|
|
|
|
|
this.$el.trigger('show-identity', this.conversation);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Whisper.VerifiedChangeView = Whisper.View.extend({
|
|
|
|
|
tagName: 'li',
|
|
|
|
|
className: 'verified-change advisory',
|
|
|
|
|
templateName: 'verified-change',
|
|
|
|
|
id() {
|
|
|
|
|
return this.model.id;
|
|
|
|
|
},
|
|
|
|
|
initialize() {
|
|
|
|
|
this.conversation = this.model.getModelForVerifiedChange();
|
|
|
|
|
this.listenTo(this.conversation, 'change', this.render);
|
|
|
|
|
this.listenTo(this.model, 'unload', this.remove);
|
|
|
|
|
},
|
|
|
|
|
events: {
|
|
|
|
|
'click .content': 'showIdentity',
|
|
|
|
|
},
|
|
|
|
|
render_attributes() {
|
|
|
|
|
let key;
|
|
|
|
|
|
|
|
|
|
if (this.model.get('verified')) {
|
|
|
|
|
if (this.model.get('local')) {
|
|
|
|
|
key = 'youMarkedAsVerified';
|
|
|
|
|
} else {
|
|
|
|
|
key = 'youMarkedAsVerifiedOtherDevice';
|
2016-09-18 06:55:05 +00:00
|
|
|
|
}
|
2018-04-09 16:43:03 +00:00
|
|
|
|
return {
|
|
|
|
|
icon: 'verified',
|
|
|
|
|
content: i18n(key, this.conversation.getTitle()),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.model.get('local')) {
|
|
|
|
|
key = 'youMarkedAsNotVerified';
|
|
|
|
|
} else {
|
|
|
|
|
key = 'youMarkedAsNotVerifiedOtherDevice';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
icon: 'shield',
|
|
|
|
|
content: i18n(key, this.conversation.getTitle()),
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
showIdentity() {
|
|
|
|
|
this.$el.trigger('show-identity', this.conversation);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Whisper.MessageView = Whisper.View.extend({
|
|
|
|
|
tagName: 'li',
|
|
|
|
|
templateName: 'message',
|
|
|
|
|
id() {
|
|
|
|
|
return this.model.id;
|
|
|
|
|
},
|
|
|
|
|
initialize() {
|
|
|
|
|
// loadedAttachmentViews :: Promise (Array AttachmentView) | null
|
|
|
|
|
this.loadedAttachmentViews = null;
|
|
|
|
|
|
|
|
|
|
this.listenTo(this.model, 'change:errors', this.onErrorsChanged);
|
|
|
|
|
this.listenTo(this.model, 'change:body', this.render);
|
|
|
|
|
this.listenTo(this.model, 'change:delivered', this.renderDelivered);
|
|
|
|
|
this.listenTo(this.model, 'change:read_by', this.renderRead);
|
2018-04-27 21:25:04 +00:00
|
|
|
|
this.listenTo(
|
|
|
|
|
this.model,
|
|
|
|
|
'change:expirationStartTimestamp',
|
|
|
|
|
this.renderExpiring
|
|
|
|
|
);
|
2018-04-13 01:12:40 +00:00
|
|
|
|
this.listenTo(this.model, 'change', this.onChange);
|
2018-04-27 21:25:04 +00:00
|
|
|
|
this.listenTo(
|
|
|
|
|
this.model,
|
|
|
|
|
'change:flags change:group_update',
|
|
|
|
|
this.renderControl
|
|
|
|
|
);
|
2018-04-09 16:43:03 +00:00
|
|
|
|
this.listenTo(this.model, 'destroy', this.onDestroy);
|
|
|
|
|
this.listenTo(this.model, 'unload', this.onUnload);
|
|
|
|
|
this.listenTo(this.model, 'expired', this.onExpired);
|
|
|
|
|
this.listenTo(this.model, 'pending', this.renderPending);
|
|
|
|
|
this.listenTo(this.model, 'done', this.renderDone);
|
|
|
|
|
this.timeStampView = new Whisper.ExtendedTimestampView();
|
|
|
|
|
|
|
|
|
|
this.contact = this.model.isIncoming() ? this.model.getContact() : null;
|
|
|
|
|
if (this.contact) {
|
|
|
|
|
this.listenTo(this.contact, 'change:color', this.updateColor);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
events: {
|
|
|
|
|
'click .retry': 'retryMessage',
|
|
|
|
|
'click .error-icon': 'select',
|
|
|
|
|
'click .timestamp': 'select',
|
|
|
|
|
'click .status': 'select',
|
|
|
|
|
'click .some-failed': 'select',
|
|
|
|
|
'click .error-message': 'select',
|
2018-04-18 23:10:14 +00:00
|
|
|
|
'click .menu-container': 'showMenu',
|
|
|
|
|
'click .menu-list .reply': 'onReply',
|
2018-04-09 16:43:03 +00:00
|
|
|
|
},
|
|
|
|
|
retryMessage() {
|
|
|
|
|
const retrys = _.filter(
|
|
|
|
|
this.model.get('errors'),
|
|
|
|
|
this.model.isReplayableError.bind(this.model)
|
|
|
|
|
);
|
2018-04-27 21:25:04 +00:00
|
|
|
|
_.map(retrys, 'number').forEach(number => {
|
2018-04-09 16:43:03 +00:00
|
|
|
|
this.model.resend(number);
|
|
|
|
|
});
|
|
|
|
|
},
|
2018-04-18 23:10:14 +00:00
|
|
|
|
showMenu(e) {
|
|
|
|
|
if (this.menuVisible) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.menuVisible = true;
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
|
|
this.$('.menu-list').show();
|
|
|
|
|
$(document).one('click', () => {
|
|
|
|
|
this.hideMenu();
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
hideMenu() {
|
|
|
|
|
this.menuVisible = false;
|
|
|
|
|
this.$('.menu-list').hide();
|
|
|
|
|
},
|
2018-04-17 21:31:16 +00:00
|
|
|
|
onReply() {
|
|
|
|
|
this.model.trigger('reply', this.model);
|
|
|
|
|
},
|
2018-04-09 16:43:03 +00:00
|
|
|
|
onExpired() {
|
|
|
|
|
this.$el.addClass('expired');
|
2018-04-27 21:25:04 +00:00
|
|
|
|
this.$el.find('.bubble').one('webkitAnimationEnd animationend', e => {
|
2018-04-09 16:43:03 +00:00
|
|
|
|
if (e.target === this.$('.bubble')[0]) {
|
|
|
|
|
this.remove();
|
2017-06-15 19:27:41 +00:00
|
|
|
|
}
|
2018-04-09 16:43:03 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Failsafe: if in the background, animation events don't fire
|
|
|
|
|
setTimeout(this.remove.bind(this), 1000);
|
|
|
|
|
},
|
|
|
|
|
onUnload() {
|
|
|
|
|
if (this.avatarView) {
|
|
|
|
|
this.avatarView.remove();
|
|
|
|
|
}
|
2018-05-18 23:57:26 +00:00
|
|
|
|
if (this.bodyView) {
|
|
|
|
|
this.bodyView.remove();
|
|
|
|
|
}
|
|
|
|
|
if (this.contactView) {
|
|
|
|
|
this.contactView.remove();
|
|
|
|
|
}
|
|
|
|
|
if (this.controlView) {
|
|
|
|
|
this.controlView.remove();
|
|
|
|
|
}
|
2018-04-09 16:43:03 +00:00
|
|
|
|
if (this.errorIconView) {
|
|
|
|
|
this.errorIconView.remove();
|
|
|
|
|
}
|
|
|
|
|
if (this.networkErrorView) {
|
|
|
|
|
this.networkErrorView.remove();
|
|
|
|
|
}
|
2018-05-18 23:57:26 +00:00
|
|
|
|
if (this.quoteView) {
|
|
|
|
|
this.quoteView.remove();
|
|
|
|
|
}
|
2018-04-09 16:43:03 +00:00
|
|
|
|
if (this.someFailedView) {
|
|
|
|
|
this.someFailedView.remove();
|
|
|
|
|
}
|
|
|
|
|
if (this.timeStampView) {
|
|
|
|
|
this.timeStampView.remove();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: We have to do this in the background (`then` instead of `await`)
|
|
|
|
|
// as our tests rely on `onUnload` synchronously removing the view from
|
|
|
|
|
// the DOM.
|
|
|
|
|
// eslint-disable-next-line more/no-then
|
2018-04-27 21:25:04 +00:00
|
|
|
|
this.loadAttachmentViews().then(views =>
|
|
|
|
|
views.forEach(view => view.unload())
|
|
|
|
|
);
|
2018-04-09 16:43:03 +00:00
|
|
|
|
|
|
|
|
|
// No need to handle this one, since it listens to 'unload' itself:
|
|
|
|
|
// this.timerView
|
|
|
|
|
|
|
|
|
|
this.remove();
|
|
|
|
|
},
|
|
|
|
|
onDestroy() {
|
|
|
|
|
if (this.$el.hasClass('expired')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.onUnload();
|
|
|
|
|
},
|
2018-04-13 01:12:40 +00:00
|
|
|
|
onChange() {
|
2018-04-14 01:44:19 +00:00
|
|
|
|
this.renderSent();
|
2018-04-13 01:12:40 +00:00
|
|
|
|
this.renderQuote();
|
2018-05-08 02:08:45 +00:00
|
|
|
|
this.addId();
|
2018-04-13 01:12:40 +00:00
|
|
|
|
},
|
2018-04-09 16:43:03 +00:00
|
|
|
|
select(e) {
|
|
|
|
|
this.$el.trigger('select', { message: this.model });
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
},
|
|
|
|
|
className() {
|
|
|
|
|
return ['entry', this.model.get('type')].join(' ');
|
|
|
|
|
},
|
|
|
|
|
renderPending() {
|
|
|
|
|
this.$el.addClass('pending');
|
|
|
|
|
},
|
|
|
|
|
renderDone() {
|
|
|
|
|
this.$el.removeClass('pending');
|
|
|
|
|
},
|
|
|
|
|
renderSent() {
|
|
|
|
|
if (this.model.isOutgoing()) {
|
|
|
|
|
this.$el.toggleClass('sent', !!this.model.get('sent'));
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
renderDelivered() {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
if (this.model.get('delivered')) {
|
|
|
|
|
this.$el.addClass('delivered');
|
|
|
|
|
}
|
2018-04-09 16:43:03 +00:00
|
|
|
|
},
|
|
|
|
|
renderRead() {
|
|
|
|
|
if (!_.isEmpty(this.model.get('read_by'))) {
|
|
|
|
|
this.$el.addClass('read');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
onErrorsChanged() {
|
|
|
|
|
if (this.model.isIncoming()) {
|
|
|
|
|
this.render();
|
|
|
|
|
} else {
|
|
|
|
|
this.renderErrors();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
renderErrors() {
|
|
|
|
|
const errors = this.model.get('errors');
|
|
|
|
|
|
|
|
|
|
this.$('.error-icon-container').remove();
|
|
|
|
|
if (this.errorIconView) {
|
|
|
|
|
this.errorIconView.remove();
|
|
|
|
|
this.errorIconView = null;
|
|
|
|
|
}
|
|
|
|
|
if (_.size(errors) > 0) {
|
|
|
|
|
if (this.model.isIncoming()) {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
this.$('.content')
|
|
|
|
|
.text(this.model.getDescription())
|
|
|
|
|
.addClass('error-message');
|
2018-04-09 16:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
this.errorIconView = new ErrorIconView({ model: errors[0] });
|
|
|
|
|
this.errorIconView.render().$el.appendTo(this.$('.bubble'));
|
2018-04-24 02:16:00 +00:00
|
|
|
|
} else if (!this.hasContents()) {
|
|
|
|
|
const el = this.$('.content');
|
|
|
|
|
if (!el || el.length === 0) {
|
|
|
|
|
this.$('.inner-bubble').append("<div class='content'></div>");
|
|
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
|
this.$('.content')
|
|
|
|
|
.text(i18n('noContents'))
|
|
|
|
|
.addClass('error-message');
|
2018-04-09 16:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.$('.meta .hasRetry').remove();
|
|
|
|
|
if (this.networkErrorView) {
|
|
|
|
|
this.networkErrorView.remove();
|
|
|
|
|
this.networkErrorView = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.model.hasNetworkError()) {
|
|
|
|
|
this.networkErrorView = new NetworkErrorView({ model: this.model });
|
|
|
|
|
this.$('.meta').prepend(this.networkErrorView.render().el);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.$('.meta .some-failed').remove();
|
|
|
|
|
if (this.someFailedView) {
|
|
|
|
|
this.someFailedView.remove();
|
|
|
|
|
this.someFailedView = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.model.someRecipientsFailed()) {
|
|
|
|
|
this.someFailedView = new SomeFailedView();
|
|
|
|
|
this.$('.meta').prepend(this.someFailedView.render().el);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
renderControl() {
|
|
|
|
|
if (this.model.isEndSession() || this.model.isGroupUpdate()) {
|
|
|
|
|
this.$el.addClass('control');
|
2018-05-18 23:57:26 +00:00
|
|
|
|
|
|
|
|
|
if (this.controlView) {
|
|
|
|
|
this.controlView.remove();
|
|
|
|
|
this.controlView = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.controlView = new Whisper.ReactWrapperView({
|
|
|
|
|
className: 'content-wrapper',
|
|
|
|
|
Component: window.Signal.Components.Emojify,
|
|
|
|
|
props: {
|
|
|
|
|
text: this.model.getDescription(),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
this.$('.content').prepend(this.controlView.el);
|
2018-04-09 16:43:03 +00:00
|
|
|
|
} else {
|
|
|
|
|
this.$el.removeClass('control');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
renderExpiring() {
|
|
|
|
|
if (!this.timerView) {
|
|
|
|
|
this.timerView = new TimerView({ model: this.model });
|
|
|
|
|
}
|
|
|
|
|
this.timerView.setElement(this.$('.timer'));
|
|
|
|
|
this.timerView.update();
|
|
|
|
|
},
|
2018-04-12 23:57:50 +00:00
|
|
|
|
renderQuote() {
|
2018-04-17 21:31:16 +00:00
|
|
|
|
const props = this.model.getPropsForQuote();
|
|
|
|
|
if (!props) {
|
2018-04-10 01:31:52 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-17 21:31:16 +00:00
|
|
|
|
const contact = this.model.getQuoteContact();
|
2018-04-18 18:25:45 +00:00
|
|
|
|
if (this.quoteView) {
|
|
|
|
|
this.quoteView.remove();
|
|
|
|
|
this.quoteView = null;
|
2018-04-12 23:57:50 +00:00
|
|
|
|
} else if (contact) {
|
|
|
|
|
this.listenTo(contact, 'change:color', this.renderQuote);
|
2018-04-10 01:31:52 +00:00
|
|
|
|
}
|
2018-04-12 23:57:50 +00:00
|
|
|
|
|
2018-04-18 18:25:45 +00:00
|
|
|
|
this.quoteView = new Whisper.ReactWrapperView({
|
2018-04-17 16:39:41 +00:00
|
|
|
|
className: 'quote-wrapper',
|
2018-04-12 23:57:50 +00:00
|
|
|
|
Component: window.Signal.Components.Quote,
|
2018-04-20 22:17:08 +00:00
|
|
|
|
props: Object.assign({}, props, {
|
2018-05-14 20:52:10 +00:00
|
|
|
|
text: props.text,
|
2018-04-20 22:17:08 +00:00
|
|
|
|
}),
|
2018-04-12 23:57:50 +00:00
|
|
|
|
});
|
2018-04-18 18:25:45 +00:00
|
|
|
|
this.$('.inner-bubble').prepend(this.quoteView.el);
|
2018-04-10 01:31:52 +00:00
|
|
|
|
},
|
2018-05-03 02:43:23 +00:00
|
|
|
|
renderContact() {
|
|
|
|
|
const contacts = this.model.get('contact');
|
|
|
|
|
if (!contacts || !contacts.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const contact = contacts[0];
|
|
|
|
|
|
2018-05-05 01:19:54 +00:00
|
|
|
|
const regionCode = storage.get('regionCode');
|
|
|
|
|
const { contactSelector } = Signal.Types.Contact;
|
|
|
|
|
|
2018-05-03 02:43:23 +00:00
|
|
|
|
const number =
|
|
|
|
|
contact.number && contact.number[0] && contact.number[0].value;
|
|
|
|
|
const haveConversation =
|
|
|
|
|
number && Boolean(window.ConversationController.get(number));
|
2018-05-23 19:15:46 +00:00
|
|
|
|
const hasLocalSignalAccount =
|
|
|
|
|
this.contactHasSignalAccount || (number && haveConversation);
|
|
|
|
|
|
|
|
|
|
// We store this value on this. because a re-render shouldn't kick off another
|
|
|
|
|
// profile check, going to the web.
|
|
|
|
|
this.contactHasSignalAccount = hasLocalSignalAccount;
|
2018-05-03 02:43:23 +00:00
|
|
|
|
|
|
|
|
|
const onSendMessage = number
|
|
|
|
|
? () => {
|
|
|
|
|
this.model.trigger('open-conversation', number);
|
|
|
|
|
}
|
|
|
|
|
: null;
|
2018-05-23 19:15:46 +00:00
|
|
|
|
const onOpenContact = async () => {
|
|
|
|
|
// First let's finish our check with the central server to see if this user has
|
|
|
|
|
// a signal account. Then we won't have to do it a second time for the detail
|
|
|
|
|
// screen.
|
|
|
|
|
await this.checkingProfile;
|
|
|
|
|
this.model.trigger('show-contact-detail', {
|
|
|
|
|
contact,
|
|
|
|
|
hasSignalAccount: this.contactHasSignalAccount,
|
|
|
|
|
});
|
2018-05-03 02:43:23 +00:00
|
|
|
|
};
|
|
|
|
|
|
2018-05-08 17:11:40 +00:00
|
|
|
|
const getProps = ({ hasSignalAccount }) => ({
|
2018-05-05 01:19:54 +00:00
|
|
|
|
contact: contactSelector(contact, {
|
|
|
|
|
regionCode,
|
|
|
|
|
getAbsoluteAttachmentPath,
|
|
|
|
|
}),
|
|
|
|
|
hasSignalAccount,
|
|
|
|
|
onSendMessage,
|
|
|
|
|
onOpenContact,
|
|
|
|
|
});
|
2018-05-03 02:43:23 +00:00
|
|
|
|
|
|
|
|
|
if (this.contactView) {
|
|
|
|
|
this.contactView.remove();
|
|
|
|
|
this.contactView = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.contactView = new Whisper.ReactWrapperView({
|
|
|
|
|
className: 'contact-wrapper',
|
|
|
|
|
Component: window.Signal.Components.EmbeddedContact,
|
2018-05-08 17:11:40 +00:00
|
|
|
|
props: getProps({
|
|
|
|
|
hasSignalAccount: hasLocalSignalAccount,
|
|
|
|
|
}),
|
2018-05-03 02:43:23 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.$('.inner-bubble').prepend(this.contactView.el);
|
|
|
|
|
|
|
|
|
|
// If we can't verify a signal account locally, we'll go to the Signal Server.
|
2018-05-08 17:11:40 +00:00
|
|
|
|
if (number && !hasLocalSignalAccount) {
|
2018-05-03 02:43:23 +00:00
|
|
|
|
// eslint-disable-next-line more/no-then
|
2018-05-23 19:15:46 +00:00
|
|
|
|
this.checkingProfile = window.textsecure.messaging
|
2018-05-03 02:43:23 +00:00
|
|
|
|
.getProfile(number)
|
|
|
|
|
.then(() => {
|
2018-05-23 19:15:46 +00:00
|
|
|
|
this.contactHasSignalAccount = true;
|
|
|
|
|
|
2018-05-03 02:43:23 +00:00
|
|
|
|
if (!this.contactView) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-05-08 17:11:40 +00:00
|
|
|
|
this.contactView.update(getProps({ hasSignalAccount: true }));
|
2018-05-03 02:43:23 +00:00
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
// No account available, or network connectivity problem
|
|
|
|
|
});
|
2018-05-23 19:15:46 +00:00
|
|
|
|
} else {
|
|
|
|
|
this.checkingProfile = Promise.resolve();
|
2018-05-03 02:43:23 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
2018-04-09 16:43:03 +00:00
|
|
|
|
isImageWithoutCaption() {
|
|
|
|
|
const attachments = this.model.get('attachments');
|
|
|
|
|
const body = this.model.get('body');
|
|
|
|
|
if (!attachments || attachments.length === 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (body && body.trim()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const first = attachments[0];
|
2018-04-24 21:16:26 +00:00
|
|
|
|
if (Signal.Util.GoogleChrome.isImageTypeSupported(first.contentType)) {
|
2018-04-09 16:43:03 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
},
|
2018-04-24 02:16:00 +00:00
|
|
|
|
hasContents() {
|
|
|
|
|
const attachments = this.model.get('attachments');
|
|
|
|
|
const hasAttachments = attachments && attachments.length > 0;
|
|
|
|
|
|
2018-05-03 02:43:23 +00:00
|
|
|
|
const contacts = this.model.get('contact');
|
|
|
|
|
const hasContact = contacts && contacts.length > 0;
|
|
|
|
|
|
|
|
|
|
return this.hasTextContents() || hasAttachments || hasContact;
|
2018-04-24 02:16:00 +00:00
|
|
|
|
},
|
|
|
|
|
hasTextContents() {
|
|
|
|
|
const body = this.model.get('body');
|
|
|
|
|
const isGroupUpdate = this.model.isGroupUpdate();
|
|
|
|
|
const isEndSession = this.model.isEndSession();
|
|
|
|
|
|
|
|
|
|
const errors = this.model.get('errors');
|
|
|
|
|
const hasErrors = errors && errors.length > 0;
|
|
|
|
|
const errorsCanBeContents = this.model.isIncoming() && hasErrors;
|
|
|
|
|
|
|
|
|
|
return body || isGroupUpdate || isEndSession || errorsCanBeContents;
|
|
|
|
|
},
|
2018-05-08 02:08:45 +00:00
|
|
|
|
addId() {
|
|
|
|
|
// Because we initially render a sent Message before we've roundtripped with the
|
|
|
|
|
// database, we don't have its id for that first render. We do get a change event,
|
|
|
|
|
// however, and can add the id manually.
|
|
|
|
|
const { id } = this.model;
|
|
|
|
|
this.$el.attr('id', id);
|
|
|
|
|
},
|
2018-04-09 16:43:03 +00:00
|
|
|
|
render() {
|
|
|
|
|
const contact = this.model.isIncoming() ? this.model.getContact() : null;
|
2018-04-17 16:39:41 +00:00
|
|
|
|
const attachments = this.model.get('attachments');
|
|
|
|
|
|
2018-04-24 21:46:00 +00:00
|
|
|
|
const errors = this.model.get('errors');
|
|
|
|
|
const hasErrors = errors && errors.length > 0;
|
2018-04-17 16:39:41 +00:00
|
|
|
|
const hasAttachments = attachments && attachments.length > 0;
|
2018-04-24 02:16:00 +00:00
|
|
|
|
const hasBody = this.hasTextContents();
|
2018-04-17 01:46:36 +00:00
|
|
|
|
|
2018-05-14 20:52:10 +00:00
|
|
|
|
const messageBody = this.model.get('body');
|
|
|
|
|
|
2018-04-27 21:25:04 +00:00
|
|
|
|
this.$el.html(
|
|
|
|
|
Mustache.render(
|
|
|
|
|
_.result(this, 'template', ''),
|
|
|
|
|
{
|
2018-05-14 20:52:10 +00:00
|
|
|
|
message: Boolean(messageBody),
|
2018-04-27 21:25:04 +00:00
|
|
|
|
hasBody,
|
|
|
|
|
timestamp: this.model.get('sent_at'),
|
|
|
|
|
sender: (contact && contact.getTitle()) || '',
|
|
|
|
|
avatar: contact && contact.getAvatar(),
|
|
|
|
|
profileName: contact && contact.getProfileName(),
|
|
|
|
|
innerBubbleClasses: this.isImageWithoutCaption() ? '' : 'with-tail',
|
|
|
|
|
hoverIcon: !hasErrors,
|
|
|
|
|
hasAttachments,
|
|
|
|
|
reply: i18n('replyToMessage'),
|
|
|
|
|
},
|
|
|
|
|
this.render_partials()
|
|
|
|
|
)
|
|
|
|
|
);
|
2018-04-09 16:43:03 +00:00
|
|
|
|
this.timeStampView.setElement(this.$('.timestamp'));
|
|
|
|
|
this.timeStampView.update();
|
|
|
|
|
|
|
|
|
|
this.renderControl();
|
|
|
|
|
|
2018-05-14 20:52:10 +00:00
|
|
|
|
if (messageBody) {
|
|
|
|
|
if (this.bodyView) {
|
|
|
|
|
this.bodyView.remove();
|
|
|
|
|
this.bodyView = null;
|
|
|
|
|
}
|
|
|
|
|
this.bodyView = new Whisper.ReactWrapperView({
|
|
|
|
|
className: 'body-wrapper',
|
|
|
|
|
Component: window.Signal.Components.MessageBody,
|
|
|
|
|
props: {
|
|
|
|
|
text: messageBody,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
this.$('.body').append(this.bodyView.el);
|
2018-04-09 16:43:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.renderSent();
|
|
|
|
|
this.renderDelivered();
|
|
|
|
|
this.renderRead();
|
|
|
|
|
this.renderErrors();
|
|
|
|
|
this.renderExpiring();
|
2018-04-12 23:57:50 +00:00
|
|
|
|
this.renderQuote();
|
2018-05-03 02:43:23 +00:00
|
|
|
|
this.renderContact();
|
2018-04-09 16:43:03 +00:00
|
|
|
|
|
|
|
|
|
// NOTE: We have to do this in the background (`then` instead of `await`)
|
|
|
|
|
// as our code / Backbone seems to rely on `render` synchronously returning
|
|
|
|
|
// `this` instead of `Promise MessageView` (this):
|
|
|
|
|
// eslint-disable-next-line more/no-then
|
2018-04-27 21:25:04 +00:00
|
|
|
|
this.loadAttachmentViews().then(views =>
|
|
|
|
|
this.renderAttachmentViews(views)
|
|
|
|
|
);
|
2018-04-09 16:43:03 +00:00
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
},
|
|
|
|
|
updateColor() {
|
|
|
|
|
const bubble = this.$('.bubble');
|
|
|
|
|
|
|
|
|
|
// this.contact is known to be non-null if we're registered for color changes
|
|
|
|
|
const color = this.contact.getColor();
|
|
|
|
|
if (color) {
|
|
|
|
|
bubble.removeClass(Whisper.Conversation.COLORS);
|
|
|
|
|
bubble.addClass(color);
|
|
|
|
|
}
|
|
|
|
|
this.avatarView = new (Whisper.View.extend({
|
|
|
|
|
templateName: 'avatar',
|
|
|
|
|
render_attributes: { avatar: this.contact.getAvatar() },
|
|
|
|
|
}))();
|
|
|
|
|
this.$('.avatar').replaceWith(this.avatarView.render().$('.avatar'));
|
|
|
|
|
},
|
2018-03-19 21:33:14 +00:00
|
|
|
|
loadAttachmentViews() {
|
|
|
|
|
if (this.loadedAttachmentViews !== null) {
|
|
|
|
|
return this.loadedAttachmentViews;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-20 21:25:26 +00:00
|
|
|
|
const attachments = this.model.get('attachments') || [];
|
2018-04-27 21:25:04 +00:00
|
|
|
|
const loadedAttachmentViews = Promise.all(
|
|
|
|
|
attachments.map(
|
|
|
|
|
attachment =>
|
|
|
|
|
new Promise(async resolve => {
|
|
|
|
|
const attachmentWithData = await loadAttachmentData(attachment);
|
|
|
|
|
const view = new Whisper.AttachmentView({
|
|
|
|
|
model: attachmentWithData,
|
|
|
|
|
timestamp: this.model.get('sent_at'),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.listenTo(view, 'update', () => {
|
|
|
|
|
// NOTE: Can we do without `updated` flag now that we use promises?
|
|
|
|
|
view.updated = true;
|
|
|
|
|
resolve(view);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
view.render();
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
);
|
2018-03-19 21:33:14 +00:00
|
|
|
|
|
|
|
|
|
// Memoize attachment views to avoid double loading:
|
|
|
|
|
this.loadedAttachmentViews = loadedAttachmentViews;
|
|
|
|
|
|
|
|
|
|
return loadedAttachmentViews;
|
|
|
|
|
},
|
|
|
|
|
renderAttachmentViews(views) {
|
|
|
|
|
views.forEach(view => this.renderAttachmentView(view));
|
|
|
|
|
},
|
|
|
|
|
renderAttachmentView(view) {
|
|
|
|
|
if (!view.updated) {
|
2018-04-27 21:25:04 +00:00
|
|
|
|
throw new Error(
|
|
|
|
|
'Invariant violation:' +
|
|
|
|
|
' Cannot render an attachment view that isn’t ready'
|
|
|
|
|
);
|
2018-03-19 21:33:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parent = this.$('.attachments')[0];
|
|
|
|
|
const isViewAlreadyChild = parent === view.el.parentNode;
|
|
|
|
|
if (isViewAlreadyChild) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (view.el.parentNode) {
|
|
|
|
|
view.el.parentNode.removeChild(view.el);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.trigger('beforeChangeHeight');
|
|
|
|
|
this.$('.attachments').append(view.el);
|
|
|
|
|
view.setElement(view.el);
|
|
|
|
|
this.trigger('afterChangeHeight');
|
|
|
|
|
},
|
2018-04-09 16:43:03 +00:00
|
|
|
|
});
|
2018-04-27 21:25:04 +00:00
|
|
|
|
})();
|