aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-instance/instance.service.ts
blob: 58bf8ee54cefca5f4b26af9f7f2ba88c1752de01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { forkJoin } from 'rxjs'
import { catchError, map } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { MarkdownService, RestExtractor, ServerService } from '@app/core'
import { objectKeysTyped, peertubeTranslate } from '@peertube/peertube-core-utils'
import { About } from '@peertube/peertube-models'
import { environment } from '../../../environments/environment'
import { logger } from '@root-helpers/logger'

export type AboutHTML = Pick<About['instance'],
'terms' | 'codeOfConduct' | 'moderationInformation' | 'administrator' | 'creationReason' |
'maintenanceLifetime' | 'businessModel' | 'hardwareInformation'
>

@Injectable()
export class InstanceService {
  private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config'
  private static BASE_SERVER_URL = environment.apiUrl + '/api/v1/server'

  constructor (
    private authHttp: HttpClient,
    private restExtractor: RestExtractor,
    private markdownService: MarkdownService,
    private serverService: ServerService
  ) {
  }

  getAbout () {
    return this.authHttp.get<About>(InstanceService.BASE_CONFIG_URL + '/about')
               .pipe(catchError(res => this.restExtractor.handleError(res)))
  }

  contactAdministrator (fromEmail: string, fromName: string, subject: string, message: string) {
    const body = {
      fromEmail,
      fromName,
      subject,
      body: message
    }

    return this.authHttp.post(InstanceService.BASE_SERVER_URL + '/contact', body)
               .pipe(catchError(res => this.restExtractor.handleError(res)))

  }

  async buildHtml (about: About) {
    const html: AboutHTML = {
      terms: '',
      codeOfConduct: '',
      moderationInformation: '',
      administrator: '',
      creationReason: '',
      maintenanceLifetime: '',
      businessModel: '',
      hardwareInformation: ''
    }

    for (const key of objectKeysTyped(html)) {
      html[key] = await this.markdownService.enhancedMarkdownToHTML({ markdown: about.instance[key] })
    }

    return html
  }

  buildTranslatedLanguages (about: About) {
    return forkJoin([
      this.serverService.getVideoLanguages(),
      this.serverService.getServerLocale()
    ]).pipe(
      map(([ languagesArray, translations ]) => {
        return about.instance.languages
          .map(l => {
            const languageObj = languagesArray.find(la => la.id === l)
            if (!languageObj) {
              logger.error(`Cannot find language ${l} in available languages`)
              return ''
            }

            return peertubeTranslate(languageObj.label, translations)
          })
      })
    )
  }

  buildTranslatedCategories (about: About) {
    return forkJoin([
      this.serverService.getVideoCategories(),
      this.serverService.getServerLocale()
    ]).pipe(
      map(([ categoriesArray, translations ]) => {
        return about.instance.categories
          .map(c => {
            const categoryObj = categoriesArray.find(ca => ca.id === c)
            if (!categoryObj) {
              logger.error(`Cannot find instance category ${c} in available categories`)
              return ''
            }

            return peertubeTranslate(categoryObj.label, translations)
          })
      })
    )
  }
}