mempool/frontend/src/app/services/enterprise.service.ts

210 lines
6.3 KiB
TypeScript
Raw Normal View History

2022-07-21 19:58:12 +02:00
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable } from '@angular/core';
import { ApiService } from '@app/services/api.service';
import { SeoService } from '@app/services/seo.service';
2025-01-18 07:43:45 +00:00
import { Customization, StateService } from '@app/services/state.service';
2023-11-24 09:17:14 +00:00
import { ActivatedRoute } from '@angular/router';
import { BehaviorSubject } from 'rxjs';
2022-07-21 19:58:12 +02:00
@Injectable({
providedIn: 'root'
})
export class EnterpriseService {
exclusiveHostName = '.mempool.space';
subdomain: string | null = null;
2023-11-24 09:17:14 +00:00
statsUrl: string;
siteId: number;
info$: BehaviorSubject<object> = new BehaviorSubject(null);
2022-07-21 19:58:12 +02:00
constructor(
@Inject(DOCUMENT) private document: Document,
private apiService: ApiService,
private seoService: SeoService,
private stateService: StateService,
2023-11-24 09:17:14 +00:00
private activatedRoute: ActivatedRoute,
2022-07-21 19:58:12 +02:00
) {
2024-04-25 20:39:57 +00:00
const subdomain = this.stateService.env.customize?.enterprise || this.document.location.hostname.indexOf(this.exclusiveHostName) > -1
2022-07-21 19:58:12 +02:00
&& this.document.location.hostname.split(this.exclusiveHostName)[0] || false;
if (subdomain && subdomain.match(/^[A-z0-9-_]+$/)) {
this.subdomain = subdomain;
this.fetchSubdomainInfo();
this.disableSubnetworks();
this.stateService.env.ACCELERATOR = false;
} else {
2022-07-21 19:58:12 +02:00
this.insertMatomo();
}
}
getSubdomain(): string {
2022-07-21 19:58:12 +02:00
return this.subdomain;
}
disableSubnetworks(): void {
2022-07-21 19:58:12 +02:00
this.stateService.env.TESTNET_ENABLED = false;
2024-05-06 15:40:32 +00:00
this.stateService.env.TESTNET4_ENABLED = false;
2022-07-21 19:58:12 +02:00
this.stateService.env.LIQUID_ENABLED = false;
this.stateService.env.LIQUID_TESTNET_ENABLED = false;
this.stateService.env.SIGNET_ENABLED = false;
}
fetchSubdomainInfo(): void {
2024-04-25 20:39:57 +00:00
if (this.stateService.env.customize?.branding) {
const info = this.stateService.env.customize?.branding;
2022-07-21 19:58:12 +02:00
this.insertMatomo(info.site_id);
2025-01-18 07:43:45 +00:00
this.setFavicons(this.stateService.env.customize);
2025-01-18 07:19:58 +00:00
this.seoService.setCustomMeta(this.stateService.env.customize);
2024-04-27 20:39:53 +00:00
this.seoService.setEnterpriseTitle(info.title, true);
this.info$.next(info);
2024-04-25 20:39:57 +00:00
} else {
this.apiService.getEnterpriseInfo$(this.subdomain).subscribe((info) => {
this.insertMatomo(info.site_id);
this.seoService.setEnterpriseTitle(info.title);
this.info$.next(info);
},
(error) => {
if (error.status === 404) {
window.location.href = 'https://mempool.space' + window.location.pathname;
}
});
}
2022-07-21 19:58:12 +02:00
}
2025-01-18 07:43:45 +00:00
setFavicons(customize: Customization): void {
const enterprise = customize.enterprise;
const head = this.document.getElementsByTagName('head')[0];
const faviconLinks = [
{
rel: 'apple-touch-icon',
sizes: '180x180',
href: `/resources/${enterprise}/favicons/apple-touch-icon.png`
},
{
rel: 'icon',
type: 'image/png',
sizes: '32x32',
href: `/resources/${enterprise}/favicons/favicon-32x32.png`
},
{
rel: 'icon',
type: 'image/png',
sizes: '16x16',
href: `/resources/${enterprise}/favicons/favicon-16x16.png`
},
{
rel: 'manifest',
href: `/resources/${enterprise}/favicons/site.webmanifest`
},
{
rel: 'shortcut icon',
href: `/resources/${enterprise}/favicons/favicon.ico`
}
];
faviconLinks.forEach(linkInfo => {
let link = this.document.querySelector(`link[rel="${linkInfo.rel}"]${linkInfo.sizes ? `[sizes="${linkInfo.sizes}"]` : ''}`) as HTMLLinkElement;
if (!link) {
link = this.document.createElement('link');
head.appendChild(link);
}
Object.entries(linkInfo).forEach(([attr, value]) => {
link.setAttribute(attr, value);
});
});
}
insertMatomo(siteId?: number): void {
2022-07-21 19:58:12 +02:00
let statsUrl = '//stats.mempool.space/';
2023-11-24 09:17:14 +00:00
if (!siteId) {
2022-08-12 16:08:34 +04:00
switch (this.document.location.hostname) {
case 'mempool.space':
statsUrl = '//stats.mempool.space/';
siteId = 5;
break;
case 'mempool.ninja':
statsUrl = '//stats.mempool.space/';
siteId = 4;
break;
case 'liquid.network':
siteId = 8;
statsUrl = '//stats.liquid.network/';
break;
case 'liquid.place':
siteId = 10;
statsUrl = '//stats.liquid.network/';
break;
default:
return;
}
2022-07-21 19:58:12 +02:00
}
2023-11-24 09:17:14 +00:00
this.statsUrl = statsUrl;
this.siteId = siteId;
2022-07-21 19:58:12 +02:00
// @ts-ignore
2023-11-24 09:17:14 +00:00
if (window._paq && window['Matomo']) {
2023-11-30 15:46:58 +09:00
window['Matomo'].addTracker(statsUrl+'m.php', siteId.toString());
2023-11-24 09:17:14 +00:00
const matomo = this.getMatomo();
matomo.setDocumentTitle(this.seoService.getTitle());
matomo.setCustomUrl(this.getCustomUrl());
matomo.disableCookies();
matomo.trackPageView();
matomo.enableLinkTracking();
} else {
// @ts-ignore
const alreadyInitialized = !!window._paq;
// @ts-ignore
const _paq = window._paq = window._paq || [];
_paq.push(['setDocumentTitle', this.seoService.getTitle()]);
_paq.push(['setCustomUrl', this.getCustomUrl()]);
_paq.push(['disableCookies']);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
if (alreadyInitialized) {
2023-11-30 15:46:58 +09:00
_paq.push(['addTracker', statsUrl+'m.php', siteId.toString()]);
2023-11-24 09:17:14 +00:00
} else {
(function() {
2023-11-30 15:46:58 +09:00
_paq.push(['setTrackerUrl', statsUrl+'m.php']);
_paq.push(['setSiteId', siteId.toString()]);
2023-11-24 09:17:14 +00:00
const d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
// @ts-ignore
g.type='text/javascript'; g.async=true; g.src=statsUrl+'m.js'; s.parentNode.insertBefore(g,s);
})();
}
}
}
private getMatomo() {
if (this.siteId != null) {
2023-11-30 17:35:26 +09:00
return window['Matomo']?.getTracker(this.statsUrl+'m.php', this.siteId);
2023-11-24 09:17:14 +00:00
}
}
goal(id: number) {
// @ts-ignore
this.getMatomo()?.trackGoal(id);
}
2024-02-14 21:47:42 +00:00
page() {
const matomo = this.getMatomo();
if (matomo) {
matomo.setCustomUrl(this.getCustomUrl());
matomo.trackPageView();
}
2024-02-14 21:47:42 +00:00
}
2023-11-24 09:17:14 +00:00
private getCustomUrl(): string {
let url = window.location.origin + '/';
let route = this.activatedRoute;
while (route) {
const segment = route?.routeConfig?.path;
if (segment && segment.length) {
url += segment + '/';
}
route = route.firstChild;
}
return url;
2022-07-21 19:58:12 +02:00
}
}