Jelajahi Sumber

feat: GraphQL setting, right, folder resolvers

NGPixel 7 tahun lalu
induk
melakukan
574e4b97f4

+ 2 - 0
server/models/_relations.js

@@ -7,7 +7,9 @@ module.exports = db => {
   db.User.belongsToMany(db.Group, { through: 'userGroups' })
   db.Group.belongsToMany(db.User, { through: 'userGroups' })
   db.Group.hasMany(db.Right)
+  db.Right.belongsTo(db.Group)
   db.Document.belongsToMany(db.Tag, { through: 'documentTags' })
+  db.Document.hasMany(db.Comment)
   db.Tag.belongsToMany(db.Document, { through: 'documentTags' })
   db.File.belongsTo(db.Folder)
   db.Folder.hasMany(db.File)

+ 5 - 0
server/models/document.js

@@ -41,6 +41,11 @@ module.exports = (sequelize, DataTypes) => {
       allowNull: false,
       defaultValue: false
     },
+    isDraft: {
+      type: DataTypes.BOOLEAN,
+      allowNull: false,
+      defaultValue: false
+    },
     searchContent: {
       type: DataTypes.TEXT,
       allowNull: true,

+ 4 - 0
server/modules/graphql.js

@@ -10,16 +10,20 @@ const _ = require('lodash')
 const typeDefs = fs.readFileSync(path.join(wiki.SERVERPATH, 'schemas/types.graphql'), 'utf8')
 
 const DateScalar = require('../schemas/scalar-date')
+const CommentResolvers = require('../schemas/resolvers-comment')
 const DocumentResolvers = require('../schemas/resolvers-document')
 const FolderResolvers = require('../schemas/resolvers-folder')
 const GroupResolvers = require('../schemas/resolvers-group')
+const SettingResolvers = require('../schemas/resolvers-setting')
 const TagResolvers = require('../schemas/resolvers-tag')
 const UserResolvers = require('../schemas/resolvers-user')
 
 const resolvers = _.merge(
+  CommentResolvers,
   DocumentResolvers,
   FolderResolvers,
   GroupResolvers,
+  SettingResolvers,
   TagResolvers,
   UserResolvers,
   DateScalar

+ 43 - 0
server/schemas/resolvers-comment.js

@@ -0,0 +1,43 @@
+'use strict'
+
+/* global wiki */
+
+module.exports = {
+  Query: {
+    comments(obj, args, context, info) {
+      return wiki.db.Comment.findAll({ where: args })
+    }
+  },
+  Mutation: {
+    createComment(obj, args) {
+      return wiki.db.Comment.create({
+        content: args.content,
+        author: args.userId,
+        document: args.documentId
+      })
+    },
+    deleteComment(obj, args) {
+      return wiki.db.Comment.destroy({
+        where: {
+          id: args.id
+        },
+        limit: 1
+      })
+    },
+    modifyComment(obj, args) {
+      return wiki.db.Comment.update({
+        content: args.content
+      }, {
+        where: { id: args.id }
+      })
+    }
+  },
+  Comment: {
+    author(cm) {
+      return cm.getAuthor()
+    },
+    document(cm) {
+      return cm.getDocument()
+    }
+  }
+}

+ 18 - 0
server/schemas/resolvers-document.js

@@ -19,9 +19,27 @@ module.exports = {
         },
         limit: 1
       })
+    },
+    modifyDocument(obj, args) {
+      return wiki.db.Document.update({
+        title: args.title,
+        subtitle: args.subtitle
+      }, {
+        where: { id: args.id }
+      })
+    },
+    moveDocument(obj, args) {
+      return wiki.db.Document.update({
+        path: args.path
+      }, {
+        where: { id: args.id }
+      })
     }
   },
   Document: {
+    comments(doc) {
+      return doc.getComments()
+    },
     tags(doc) {
       return doc.getTags()
     }

+ 7 - 0
server/schemas/resolvers-folder.js

@@ -19,6 +19,13 @@ module.exports = {
         },
         limit: 1
       })
+    },
+    renameFolder(obj, args) {
+      return wiki.db.Folder.update({
+        name: args.name
+      }, {
+        where: { id: args.id }
+      })
     }
   },
   Folder: {

+ 7 - 0
server/schemas/resolvers-group.js

@@ -47,6 +47,13 @@ module.exports = {
           return grp.removeUser(usr)
         })
       })
