mirror of
https://github.com/usememos/memos.git
synced 2026-11-19 10:29:11 +01:00
9e3a164dff
Move memo content size into usage limits, with 8 KiB content and 32 MiB upload defaults when settings are absent. Represent stored rule targets with all_users or a positive user_id, and use that same schema for deployment configuration. Rename the memo count metric, remove reserved proto declarations, and renumber fields and enums for the unreleased schema. Synchronize enforcement, settings UI, generated clients, and tests.
94 lines
2.9 KiB
Go
94 lines
2.9 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/usememos/memos/core/memopayload"
|
|
apipb "github.com/usememos/memos/proto/gen/api"
|
|
"github.com/usememos/memos/store"
|
|
)
|
|
|
|
func mapMemoCreateError(err error, uid, operation string) error {
|
|
errMessage := err.Error()
|
|
if strings.Contains(errMessage, "UNIQUE constraint failed") ||
|
|
strings.Contains(errMessage, "duplicate key") ||
|
|
strings.Contains(errMessage, "Duplicate entry") {
|
|
return status.Errorf(codes.AlreadyExists, "memo with ID %q already exists", uid)
|
|
}
|
|
return mapMemoWriteError(err, operation)
|
|
}
|
|
|
|
type preparedMemoCreate struct {
|
|
memo *store.Memo
|
|
attachments *preparedMemoAttachments
|
|
requiredAttachmentIDs []int32
|
|
referenceRelations []*store.MemoRelation
|
|
}
|
|
|
|
// prepareMemoCreate validates and prepares the fields shared by top-level
|
|
// memo and comment creation. It does not write database or external state.
|
|
func (s *APIService) prepareMemoCreate(ctx context.Context, user *store.User, input *apipb.Memo, uid string) (*preparedMemoCreate, error) {
|
|
if user == nil || input == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "memo is required")
|
|
}
|
|
visibility, err := validateCreateMemoVisibility(input.Visibility)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
memo := &store.Memo{
|
|
UID: uid,
|
|
CreatorID: user.ID,
|
|
Content: input.Content,
|
|
Visibility: visibility,
|
|
}
|
|
if input.Space != nil && input.GetSpace() != "" {
|
|
space, err := s.resolveSpaceForMemoPlacement(ctx, input.GetSpace(), user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
memo.SpaceID = &space.ID
|
|
}
|
|
if memo.Visibility == store.SpaceAudience && memo.SpaceID == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "SPACE visibility requires a space")
|
|
}
|
|
if input.CreateTime != nil && input.CreateTime.IsValid() {
|
|
memo.CreatedTs = input.CreateTime.AsTime().Unix()
|
|
}
|
|
if input.UpdateTime != nil && input.UpdateTime.IsValid() {
|
|
memo.UpdatedTs = input.UpdateTime.AsTime().Unix()
|
|
}
|
|
|
|
if err := s.checkMemoContentQuota(ctx, user.ID, memo.Content); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := memopayload.RebuildMemoPayload(ctx, memo, s.MarkdownService); err != nil {
|
|
return nil, status.Errorf(codes.Internal, "failed to rebuild memo payload: %v", err)
|
|
}
|
|
if input.Location != nil {
|
|
memo.Payload.Location = convertLocationToStore(input.Location)
|
|
}
|
|
|
|
attachments, err := s.prepareMemoAttachments(ctx, user, memo, input.Attachments)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
requiredAttachmentIDs, err := s.resolveMemoAttachmentReferences(memo.Content, attachments.normalized)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
relations, err := s.prepareMemoRelations(ctx, memo, input.Relations)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &preparedMemoCreate{
|
|
memo: memo,
|
|
attachments: attachments,
|
|
requiredAttachmentIDs: requiredAttachmentIDs,
|
|
referenceRelations: relations,
|
|
}, nil
|
|
}
|