Commit dd062786 authored by Steven's avatar Steven

feat: add batch upsert tags

parent fdd17ce8
...@@ -585,6 +585,16 @@ ExportMemos exports memos. ...@@ -585,6 +585,16 @@ ExportMemos exports memos.
| 200 | A successful response. | [v2GetTagSuggestionsResponse](#v2gettagsuggestionsresponse) | | 200 | A successful response. | [v2GetTagSuggestionsResponse](#v2gettagsuggestionsresponse) |
| default | An unexpected error response. | [googlerpcStatus](#googlerpcstatus) | | default | An unexpected error response. | [googlerpcStatus](#googlerpcstatus) |
### /api/v2/tags:batchUpsert
#### POST
##### Responses
| Code | Description | Schema |
| ---- | ----------- | ------ |
| 200 | A successful response. | [v2BatchUpsertTagResponse](#v2batchupserttagresponse) |
| default | An unexpected error response. | [googlerpcStatus](#googlerpcstatus) |
### /api/v2/tags:rename ### /api/v2/tags:rename
#### PATCH #### PATCH
...@@ -1033,6 +1043,12 @@ CreateUser creates a new user. ...@@ -1033,6 +1043,12 @@ CreateUser creates a new user.
| createTime | dateTime | | No | | createTime | dateTime | | No |
| payload | [apiv2ActivityPayload](#apiv2activitypayload) | | No | | payload | [apiv2ActivityPayload](#apiv2activitypayload) | | No |
#### v2BatchUpsertTagResponse
| Name | Type | Description | Required |
| ---- | ---- | ----------- | -------- |
| v2BatchUpsertTagResponse | object | | |
#### v2CreateMemoCommentResponse #### v2CreateMemoCommentResponse
| Name | Type | Description | Required | | Name | Type | Description | Required |
...@@ -1425,6 +1441,12 @@ CreateUser creates a new user. ...@@ -1425,6 +1441,12 @@ CreateUser creates a new user.
| ---- | ---- | ----------- | -------- | | ---- | ---- | ----------- | -------- |
| workspaceProfile | [v2WorkspaceProfile](#v2workspaceprofile) | | No | | workspaceProfile | [v2WorkspaceProfile](#v2workspaceprofile) | | No |
#### v2UpsertTagRequest
| Name | Type | Description | Required |
| ---- | ---- | ----------- | -------- |
| name | string | | No |
#### v2UpsertTagResponse #### v2UpsertTagResponse
| Name | Type | Description | Required | | Name | Type | Description | Required |
......
...@@ -736,6 +736,20 @@ paths: ...@@ -736,6 +736,20 @@ paths:
type: string type: string
tags: tags:
- TagService - TagService
/api/v2/tags:batchUpsert:
post:
operationId: TagService_BatchUpsertTag
responses:
"200":
description: A successful response.
schema:
$ref: '#/definitions/v2BatchUpsertTagResponse'
default:
description: An unexpected error response.
schema:
$ref: '#/definitions/googlerpcStatus'
tags:
- TagService
/api/v2/tags:rename: /api/v2/tags:rename:
patch: patch:
operationId: TagService_RenameTag operationId: TagService_RenameTag
...@@ -1416,6 +1430,8 @@ definitions: ...@@ -1416,6 +1430,8 @@ definitions:
format: date-time format: date-time
payload: payload:
$ref: '#/definitions/apiv2ActivityPayload' $ref: '#/definitions/apiv2ActivityPayload'
v2BatchUpsertTagResponse:
type: object
v2CreateMemoCommentResponse: v2CreateMemoCommentResponse:
type: object type: object
properties: properties:
...@@ -1836,6 +1852,11 @@ definitions: ...@@ -1836,6 +1852,11 @@ definitions:
properties: properties:
workspaceProfile: workspaceProfile:
$ref: '#/definitions/v2WorkspaceProfile' $ref: '#/definitions/v2WorkspaceProfile'
v2UpsertTagRequest:
type: object
properties:
name:
type: string
v2UpsertTagResponse: v2UpsertTagResponse:
type: object type: object
properties: properties:
......
...@@ -11,9 +11,6 @@ import ( ...@@ -11,9 +11,6 @@ import (
"github.com/google/cel-go/cel" "github.com/google/cel-go/cel"
"github.com/lithammer/shortuuid/v4" "github.com/lithammer/shortuuid/v4"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/yourselfhosted/gomark/ast"
"github.com/yourselfhosted/gomark/parser"
"github.com/yourselfhosted/gomark/parser/tokenizer"
"go.uber.org/zap" "go.uber.org/zap"
expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1" expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
...@@ -48,11 +45,6 @@ func (s *APIV2Service) CreateMemo(ctx context.Context, request *apiv2pb.CreateMe ...@@ -48,11 +45,6 @@ func (s *APIV2Service) CreateMemo(ctx context.Context, request *apiv2pb.CreateMe
return nil, status.Errorf(codes.InvalidArgument, "content too long") return nil, status.Errorf(codes.InvalidArgument, "content too long")
} }
nodes, err := parser.Parse(tokenizer.Tokenize(request.Content))
if err != nil {
return nil, errors.Wrap(err, "failed to parse memo content")
}
create := &store.Memo{ create := &store.Memo{
ResourceName: shortuuid.New(), ResourceName: shortuuid.New(),
CreatorID: user.ID, CreatorID: user.ID,
...@@ -74,18 +66,6 @@ func (s *APIV2Service) CreateMemo(ctx context.Context, request *apiv2pb.CreateMe ...@@ -74,18 +66,6 @@ func (s *APIV2Service) CreateMemo(ctx context.Context, request *apiv2pb.CreateMe
} }
metric.Enqueue("memo create") metric.Enqueue("memo create")
// Dynamically upsert tags from memo content.
traverseASTNodes(nodes, func(node ast.Node) {
if tag, ok := node.(*ast.Tag); ok {
if _, err := s.Store.UpsertTag(ctx, &store.Tag{
Name: tag.Content,
CreatorID: user.ID,
}); err != nil {
log.Warn("Failed to create tag", zap.Error(err))
}
}
})
memoMessage, err := s.convertMemoFromStore(ctx, memo) memoMessage, err := s.convertMemoFromStore(ctx, memo)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "failed to convert memo") return nil, errors.Wrap(err, "failed to convert memo")
...@@ -250,22 +230,6 @@ func (s *APIV2Service) UpdateMemo(ctx context.Context, request *apiv2pb.UpdateMe ...@@ -250,22 +230,6 @@ func (s *APIV2Service) UpdateMemo(ctx context.Context, request *apiv2pb.UpdateMe
for _, path := range request.UpdateMask.Paths { for _, path := range request.UpdateMask.Paths {
if path == "content" { if path == "content" {
update.Content = &request.Memo.Content update.Content = &request.Memo.Content
nodes, err := parser.Parse(tokenizer.Tokenize(*update.Content))
if err != nil {
return nil, errors.Wrap(err, "failed to parse memo content")
}
// Dynamically upsert tags from memo content.
traverseASTNodes(nodes, func(node ast.Node) {
if tag, ok := node.(*ast.Tag); ok {
if _, err := s.Store.UpsertTag(ctx, &store.Tag{
Name: tag.Content,
CreatorID: user.ID,
}); err != nil {
log.Warn("Failed to create tag", zap.Error(err))
}
}
})
} else if path == "resource_name" { } else if path == "resource_name" {
update.ResourceName = &request.Memo.Name update.ResourceName = &request.Memo.Name
if !util.ResourceNameMatcher.MatchString(*update.ResourceName) { if !util.ResourceNameMatcher.MatchString(*update.ResourceName) {
......
package v2
import (
"github.com/yourselfhosted/gomark/ast"
)
func traverseASTNodes(nodes []ast.Node, fn func(ast.Node)) {
for _, node := range nodes {
fn(node)
switch n := node.(type) {
case *ast.Paragraph:
traverseASTNodes(n.Children, fn)
case *ast.Heading:
traverseASTNodes(n.Children, fn)
case *ast.Blockquote:
traverseASTNodes(n.Children, fn)
case *ast.OrderedList:
traverseASTNodes(n.Children, fn)
case *ast.UnorderedList:
traverseASTNodes(n.Children, fn)
case *ast.TaskList:
traverseASTNodes(n.Children, fn)
case *ast.Bold:
traverseASTNodes(n.Children, fn)
}
}
}
...@@ -3,7 +3,7 @@ package v2 ...@@ -3,7 +3,7 @@ package v2
import ( import (
"context" "context"
"fmt" "fmt"
"regexp" "slices"
"sort" "sort"
"github.com/pkg/errors" "github.com/pkg/errors"
...@@ -11,7 +11,6 @@ import ( ...@@ -11,7 +11,6 @@ import (
"github.com/yourselfhosted/gomark/parser" "github.com/yourselfhosted/gomark/parser"
"github.com/yourselfhosted/gomark/parser/tokenizer" "github.com/yourselfhosted/gomark/parser/tokenizer"
"github.com/yourselfhosted/gomark/restore" "github.com/yourselfhosted/gomark/restore"
"golang.org/x/exp/slices"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
...@@ -42,6 +41,15 @@ func (s *APIV2Service) UpsertTag(ctx context.Context, request *apiv2pb.UpsertTag ...@@ -42,6 +41,15 @@ func (s *APIV2Service) UpsertTag(ctx context.Context, request *apiv2pb.UpsertTag
}, nil }, nil
} }
func (s *APIV2Service) BatchUpsertTag(ctx context.Context, request *apiv2pb.BatchUpsertTagRequest) (*apiv2pb.BatchUpsertTagResponse, error) {
for _, r := range request.Requests {
if _, err := s.UpsertTag(ctx, r); err != nil {
return nil, status.Errorf(codes.Internal, "failed to batch upsert tags: %v", err)
}
}
return &apiv2pb.BatchUpsertTagResponse{}, nil
}
func (s *APIV2Service) ListTags(ctx context.Context, request *apiv2pb.ListTagsRequest) (*apiv2pb.ListTagsResponse, error) { func (s *APIV2Service) ListTags(ctx context.Context, request *apiv2pb.ListTagsRequest) (*apiv2pb.ListTagsResponse, error) {
username, err := ExtractUsernameFromName(request.User) username, err := ExtractUsernameFromName(request.User)
if err != nil { if err != nil {
...@@ -183,7 +191,7 @@ func (s *APIV2Service) GetTagSuggestions(ctx context.Context, request *apiv2pb.G ...@@ -183,7 +191,7 @@ func (s *APIV2Service) GetTagSuggestions(ctx context.Context, request *apiv2pb.G
ContentSearch: []string{"#"}, ContentSearch: []string{"#"},
RowStatus: &normalRowStatus, RowStatus: &normalRowStatus,
} }
memoList, err := s.Store.ListMemos(ctx, memoFind) memos, err := s.Store.ListMemos(ctx, memoFind)
if err != nil { if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list memos: %v", err) return nil, status.Errorf(codes.Internal, "failed to list memos: %v", err)
} }
...@@ -200,12 +208,21 @@ func (s *APIV2Service) GetTagSuggestions(ctx context.Context, request *apiv2pb.G ...@@ -200,12 +208,21 @@ func (s *APIV2Service) GetTagSuggestions(ctx context.Context, request *apiv2pb.G
tagNameList = append(tagNameList, tag.Name) tagNameList = append(tagNameList, tag.Name)
} }
tagMapSet := make(map[string]bool) tagMapSet := make(map[string]bool)
for _, memo := range memoList { for _, memo := range memos {
for _, tag := range findTagListFromMemoContent(memo.Content) { nodes, err := parser.Parse(tokenizer.Tokenize(memo.Content))
if err != nil {
return nil, errors.Wrap(err, "failed to parse memo content")
}
// Dynamically upsert tags from memo content.
traverseASTNodes(nodes, func(node ast.Node) {
if tagNode, ok := node.(*ast.Tag); ok {
tag := tagNode.Content
if !slices.Contains(tagNameList, tag) { if !slices.Contains(tagNameList, tag) {
tagMapSet[tag] = true tagMapSet[tag] = true
} }
} }
})
} }
suggestions := []string{} suggestions := []string{}
for tag := range tagMapSet { for tag := range tagMapSet {
...@@ -231,20 +248,24 @@ func (s *APIV2Service) convertTagFromStore(ctx context.Context, tag *store.Tag) ...@@ -231,20 +248,24 @@ func (s *APIV2Service) convertTagFromStore(ctx context.Context, tag *store.Tag)
}, nil }, nil
} }
var tagRegexp = regexp.MustCompile(`#([^\s#,]+)`) func traverseASTNodes(nodes []ast.Node, fn func(ast.Node)) {
for _, node := range nodes {
func findTagListFromMemoContent(memoContent string) []string { fn(node)
tagMapSet := make(map[string]bool) switch n := node.(type) {
matches := tagRegexp.FindAllStringSubmatch(memoContent, -1) case *ast.Paragraph:
for _, v := range matches { traverseASTNodes(n.Children, fn)
tagName := v[1] case *ast.Heading:
tagMapSet[tagName] = true traverseASTNodes(n.Children, fn)
case *ast.Blockquote:
traverseASTNodes(n.Children, fn)
case *ast.OrderedList:
traverseASTNodes(n.Children, fn)
case *ast.UnorderedList:
traverseASTNodes(n.Children, fn)
case *ast.TaskList:
traverseASTNodes(n.Children, fn)
case *ast.Bold:
traverseASTNodes(n.Children, fn)
} }
tagList := []string{}
for tag := range tagMapSet {
tagList = append(tagList, tag)
} }
sort.Strings(tagList)
return tagList
} }
...@@ -10,6 +10,9 @@ service TagService { ...@@ -10,6 +10,9 @@ service TagService {
rpc UpsertTag(UpsertTagRequest) returns (UpsertTagResponse) { rpc UpsertTag(UpsertTagRequest) returns (UpsertTagResponse) {
option (google.api.http) = {post: "/api/v2/tags"}; option (google.api.http) = {post: "/api/v2/tags"};
} }
rpc BatchUpsertTag(BatchUpsertTagRequest) returns (BatchUpsertTagResponse) {
option (google.api.http) = {post: "/api/v2/tags:batchUpsert"};
}
rpc ListTags(ListTagsRequest) returns (ListTagsResponse) { rpc ListTags(ListTagsRequest) returns (ListTagsResponse) {
option (google.api.http) = {get: "/api/v2/tags"}; option (google.api.http) = {get: "/api/v2/tags"};
} }
...@@ -39,6 +42,13 @@ message UpsertTagResponse { ...@@ -39,6 +42,13 @@ message UpsertTagResponse {
Tag tag = 1; Tag tag = 1;
} }
message BatchUpsertTagRequest {
repeated UpsertTagRequest requests = 1;
}
message BatchUpsertTagResponse {
}
message ListTagsRequest { message ListTagsRequest {
// The creator of tags. // The creator of tags.
// Format: users/{username} // Format: users/{username}
......
...@@ -134,6 +134,8 @@ ...@@ -134,6 +134,8 @@
- [MemoService](#memos-api-v2-MemoService) - [MemoService](#memos-api-v2-MemoService)
- [api/v2/tag_service.proto](#api_v2_tag_service-proto) - [api/v2/tag_service.proto](#api_v2_tag_service-proto)
- [BatchUpsertTagRequest](#memos-api-v2-BatchUpsertTagRequest)
- [BatchUpsertTagResponse](#memos-api-v2-BatchUpsertTagResponse)
- [DeleteTagRequest](#memos-api-v2-DeleteTagRequest) - [DeleteTagRequest](#memos-api-v2-DeleteTagRequest)
- [DeleteTagResponse](#memos-api-v2-DeleteTagResponse) - [DeleteTagResponse](#memos-api-v2-DeleteTagResponse)
- [GetTagSuggestionsRequest](#memos-api-v2-GetTagSuggestionsRequest) - [GetTagSuggestionsRequest](#memos-api-v2-GetTagSuggestionsRequest)
...@@ -1869,6 +1871,31 @@ Used internally for obfuscating the page token. ...@@ -1869,6 +1871,31 @@ Used internally for obfuscating the page token.
<a name="memos-api-v2-BatchUpsertTagRequest"></a>
### BatchUpsertTagRequest
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| requests | [UpsertTagRequest](#memos-api-v2-UpsertTagRequest) | repeated | |
<a name="memos-api-v2-BatchUpsertTagResponse"></a>
### BatchUpsertTagResponse
<a name="memos-api-v2-DeleteTagRequest"></a> <a name="memos-api-v2-DeleteTagRequest"></a>
### DeleteTagRequest ### DeleteTagRequest
...@@ -2046,6 +2073,7 @@ Used internally for obfuscating the page token. ...@@ -2046,6 +2073,7 @@ Used internally for obfuscating the page token.
| Method Name | Request Type | Response Type | Description | | Method Name | Request Type | Response Type | Description |
| ----------- | ------------ | ------------- | ------------| | ----------- | ------------ | ------------- | ------------|
| UpsertTag | [UpsertTagRequest](#memos-api-v2-UpsertTagRequest) | [UpsertTagResponse](#memos-api-v2-UpsertTagResponse) | | | UpsertTag | [UpsertTagRequest](#memos-api-v2-UpsertTagRequest) | [UpsertTagResponse](#memos-api-v2-UpsertTagResponse) | |
| BatchUpsertTag | [BatchUpsertTagRequest](#memos-api-v2-BatchUpsertTagRequest) | [BatchUpsertTagResponse](#memos-api-v2-BatchUpsertTagResponse) | |
| ListTags | [ListTagsRequest](#memos-api-v2-ListTagsRequest) | [ListTagsResponse](#memos-api-v2-ListTagsResponse) | | | ListTags | [ListTagsRequest](#memos-api-v2-ListTagsRequest) | [ListTagsResponse](#memos-api-v2-ListTagsResponse) | |
| RenameTag | [RenameTagRequest](#memos-api-v2-RenameTagRequest) | [RenameTagResponse](#memos-api-v2-RenameTagResponse) | | | RenameTag | [RenameTagRequest](#memos-api-v2-RenameTagRequest) | [RenameTagResponse](#memos-api-v2-RenameTagResponse) | |
| DeleteTag | [DeleteTagRequest](#memos-api-v2-DeleteTagRequest) | [DeleteTagResponse](#memos-api-v2-DeleteTagResponse) | | | DeleteTag | [DeleteTagRequest](#memos-api-v2-DeleteTagRequest) | [DeleteTagResponse](#memos-api-v2-DeleteTagResponse) | |
......
This diff is collapsed.
...@@ -67,6 +67,42 @@ func local_request_TagService_UpsertTag_0(ctx context.Context, marshaler runtime ...@@ -67,6 +67,42 @@ func local_request_TagService_UpsertTag_0(ctx context.Context, marshaler runtime
} }
var (
filter_TagService_BatchUpsertTag_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)}
)
func request_TagService_BatchUpsertTag_0(ctx context.Context, marshaler runtime.Marshaler, client TagServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq BatchUpsertTagRequest
var metadata runtime.ServerMetadata
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_TagService_BatchUpsertTag_0); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
msg, err := client.BatchUpsertTag(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD))
return msg, metadata, err
}
func local_request_TagService_BatchUpsertTag_0(ctx context.Context, marshaler runtime.Marshaler, server TagServiceServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq BatchUpsertTagRequest
var metadata runtime.ServerMetadata
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_TagService_BatchUpsertTag_0); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
msg, err := server.BatchUpsertTag(ctx, &protoReq)
return msg, metadata, err
}
var ( var (
filter_TagService_ListTags_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} filter_TagService_ListTags_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)}
) )
...@@ -242,6 +278,31 @@ func RegisterTagServiceHandlerServer(ctx context.Context, mux *runtime.ServeMux, ...@@ -242,6 +278,31 @@ func RegisterTagServiceHandlerServer(ctx context.Context, mux *runtime.ServeMux,
}) })
mux.Handle("POST", pattern_TagService_BatchUpsertTag_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
var stream runtime.ServerTransportStream
ctx = grpc.NewContextWithServerTransportStream(ctx, &stream)
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
var err error
var annotatedContext context.Context
annotatedContext, err = runtime.AnnotateIncomingContext(ctx, mux, req, "/memos.api.v2.TagService/BatchUpsertTag", runtime.WithHTTPPathPattern("/api/v2/tags:batchUpsert"))
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
resp, md, err := local_request_TagService_BatchUpsertTag_0(annotatedContext, inboundMarshaler, server, req, pathParams)
md.HeaderMD, md.TrailerMD = metadata.Join(md.HeaderMD, stream.Header()), metadata.Join(md.TrailerMD, stream.Trailer())
annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md)
if err != nil {
runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err)
return
}
forward_TagService_BatchUpsertTag_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
})
mux.Handle("GET", pattern_TagService_ListTags_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { mux.Handle("GET", pattern_TagService_ListTags_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context()) ctx, cancel := context.WithCancel(req.Context())
defer cancel() defer cancel()
...@@ -405,6 +466,28 @@ func RegisterTagServiceHandlerClient(ctx context.Context, mux *runtime.ServeMux, ...@@ -405,6 +466,28 @@ func RegisterTagServiceHandlerClient(ctx context.Context, mux *runtime.ServeMux,
}) })
mux.Handle("POST", pattern_TagService_BatchUpsertTag_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
var err error
var annotatedContext context.Context
annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/memos.api.v2.TagService/BatchUpsertTag", runtime.WithHTTPPathPattern("/api/v2/tags:batchUpsert"))
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
resp, md, err := request_TagService_BatchUpsertTag_0(annotatedContext, inboundMarshaler, client, req, pathParams)
annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md)
if err != nil {
runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err)
return
}
forward_TagService_BatchUpsertTag_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
})
mux.Handle("GET", pattern_TagService_ListTags_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { mux.Handle("GET", pattern_TagService_ListTags_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context()) ctx, cancel := context.WithCancel(req.Context())
defer cancel() defer cancel()
...@@ -499,6 +582,8 @@ func RegisterTagServiceHandlerClient(ctx context.Context, mux *runtime.ServeMux, ...@@ -499,6 +582,8 @@ func RegisterTagServiceHandlerClient(ctx context.Context, mux *runtime.ServeMux,
var ( var (
pattern_TagService_UpsertTag_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v2", "tags"}, "")) pattern_TagService_UpsertTag_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v2", "tags"}, ""))
pattern_TagService_BatchUpsertTag_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v2", "tags"}, "batchUpsert"))
pattern_TagService_ListTags_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v2", "tags"}, "")) pattern_TagService_ListTags_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v2", "tags"}, ""))
pattern_TagService_RenameTag_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v2", "tags"}, "rename")) pattern_TagService_RenameTag_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v2", "tags"}, "rename"))
...@@ -511,6 +596,8 @@ var ( ...@@ -511,6 +596,8 @@ var (
var ( var (
forward_TagService_UpsertTag_0 = runtime.ForwardResponseMessage forward_TagService_UpsertTag_0 = runtime.ForwardResponseMessage
forward_TagService_BatchUpsertTag_0 = runtime.ForwardResponseMessage
forward_TagService_ListTags_0 = runtime.ForwardResponseMessage forward_TagService_ListTags_0 = runtime.ForwardResponseMessage
forward_TagService_RenameTag_0 = runtime.ForwardResponseMessage forward_TagService_RenameTag_0 = runtime.ForwardResponseMessage
......
...@@ -20,6 +20,7 @@ const _ = grpc.SupportPackageIsVersion7 ...@@ -20,6 +20,7 @@ const _ = grpc.SupportPackageIsVersion7
const ( const (
TagService_UpsertTag_FullMethodName = "/memos.api.v2.TagService/UpsertTag" TagService_UpsertTag_FullMethodName = "/memos.api.v2.TagService/UpsertTag"
TagService_BatchUpsertTag_FullMethodName = "/memos.api.v2.TagService/BatchUpsertTag"
TagService_ListTags_FullMethodName = "/memos.api.v2.TagService/ListTags" TagService_ListTags_FullMethodName = "/memos.api.v2.TagService/ListTags"
TagService_RenameTag_FullMethodName = "/memos.api.v2.TagService/RenameTag" TagService_RenameTag_FullMethodName = "/memos.api.v2.TagService/RenameTag"
TagService_DeleteTag_FullMethodName = "/memos.api.v2.TagService/DeleteTag" TagService_DeleteTag_FullMethodName = "/memos.api.v2.TagService/DeleteTag"
...@@ -31,6 +32,7 @@ const ( ...@@ -31,6 +32,7 @@ const (
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type TagServiceClient interface { type TagServiceClient interface {
UpsertTag(ctx context.Context, in *UpsertTagRequest, opts ...grpc.CallOption) (*UpsertTagResponse, error) UpsertTag(ctx context.Context, in *UpsertTagRequest, opts ...grpc.CallOption) (*UpsertTagResponse, error)
BatchUpsertTag(ctx context.Context, in *BatchUpsertTagRequest, opts ...grpc.CallOption) (*BatchUpsertTagResponse, error)
ListTags(ctx context.Context, in *ListTagsRequest, opts ...grpc.CallOption) (*ListTagsResponse, error) ListTags(ctx context.Context, in *ListTagsRequest, opts ...grpc.CallOption) (*ListTagsResponse, error)
RenameTag(ctx context.Context, in *RenameTagRequest, opts ...grpc.CallOption) (*RenameTagResponse, error) RenameTag(ctx context.Context, in *RenameTagRequest, opts ...grpc.CallOption) (*RenameTagResponse, error)
DeleteTag(ctx context.Context, in *DeleteTagRequest, opts ...grpc.CallOption) (*DeleteTagResponse, error) DeleteTag(ctx context.Context, in *DeleteTagRequest, opts ...grpc.CallOption) (*DeleteTagResponse, error)
...@@ -54,6 +56,15 @@ func (c *tagServiceClient) UpsertTag(ctx context.Context, in *UpsertTagRequest, ...@@ -54,6 +56,15 @@ func (c *tagServiceClient) UpsertTag(ctx context.Context, in *UpsertTagRequest,
return out, nil return out, nil
} }
func (c *tagServiceClient) BatchUpsertTag(ctx context.Context, in *BatchUpsertTagRequest, opts ...grpc.CallOption) (*BatchUpsertTagResponse, error) {
out := new(BatchUpsertTagResponse)
err := c.cc.Invoke(ctx, TagService_BatchUpsertTag_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *tagServiceClient) ListTags(ctx context.Context, in *ListTagsRequest, opts ...grpc.CallOption) (*ListTagsResponse, error) { func (c *tagServiceClient) ListTags(ctx context.Context, in *ListTagsRequest, opts ...grpc.CallOption) (*ListTagsResponse, error) {
out := new(ListTagsResponse) out := new(ListTagsResponse)
err := c.cc.Invoke(ctx, TagService_ListTags_FullMethodName, in, out, opts...) err := c.cc.Invoke(ctx, TagService_ListTags_FullMethodName, in, out, opts...)
...@@ -95,6 +106,7 @@ func (c *tagServiceClient) GetTagSuggestions(ctx context.Context, in *GetTagSugg ...@@ -95,6 +106,7 @@ func (c *tagServiceClient) GetTagSuggestions(ctx context.Context, in *GetTagSugg
// for forward compatibility // for forward compatibility
type TagServiceServer interface { type TagServiceServer interface {
UpsertTag(context.Context, *UpsertTagRequest) (*UpsertTagResponse, error) UpsertTag(context.Context, *UpsertTagRequest) (*UpsertTagResponse, error)
BatchUpsertTag(context.Context, *BatchUpsertTagRequest) (*BatchUpsertTagResponse, error)
ListTags(context.Context, *ListTagsRequest) (*ListTagsResponse, error) ListTags(context.Context, *ListTagsRequest) (*ListTagsResponse, error)
RenameTag(context.Context, *RenameTagRequest) (*RenameTagResponse, error) RenameTag(context.Context, *RenameTagRequest) (*RenameTagResponse, error)
DeleteTag(context.Context, *DeleteTagRequest) (*DeleteTagResponse, error) DeleteTag(context.Context, *DeleteTagRequest) (*DeleteTagResponse, error)
...@@ -109,6 +121,9 @@ type UnimplementedTagServiceServer struct { ...@@ -109,6 +121,9 @@ type UnimplementedTagServiceServer struct {
func (UnimplementedTagServiceServer) UpsertTag(context.Context, *UpsertTagRequest) (*UpsertTagResponse, error) { func (UnimplementedTagServiceServer) UpsertTag(context.Context, *UpsertTagRequest) (*UpsertTagResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method UpsertTag not implemented") return nil, status.Errorf(codes.Unimplemented, "method UpsertTag not implemented")
} }
func (UnimplementedTagServiceServer) BatchUpsertTag(context.Context, *BatchUpsertTagRequest) (*BatchUpsertTagResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method BatchUpsertTag not implemented")
}
func (UnimplementedTagServiceServer) ListTags(context.Context, *ListTagsRequest) (*ListTagsResponse, error) { func (UnimplementedTagServiceServer) ListTags(context.Context, *ListTagsRequest) (*ListTagsResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method ListTags not implemented") return nil, status.Errorf(codes.Unimplemented, "method ListTags not implemented")
} }
...@@ -152,6 +167,24 @@ func _TagService_UpsertTag_Handler(srv interface{}, ctx context.Context, dec fun ...@@ -152,6 +167,24 @@ func _TagService_UpsertTag_Handler(srv interface{}, ctx context.Context, dec fun
return interceptor(ctx, in, info, handler) return interceptor(ctx, in, info, handler)
} }
func _TagService_BatchUpsertTag_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(BatchUpsertTagRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(TagServiceServer).BatchUpsertTag(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: TagService_BatchUpsertTag_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(TagServiceServer).BatchUpsertTag(ctx, req.(*BatchUpsertTagRequest))
}
return interceptor(ctx, in, info, handler)
}
func _TagService_ListTags_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { func _TagService_ListTags_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ListTagsRequest) in := new(ListTagsRequest)
if err := dec(in); err != nil { if err := dec(in); err != nil {
...@@ -235,6 +268,10 @@ var TagService_ServiceDesc = grpc.ServiceDesc{ ...@@ -235,6 +268,10 @@ var TagService_ServiceDesc = grpc.ServiceDesc{
MethodName: "UpsertTag", MethodName: "UpsertTag",
Handler: _TagService_UpsertTag_Handler, Handler: _TagService_UpsertTag_Handler,
}, },
{
MethodName: "BatchUpsertTag",
Handler: _TagService_BatchUpsertTag_Handler,
},
{ {
MethodName: "ListTags", MethodName: "ListTags",
Handler: _TagService_ListTags_Handler, Handler: _TagService_ListTags_Handler,
......
...@@ -6,7 +6,7 @@ import useLocalStorage from "react-use/lib/useLocalStorage"; ...@@ -6,7 +6,7 @@ import useLocalStorage from "react-use/lib/useLocalStorage";
import { memoServiceClient } from "@/grpcweb"; import { memoServiceClient } from "@/grpcweb";
import { TAB_SPACE_WIDTH, UNKNOWN_ID } from "@/helpers/consts"; import { TAB_SPACE_WIDTH, UNKNOWN_ID } from "@/helpers/consts";
import { isValidUrl } from "@/helpers/utils"; import { isValidUrl } from "@/helpers/utils";
import { useGlobalStore, useResourceStore } from "@/store/module"; import { useGlobalStore, useResourceStore, useTagStore } from "@/store/module";
import { useMemoStore, useUserStore } from "@/store/v1"; import { useMemoStore, useUserStore } from "@/store/v1";
import { MemoRelation, MemoRelation_Type } from "@/types/proto/api/v2/memo_relation_service"; import { MemoRelation, MemoRelation_Type } from "@/types/proto/api/v2/memo_relation_service";
import { Memo, Visibility } from "@/types/proto/api/v2/memo_service"; import { Memo, Visibility } from "@/types/proto/api/v2/memo_service";
...@@ -14,6 +14,7 @@ import { Resource } from "@/types/proto/api/v2/resource_service"; ...@@ -14,6 +14,7 @@ import { Resource } from "@/types/proto/api/v2/resource_service";
import { UserSetting } from "@/types/proto/api/v2/user_service"; import { UserSetting } from "@/types/proto/api/v2/user_service";
import { useTranslate } from "@/utils/i18n"; import { useTranslate } from "@/utils/i18n";
import { convertVisibilityFromString, convertVisibilityToString } from "@/utils/memo"; import { convertVisibilityFromString, convertVisibilityToString } from "@/utils/memo";
import { extractTagsFromContent } from "@/utils/tag";
import showCreateResourceDialog from "../CreateResourceDialog"; import showCreateResourceDialog from "../CreateResourceDialog";
import Icon from "../Icon"; import Icon from "../Icon";
import VisibilityIcon from "../VisibilityIcon"; import VisibilityIcon from "../VisibilityIcon";
...@@ -57,6 +58,7 @@ const MemoEditor = (props: Props) => { ...@@ -57,6 +58,7 @@ const MemoEditor = (props: Props) => {
const userStore = useUserStore(); const userStore = useUserStore();
const memoStore = useMemoStore(); const memoStore = useMemoStore();
const resourceStore = useResourceStore(); const resourceStore = useResourceStore();
const tagStore = useTagStore();
const [state, setState] = useState<State>({ const [state, setState] = useState<State>({
memoVisibility: Visibility.PRIVATE, memoVisibility: Visibility.PRIVATE,
resourceList: [], resourceList: [],
...@@ -326,6 +328,10 @@ const MemoEditor = (props: Props) => { ...@@ -326,6 +328,10 @@ const MemoEditor = (props: Props) => {
toast.error(error.details); toast.error(error.details);
} }
// Batch upsert tags.
const tags = extractTagsFromContent(content);
await tagStore.batchUpsertTag(tags);
setState((state) => { setState((state) => {
return { return {
...state, ...state,
......
...@@ -25,6 +25,17 @@ export const useTagStore = () => { ...@@ -25,6 +25,17 @@ export const useTagStore = () => {
store.dispatch(upsertTagAction(tagName)); store.dispatch(upsertTagAction(tagName));
}; };
const batchUpsertTag = async (tagNames: string[]) => {
await tagServiceClient.batchUpsertTag({
requests: tagNames.map((name) => ({
name,
})),
});
for (const tagName of tagNames) {
store.dispatch(upsertTagAction(tagName));
}
};
const deleteTag = async (tagName: string) => { const deleteTag = async (tagName: string) => {
await tagServiceClient.deleteTag({ await tagServiceClient.deleteTag({
tag: { tag: {
...@@ -40,6 +51,7 @@ export const useTagStore = () => { ...@@ -40,6 +51,7 @@ export const useTagStore = () => {
getState, getState,
fetchTags, fetchTags,
upsertTag, upsertTag,
batchUpsertTag,
deleteTag, deleteTag,
}; };
}; };
import { Node } from "@/types/node";
export const TAG_REG = /#([^\s#,]+)/; export const TAG_REG = /#([^\s#,]+)/;
// extractTagsFromContent extracts tags from content.
export const extractTagsFromContent = (content: string) => {
const nodes = window.parse(content);
const tags = new Set<string>();
const traverse = (nodes: Node[], handle: (node: Node) => void) => {
for (const node of nodes) {
if (!node) {
continue;
}
handle(node);
if (node.paragraphNode || node.unorderedListNode || node.orderedListNode) {
const children = ((node.paragraphNode || node.unorderedListNode || node.orderedListNode) as any).children;
if (Array.isArray(children)) {
traverse(children, handle);
}
}
}
};
traverse(nodes, (node) => {
if (node.tagNode?.content) {
tags.add(node.tagNode.content);
}
});
return Array.from(tags);
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment