2018-05-14 00:32:13 -04:00
|
|
|
import { Component, OnDestroy, OnInit } from "@angular/core";
|
|
|
|
import { ActivatedRoute } from "@angular/router";
|
|
|
|
import { CapableWidget } from "../capable-widget";
|
|
|
|
import { Subscription } from "rxjs/Subscription";
|
|
|
|
import { ScalarWidgetApi } from "../../shared/services/scalar/scalar-widget.api";
|
|
|
|
import { StickerApiService } from "../../shared/services/integrations/sticker-api.service";
|
|
|
|
import { SessionStorage } from "../../shared/SessionStorage";
|
|
|
|
import { ScalarServerApiService } from "../../shared/services/scalar/scalar-server-api.service";
|
|
|
|
import { FE_Sticker, FE_UserStickerPack } from "../../shared/models/integration";
|
|
|
|
import { MediaService } from "../../shared/services/media.service";
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: "my-generic-widget-wrapper",
|
|
|
|
templateUrl: "sticker-picker.component.html",
|
|
|
|
styleUrls: ["sticker-picker.component.scss"],
|
|
|
|
})
|
|
|
|
export class StickerPickerWidgetWrapperComponent extends CapableWidget implements OnInit, OnDestroy {
|
|
|
|
|
|
|
|
public isLoading = true;
|
|
|
|
public authError = false;
|
|
|
|
public packs: FE_UserStickerPack[];
|
|
|
|
|
|
|
|
private stickerWidgetApiSubscription: Subscription;
|
|
|
|
|
2018-05-20 19:41:06 -04:00
|
|
|
constructor(activatedRoute: ActivatedRoute,
|
2018-05-14 00:32:13 -04:00
|
|
|
private media: MediaService,
|
|
|
|
private scalarApi: ScalarServerApiService,
|
|
|
|
private stickerApi: StickerApiService) {
|
|
|
|
super();
|
|
|
|
this.supportsStickers = true;
|
|
|
|
|
|
|
|
let params: any = activatedRoute.snapshot.queryParams;
|
2018-05-20 19:41:06 -04:00
|
|
|
|
|
|
|
let token = params.scalar_token;
|
|
|
|
if (!token) token = localStorage.getItem("dim-scalar-token");
|
|
|
|
else localStorage.setItem("dim-scalar-token", token);
|
|
|
|
|
2018-05-14 00:32:13 -04:00
|
|
|
if (!params.widgetId) {
|
|
|
|
console.error("No widgetId query parameter");
|
|
|
|
this.authError = true;
|
2018-05-20 20:25:37 -04:00
|
|
|
this.isLoading = false;
|
2018-05-14 00:32:13 -04:00
|
|
|
} else {
|
|
|
|
ScalarWidgetApi.widgetId = params.widgetId;
|
|
|
|
}
|
2018-05-20 19:41:06 -04:00
|
|
|
|
2018-05-20 20:25:37 -04:00
|
|
|
if (!this.authError) {
|
|
|
|
SessionStorage.scalarToken = token;
|
|
|
|
this.authError = !token;
|
|
|
|
this.isLoading = !this.authError;
|
|
|
|
}
|
2018-05-14 00:32:13 -04:00
|
|
|
}
|
|
|
|
|
2018-05-20 19:41:06 -04:00
|
|
|
public ngOnInit() {
|
2018-05-14 00:32:13 -04:00
|
|
|
super.ngOnInit();
|
|
|
|
this.stickerWidgetApiSubscription = ScalarWidgetApi.requestReceived.subscribe(request => {
|
|
|
|
if (request.action === "visibility") {
|
|
|
|
if ((<any>request).visible) this.loadStickers();
|
|
|
|
ScalarWidgetApi.replyAcknowledge(request);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.loadStickers();
|
|
|
|
}
|
|
|
|
|
|
|
|
public ngOnDestroy() {
|
|
|
|
super.ngOnDestroy();
|
|
|
|
if (this.stickerWidgetApiSubscription) this.stickerWidgetApiSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
public getThumbnailUrl(mxc: string, width: number, height: number, method: "crop" | "scale" = "scale"): string {
|
|
|
|
return this.media.getThumbnailUrl(mxc, width, height, method, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async loadStickers() {
|
|
|
|
if (this.authError) return; // Don't bother
|
|
|
|
|
2018-05-20 19:41:06 -04:00
|
|
|
if (!SessionStorage.userId) {
|
|
|
|
try {
|
|
|
|
const info = await this.scalarApi.getAccount();
|
|
|
|
SessionStorage.userId = info.user_id;
|
|
|
|
console.log("Dimension scalar_token belongs to " + SessionStorage.userId);
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
this.authError = true;
|
2018-05-20 20:25:37 -04:00
|
|
|
this.isLoading = false;
|
2018-05-20 19:41:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 00:32:13 -04:00
|
|
|
console.log("Attempting to load available stickers...");
|
|
|
|
try {
|
|
|
|
const packs = await this.stickerApi.getPacks();
|
|
|
|
this.packs = packs.filter(p => p.isSelected);
|
|
|
|
console.log("User has " + this.packs.length + "/" + packs.length + " sticker packs selected");
|
|
|
|
this.isLoading = false;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public sendSticker(sticker: FE_Sticker, pack: FE_UserStickerPack) {
|
|
|
|
ScalarWidgetApi.sendSticker(sticker, pack);
|
|
|
|
}
|
|
|
|
}
|