fchat-rising/chat/core.ts

148 lines
5.2 KiB
TypeScript
Raw Permalink Normal View History

import Vue, {WatchHandler} from 'vue';
2019-07-07 01:37:15 +00:00
import { CacheManager } from '../learn/cache-manager';
2019-09-17 17:14:14 +00:00
import {Channels, Characters} from '../fchat';
2017-09-02 01:50:31 +00:00
import BBCodeParser from './bbcode';
import {Settings as SettingsImpl} from './common';
2019-09-17 17:14:14 +00:00
import Conversations from './conversations';
2017-09-02 01:50:31 +00:00
import {Channel, Character, Connection, Conversation, Logs, Notifications, Settings, State as StateInterface} from './interfaces';
import { AdCoordinatorGuest } from './ads/ad-coordinator-guest';
2022-12-25 05:44:55 +00:00
import { AdCenter } from './ads/ad-center';
2020-06-30 21:51:06 +00:00
import { GeneralSettings } from '../electron/common';
2020-11-27 06:12:47 +00:00
import { SiteSession } from '../site/site-session';
2022-01-01 00:06:08 +00:00
import _ from 'lodash';
2024-01-27 03:45:59 +00:00
import { initYiffbot4000Integration } from '../learn/yiffbot';
2017-09-02 01:50:31 +00:00
function createBBCodeParser(): BBCodeParser {
const parser = new BBCodeParser();
for(const tag of state.settings.disallowedTags)
parser.removeTag(tag);
return parser;
}
class State implements StateInterface {
_settings: Settings | undefined = undefined;
hiddenUsers: string[] = [];
2023-05-30 01:35:04 +00:00
favoriteEIcons: Record<string, boolean> = {};
2017-09-02 01:50:31 +00:00
get settings(): Settings {
if(this._settings === undefined) throw new Error('Settings load failed.');
return this._settings;
}
set settings(value: Settings) {
this._settings = value;
//tslint:disable-next-line:no-floating-promises
if(data.settingsStore !== undefined) data.settingsStore.set('settings', value);
data.bbCodeParser = createBBCodeParser();
}
}
interface VueState {
readonly channels: Channel.State
readonly characters: Character.State
readonly conversations: Conversation.State
readonly state: StateInterface
}
const state = new State();
const vue = <Vue & VueState>new Vue({
data: {
channels: undefined,
characters: undefined,
conversations: undefined,
state
}
});
const data = {
connection: <Connection | undefined>undefined,
2018-03-28 13:51:05 +00:00
logs: <Logs | undefined>undefined,
2017-09-02 01:50:31 +00:00
settingsStore: <Settings.Store | undefined>undefined,
state: vue.state,
bbCodeParser: new BBCodeParser(),
2017-09-02 01:50:31 +00:00
conversations: <Conversation.State | undefined>undefined,
channels: <Channel.State | undefined>undefined,
characters: <Character.State | undefined>undefined,
notifications: <Notifications | undefined>undefined,
2019-07-07 01:37:15 +00:00
cache: <CacheManager | undefined>undefined,
adCoordinator: <AdCoordinatorGuest | undefined>undefined,
2022-12-25 05:44:55 +00:00
adCenter: <AdCenter | undefined>undefined,
2020-11-27 06:12:47 +00:00
siteSession: <SiteSession | undefined>undefined,
2019-09-17 17:14:14 +00:00
register<K extends 'characters' | 'conversations' | 'channels'>(module: K, subState: VueState[K]): void {
2017-09-02 01:50:31 +00:00
Vue.set(vue, module, subState);
2019-09-17 17:14:14 +00:00
(<VueState[K]>data[module]) = subState;
2017-09-02 01:50:31 +00:00
},
2021-09-11 00:21:31 +00:00
watch<T>(getter: (this: VueState) => T, callback: (n: any, o: any) => void): void {
2017-09-02 01:50:31 +00:00
vue.$watch(getter, callback);
},
async reloadSettings(): Promise<void> {
2022-01-01 00:06:08 +00:00
const s = await core.settingsStore.get('settings');
state._settings = _.mergeWith(new SettingsImpl(), s, (oVal, sVal) => {
if (_.isArray(oVal) && _.isArray(sVal)) {
return sVal;
}
});
const hiddenUsers = await core.settingsStore.get('hiddenUsers');
state.hiddenUsers = hiddenUsers !== undefined ? hiddenUsers : [];
2023-05-30 01:35:04 +00:00
const favoriteEIcons = await core.settingsStore.get('favoriteEIcons');
state.favoriteEIcons = favoriteEIcons !== undefined ? favoriteEIcons : {};
2017-09-02 01:50:31 +00:00
}
};
2020-06-30 21:51:06 +00:00
export function init(
this: any, connection: Connection, settings: GeneralSettings, logsClass: new() => Logs,
settingsClass: new() => Settings.Store, notificationsClass: new() => Notifications): void {
2017-09-02 01:50:31 +00:00
data.connection = connection;
data.logs = new logsClass();
data.settingsStore = new settingsClass();
data.notifications = new notificationsClass();
2019-07-07 01:37:15 +00:00
data.cache = new CacheManager();
data.adCoordinator = new AdCoordinatorGuest();
2022-12-25 05:44:55 +00:00
data.adCenter = new AdCenter();
2020-11-27 06:12:47 +00:00
data.siteSession = new SiteSession();
2019-07-07 01:37:15 +00:00
2020-06-30 21:51:06 +00:00
(data.state as any).generalSettings = settings;
2019-07-07 01:37:15 +00:00
2019-09-17 17:14:14 +00:00
data.register('characters', Characters(connection));
data.register('channels', Channels(connection, core.characters));
data.register('conversations', Conversations());
2021-03-17 23:29:56 +00:00
data.watch(() => state.hiddenUsers, async(newValue) => {
if(data.settingsStore !== undefined) await data.settingsStore.set('hiddenUsers', newValue);
});
2024-01-27 03:45:59 +00:00
initYiffbot4000Integration();
2017-09-02 01:50:31 +00:00
connection.onEvent('connecting', async() => {
await data.reloadSettings();
data.bbCodeParser = createBBCodeParser();
});
}
2018-01-06 16:14:21 +00:00
export interface Core {
2017-09-02 01:50:31 +00:00
readonly connection: Connection
2018-03-28 13:51:05 +00:00
readonly logs: Logs
2017-09-02 01:50:31 +00:00
readonly state: StateInterface
readonly settingsStore: Settings.Store
readonly conversations: Conversation.State
readonly characters: Character.State
readonly channels: Channel.State
readonly bbCodeParser: BBCodeParser
readonly notifications: Notifications
2019-07-07 01:37:15 +00:00
readonly cache: CacheManager
readonly adCoordinator: AdCoordinatorGuest;
2022-12-25 05:44:55 +00:00
readonly adCenter: AdCenter;
2020-11-27 06:12:47 +00:00
readonly siteSession: SiteSession;
2019-07-07 01:37:15 +00:00
watch<T>(getter: (this: VueState) => T, callback: WatchHandler<T>): void
2018-01-06 16:14:21 +00:00
}
const core = <Core><any>data; /*tslint:disable-line:no-any*///hack
2017-09-02 01:50:31 +00:00
export default core;