mirror of
https://github.com/usememos/memos.git
synced 2024-10-26 11:40:26 +03:00
chore: implement search random memos
This commit is contained in:
parent
03c93785f4
commit
bb10bb200c
@ -134,16 +134,17 @@ func (s *APIV2Service) ListMemos(ctx context.Context, request *apiv2pb.ListMemos
|
||||
}
|
||||
|
||||
func (s *APIV2Service) SearchMemos(ctx context.Context, request *apiv2pb.SearchMemosRequest) (*apiv2pb.SearchMemosResponse, error) {
|
||||
defaultSearchLimit := 10
|
||||
memoFind := &store.FindMemo{
|
||||
// Exclude comments by default.
|
||||
ExcludeComments: true,
|
||||
Limit: &defaultSearchLimit,
|
||||
}
|
||||
if err := s.buildMemoFindWithFilter(ctx, memoFind, request.Filter); err != nil {
|
||||
err := s.buildMemoFindWithFilter(ctx, memoFind, request.Filter)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "failed to build find memos with filter")
|
||||
}
|
||||
|
||||
defaultSearchLimit := 10
|
||||
memoFind.Limit = &defaultSearchLimit
|
||||
memos, err := s.Store.ListMemos(ctx, memoFind)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "failed to search memos")
|
||||
@ -696,6 +697,12 @@ func (s *APIV2Service) buildMemoFindWithFilter(ctx context.Context, find *store.
|
||||
if filter.RowStatus != nil {
|
||||
find.RowStatus = filter.RowStatus
|
||||
}
|
||||
if filter.Random {
|
||||
find.Random = filter.Random
|
||||
}
|
||||
if filter.Limit != nil {
|
||||
find.Limit = filter.Limit
|
||||
}
|
||||
}
|
||||
|
||||
// If the user is not authenticated, only public memos are visible.
|
||||
@ -730,6 +737,8 @@ var SearchMemosFilterCELAttributes = []cel.EnvOption{
|
||||
cel.Variable("creator", cel.StringType),
|
||||
cel.Variable("uid", cel.StringType),
|
||||
cel.Variable("row_status", cel.StringType),
|
||||
cel.Variable("random", cel.BoolType),
|
||||
cel.Variable("limit", cel.IntType),
|
||||
}
|
||||
|
||||
type SearchMemosFilter struct {
|
||||
@ -741,6 +750,8 @@ type SearchMemosFilter struct {
|
||||
Creator *string
|
||||
UID *string
|
||||
RowStatus *store.RowStatus
|
||||
Random bool
|
||||
Limit *int
|
||||
}
|
||||
|
||||
func parseSearchMemosFilter(expression string) (*SearchMemosFilter, error) {
|
||||
@ -798,6 +809,12 @@ func findSearchMemosField(callExpr *expr.Expr_Call, filter *SearchMemosFilter) {
|
||||
} else if idExpr.Name == "row_status" {
|
||||
rowStatus := store.RowStatus(callExpr.Args[1].GetConstExpr().GetStringValue())
|
||||
filter.RowStatus = &rowStatus
|
||||
} else if idExpr.Name == "random" {
|
||||
value := callExpr.Args[1].GetConstExpr().GetBoolValue()
|
||||
filter.Random = value
|
||||
} else if idExpr.Name == "limit" {
|
||||
limit := int(callExpr.Args[1].GetConstExpr().GetInt64Value())
|
||||
filter.Limit = &limit
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -91,6 +91,9 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
|
||||
orders = append(orders, "`created_ts` DESC")
|
||||
}
|
||||
orders = append(orders, "`id` DESC")
|
||||
if find.Random {
|
||||
orders = append(orders, "RAND()")
|
||||
}
|
||||
|
||||
fields := []string{
|
||||
"`memo`.`id` AS `id`",
|
||||
|
@ -82,6 +82,9 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
|
||||
orders = append(orders, "created_ts DESC")
|
||||
}
|
||||
orders = append(orders, "id DESC")
|
||||
if find.Random {
|
||||
orders = append(orders, "RAND()")
|
||||
}
|
||||
|
||||
fields := []string{
|
||||
`memo.id AS id`,
|
||||
|
@ -71,16 +71,19 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
|
||||
where = append(where, "`parent_id` IS NULL")
|
||||
}
|
||||
|
||||
orders := []string{}
|
||||
orderBy := []string{}
|
||||
if find.OrderByPinned {
|
||||
orders = append(orders, "`pinned` DESC")
|
||||
orderBy = append(orderBy, "`pinned` DESC")
|
||||
}
|
||||
if find.OrderByUpdatedTs {
|
||||
orders = append(orders, "`updated_ts` DESC")
|
||||
orderBy = append(orderBy, "`updated_ts` DESC")
|
||||
} else {
|
||||
orders = append(orders, "`created_ts` DESC")
|
||||
orderBy = append(orderBy, "`created_ts` DESC")
|
||||
}
|
||||
orderBy = append(orderBy, "`id` DESC")
|
||||
if find.Random {
|
||||
orderBy = []string{"RANDOM()"}
|
||||
}
|
||||
orders = append(orders, "`id` DESC")
|
||||
|
||||
fields := []string{
|
||||
"`memo`.`id` AS `id`",
|
||||
@ -101,7 +104,7 @@ func (d *DB) ListMemos(ctx context.Context, find *store.FindMemo) ([]*store.Memo
|
||||
"LEFT JOIN `memo_organizer` ON `memo`.`id` = `memo_organizer`.`memo_id` AND `memo`.`creator_id` = `memo_organizer`.`user_id` " +
|
||||
"LEFT JOIN `memo_relation` ON `memo`.`id` = `memo_relation`.`memo_id` AND `memo_relation`.`type` = \"COMMENT\" " +
|
||||
"WHERE " + strings.Join(where, " AND ") + " " +
|
||||
"ORDER BY " + strings.Join(orders, ", ")
|
||||
"ORDER BY " + strings.Join(orderBy, ", ")
|
||||
if find.Limit != nil {
|
||||
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
|
||||
if find.Offset != nil {
|
||||
|
@ -69,6 +69,7 @@ type FindMemo struct {
|
||||
VisibilityList []Visibility
|
||||
ExcludeContent bool
|
||||
ExcludeComments bool
|
||||
Random bool
|
||||
|
||||
// Pagination
|
||||
Limit *int
|
||||
|
Loading…
Reference in New Issue
Block a user