richpartners-telegram-sdk
Version:
richPartners ads telegram
169 lines • 8.63 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WidgetManager = void 0;
const tslib_1 = require("tslib");
const index_js_1 = require("../types/index.js");
const index_js_2 = require("../services/index.js");
const index_js_3 = require("../config/index.js");
const index_js_4 = require("../factory/index.js");
const crypto_js_1 = tslib_1.__importDefault(require("crypto-js"));
class WidgetManager {
constructor() {
this.publisherId = '';
this.appId = '';
this.debug = false;
this.shouldUpdateConfig = false;
this.activeWidgetTypes = [];
this.pushStyleAutoMode = false;
this.embeddedBannerAutoMode = false;
this.interstitialBannerAutoMode = false;
this.interstitialVideoAutoMode = false;
this.isPremium = false;
this.initAppSettings = () => {
if (this.isUploadedOldVersion()) {
this.shouldUpdateConfig = true;
return;
}
if (index_js_2.LS.getWithPrefix(index_js_3.LOCAL_STORAGE_KEYS.WIDGET_TYPE)
&& index_js_2.LS.getWithPrefix(index_js_3.LOCAL_STORAGE_KEYS.ACTIVE_WIDGET_TYPE)
&& index_js_2.LS.getWithPrefix(index_js_3.LOCAL_STORAGE_KEYS.CONFIG_UPDATE_TIME)
&& index_js_2.LS.getWithPrefix(index_js_3.LOCAL_STORAGE_KEYS.PUSH_STYLE_AUTO_MODE) !== undefined
&& index_js_2.LS.getWithPrefix(index_js_3.LOCAL_STORAGE_KEYS.EMBEDDED_BANNER_AUTO_MODE) !== undefined
&& index_js_2.LS.getWithPrefix(index_js_3.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_AUTO_MODE) !== undefined
&& index_js_2.LS.getWithPrefix(index_js_3.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_AUTO_MODE) !== undefined) {
this.widgetTypes = index_js_2.LS.getWithPrefix(index_js_3.LOCAL_STORAGE_KEYS.WIDGET_TYPE);
this.activeWidgetTypes = index_js_2.LS.getWithPrefix(index_js_3.LOCAL_STORAGE_KEYS.ACTIVE_WIDGET_TYPE);
this.pushStyleAutoMode = index_js_2.LS.getWithPrefix(index_js_3.LOCAL_STORAGE_KEYS.PUSH_STYLE_AUTO_MODE);
this.embeddedBannerAutoMode = index_js_2.LS.getWithPrefix(index_js_3.LOCAL_STORAGE_KEYS.EMBEDDED_BANNER_AUTO_MODE);
this.interstitialBannerAutoMode = index_js_2.LS.getWithPrefix(index_js_3.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_AUTO_MODE);
this.interstitialVideoAutoMode = index_js_2.LS.getWithPrefix(index_js_3.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_AUTO_MODE);
this.isPremium = index_js_2.LS.getWithPrefix(index_js_3.LOCAL_STORAGE_KEYS.IS_PREMIUM);
return;
}
this.shouldUpdateConfig = true;
};
this.initPersonalSettings = () => {
if (!this.shouldUpdateConfig) {
let richPartnersAds = null;
this.activeWidgetTypes.forEach(type => {
richPartnersAds = index_js_4.AdsFactory.createRichPartnersAdsByType(String(type));
let configInstalledInLocalStorage = richPartnersAds.isConfigInstalledInLocalStorage();
if (configInstalledInLocalStorage) {
richPartnersAds.loadConfigByLocalStorage();
}
else {
this.shouldUpdateConfig = true;
}
});
}
};
this.transformWidgetTypes = (widgetTypes) => {
const transformed = {
PUSH_STYLE: '',
EMBEDDED_BANNER: '',
};
if (widgetTypes.native || widgetTypes.banner) {
if (widgetTypes.native) {
transformed.PUSH_STYLE = widgetTypes.native;
}
if (widgetTypes.banner) {
transformed.EMBEDDED_BANNER = widgetTypes.banner;
}
}
else {
Object.assign(transformed, widgetTypes);
}
return transformed;
};
}
/**
* @param config
*/
initialize(config) {
this.publisherId = config.pubId;
this.appId = config.appId;
this.debug = config.debug ?? false;
this.initAppSettings();
this.initPersonalSettings();
if (this.shouldUpdateConfig) {
this.loadConfiguration().then((telegramConfig) => {
this.updateAppConfigs(telegramConfig);
this.updateWidgetConfigs(telegramConfig);
}).catch((err) => {
throw new Error(err.toString());
});
}
}
async loadConfiguration() {
const response = await fetch(this.getJsonConfigUrl(), {
method: 'GET',
headers: {
accept: 'application/json',
},
});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const json = await response.json();
return json.telegram;
}
getDebug() {
return this.debug;
}
getActiveWidgetTypes() {
return this.activeWidgetTypes;
}
getWidgetIdByType(widgetType) {
return !this.isWidgetTypeValid(widgetType) ? null : String(this.widgetTypes[widgetType]);
}
isWidgetTypeValid(widgetType) {
return this.widgetTypes[widgetType] && this.activeWidgetTypes.includes(widgetType);
}
updateAppConfigs(config) {
let appConfig = config.app_id[this.appId];
if (appConfig) {
this.widgetTypes = this.transformWidgetTypes(appConfig.widgetTypes);
this.activeWidgetTypes = [];
this.pushStyleAutoMode = (appConfig.pushStyleAutoMode ?? false) || (appConfig.nativeAutoMode ?? false);
this.embeddedBannerAutoMode = (appConfig.embeddedBannerAutoMode ?? false) || (appConfig.bannerAutoMode ?? false);
this.interstitialBannerAutoMode = appConfig.embeddedBannerAutoMode ?? false;
if (appConfig.activeWidgetTypes.includes(index_js_1.WidgetType.PUSH_STYLE_OLD) || appConfig.activeWidgetTypes.includes(index_js_1.WidgetType.PUSH_STYLE)) {
this.activeWidgetTypes.push(index_js_1.WidgetType.PUSH_STYLE);
}
if (appConfig.activeWidgetTypes.includes(index_js_1.WidgetType.EMBEDDED_BANNER_OLD) || appConfig.activeWidgetTypes.includes(index_js_1.WidgetType.EMBEDDED_BANNER)) {
this.activeWidgetTypes.push(index_js_1.WidgetType.EMBEDDED_BANNER);
}
if (appConfig.activeWidgetTypes.includes(index_js_1.WidgetType.INTERSTITIAL_BANNER)) {
this.activeWidgetTypes.push(index_js_1.WidgetType.INTERSTITIAL_BANNER);
}
if (appConfig.activeWidgetTypes.includes(index_js_1.WidgetType.INTERSTITIAL_VIDEO)) {
this.activeWidgetTypes.push(index_js_1.WidgetType.INTERSTITIAL_VIDEO);
}
index_js_2.LS.set(index_js_3.LOCAL_STORAGE_KEYS.WIDGET_TYPE, this.widgetTypes);
index_js_2.LS.set(index_js_3.LOCAL_STORAGE_KEYS.ACTIVE_WIDGET_TYPE, this.activeWidgetTypes);
index_js_2.LS.set(index_js_3.LOCAL_STORAGE_KEYS.PUSH_STYLE_AUTO_MODE, this.pushStyleAutoMode);
index_js_2.LS.set(index_js_3.LOCAL_STORAGE_KEYS.EMBEDDED_BANNER_AUTO_MODE, this.embeddedBannerAutoMode);
index_js_2.LS.set(index_js_3.LOCAL_STORAGE_KEYS.INTERSTITIAL_BANNER_AUTO_MODE, this.interstitialBannerAutoMode);
index_js_2.LS.set(index_js_3.LOCAL_STORAGE_KEYS.INTERSTITIAL_VIDEO_AUTO_MODE, this.interstitialVideoAutoMode);
index_js_2.LS.set(index_js_3.LOCAL_STORAGE_KEYS.CONFIG_UPDATE_TIME, 1, index_js_3.CONFIG_UPDATE_TIME);
}
}
updateWidgetConfigs(config) {
let richPartnersAds = null;
this.activeWidgetTypes.forEach(type => {
richPartnersAds = index_js_4.AdsFactory.createRichPartnersAdsByType(String(type));
richPartnersAds.updateConfigParams(config);
richPartnersAds.setWidgetId(this.widgetTypes[richPartnersAds.getType()]);
});
}
isUploadedOldVersion() {
const activeWidgetTypes = index_js_2.LS.get(index_js_3.LOCAL_STORAGE_KEYS.ACTIVE_WIDGET_TYPE) || [];
return activeWidgetTypes.includes(index_js_1.WidgetType.EMBEDDED_BANNER_OLD) || activeWidgetTypes.includes(index_js_1.WidgetType.PUSH_STYLE_OLD);
}
getJsonConfigUrl() {
const hash = crypto_js_1.default.MD5(String(this.publisherId));
return index_js_3.CONFIG_URL + hash + '.json';
}
}
exports.WidgetManager = WidgetManager;
//# sourceMappingURL=WidgetManager.js.map