mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
fix: list memo relations
This commit is contained in:
@@ -70,9 +70,21 @@ func (s *APIV1Service) ListMemoRelations(ctx context.Context, request *v1pb.List
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.Internal, "failed to get memo")
|
return nil, status.Errorf(codes.Internal, "failed to get memo")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentUser, err := s.GetCurrentUser(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, status.Errorf(codes.Internal, "failed to get user")
|
||||||
|
}
|
||||||
|
var memoFilter string
|
||||||
|
if currentUser == nil {
|
||||||
|
memoFilter = `visibility == "PUBLIC"`
|
||||||
|
} else {
|
||||||
|
memoFilter = fmt.Sprintf(`creator_id == %d || visibility in ["PUBLIC", "PROTECTED"]`, currentUser.ID)
|
||||||
|
}
|
||||||
relationList := []*v1pb.MemoRelation{}
|
relationList := []*v1pb.MemoRelation{}
|
||||||
tempList, err := s.Store.ListMemoRelations(ctx, &store.FindMemoRelation{
|
tempList, err := s.Store.ListMemoRelations(ctx, &store.FindMemoRelation{
|
||||||
MemoID: &memo.ID,
|
MemoID: &memo.ID,
|
||||||
|
MemoFilter: &memoFilter,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -86,6 +98,7 @@ func (s *APIV1Service) ListMemoRelations(ctx context.Context, request *v1pb.List
|
|||||||
}
|
}
|
||||||
tempList, err = s.Store.ListMemoRelations(ctx, &store.FindMemoRelation{
|
tempList, err = s.Store.ListMemoRelations(ctx, &store.FindMemoRelation{
|
||||||
RelatedMemoID: &memo.ID,
|
RelatedMemoID: &memo.ID,
|
||||||
|
MemoFilter: &memoFilter,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@@ -2,8 +2,10 @@ package sqlite
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/usememos/memos/plugin/filter"
|
||||||
"github.com/usememos/memos/store"
|
"github.com/usememos/memos/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -46,6 +48,25 @@ func (d *DB) ListMemoRelations(ctx context.Context, find *store.FindMemoRelation
|
|||||||
if find.Type != nil {
|
if find.Type != nil {
|
||||||
where, args = append(where, "type = ?"), append(args, find.Type)
|
where, args = append(where, "type = ?"), append(args, find.Type)
|
||||||
}
|
}
|
||||||
|
if find.MemoFilter != nil {
|
||||||
|
// Parse filter string and return the parsed expression.
|
||||||
|
// The filter string should be a CEL expression.
|
||||||
|
parsedExpr, err := filter.Parse(*find.MemoFilter, filter.MemoFilterCELAttributes...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
convertCtx := filter.NewConvertContext()
|
||||||
|
// ConvertExprToSQL converts the parsed expression to a SQL condition string.
|
||||||
|
if err := d.ConvertExprToSQL(convertCtx, parsedExpr.GetExpr()); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
condition := convertCtx.Buffer.String()
|
||||||
|
if condition != "" {
|
||||||
|
where = append(where, fmt.Sprintf("memo_id IN (SELECT id FROM memo WHERE %s)", condition))
|
||||||
|
where = append(where, fmt.Sprintf("related_memo_id IN (SELECT id FROM memo WHERE %s)", condition))
|
||||||
|
args = append(args, append(convertCtx.Args, convertCtx.Args...)...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rows, err := d.db.QueryContext(ctx, `
|
rows, err := d.db.QueryContext(ctx, `
|
||||||
SELECT
|
SELECT
|
||||||
|
@@ -23,6 +23,7 @@ type FindMemoRelation struct {
|
|||||||
MemoID *int32
|
MemoID *int32
|
||||||
RelatedMemoID *int32
|
RelatedMemoID *int32
|
||||||
Type *MemoRelationType
|
Type *MemoRelationType
|
||||||
|
MemoFilter *string
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeleteMemoRelation struct {
|
type DeleteMemoRelation struct {
|
||||||
|
Reference in New Issue
Block a user