2020-09-12 13:49:20 +00:00
|
|
|
import {ServerCommand, SingleCommandHandler} from "../connection/ConnectionBase";
|
|
|
|
import * as log from "../log";
|
|
|
|
import {LogCategory} from "../log";
|
2020-03-30 11:44:18 +00:00
|
|
|
import {
|
|
|
|
ClientNameInfo,
|
|
|
|
CommandResult,
|
2020-08-22 20:23:19 +00:00
|
|
|
Playlist, PlaylistInfo, PlaylistSong,
|
2020-03-30 11:44:18 +00:00
|
|
|
QueryList,
|
|
|
|
QueryListEntry, ServerGroupClient
|
2020-09-12 13:49:20 +00:00
|
|
|
} from "../connection/ServerConnectionDeclaration";
|
|
|
|
import {AbstractCommandHandler} from "../connection/AbstractCommandHandler";
|
|
|
|
import {tr} from "../i18n/localize";
|
|
|
|
import {ErrorCode} from "../connection/ErrorCode";
|
2020-03-30 11:44:18 +00:00
|
|
|
|
|
|
|
export class CommandHelper extends AbstractCommandHandler {
|
2020-09-07 10:42:00 +00:00
|
|
|
private whoAmIResponse: any;
|
2020-08-10 17:39:28 +00:00
|
|
|
private infoByUniqueIdRequest: {[unique_id: string]:((resolved: ClientNameInfo) => any)[]} = {};
|
|
|
|
private infoByDatabaseIdRequest: {[database_id: number]:((resolved: ClientNameInfo) => any)[]} = {};
|
2020-03-30 11:44:18 +00:00
|
|
|
|
|
|
|
constructor(connection) {
|
|
|
|
super(connection);
|
|
|
|
|
|
|
|
this.volatile_handler_boss = false;
|
|
|
|
this.ignore_consumed = true;
|
|
|
|
}
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
initialize() {
|
|
|
|
this.connection.command_handler_boss().register_handler(this);
|
|
|
|
}
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
destroy() {
|
|
|
|
if(this.connection) {
|
|
|
|
const hboss = this.connection.command_handler_boss();
|
2020-09-07 10:42:00 +00:00
|
|
|
hboss?.unregister_handler(this);
|
2020-03-29 10:54:15 +00:00
|
|
|
}
|
2020-09-07 10:42:00 +00:00
|
|
|
|
2020-08-10 17:39:28 +00:00
|
|
|
this.infoByUniqueIdRequest = undefined;
|
2020-09-07 10:42:00 +00:00
|
|
|
this.infoByDatabaseIdRequest = undefined;
|
2020-03-30 11:44:18 +00:00
|
|
|
}
|
2020-02-22 13:30:17 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
handle_command(command: ServerCommand): boolean {
|
2020-09-07 10:42:00 +00:00
|
|
|
if(command.command == "notifyclientnamefromuid") {
|
|
|
|
this.handleNotifyClientNameFromUniqueId(command.arguments);
|
|
|
|
} else if(command.command == "notifyclientgetnamefromdbid") {
|
|
|
|
this.handleNotifyClientGetNameFromDatabaseId(command.arguments);
|
|
|
|
} else {
|
2020-03-30 11:44:18 +00:00
|
|
|
return false;
|
2020-09-07 10:42:00 +00:00
|
|
|
}
|
2020-03-30 11:44:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
async getInfoFromUniqueId(...uniqueIds: string[]) : Promise<ClientNameInfo[]> {
|
2020-03-30 11:44:18 +00:00
|
|
|
const response: ClientNameInfo[] = [];
|
|
|
|
const request = [];
|
2020-09-07 10:42:00 +00:00
|
|
|
const uniqueUniqueIds = new Set(uniqueIds);
|
|
|
|
if(uniqueUniqueIds.size === 0) return [];
|
2020-02-22 13:30:17 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
const resolvers: {[uniqueId: string]: (resolved: ClientNameInfo) => any} = {};
|
2020-02-22 13:30:17 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
for(const uniqueId of uniqueUniqueIds) {
|
|
|
|
request.push({ cluid: uniqueId });
|
2020-03-27 22:36:57 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
const requestCallbacks = this.infoByUniqueIdRequest[uniqueId] || (this.infoByUniqueIdRequest[uniqueId] = []);
|
|
|
|
requestCallbacks.push(resolvers[uniqueId] = info => response.push(info));
|
2020-03-30 11:44:18 +00:00
|
|
|
}
|
2020-02-22 13:30:17 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
try {
|
|
|
|
await this.connection.send_command("clientgetnamefromuid", request);
|
|
|
|
} catch(error) {
|
2020-08-22 20:23:19 +00:00
|
|
|
if(error instanceof CommandResult && error.id == ErrorCode.DATABASE_EMPTY_RESULT) {
|
2020-03-30 11:44:18 +00:00
|
|
|
/* nothing */
|
|
|
|
} else {
|
|
|
|
throw error;
|
2020-03-29 10:54:15 +00:00
|
|
|
}
|
2020-03-30 11:44:18 +00:00
|
|
|
} finally {
|
|
|
|
/* cleanup */
|
2020-09-07 10:42:00 +00:00
|
|
|
for(const uniqueId of Object.keys(resolvers)) {
|
|
|
|
this.infoByUniqueIdRequest[uniqueId]?.remove(resolvers[uniqueId]);
|
|
|
|
}
|
2020-03-29 10:54:15 +00:00
|
|
|
}
|
2020-02-22 13:30:17 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
return response;
|
|
|
|
}
|
2020-02-22 13:30:17 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
private handleNotifyClientGetNameFromDatabaseId(json: any[]) {
|
2020-03-30 11:44:18 +00:00
|
|
|
for(const entry of json) {
|
|
|
|
const info: ClientNameInfo = {
|
2020-09-07 10:42:00 +00:00
|
|
|
clientUniqueId: entry["cluid"],
|
|
|
|
clientNickname: entry["clname"],
|
|
|
|
clientDatabaseId: parseInt(entry["cldbid"])
|
2020-03-30 11:44:18 +00:00
|
|
|
};
|
2020-02-22 13:30:17 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
const callbacks = this.infoByDatabaseIdRequest[info.clientDatabaseId] || [];
|
|
|
|
delete this.infoByDatabaseIdRequest[info.clientDatabaseId];
|
2020-03-30 11:44:18 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
callbacks.forEach(callback => callback(info));
|
2020-02-22 13:30:17 +00:00
|
|
|
}
|
2020-03-30 11:44:18 +00:00
|
|
|
}
|
2020-02-22 13:30:17 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
async getInfoFromClientDatabaseId(...clientDatabaseIds: number[]) : Promise<ClientNameInfo[]> {
|
2020-03-30 11:44:18 +00:00
|
|
|
const response: ClientNameInfo[] = [];
|
|
|
|
const request = [];
|
2020-09-07 10:42:00 +00:00
|
|
|
const uniqueClientDatabaseIds = new Set(clientDatabaseIds);
|
|
|
|
if(!uniqueClientDatabaseIds.size) return [];
|
|
|
|
|
|
|
|
const resolvers: {[dbid: number]: (resolved: ClientNameInfo) => any} = {};
|
2019-03-25 19:04:04 +00:00
|
|
|
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
for(const clientDatabaseId of uniqueClientDatabaseIds) {
|
|
|
|
request.push({ cldbid: clientDatabaseId });
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-12-08 11:14:53 +00:00
|
|
|
const requestCallbacks = this.infoByDatabaseIdRequest[clientDatabaseId] || (this.infoByDatabaseIdRequest[clientDatabaseId] = []);
|
2020-09-07 10:42:00 +00:00
|
|
|
requestCallbacks.push(resolvers[clientDatabaseId] = info => response.push(info));
|
2020-03-30 11:44:18 +00:00
|
|
|
}
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
try {
|
|
|
|
await this.connection.send_command("clientgetnamefromdbid", request);
|
|
|
|
} catch(error) {
|
2020-08-22 20:23:19 +00:00
|
|
|
if(error instanceof CommandResult && error.id == ErrorCode.DATABASE_EMPTY_RESULT) {
|
2020-03-30 11:44:18 +00:00
|
|
|
/* nothing */
|
|
|
|
} else {
|
|
|
|
throw error;
|
2020-03-29 10:54:15 +00:00
|
|
|
}
|
2020-03-30 11:44:18 +00:00
|
|
|
} finally {
|
|
|
|
/* cleanup */
|
2020-09-07 10:42:00 +00:00
|
|
|
for(const cldbid of Object.keys(resolvers)) {
|
|
|
|
this.infoByDatabaseIdRequest[cldbid]?.remove(resolvers[cldbid]);
|
|
|
|
}
|
2020-03-29 10:54:15 +00:00
|
|
|
}
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
return response;
|
|
|
|
}
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
private handleNotifyClientNameFromUniqueId(json: any[]) {
|
2020-03-30 11:44:18 +00:00
|
|
|
for(const entry of json) {
|
|
|
|
const info: ClientNameInfo = {
|
2020-09-07 10:42:00 +00:00
|
|
|
clientUniqueId: entry["cluid"],
|
|
|
|
clientNickname: entry["clname"],
|
|
|
|
clientDatabaseId: parseInt(entry["cldbid"])
|
2020-03-30 11:44:18 +00:00
|
|
|
};
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-08-10 17:39:28 +00:00
|
|
|
const functions = this.infoByUniqueIdRequest[entry["cluid"]] || [];
|
|
|
|
delete this.infoByUniqueIdRequest[entry["cluid"]];
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
for(const fn of functions) {
|
2020-03-30 11:44:18 +00:00
|
|
|
fn(info);
|
2020-09-07 10:42:00 +00:00
|
|
|
}
|
2020-03-30 11:44:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
requestQueryList(server_id: number = undefined) : Promise<QueryList> {
|
2020-03-30 11:44:18 +00:00
|
|
|
return new Promise<QueryList>((resolve, reject) => {
|
|
|
|
const single_handler = {
|
|
|
|
command: "notifyquerylist",
|
|
|
|
function: command => {
|
|
|
|
const json = command.arguments;
|
2020-03-27 22:36:57 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
const result = {} as QueryList;
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
result.flag_all = json[0]["flag_all"];
|
|
|
|
result.flag_own = json[0]["flag_own"];
|
|
|
|
result.queries = [];
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
for(const entry of json) {
|
|
|
|
const rentry = {} as QueryListEntry;
|
|
|
|
rentry.bounded_server = parseInt(entry["client_bound_server"]);
|
|
|
|
rentry.username = entry["client_login_name"];
|
|
|
|
rentry.unique_id = entry["client_unique_identifier"];
|
2020-03-29 10:54:15 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
result.queries.push(rentry);
|
2019-02-23 13:15:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
resolve(result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.handler_boss.register_single_handler(single_handler);
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
let data = {};
|
2020-09-07 10:42:00 +00:00
|
|
|
if(server_id !== undefined) {
|
2020-03-30 11:44:18 +00:00
|
|
|
data["server_id"] = server_id;
|
2020-09-07 10:42:00 +00:00
|
|
|
}
|
2020-03-27 22:36:57 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
this.connection.send_command("querylist", data).catch(error => {
|
|
|
|
if(error instanceof CommandResult) {
|
2020-08-22 20:23:19 +00:00
|
|
|
if(error.id == ErrorCode.DATABASE_EMPTY_RESULT) {
|
2020-03-30 11:44:18 +00:00
|
|
|
resolve(undefined);
|
|
|
|
return;
|
2019-02-23 13:15:22 +00:00
|
|
|
}
|
2020-03-30 11:44:18 +00:00
|
|
|
}
|
|
|
|
reject(error);
|
2020-09-07 10:42:00 +00:00
|
|
|
}).then(() => {
|
|
|
|
this.handler_boss.remove_single_handler(single_handler);
|
2020-03-29 10:54:15 +00:00
|
|
|
});
|
2020-03-30 11:44:18 +00:00
|
|
|
});
|
|
|
|
}
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
requestPlaylistList() : Promise<Playlist[]> {
|
2020-03-30 11:44:18 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const single_handler: SingleCommandHandler = {
|
|
|
|
command: "notifyplaylistlist",
|
|
|
|
function: command => {
|
|
|
|
const json = command.arguments;
|
|
|
|
const result: Playlist[] = [];
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
for(const entry of json) {
|
|
|
|
try {
|
|
|
|
result.push({
|
|
|
|
playlist_id: parseInt(entry["playlist_id"]),
|
|
|
|
playlist_bot_id: parseInt(entry["playlist_bot_id"]),
|
|
|
|
playlist_title: entry["playlist_title"],
|
|
|
|
playlist_type: parseInt(entry["playlist_type"]),
|
|
|
|
playlist_owner_dbid: parseInt(entry["playlist_owner_dbid"]),
|
|
|
|
playlist_owner_name: entry["playlist_owner_name"],
|
|
|
|
|
|
|
|
needed_power_modify: parseInt(entry["needed_power_modify"]),
|
|
|
|
needed_power_permission_modify: parseInt(entry["needed_power_permission_modify"]),
|
|
|
|
needed_power_delete: parseInt(entry["needed_power_delete"]),
|
|
|
|
needed_power_song_add: parseInt(entry["needed_power_song_add"]),
|
|
|
|
needed_power_song_move: parseInt(entry["needed_power_song_move"]),
|
|
|
|
needed_power_song_remove: parseInt(entry["needed_power_song_remove"])
|
|
|
|
});
|
|
|
|
} catch(error) {
|
|
|
|
log.error(LogCategory.NETWORKING, tr("Failed to parse playlist entry: %o"), error);
|
|
|
|
}
|
2020-03-27 22:36:57 +00:00
|
|
|
}
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
resolve(result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.handler_boss.register_single_handler(single_handler);
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
this.connection.send_command("playlistlist").catch(error => {
|
|
|
|
if(error instanceof CommandResult) {
|
2020-08-22 20:23:19 +00:00
|
|
|
if(error.id == ErrorCode.DATABASE_EMPTY_RESULT) {
|
2020-03-30 11:44:18 +00:00
|
|
|
resolve([]);
|
|
|
|
return;
|
2019-02-23 13:15:22 +00:00
|
|
|
}
|
2020-03-30 11:44:18 +00:00
|
|
|
}
|
|
|
|
reject(error);
|
2020-09-07 10:42:00 +00:00
|
|
|
}).then(() => {
|
|
|
|
this.handler_boss.remove_single_handler(single_handler);
|
|
|
|
});
|
2020-03-30 11:44:18 +00:00
|
|
|
});
|
|
|
|
}
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
requestPlaylistSongs(playlist_id: number, process_result?: boolean) : Promise<PlaylistSong[]> {
|
2020-04-04 10:03:48 +00:00
|
|
|
let bulked_response = false;
|
|
|
|
let bulk_index = 0;
|
|
|
|
|
|
|
|
const result: PlaylistSong[] = [];
|
2020-03-30 11:44:18 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const single_handler: SingleCommandHandler = {
|
2020-04-04 10:03:48 +00:00
|
|
|
command: ["notifyplaylistsonglist", "notifyplaylistsonglistfinished"],
|
2020-03-30 11:44:18 +00:00
|
|
|
function: command => {
|
|
|
|
const json = command.arguments;
|
2020-02-22 13:30:17 +00:00
|
|
|
|
2020-04-04 10:03:48 +00:00
|
|
|
if(bulk_index === 0) {
|
|
|
|
/* we're sending the response as bulk */
|
|
|
|
bulked_response = parseInt(json[0]["version"]) >= 2;
|
2020-03-30 11:44:18 +00:00
|
|
|
}
|
2020-02-22 13:30:17 +00:00
|
|
|
|
2020-04-04 10:03:48 +00:00
|
|
|
if(parseInt(json[0]["playlist_id"]) !== playlist_id)
|
|
|
|
return false; /* not our request */
|
|
|
|
|
|
|
|
if(command.command === "notifyplaylistsonglistfinished") {
|
|
|
|
resolve(result);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
for(const entry of json) {
|
|
|
|
try {
|
|
|
|
result.push({
|
|
|
|
song_id: parseInt(entry["song_id"]),
|
|
|
|
song_invoker: entry["song_invoker"],
|
|
|
|
song_previous_song_id: parseInt(entry["song_previous_song_id"]),
|
|
|
|
song_url: entry["song_url"],
|
|
|
|
song_url_loader: entry["song_url_loader"],
|
|
|
|
|
|
|
|
song_loaded: entry["song_loaded"] == true || entry["song_loaded"] == "1",
|
|
|
|
song_metadata: entry["song_metadata"]
|
|
|
|
});
|
|
|
|
} catch(error) {
|
|
|
|
log.error(LogCategory.NETWORKING, tr("Failed to parse playlist song entry: %o"), error);
|
|
|
|
}
|
|
|
|
}
|
2020-02-22 13:30:17 +00:00
|
|
|
|
2020-04-04 10:03:48 +00:00
|
|
|
if(bulked_response) {
|
|
|
|
bulk_index++;
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
return true;
|
2020-03-29 10:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-30 11:44:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
this.handler_boss.register_single_handler(single_handler);
|
2019-05-25 10:39:09 +00:00
|
|
|
|
2020-04-21 13:18:16 +00:00
|
|
|
this.connection.send_command("playlistsonglist", {playlist_id: playlist_id}, { process_result: process_result }).catch(error => {
|
2020-03-30 11:44:18 +00:00
|
|
|
if(error instanceof CommandResult) {
|
2020-08-22 20:23:19 +00:00
|
|
|
if(error.id == ErrorCode.DATABASE_EMPTY_RESULT) {
|
2020-03-30 11:44:18 +00:00
|
|
|
resolve([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reject(error);
|
2020-09-07 10:42:00 +00:00
|
|
|
}).catch(() => {
|
|
|
|
this.handler_boss.remove_single_handler(single_handler);
|
|
|
|
});
|
2020-03-30 11:44:18 +00:00
|
|
|
});
|
|
|
|
}
|
2020-03-27 22:36:57 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
request_playlist_client_list(playlist_id: number) : Promise<number[]> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const single_handler: SingleCommandHandler = {
|
|
|
|
command: "notifyplaylistclientlist",
|
|
|
|
function: command => {
|
|
|
|
const json = command.arguments;
|
2019-05-25 10:39:09 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
if(json[0]["playlist_id"] != playlist_id) {
|
|
|
|
log.error(LogCategory.NETWORKING, tr("Received invalid notification for playlist clients"));
|
|
|
|
return false;
|
2019-05-25 10:39:09 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
const result: number[] = [];
|
2019-05-25 10:39:09 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
for(const entry of json) {
|
2020-03-30 11:44:18 +00:00
|
|
|
result.push(parseInt(entry["cldbid"]));
|
2020-09-07 10:42:00 +00:00
|
|
|
}
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
resolve(result.filter(e => !isNaN(e)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.handler_boss.register_single_handler(single_handler);
|
|
|
|
|
|
|
|
this.connection.send_command("playlistclientlist", {playlist_id: playlist_id}).catch(error => {
|
2020-08-22 20:23:19 +00:00
|
|
|
if(error instanceof CommandResult && error.id == ErrorCode.DATABASE_EMPTY_RESULT) {
|
2020-03-30 11:44:18 +00:00
|
|
|
resolve([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
reject(error);
|
2020-09-07 10:42:00 +00:00
|
|
|
}).then(() => {
|
|
|
|
this.handler_boss.remove_single_handler(single_handler);
|
|
|
|
});
|
2020-03-30 11:44:18 +00:00
|
|
|
});
|
|
|
|
}
|
2020-03-29 10:54:15 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
requestClientsByServerGroup(group_id: number) : Promise<ServerGroupClient[]> {
|
2020-03-30 11:44:18 +00:00
|
|
|
//servergroupclientlist sgid=2
|
|
|
|
//notifyservergroupclientlist sgid=6 cldbid=2 client_nickname=WolverinDEV client_unique_identifier=xxjnc14LmvTk+Lyrm8OOeo4tOqw=
|
|
|
|
return new Promise<ServerGroupClient[]>((resolve, reject) => {
|
|
|
|
const single_handler: SingleCommandHandler = {
|
|
|
|
command: "notifyservergroupclientlist",
|
|
|
|
function: command => {
|
|
|
|
if (command.arguments[0]["sgid"] != group_id) {
|
|
|
|
log.error(LogCategory.NETWORKING, tr("Received invalid notification for server group client list"));
|
|
|
|
return false;
|
2020-03-27 22:36:57 +00:00
|
|
|
}
|
2020-03-29 10:54:15 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
try {
|
|
|
|
const result: ServerGroupClient[] = [];
|
2020-06-15 14:56:05 +00:00
|
|
|
for(const entry of command.arguments) {
|
|
|
|
if(!('cldbid' in entry))
|
|
|
|
continue;
|
2020-03-30 11:44:18 +00:00
|
|
|
result.push({
|
|
|
|
client_database_id: parseInt(entry["cldbid"]),
|
|
|
|
client_nickname: entry["client_nickname"],
|
|
|
|
client_unique_identifier: entry["client_unique_identifier"]
|
|
|
|
});
|
2020-06-15 14:56:05 +00:00
|
|
|
}
|
2020-03-30 11:44:18 +00:00
|
|
|
resolve(result);
|
|
|
|
} catch (error) {
|
|
|
|
log.error(LogCategory.NETWORKING, tr("Failed to parse server group client list: %o"), error);
|
|
|
|
reject("failed to parse info");
|
|
|
|
}
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.handler_boss.register_single_handler(single_handler);
|
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
this.connection.send_command("servergroupclientlist", {sgid: group_id}).catch(reject).then(() => {
|
2020-03-30 11:44:18 +00:00
|
|
|
this.handler_boss.remove_single_handler(single_handler);
|
2020-09-07 10:42:00 +00:00
|
|
|
});
|
2020-03-30 11:44:18 +00:00
|
|
|
});
|
|
|
|
}
|
2020-03-29 10:54:15 +00:00
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
requestPlaylistInfo(playlist_id: number) : Promise<PlaylistInfo> {
|
2020-03-30 11:44:18 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const single_handler: SingleCommandHandler = {
|
|
|
|
command: "notifyplaylistinfo",
|
|
|
|
function: command => {
|
|
|
|
const json = command.arguments[0];
|
|
|
|
if (json["playlist_id"] != playlist_id) {
|
|
|
|
log.error(LogCategory.NETWORKING, tr("Received invalid notification for playlist info"));
|
|
|
|
return;
|
|
|
|
}
|
2020-03-29 10:54:15 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
try {
|
|
|
|
resolve({
|
|
|
|
playlist_id: parseInt(json["playlist_id"]),
|
|
|
|
playlist_title: json["playlist_title"],
|
|
|
|
playlist_description: json["playlist_description"],
|
|
|
|
playlist_type: parseInt(json["playlist_type"]),
|
|
|
|
|
|
|
|
playlist_owner_dbid: parseInt(json["playlist_owner_dbid"]),
|
|
|
|
playlist_owner_name: json["playlist_owner_name"],
|
|
|
|
|
|
|
|
playlist_flag_delete_played: json["playlist_flag_delete_played"] == true || json["playlist_flag_delete_played"] == "1",
|
|
|
|
playlist_flag_finished: json["playlist_flag_finished"] == true || json["playlist_flag_finished"] == "1",
|
|
|
|
playlist_replay_mode: parseInt(json["playlist_replay_mode"]),
|
|
|
|
playlist_current_song_id: parseInt(json["playlist_current_song_id"]),
|
|
|
|
|
|
|
|
playlist_max_songs: parseInt(json["playlist_max_songs"])
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
log.error(LogCategory.NETWORKING, tr("Failed to parse playlist info: %o"), error);
|
|
|
|
reject("failed to parse info");
|
2019-02-23 13:15:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.handler_boss.register_single_handler(single_handler);
|
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
this.connection.send_command("playlistinfo", { playlist_id: playlist_id }).catch(reject).then(() => {
|
2020-03-30 11:44:18 +00:00
|
|
|
this.handler_boss.remove_single_handler(single_handler);
|
2020-09-07 10:42:00 +00:00
|
|
|
});
|
2020-03-30 11:44:18 +00:00
|
|
|
});
|
|
|
|
}
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* Its just a workaround for the query management.
|
2020-08-10 17:39:28 +00:00
|
|
|
* There is no garantee that the whoami trick will work forever
|
2020-03-30 11:44:18 +00:00
|
|
|
*/
|
2020-09-07 10:42:00 +00:00
|
|
|
getCurrentVirtualServerId() : Promise<number> {
|
|
|
|
if(this.whoAmIResponse) {
|
|
|
|
return Promise.resolve(parseInt(this.whoAmIResponse["virtualserver_id"]));
|
|
|
|
}
|
2020-03-30 11:44:18 +00:00
|
|
|
|
|
|
|
return new Promise<number>((resolve, reject) => {
|
|
|
|
const single_handler: SingleCommandHandler = {
|
|
|
|
function: command => {
|
|
|
|
if(command.command != "" && command.command.indexOf("=") == -1)
|
|
|
|
return false;
|
|
|
|
|
2020-09-07 10:42:00 +00:00
|
|
|
this.whoAmIResponse = command.arguments[0];
|
|
|
|
resolve(parseInt(this.whoAmIResponse["virtualserver_id"]));
|
2020-03-30 11:44:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.handler_boss.register_single_handler(single_handler);
|
2019-02-23 13:15:22 +00:00
|
|
|
|
2020-03-30 11:44:18 +00:00
|
|
|
this.connection.send_command("whoami").catch(error => {
|
|
|
|
this.handler_boss.remove_single_handler(single_handler);
|
|
|
|
reject(error);
|
2019-02-23 13:15:22 +00:00
|
|
|
});
|
2020-03-30 11:44:18 +00:00
|
|
|
});
|
2019-02-23 13:15:22 +00:00
|
|
|
}
|
|
|
|
}
|