You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.6 KiB

  1. const _ = require('lodash')
  2. const graphHelper = require('../../helpers/graph')
  3. /* global WIKI */
  4. module.exports = {
  5. Query: {
  6. async search() { return {} }
  7. },
  8. Mutation: {
  9. async search() { return {} }
  10. },
  11. SearchQuery: {
  12. async searchEngines(obj, args, context, info) {
  13. let searchEngines = await WIKI.models.searchEngines.getSearchEngines()
  14. searchEngines = searchEngines.map(searchEngine => {
  15. const searchEngineInfo = _.find(WIKI.data.searchEngines, ['key', searchEngine.key]) || {}
  16. return {
  17. ...searchEngineInfo,
  18. ...searchEngine,
  19. config: _.sortBy(_.transform(searchEngine.config, (res, value, key) => {
  20. const configData = _.get(searchEngineInfo.props, key, {})
  21. res.push({
  22. key,
  23. value: JSON.stringify({
  24. ...configData,
  25. value
  26. })
  27. })
  28. }, []), 'key')
  29. }
  30. })
  31. if (args.filter) { searchEngines = graphHelper.filter(searchEngines, args.filter) }
  32. if (args.orderBy) { searchEngines = graphHelper.orderBy(searchEngines, args.orderBy) }
  33. return searchEngines
  34. }
  35. },
  36. SearchMutation: {
  37. async updateSearchEngines(obj, args, context) {
  38. try {
  39. let newActiveEngine = ''
  40. for (let searchEngine of args.engines) {
  41. if (searchEngine.isEnabled) {
  42. newActiveEngine = searchEngine.key
  43. }
  44. await WIKI.models.searchEngines.query().patch({
  45. isEnabled: searchEngine.isEnabled,
  46. config: _.reduce(searchEngine.config, (result, value, key) => {
  47. _.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
  48. return result
  49. }, {})
  50. }).where('key', searchEngine.key)
  51. }
  52. if (newActiveEngine !== WIKI.data.searchEngine.key) {
  53. try {
  54. await WIKI.data.searchEngine.deactivate()
  55. } catch (err) {
  56. WIKI.logger.warn('Failed to deactivate previous search engine:', err)
  57. }
  58. }
  59. await WIKI.models.searchEngines.initEngine({ activate: true })
  60. return {
  61. responseResult: graphHelper.generateSuccess('Search Engines updated successfully')
  62. }
  63. } catch (err) {
  64. return graphHelper.generateError(err)
  65. }
  66. },
  67. async rebuildIndex (obj, args, context) {
  68. try {
  69. await WIKI.data.searchEngine.rebuild()
  70. return {
  71. responseResult: graphHelper.generateSuccess('Index rebuilt successfully')
  72. }
  73. } catch (err) {
  74. return graphHelper.generateError(err)
  75. }
  76. }
  77. }
  78. }