cordis/client/webapp/actions.ts

134 lines
5.5 KiB
TypeScript

import * as electronRemote from '@electron/remote';
const electronConsole = electronRemote.getGlobal('console') as Console;
import Logger from '../../logger/logger';
const LOG = Logger.create(__filename, electronConsole);
import Util from './util';
import Globals from './globals';
import UI from './ui';
import ClientController from './client-controller';
import { Channel } from './data-types';
import Q from './q-module';
export default class Actions {
private ui: UI;
constructor (ui: UI) {
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: [], roleName: null, roleColor: null, rolePriority: null });
}
}
async fetchAndUpdateMembers(q: Q, server: ClientController) {
await Util.withPotentialErrorWarnOnCancel(q, {
taskFunc: async () => {
if (this.ui.activeServer === null || this.ui.activeServer.id !== server.id) return;
let members = await server.grabMembers();
await this.ui.setMembers(server, members);
},
errorIndicatorAddFunc: async (errorIndicatorElement) => {
await this.ui.setMembersErrorIndicator(server, errorIndicatorElement);
},
errorContainer: q.$('#server-members'),
errorMessage: 'Error loading members'
});
}
async fetchAndUpdateChannels(q: Q, server: ClientController) {
await Util.withPotentialErrorWarnOnCancel(q, {
taskFunc: async () => {
if (this.ui.activeServer === null || this.ui.activeServer.id !== server.id) return;
let channels = await server.grabChannels();
await this.ui.setChannels(this, server, channels);
if (this.ui.activeServer === null || this.ui.activeServer.id !== server.id) return;
if (this.ui.activeChannel === null) {
// click on the first channel in the list if no channel is active yet
let element = q.$_('#channel-list .channel');
if (element) {
element.click();
}
}
},
errorIndicatorAddFunc: async (errorIndicatorElement) => {
await this.ui.setChannelsErrorIndicator(server, errorIndicatorElement);
},
errorContainer: q.$('#channel-list'),
errorMessage: 'Error fetching channels'
});
}
async fetchAndUpdateMessagesRecent(q: Q, server: ClientController, channel: Channel | { id: string }) {
await Util.withPotentialErrorWarnOnCancel(q, {
taskFunc: async () => {
LOG.debug('active server is s#' + this.ui.activeServer?.id);
LOG.debug('active channel is ch#' + this.ui.activeChannel?.id);
if (this.ui.activeServer === null || this.ui.activeServer.id !== server.id) return;
if (this.ui.activeChannel === null || this.ui.activeChannel.id !== server.id) 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: q.$('#channel-feed'),
errorMessage: 'Error fetching messages'
});
}
async fetchAndUpdateMessagesBefore(q: Q, server: ClientController, channel: Channel) {
await Util.withPotentialErrorWarnOnCancel(q, {
taskFunc: async () => {
if (this.ui.activeServer === null || this.ui.activeServer.id !== server.id) return;
if (this.ui.activeChannel === null || this.ui.activeChannel.id !== server.id) 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: q.$('#channel-feed'),
errorClasses: [ 'before' ],
errorMessage: 'Error loading older messages'
});
}
async fetchAndUpdateMessagesAfter(q: Q, server: ClientController, channel: Channel) {
await Util.withPotentialErrorWarnOnCancel(q, {
taskFunc: async () => {
if (this.ui.activeServer === null || this.ui.activeServer.id !== server.id) return;
if (this.ui.activeChannel === null || this.ui.activeChannel.id !== server.id) 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: q.$('#channel-feed'),
errorClasses: [ 'after' ],
errorMessage: 'Error loading newer messages'
});
}
}