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.

280 lines
7.1 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. const _ = require('lodash')
  2. const graphHelper = require('../../helpers/graph')
  3. /* global WIKI */
  4. module.exports = {
  5. Query: {
  6. async pages() { return {} }
  7. },
  8. Mutation: {
  9. async pages() { return {} }
  10. },
  11. PageQuery: {
  12. /**
  13. * PAGE HISTORY
  14. */
  15. async history(obj, args, context, info) {
  16. return WIKI.models.pageHistory.getHistory({
  17. pageId: args.id,
  18. offsetPage: args.offsetPage || 0,
  19. offsetSize: args.offsetSize || 100
  20. })
  21. },
  22. /**
  23. * SEARCH PAGES
  24. */
  25. async search (obj, args, context) {
  26. if (WIKI.data.searchEngine) {
  27. const resp = await WIKI.data.searchEngine.query(args.query, args)
  28. return {
  29. ...resp,
  30. results: _.filter(resp.results, r => {
  31. return WIKI.auth.checkAccess(context.req.user, ['read:pages'], {
  32. path: r.path,
  33. locale: r.locale
  34. })
  35. })
  36. }
  37. } else {
  38. return {
  39. results: [],
  40. suggestions: [],
  41. totalHits: 0
  42. }
  43. }
  44. },
  45. /**
  46. * LIST PAGES
  47. */
  48. async list (obj, args, context, info) {
  49. let results = await WIKI.models.pages.query().column([
  50. 'pages.id',
  51. 'path',
  52. { locale: 'localeCode' },
  53. 'title',
  54. 'description',
  55. 'isPublished',
  56. 'isPrivate',
  57. 'privateNS',
  58. 'contentType',
  59. 'createdAt',
  60. 'updatedAt'
  61. ])
  62. .eagerAlgorithm(WIKI.models.Objection.Model.JoinEagerAlgorithm)
  63. .eager('tags(selectTags)', {
  64. selectTags: builder => {
  65. builder.select('tag')
  66. }
  67. })
  68. .modify(queryBuilder => {
  69. if (args.limit) {
  70. queryBuilder.limit(args.limit)
  71. }
  72. if (args.locale) {
  73. queryBuilder.where('localeCode', args.locale)
  74. }
  75. if (args.tags && args.tags.length > 0) {
  76. queryBuilder.whereIn('tags.tag', args.tags)
  77. }
  78. const orderDir = args.orderByDirection === 'DESC' ? 'desc' : 'asc'
  79. switch (args.orderBy) {
  80. case 'CREATED':
  81. queryBuilder.orderBy('createdAt', orderDir)
  82. break
  83. case 'PATH':
  84. queryBuilder.orderBy('path', orderDir)
  85. break
  86. case 'TITLE':
  87. queryBuilder.orderBy('title', orderDir)
  88. break
  89. case 'UPDATED':
  90. queryBuilder.orderBy('updatedAt', orderDir)
  91. break
  92. default:
  93. queryBuilder.orderBy('pages.id', orderDir)
  94. break
  95. }
  96. })
  97. results = _.filter(results, r => {
  98. return WIKI.auth.checkAccess(context.req.user, ['read:pages'], {
  99. path: r.path,
  100. locale: r.locale
  101. })
  102. }).map(r => ({
  103. ...r,
  104. tags: _.map(r.tags, 'tag')
  105. }))
  106. if (args.tags && args.tags.length > 0) {
  107. results = _.filter(results, r => _.every(args.tags, t => _.includes(r.tags, t)))
  108. }
  109. return results
  110. },
  111. /**
  112. * FETCH SINGLE PAGE
  113. */
  114. async single (obj, args, context, info) {
  115. let page = await WIKI.models.pages.getPageFromDb(args.id)
  116. if (page) {
  117. return {
  118. ...page,
  119. locale: page.localeCode,
  120. editor: page.editorKey
  121. }
  122. } else {
  123. throw new WIKI.Error.PageNotFound()
  124. }
  125. },
  126. /**
  127. * FETCH TAGS
  128. */
  129. async tags (obj, args, context, info) {
  130. return WIKI.models.tags.query().orderBy('tag', 'asc')
  131. },
  132. /**
  133. * FETCH PAGE TREE
  134. */
  135. async tree (obj, args, context, info) {
  136. let results = []
  137. let conds = {
  138. localeCode: args.locale,
  139. parent: (args.parent < 1) ? null : args.parent
  140. }
  141. switch (args.mode) {
  142. case 'FOLDERS':
  143. conds.isFolder = true
  144. results = await WIKI.models.knex('pageTree').where(conds)
  145. break
  146. case 'PAGES':
  147. await WIKI.models.knex('pageTree').where(conds).andWhereNotNull('pageId')
  148. break
  149. default:
  150. results = await WIKI.models.knex('pageTree').where(conds)
  151. break
  152. }
  153. return results.filter(r => {
  154. return WIKI.auth.checkAccess(context.req.user, ['read:pages'], {
  155. path: r.path,
  156. locale: r.localeCode
  157. })
  158. }).map(r => ({
  159. ...r,
  160. parent: r.parent || 0,
  161. locale: r.localeCode
  162. }))
  163. }
  164. },
  165. PageMutation: {
  166. /**
  167. * CREATE PAGE
  168. */
  169. async create(obj, args, context) {
  170. try {
  171. const page = await WIKI.models.pages.createPage({
  172. ...args,
  173. user: context.req.user
  174. })
  175. return {
  176. responseResult: graphHelper.generateSuccess('Page created successfully.'),
  177. page
  178. }
  179. } catch (err) {
  180. return graphHelper.generateError(err)
  181. }
  182. },
  183. /**
  184. * UPDATE PAGE
  185. */
  186. async update(obj, args, context) {
  187. try {
  188. const page = await WIKI.models.pages.updatePage({
  189. ...args,
  190. user: context.req.user
  191. })
  192. return {
  193. responseResult: graphHelper.generateSuccess('Page has been updated.'),
  194. page
  195. }
  196. } catch (err) {
  197. return graphHelper.generateError(err)
  198. }
  199. },
  200. /**
  201. * MOVE PAGE
  202. */
  203. async move(obj, args, context) {
  204. try {
  205. await WIKI.models.pages.movePage({
  206. ...args,
  207. user: context.req.user
  208. })
  209. return {
  210. responseResult: graphHelper.generateSuccess('Page has been moved.')
  211. }
  212. } catch (err) {
  213. return graphHelper.generateError(err)
  214. }
  215. },
  216. /**
  217. * DELETE PAGE
  218. */
  219. async delete(obj, args, context) {
  220. try {
  221. await WIKI.models.pages.deletePage({
  222. ...args,
  223. user: context.req.user
  224. })
  225. return {
  226. responseResult: graphHelper.generateSuccess('Page has been deleted.')
  227. }
  228. } catch (err) {
  229. return graphHelper.generateError(err)
  230. }
  231. },
  232. /**
  233. * FLUSH PAGE CACHE
  234. */
  235. async flushCache(obj, args, context) {
  236. try {
  237. await WIKI.models.pages.flushCache()
  238. return {
  239. responseResult: graphHelper.generateSuccess('Pages Cache has been flushed successfully.')
  240. }
  241. } catch (err) {
  242. return graphHelper.generateError(err)
  243. }
  244. },
  245. /**
  246. * MIGRATE ALL PAGES FROM SOURCE LOCALE TO TARGET LOCALE
  247. */
  248. async migrateToLocale(obj, args, context) {
  249. try {
  250. const count = await WIKI.models.pages.migrateToLocale(args)
  251. return {
  252. responseResult: graphHelper.generateSuccess('Migrated content to target locale successfully.'),
  253. count
  254. }
  255. } catch (err) {
  256. return graphHelper.generateError(err)
  257. }
  258. },
  259. /**
  260. * REBUILD TREE
  261. */
  262. async rebuildTree(obj, args, context) {
  263. try {
  264. await WIKI.models.pages.rebuildTree()
  265. return {
  266. responseResult: graphHelper.generateSuccess('Page tree rebuilt successfully.')
  267. }
  268. } catch (err) {
  269. return graphHelper.generateError(err)
  270. }
  271. }
  272. },
  273. Page: {
  274. // comments(pg) {
  275. // return pg.$relatedQuery('comments')
  276. // }
  277. }
  278. }