mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 05:51:51 +08:00
Comments: support live comments in dashboards and annotations (#44980)
This commit is contained in:
@ -462,6 +462,11 @@ func (hs *HTTPServer) registerRoutes() {
|
||||
|
||||
// short urls
|
||||
apiRoute.Post("/short-urls", routing.Wrap(hs.createShortURL))
|
||||
|
||||
apiRoute.Group("/comments", func(commentRoute routing.RouteRegister) {
|
||||
commentRoute.Post("/get", routing.Wrap(hs.commentsGet))
|
||||
commentRoute.Post("/create", routing.Wrap(hs.commentsCreate))
|
||||
})
|
||||
}, reqSignedIn)
|
||||
|
||||
// admin api
|
||||
|
49
pkg/api/comments.go
Normal file
49
pkg/api/comments.go
Normal file
@ -0,0 +1,49 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/grafana/grafana/pkg/api/response"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/comments"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
)
|
||||
|
||||
func (hs *HTTPServer) commentsGet(c *models.ReqContext) response.Response {
|
||||
cmd := comments.GetCmd{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
items, err := hs.commentsService.Get(c.Req.Context(), c.OrgId, c.SignedInUser, cmd)
|
||||
if err != nil {
|
||||
if errors.Is(err, comments.ErrPermissionDenied) {
|
||||
return response.Error(http.StatusForbidden, "permission denied", err)
|
||||
}
|
||||
return response.Error(http.StatusInternalServerError, "internal error", err)
|
||||
}
|
||||
return response.JSON(200, util.DynMap{
|
||||
"comments": items,
|
||||
})
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) commentsCreate(c *models.ReqContext) response.Response {
|
||||
cmd := comments.CreateCmd{}
|
||||
if err := web.Bind(c.Req, &cmd); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
if c.SignedInUser.UserId == 0 && !c.SignedInUser.HasRole(models.ROLE_ADMIN) {
|
||||
return response.Error(http.StatusForbidden, "admin role required", nil)
|
||||
}
|
||||
comment, err := hs.commentsService.Create(c.Req.Context(), c.OrgId, c.SignedInUser, cmd)
|
||||
if err != nil {
|
||||
if errors.Is(err, comments.ErrPermissionDenied) {
|
||||
return response.Error(http.StatusForbidden, "permission denied", err)
|
||||
}
|
||||
return response.Error(http.StatusInternalServerError, "internal error", err)
|
||||
}
|
||||
return response.JSON(200, util.DynMap{
|
||||
"comment": comment,
|
||||
})
|
||||
}
|
@ -93,7 +93,7 @@ func newTestLive(t *testing.T) *live.GrafanaLive {
|
||||
nil,
|
||||
&usagestats.UsageStatsMock{T: t},
|
||||
nil,
|
||||
features)
|
||||
features, nil)
|
||||
require.NoError(t, err)
|
||||
return gLive
|
||||
}
|
||||
|
@ -30,6 +30,7 @@ import (
|
||||
acmiddleware "github.com/grafana/grafana/pkg/services/accesscontrol/middleware"
|
||||
"github.com/grafana/grafana/pkg/services/alerting"
|
||||
"github.com/grafana/grafana/pkg/services/cleanup"
|
||||
"github.com/grafana/grafana/pkg/services/comments"
|
||||
"github.com/grafana/grafana/pkg/services/contexthandler"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
|
||||
@ -134,6 +135,7 @@ type HTTPServer struct {
|
||||
dashboardProvisioningService dashboards.DashboardProvisioningService
|
||||
folderService dashboards.FolderService
|
||||
DatasourcePermissionsService DatasourcePermissionsService
|
||||
commentsService *comments.Service
|
||||
AlertNotificationService *alerting.AlertNotificationService
|
||||
DashboardsnapshotsService *dashboardsnapshots.Service
|
||||
}
|
||||
@ -166,7 +168,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
|
||||
notificationService *notifications.NotificationService, dashboardService dashboards.DashboardService,
|
||||
dashboardProvisioningService dashboards.DashboardProvisioningService, folderService dashboards.FolderService,
|
||||
datasourcePermissionsService DatasourcePermissionsService, alertNotificationService *alerting.AlertNotificationService,
|
||||
dashboardsnapshotsService *dashboardsnapshots.Service,
|
||||
dashboardsnapshotsService *dashboardsnapshots.Service, commentsService *comments.Service,
|
||||
) (*HTTPServer, error) {
|
||||
web.Env = cfg.Env
|
||||
m := web.New()
|
||||
@ -231,6 +233,7 @@ func ProvideHTTPServer(opts ServerOptions, cfg *setting.Cfg, routeRegister routi
|
||||
dashboardProvisioningService: dashboardProvisioningService,
|
||||
folderService: folderService,
|
||||
DatasourcePermissionsService: datasourcePermissionsService,
|
||||
commentsService: commentsService,
|
||||
teamPermissionsService: permissionsServices.GetTeamService(),
|
||||
AlertNotificationService: alertNotificationService,
|
||||
DashboardsnapshotsService: dashboardsnapshotsService,
|
||||
|
Reference in New Issue
Block a user