126 lines
5.2 KiB
TypeScript
126 lines
5.2 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 {
|
|
static async fetchAndUpdateConnection(ui: UI, server: ClientController) {
|
|
// Explicitly not using withPotentialError to make this simpler
|
|
try {
|
|
let connection = await server.fetchConnectionInfo();
|
|
ui.setActiveConnection(server, connection);
|
|
} catch (e) {
|
|
LOG.error('Error updating current connection', e);
|
|
ui.setActiveConnection(server, { id: null, avatarResourceId: null, displayName: 'Error', status: 'Error', privileges: [], roleName: null, roleColor: null, rolePriority: null });
|
|
}
|
|
}
|
|
|
|
static async fetchAndUpdateMembers(q: Q, ui: UI, server: ClientController) {
|
|
await Util.withPotentialErrorWarnOnCancel(q, {
|
|
taskFunc: async () => {
|
|
if (ui.activeServer === null || ui.activeServer.id !== server.id) return;
|
|
let members = await server.grabMembers();
|
|
await ui.setMembers(server, members);
|
|
},
|
|
errorIndicatorAddFunc: async (errorIndicatorElement) => {
|
|
await ui.setMembersErrorIndicator(server, errorIndicatorElement);
|
|
},
|
|
errorContainer: q.$('#server-members'),
|
|
errorMessage: 'Error loading members'
|
|
});
|
|
}
|
|
|
|
static async fetchAndUpdateChannels(q: Q, ui: UI, server: ClientController) {
|
|
await Util.withPotentialErrorWarnOnCancel(q, {
|
|
taskFunc: async () => {
|
|
if (ui.activeServer === null || ui.activeServer.id !== server.id) return;
|
|
let channels = await server.grabChannels();
|
|
await ui.setChannels(server, channels);
|
|
if (ui.activeServer === null || ui.activeServer.id !== server.id) return;
|
|
if (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 ui.setChannelsErrorIndicator(server, errorIndicatorElement);
|
|
},
|
|
errorContainer: q.$('#channel-list'),
|
|
errorMessage: 'Error fetching channels'
|
|
});
|
|
}
|
|
|
|
static async fetchAndUpdateMessagesRecent(q: Q, ui: UI, server: ClientController, channel: Channel | { id: string }) {
|
|
await Util.withPotentialErrorWarnOnCancel(q, {
|
|
taskFunc: async () => {
|
|
if (ui.activeServer === null || ui.activeServer.id !== server.id) return;
|
|
if (ui.activeChannel === null || ui.activeChannel.id !== channel.id) return;
|
|
let messages = await server.grabRecentMessages(channel.id, Globals.MESSAGES_PER_REQUEST);
|
|
await ui.setMessages(server, channel, messages, { atTop: messages.length < Globals.MESSAGES_PER_REQUEST, atBottom: true });
|
|
},
|
|
errorIndicatorAddFunc: async (errorIndicatorElement) => {
|
|
await ui.setMessagesErrorIndicator(server, channel, errorIndicatorElement);
|
|
},
|
|
errorContainer: q.$('#channel-feed'),
|
|
errorMessage: 'Error fetching messages'
|
|
});
|
|
}
|
|
|
|
static async fetchAndUpdateMessagesBefore(q: Q, ui: UI, server: ClientController, channel: Channel) {
|
|
await Util.withPotentialErrorWarnOnCancel(q, {
|
|
taskFunc: async () => {
|
|
if (ui.activeServer === null || ui.activeServer.id !== server.id) return;
|
|
if (ui.activeChannel === null || ui.activeChannel.id !== channel.id) return;
|
|
let topPair = 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 ui.addMessagesBefore(server, channel, messages, topPair.message);
|
|
} else {
|
|
ui.messagesAtTop = true;
|
|
}
|
|
},
|
|
errorIndicatorAddFunc: async (errorIndicatorElement) => {
|
|
await ui.addMessagesErrorIndicatorBefore(server, channel, errorIndicatorElement);
|
|
},
|
|
errorContainer: q.$('#channel-feed'),
|
|
errorClasses: [ 'before' ],
|
|
errorMessage: 'Error loading older messages'
|
|
});
|
|
}
|
|
|
|
static async fetchAndUpdateMessagesAfter(q: Q, ui: UI, server: ClientController, channel: Channel) {
|
|
await Util.withPotentialErrorWarnOnCancel(q, {
|
|
taskFunc: async () => {
|
|
if (ui.activeServer === null || ui.activeServer.id !== server.id) return;
|
|
if (ui.activeChannel === null || ui.activeChannel.id !== channel.id) return;
|
|
let bottomPair = 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 ui.addMessagesAfter(server, channel, messages, bottomPair.message);
|
|
} else {
|
|
ui.messagesAtBottom = true;
|
|
}
|
|
},
|
|
errorIndicatorAddFunc: async (errorIndicatorElement) => {
|
|
await ui.addMessagesErrorIndicatorAfter(server, channel, errorIndicatorElement);
|
|
},
|
|
errorContainer: q.$('#channel-feed'),
|
|
errorClasses: [ 'after' ],
|
|
errorMessage: 'Error loading newer messages'
|
|
});
|
|
}
|
|
}
|