activity_service.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package v1
  2. import (
  3. "context"
  4. "time"
  5. "google.golang.org/grpc/codes"
  6. "google.golang.org/grpc/status"
  7. "google.golang.org/protobuf/types/known/timestamppb"
  8. v1pb "github.com/usememos/memos/proto/gen/api/v1"
  9. storepb "github.com/usememos/memos/proto/gen/store"
  10. "github.com/usememos/memos/store"
  11. )
  12. func (s *APIV1Service) GetActivity(ctx context.Context, request *v1pb.GetActivityRequest) (*v1pb.Activity, error) {
  13. activity, err := s.Store.GetActivity(ctx, &store.FindActivity{
  14. ID: &request.Id,
  15. })
  16. if err != nil {
  17. return nil, status.Errorf(codes.Internal, "failed to get activity: %v", err)
  18. }
  19. activityMessage, err := s.convertActivityFromStore(ctx, activity)
  20. if err != nil {
  21. return nil, status.Errorf(codes.Internal, "failed to convert activity from store: %v", err)
  22. }
  23. return activityMessage, nil
  24. }
  25. func (*APIV1Service) convertActivityFromStore(_ context.Context, activity *store.Activity) (*v1pb.Activity, error) {
  26. return &v1pb.Activity{
  27. Id: activity.ID,
  28. CreatorId: activity.CreatorID,
  29. Type: activity.Type.String(),
  30. Level: activity.Level.String(),
  31. CreateTime: timestamppb.New(time.Unix(activity.CreatedTs, 0)),
  32. Payload: convertActivityPayloadFromStore(activity.Payload),
  33. }, nil
  34. }
  35. func convertActivityPayloadFromStore(payload *storepb.ActivityPayload) *v1pb.ActivityPayload {
  36. v2Payload := &v1pb.ActivityPayload{}
  37. if payload.MemoComment != nil {
  38. v2Payload.MemoComment = &v1pb.ActivityMemoCommentPayload{
  39. MemoId: payload.MemoComment.MemoId,
  40. RelatedMemoId: payload.MemoComment.RelatedMemoId,
  41. }
  42. }
  43. if payload.VersionUpdate != nil {
  44. v2Payload.VersionUpdate = &v1pb.ActivityVersionUpdatePayload{
  45. Version: payload.VersionUpdate.Version,
  46. }
  47. }
  48. return v2Payload
  49. }