Commit dd062786 authored by Steven's avatar Steven

feat: add batch upsert tags

parent fdd17ce8
......@@ -585,6 +585,16 @@ ExportMemos exports memos.
| 200 | A successful response. | [v2GetTagSuggestionsResponse](#v2gettagsuggestionsresponse) |
| 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
#### PATCH
......@@ -1033,6 +1043,12 @@ CreateUser creates a new user.
| createTime | dateTime | | No |
| payload | [apiv2ActivityPayload](#apiv2activitypayload) | | No |
#### v2BatchUpsertTagResponse
| Name | Type | Description | Required |
| ---- | ---- | ----------- | -------- |
| v2BatchUpsertTagResponse | object | | |
#### v2CreateMemoCommentResponse
| Name | Type | Description | Required |
......@@ -1425,6 +1441,12 @@ CreateUser creates a new user.
| ---- | ---- | ----------- | -------- |
| workspaceProfile | [v2WorkspaceProfile](#v2workspaceprofile) | | No |
#### v2UpsertTagRequest
| Name | Type | Description | Required |
| ---- | ---- | ----------- | -------- |
| name | string | | No |
#### v2UpsertTagResponse
| Name | Type | Description | Required |
......
......@@ -736,6 +736,20 @@ paths:
type: string
tags:
- 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:
patch:
operationId: TagService_RenameTag
......@@ -1416,6 +1430,8 @@ definitions:
format: date-time
payload:
$ref: '#/definitions/apiv2ActivityPayload'
v2BatchUpsertTagResponse:
type: object
v2CreateMemoCommentResponse:
type: object
properties:
......@@ -1836,6 +1852,11 @@ definitions:
properties:
workspaceProfile:
$ref: '#/definitions/v2WorkspaceProfile'
v2UpsertTagRequest:
type: object
properties:
name:
type: string
v2UpsertTagResponse:
type: object
properties:
......
......@@ -11,9 +11,6 @@ import (
"github.com/google/cel-go/cel"
"github.com/lithammer/shortuuid/v4"
"github.com/pkg/errors"
"github.com/yourselfhosted/gomark/ast"
"github.com/yourselfhosted/gomark/parser"
"github.com/yourselfhosted/gomark/parser/tokenizer"
"go.uber.org/zap"
expr "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
"google.golang.org/grpc/codes"
......@@ -48,11 +45,6 @@ func (s *APIV2Service) CreateMemo(ctx context.Context, request *apiv2pb.CreateMe
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{
ResourceName: shortuuid.New(),
CreatorID: user.ID,
......@@ -74,18 +66,6 @@ func (s *APIV2Service) CreateMemo(ctx context.Context, request *apiv2pb.CreateMe
}
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)
if err != nil {
return nil, errors.Wrap(err, "failed to convert memo")
......@@ -250,22 +230,6 @@ func (s *APIV2Service) UpdateMemo(ctx context.Context, request *apiv2pb.UpdateMe
for _, path := range request.UpdateMask.Paths {
if path == "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" {
update.ResourceName = &request.Memo.Name
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
import (
"context"
"fmt"
"regexp"
"slices"
"sort"
"github.com/pkg/errors"
......@@ -11,7 +11,6 @@ import (
"github.com/yourselfhosted/gomark/parser"
"github.com/yourselfhosted/gomark/parser/tokenizer"
"github.com/yourselfhosted/gomark/restore"
"golang.org/x/exp/slices"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
......@@ -42,6 +41,15 @@ func (s *APIV2Service) UpsertTag(ctx context.Context, request *apiv2pb.UpsertTag
}, 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) {
username, err := ExtractUsernameFromName(request.User)
if err != nil {
......@@ -183,7 +191,7 @@ func (s *APIV2Service) GetTagSuggestions(ctx context.Context, request *apiv2pb.G
ContentSearch: []string{"#"},
RowStatus: &normalRowStatus,
}
memoList, err := s.Store.ListMemos(ctx, memoFind)
memos, err := s.Store.ListMemos(ctx, memoFind)
if err != nil {
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
tagNameList = append(tagNameList, tag.Name)
}
tagMapSet := make(map[string]bool)
for _, memo := range memoList {
for _, tag := range findTagListFromMemoContent(memo.Content) {
if !slices.Contains(tagNameList, tag) {
tagMapSet[tag] = true
}
for _, memo := range memos {
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) {
tagMapSet[tag] = true
}
}
})
}
suggestions := []string{}
for tag := range tagMapSet {
......@@ -231,20 +248,24 @@ func (s *APIV2Service) convertTagFromStore(ctx context.Context, tag *store.Tag)
}, nil
}
var tagRegexp = regexp.MustCompile(`#([^\s#,]+)`)
func findTagListFromMemoContent(memoContent string) []string {
tagMapSet := make(map[string]bool)
matches := tagRegexp.FindAllStringSubmatch(memoContent, -1)
for _, v := range matches {
tagName := v[1]
tagMapSet[tagName] = true
}
tagList := []string{}
for tag := range tagMapSet {
tagList = append(tagList, tag)
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)
}
}
sort.Strings(tagList)
return tagList
}
......@@ -10,6 +10,9 @@ service TagService {
rpc UpsertTag(UpsertTagRequest) returns (UpsertTagResponse) {
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) {
option (google.api.http) = {get: "/api/v2/tags"};
}
......@@ -39,6 +42,13 @@ message UpsertTagResponse {
Tag tag = 1;
}
message BatchUpsertTagRequest {
repeated UpsertTagRequest requests = 1;
}
message BatchUpsertTagResponse {
}
message ListTagsRequest {
// The creator of tags.
// Format: users/{username}
......
......@@ -134,6 +134,8 @@
- [MemoService](#memos-api-v2-MemoService)
- [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)
- [DeleteTagResponse](#memos-api-v2-DeleteTagResponse)
- [GetTagSuggestionsRequest](#memos-api-v2-GetTagSuggestionsRequest)
......@@ -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>
### DeleteTagRequest
......@@ -2046,6 +2073,7 @@ Used internally for obfuscating the page token.
| Method Name | Request Type | Response Type | Description |
| ----------- | ------------ | ------------- | ------------|
| 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) | |
| RenameTag | [RenameTagRequest](#memos-api-v2-RenameTagRequest) | [RenameTagResponse](#memos-api-v2-RenameTagResponse) | |
| 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
}
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 (
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,
})
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) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
......@@ -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) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
......@@ -499,6 +582,8 @@ func RegisterTagServiceHandlerClient(ctx context.Context, mux *runtime.ServeMux,
var (
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_RenameTag_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v2", "tags"}, "rename"))
......@@ -511,6 +596,8 @@ var (
var (
forward_TagService_UpsertTag_0 = runtime.ForwardResponseMessage
forward_TagService_BatchUpsertTag_0 = runtime.ForwardResponseMessage
forward_TagService_ListTags_0 = runtime.ForwardResponseMessage
forward_TagService_RenameTag_0 = runtime.ForwardResponseMessage
......
......@@ -20,6 +20,7 @@ const _ = grpc.SupportPackageIsVersion7
const (
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_RenameTag_FullMethodName = "/memos.api.v2.TagService/RenameTag"
TagService_DeleteTag_FullMethodName = "/memos.api.v2.TagService/DeleteTag"
......@@ -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.
type TagServiceClient interface {
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)
RenameTag(ctx context.Context, in *RenameTagRequest, opts ...grpc.CallOption) (*RenameTagResponse, 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,
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) {
out := new(ListTagsResponse)
err := c.cc.Invoke(ctx, TagService_ListTags_FullMethodName, in, out, opts...)
......@@ -95,6 +106,7 @@ func (c *tagServiceClient) GetTagSuggestions(ctx context.Context, in *GetTagSugg
// for forward compatibility
type TagServiceServer interface {
UpsertTag(context.Context, *UpsertTagRequest) (*UpsertTagResponse, error)
BatchUpsertTag(context.Context, *BatchUpsertTagRequest) (*BatchUpsertTagResponse, error)
ListTags(context.Context, *ListTagsRequest) (*ListTagsResponse, error)
RenameTag(context.Context, *RenameTagRequest) (*RenameTagResponse, error)
DeleteTag(context.Context, *DeleteTagRequest) (*DeleteTagResponse, error)
......@@ -109,6 +121,9 @@ type UnimplementedTagServiceServer struct {
func (UnimplementedTagServiceServer) UpsertTag(context.Context, *UpsertTagRequest) (*UpsertTagResponse, error) {
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) {
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
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) {
in := new(ListTagsRequest)
if err := dec(in); err != nil {
......@@ -235,6 +268,10 @@ var TagService_ServiceDesc = grpc.ServiceDesc{
MethodName: "UpsertTag",
Handler: _TagService_UpsertTag_Handler,
},
{
MethodName: "BatchUpsertTag",
Handler: _TagService_BatchUpsertTag_Handler,
},
{
MethodName: "ListTags",
Handler: _TagService_ListTags_Handler,
......
......@@ -6,7 +6,7 @@ import useLocalStorage from "react-use/lib/useLocalStorage";
import { memoServiceClient } from "@/grpcweb";
import { TAB_SPACE_WIDTH, UNKNOWN_ID } from "@/helpers/consts";
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 { MemoRelation, MemoRelation_Type } from "@/types/proto/api/v2/memo_relation_service";
import { Memo, Visibility } from "@/types/proto/api/v2/memo_service";
......@@ -14,6 +14,7 @@ import { Resource } from "@/types/proto/api/v2/resource_service";
import { UserSetting } from "@/types/proto/api/v2/user_service";
import { useTranslate } from "@/utils/i18n";
import { convertVisibilityFromString, convertVisibilityToString } from "@/utils/memo";
import { extractTagsFromContent } from "@/utils/tag";
import showCreateResourceDialog from "../CreateResourceDialog";
import Icon from "../Icon";
import VisibilityIcon from "../VisibilityIcon";
......@@ -57,6 +58,7 @@ const MemoEditor = (props: Props) => {
const userStore = useUserStore();
const memoStore = useMemoStore();
const resourceStore = useResourceStore();
const tagStore = useTagStore();
const [state, setState] = useState<State>({
memoVisibility: Visibility.PRIVATE,
resourceList: [],
......@@ -326,6 +328,10 @@ const MemoEditor = (props: Props) => {
toast.error(error.details);
}
// Batch upsert tags.
const tags = extractTagsFromContent(content);
await tagStore.batchUpsertTag(tags);
setState((state) => {
return {
...state,
......
......@@ -25,6 +25,17 @@ export const useTagStore = () => {
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) => {
await tagServiceClient.deleteTag({
tag: {
......@@ -40,6 +51,7 @@ export const useTagStore = () => {
getState,
fetchTags,
upsertTag,
batchUpsertTag,
deleteTag,
};
};
import { Node } from "@/types/node";
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