✨ Internal grpc apis
This commit is contained in:
		
							
								
								
									
										104
									
								
								pkg/internal/grpc/attachment.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								pkg/internal/grpc/attachment.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,104 @@ | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"git.solsynth.dev/hypernet/paperclip/pkg/internal/database" | ||||
| 	"git.solsynth.dev/hypernet/paperclip/pkg/internal/models" | ||||
| 	"git.solsynth.dev/hypernet/paperclip/pkg/proto" | ||||
| 	"github.com/samber/lo" | ||||
| 	"google.golang.org/grpc/codes" | ||||
| 	"google.golang.org/grpc/status" | ||||
| ) | ||||
|  | ||||
| func (v *Server) GetAttachment(ctx context.Context, request *proto.GetAttachmentRequest) (*proto.GetAttachmentResponse, error) { | ||||
| 	tx := database.C | ||||
| 	if request.Id != nil { | ||||
| 		tx = tx.Where("id = ?", request.Id) | ||||
| 	} else if request.Rid != nil { | ||||
| 		tx = tx.Where("rid = ?", request.Rid) | ||||
| 	} else { | ||||
| 		return nil, status.Error(codes.InvalidArgument, "you must provide id or random id") | ||||
| 	} | ||||
|  | ||||
| 	var attachment models.Attachment | ||||
| 	if err := tx.First(&attachment).Error; err != nil { | ||||
| 		return nil, status.Error(codes.NotFound, "attachment not found") | ||||
| 	} | ||||
|  | ||||
| 	return &proto.GetAttachmentResponse{ | ||||
| 		Attachment: lo.ToPtr(attachment).ToAttachmentInfo(), | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (v *Server) ListAttachment(ctx context.Context, request *proto.ListAttachmentRequest) (*proto.ListAttachmentResponse, error) { | ||||
| 	tx := database.C | ||||
| 	if len(request.Id) == 0 && len(request.Rid) == 0 { | ||||
| 		return nil, status.Error(codes.InvalidArgument, "you must provide at least one id or random id") | ||||
| 	} | ||||
| 	if len(request.Id) > 0 { | ||||
| 		tx = tx.Where("id IN ?", request.Id) | ||||
| 	} | ||||
| 	if len(request.Rid) > 0 { | ||||
| 		tx = tx.Where("rid IN ?", request.Rid) | ||||
| 	} | ||||
|  | ||||
| 	attachments := make([]models.Attachment, 0) | ||||
| 	err := tx.Find(&attachments).Error | ||||
| 	if err != nil { | ||||
| 		return nil, status.Error(codes.Internal, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	return &proto.ListAttachmentResponse{ | ||||
| 		Attachments: lo.Map(attachments, func(v models.Attachment, _ int) *proto.AttachmentInfo { | ||||
| 			return v.ToAttachmentInfo() | ||||
| 		}), | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (v *Server) UpdateVisibility(ctx context.Context, request *proto.UpdateVisibilityRequest) (*proto.UpdateVisibilityResponse, error) { | ||||
| 	tx := database.C | ||||
| 	if len(request.Id) == 0 && len(request.Rid) == 0 { | ||||
| 		return nil, status.Error(codes.InvalidArgument, "you must provide at least one id or random id") | ||||
| 	} | ||||
| 	if len(request.Id) > 0 { | ||||
| 		tx = tx.Where("id IN ?", request.Id) | ||||
| 	} | ||||
| 	if len(request.Rid) > 0 { | ||||
| 		tx = tx.Where("rid IN ?", request.Rid) | ||||
| 	} | ||||
|  | ||||
| 	var rowsAffected int64 | ||||
| 	if err := tx.Update("is_indexable", request.IsIndexable).Error; err != nil { | ||||
| 		return nil, status.Error(codes.Internal, err.Error()) | ||||
| 	} else { | ||||
| 		rowsAffected = tx.RowsAffected | ||||
| 	} | ||||
|  | ||||
| 	return &proto.UpdateVisibilityResponse{ | ||||
| 		Count: int32(rowsAffected), | ||||
| 	}, nil | ||||
| } | ||||
|  | ||||
| func (v *Server) DeleteAttachment(ctx context.Context, request *proto.DeleteAttachmentRequest) (*proto.DeleteAttachmentResponse, error) { | ||||
| 	tx := database.C | ||||
| 	if len(request.Id) == 0 && len(request.Rid) == 0 { | ||||
| 		return nil, status.Error(codes.InvalidArgument, "you must provide at least one id or random id") | ||||
| 	} | ||||
| 	if len(request.Id) > 0 { | ||||
| 		tx = tx.Where("id IN ?", request.Id) | ||||
| 	} | ||||
| 	if len(request.Rid) > 0 { | ||||
| 		tx = tx.Where("rid IN ?", request.Rid) | ||||
| 	} | ||||
|  | ||||
| 	var rowsAffected int64 | ||||
| 	if err := tx.Delete(&models.Attachment{}).Error; err != nil { | ||||
| 		return nil, status.Error(codes.Internal, err.Error()) | ||||
| 	} else { | ||||
| 		rowsAffected = tx.RowsAffected | ||||
| 	} | ||||
|  | ||||
| 	return &proto.DeleteAttachmentResponse{ | ||||
| 		Count: int32(rowsAffected), | ||||
| 	}, nil | ||||
| } | ||||
| @@ -1,9 +1,10 @@ | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"git.solsynth.dev/hypernet/paperclip/pkg/proto" | ||||
| 	"net" | ||||
|  | ||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/proto" | ||||
| 	nproto "git.solsynth.dev/hypernet/nexus/pkg/proto" | ||||
| 	"github.com/spf13/viper" | ||||
| 	"google.golang.org/grpc" | ||||
| 	health "google.golang.org/grpc/health/grpc_health_v1" | ||||
| @@ -11,7 +12,8 @@ import ( | ||||
| ) | ||||
|  | ||||
| type Server struct { | ||||
| 	proto.UnimplementedDirectoryServiceServer | ||||
| 	nproto.UnimplementedDirectoryServiceServer | ||||
| 	proto.UnimplementedAttachmentServiceServer | ||||
| 	health.UnimplementedHealthServer | ||||
|  | ||||
| 	srv *grpc.Server | ||||
| @@ -22,7 +24,8 @@ func NewGrpc() *Server { | ||||
| 		srv: grpc.NewServer(), | ||||
| 	} | ||||
|  | ||||
| 	proto.RegisterDirectoryServiceServer(server.srv, server) | ||||
| 	nproto.RegisterDirectoryServiceServer(server.srv, server) | ||||
| 	proto.RegisterAttachmentServiceServer(server.srv, server) | ||||
| 	health.RegisterHealthServer(server.srv, server) | ||||
|  | ||||
| 	reflection.Register(server.srv) | ||||
|   | ||||
| @@ -3,6 +3,8 @@ package models | ||||
| import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
| 	"git.solsynth.dev/hypernet/paperclip/pkg/proto" | ||||
| 	"strconv" | ||||
| 	"time" | ||||
|  | ||||
| 	"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda" | ||||
| @@ -77,6 +79,23 @@ type Attachment struct { | ||||
| 	IsMature   bool              `json:"is_mature" gorm:"-"` | ||||
| } | ||||
|  | ||||
| func (v *Attachment) ToAttachmentInfo() *proto.AttachmentInfo { | ||||
| 	if v == nil { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	return &proto.AttachmentInfo{ | ||||
| 		Id:          v.Uuid, | ||||
| 		Rid:         v.Rid, | ||||
| 		Name:        v.Name, | ||||
| 		Type:        v.MimeType, | ||||
| 		Size:        strconv.FormatInt(v.Size, 10), | ||||
| 		Hash:        v.HashCode, | ||||
| 		Mime:        v.MimeType, | ||||
| 		IsIndexable: v.IsIndexable, | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func (v *Attachment) AfterUpdate(tx *gorm.DB) error { | ||||
| 	cacheManager := cache.New[any](localCache.S) | ||||
| 	marshal := marshaler.New(cacheManager) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user