|
|
@ -1,8 +1,8 @@ |
|
|
|
import { SOCKET_PATH } from "@autoplex-api/manager"; |
|
|
|
import { SOCKET_PATH, IpcMethod } from "@autoplex-api/manager"; |
|
|
|
import { IMovie } from "@autoplex-api/search"; |
|
|
|
import { IpcServerService } from "@autoplex/ipc"; |
|
|
|
import { MovieTicket } from "@autoplex/database"; |
|
|
|
import Supervisor from "./Supervisor"; |
|
|
|
import TorrentManager from "./TorrentManager"; |
|
|
|
import TicketManager from "./TicketManager/TicketManager"; |
|
|
|
|
|
|
|
export default class IpcInterface extends IpcServerService |
|
|
|
{ |
|
|
@ -19,54 +19,88 @@ export default class IpcInterface extends IpcServerService |
|
|
|
/** |
|
|
|
* Store a reference to the torrent client IPC |
|
|
|
*/ |
|
|
|
protected torrentManager!: TorrentManager; |
|
|
|
protected supervisor!: Supervisor; |
|
|
|
|
|
|
|
/** |
|
|
|
* Install the the event handlers |
|
|
|
* Store a reference to the ticket manager |
|
|
|
*/ |
|
|
|
protected override installMessageHandlers() { |
|
|
|
this.addMessageHandler("movie_ticket_added", this.onMovieTicketAdded); |
|
|
|
this.addMessageHandler("movie_ticket_states", this.getMovieTicketStates); |
|
|
|
} |
|
|
|
protected ticketManager!: TicketManager; |
|
|
|
|
|
|
|
/** |
|
|
|
* Link the required services |
|
|
|
*/ |
|
|
|
public override link() { |
|
|
|
this.torrentManager = this.app.service<TorrentManager>("Torrent Manager"); |
|
|
|
this.supervisor = this.app.service<Supervisor>("Supervisor"); |
|
|
|
this.ticketManager = this.app.service<TicketManager>("Ticket Manager"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Install the the event handlers |
|
|
|
*/ |
|
|
|
protected override installMessageHandlers() { |
|
|
|
this.addMessageHandler(IpcMethod.ActiveMovieRequests, this.activeMovieRequests); |
|
|
|
this.addMessageHandler(IpcMethod.ActiveMovieRequestsForUser, this.activeMovieRequestsForUser); |
|
|
|
this.addMessageHandler(IpcMethod.MapMovieRequests, this.mapMovieRequests); |
|
|
|
this.addMessageHandler(IpcMethod.MovieRequestStatus, this.movieRequestStatus); |
|
|
|
this.addMessageHandler(IpcMethod.CreateMovieRequest, this.createMovieRequest); |
|
|
|
this.addMessageHandler(IpcMethod.CancelMovieRequest, this.cancelMovieRequest); |
|
|
|
} |
|
|
|
|
|
|
|
// Interface Methods ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public async activeMovieRequests() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public async activeMovieRequestsForUser() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public async mapMovieRequests() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public async movieRequestStatus() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public async createMovieRequest(userId: number, tmdbId: number) { |
|
|
|
return await this.ticketManager.createMovieTicket(userId, tmdbId); |
|
|
|
} |
|
|
|
|
|
|
|
public async cancelMovieRequest() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Invoked when a new Movie ticket has been created |
|
|
|
*/ |
|
|
|
protected async onMovieTicketAdded(ticketId: number) { |
|
|
|
let movie = await MovieTicket.findOne(ticketId); |
|
|
|
if (movie === undefined) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
this.app.service<Supervisor>("Supervisor").onMovieTicketNeedsTorrent(movie); |
|
|
|
} |
|
|
|
// protected async onMovieTicketAdded(ticketId: number) {
|
|
|
|
// let movie = await MovieTicket.findOne(ticketId);
|
|
|
|
// if (movie === undefined) {
|
|
|
|
// return null;
|
|
|
|
// }
|
|
|
|
// this.app.service<Supervisor>("Supervisor").onMovieTicketNeedsTorrent(movie);
|
|
|
|
// }
|
|
|
|
|
|
|
|
/** |
|
|
|
* Get the states of the provided movie tickets |
|
|
|
*/ |
|
|
|
protected async getMovieTicketStates(ticketIds: number[]) { |
|
|
|
let tickets = await MovieTicket.findByIds(ticketIds, { relations: ["torrents"] }); |
|
|
|
let torrents = this.torrentManager.cachedTorrents; |
|
|
|
let results: any = {}; |
|
|
|
for (let ticket of tickets) { |
|
|
|
let result: any = {}; |
|
|
|
if (ticket.isCanceled) { |
|
|
|
continue; |
|
|
|
} |
|
|
|
if (ticket.torrents.length > 0) { |
|
|
|
let progressList = ticket.torrents.map(torrent => torrents[torrent.infoHash].progress); |
|
|
|
result["progress"] = Math.max(...progressList); |
|
|
|
} |
|
|
|
results[ticket.id] = result; |
|
|
|
} |
|
|
|
return results; |
|
|
|
} |
|
|
|
// protected async getMovieTicketStates(ticketIds: number[]) {
|
|
|
|
// let tickets = await MovieTicket.findByIds(ticketIds, { relations: ["torrents"] });
|
|
|
|
// let torrents = this.torrentManager.cachedTorrents;
|
|
|
|
// let results: any = {};
|
|
|
|
// for (let ticket of tickets) {
|
|
|
|
// let result: any = {};
|
|
|
|
// if (ticket.isCanceled) {
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
// if (ticket.torrents.length > 0) {
|
|
|
|
// let progressList = ticket.torrents.map(torrent => torrents[torrent.infoHash].progress);
|
|
|
|
// result["progress"] = Math.max(...progressList);
|
|
|
|
// }
|
|
|
|
// results[ticket.id] = result;
|
|
|
|
// }
|
|
|
|
// return results;
|
|
|
|
// }
|
|
|
|
} |