<commit_before>import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { filter } from 'rxjs/operators'

import { environment } from '../../../../environments/environment'
import { Doc } from '../../Doc'
import { Folder } from '../../Folder'
import { EProperties } from '../../settings/EProperties'
import { SettingsService } from '../../settings/settings.service'
import { Storage } from '../Storage'

@Injectable()
export class BotStorageService extends Storage {
  public static NOT_AUTHORIZED = 1
  public static NOT_RESPONDING = 2

  public name: string
  public remote: Folder

  private url: string
  private webSocketPath: string
  private secure: boolean

  constructor(private http: HttpClient, private settings: SettingsService) {
    super()
    const botStorage = environment.botStorage || undefined

    if (botStorage && 'url' in botStorage && 'secure' in botStorage && 'webSocketPath' in botStorage) {
      const bs = botStorage as any
      this.name = ''
      this.secure = bs.secure
      this.url = bs.url
      this.webSocketPath = bs.webSocketPath
      this.remote = Folder.create('Remote storage', 'cloud', true)
      this.remote.id = this.id
    }

    settings.onChange.pipe(filter((properties) => properties.includes(EProperties.profile))).subscribe(() => this.updateStatus())
  }

  async fetchDocs(): Promise<string[]> {
    if (this.url && this.status !== BotStorageService.NOT_AUTHORIZED) {
      return (await new Promise((resolve) => {
        this.http.get(`${this.httpURL}/docs/${this.settings.profile.login}`).subscribe(
          (keys) => resolve(keys),
          (err) => {
            log.warn('Could not retreive documents keys from the bot storage')
            super.setStatus(BotStorageService.NOT_RESPONDING)
            resolve([])
          }
        )
      })) as string[]
    }
    return []
  }

  async remove(doc: Doc): Promise<void> {
    return (await new Promise((resolve) => {
      this.http
        .post<{ key: string; login: string }>(`${this.httpURL}/remove`, {
          key: doc.key,
          login: this.settings.profile.login,
        })
        .subscribe(() => resolve())
    })) as Promise<void>
  }

  get login() {
    return this.extractHostname(this.url)
  }

  get httpURL() {
    const scheme = this.secure ? 'https' : 'http'
    return `${scheme}://${this.url}`
  }

  get wsURL() {
    const scheme = this.secure ? 'wss' : 'ws'
    return `${scheme}://${this.url}/${this.webSocketPath}`
  }

  get id() {
    return `${this.url}`
  }

  private updateStatus(): Promise<void> {
    if (this.url) {
      if (!this.settings.isAuthenticated() && !(environment.botStorage as any).isAnonymousAllowed) {
        super.setStatus(BotStorageService.NOT_AUTHORIZED)
      } else {
        return new Promise((resolve) => {
          this.http.get(`${this.httpURL}/name`, { responseType: 'text' }).subscribe(
            (name: string) => {
              this.name = name
              super.setStatus(BotStorageService.AVAILABLE)
              resolve()
            },
            (err) => {
              super.setStatus(BotStorageService.NOT_RESPONDING)
              resolve()
            }<commit_msg>fix(bot): parsing of botStorage environment property<commit_after>import { HttpClient } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { filter } from 'rxjs/operators'

import { environment } from '../../../../environments/environment'
import { Doc } from '../../Doc'
import { Folder } from '../../Folder'
import { EProperties } from '../../settings/EProperties'
import { SettingsService } from '../../settings/settings.service'
import { Storage } from '../Storage'

@Injectable()
export class BotStorageService extends Storage {
  public static NOT_AUTHORIZED = 1
  public static NOT_RESPONDING = 2

  public name: string
  public remote: Folder

  private url: string
  private webSocketPath: string
  private secure: boolean

  constructor(private http: HttpClient, private settings: SettingsService) {
    super()
    const botStorage = environment.botStorage || undefined

    if (botStorage && 'url' in botStorage && 'secure' in botStorage && 'webSocketPath' in botStorage) {
      const bs = botStorage as any
      this.name = ''
      this.secure = bs.secure
      this.url = bs.url
      this.webSocketPath = bs.webSocketPath
      this.remote = Folder.create('Remote storage', 'cloud', true)
      this.remote.id = this.id
    }

    settings.onChange.pipe(filter((properties) => properties.includes(EProperties.profile))).subscribe(() => this.updateStatus())
  }

  async fetchDocs(): Promise<string[]> {
    if (this.url && this.status !== BotStorageService.NOT_AUTHORIZED) {
      return (await new Promise((resolve) => {
        this.http.get(`${this.httpURL}/docs/${this.settings.profile.login}`).subscribe(
          (keys) => resolve(keys),
          (err) => {
            log.warn('Could not retreive documents keys from the bot storage')
            super.setStatus(BotStorageService.NOT_RESPONDING)
            resolve([])
          }
        )
      })) as string[]
    }
    return []
  }

  async remove(doc: Doc): Promise<void> {
    return (await new Promise((resolve) => {
      this.http
        .post<{ key: string; login: string }>(`${this.httpURL}/remove`, {
          key: doc.key,
          login: this.settings.profile.login,
        })
        .subscribe(() => resolve())
    })) as Promise<void>
  }

  get login() {
    return this.extractHostname(this.url)
  }

  get httpURL() {
    const scheme = this.secure ? 'https' : 'http'
    return `${scheme}://${this.url}`
  }

  get wsURL() {
    const scheme = this.secure ? 'wss' : 'ws'
    if (this.webSocketPath && this.webSocketPath !== '') {
      return `${scheme}://${this.url}/${this.webSocketPath}`
    } else {
      return `${scheme}://${this.url}`
    }
  }

  get id() {
    return `${this.url}`
  }

  private updateStatus(): Promise<void> {
    if (this.url) {
      if (!this.settings.isAuthenticated() && !(environment.botStorage as any).isAnonymousAllowed) {
        super.setStatus(BotStorageService.NOT_AUTHORIZED)
      } else {
        return new Promise((resolve) => {
          this.http.get(`${this.httpURL}/name`, { responseType: 'text' }).subscribe(
            (name: string) => {
              this.name = name
              super.setStatus(BotStorageService.AVAILABLE)
              resolve()
            },
            (err) => {
              super.setStatus(BotStorageService.NOT_RESPONDING)
              resolve()
            }
{'fraction_non_alphanumeric': 0.1488329796908154, 'fraction_numerical': 0.0006062443164595332, 'mean_word_length': 2.59814612868048, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, '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': '667793', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1922, 'n_words': 441}