126 lines
5.1 KiB
TypeScript
126 lines
5.1 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 CombinedGuild from './guild-combined';
|
|
import { Channel } from './data-types';
|
|
import Q from './q-module';
|
|
|
|
export default class Actions {
|
|
static async fetchAndUpdateConnection(ui: UI, guild: CombinedGuild) {
|
|
// Explicitly not using withPotentialError to make this simpler
|
|
try {
|
|
let connection = await guild.fetchConnectionInfo();
|
|
ui.setActiveConnection(guild, connection);
|
|
} catch (e) {
|
|
LOG.error('Error updating current connection', e);
|
|
ui.setActiveConnection(guild, { id: null, avatarResourceId: null, displayName: 'Error', status: 'Error', privileges: [], roleName: null, roleColor: null, rolePriority: null });
|
|
}
|
|
}
|
|
|
|
static async fetchAndUpdateMembers(q: Q, ui: UI, guild: CombinedGuild) {
|
|
await Util.withPotentialErrorWarnOnCancel(q, {
|
|
taskFunc: async () => {
|
|
if (ui.activeGuild === null || ui.activeGuild.id !== guild.id) return;
|
|
let members = await guild.fetchMembers();
|
|
await ui.setMembers(guild, members);
|
|
},
|
|
errorIndicatorAddFunc: async (errorIndicatorElement) => {
|
|
await ui.setMembersErrorIndicator(guild, errorIndicatorElement);
|
|
},
|
|
errorContainer: q.$('#guild-members'),
|
|
errorMessage: 'Error loading members'
|
|
});
|
|
}
|
|
|
|
static async fetchAndUpdateChannels(q: Q, ui: UI, guild: CombinedGuild) {
|
|
await Util.withPotentialErrorWarnOnCancel(q, {
|
|
taskFunc: async () => {
|
|
if (ui.activeGuild === null || ui.activeGuild.id !== guild.id) return;
|
|
let channels = await guild.fetchChannels();
|
|
await ui.setChannels(guild, channels);
|
|
if (ui.activeGuild === null || ui.activeGuild.id !== guild.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(guild, errorIndicatorElement);
|
|
},
|
|
errorContainer: q.$('#channel-list'),
|
|
errorMessage: 'Error fetching channels'
|
|
});
|
|
}
|
|
|
|
static async fetchAndUpdateMessagesRecent(q: Q, ui: UI, guild: CombinedGuild, channel: Channel | { id: string }) {
|
|
await Util.withPotentialErrorWarnOnCancel(q, {
|
|
taskFunc: async () => {
|
|
if (ui.activeGuild === null || ui.activeGuild.id !== guild.id) return;
|
|
if (ui.activeChannel === null || ui.activeChannel.id !== channel.id) return;
|
|
let messages = await guild.fetchMessagesRecent(channel.id, Globals.MESSAGES_PER_REQUEST);
|
|
await ui.setMessages(guild, channel, messages, { atTop: messages.length < Globals.MESSAGES_PER_REQUEST, atBottom: true });
|
|
},
|
|
errorIndicatorAddFunc: async (errorIndicatorElement) => {
|
|
await ui.setMessagesErrorIndicator(guild, channel, errorIndicatorElement);
|
|
},
|
|
errorContainer: q.$('#channel-feed'),
|
|
errorMessage: 'Error fetching messages'
|
|
});
|
|
}
|
|
|
|
static async fetchAndUpdateMessagesBefore(q: Q, ui: UI, guild: CombinedGuild, channel: Channel) {
|
|
await Util.withPotentialErrorWarnOnCancel(q, {
|
|
taskFunc: async () => {
|
|
if (ui.activeGuild === null || ui.activeGuild.id !== guild.id) return;
|
|
if (ui.activeChannel === null || ui.activeChannel.id !== channel.id) return;
|
|
let topPair = ui.getTopMessagePair();
|
|
if (topPair == null) return;
|
|
let messages = await guild.fetchMessagesBefore(channel.id, topPair.message.id, Globals.MESSAGES_PER_REQUEST);
|
|
if (messages && messages.length > 0) {
|
|
await ui.addMessagesBefore(guild, channel, messages, topPair.message);
|
|
} else {
|
|
ui.messagesAtTop = true;
|
|
}
|
|
},
|
|
errorIndicatorAddFunc: async (errorIndicatorElement) => {
|
|
await ui.addMessagesErrorIndicatorBefore(guild, channel, errorIndicatorElement);
|
|
},
|
|
errorContainer: q.$('#channel-feed'),
|
|
errorClasses: [ 'before' ],
|
|
errorMessage: 'Error loading older messages'
|
|
});
|
|
}
|
|
|
|
static async fetchAndUpdateMessagesAfter(q: Q, ui: UI, guild: CombinedGuild, channel: Channel) {
|
|
await Util.withPotentialErrorWarnOnCancel(q, {
|
|
taskFunc: async () => {
|
|
if (ui.activeGuild === null || ui.activeGuild.id !== guild.id) return;
|
|
if (ui.activeChannel === null || ui.activeChannel.id !== channel.id) return;
|
|
let bottomPair = ui.getBottomMessagePair();
|
|
if (bottomPair == null) return;
|
|
let messages = await guild.fetchMessagesAfter(channel.id, bottomPair.message.id, Globals.MESSAGES_PER_REQUEST);
|
|
if (messages && messages.length > 0) {
|
|
await ui.addMessagesAfter(guild, channel, messages, bottomPair.message);
|
|
} else {
|
|
ui.messagesAtBottom = true;
|
|
}
|
|
},
|
|
errorIndicatorAddFunc: async (errorIndicatorElement) => {
|
|
await ui.addMessagesErrorIndicatorAfter(guild, channel, errorIndicatorElement);
|
|
},
|
|
errorContainer: q.$('#channel-feed'),
|
|
errorClasses: [ 'after' ],
|
|
errorMessage: 'Error loading newer messages'
|
|
});
|
|
}
|
|
}
|