2021-09-17 18:27:53 +00:00
|
|
|
// Copyright 2014-2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-12 23:59:08 +00:00
|
|
|
import * as Backbone from 'backbone';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationModel } from '../models/conversations';
|
2021-09-22 20:59:54 +00:00
|
|
|
import { showToast } from '../util/showToast';
|
2022-04-05 00:38:22 +00:00
|
|
|
import { strictAssert } from '../util/assert';
|
2021-09-22 20:59:54 +00:00
|
|
|
import { ToastStickerPackInstallFailed } from '../components/ToastStickerPackInstallFailed';
|
2021-09-17 18:27:53 +00:00
|
|
|
|
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
const { Whisper } = window;
|
|
|
|
|
2021-10-12 23:59:08 +00:00
|
|
|
class ConversationStack extends Backbone.View {
|
2021-11-12 23:44:20 +00:00
|
|
|
public override className = 'conversation-stack';
|
2021-10-12 23:59:08 +00:00
|
|
|
|
|
|
|
private conversationStack: Array<ConversationModel> = [];
|
|
|
|
|
|
|
|
private getTopConversation(): undefined | ConversationModel {
|
|
|
|
return this.conversationStack[this.conversationStack.length - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
public open(conversation: ConversationModel, messageId: string): void {
|
|
|
|
const topConversation = this.getTopConversation();
|
|
|
|
|
|
|
|
if (!topConversation || topConversation.id !== conversation.id) {
|
2021-09-17 18:27:53 +00:00
|
|
|
const view = new Whisper.ConversationView({
|
|
|
|
model: conversation,
|
|
|
|
});
|
|
|
|
this.listenTo(conversation, 'unload', () => this.onUnload(conversation));
|
2021-09-21 22:37:10 +00:00
|
|
|
this.listenTo(conversation, 'showSafetyNumber', () =>
|
|
|
|
view.showSafetyNumber()
|
|
|
|
);
|
2021-09-17 18:27:53 +00:00
|
|
|
view.$el.appendTo(this.el);
|
|
|
|
|
2021-10-12 23:59:08 +00:00
|
|
|
if (topConversation) {
|
|
|
|
topConversation.trigger('unload', 'opened another conversation');
|
2021-09-17 18:27:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-12 23:59:08 +00:00
|
|
|
this.conversationStack.push(conversation);
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
conversation.trigger('opened', messageId);
|
|
|
|
} else if (messageId) {
|
|
|
|
conversation.trigger('scroll-to-message', messageId);
|
|
|
|
}
|
|
|
|
|
2021-10-12 23:59:08 +00:00
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
public unload(): void {
|
|
|
|
this.getTopConversation()?.trigger('unload', 'force unload requested');
|
|
|
|
}
|
|
|
|
|
|
|
|
private onUnload(conversation: ConversationModel) {
|
|
|
|
this.stopListening(conversation);
|
|
|
|
this.conversationStack = this.conversationStack.filter(
|
|
|
|
(c: ConversationModel) => c !== conversation
|
|
|
|
);
|
|
|
|
|
|
|
|
this.render();
|
|
|
|
}
|
|
|
|
|
2021-11-12 23:44:20 +00:00
|
|
|
public override render(): ConversationStack {
|
2021-10-12 23:59:08 +00:00
|
|
|
const isAnyConversationOpen = Boolean(this.conversationStack.length);
|
|
|
|
this.$('.no-conversation-open').toggle(!isAnyConversationOpen);
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
// Make sure poppers are positioned properly
|
|
|
|
window.dispatchEvent(new Event('resize'));
|
|
|
|
|
2021-10-12 23:59:08 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
2021-09-17 18:27:53 +00:00
|
|
|
|
|
|
|
const AppLoadingScreen = Whisper.View.extend({
|
|
|
|
template: () => $('#app-loading-screen').html(),
|
|
|
|
className: 'app-loading-screen',
|
|
|
|
updateProgress(count: number) {
|
|
|
|
if (count > 0) {
|
|
|
|
const message = window.i18n('loadingMessages', [count.toString()]);
|
|
|
|
this.$('.message').text(message);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
render_attributes: {
|
|
|
|
message: window.i18n('loading'),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Whisper.InboxView = Whisper.View.extend({
|
|
|
|
template: () => $('#two-column').html(),
|
2021-10-12 23:59:08 +00:00
|
|
|
className: 'Inbox',
|
2021-09-17 18:27:53 +00:00
|
|
|
initialize(
|
|
|
|
options: {
|
|
|
|
initialLoadComplete?: boolean;
|
|
|
|
window?: typeof window;
|
|
|
|
} = {}
|
|
|
|
) {
|
|
|
|
this.ready = false;
|
|
|
|
this.render();
|
|
|
|
|
|
|
|
this.conversation_stack = new ConversationStack({
|
|
|
|
el: this.$('.conversation-stack'),
|
|
|
|
});
|
|
|
|
|
|
|
|
this.renderWhatsNew();
|
|
|
|
|
|
|
|
Whisper.events.on('refreshConversation', ({ oldId, newId }) => {
|
|
|
|
const convo = this.conversation_stack.lastConversation;
|
|
|
|
if (convo && convo.get('id') === oldId) {
|
|
|
|
this.conversation_stack.open(newId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Close current opened conversation to reload the group information once
|
|
|
|
// linked.
|
|
|
|
Whisper.events.on('setupAsNewDevice', () => {
|
|
|
|
this.conversation_stack.unload();
|
|
|
|
});
|
|
|
|
|
2022-04-05 00:38:22 +00:00
|
|
|
window.Whisper.events.on('showConversation', (id, messageId) => {
|
|
|
|
const conversation = window.ConversationController.get(id);
|
|
|
|
strictAssert(conversation, 'Conversation must be found');
|
2021-11-12 01:17:29 +00:00
|
|
|
|
2022-04-05 00:38:22 +00:00
|
|
|
conversation.setMarkedUnread(false);
|
2021-11-12 01:17:29 +00:00
|
|
|
|
2022-04-05 00:38:22 +00:00
|
|
|
const { openConversationExternal } = window.reduxActions.conversations;
|
|
|
|
if (openConversationExternal) {
|
|
|
|
openConversationExternal(conversation.id, messageId);
|
2021-09-17 18:27:53 +00:00
|
|
|
}
|
2022-04-05 00:38:22 +00:00
|
|
|
|
|
|
|
this.conversation_stack.open(conversation, messageId);
|
|
|
|
});
|
2021-09-17 18:27:53 +00:00
|
|
|
|
|
|
|
window.Whisper.events.on('loadingProgress', count => {
|
|
|
|
const view = this.appLoadingScreen;
|
|
|
|
if (view) {
|
|
|
|
view.updateProgress(count);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!options.initialLoadComplete) {
|
|
|
|
this.appLoadingScreen = new AppLoadingScreen();
|
|
|
|
this.appLoadingScreen.render();
|
|
|
|
this.appLoadingScreen.$el.prependTo(this.el);
|
|
|
|
this.startConnectionListener();
|
|
|
|
} else {
|
|
|
|
this.setupLeftPane();
|
|
|
|
}
|
|
|
|
|
|
|
|
Whisper.events.on('pack-install-failed', () => {
|
2021-09-22 20:59:54 +00:00
|
|
|
showToast(ToastStickerPackInstallFailed);
|
2021-09-17 18:27:53 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
render_attributes: {
|
|
|
|
welcomeToSignal: window.i18n('welcomeToSignal'),
|
|
|
|
// TODO DESKTOP-1451: add back the selectAContact message
|
|
|
|
selectAContact: '',
|
|
|
|
},
|
|
|
|
events: {
|
|
|
|
click: 'onClick',
|
|
|
|
},
|
|
|
|
renderWhatsNew() {
|
2021-10-23 00:41:45 +00:00
|
|
|
if (this.whatsNewLink) {
|
2021-09-17 18:27:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-10-23 00:41:45 +00:00
|
|
|
const { showWhatsNewModal } = window.reduxActions.globalModals;
|
|
|
|
this.whatsNewLink = new Whisper.ReactWrapperView({
|
|
|
|
Component: window.Signal.Components.WhatsNewLink,
|
2021-09-17 18:27:53 +00:00
|
|
|
props: {
|
|
|
|
i18n: window.i18n,
|
2021-10-23 00:41:45 +00:00
|
|
|
showWhatsNewModal,
|
2021-09-17 18:27:53 +00:00
|
|
|
},
|
|
|
|
});
|
2021-10-23 00:41:45 +00:00
|
|
|
this.$('.whats-new-placeholder').append(this.whatsNewLink.el);
|
2021-09-17 18:27:53 +00:00
|
|
|
},
|
|
|
|
setupLeftPane() {
|
|
|
|
if (this.leftPaneView) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.leftPaneView = new Whisper.ReactWrapperView({
|
|
|
|
className: 'left-pane-wrapper',
|
|
|
|
JSX: window.Signal.State.Roots.createLeftPane(window.reduxStore),
|
|
|
|
});
|
|
|
|
|
2021-10-12 23:59:08 +00:00
|
|
|
this.$('.left-pane-placeholder').replaceWith(this.leftPaneView.el);
|
2021-09-17 18:27:53 +00:00
|
|
|
},
|
|
|
|
startConnectionListener() {
|
|
|
|
this.interval = setInterval(() => {
|
|
|
|
const status = window.getSocketStatus();
|
|
|
|
switch (status) {
|
|
|
|
case 'CONNECTING':
|
|
|
|
break;
|
|
|
|
case 'OPEN':
|
|
|
|
clearInterval(this.interval);
|
|
|
|
// if we've connected, we can wait for real empty event
|
|
|
|
this.interval = null;
|
|
|
|
break;
|
|
|
|
case 'CLOSING':
|
|
|
|
case 'CLOSED':
|
|
|
|
clearInterval(this.interval);
|
|
|
|
this.interval = null;
|
|
|
|
// if we failed to connect, we pretend we got an empty event
|
|
|
|
this.onEmpty();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log.warn(
|
|
|
|
`startConnectionListener: Found unexpected socket status ${status}; calling onEmpty() manually.`
|
|
|
|
);
|
|
|
|
this.onEmpty();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}, 1000);
|
|
|
|
},
|
|
|
|
onEmpty() {
|
|
|
|
this.setupLeftPane();
|
|
|
|
|
|
|
|
const view = this.appLoadingScreen;
|
|
|
|
if (view) {
|
|
|
|
this.appLoadingScreen = null;
|
|
|
|
view.remove();
|
|
|
|
|
|
|
|
const searchInput = document.querySelector(
|
2021-11-01 18:43:02 +00:00
|
|
|
'.LeftPaneSearchInput__input'
|
2021-09-17 18:27:53 +00:00
|
|
|
) as HTMLElement;
|
|
|
|
searchInput?.focus?.();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|