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.

82 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, false)
  21. if (configData) {
  22. res.push({
  23. key,
  24. value: JSON.stringify({
  25. ...configData,
  26. value
  27. })
  28. })
  29. }
  30. }, []), 'key')
  31. }
  32. })
  33. // if (args.filter) { searchEngines = graphHelper.filter(searchEngines, args.filter) }
  34. if (args.orderBy) { searchEngines = _.sortBy(searchEngines, [args.orderBy]) }
  35. return searchEngines
  36. }
  37. },
  38. SearchMutation: {
  39. async updateSearchEngines(obj, args, context) {
  40. try {
  41. let newActiveEngine = ''
  42. for (let searchEngine of args.engines) {
  43. if (searchEngine.isEnabled) {
  44. newActiveEngine = searchEngine.key
  45. }
  46. await WIKI.models.searchEngines.query().patch({
  47. isEnabled: searchEngine.isEnabled,
  48. config: _.reduce(searchEngine.config, (result, value, key) => {
  49. _.set(result, `${value.key}`, _.get(JSON.parse(value.value), 'v', null))
  50. return result
  51. }, {})
  52. }).where('key', searchEngine.key)
  53. }
  54. if (newActiveEngine !== WIKI.data.searchEngine.key) {
  55. try {
  56. await WIKI.data.searchEngine.deactivate()
  57. } catch (err) {
  58. WIKI.logger.warn('Failed to deactivate previous search engine:', err)
  59. }
  60. }
  61. await WIKI.models.searchEngines.initEngine({ activate: true })
  62. return {
  63. responseResult: graphHelper.generateSuccess('Search Engines updated successfully')
  64. }
  65. } catch (err) {
  66. return graphHelper.generateError(err)
  67. }
  68. },
  69. async rebuildIndex (obj, args, context) {
  70. try {
  71. await WIKI.data.searchEngine.rebuild()
  72. return {
  73. responseResult: graphHelper.generateSuccess('Index rebuilt successfully')
  74. }
  75. } catch (err) {
  76. return graphHelper.generateError(err)
  77. }
  78. }
  79. }
  80. }