cordis/client/webapp/actions.ts
2021-10-30 12:26:41 -05:00

137 lines
6.1 KiB
TypeScript

import * as electronRemote from '@electron/remote';
const electronConsole = electronRemote.getGlobal('console') as Console;
import Logger from '../../logger/logger';
const LOG = new Logger(__filename, electronConsole);
import Util from './util';
import Globals from './globals';
import { $, $$, $$$, $$$$ } from './elements/require/q-module';
import UI from './ui';
import ClientController from './client-controller';
import { Channel } from './data-types';
export default class Actions {
private document: Document;
private ui: UI;
constructor (document: Document, ui: UI) {
this.document = document;
this.ui = ui;
}
async fetchAndUpdateConnection(server: ClientController) {
// Explicitly not using withPotentialError to make this simpler
try {
let connection = await server.fetchConnectionInfo();
this.ui.setActiveConnection(server, connection);
} catch (e) {
LOG.error('Error updating current connection', e);
this.ui.setActiveConnection(server, { id: null, avatarResourceId: null, displayName: 'Error', status: 'Error', privileges: [] });
}
}
async fetchAndUpdateMembers(server: ClientController) {
$.setDocument(this.document);
await Util.withPotentialErrorWarnOnCancel({
taskFunc: async () => {
if (!this.ui.isActiveServer(server)) return;
let members = await server.grabMembers();
await this.ui.setMembers(server, members);
},
errorIndicatorAddFunc: async (errorIndicatorElement) => {
await this.ui.setMembersErrorIndicator(server, errorIndicatorElement);
},
errorContainer: $('#server-members'),
errorMessage: 'Error loading members'
});
}
async fetchAndUpdateChannels(server: ClientController) {
$.setDocument(this.document);
await Util.withPotentialErrorWarnOnCancel({
taskFunc: async () => {
if (!this.ui.isActiveServer(server)) return;
let channels = await server.grabChannels();
await this.ui.setChannels(server, channels);
if (!this.ui.isActiveServer(server)) return;
if (!this.ui.hasActiveChannel()) {
// click on the first channel in the list if no channel is active yet
let element = $('#channel-list .channel');
if (element) {
element.click();
}
}
},
errorIndicatorAddFunc: async (errorIndicatorElement) => {
await this.ui.setChannelsErrorIndicator(server, errorIndicatorElement);
},
errorContainer: $('#channel-list'),
errorMessage: 'Error fetching channels'
});
}
async fetchAndUpdateMessagesRecent(server: ClientController, channel: Channel) {
$.setDocument(this.document);
await Util.withPotentialErrorWarnOnCancel({
taskFunc: async () => {
if (!this.ui.isActiveServer(server) || !this.ui.isActiveChannel(channel)) return;
let messages = await server.grabRecentMessages(channel.id, Globals.MESSAGES_PER_REQUEST);
await this.ui.setMessages(server, channel, messages, { atTop: messages.length < Globals.MESSAGES_PER_REQUEST, atBottom: true });
},
errorIndicatorAddFunc: async (errorIndicatorElement) => {
await this.ui.setMessagesErrorIndicator(server, channel, errorIndicatorElement);
},
errorContainer: $('#channel-feed'),
errorMessage: 'Error fetching messages'
});
}
async fetchAndUpdateMessagesBefore(server: ClientController, channel: Channel) {
$.setDocument(this.document);
await Util.withPotentialErrorWarnOnCancel({
taskFunc: async () => {
if (!this.ui.isActiveServer(server) || !this.ui.isActiveChannel(channel)) return;
let topPair = this.ui.getTopMessagePair();
if (topPair == null) return;
let messages = await server.fetchMessagesBefore(channel.id, topPair.message.id, Globals.MESSAGES_PER_REQUEST);
if (messages && messages.length > 0) {
await this.ui.addMessagesBefore(server, channel, messages, topPair.message);
} else {
this.ui.messagesAtTop = true;
}
},
errorIndicatorAddFunc: async (errorIndicatorElement) => {
await this.ui.addMessagesErrorIndicatorBefore(server, channel, errorIndicatorElement);
},
errorContainer: $('#channel-feed'),
errorClasses: [ 'before' ],
errorMessage: 'Error loading older messages'
});
}
async fetchAndUpdateMessagesAfter(server: ClientController, channel: Channel) {
$.setDocument(this.document);
await Util.withPotentialErrorWarnOnCancel({
taskFunc: async () => {
if (!this.ui.isActiveServer(server) || !this.ui.isActiveChannel(channel)) return;
let bottomPair = this.ui.getBottomMessagePair();
if (bottomPair == null) return;
let messages = await server.fetchMessagesAfter(channel.id, bottomPair.message.id, Globals.MESSAGES_PER_REQUEST);
if (messages && messages.length > 0) {
await this.ui.addMessagesAfter(server, channel, messages, bottomPair.message);
} else {
this.ui.messagesAtBottom = true;
}
},
errorIndicatorAddFunc: async (errorIndicatorElement) => {
await this.ui.addMessagesErrorIndicatorAfter(server, channel, errorIndicatorElement);
},
errorContainer: $('#channel-feed'),
errorClasses: [ 'after' ],
errorMessage: 'Error loading newer messages'
});
}
}