+    },
+    renameGroup(obj, args) {
+      return wiki.db.Group.update({
+        name: args.name
+      }, {
+        where: { id: args.id }
+      })
     }
   },
   Group: {

+ 54 - 0
server/schemas/resolvers-right.js

@@ -0,0 +1,54 @@
+'use strict'
+
+/* global wiki */
+
+const gql = require('graphql')
+
+module.exports = {
+  Query: {
+    rights(obj, args, context, info) {
+      return wiki.db.Right.findAll({ where: args })
+    }
+  },
+  Mutation: {
+    addRightToGroup(obj, args) {
+      return wiki.db.Group.findById(args.groupId).then(grp => {
+        if (!grp) {
+          throw new gql.GraphQLError('Invalid Group ID')
+        }
+        return wiki.db.Right.create({
+          path: args.path,
+          role: args.role,
+          exact: args.exact,
+          allow: args.allow,
+          group: grp
+        })
+      })
+    },
+    removeRightFromGroup(obj, args) {
+      return wiki.db.Right.destroy({
+        where: {
+          id: args.rightId
+        },
+        limit: 1
+      })
+    },
+    modifyRight(obj, args) {
+      return wiki.db.Right.update({
+        path: args.path,
+        role: args.role,
+        exact: args.exact,
+        allow: args.allow
+      }, {
+        where: {
+          id: args.id
+        }
+      })
+    }
+  },
+  Right: {
+    group(rt) {
+      return rt.getGroup()
+    }
+  }
+}

+ 25 - 0
server/schemas/resolvers-setting.js

@@ -0,0 +1,25 @@
+'use strict'
+
+/* global wiki */
+
+const _ = require('lodash')
+
+module.exports = {
+  Query: {
+    settings(obj, args, context, info) {
+      return wiki.db.Setting.findAll({ where: args, raw: true }).then(entries => {
+        return _.map(entries, entry => {
+          entry.config = JSON.stringify(entry.config)
+          return entry
+        })
+      })
+    }
+  },
+  Mutation: {
+    setConfigEntry(obj, args) {
+      return wiki.db.Setting.update({
+        value: args.value
+      }, { where: { key: args.key } })
+    }
+  }
+}

+ 7 - 0
server/schemas/resolvers-tag.js

@@ -47,6 +47,13 @@ module.exports = {
           return tag.removeDocument(doc)
         })
       })
+    },
+    renameTag(obj, args) {
+      return wiki.db.Group.update({
+        key: args.key
+      }, {
+        where: { id: args.id }
+      })
     }
   },
   Tag: {

+ 17 - 0
server/schemas/resolvers-user.js

@@ -19,6 +19,23 @@ module.exports = {
         },
         limit: 1
       })
+    },
+    modifyUser(obj, args) {
+      return wiki.db.User.update({
+        email: args.email,
+        name: args.name,
+        provider: args.provider,
+        providerId: args.providerId,
+        role: args.role
+      }, {
+        where: { id: args.id }
+      })
+    },
+    resetUserPassword(obj, args) {
+      return false
+    },
+    setUserPassword(obj, args) {
+      return false
     }
   },
   User: {

+ 43 - 1
server/schemas/types.graphql

@@ -52,6 +52,7 @@ type Document implements Base {
   isDirectory: Boolean!
   isEntry: Boolean!
   searchContent: String
+  comments: [Comment]
   tags: [Tag]
 }
 
@@ -93,6 +94,7 @@ type Right implements Base {
   role: RightRole!
   exact: Boolean!
   allow: Boolean!
+  group: Group!
 }
 
 type Setting implements Base {
@@ -145,6 +147,14 @@ type Query {
 
 # Mutations (Create, Update, Delete)
 type Mutation {
+  addRightToGroup(
+    groupId: Int!
+    path: String!
+    role: RightRole!
+    exact: Boolean!
+    allow: Boolean!
+  ): Right
+
   assignTagToDocument(
     tagId: Int!
     documentId: Int!
@@ -155,6 +165,12 @@ type Mutation {
     groupId: Int!
   ): OperationResult
 
+  createComment(
+    userId: Int!
+    documentId: Int!
+    content: String!
+  ): Comment
+
   createDocument(
     path: String!
     title: String!
@@ -182,6 +198,10 @@ type Mutation {
     role: UserRole!
   ): User
 
+  deleteComment(
+    id: Int!
+  ): OperationResult
+
   deleteDocument(
     id: Int!
   ): OperationResult
@@ -202,6 +222,11 @@ type Mutation {
     id: Int!
   ): OperationResult
 
+  modifyComment(
+    id: Int!
+    content: String!
+  ): Document
+
   modifyDocument(
     id: Int!
     title: String
@@ -217,6 +242,14 @@ type Mutation {
     role: UserRole
   ): User
 
+  modifyRight(
+    id: Int!
+    path: String
+    role: RightRole
+    exact: Boolean
+    allow: Boolean
+  ): Right
+
   moveDocument(
     id: Int!
     path: String!
@@ -234,7 +267,7 @@ type Mutation {
 
   renameTag(
     id: Int!
-    name: String!
+    key: String!
   ): OperationResult
 
   removeTagFromDocument(
@@ -242,6 +275,10 @@ type Mutation {
     documentId: Int!
   ): OperationResult
 
+  removeRightFromGroup(
+    rightId: Int!
+  ): OperationResult
+
   removeUserFromGroup(
     userId: Int!
     groupId: Int!
@@ -251,6 +288,11 @@ type Mutation {
     id: Int!
   ): OperationResult
 
+  setConfigEntry(
+    key: String!
+    value: String!
+  ): OperationResult
+
   setUserPassword(
     id: Int!
     passwordRaw: String!