diff --git a/api/doc.go b/api/doc.go index 65aaadb..581069c 100644 --- a/api/doc.go +++ b/api/doc.go @@ -69,10 +69,20 @@ func (s *HttpServer) update() gin.HandlerFunc { body.Namespace = namespace enId, _ := strconv.Atoi(entryId) body.EntryId = int64(enId) - // update the document - if err := s.chain.UpdateDocument(c, body.ToModel()); err != nil { - c.String(500, fmt.Sprintf("update document error: %s", err)) - return + + if body.IsUpdate() { + // update the document + if err := s.chain.UpdateDocument(c, body.ToModel()); err != nil { + c.String(500, fmt.Sprintf("update document error: %s", err)) + return + } + } + if body.IsUpdateToken() { + // update token + if err := s.chain.UpdateTokens(c, namespace, body.EntryId); err != nil { + c.String(500, fmt.Sprintf("update document token error: %s", err)) + return + } } c.JSON(200, body) } diff --git a/api/request.go b/api/request.go index c9fdc85..98421c5 100644 --- a/api/request.go +++ b/api/request.go @@ -38,11 +38,12 @@ func (r *DocRequest) Valid() error { } type DocUpdateRequest struct { - Namespace string `json:"namespace"` - EntryId int64 `json:"entryId,omitempty"` - ParentID *int64 `json:"parentId,omitempty"` - UnRead *bool `json:"unRead,omitempty"` - Mark *bool `json:"mark,omitempty"` + Namespace string `json:"namespace"` + EntryId int64 `json:"entryId,omitempty"` + ParentID *int64 `json:"parentId,omitempty"` + UnRead *bool `json:"unRead,omitempty"` + Mark *bool `json:"mark,omitempty"` + UpdateToken *bool `json:"updateToken,omitempty"` } func (r *DocUpdateRequest) Valid() error { @@ -55,6 +56,14 @@ func (r *DocUpdateRequest) Valid() error { return nil } +func (r *DocUpdateRequest) IsUpdate() bool { + return r.ParentID != nil || r.UnRead != nil || r.Mark != nil +} + +func (r *DocUpdateRequest) IsUpdateToken() bool { + return r.UpdateToken != nil && *r.UpdateToken +} + func (r *DocUpdateRequest) ToModel() *doc.Document { return &doc.Document{ EntryId: r.EntryId, diff --git a/pkg/service/chain.go b/pkg/service/chain.go index 217ba60..12838cf 100644 --- a/pkg/service/chain.go +++ b/pkg/service/chain.go @@ -101,6 +101,33 @@ func (c *Chain) CreateDocument(ctx context.Context, document *doc.Document) erro }) } +func (c *Chain) UpdateTokens(ctx context.Context, namespace string, entryId int64) error { + return ChainPool.Run(ctx, func(ctx context.Context) error { + ctx = c.WithNamespace(ctx, namespace) + c.Log.Debugf("update document tokens entryId: %d", entryId) + var ( + document *doc.Document + err error + ) + if document, err = c.GetDocument(ctx, namespace, entryId); err != nil { + c.Log.Errorf("get document error: %s", err) + return err + } else if document == nil { + c.Log.Debugf("document of entryId not found: %d", entryId) + return fmt.Errorf("document of entryId %d not found", entryId) + } + for _, plugin := range c.Plugins { + err := plugin.Run(ctx, document) + if err != nil { + c.Log.Errorf("plugin error: %s", err) + return err + } + } + c.Log.Debugf("update tokens: %+v", document.Name) + return c.DocClient.UpdateTokens(ctx, document) + }) +} + func (c *Chain) UpdateDocument(ctx context.Context, document *doc.Document) error { return ChainPool.Run(ctx, func(ctx context.Context) error { ctx = c.WithNamespace(ctx, document.Namespace)