diff options
Diffstat (limited to 'client/src/app/shared/shared-instance/instance.service.ts')
-rw-r--r-- | client/src/app/shared/shared-instance/instance.service.ts | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/client/src/app/shared/shared-instance/instance.service.ts b/client/src/app/shared/shared-instance/instance.service.ts new file mode 100644 index 000000000..ba9797bb5 --- /dev/null +++ b/client/src/app/shared/shared-instance/instance.service.ts | |||
@@ -0,0 +1,88 @@ | |||
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 { About, peertubeTranslate } from '@shared/models' | ||
7 | import { environment } from '../../../environments/environment' | ||
8 | |||
9 | @Injectable() | ||
10 | export class InstanceService { | ||
11 | private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config' | ||
12 | private static BASE_SERVER_URL = environment.apiUrl + '/api/v1/server' | ||
13 | |||
14 | constructor ( | ||
15 | private authHttp: HttpClient, | ||
16 | private restExtractor: RestExtractor, | ||
17 | private markdownService: MarkdownService, | ||
18 | private serverService: ServerService | ||
19 | ) { | ||
20 | } | ||
21 | |||
22 | getAbout () { | ||
23 | return this.authHttp.get<About>(InstanceService.BASE_CONFIG_URL + '/about') | ||
24 | .pipe(catchError(res => this.restExtractor.handleError(res))) | ||
25 | } | ||
26 | |||
27 | contactAdministrator (fromEmail: string, fromName: string, subject: string, message: string) { | ||
28 | const body = { | ||
29 | fromEmail, | ||
30 | fromName, | ||
31 | subject, | ||
32 | body: message | ||
33 | } | ||
34 | |||
35 | return this.authHttp.post(InstanceService.BASE_SERVER_URL + '/contact', body) | ||
36 | .pipe(catchError(res => this.restExtractor.handleError(res))) | ||
37 | |||
38 | } | ||
39 | |||
40 | async buildHtml (about: About) { | ||
41 | const html = { | ||
42 | description: '', | ||
43 | terms: '', | ||
44 | codeOfConduct: '', | ||
45 | moderationInformation: '', | ||
46 | administrator: '', | ||
47 | hardwareInformation: '' | ||
48 | } | ||
49 | |||
50 | for (const key of Object.keys(html)) { | ||
51 | html[ key ] = await this.markdownService.textMarkdownToHTML(about.instance[ key ]) | ||
52 | } | ||
53 | |||
54 | return html | ||
55 | } | ||
56 | |||
57 | buildTranslatedLanguages (about: About) { | ||
58 | return forkJoin([ | ||
59 | this.serverService.getVideoLanguages(), | ||
60 | this.serverService.getServerLocale() | ||
61 | ]).pipe( | ||
62 | map(([ languagesArray, translations ]) => { | ||
63 | return about.instance.languages | ||
64 | .map(l => { | ||
65 | const languageObj = languagesArray.find(la => la.id === l) | ||
66 | |||
67 | return peertubeTranslate(languageObj.label, translations) | ||
68 | }) | ||
69 | }) | ||
70 | ) | ||
71 | } | ||
72 | |||
73 | buildTranslatedCategories (about: About) { | ||
74 | return forkJoin([ | ||
75 | this.serverService.getVideoCategories(), | ||
76 | this.serverService.getServerLocale() | ||
77 | ]).pipe( | ||
78 | map(([ categoriesArray, translations ]) => { | ||
79 | return about.instance.categories | ||
80 | .map(c => { | ||
81 | const categoryObj = categoriesArray.find(ca => ca.id === c) | ||
82 | |||
83 | return peertubeTranslate(categoryObj.label, translations) | ||
84 | }) | ||
85 | }) | ||
86 | ) | ||
87 | } | ||
88 | } | ||