Always translate user role

This commit is contained in:
Chocobozzz 2023-04-17 09:21:02 +02:00
parent 29ccdcb51f
commit 208c97e111
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 32 additions and 27 deletions

View File

@ -2,6 +2,7 @@ import { Directive, OnInit } from '@angular/core'
import { ConfigService } from '@app/+admin/config/shared/config.service' import { ConfigService } from '@app/+admin/config/shared/config.service'
import { AuthService, ScreenService, ServerService, User } from '@app/core' import { AuthService, ScreenService, ServerService, User } from '@app/core'
import { FormReactive } from '@app/shared/shared-forms' import { FormReactive } from '@app/shared/shared-forms'
import { peertubeTranslate } from '@shared/core-utils'
import { USER_ROLE_LABELS } from '@shared/core-utils/users' import { USER_ROLE_LABELS } from '@shared/core-utils/users'
import { HTMLServerConfig, UserAdminFlag, UserRole } from '@shared/models' import { HTMLServerConfig, UserAdminFlag, UserRole } from '@shared/models'
import { SelectOptionsItem } from '../../../../../types/select-options-item.model' import { SelectOptionsItem } from '../../../../../types/select-options-item.model'
@ -25,7 +26,7 @@ export abstract class UserEdit extends FormReactive implements OnInit {
abstract isCreation (): boolean abstract isCreation (): boolean
abstract getFormButtonTitle (): string abstract getFormButtonTitle (): string
ngOnInit (): void { ngOnInit () {
this.serverConfig = this.serverService.getHTMLConfig() this.serverConfig = this.serverService.getHTMLConfig()
this.buildRoles() this.buildRoles()
@ -49,15 +50,18 @@ export abstract class UserEdit extends FormReactive implements OnInit {
buildRoles () { buildRoles () {
const authUser = this.auth.getUser() const authUser = this.auth.getUser()
this.serverService.getServerLocale()
.subscribe(translations => {
if (authUser.role.id === UserRole.ADMINISTRATOR) { if (authUser.role.id === UserRole.ADMINISTRATOR) {
this.roles = Object.keys(USER_ROLE_LABELS) this.roles = Object.keys(USER_ROLE_LABELS)
.map(key => ({ value: key.toString(), label: USER_ROLE_LABELS[key] })) .map(key => ({ value: key.toString(), label: peertubeTranslate(USER_ROLE_LABELS[key], translations) }))
return return
} }
this.roles = [ this.roles = [
{ value: UserRole.USER.toString(), label: USER_ROLE_LABELS[UserRole.USER] } { value: UserRole.USER.toString(), label: peertubeTranslate(USER_ROLE_LABELS[UserRole.USER], translations) }
] ]
})
} }
displayDangerZone () { displayDangerZone () {

View File

@ -137,7 +137,7 @@ export class ServerService {
return this.videoPlaylistPrivaciesObservable.pipe(first()) return this.videoPlaylistPrivaciesObservable.pipe(first())
} }
getServerLocale () { getServerLocale (): Observable<{ [ id: string ]: string }> {
if (!this.localeObservable) { if (!this.localeObservable) {
const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId)

View File

@ -1,12 +1,12 @@
import { SortMeta } from 'primeng/api' import { SortMeta } from 'primeng/api'
import { from, Observable } from 'rxjs' import { from, Observable } from 'rxjs'
import { catchError, concatMap, map, toArray } from 'rxjs/operators' import { catchError, concatMap, map, switchMap, toArray } from 'rxjs/operators'
import { HttpClient, HttpParams } from '@angular/common/http' import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core' import { Injectable } from '@angular/core'
import { RestExtractor, RestPagination, RestService, UserService } from '@app/core' import { RestExtractor, RestPagination, RestService, ServerService, UserService } from '@app/core'
import { getBytes } from '@root-helpers/bytes' import { getBytes } from '@root-helpers/bytes'
import { arrayify } from '@shared/core-utils' import { arrayify, peertubeTranslate } from '@shared/core-utils'
import { ResultList, User as UserServerModel, UserCreate, UserRole, UserUpdate } from '@shared/models' import { ResultList, User as UserServerModel, UserCreate, UserUpdate } from '@shared/models'
@Injectable() @Injectable()
export class UserAdminService { export class UserAdminService {
@ -14,7 +14,8 @@ export class UserAdminService {
constructor ( constructor (
private authHttp: HttpClient, private authHttp: HttpClient,
private restExtractor: RestExtractor, private restExtractor: RestExtractor,
private restService: RestService private restService: RestService,
private serverService: ServerService
) { } ) { }
addUser (userCreate: UserCreate) { addUser (userCreate: UserCreate) {
@ -59,7 +60,13 @@ export class UserAdminService {
return this.authHttp.get<ResultList<UserServerModel>>(UserService.BASE_USERS_URL, { params }) return this.authHttp.get<ResultList<UserServerModel>>(UserService.BASE_USERS_URL, { params })
.pipe( .pipe(
map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))), switchMap(data => {
return this.serverService.getServerLocale()
.pipe(map(translations => ({ data, translations })))
}),
map(({ data, translations }) => {
return this.restExtractor.applyToResultListData(data, this.formatUser.bind(this), [translations])
}),
catchError(err => this.restExtractor.handleError(err)) catchError(err => this.restExtractor.handleError(err))
) )
} }
@ -98,7 +105,7 @@ export class UserAdminService {
) )
} }
private formatUser (user: UserServerModel) { private formatUser (user: UserServerModel, translations: { [ id: string ]: string } = {}) {
let videoQuota let videoQuota
if (user.videoQuota === -1) { if (user.videoQuota === -1) {
videoQuota = '∞' videoQuota = '∞'
@ -118,16 +125,10 @@ export class UserAdminService {
videoQuotaUsedDaily = getBytes(user.videoQuotaUsedDaily || 0, 0) + '' videoQuotaUsedDaily = getBytes(user.videoQuotaUsedDaily || 0, 0) + ''
} }
const roleLabels: { [ id in UserRole ]: string } = {
[UserRole.USER]: $localize`User`,
[UserRole.ADMINISTRATOR]: $localize`Administrator`,
[UserRole.MODERATOR]: $localize`Moderator`
}
return Object.assign(user, { return Object.assign(user, {
role: { role: {
id: user.role.id, id: user.role.id,
label: roleLabels[user.role.id] label: peertubeTranslate(user.role.label, translations)
}, },
videoQuota, videoQuota,
videoQuotaUsed, videoQuotaUsed,