127 lines
3.6 KiB
TypeScript
127 lines
3.6 KiB
TypeScript
import { Injectable, inject } from '@angular/core';
|
|
import { DataService } from './data.service';
|
|
|
|
export type PortalStatus = 'active' | 'inactive' | 'expired' | 'unavailable';
|
|
|
|
interface XtreamPortalStatusResponse {
|
|
payload?: {
|
|
user_info?: {
|
|
status?: string;
|
|
exp_date?: string;
|
|
};
|
|
};
|
|
}
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class PortalStatusService {
|
|
private readonly dataService = inject(DataService);
|
|
|
|
/**
|
|
* Checks the status of an Xtream Code portal
|
|
*
|
|
* @param serverUrl The base URL of the server
|
|
* @param username The username for authentication
|
|
* @param password The password for authentication
|
|
* @returns A promise that resolves to the portal status
|
|
*/
|
|
async checkPortalStatus(
|
|
serverUrl: string,
|
|
username: string,
|
|
password: string
|
|
): Promise<PortalStatus> {
|
|
try {
|
|
let normalizedUrl = serverUrl;
|
|
if (serverUrl && !serverUrl.endsWith('/')) {
|
|
normalizedUrl = serverUrl;
|
|
}
|
|
|
|
const response =
|
|
await this.dataService.sendIpcEvent<XtreamPortalStatusResponse>(
|
|
'XTREAM_REQUEST',
|
|
{
|
|
url: normalizedUrl,
|
|
params: {
|
|
password,
|
|
username,
|
|
action: 'get_account_info',
|
|
},
|
|
suppressErrorLog: true,
|
|
}
|
|
);
|
|
const payload = response?.payload;
|
|
|
|
if (!payload?.user_info?.status) {
|
|
return 'unavailable';
|
|
}
|
|
|
|
if (payload.user_info.status === 'Active') {
|
|
if (!payload.user_info.exp_date) {
|
|
return 'active';
|
|
}
|
|
|
|
const expDate = new Date(
|
|
parseInt(payload.user_info.exp_date, 10) * 1000
|
|
);
|
|
return expDate < new Date() ? 'expired' : 'active';
|
|
} else {
|
|
return 'inactive';
|
|
}
|
|
} catch (error) {
|
|
return 'unavailable';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets a user-friendly message based on the portal status
|
|
*
|
|
* @param status The portal status
|
|
* @returns A message describing the status
|
|
*/
|
|
getStatusMessage(status: PortalStatus | null): string {
|
|
switch (status) {
|
|
case 'active':
|
|
return 'Connection successful! Portal is active.';
|
|
case 'inactive':
|
|
return 'Portal is inactive.';
|
|
case 'expired':
|
|
return 'Portal subscription has expired.';
|
|
case 'unavailable':
|
|
return 'Could not connect to the portal.';
|
|
default:
|
|
return '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets a CSS class name based on the portal status
|
|
*
|
|
* @param status The portal status
|
|
* @returns A CSS class name
|
|
*/
|
|
getStatusClass(status: PortalStatus | null): string {
|
|
return status ? `status-${status}` : '';
|
|
}
|
|
|
|
/**
|
|
* Gets an icon name based on the portal status
|
|
*
|
|
* @param status The portal status
|
|
* @returns A material icon name
|
|
*/
|
|
getStatusIcon(status: PortalStatus | null): string {
|
|
switch (status) {
|
|
case 'active':
|
|
return 'check_circle';
|
|
case 'inactive':
|
|
return 'cancel';
|
|
case 'expired':
|
|
return 'warning';
|
|
case 'unavailable':
|
|
default:
|
|
return 'error';
|
|
}
|
|
}
|
|
}
|