Commit 79bb3253 authored by Steven's avatar Steven

chore: add activity service

parent 18107248
package v2
import (
"context"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/store"
)
func (s *APIV2Service) GetActivity(ctx context.Context, request *apiv2pb.GetActivityRequest) (*apiv2pb.GetActivityResponse, error) {
activity, err := s.Store.GetActivity(ctx, &store.FindActivity{
ID: &request.Id,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get activity: %v", err)
}
activityMessage, err := s.convertActivityFromStore(ctx, activity)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to convert activity from store: %v", err)
}
return &apiv2pb.GetActivityResponse{
Activity: activityMessage,
}, nil
}
func (*APIV2Service) convertActivityFromStore(_ context.Context, activity *store.Activity) (*apiv2pb.Activity, error) {
return &apiv2pb.Activity{
Id: activity.ID,
CreatorId: activity.CreatorID,
Type: activity.Type.String(),
Level: activity.Level.String(),
CreateTime: timestamppb.New(time.Unix(activity.CreatedTs, 0)),
Payload: convertActivityPayloadFromStore(activity.Payload),
}, nil
}
func convertActivityPayloadFromStore(payload *storepb.ActivityPayload) *apiv2pb.ActivityPayload {
v2Payload := &apiv2pb.ActivityPayload{}
if payload.MemoComment != nil {
v2Payload.MemoComment = &apiv2pb.ActivityMemoCommentPayload{
MemoId: payload.MemoComment.MemoId,
RelatedMemoId: payload.MemoComment.RelatedMemoId,
}
}
return v2Payload
}
......@@ -23,6 +23,7 @@ type APIV2Service struct {
apiv2pb.UnimplementedResourceServiceServer
apiv2pb.UnimplementedTagServiceServer
apiv2pb.UnimplementedInboxServiceServer
apiv2pb.UnimplementedActivityServiceServer
Secret string
Profile *profile.Profile
......@@ -54,6 +55,7 @@ func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store
apiv2pb.RegisterTagServiceServer(grpcServer, apiv2Service)
apiv2pb.RegisterResourceServiceServer(grpcServer, apiv2Service)
apiv2pb.RegisterInboxServiceServer(grpcServer, apiv2Service)
apiv2pb.RegisterActivityServiceServer(grpcServer, apiv2Service)
reflection.Register(grpcServer)
return apiv2Service
......@@ -95,6 +97,9 @@ func (s *APIV2Service) RegisterGateway(ctx context.Context, e *echo.Echo) error
if err := apiv2pb.RegisterInboxServiceHandler(context.Background(), gwMux, conn); err != nil {
return err
}
if err := apiv2pb.RegisterActivityServiceHandler(context.Background(), gwMux, conn); err != nil {
return err
}
e.Any("/api/v2/*", echo.WrapHandler(gwMux))
// GRPC web proxy.
......
syntax = "proto3";
package memos.api.v2;
import "google/api/annotations.proto";
import "google/protobuf/timestamp.proto";
option go_package = "gen/api/v2";
service ActivityService {
rpc GetActivity(GetActivityRequest) returns (GetActivityResponse) {
option (google.api.http) = {get: "/v2/activities"};
}
}
message Activity {
int32 id = 1;
int32 creator_id = 2;
string type = 3;
string level = 4;
google.protobuf.Timestamp create_time = 5;
ActivityPayload payload = 6;
}
message ActivityMemoCommentPayload {
int32 memo_id = 1;
int32 related_memo_id = 2;
}
message ActivityPayload {
ActivityMemoCommentPayload memo_comment = 1;
}
message GetActivityRequest {
int32 id = 1;
}
message GetActivityResponse {
Activity activity = 1;
}
......@@ -3,6 +3,15 @@
## Table of Contents
- [api/v2/activity_service.proto](#api_v2_activity_service-proto)
- [Activity](#memos-api-v2-Activity)
- [ActivityMemoCommentPayload](#memos-api-v2-ActivityMemoCommentPayload)
- [ActivityPayload](#memos-api-v2-ActivityPayload)
- [GetActivityRequest](#memos-api-v2-GetActivityRequest)
- [GetActivityResponse](#memos-api-v2-GetActivityResponse)
- [ActivityService](#memos-api-v2-ActivityService)
- [api/v2/common.proto](#api_v2_common-proto)
- [RowStatus](#memos-api-v2-RowStatus)
......@@ -94,6 +103,113 @@
<a name="api_v2_activity_service-proto"></a>
<p align="right"><a href="#top">Top</a></p>
## api/v2/activity_service.proto
<a name="memos-api-v2-Activity"></a>
### Activity
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| id | [int32](#int32) | | |
| creator_id | [int32](#int32) | | |
| type | [string](#string) | | |
| level | [string](#string) | | |
| create_time | [google.protobuf.Timestamp](#google-protobuf-Timestamp) | | |
| payload | [ActivityPayload](#memos-api-v2-ActivityPayload) | | |
<a name="memos-api-v2-ActivityMemoCommentPayload"></a>
### ActivityMemoCommentPayload
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| memo_id | [int32](#int32) | | |
| related_memo_id | [int32](#int32) | | |
<a name="memos-api-v2-ActivityPayload"></a>
### ActivityPayload
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| memo_comment | [ActivityMemoCommentPayload](#memos-api-v2-ActivityMemoCommentPayload) | | |
<a name="memos-api-v2-GetActivityRequest"></a>
### GetActivityRequest
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| id | [int32](#int32) | | |
<a name="memos-api-v2-GetActivityResponse"></a>
### GetActivityResponse
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| activity | [Activity](#memos-api-v2-Activity) | | |
<a name="memos-api-v2-ActivityService"></a>
### ActivityService
| Method Name | Request Type | Response Type | Description |
| ----------- | ------------ | ------------- | ------------|
| GetActivity | [GetActivityRequest](#memos-api-v2-GetActivityRequest) | [GetActivityResponse](#memos-api-v2-GetActivityResponse) | |
<a name="api_v2_common-proto"></a>
<p align="right"><a href="#top">Top</a></p>
......
This diff is collapsed.
// Code generated by protoc-gen-grpc-gateway. DO NOT EDIT.
// source: api/v2/activity_service.proto
/*
Package apiv2 is a reverse proxy.
It translates gRPC into RESTful JSON APIs.
*/
package apiv2
import (
"context"
"io"
"net/http"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/grpc-ecosystem/grpc-gateway/v2/utilities"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
)
// Suppress "imported and not used" errors
var _ codes.Code
var _ io.Reader
var _ status.Status
var _ = runtime.String
var _ = utilities.NewDoubleArray
var _ = metadata.Join
var (
filter_ActivityService_GetActivity_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)}
)
func request_ActivityService_GetActivity_0(ctx context.Context, marshaler runtime.Marshaler, client ActivityServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq GetActivityRequest
var metadata runtime.ServerMetadata
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_ActivityService_GetActivity_0); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
msg, err := client.GetActivity(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD))
return msg, metadata, err
}
func local_request_ActivityService_GetActivity_0(ctx context.Context, marshaler runtime.Marshaler, server ActivityServiceServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq GetActivityRequest
var metadata runtime.ServerMetadata
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_ActivityService_GetActivity_0); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
msg, err := server.GetActivity(ctx, &protoReq)
return msg, metadata, err
}
// RegisterActivityServiceHandlerServer registers the http handlers for service ActivityService to "mux".
// UnaryRPC :call ActivityServiceServer directly.
// StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906.
// Note that using this registration option will cause many gRPC library features to stop working. Consider using RegisterActivityServiceHandlerFromEndpoint instead.
func RegisterActivityServiceHandlerServer(ctx context.Context, mux *runtime.ServeMux, server ActivityServiceServer) error {
mux.Handle("GET", pattern_ActivityService_GetActivity_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
var stream runtime.ServerTransportStream
ctx = grpc.NewContextWithServerTransportStream(ctx, &stream)
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
var err error
var annotatedContext context.Context
annotatedContext, err = runtime.AnnotateIncomingContext(ctx, mux, req, "/memos.api.v2.ActivityService/GetActivity", runtime.WithHTTPPathPattern("/v2/activities"))
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
resp, md, err := local_request_ActivityService_GetActivity_0(annotatedContext, inboundMarshaler, server, req, pathParams)
md.HeaderMD, md.TrailerMD = metadata.Join(md.HeaderMD, stream.Header()), metadata.Join(md.TrailerMD, stream.Trailer())
annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md)
if err != nil {
runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err)
return
}
forward_ActivityService_GetActivity_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
})
return nil
}
// RegisterActivityServiceHandlerFromEndpoint is same as RegisterActivityServiceHandler but
// automatically dials to "endpoint" and closes the connection when "ctx" gets done.
func RegisterActivityServiceHandlerFromEndpoint(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error) {
conn, err := grpc.DialContext(ctx, endpoint, opts...)
if err != nil {
return err
}
defer func() {
if err != nil {
if cerr := conn.Close(); cerr != nil {
grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr)
}
return
}
go func() {
<-ctx.Done()
if cerr := conn.Close(); cerr != nil {
grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr)
}
}()
}()
return RegisterActivityServiceHandler(ctx, mux, conn)
}
// RegisterActivityServiceHandler registers the http handlers for service ActivityService to "mux".
// The handlers forward requests to the grpc endpoint over "conn".
func RegisterActivityServiceHandler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error {
return RegisterActivityServiceHandlerClient(ctx, mux, NewActivityServiceClient(conn))
}
// RegisterActivityServiceHandlerClient registers the http handlers for service ActivityService
// to "mux". The handlers forward requests to the grpc endpoint over the given implementation of "ActivityServiceClient".
// Note: the gRPC framework executes interceptors within the gRPC handler. If the passed in "ActivityServiceClient"
// doesn't go through the normal gRPC flow (creating a gRPC client etc.) then it will be up to the passed in
// "ActivityServiceClient" to call the correct interceptors.
func RegisterActivityServiceHandlerClient(ctx context.Context, mux *runtime.ServeMux, client ActivityServiceClient) error {
mux.Handle("GET", pattern_ActivityService_GetActivity_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
var err error
var annotatedContext context.Context
annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/memos.api.v2.ActivityService/GetActivity", runtime.WithHTTPPathPattern("/v2/activities"))
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
resp, md, err := request_ActivityService_GetActivity_0(annotatedContext, inboundMarshaler, client, req, pathParams)
annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md)
if err != nil {
runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err)
return
}
forward_ActivityService_GetActivity_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
})
return nil
}
var (
pattern_ActivityService_GetActivity_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1}, []string{"v2", "activities"}, ""))
)
var (
forward_ActivityService_GetActivity_0 = runtime.ForwardResponseMessage
)
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.3.0
// - protoc (unknown)
// source: api/v2/activity_service.proto
package apiv2
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
const (
ActivityService_GetActivity_FullMethodName = "/memos.api.v2.ActivityService/GetActivity"
)
// ActivityServiceClient is the client API for ActivityService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type ActivityServiceClient interface {
GetActivity(ctx context.Context, in *GetActivityRequest, opts ...grpc.CallOption) (*GetActivityResponse, error)
}
type activityServiceClient struct {
cc grpc.ClientConnInterface
}
func NewActivityServiceClient(cc grpc.ClientConnInterface) ActivityServiceClient {
return &activityServiceClient{cc}
}
func (c *activityServiceClient) GetActivity(ctx context.Context, in *GetActivityRequest, opts ...grpc.CallOption) (*GetActivityResponse, error) {
out := new(GetActivityResponse)
err := c.cc.Invoke(ctx, ActivityService_GetActivity_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// ActivityServiceServer is the server API for ActivityService service.
// All implementations must embed UnimplementedActivityServiceServer
// for forward compatibility
type ActivityServiceServer interface {
GetActivity(context.Context, *GetActivityRequest) (*GetActivityResponse, error)
mustEmbedUnimplementedActivityServiceServer()
}
// UnimplementedActivityServiceServer must be embedded to have forward compatible implementations.
type UnimplementedActivityServiceServer struct {
}
func (UnimplementedActivityServiceServer) GetActivity(context.Context, *GetActivityRequest) (*GetActivityResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetActivity not implemented")
}
func (UnimplementedActivityServiceServer) mustEmbedUnimplementedActivityServiceServer() {}
// UnsafeActivityServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to ActivityServiceServer will
// result in compilation errors.
type UnsafeActivityServiceServer interface {
mustEmbedUnimplementedActivityServiceServer()
}
func RegisterActivityServiceServer(s grpc.ServiceRegistrar, srv ActivityServiceServer) {
s.RegisterService(&ActivityService_ServiceDesc, srv)
}
func _ActivityService_GetActivity_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetActivityRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ActivityServiceServer).GetActivity(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: ActivityService_GetActivity_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ActivityServiceServer).GetActivity(ctx, req.(*GetActivityRequest))
}
return interceptor(ctx, in, info, handler)
}
// ActivityService_ServiceDesc is the grpc.ServiceDesc for ActivityService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var ActivityService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "memos.api.v2.ActivityService",
HandlerType: (*ActivityServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "GetActivity",
Handler: _ActivityService_GetActivity_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "api/v2/activity_service.proto",
}
......@@ -50,3 +50,14 @@ func (s *Store) CreateActivity(ctx context.Context, create *Activity) (*Activity
func (s *Store) ListActivities(ctx context.Context, find *FindActivity) ([]*Activity, error) {
return s.driver.ListActivities(ctx, find)
}
func (s *Store) GetActivity(ctx context.Context, find *FindActivity) (*Activity, error) {
list, err := s.ListActivities(ctx, find)
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, nil
}
return list[0], nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment