mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 12:02:50 +08:00
feat: add plugin application runtime and unified instance authorization
This commit is contained in:
1 parent
8bca848c21
commit
3430a8a753
80 files changed
+2133
-1004
No files matched your search
@@ -1,54 +1,47 @@
|
||||
import { reactive, ref, shallowRef } from "vue";
|
||||
import { createAdminRequests } from "../services/admin-requests.js";
|
||||
import { isAdminRequestCancelled, type AdminApi } from "../services/admin-api.js";
|
||||
import { parsePluginPackage, PLUGIN_LIMIT_BYTES, type InstalledPlugin, type PluginCatalog, type PluginStoreEntry } from "../../../src/shared/plugins.js";
|
||||
import { reactive, ref, shallowRef } from 'vue';
|
||||
import { createAdminRequests } from '../services/admin-requests.js';
|
||||
import { isAdminRequestCancelled, type AdminApi } from '../services/admin-api.js';
|
||||
import { parsePluginPackage, PLUGIN_LIMIT_BYTES, type InstalledPlugin, type PluginCatalog, type PluginStoreEntry, type PluginRuntimeSettings, type PluginSettingsInput } from '../../../src/shared/plugins.js';
|
||||
|
||||
export function useAdminPlugins(api: AdminApi) {
|
||||
const installed = shallowRef<InstalledPlugin[]>([]), catalog = shallowRef<PluginCatalog | null>(null);
|
||||
const busy = ref(false), error = ref(''), requests = createAdminRequests();
|
||||
const drafts = reactive<Record<string, { enabled: boolean; scope: 'server'|'instance'; targets: string; config: string }>>({});
|
||||
function apply(records: InstalledPlugin[]) {
|
||||
installed.value = records;
|
||||
for (const record of records) if (!drafts[record.manifest.id]) drafts[record.manifest.id] = {
|
||||
enabled: record.settings.enabled, scope: record.settings.scope, targets: record.settings.targets.join('\n'), config: JSON.stringify(record.settings.config, null, 2),
|
||||
};
|
||||
for (const id of Object.keys(drafts)) if (!records.some(r => r.manifest.id === id)) delete drafts[id];
|
||||
const installed=shallowRef<InstalledPlugin[]>([]),catalog=shallowRef<PluginCatalog|null>(null),runtime=shallowRef<PluginRuntimeSettings|null>(null);
|
||||
const origin=ref(''),busy=ref(false),error=ref(''),catalogError=ref(''),requests=createAdminRequests();
|
||||
const drafts=reactive<Record<string,{enabled:boolean;trusted:boolean;scope:'server'|'instance';targets:string;config:string;serverConfigAction:'keep'|'replace'|'remove';serverConfig:string}>>({});
|
||||
function apply(records:InstalledPlugin[]){
|
||||
installed.value=records;
|
||||
for(const record of records)if(!drafts[record.manifest.id])drafts[record.manifest.id]={enabled:record.settings.enabled,trusted:record.settings.trusted,scope:record.settings.scope,targets:record.settings.targets.join('\n'),config:JSON.stringify(record.settings.config,null,2),serverConfigAction:'keep',serverConfig:''};
|
||||
for(const id of Object.keys(drafts))if(!records.some(r=>r.manifest.id===id))delete drafts[id];
|
||||
}
|
||||
async function work(run: (signal: AbortSignal, current: () => boolean) => Promise<void>) {
|
||||
if (busy.value) return;
|
||||
const request = requests.begin('plugins'); busy.value = true; error.value = '';
|
||||
const timer = setTimeout(() => { if (request.isCurrent()) { error.value='PLUGIN_TIMEOUT'; requests.cancel('plugins'); busy.value=false; } }, 90_000);
|
||||
try { await run(request.signal, request.isCurrent); }
|
||||
catch (e) { if (request.isCurrent() && !isAdminRequestCancelled(e)) error.value = e instanceof Error ? e.message : 'PLUGIN_FAILED'; }
|
||||
finally { clearTimeout(timer); if (request.isCurrent()) busy.value = false; request.finish(); }
|
||||
async function work(run:(signal:AbortSignal,current:()=>boolean)=>Promise<void>){
|
||||
if(busy.value)return;const request=requests.begin('plugins');busy.value=true;error.value='';
|
||||
const timer=setTimeout(()=>{if(request.isCurrent()){error.value='PLUGIN_TIMEOUT';requests.cancel('plugins');busy.value=false;}},90_000);
|
||||
try{await run(request.signal,request.isCurrent);}catch(e){if(request.isCurrent()&&!isAdminRequestCancelled(e))error.value=e instanceof Error?e.message:'PLUGIN_FAILED';}
|
||||
finally{clearTimeout(timer);if(request.isCurrent())busy.value=false;request.finish();}
|
||||
}
|
||||
function load() { return work(async (signal, current) => {
|
||||
const result = await api.plugins(signal); if (current()) apply(result.plugins);
|
||||
// Installed packages remain manageable even when GitHub is unavailable.
|
||||
const store = await api.pluginStore(signal); if (current()) catalog.value = store;
|
||||
}); }
|
||||
async function install(entry: PluginStoreEntry) {
|
||||
return work(async (signal, current) => {
|
||||
const result = await api.installPlugin({ id: entry.id, revision: entry.revision, installedRevision: installed.value.find(p => p.manifest.id === entry.id)?.revision ?? null }, signal);
|
||||
if (current()) { delete drafts[entry.id]; apply([...installed.value.filter(p => p.manifest.id !== entry.id), result.plugin]); }
|
||||
});
|
||||
}
|
||||
function save(record: InstalledPlugin) {
|
||||
return work(async (signal, current) => {
|
||||
const draft = drafts[record.manifest.id], captured = JSON.stringify(draft);
|
||||
const settings = { enabled: draft.enabled, scope: draft.scope, targets: draft.targets.split(/\r?\n/).map(t => t.trim()).filter(Boolean), config: JSON.parse(draft.config) };
|
||||
const result = await api.configurePlugin(record.manifest.id, record.revision, settings, signal);
|
||||
if (current()) { if (JSON.stringify(drafts[record.manifest.id]) === captured) delete drafts[record.manifest.id]; apply(installed.value.map(p => p.manifest.id === record.manifest.id ? result.plugin : p)); }
|
||||
});
|
||||
}
|
||||
function remove(record: InstalledPlugin) { return work(async (signal, current) => { await api.removePlugin(record.manifest.id, record.revision, signal); if (current()) apply(installed.value.filter(p => p.manifest.id !== record.manifest.id)); }); }
|
||||
function upload(file: File) { return work(async (signal, current) => {
|
||||
if (file.size > PLUGIN_LIMIT_BYTES) throw new Error('PLUGIN_TOO_LARGE');
|
||||
const pack = parsePluginPackage(JSON.parse(await file.text())); if (!current()) return;
|
||||
const result = await api.uploadPlugin(pack, installed.value.find(p => p.manifest.id === pack.manifest.id)?.revision ?? null, signal);
|
||||
if (current()) { delete drafts[pack.manifest.id]; apply([...installed.value.filter(p => p.manifest.id !== pack.manifest.id), result.plugin]); }
|
||||
}); }
|
||||
function cancelRequests() { requests.reset(); busy.value = false; }
|
||||
function reset() { cancelRequests(); installed.value = []; catalog.value = null; error.value = ''; for (const id of Object.keys(drafts)) delete drafts[id]; }
|
||||
return { installed, catalog, busy, error, drafts, load, install, save, remove, upload, cancelRequests, reset };
|
||||
function load(){return work(async(signal,current)=>{
|
||||
const records=await api.plugins(signal);if(current())apply(records.plugins);
|
||||
const settings=await api.pluginRuntime(signal);if(current()){runtime.value=settings.runtime;origin.value=settings.runtime.publicOrigin;}
|
||||
try{const store=await api.pluginStore(signal);if(current()){catalog.value=store;catalogError.value='';}}catch(e){if(current()&&!isAdminRequestCancelled(e)){catalog.value=null;catalogError.value=e instanceof Error?e.message:'PLUGIN_STORE_UNAVAILABLE';}}
|
||||
});}
|
||||
function install(entry:PluginStoreEntry){return work(async(signal,current)=>{
|
||||
const result=await api.installPlugin({id:entry.id,revision:entry.revision,installedRevision:installed.value.find(p=>p.manifest.id===entry.id)?.revision??null},signal);
|
||||
if(current()){delete drafts[entry.id];apply([...installed.value.filter(p=>p.manifest.id!==entry.id),result.plugin]);}
|
||||
});}
|
||||
function save(record:InstalledPlugin){return work(async(signal,current)=>{
|
||||
const draft=drafts[record.manifest.id],captured=JSON.stringify(draft);
|
||||
const settings:PluginSettingsInput={enabled:draft.enabled,trusted:draft.trusted,scope:draft.scope,targets:draft.targets.split(/\r?\n/).map(t=>t.trim()).filter(Boolean),config:JSON.parse(draft.config),serverConfigAction:draft.serverConfigAction,...(draft.serverConfigAction==='replace'?{serverConfig:JSON.parse(draft.serverConfig)}:{})};
|
||||
const result=await api.configurePlugin(record.manifest.id,record.revision,settings,signal);
|
||||
if(current()){if(JSON.stringify(drafts[record.manifest.id])===captured)delete drafts[record.manifest.id];apply(installed.value.map(p=>p.manifest.id===record.manifest.id?result.plugin:p));}
|
||||
});}
|
||||
function restart(record:InstalledPlugin){return work(async(signal,current)=>{const result=await api.restartPlugin(record.manifest.id,record.revision,signal);if(current())apply(installed.value.map(p=>p.manifest.id===record.manifest.id?result.plugin:p));});}
|
||||
function saveRuntime(){return work(async(signal,current)=>{if(!runtime.value)return;const captured=origin.value,result=await api.configurePluginRuntime(captured,runtime.value.revision,signal);if(current()){runtime.value=result.runtime;if(origin.value===captured)origin.value=result.runtime.publicOrigin;}});}
|
||||
function remove(record:InstalledPlugin){return work(async(signal,current)=>{await api.removePlugin(record.manifest.id,record.revision,signal);if(current())apply(installed.value.filter(p=>p.manifest.id!==record.manifest.id));});}
|
||||
function upload(file:File){return work(async(signal,current)=>{
|
||||
if(file.size>PLUGIN_LIMIT_BYTES)throw new Error('PLUGIN_TOO_LARGE');const pack=parsePluginPackage(JSON.parse(await file.text()));if(!current())return;
|
||||
const result=await api.uploadPlugin(pack,installed.value.find(p=>p.manifest.id===pack.manifest.id)?.revision??null,signal);
|
||||
if(current()){delete drafts[pack.manifest.id];apply([...installed.value.filter(p=>p.manifest.id!==pack.manifest.id),result.plugin]);}
|
||||
});}
|
||||
function cancelRequests(){requests.reset();busy.value=false;for(const draft of Object.values(drafts)){draft.serverConfig='';draft.serverConfigAction='keep';}}
|
||||
function reset(){cancelRequests();installed.value=[];catalog.value=null;runtime.value=null;origin.value='';error.value='';catalogError.value='';for(const id of Object.keys(drafts))delete drafts[id];}
|
||||
return {installed,catalog,runtime,origin,busy,error,catalogError,drafts,load,install,save,restart,saveRuntime,remove,upload,cancelRequests,reset};
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { onScopeDispose, ref, shallowRef, type Ref } from "vue";
|
||||
import { computed, onScopeDispose, ref, shallowRef, watch, type Ref } from "vue";
|
||||
import { canUseSkin, thirdPartyAllowed } from '../services/instance-authorization.js';
|
||||
import { listInstalledSkins, loadLocalPreferences, saveLocalPreferences } from "../services/local-persistence.js";
|
||||
import { BUILTIN_SKIN_CATALOG, getPublicDefaultSkinId, isPublicSkinEnabled, listPublicSkins, type SkinCatalogEntry } from "../services/skin-catalog.js";
|
||||
import { activateSkin, BUILTIN_DARK_SKIN, BUILTIN_LIGHT_SKIN, clearCustomSkinStyle, getStoredSkinId } from "../services/skin-runtime.js";
|
||||
@@ -19,7 +20,8 @@ const fallbackId = () => isDarkTheme(getStoredTheme()) ? BUILTIN_DARK_SKIN : BUI
|
||||
/** One page owns initialization, choices and their late responses, including persistence. */
|
||||
export function usePublicSkin(options: PublicSkinOptions = {}) {
|
||||
const activeSkin = options.activeSkin ?? shallowRef<InstalledSkin | null>(null);
|
||||
const stored = getStoredSkinId();
|
||||
const candidate = getStoredSkinId();
|
||||
const stored = candidate && canUseSkin(candidate) ? candidate : null;
|
||||
const activeSkinId = ref(stored ?? fallbackId());
|
||||
const skinReady = ref(Boolean(stored && builtin(stored)));
|
||||
const skinLoadError = shallowRef<{ skinId: string } | null>(null);
|
||||
@@ -37,6 +39,7 @@ export function usePublicSkin(options: PublicSkinOptions = {}) {
|
||||
|
||||
async function apply(id: string, operation: ReturnType<typeof createSkinOperation>, persist = true, updateTheme = false, clearLoadError = true) {
|
||||
operation.check();
|
||||
if (!canUseSkin(id)) id = fallbackId();
|
||||
const entry = catalogSkins.value.find(skin => skin.id === id);
|
||||
const skin = await operation.wait(activateSkin(id, entry?.version, options.appVersion?.(), { signal: operation.signal }));
|
||||
if (!owns(operation)) return;
|
||||
@@ -119,5 +122,19 @@ export function usePublicSkin(options: PublicSkinOptions = {}) {
|
||||
await select(fallbackId());
|
||||
}
|
||||
|
||||
return { activeSkin, activeSkinId, skinReady, skinLoadError, installedSkins, catalogSkins, initialize, select, cancel, reset, switchToBuiltIn, switchToBuiltInAfterLoadError };
|
||||
watch(thirdPartyAllowed, allowed => {
|
||||
if (disposed) return;
|
||||
if (allowed) { void initialize(); return; }
|
||||
cancel();
|
||||
activeSkin.value = null;
|
||||
skinLoadError.value = null;
|
||||
clearCustomSkinStyle();
|
||||
activeSkinId.value = fallbackId();
|
||||
skinReady.value = true;
|
||||
void select(fallbackId());
|
||||
}, { flush: 'sync' });
|
||||
return { activeSkin, activeSkinId, skinReady, skinLoadError,
|
||||
installedSkins: computed(() => installedSkins.value.filter(skin => canUseSkin(skin.id))),
|
||||
catalogSkins: computed(() => catalogSkins.value.filter(skin => canUseSkin(skin.id))),
|
||||
initialize, select, cancel, reset, switchToBuiltIn, switchToBuiltInAfterLoadError };
|
||||
}
|
||||
@@ -7,7 +7,6 @@ import { createAccompaniment, type AccompanimentErrorCode } from "../voice/accom
|
||||
import { createWebRtcTransport } from "../voice/webrtc-transport.js";
|
||||
import { createVoiceConnection } from "../voice/connection.js";
|
||||
import { createVoiceCommands } from "../voice/commands.js";
|
||||
import { createPluginTransport } from "../services/plugin-transport.js";
|
||||
import { createVoiceSessionState } from "../voice/session-state.js";
|
||||
import { applySavedMemberVolumes } from "../voice/member-volume-preferences.js";
|
||||
import { createAudioDiagnostics, type AudioPermission, type VoiceAudioStatusSample } from "../voice/audio-diagnostics.js";
|
||||
@@ -201,7 +200,7 @@ export function useVoiceWebSocket() {
|
||||
const ws = shallowRef<WebSocket | null>(null);
|
||||
const serverInfo = shallowRef<VoiceServerInfo | null>(null);
|
||||
const state = reactive<VoiceState>({ connected: false, connecting: false, reconnecting: false, reconnectAttempt: 0, reconnectFailed: false, tsClientId: 0, error: "", errorCode: "", microphoneError: "", microphoneErrorCode: "", audioNotice: "", audioNoticeCode: "", channelSwitchedChannelId: "" });
|
||||
const plugins = createPluginTransport(() => ws.value, () => state.connected);
|
||||
const pluginSession = ref("");
|
||||
const sessionState = createVoiceSessionState({
|
||||
selfId: () => state.tsClientId,
|
||||
onMemberRemoved(id) {
|
||||
@@ -1025,7 +1024,7 @@ export function useVoiceWebSocket() {
|
||||
|
||||
/** All ways a session ends release the same owned media and pending work. */
|
||||
function releaseSessionResources(sendScreenStop = false): void {
|
||||
plugins.reset();
|
||||
pluginSession.value = "";
|
||||
chatGeneration++;
|
||||
inputDeviceGeneration++;
|
||||
outputDeviceGeneration++;
|
||||
@@ -1046,7 +1045,6 @@ export function useVoiceWebSocket() {
|
||||
}
|
||||
|
||||
function handleMessage(raw: unknown): void {
|
||||
if (plugins.receive(raw)) return;
|
||||
const msg = parseServerMessage(raw);
|
||||
if (!msg || screenShare.handleMessage(msg) || sessionState.receive(msg)) return;
|
||||
switch (msg.type) {
|
||||
@@ -1069,6 +1067,7 @@ export function useVoiceWebSocket() {
|
||||
state.errorCode = "";
|
||||
state.channelSwitchedChannelId = "";
|
||||
state.tsClientId = Number(msg.tsClientId) || 0;
|
||||
pluginSession.value = msg.pluginSession ?? "";
|
||||
// The mute preference is local to the browser, while TeamSpeak shows
|
||||
// the gateway's own client_input_muted flag to other clients. Send it
|
||||
// as soon as the session is ready so a muted reconnect is visible to
|
||||
@@ -1420,7 +1419,7 @@ export function useVoiceWebSocket() {
|
||||
}
|
||||
|
||||
return {
|
||||
plugins,
|
||||
pluginSession,
|
||||
...screenShare.api,
|
||||
ws,
|
||||
state,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { computed, reactive, ref, type Ref } from "vue";
|
||||
import { DEFAULT_TEAM_SPEAK_PORT, splitTeamSpeakTarget } from "../services/teamspeak-target.js";
|
||||
import { APP_VERSION } from "../services/app-version.js";
|
||||
import type { Language } from "../i18n/web-client.js";
|
||||
|
||||
interface RelayOption {
|
||||
@@ -28,7 +29,7 @@ export function useWebClientPublicConfig({
|
||||
const initialized = ref(false);
|
||||
const mobileMode = ref(false);
|
||||
const siteName = ref("WebSpeak");
|
||||
const appVersion = ref("0.2.7");
|
||||
const appVersion = ref(APP_VERSION);
|
||||
const visitorNumber = ref<number | null>(null);
|
||||
const visitorTotal = ref<number | null>(null);
|
||||
const accelerationRelays = ref<RelayOption[]>([]);
|
||||
|
||||
Reference in new issue
Block a user