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