2017-05-27 03:27:36 -04:00
|
|
|
import { Component } from "@angular/core";
|
|
|
|
import { ActivatedRoute } from "@angular/router";
|
|
|
|
import { ApiService } from "../shared/api.service";
|
|
|
|
import { Bot } from "../shared/models/bot";
|
2017-05-27 19:45:07 -04:00
|
|
|
import { ScalarService } from "../shared/scalar.service";
|
|
|
|
import { ToasterService } from "angular2-toaster";
|
2017-05-27 03:27:36 -04:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-riot',
|
|
|
|
templateUrl: './riot.component.html',
|
|
|
|
styleUrls: ['./riot.component.scss'],
|
|
|
|
})
|
|
|
|
export class RiotComponent {
|
|
|
|
|
|
|
|
public error: string;
|
|
|
|
public bots: Bot[] = [];
|
2017-05-27 19:45:07 -04:00
|
|
|
public loading = true;
|
|
|
|
public roomId: string;
|
2017-05-27 03:27:36 -04:00
|
|
|
|
2017-05-27 19:45:07 -04:00
|
|
|
private scalarToken: string;
|
|
|
|
|
2017-05-27 19:46:27 -04:00
|
|
|
constructor(private activatedRoute: ActivatedRoute,
|
|
|
|
private api: ApiService,
|
|
|
|
private scalar: ScalarService,
|
|
|
|
private toaster: ToasterService) {
|
2017-05-27 03:27:36 -04:00
|
|
|
let params: any = this.activatedRoute.snapshot.queryParams;
|
|
|
|
if (!params.scalar_token || !params.room_id) this.error = "Missing scalar token or room ID";
|
2017-05-27 19:45:07 -04:00
|
|
|
else {
|
|
|
|
this.roomId = params.room_id;
|
|
|
|
this.scalarToken = params.scalar_token;
|
|
|
|
|
|
|
|
this.api.checkScalarToken(params.scalar_token).then(isValid => {
|
|
|
|
if (isValid) this.init();
|
|
|
|
else this.error = "Invalid scalar token";
|
|
|
|
}).catch(err => {
|
|
|
|
this.error = "Unable to communicate with Dimension";
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
}
|
2017-05-27 03:27:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private init() {
|
|
|
|
this.api.getBots().then(bots => {
|
|
|
|
this.bots = bots;
|
2017-05-27 19:45:07 -04:00
|
|
|
let promises = bots.map(b => this.updateBotState(b));
|
|
|
|
return Promise.all(promises);
|
|
|
|
}).then(() => this.loading = false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private updateBotState(bot: Bot) {
|
|
|
|
return this.scalar.getMembershipState(this.roomId, bot.mxid).then(payload => {
|
|
|
|
bot.isBroken = false;
|
|
|
|
|
|
|
|
if (!payload.response) {
|
|
|
|
bot.isEnabled = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.isEnabled = (payload.response.membership === 'join' || payload.response.membership === 'invite');
|
|
|
|
}, (error) => {
|
|
|
|
console.error(error);
|
|
|
|
bot.isEnabled = false;
|
|
|
|
bot.isBroken = true;
|
2017-05-27 03:27:36 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-27 19:45:07 -04:00
|
|
|
public updateBot(bot: Bot) {
|
|
|
|
let promise = null;
|
|
|
|
|
|
|
|
if (!bot.isEnabled) {
|
|
|
|
promise = this.api.kickUser(this.roomId, bot.mxid, this.scalarToken);
|
|
|
|
} else promise = this.scalar.inviteUser(this.roomId, bot.mxid);
|
|
|
|
|
|
|
|
promise
|
|
|
|
.then(() => this.toaster.pop("success", bot.name + " invited to the room"))
|
|
|
|
.catch(err => {
|
2017-05-27 19:46:27 -04:00
|
|
|
let errorMessage = "Could not update bot status";
|
2017-05-27 19:45:07 -04:00
|
|
|
|
2017-05-27 19:46:27 -04:00
|
|
|
if (err.json) {
|
|
|
|
errorMessage = err.json().error;
|
|
|
|
} else errorMessage = err.response.error.message;
|
2017-05-27 19:45:07 -04:00
|
|
|
|
2017-05-27 19:46:27 -04:00
|
|
|
bot.isEnabled = !bot.isEnabled;
|
|
|
|
this.toaster.pop("error", errorMessage);
|
|
|
|
});
|
2017-05-27 19:45:07 -04:00
|
|
|
}
|
2017-05-27 03:27:36 -04:00
|
|
|
}
|