memos/server/router/api/v1/common.go

60 lines
1.4 KiB
Go
Raw Normal View History

2024-04-28 00:44:29 +08:00
package v1
import (
2024-01-27 11:14:17 +08:00
"encoding/base64"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
2023-09-14 20:16:17 +08:00
2024-04-28 00:44:29 +08:00
v1pb "github.com/usememos/memos/proto/gen/api/v1"
"github.com/usememos/memos/store"
)
2024-04-28 00:44:29 +08:00
func convertRowStatusFromStore(rowStatus store.RowStatus) v1pb.RowStatus {
switch rowStatus {
case store.Normal:
2024-04-28 00:44:29 +08:00
return v1pb.RowStatus_ACTIVE
case store.Archived:
2024-04-28 00:44:29 +08:00
return v1pb.RowStatus_ARCHIVED
default:
2024-04-28 00:44:29 +08:00
return v1pb.RowStatus_ROW_STATUS_UNSPECIFIED
}
}
2023-09-10 18:56:24 +08:00
2024-04-28 00:44:29 +08:00
func convertRowStatusToStore(rowStatus v1pb.RowStatus) store.RowStatus {
2023-09-10 18:56:24 +08:00
switch rowStatus {
2024-04-28 00:44:29 +08:00
case v1pb.RowStatus_ACTIVE:
2023-09-10 18:56:24 +08:00
return store.Normal
2024-04-28 00:44:29 +08:00
case v1pb.RowStatus_ARCHIVED:
2023-09-10 18:56:24 +08:00
return store.Archived
default:
return store.Normal
}
}
2023-09-14 20:16:17 +08:00
2024-01-27 11:14:17 +08:00
func getPageToken(limit int, offset int) (string, error) {
2024-04-28 00:44:29 +08:00
return marshalPageToken(&v1pb.PageToken{
2024-01-27 11:14:17 +08:00
Limit: int32(limit),
Offset: int32(offset),
})
}
2024-04-28 00:44:29 +08:00
func marshalPageToken(pageToken *v1pb.PageToken) (string, error) {
2024-01-27 11:14:17 +08:00
b, err := proto.Marshal(pageToken)
if err != nil {
return "", errors.Wrapf(err, "failed to marshal page token")
}
return base64.StdEncoding.EncodeToString(b), nil
}
2024-04-28 00:44:29 +08:00
func unmarshalPageToken(s string, pageToken *v1pb.PageToken) error {
2024-01-27 11:14:17 +08:00
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return errors.Wrapf(err, "failed to decode page token")
}
if err := proto.Unmarshal(b, pageToken); err != nil {
return errors.Wrapf(err, "failed to unmarshal page token")
}
return nil
}