<commit_before>import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import 'rxjs/add/operator/do'
import { ReplaySubject } from 'rxjs/ReplaySubject'
import { ServerConfig } from '../../../../../shared'
import { About } from '../../../../../shared/models/config/about.model'
import { environment } from '../../../environments/environment'

@Injectable()
export class ServerService {
  private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
  private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
  private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'

  videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
  videoCategoriesLoaded = new ReplaySubject<boolean>(1)
  videoLicencesLoaded = new ReplaySubject<boolean>(1)
  videoLanguagesLoaded = new ReplaySubject<boolean>(1)

  private config: ServerConfig = {
    instance: {
      name: 'PeerTube'
    },
    serverVersion: 'Unknown',
    signup: {
      allowed: false
    },
    transcoding: {
      enabledResolutions: []
    },
    avatar: {
      file: {
        size: { max: 0 },
        extensions: []
      }
    },
    video: {
      image: {
        size: { max: 0 },
        extensions: []
      },
      file: {
        extensions: []
      }
    }
  }
  private videoCategories: Array<{ id: number, label: string }> = []
  private videoLicences: Array<{ id: number, label: string }> = []
  private videoLanguages: Array<{ id: number, label: string }> = []
  private videoPrivacies: Array<{ id: number, label: string }> = []

  constructor (private http: HttpClient) {
    this.loadConfigLocally()
  }

  loadConfig () {
    this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
      .do(this.saveConfigLocally)
      .subscribe(data => this.config = data)
  }

  loadVideoCategories () {
    return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded)
  }

  loadVideoLicences () {
    return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
  }

  loadVideoLanguages () {
    return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded)
  }

  loadVideoPrivacies () {
    return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
  }

  getConfig () {
    return this.config
  }

  getVideoCategories () {
    return this.videoCategories
  }

  getVideoLicences () {
    return this.videoLicences
  }

  getVideoLanguages () {
    return this.videoLanguages
  }

  getVideoPrivacies () {
    return this.videoPrivacies
  }

  getAbout () {
    return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
  }

  private loadVideoAttributeEnum (
    attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
    hashToPopulate: { id: number, label: string }[],
    notifier: ReplaySubject<boolean>
  ) {
    return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
       .subscribe(data => {
         Object.keys(data)
               .forEach(dataKey => {
                 hashToPopulate.push({
                   id: parseInt(dataKey, 10),
                   label: data[dataKey]
                 })
               })

         notifier.next(true)
       })
  }

  private saveConfigLocally (config: ServerConfig) {
    localStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
  }

  private loadConfigLocally () {
    const configString = localStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)<commit_msg>Sort video categories/languages<commit_after>import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import 'rxjs/add/operator/do'
import { ReplaySubject } from 'rxjs/ReplaySubject'
import { ServerConfig } from '../../../../../shared'
import { About } from '../../../../../shared/models/config/about.model'
import { environment } from '../../../environments/environment'

@Injectable()
export class ServerService {
  private static BASE_CONFIG_URL = environment.apiUrl + '/api/v1/config/'
  private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
  private static CONFIG_LOCAL_STORAGE_KEY = 'server-config'

  videoPrivaciesLoaded = new ReplaySubject<boolean>(1)
  videoCategoriesLoaded = new ReplaySubject<boolean>(1)
  videoLicencesLoaded = new ReplaySubject<boolean>(1)
  videoLanguagesLoaded = new ReplaySubject<boolean>(1)

  private config: ServerConfig = {
    instance: {
      name: 'PeerTube'
    },
    serverVersion: 'Unknown',
    signup: {
      allowed: false
    },
    transcoding: {
      enabledResolutions: []
    },
    avatar: {
      file: {
        size: { max: 0 },
        extensions: []
      }
    },
    video: {
      image: {
        size: { max: 0 },
        extensions: []
      },
      file: {
        extensions: []
      }
    }
  }
  private videoCategories: Array<{ id: number, label: string }> = []
  private videoLicences: Array<{ id: number, label: string }> = []
  private videoLanguages: Array<{ id: number, label: string }> = []
  private videoPrivacies: Array<{ id: number, label: string }> = []

  constructor (private http: HttpClient) {
    this.loadConfigLocally()
  }

  loadConfig () {
    this.http.get<ServerConfig>(ServerService.BASE_CONFIG_URL)
      .do(this.saveConfigLocally)
      .subscribe(data => this.config = data)
  }

  loadVideoCategories () {
    return this.loadVideoAttributeEnum('categories', this.videoCategories, this.videoCategoriesLoaded, true)
  }

  loadVideoLicences () {
    return this.loadVideoAttributeEnum('licences', this.videoLicences, this.videoLicencesLoaded)
  }

  loadVideoLanguages () {
    return this.loadVideoAttributeEnum('languages', this.videoLanguages, this.videoLanguagesLoaded, true)
  }

  loadVideoPrivacies () {
    return this.loadVideoAttributeEnum('privacies', this.videoPrivacies, this.videoPrivaciesLoaded)
  }

  getConfig () {
    return this.config
  }

  getVideoCategories () {
    return this.videoCategories
  }

  getVideoLicences () {
    return this.videoLicences
  }

  getVideoLanguages () {
    return this.videoLanguages
  }

  getVideoPrivacies () {
    return this.videoPrivacies
  }

  getAbout () {
    return this.http.get<About>(ServerService.BASE_CONFIG_URL + '/about')
  }

  private loadVideoAttributeEnum (
    attributeName: 'categories' | 'licences' | 'languages' | 'privacies',
    hashToPopulate: { id: number, label: string }[],
    notifier: ReplaySubject<boolean>,
    sort = false
  ) {
    return this.http.get(ServerService.BASE_VIDEO_URL + attributeName)
       .subscribe(data => {
         Object.keys(data)
               .forEach(dataKey => {
                 hashToPopulate.push({
                   id: parseInt(dataKey, 10),
                   label: data[dataKey]
                 })
               })

         if (sort === true) {
           hashToPopulate.sort((a, b) => {
             if (a.label < b.label) return -1
             if (a.label === b.label) return 0
             return 1
           })
         }

         notifier.next(true)
       })
  }

  private saveConfigLocally (config: ServerConfig) {
    localStorage.setItem(ServerService.CONFIG_LOCAL_STORAGE_KEY, JSON.stringify(config))
  }

  private loadConfigLocally () {
    const configString = localStorage.getItem(ServerService.CONFIG_LOCAL_STORAGE_KEY)
{'fraction_non_alphanumeric': 0.12530413625304138, 'fraction_numerical': 0.003108948364422817, 'mean_word_length': 2.910676532769556, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6603718', 'n_tokens_mistral': 2259, 'n_tokens_neox': 2194, 'n_words': 420}