2018-05-13 01:51:31 -04:00
|
|
|
import { Component, OnInit } from "@angular/core";
|
|
|
|
import { FE_UserStickerPack } from "../../shared/models/integration";
|
|
|
|
import { StickerApiService } from "../../shared/services/integrations/sticker-api.service";
|
|
|
|
import { ToasterService } from "angular2-toaster";
|
|
|
|
import { MediaService } from "../../shared/services/media.service";
|
2018-05-14 00:32:13 -04:00
|
|
|
import { ScalarClientApiService } from "../../shared/services/scalar/scalar-client-api.service";
|
|
|
|
import { WIDGET_STICKER_PICKER } from "../../shared/models/widget";
|
2018-05-13 01:51:31 -04:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
templateUrl: "stickerpicker.component.html",
|
|
|
|
styleUrls: ["stickerpicker.component.scss"],
|
|
|
|
})
|
|
|
|
export class StickerpickerComponent implements OnInit {
|
|
|
|
|
|
|
|
public isLoading = true;
|
|
|
|
public isUpdating = false;
|
|
|
|
public packs: FE_UserStickerPack[];
|
|
|
|
|
2019-03-21 00:32:29 -04:00
|
|
|
// Import stuff
|
|
|
|
public packUrl = "";
|
|
|
|
public isImporting = false;
|
2019-03-21 01:53:10 -04:00
|
|
|
public customEnabled = false;
|
|
|
|
public managerUrl: string;
|
|
|
|
public stickerBot: string;
|
2019-03-21 00:32:29 -04:00
|
|
|
|
2018-05-13 01:51:31 -04:00
|
|
|
constructor(private stickerApi: StickerApiService,
|
|
|
|
private media: MediaService,
|
2018-05-14 00:32:13 -04:00
|
|
|
private scalarClient: ScalarClientApiService,
|
|
|
|
private toaster: ToasterService,
|
|
|
|
private window: Window) {
|
2018-05-13 01:51:31 -04:00
|
|
|
this.isLoading = true;
|
|
|
|
this.isUpdating = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async ngOnInit() {
|
|
|
|
try {
|
|
|
|
this.packs = await this.stickerApi.getPacks();
|
|
|
|
this.isLoading = false;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
this.toaster.pop("error", "Failed to load sticker packs");
|
|
|
|
}
|
2019-03-21 01:53:10 -04:00
|
|
|
|
|
|
|
this.stickerApi.getConfig().then(config => {
|
|
|
|
this.customEnabled = config.enabled;
|
|
|
|
this.managerUrl = config.managerUrl;
|
|
|
|
this.stickerBot = config.stickerBot;
|
|
|
|
}).catch(err => console.error(err));
|
2018-05-13 01:51:31 -04:00
|
|
|
}
|
|
|
|
|
2019-03-21 00:32:29 -04:00
|
|
|
public importPack() {
|
|
|
|
this.isImporting = true;
|
|
|
|
this.stickerApi.importStickerpack(this.packUrl).then(pack => {
|
|
|
|
// Insert at top for visibility
|
|
|
|
this.packs.splice(0, 0, pack);
|
|
|
|
this.packUrl = "";
|
|
|
|
this.isImporting = false;
|
|
|
|
this.toaster.pop("success", "Stickerpack added");
|
2019-03-23 20:44:06 -04:00
|
|
|
this.addWidget();
|
2019-03-21 00:32:29 -04:00
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
this.isImporting = false;
|
|
|
|
this.toaster.pop("error", "Error adding stickerpack");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-13 01:51:31 -04:00
|
|
|
public getThumbnailUrl(mxc: string, width: number, height: number, method: "crop" | "scale" = "scale"): string {
|
|
|
|
return this.media.getThumbnailUrl(mxc, width, height, method, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public toggleSelected(pack: FE_UserStickerPack) {
|
|
|
|
pack.isSelected = !pack.isSelected;
|
|
|
|
this.isUpdating = true;
|
|
|
|
this.stickerApi.togglePackSelection(pack.id, pack.isSelected).then(() => {
|
|
|
|
this.isUpdating = false;
|
|
|
|
this.toaster.pop("success", "Stickers updated");
|
2018-05-14 00:32:13 -04:00
|
|
|
|
|
|
|
if (this.packs.filter(p => p.isSelected).length > 0) this.addWidget();
|
2018-05-13 01:51:31 -04:00
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
pack.isSelected = !pack.isSelected; // revert change
|
|
|
|
this.isUpdating = false;
|
|
|
|
this.toaster.pop("error", "Error updating stickers");
|
|
|
|
});
|
|
|
|
}
|
2018-05-14 00:32:13 -04:00
|
|
|
|
|
|
|
private async addWidget() {
|
|
|
|
try {
|
|
|
|
const widgets = await this.scalarClient.getWidgets();
|
|
|
|
const stickerPicker = widgets.response.find(w => w.content && w.content.type === "m.stickerpicker");
|
2018-10-24 02:27:57 -04:00
|
|
|
const widgetId = stickerPicker ? ((<any>stickerPicker).id || stickerPicker.state_key) : "dimension-stickerpicker-" + (new Date().getTime());
|
2018-05-14 00:32:13 -04:00
|
|
|
|
2018-10-24 02:27:57 -04:00
|
|
|
console.log("Force-setting new widget of ID " + widgetId);
|
2018-05-14 00:32:13 -04:00
|
|
|
await this.scalarClient.setUserWidget({
|
2018-10-24 02:27:57 -04:00
|
|
|
id: widgetId,
|
2018-05-14 00:32:13 -04:00
|
|
|
type: WIDGET_STICKER_PICKER[0],
|
|
|
|
url: this.window.location.origin + "/widgets/stickerpicker",
|
|
|
|
data: {
|
|
|
|
dimension: {
|
|
|
|
wrapperId: "stickerpicker",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Failed to check for Dimension sticker picker");
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
2018-05-13 01:51:31 -04:00
|
|
|
}
|