<commit_before>import { getHandler } from '@xen-orchestra/fs'
import { noSuchObject } from 'xo-common/api-errors'
import { ignoreErrors } from 'promise-toolbox'

import { mapToArray } from '../utils'
import { Remotes } from '../models/remote'

// ===================================================================

export default class {
  constructor (xo) {
    this._remotes = new Remotes({
      connection: xo._redis,
      prefix: 'xo:remote',
      indexes: ['enabled'],
    })

    xo.on('clean', () => this._remotes.rebuildIndexes())
    xo.on('start', async () => {
      xo.addConfigManager(
        'remotes',
        () => this._remotes.get(),
        remotes =>
          Promise.all(
            mapToArray(remotes, remote => this._remotes.update(remote))
          )
      )

      const remotes = await this.getAllRemotes()
      remotes.forEach(remote => {
        ignoreErrors.call(this.updateRemote(remote.id, {}))
      })
    })
    xo.on('stop', async () => {
      const remotes = await this.getAllRemotes()
      for (const remote of remotes) {
        try {
          ;(await this.getRemoteHandler(remote, true)).forget()
        } catch (_) {}
      }
    })
  }

  async getRemoteHandler (remote, ignoreDisabled) {
    if (typeof remote === 'string') {
      remote = await this.getRemote(remote)
    }

    if (!(ignoreDisabled || remote.enabled)) {
      throw new Error('remote is disabled')
    }

    return getHandler(remote)
  }

  async testRemote (remote) {
    const handler = await this.getRemoteHandler(remote, true)
    return handler.test()
  }

  async getAllRemotes () {
    return this._remotes.get()
  }

  async _getRemote (id) {
    const remote = await this._remotes.first(id)
    if (!remote) {
      throw noSuchObject(id, 'remote')
    }

    return remote
  }

  async getRemote (id) {
    return (await this._getRemote(id)).properties
  }

  async createRemote ({ name, url }) {
    const remote = await this._remotes.create(name, url)
    return /* await */ this.updateRemote(remote.get('id'), { enabled: true })
  }

  async updateRemote (id, { name, url, enabled, error }) {
    const remote = await this._getRemote(id)
    this._updateRemote(remote, { name, url, enabled, error })
    const handler = await this.getRemoteHandler(remote.properties, true)
    const props = await handler.sync()
    this._updateRemote(remote, props)
    return (await this._remotes.update(remote)).properties
  }

  _updateRemote (remote, { name, url, enabled, error }) {
    if (name) remote.set('name', name)
    if (url) remote.set('url', url)
    if (enabled !== undefined) remote.set('enabled', enabled)
    if (error) {
      remote.set('error', error)
    } else {
      remote.set('error', '')
    }
  }

  async removeRemote (id) {
    const handler = await this.getRemoteHandler(id, true)
    await handler.forget()
    await this._remotes.remove(id)
  }<commit_msg>chore(xo-server/updateRemote): remove unused error param<commit_after>import { getHandler } from '@xen-orchestra/fs'
import { noSuchObject } from 'xo-common/api-errors'
import { ignoreErrors } from 'promise-toolbox'

import { mapToArray } from '../utils'
import { Remotes } from '../models/remote'

// ===================================================================

export default class {
  constructor (xo) {
    this._remotes = new Remotes({
      connection: xo._redis,
      prefix: 'xo:remote',
      indexes: ['enabled'],
    })

    xo.on('clean', () => this._remotes.rebuildIndexes())
    xo.on('start', async () => {
      xo.addConfigManager(
        'remotes',
        () => this._remotes.get(),
        remotes =>
          Promise.all(
            mapToArray(remotes, remote => this._remotes.update(remote))
          )
      )

      const remotes = await this.getAllRemotes()
      remotes.forEach(remote => {
        ignoreErrors.call(this.updateRemote(remote.id, {}))
      })
    })
    xo.on('stop', async () => {
      const remotes = await this.getAllRemotes()
      for (const remote of remotes) {
        try {
          ;(await this.getRemoteHandler(remote, true)).forget()
        } catch (_) {}
      }
    })
  }

  async getRemoteHandler (remote, ignoreDisabled) {
    if (typeof remote === 'string') {
      remote = await this.getRemote(remote)
    }

    if (!(ignoreDisabled || remote.enabled)) {
      throw new Error('remote is disabled')
    }

    return getHandler(remote)
  }

  async testRemote (remote) {
    const handler = await this.getRemoteHandler(remote, true)
    return handler.test()
  }

  async getAllRemotes () {
    return this._remotes.get()
  }

  async _getRemote (id) {
    const remote = await this._remotes.first(id)
    if (!remote) {
      throw noSuchObject(id, 'remote')
    }

    return remote
  }

  async getRemote (id) {
    return (await this._getRemote(id)).properties
  }

  async createRemote ({ name, url }) {
    const remote = await this._remotes.create(name, url)
    return /* await */ this.updateRemote(remote.get('id'), { enabled: true })
  }

  async updateRemote (id, { name, url, enabled }) {
    const remote = await this._getRemote(id)
    this._updateRemote(remote, { name, url, enabled })
    const handler = await this.getRemoteHandler(remote.properties, true)
    const props = await handler.sync()
    this._updateRemote(remote, props)
    return (await this._remotes.update(remote)).properties
  }

  _updateRemote (remote, { name, url, enabled, error }) {
    if (name) remote.set('name', name)
    if (url) remote.set('url', url)
    if (enabled !== undefined) remote.set('enabled', enabled)
    if (error) {
      remote.set('error', error)
    } else {
      remote.set('error', '')
    }
  }

  async removeRemote (id) {
    const handler = await this.getRemoteHandler(id, true)
    await handler.forget()
    await this._remotes.remove(id)
  }
{'fraction_non_alphanumeric': 0.16163167776071002, 'fraction_numerical': 0.0, 'mean_word_length': 2.8325703073904513, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '4928789', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1777, 'n_words': 410}