2018-03-31 01:12:31 -04:00
|
|
|
import { Component, OnInit } from "@angular/core";
|
|
|
|
import { ToasterService } from "angular2-toaster";
|
|
|
|
import { AdminIrcApiService } from "../../../shared/services/admin/admin-irc-api.service";
|
|
|
|
import { FE_Upstream } from "../../../shared/models/admin-responses";
|
|
|
|
import { AdminUpstreamApiService } from "../../../shared/services/admin/admin-upstream-api.service";
|
|
|
|
import { FE_IrcBridge } from "../../../shared/models/irc";
|
2018-03-31 13:26:00 -04:00
|
|
|
import { AdminIrcBridgeNetworksComponent, IrcNetworksDialogContext } from "./networks/networks.component";
|
2021-11-30 21:27:37 -05:00
|
|
|
import { AdminIrcBridgeAddSelfhostedComponent } from "./add-selfhosted/add-selfhosted.component";
|
2020-10-23 07:30:20 -04:00
|
|
|
import { TranslateService } from "@ngx-translate/core";
|
2021-08-16 18:00:59 -04:00
|
|
|
import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
|
2018-03-30 21:22:15 -04:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
templateUrl: "./irc.component.html",
|
|
|
|
styleUrls: ["./irc.component.scss"],
|
|
|
|
})
|
2018-03-31 01:12:31 -04:00
|
|
|
export class AdminIrcBridgeComponent implements OnInit {
|
2018-03-30 21:22:15 -04:00
|
|
|
|
|
|
|
public isLoading = true;
|
2018-03-31 01:12:31 -04:00
|
|
|
public isUpdating = false;
|
2018-03-30 21:22:15 -04:00
|
|
|
public hasModularBridge = false;
|
2018-03-31 01:12:31 -04:00
|
|
|
public configurations: FE_IrcBridge[] = [];
|
2018-03-30 21:22:15 -04:00
|
|
|
|
2018-03-31 01:12:31 -04:00
|
|
|
private upstreams: FE_Upstream[];
|
|
|
|
|
|
|
|
constructor(private upstreamApi: AdminUpstreamApiService,
|
2021-09-01 19:01:01 -04:00
|
|
|
private ircApi: AdminIrcApiService,
|
|
|
|
private toaster: ToasterService,
|
|
|
|
private modal: NgbModal,
|
|
|
|
public translate: TranslateService) {
|
2020-10-23 07:30:20 -04:00
|
|
|
this.translate = translate;
|
2018-03-31 01:12:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnInit() {
|
|
|
|
this.reload().then(() => this.isLoading = false);
|
2018-03-30 21:22:15 -04:00
|
|
|
}
|
|
|
|
|
2018-03-31 01:12:31 -04:00
|
|
|
private async reload(): Promise<any> {
|
|
|
|
try {
|
|
|
|
this.upstreams = await this.upstreamApi.getUpstreams();
|
|
|
|
this.configurations = await this.ircApi.getBridges();
|
|
|
|
|
|
|
|
this.hasModularBridge = false;
|
|
|
|
for (const bridge of this.configurations) {
|
|
|
|
if (bridge.upstreamId) {
|
|
|
|
this.hasModularBridge = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Error loading bridges').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("error", res);
|
|
|
|
});
|
2018-03-31 01:12:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public getEnabledNetworksString(bridge: FE_IrcBridge): string {
|
|
|
|
const networkIds = Object.keys(bridge.availableNetworks);
|
|
|
|
const result = networkIds.filter(i => bridge.availableNetworks[i].isEnabled)
|
|
|
|
.map(i => bridge.availableNetworks[i].name)
|
|
|
|
.join(", ");
|
|
|
|
if (!result) return "None";
|
|
|
|
return result;
|
2018-03-30 21:22:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public addModularHostedBridge() {
|
2018-03-31 01:12:31 -04:00
|
|
|
this.isUpdating = true;
|
2018-03-30 21:22:15 -04:00
|
|
|
|
2018-03-31 01:12:31 -04:00
|
|
|
const createBridge = (upstream: FE_Upstream) => {
|
|
|
|
return this.ircApi.newFromUpstream(upstream).then(bridge => {
|
|
|
|
this.configurations.push(bridge);
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get(['Click the pencil icon to enable networks.', 'matrix.org\'s IRC bridge added']).subscribe((res: string) => {
|
|
|
|
this.toaster.pop("success", res[0], res[1]);
|
|
|
|
});
|
2018-03-31 01:12:31 -04:00
|
|
|
this.isUpdating = false;
|
|
|
|
this.hasModularBridge = true;
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
this.isUpdating = false;
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Error adding matrix.org\'s IRC Bridge').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("error", res);
|
|
|
|
});
|
2018-03-31 01:12:31 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
const vectorUpstreams = this.upstreams.filter(u => u.type === "vector");
|
|
|
|
if (vectorUpstreams.length === 0) {
|
|
|
|
console.log("Creating default scalar upstream");
|
|
|
|
const scalarUrl = "https://scalar.vector.im/api";
|
|
|
|
this.upstreamApi.newUpstream("modular", "vector", scalarUrl, scalarUrl).then(upstream => {
|
|
|
|
this.upstreams.push(upstream);
|
|
|
|
createBridge(upstream);
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Error creating matrix.org\'s IRC Bridge').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("error", res);
|
|
|
|
});
|
2018-03-31 01:12:31 -04:00
|
|
|
});
|
|
|
|
} else createBridge(vectorUpstreams[0]);
|
2018-03-30 21:22:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public addSelfHostedBridge() {
|
2021-08-16 18:00:59 -04:00
|
|
|
const selfhostedRef = this.modal.open(AdminIrcBridgeAddSelfhostedComponent, {
|
|
|
|
backdrop: 'static',
|
2018-03-31 13:45:19 -04:00
|
|
|
size: 'lg',
|
2021-08-16 18:00:59 -04:00
|
|
|
});
|
|
|
|
selfhostedRef.result.then(() => {
|
|
|
|
try {
|
|
|
|
this.reload()
|
|
|
|
} catch (err) {
|
2018-03-31 13:45:19 -04:00
|
|
|
console.error(err);
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Failed to get an update IRC bridge list').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("error", res);
|
|
|
|
});
|
2021-08-16 18:00:59 -04:00
|
|
|
}
|
|
|
|
})
|
2018-03-30 21:22:15 -04:00
|
|
|
}
|
|
|
|
|
2018-03-31 01:12:31 -04:00
|
|
|
public editNetworks(bridge: FE_IrcBridge) {
|
2021-08-16 18:00:59 -04:00
|
|
|
const selfhostedRef = this.modal.open(AdminIrcBridgeNetworksComponent, {
|
|
|
|
backdrop: 'static',
|
2018-03-31 13:26:00 -04:00
|
|
|
size: 'lg',
|
2021-08-16 18:00:59 -04:00
|
|
|
});
|
|
|
|
selfhostedRef.result.then(() => {
|
|
|
|
try {
|
|
|
|
this.reload()
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
2021-09-01 19:01:01 -04:00
|
|
|
this.translate.get('Failed to get an update IRC bridge list').subscribe((res: string) => {
|
|
|
|
this.toaster.pop("error", res);
|
|
|
|
});
|
2021-08-16 18:00:59 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const selfhostedInstance = selfhostedRef.componentInstance as IrcNetworksDialogContext;
|
|
|
|
selfhostedInstance.bridge = bridge;
|
2018-03-30 21:22:15 -04:00
|
|
|
}
|
|
|
|
}
|