]>
Commit | Line | Data |
---|---|---|
1 | import { forkJoin } from 'rxjs' | |
2 | import { catchError, map } from 'rxjs/operators' | |
3 | import { HttpClient } from '@angular/common/http' | |
4 | import { Injectable } from '@angular/core' | |
5 | import { MarkdownService, RestExtractor, ServerService } from '@app/core' | |
6 | import { objectKeysTyped } from '@shared/core-utils' | |
7 | import { peertubeTranslate } from '@shared/core-utils/i18n' | |
8 | import { About } from '@shared/models' | |
9 | import { environment } from '../../../environments/environment' | |
10 | ||
11 | export type AboutHTML = Pick<About['instance'], | |
12 | 'terms' | 'codeOfConduct' | 'moderationInformation' | 'administrator' | 'creationReason' | | |
13 | 'maintenanceLifetime' | 'businessModel' | 'hardwareInformation' | |
14 | > | |
15 | ||
16 | @Injectable() | |
17 | export class InstanceService { | |
18 | private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config' | |
19 | private static BASE_SERVER_URL = environment.apiUrl + '/api/v1/server' | |
20 | ||
21 | constructor ( | |
22 | private authHttp: HttpClient, | |
23 | private restExtractor: RestExtractor, | |
24 | private markdownService: MarkdownService, | |
25 | private serverService: ServerService | |
26 | ) { | |
27 | } | |
28 | ||
29 | getAbout () { | |
30 | return this.authHttp.get<About>(InstanceService.BASE_CONFIG_URL + '/about') | |
31 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
32 | } | |
33 | ||
34 | contactAdministrator (fromEmail: string, fromName: string, subject: string, message: string) { | |
35 | const body = { | |
36 | fromEmail, | |
37 | fromName, | |
38 | subject, | |
39 | body: message | |
40 | } | |
41 | ||
42 | return this.authHttp.post(InstanceService.BASE_SERVER_URL + '/contact', body) | |
43 | .pipe(catchError(res => this.restExtractor.handleError(res))) | |
44 | ||
45 | } | |
46 | ||
47 | async buildHtml (about: About) { | |
48 | const html: AboutHTML = { | |
49 | terms: '', | |
50 | codeOfConduct: '', | |
51 | moderationInformation: '', | |
52 | administrator: '', | |
53 | creationReason: '', | |
54 | maintenanceLifetime: '', | |
55 | businessModel: '', | |
56 | hardwareInformation: '' | |
57 | } | |
58 | ||
59 | for (const key of objectKeysTyped(html)) { | |
60 | html[key] = await this.markdownService.enhancedMarkdownToHTML({ markdown: about.instance[key] }) | |
61 | } | |
62 | ||
63 | return html | |
64 | } | |
65 | ||
66 | buildTranslatedLanguages (about: About) { | |
67 | return forkJoin([ | |
68 | this.serverService.getVideoLanguages(), | |
69 | this.serverService.getServerLocale() | |
70 | ]).pipe( | |
71 | map(([ languagesArray, translations ]) => { | |
72 | return about.instance.languages | |
73 | .map(l => { | |
74 | const languageObj = languagesArray.find(la => la.id === l) | |
75 | ||
76 | return peertubeTranslate(languageObj.label, translations) | |
77 | }) | |
78 | }) | |
79 | ) | |
80 | } | |
81 | ||
82 | buildTranslatedCategories (about: About) { | |
83 | return forkJoin([ | |
84 | this.serverService.getVideoCategories(), | |
85 | this.serverService.getServerLocale() | |
86 | ]).pipe( | |
87 | map(([ categoriesArray, translations ]) => { | |
88 | return about.instance.categories | |
89 | .map(c => { | |
90 | const categoryObj = categoriesArray.find(ca => ca.id === c) | |
91 | ||
92 | return peertubeTranslate(categoryObj.label, translations) | |
93 | }) | |
94 | }) | |
95 | ) | |
96 | } | |
97 | } |