You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@answer.apache.org by li...@apache.org on 2024/01/02 07:27:22 UTC
(incubator-answer) 01/06: feat(notification): add user config plugin API
This is an automated email from the ASF dual-hosted git repository.
linkinstar pushed a commit to branch feat/1.2.5/notification
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
commit 645df7f2a611b6a294101da1eb9431f4b692fd66
Author: LinkinStars <li...@foxmail.com>
AuthorDate: Tue Dec 26 15:27:04 2023 +0800
feat(notification): add user config plugin API
---
cmd/wire_gen.go | 30 +---
docs/docs.go | 172 ++++++++++++++++++---
docs/swagger.json | 153 ++++++++++++++++++
docs/swagger.yaml | 108 +++++++++++--
internal/controller/controller.go | 1 +
internal/controller/user_plugin_controller.go | 144 +++++++++++++++++
internal/controller_admin/plugin_controller.go | 10 +-
.../plugin_user_config_entity.go} | 40 ++---
internal/migrations/init_data.go | 1 +
internal/migrations/migrations.go | 1 +
.../controller.go => migrations/v19.go} | 37 ++---
internal/repo/plugin_config/plugin_config_repo.go | 2 +-
.../repo/plugin_config/plugin_user_config_repo.go | 88 +++++++++++
internal/repo/provider.go | 1 +
internal/router/answer_api_router.go | 7 +
internal/schema/plugin_user_schema.go | 100 ++++++++++++
.../service/plugin_common/plugin_common_service.go | 62 ++++++--
plugin/plugin.go | 4 +
plugin/user_config.go | 52 +++++++
19 files changed, 882 insertions(+), 131 deletions(-)
diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go
index a057660c..118cb866 100644
--- a/cmd/wire_gen.go
+++ b/cmd/wire_gen.go
@@ -1,28 +1,8 @@
-//go:build !wireinject
-// +build !wireinject
-
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
// Code generated by Wire. DO NOT EDIT.
//go:generate go run github.com/google/wire/cmd/wire
+//go:build !wireinject
+// +build !wireinject
package answercmd
@@ -241,10 +221,12 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
activityController := controller.NewActivityController(activityService)
roleController := controller_admin.NewRoleController(roleService)
pluginConfigRepo := plugin_config.NewPluginConfigRepo(dataData)
- pluginCommonService := plugin_common.NewPluginCommonService(pluginConfigRepo, configService, dataData)
+ pluginUserConfigRepo := plugin_config.NewPluginUserConfigRepo(dataData)
+ pluginCommonService := plugin_common.NewPluginCommonService(pluginConfigRepo, pluginUserConfigRepo, configService, dataData)
pluginController := controller_admin.NewPluginController(pluginCommonService)
permissionController := controller.NewPermissionController(rankService)
- answerAPIRouter := router.NewAnswerAPIRouter(langController, userController, commentController, reportController, voteController, tagController, followController, collectionController, questionController, answerController, searchController, revisionController, rankController, controller_adminReportController, userAdminController, reasonController, themeController, siteInfoController, controllerSiteInfoController, notificationController, dashboardController, uploadController, activityCon [...]
+ userPluginController := controller.NewUserPluginController(pluginCommonService)
+ answerAPIRouter := router.NewAnswerAPIRouter(langController, userController, commentController, reportController, voteController, tagController, followController, collectionController, questionController, answerController, searchController, revisionController, rankController, controller_adminReportController, userAdminController, reasonController, themeController, siteInfoController, controllerSiteInfoController, notificationController, dashboardController, uploadController, activityCon [...]
swaggerRouter := router.NewSwaggerRouter(swaggerConf)
uiRouter := router.NewUIRouter(controllerSiteInfoController, siteInfoCommonService)
authUserMiddleware := middleware.NewAuthUserMiddleware(authService, siteInfoCommonService)
diff --git a/docs/docs.go b/docs/docs.go
index 7a6d08bd..a6dbd0ed 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -1,22 +1,3 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
// Code generated by swaggo/swag. DO NOT EDIT.
package docs
@@ -5895,6 +5876,132 @@ const docTemplate = `{
}
}
},
+ "/answer/api/v1/user/plugin/config": {
+ "get": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "get user plugin config",
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "UserPlugin"
+ ],
+ "summary": "get user plugin config",
+ "parameters": [
+ {
+ "type": "string",
+ "description": "plugin_slug_name",
+ "name": "plugin_slug_name",
+ "in": "query",
+ "required": true
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "allOf": [
+ {
+ "$ref": "#/definitions/handler.RespBody"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "data": {
+ "$ref": "#/definitions/schema.GetPluginConfigResp"
+ }
+ }
+ }
+ ]
+ }
+ }
+ }
+ },
+ "put": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "update user plugin config",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "UserPlugin"
+ ],
+ "summary": "update user plugin config",
+ "parameters": [
+ {
+ "description": "UpdatePluginConfigReq",
+ "name": "data",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/schema.UpdateUserPluginConfigReq"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/handler.RespBody"
+ }
+ }
+ }
+ }
+ },
+ "/answer/api/v1/user/plugin/configs": {
+ "get": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "get plugin list that used for user.",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "UserPlugin"
+ ],
+ "summary": "get plugin list that used for user.",
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "allOf": [
+ {
+ "$ref": "#/definitions/handler.RespBody"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "data": {
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/schema.GetUserPluginListResp"
+ }
+ }
+ }
+ }
+ ]
+ }
+ }
+ }
+ }
+ },
"/answer/api/v1/user/ranking": {
"get": {
"security": [
@@ -7894,6 +8001,17 @@ const docTemplate = `{
}
}
},
+ "schema.GetUserPluginListResp": {
+ "type": "object",
+ "properties": {
+ "name": {
+ "type": "string"
+ },
+ "slug_name": {
+ "type": "string"
+ }
+ }
+ },
"schema.GetVoteWithPageResp": {
"type": "object",
"properties": {
@@ -9464,6 +9582,22 @@ const docTemplate = `{
}
}
},
+ "schema.UpdateUserPluginConfigReq": {
+ "type": "object",
+ "required": [
+ "plugin_slug_name"
+ ],
+ "properties": {
+ "config_fields": {
+ "type": "object",
+ "additionalProperties": {}
+ },
+ "plugin_slug_name": {
+ "type": "string",
+ "maxLength": 100
+ }
+ }
+ },
"schema.UpdateUserRoleReq": {
"type": "object",
"required": [
diff --git a/docs/swagger.json b/docs/swagger.json
index 77a1ada3..4c52427b 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -5864,6 +5864,132 @@
}
}
},
+ "/answer/api/v1/user/plugin/config": {
+ "get": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "get user plugin config",
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "UserPlugin"
+ ],
+ "summary": "get user plugin config",
+ "parameters": [
+ {
+ "type": "string",
+ "description": "plugin_slug_name",
+ "name": "plugin_slug_name",
+ "in": "query",
+ "required": true
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "allOf": [
+ {
+ "$ref": "#/definitions/handler.RespBody"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "data": {
+ "$ref": "#/definitions/schema.GetPluginConfigResp"
+ }
+ }
+ }
+ ]
+ }
+ }
+ }
+ },
+ "put": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "update user plugin config",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "UserPlugin"
+ ],
+ "summary": "update user plugin config",
+ "parameters": [
+ {
+ "description": "UpdatePluginConfigReq",
+ "name": "data",
+ "in": "body",
+ "required": true,
+ "schema": {
+ "$ref": "#/definitions/schema.UpdateUserPluginConfigReq"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "$ref": "#/definitions/handler.RespBody"
+ }
+ }
+ }
+ }
+ },
+ "/answer/api/v1/user/plugin/configs": {
+ "get": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "get plugin list that used for user.",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "UserPlugin"
+ ],
+ "summary": "get plugin list that used for user.",
+ "responses": {
+ "200": {
+ "description": "OK",
+ "schema": {
+ "allOf": [
+ {
+ "$ref": "#/definitions/handler.RespBody"
+ },
+ {
+ "type": "object",
+ "properties": {
+ "data": {
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/schema.GetUserPluginListResp"
+ }
+ }
+ }
+ }
+ ]
+ }
+ }
+ }
+ }
+ },
"/answer/api/v1/user/ranking": {
"get": {
"security": [
@@ -7863,6 +7989,17 @@
}
}
},
+ "schema.GetUserPluginListResp": {
+ "type": "object",
+ "properties": {
+ "name": {
+ "type": "string"
+ },
+ "slug_name": {
+ "type": "string"
+ }
+ }
+ },
"schema.GetVoteWithPageResp": {
"type": "object",
"properties": {
@@ -9433,6 +9570,22 @@
}
}
},
+ "schema.UpdateUserPluginConfigReq": {
+ "type": "object",
+ "required": [
+ "plugin_slug_name"
+ ],
+ "properties": {
+ "config_fields": {
+ "type": "object",
+ "additionalProperties": {}
+ },
+ "plugin_slug_name": {
+ "type": "string",
+ "maxLength": 100
+ }
+ }
+ },
"schema.UpdateUserRoleReq": {
"type": "object",
"required": [
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index f3f53a76..374e53ec 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -1,20 +1,3 @@
-# Licensed to the Apache Software Foundation (ASF) under one
-# or more contributor license agreements. See the NOTICE file
-# distributed with this work for additional information
-# regarding copyright ownership. The ASF licenses this file
-# to you under the Apache License, Version 2.0 (the
-# "License"); you may not use this file except in compliance
-# with the License. You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing,
-# software distributed under the License is distributed on an
-# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-# KIND, either express or implied. See the License for the
-# specific language governing permissions and limitations
-# under the License.
-
definitions:
constant.NotificationChannelKey:
enum:
@@ -1059,6 +1042,13 @@ definitions:
description: username
type: string
type: object
+ schema.GetUserPluginListResp:
+ properties:
+ name:
+ type: string
+ slug_name:
+ type: string
+ type: object
schema.GetVoteWithPageResp:
properties:
answer_id:
@@ -2140,6 +2130,17 @@ definitions:
- password
- user_id
type: object
+ schema.UpdateUserPluginConfigReq:
+ properties:
+ config_fields:
+ additionalProperties: {}
+ type: object
+ plugin_slug_name:
+ maxLength: 100
+ type: string
+ required:
+ - plugin_slug_name
+ type: object
schema.UpdateUserRoleReq:
properties:
role_id:
@@ -6010,6 +6011,79 @@ paths:
summary: RetrievePassWord
tags:
- User
+ /answer/api/v1/user/plugin/config:
+ get:
+ description: get user plugin config
+ parameters:
+ - description: plugin_slug_name
+ in: query
+ name: plugin_slug_name
+ required: true
+ type: string
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ allOf:
+ - $ref: '#/definitions/handler.RespBody'
+ - properties:
+ data:
+ $ref: '#/definitions/schema.GetPluginConfigResp'
+ type: object
+ security:
+ - ApiKeyAuth: []
+ summary: get user plugin config
+ tags:
+ - UserPlugin
+ put:
+ consumes:
+ - application/json
+ description: update user plugin config
+ parameters:
+ - description: UpdatePluginConfigReq
+ in: body
+ name: data
+ required: true
+ schema:
+ $ref: '#/definitions/schema.UpdateUserPluginConfigReq'
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ $ref: '#/definitions/handler.RespBody'
+ security:
+ - ApiKeyAuth: []
+ summary: update user plugin config
+ tags:
+ - UserPlugin
+ /answer/api/v1/user/plugin/configs:
+ get:
+ consumes:
+ - application/json
+ description: get plugin list that used for user.
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ allOf:
+ - $ref: '#/definitions/handler.RespBody'
+ - properties:
+ data:
+ items:
+ $ref: '#/definitions/schema.GetUserPluginListResp'
+ type: array
+ type: object
+ security:
+ - ApiKeyAuth: []
+ summary: get plugin list that used for user.
+ tags:
+ - UserPlugin
/answer/api/v1/user/ranking:
get:
consumes:
diff --git a/internal/controller/controller.go b/internal/controller/controller.go
index 4e6f2564..0dd8e3ef 100644
--- a/internal/controller/controller.go
+++ b/internal/controller/controller.go
@@ -46,4 +46,5 @@ var ProviderSetController = wire.NewSet(
NewConnectorController,
NewUserCenterController,
NewPermissionController,
+ NewUserPluginController,
)
diff --git a/internal/controller/user_plugin_controller.go b/internal/controller/user_plugin_controller.go
new file mode 100644
index 00000000..90628125
--- /dev/null
+++ b/internal/controller/user_plugin_controller.go
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package controller
+
+import (
+ "encoding/json"
+ "github.com/apache/incubator-answer/internal/base/middleware"
+
+ "github.com/apache/incubator-answer/internal/base/handler"
+ "github.com/apache/incubator-answer/internal/schema"
+ "github.com/apache/incubator-answer/internal/service/plugin_common"
+ "github.com/apache/incubator-answer/plugin"
+ "github.com/gin-gonic/gin"
+)
+
+// UserPluginController role controller
+type UserPluginController struct {
+ pluginCommonService *plugin_common.PluginCommonService
+}
+
+// NewUserPluginController new controller
+func NewUserPluginController(pluginCommonService *plugin_common.PluginCommonService) *UserPluginController {
+ return &UserPluginController{pluginCommonService: pluginCommonService}
+}
+
+// GetUserPluginList get plugin list that used for user.
+// @Summary get plugin list that used for user.
+// @Description get plugin list that used for user.
+// @Tags UserPlugin
+// @Security ApiKeyAuth
+// @Accept json
+// @Produce json
+// @Success 200 {object} handler.RespBody{data=[]schema.GetUserPluginListResp}
+// @Router /answer/api/v1/user/plugin/configs [get]
+func (pc *UserPluginController) GetUserPluginList(ctx *gin.Context) {
+ resp := make([]*schema.GetUserPluginListResp, 0)
+ _ = plugin.CallUserConfig(func(base plugin.UserConfig) error {
+ info := base.Info()
+ resp = append(resp, &schema.GetUserPluginListResp{
+ Name: info.Name.Translate(ctx),
+ SlugName: info.SlugName,
+ })
+ return nil
+ })
+ handler.HandleResponse(ctx, nil, resp)
+}
+
+// GetUserPluginConfig get user plugin config
+// @Summary get user plugin config
+// @Description get user plugin config
+// @Tags UserPlugin
+// @Security ApiKeyAuth
+// @Produce json
+// @Param plugin_slug_name query string true "plugin_slug_name"
+// @Success 200 {object} handler.RespBody{data=schema.GetPluginConfigResp}
+// @Router /answer/api/v1/user/plugin/config [get]
+func (pc *UserPluginController) GetUserPluginConfig(ctx *gin.Context) {
+ req := &schema.GetUserPluginConfigReq{}
+ if handler.BindAndCheck(ctx, req) {
+ return
+ }
+
+ req.UserID = middleware.GetLoginUserIDFromContext(ctx)
+
+ resp := &schema.GetUserPluginConfigResp{}
+ _ = plugin.CallUserConfig(func(fn plugin.UserConfig) error {
+ if fn.Info().SlugName != req.PluginSlugName {
+ return nil
+ }
+ info := fn.Info()
+ resp.Name = info.Name.Translate(ctx)
+ resp.SlugName = info.SlugName
+ resp.SetConfigFields(ctx, fn.UserConfigFields())
+ return nil
+ })
+
+ configValue, err := pc.pluginCommonService.GetUserPluginConfig(ctx, req)
+ if err != nil {
+ handler.HandleResponse(ctx, err, nil)
+ return
+ }
+ if len(configValue) > 0 {
+ configValueMapping := make(map[string]any)
+ _ = json.Unmarshal([]byte(configValue), &configValueMapping)
+ for _, field := range resp.ConfigFields {
+ if value, ok := configValueMapping[field.Name]; ok {
+ field.Value = value
+ }
+ }
+ }
+
+ handler.HandleResponse(ctx, err, resp)
+}
+
+// UpdatePluginUserConfig update user plugin config
+// @Summary update user plugin config
+// @Description update user plugin config
+// @Tags UserPlugin
+// @Accept json
+// @Produce json
+// @Security ApiKeyAuth
+// @Param data body schema.UpdateUserPluginConfigReq true "UpdatePluginConfigReq"
+// @Success 200 {object} handler.RespBody
+// @Router /answer/api/v1/user/plugin/config [put]
+func (pc *UserPluginController) UpdatePluginUserConfig(ctx *gin.Context) {
+ req := &schema.UpdateUserPluginConfigReq{}
+ if handler.BindAndCheck(ctx, req) {
+ return
+ }
+
+ req.UserID = middleware.GetLoginUserIDFromContext(ctx)
+
+ configFields, _ := json.Marshal(req.ConfigFields)
+ err := plugin.CallUserConfig(func(fn plugin.UserConfig) error {
+ if fn.Info().SlugName == req.PluginSlugName {
+ return fn.UserConfigReceiver(req.UserID, configFields)
+ }
+ return nil
+ })
+ if err != nil {
+ handler.HandleResponse(ctx, err, nil)
+ return
+ }
+
+ err = pc.pluginCommonService.UpdatePluginUserConfig(ctx, req)
+ handler.HandleResponse(ctx, err, nil)
+}
diff --git a/internal/controller_admin/plugin_controller.go b/internal/controller_admin/plugin_controller.go
index 769fc3bd..bfde841d 100644
--- a/internal/controller_admin/plugin_controller.go
+++ b/internal/controller_admin/plugin_controller.go
@@ -31,12 +31,12 @@ import (
// PluginController role controller
type PluginController struct {
- PluginCommonService *plugin_common.PluginCommonService
+ pluginCommonService *plugin_common.PluginCommonService
}
// NewPluginController new controller
-func NewPluginController(PluginCommonService *plugin_common.PluginCommonService) *PluginController {
- return &PluginController{PluginCommonService: PluginCommonService}
+func NewPluginController(pluginCommonService *plugin_common.PluginCommonService) *PluginController {
+ return &PluginController{pluginCommonService: pluginCommonService}
}
// GetAllPluginStatus get all plugins status
@@ -150,7 +150,7 @@ func (pc *PluginController) UpdatePluginStatus(ctx *gin.Context) {
}
plugin.StatusManager.Enable(req.PluginSlugName, req.Enabled)
- err := pc.PluginCommonService.UpdatePluginStatus(ctx)
+ err := pc.pluginCommonService.UpdatePluginStatus(ctx)
handler.HandleResponse(ctx, err, nil)
}
@@ -220,6 +220,6 @@ func (pc *PluginController) UpdatePluginConfig(ctx *gin.Context) {
return
}
- err = pc.PluginCommonService.UpdatePluginConfig(ctx, req)
+ err = pc.pluginCommonService.UpdatePluginConfig(ctx, req)
handler.HandleResponse(ctx, err, nil)
}
diff --git a/internal/controller/controller.go b/internal/entity/plugin_user_config_entity.go
similarity index 54%
copy from internal/controller/controller.go
copy to internal/entity/plugin_user_config_entity.go
index 4e6f2564..edf93c9f 100644
--- a/internal/controller/controller.go
+++ b/internal/entity/plugin_user_config_entity.go
@@ -17,33 +17,17 @@
* under the License.
*/
-package controller
+package entity
-import "github.com/google/wire"
+// PluginUserConfig plugin config
+type PluginUserConfig struct {
+ ID int `xorm:"not null pk autoincr INT(11) id"`
+ UserID string `xorm:"not null default 0 BIGINT(20) UNIQUE(uk_up) user_id"`
+ PluginSlugName string `xorm:"VARCHAR(128) UNIQUE(uk_up) plugin_slug_name"`
+ Value string `xorm:"TEXT value"`
+}
-// ProviderSetController is controller providers.
-var ProviderSetController = wire.NewSet(
- NewLangController,
- NewCommentController,
- NewReportController,
- NewVoteController,
- NewTagController,
- NewFollowController,
- NewCollectionController,
- NewUserController,
- NewQuestionController,
- NewAnswerController,
- NewSearchController,
- NewRevisionController,
- NewRankController,
- NewReasonController,
- NewNotificationController,
- NewSiteInfoController,
- NewDashboardController,
- NewUploadController,
- NewActivityController,
- NewTemplateController,
- NewConnectorController,
- NewUserCenterController,
- NewPermissionController,
-)
+// TableName config table name
+func (PluginUserConfig) TableName() string {
+ return "plugin_user_config"
+}
diff --git a/internal/migrations/init_data.go b/internal/migrations/init_data.go
index b39ece43..494e0cf5 100644
--- a/internal/migrations/init_data.go
+++ b/internal/migrations/init_data.go
@@ -67,6 +67,7 @@ var (
&entity.PluginConfig{},
&entity.UserExternalLogin{},
&entity.UserNotificationConfig{},
+ &entity.PluginUserConfig{},
}
roles = []*entity.Role{
diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.go
index 41515383..6fa26088 100644
--- a/internal/migrations/migrations.go
+++ b/internal/migrations/migrations.go
@@ -94,6 +94,7 @@ var migrations = []Migration{
NewMigration("v1.1.3", "set default user notification config", setDefaultUserNotificationConfig, false),
NewMigration("v1.2.0", "add recover answer permission", addRecoverPermission, true),
NewMigration("v1.2.1", "add password login control", addPasswordLoginControl, true),
+ NewMigration("v1.2.5", "add notification plugin", addNotificationPlugin, true),
}
func GetMigrations() []Migration {
diff --git a/internal/controller/controller.go b/internal/migrations/v19.go
similarity index 55%
copy from internal/controller/controller.go
copy to internal/migrations/v19.go
index 4e6f2564..c1505edd 100644
--- a/internal/controller/controller.go
+++ b/internal/migrations/v19.go
@@ -17,33 +17,14 @@
* under the License.
*/
-package controller
+package migrations
-import "github.com/google/wire"
-
-// ProviderSetController is controller providers.
-var ProviderSetController = wire.NewSet(
- NewLangController,
- NewCommentController,
- NewReportController,
- NewVoteController,
- NewTagController,
- NewFollowController,
- NewCollectionController,
- NewUserController,
- NewQuestionController,
- NewAnswerController,
- NewSearchController,
- NewRevisionController,
- NewRankController,
- NewReasonController,
- NewNotificationController,
- NewSiteInfoController,
- NewDashboardController,
- NewUploadController,
- NewActivityController,
- NewTemplateController,
- NewConnectorController,
- NewUserCenterController,
- NewPermissionController,
+import (
+ "context"
+ "github.com/apache/incubator-answer/internal/entity"
+ "xorm.io/xorm"
)
+
+func addNotificationPlugin(ctx context.Context, x *xorm.Engine) error {
+ return x.Context(ctx).Sync(new(entity.PluginUserConfig))
+}
diff --git a/internal/repo/plugin_config/plugin_config_repo.go b/internal/repo/plugin_config/plugin_config_repo.go
index ddcd3ed2..1ad2e495 100644
--- a/internal/repo/plugin_config/plugin_config_repo.go
+++ b/internal/repo/plugin_config/plugin_config_repo.go
@@ -50,7 +50,7 @@ func (ur *pluginConfigRepo) SavePluginConfig(ctx context.Context, pluginSlugName
old.Value = configValue
_, err = ur.data.DB.Context(ctx).ID(old.ID).Update(old)
} else {
- _, err = ur.data.DB.Context(ctx).InsertOne(&entity.PluginConfig{PluginSlugName: pluginSlugName, Value: configValue})
+ _, err = ur.data.DB.Context(ctx).Insert(&entity.PluginConfig{PluginSlugName: pluginSlugName, Value: configValue})
}
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
diff --git a/internal/repo/plugin_config/plugin_user_config_repo.go b/internal/repo/plugin_config/plugin_user_config_repo.go
new file mode 100644
index 00000000..1f233783
--- /dev/null
+++ b/internal/repo/plugin_config/plugin_user_config_repo.go
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package plugin_config
+
+import (
+ "context"
+ "xorm.io/xorm"
+
+ "github.com/apache/incubator-answer/internal/base/data"
+ "github.com/apache/incubator-answer/internal/base/reason"
+ "github.com/apache/incubator-answer/internal/entity"
+ "github.com/apache/incubator-answer/internal/service/plugin_common"
+ "github.com/segmentfault/pacman/errors"
+)
+
+type pluginUserConfigRepo struct {
+ data *data.Data
+}
+
+// NewPluginUserConfigRepo new repository
+func NewPluginUserConfigRepo(data *data.Data) plugin_common.PluginUserConfigRepo {
+ return &pluginUserConfigRepo{
+ data: data,
+ }
+}
+
+func (ur *pluginUserConfigRepo) SaveUserPluginConfig(ctx context.Context, userID string,
+ pluginSlugName, configValue string) (err error) {
+ _, err = ur.data.DB.Transaction(func(session *xorm.Session) (interface{}, error) {
+ session = session.Context(ctx)
+ old := &entity.PluginUserConfig{
+ UserID: userID,
+ PluginSlugName: pluginSlugName,
+ }
+ exist, err := session.Get(old)
+ if err != nil {
+ return nil, err
+ }
+ if exist {
+ old.Value = configValue
+ _, err = session.ID(old.ID).Update(old)
+ } else {
+ _, err = session.Insert(&entity.PluginUserConfig{
+ UserID: userID,
+ PluginSlugName: pluginSlugName,
+ Value: configValue,
+ })
+ }
+ if err != nil {
+ return nil, err
+ }
+ return nil, nil
+ })
+ if err != nil {
+ err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ return nil
+}
+
+func (ur *pluginUserConfigRepo) GetPluginUserConfig(ctx context.Context, userID, pluginSlugName string) (
+ pluginUserConfig *entity.PluginUserConfig, exist bool, err error) {
+ pluginUserConfig = &entity.PluginUserConfig{
+ UserID: userID,
+ PluginSlugName: pluginSlugName,
+ }
+ exist, err = ur.data.DB.Context(ctx).Get(pluginUserConfig)
+ if err != nil {
+ err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ return pluginUserConfig, exist, err
+}
diff --git a/internal/repo/provider.go b/internal/repo/provider.go
index 0814487e..931ccde1 100644
--- a/internal/repo/provider.go
+++ b/internal/repo/provider.go
@@ -96,4 +96,5 @@ var ProviderSetRepo = wire.NewSet(
plugin_config.NewPluginConfigRepo,
user_notification_config.NewUserNotificationConfigRepo,
limit.NewRateLimitRepo,
+ plugin_config.NewPluginUserConfigRepo,
)
diff --git a/internal/router/answer_api_router.go b/internal/router/answer_api_router.go
index 0a6d7b83..a1a0ee86 100644
--- a/internal/router/answer_api_router.go
+++ b/internal/router/answer_api_router.go
@@ -53,6 +53,7 @@ type AnswerAPIRouter struct {
roleController *controller_admin.RoleController
pluginController *controller_admin.PluginController
permissionController *controller.PermissionController
+ userPluginController *controller.UserPluginController
}
func NewAnswerAPIRouter(
@@ -82,6 +83,7 @@ func NewAnswerAPIRouter(
roleController *controller_admin.RoleController,
pluginController *controller_admin.PluginController,
permissionController *controller.PermissionController,
+ userPluginController *controller.UserPluginController,
) *AnswerAPIRouter {
return &AnswerAPIRouter{
langController: langController,
@@ -110,6 +112,7 @@ func NewAnswerAPIRouter(
roleController: roleController,
pluginController: pluginController,
permissionController: permissionController,
+ userPluginController: userPluginController,
}
}
@@ -268,6 +271,10 @@ func (a *AnswerAPIRouter) RegisterAnswerAPIRouter(r *gin.RouterGroup) {
r.GET("/activity/timeline", a.activityController.GetObjectTimeline)
r.GET("/activity/timeline/detail", a.activityController.GetObjectTimelineDetail)
+ // plugin
+ r.GET("/user/plugin/configs", a.userPluginController.GetUserPluginList)
+ r.GET("/user/plugin/config", a.userPluginController.GetUserPluginConfig)
+ r.PUT("/user/plugin/config", a.userPluginController.UpdatePluginUserConfig)
}
func (a *AnswerAPIRouter) RegisterAnswerAdminAPIRouter(r *gin.RouterGroup) {
diff --git a/internal/schema/plugin_user_schema.go b/internal/schema/plugin_user_schema.go
new file mode 100644
index 00000000..b133a279
--- /dev/null
+++ b/internal/schema/plugin_user_schema.go
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package schema
+
+import (
+ "github.com/apache/incubator-answer/plugin"
+ "github.com/gin-gonic/gin"
+)
+
+type GetUserPluginListResp struct {
+ Name string `json:"name"`
+ SlugName string `json:"slug_name"`
+}
+
+type UpdateUserPluginReq struct {
+ PluginSlugName string `validate:"required,gt=1,lte=100" json:"plugin_slug_name"`
+ UserID string `json:"-"`
+}
+
+type GetUserPluginConfigReq struct {
+ PluginSlugName string `validate:"required,gt=1,lte=100" form:"plugin_slug_name"`
+ UserID string `json:"-"`
+}
+
+type GetUserPluginConfigResp struct {
+ Name string `json:"name"`
+ SlugName string `json:"slug_name"`
+ ConfigFields []*ConfigField `json:"config_fields"`
+}
+
+func (g *GetUserPluginConfigResp) SetConfigFields(ctx *gin.Context, fields []plugin.ConfigField) {
+ for _, field := range fields {
+ configField := &ConfigField{
+ Name: field.Name,
+ Type: string(field.Type),
+ Title: field.Title.Translate(ctx),
+ Description: field.Description.Translate(ctx),
+ Required: field.Required,
+ Value: field.Value,
+ UIOptions: ConfigFieldUIOptions{
+ Rows: field.UIOptions.Rows,
+ InputType: string(field.UIOptions.InputType),
+ Variant: field.UIOptions.Variant,
+ },
+ }
+ configField.UIOptions.Placeholder = field.UIOptions.Placeholder.Translate(ctx)
+ configField.UIOptions.Label = field.UIOptions.Label.Translate(ctx)
+ configField.UIOptions.Text = field.UIOptions.Text.Translate(ctx)
+ if field.UIOptions.Action != nil {
+ uiOptionAction := &UIOptionAction{
+ Url: field.UIOptions.Action.Url,
+ Method: field.UIOptions.Action.Method,
+ }
+ if field.UIOptions.Action.Loading != nil {
+ uiOptionAction.Loading = &LoadingAction{
+ Text: field.UIOptions.Action.Loading.Text.Translate(ctx),
+ State: string(field.UIOptions.Action.Loading.State),
+ }
+ }
+ if field.UIOptions.Action.OnComplete != nil {
+ uiOptionAction.OnCompleteAction = &OnCompleteAction{
+ ToastReturnMessage: field.UIOptions.Action.OnComplete.ToastReturnMessage,
+ RefreshFormConfig: field.UIOptions.Action.OnComplete.RefreshFormConfig,
+ }
+ }
+ configField.UIOptions.Action = uiOptionAction
+ }
+
+ for _, option := range field.Options {
+ configField.Options = append(configField.Options, ConfigFieldOption{
+ Label: option.Label.Translate(ctx),
+ Value: option.Value,
+ })
+ }
+ g.ConfigFields = append(g.ConfigFields, configField)
+ }
+}
+
+type UpdateUserPluginConfigReq struct {
+ PluginSlugName string `validate:"required,gt=1,lte=100" json:"plugin_slug_name"`
+ ConfigFields map[string]any `json:"config_fields"`
+ UserID string `json:"-"`
+}
diff --git a/internal/service/plugin_common/plugin_common_service.go b/internal/service/plugin_common/plugin_common_service.go
index 832fb321..099051cf 100644
--- a/internal/service/plugin_common/plugin_common_service.go
+++ b/internal/service/plugin_common/plugin_common_service.go
@@ -22,12 +22,11 @@ package plugin_common
import (
"context"
"encoding/json"
- "github.com/apache/incubator-answer/internal/base/data"
- "github.com/apache/incubator-answer/internal/repo/search_sync"
-
"github.com/apache/incubator-answer/internal/base/constant"
+ "github.com/apache/incubator-answer/internal/base/data"
"github.com/apache/incubator-answer/internal/base/reason"
"github.com/apache/incubator-answer/internal/entity"
+ "github.com/apache/incubator-answer/internal/repo/search_sync"
"github.com/apache/incubator-answer/internal/schema"
"github.com/apache/incubator-answer/internal/service/config"
"github.com/apache/incubator-answer/plugin"
@@ -40,16 +39,24 @@ type PluginConfigRepo interface {
GetPluginConfigAll(ctx context.Context) (pluginConfigs []*entity.PluginConfig, err error)
}
+type PluginUserConfigRepo interface {
+ SaveUserPluginConfig(ctx context.Context, userID string, pluginSlugName, configValue string) (err error)
+ GetPluginUserConfig(ctx context.Context, userID, pluginSlugName string) (
+ pluginUserConfig *entity.PluginUserConfig, exist bool, err error)
+}
+
// PluginCommonService user service
type PluginCommonService struct {
- configService *config.ConfigService
- pluginConfigRepo PluginConfigRepo
- data *data.Data
+ configService *config.ConfigService
+ pluginConfigRepo PluginConfigRepo
+ pluginUserConfigRepo PluginUserConfigRepo
+ data *data.Data
}
// NewPluginCommonService new report service
func NewPluginCommonService(
pluginConfigRepo PluginConfigRepo,
+ pluginUserConfigRepo PluginUserConfigRepo,
configService *config.ConfigService,
data *data.Data,
) *PluginCommonService {
@@ -82,10 +89,24 @@ func NewPluginCommonService(
}
}
+ // init plugin user config
+ plugin.RegisterGetPluginUserConfigFunc(func(userID, pluginSlugName string) []byte {
+ pluginUserConfig, exist, err := pluginUserConfigRepo.GetPluginUserConfig(context.Background(), userID, pluginSlugName)
+ if err != nil {
+ log.Error(err)
+ return nil
+ }
+ if !exist {
+ return nil
+ }
+ return []byte(pluginUserConfig.Value)
+ })
+
return &PluginCommonService{
- configService: configService,
- pluginConfigRepo: pluginConfigRepo,
- data: data,
+ configService: configService,
+ pluginConfigRepo: pluginConfigRepo,
+ pluginUserConfigRepo: pluginUserConfigRepo,
+ data: data,
}
}
@@ -114,3 +135,26 @@ func (ps *PluginCommonService) UpdatePluginConfig(ctx context.Context, req *sche
})
return nil
}
+
+// UpdatePluginUserConfig update plugin config
+func (ps *PluginCommonService) UpdatePluginUserConfig(ctx context.Context, req *schema.UpdateUserPluginConfigReq) (err error) {
+ configValue, _ := json.Marshal(req.ConfigFields)
+ err = ps.pluginUserConfigRepo.SaveUserPluginConfig(ctx, req.UserID, req.PluginSlugName, string(configValue))
+ if err != nil {
+ return err
+ }
+ return nil
+}
+
+// GetUserPluginConfig get user plugin config
+func (ps *PluginCommonService) GetUserPluginConfig(ctx context.Context, req *schema.GetUserPluginConfigReq) (
+ configValue string, err error) {
+ pluginUserConfig, exist, err := ps.pluginUserConfigRepo.GetPluginUserConfig(ctx, req.UserID, req.PluginSlugName)
+ if err != nil {
+ return "", err
+ }
+ if !exist {
+ return "", nil
+ }
+ return pluginUserConfig.Value, nil
+}
diff --git a/plugin/plugin.go b/plugin/plugin.go
index 1fec7842..2acc09b8 100644
--- a/plugin/plugin.go
+++ b/plugin/plugin.go
@@ -48,6 +48,10 @@ func Register(p Base) {
registerConfig(p.(Config))
}
+ if _, ok := p.(UserConfig); ok {
+ registerUserConfig(p.(UserConfig))
+ }
+
if _, ok := p.(Connector); ok {
registerConnector(p.(Connector))
}
diff --git a/plugin/user_config.go b/plugin/user_config.go
new file mode 100644
index 00000000..b8922edd
--- /dev/null
+++ b/plugin/user_config.go
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package plugin
+
+type UserConfig interface {
+ Base
+
+ // UserConfigFields returns the list of config fields
+ UserConfigFields() []ConfigField
+ // UserConfigReceiver receives the config data, it calls when the config is saved or initialized.
+ // We recommend to unmarshal the data to a struct, and then use the struct to do something.
+ // The config is encoded in JSON format.
+ // It depends on the definition of ConfigFields.
+ UserConfigReceiver(userID string, config []byte) error
+}
+
+var (
+ // CallUserConfig is a function that calls all registered config plugins
+ CallUserConfig,
+ registerUserConfig = MakePlugin[UserConfig](true)
+ getPluginUserConfigFn func(userID, pluginSlugName string) []byte
+)
+
+// GetPluginUserConfig returns the user config of the given user id
+func GetPluginUserConfig(userID, pluginSlugName string) []byte {
+ if getPluginUserConfigFn != nil {
+ return getPluginUserConfigFn(userID, pluginSlugName)
+ }
+ return nil
+}
+
+// RegisterGetPluginUserConfigFunc registers a function to get the user config of the given user id
+func RegisterGetPluginUserConfigFunc(fn func(userID, pluginSlugName string) []byte) {
+ getPluginUserConfigFn = fn
+}