diff --git a/go.mod b/go.mod index 74db51f..fb20bc7 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,7 @@ go 1.22 toolchain go1.22.1 require ( - git.solsynth.dev/hydrogen/dealer v0.0.0-20240817155242-38b52aec2fb0 - git.solsynth.dev/hydrogen/paperclip v0.0.0-20240622051057-0f56dba45745 + git.solsynth.dev/hydrogen/dealer v0.0.0-20240823110807-df5676cbe4d8 github.com/go-playground/validator/v10 v10.17.0 github.com/gofiber/fiber/v2 v2.52.4 github.com/google/uuid v1.6.0 @@ -132,12 +131,12 @@ require ( go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.27.0 // indirect go.uber.org/zap/exp v0.2.0 // indirect - golang.org/x/crypto v0.25.0 // indirect + golang.org/x/crypto v0.26.0 // indirect golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect - golang.org/x/net v0.27.0 // indirect - golang.org/x/sync v0.7.0 // indirect - golang.org/x/sys v0.22.0 // indirect - golang.org/x/text v0.16.0 // indirect + golang.org/x/net v0.28.0 // indirect + golang.org/x/sync v0.8.0 // indirect + golang.org/x/sys v0.23.0 // indirect + golang.org/x/text v0.17.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240711142825-46eb208f015d // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/ini.v1 v1.67.0 // indirect diff --git a/go.sum b/go.sum index 0690018..e487e2d 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,7 @@ -git.solsynth.dev/hydrogen/dealer v0.0.0-20240719153055-607eba001f65 h1:p9PIsp5RyrLdlOR7MXyeGBx04v3+1IMEEBBV22fJZPo= -git.solsynth.dev/hydrogen/dealer v0.0.0-20240719153055-607eba001f65/go.mod h1:oPdUxLy6TFeRxiRC/BoNb3YUNcnSiOnJrzFTnCPSoCA= -git.solsynth.dev/hydrogen/dealer v0.0.0-20240817155242-38b52aec2fb0 h1:2YAKTqgyU+CjP0+m8S29xTqYBerQs/PQIOLAXeeaxzY= -git.solsynth.dev/hydrogen/dealer v0.0.0-20240817155242-38b52aec2fb0/go.mod h1:IZd94qZZIj+MO9EqjGDqnAD9nWurlNPyhVPKemAY5lw= -git.solsynth.dev/hydrogen/paperclip v0.0.0-20240622051057-0f56dba45745 h1:40BUsQMNXjqHyytkyF9py1HjTAWlRgO6R57YXUrHNy4= -git.solsynth.dev/hydrogen/paperclip v0.0.0-20240622051057-0f56dba45745/go.mod h1:FsQGSLTl0gvo+9Jmbot02S72suyF9tFTrzDj70Xhifo= +git.solsynth.dev/hydrogen/dealer v0.0.0-20240823105014-4f93800f7283 h1:dfSqc32SSIBsUkhRTKzk90C4v8YCQObavUIrw/+ZfQc= +git.solsynth.dev/hydrogen/dealer v0.0.0-20240823105014-4f93800f7283/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= +git.solsynth.dev/hydrogen/dealer v0.0.0-20240823110807-df5676cbe4d8 h1:2uoqdm2Z5yhJck0udcx9EAPHaoNse0pPZihJG5J8Q8A= +git.solsynth.dev/hydrogen/dealer v0.0.0-20240823110807-df5676cbe4d8/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= @@ -462,8 +460,8 @@ golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIi golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg= golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= -golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= -golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= +golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw= +golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54= golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY= golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8/go.mod h1:jj3sYF3dwk5D+ghuXyeI3r5MFf+NT2An6/9dOA95KSI= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= @@ -490,8 +488,8 @@ golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.13.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA= golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI= golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY= -golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys= -golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -501,8 +499,8 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -548,8 +546,8 @@ golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= +golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -573,8 +571,8 @@ golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= -golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= -golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190907020128-2ca718005c18/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= diff --git a/pkg/internal/grpc/server.go b/pkg/internal/grpc/server.go index e6de4f0..e9995a4 100644 --- a/pkg/internal/grpc/server.go +++ b/pkg/internal/grpc/server.go @@ -1,7 +1,7 @@ package grpc import ( - "git.solsynth.dev/hydrogen/paperclip/pkg/proto" + "git.solsynth.dev/hydrogen/dealer/pkg/proto" "github.com/spf13/viper" "google.golang.org/grpc" health "google.golang.org/grpc/health/grpc_health_v1" @@ -10,7 +10,7 @@ import ( ) type Server struct { - proto.UnimplementedAttachmentsServer + proto.UnimplementedStreamControllerServer } var S *grpc.Server @@ -19,6 +19,7 @@ func NewGRPC() { S = grpc.NewServer() health.RegisterHealthServer(S, &Server{}) + proto.RegisterStreamControllerServer(S, &Server{}) reflection.Register(S) } diff --git a/pkg/internal/grpc/stream.go b/pkg/internal/grpc/stream.go new file mode 100644 index 0000000..25dc861 --- /dev/null +++ b/pkg/internal/grpc/stream.go @@ -0,0 +1,51 @@ +package grpc + +import ( + "context" + "fmt" + + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" + "git.solsynth.dev/hydrogen/dealer/pkg/proto" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/services" + jsoniter "github.com/json-iterator/go" +) + +func (v *Server) EmitStreamEvent(_ context.Context, in *proto.StreamEventRequest) (*proto.StreamEventResponse, error) { + sc := proto.NewStreamControllerClient(gap.H.GetDealerGrpcConn()) + + switch in.GetEvent() { + case "status.typing": + var data struct { + ChannelID uint `json:"channel_id"` + } + + err := jsoniter.Unmarshal(in.GetPayload(), &data) + if err == nil { + err = exts.ValidateStruct(data) + } + if err != nil { + sc.PushStream(context.Background(), &proto.PushStreamRequest{ + ClientId: &in.ClientId, + Body: hyper.NetworkPackage{ + Action: "error", + Message: fmt.Sprintf("unable parse payload: %v", err), + }.Marshal(), + }) + } + + err = services.SetTypingStatus(data.ChannelID, uint(in.GetUserId())) + if err != nil { + sc.PushStream(context.Background(), &proto.PushStreamRequest{ + ClientId: &in.ClientId, + Body: hyper.NetworkPackage{ + Action: "error", + Message: fmt.Sprintf("unable boardcast status: %v", err), + }.Marshal(), + }) + } + } + + return &proto.StreamEventResponse{}, nil +} diff --git a/pkg/internal/server/exts/utils.go b/pkg/internal/server/exts/utils.go index f34e873..e6ca7e4 100644 --- a/pkg/internal/server/exts/utils.go +++ b/pkg/internal/server/exts/utils.go @@ -16,3 +16,7 @@ func BindAndValidate(c *fiber.Ctx, out any) error { return nil } + +func ValidateStruct(in any) error { + return validation.Struct(in) +} diff --git a/pkg/internal/services/status.go b/pkg/internal/services/status.go new file mode 100644 index 0000000..4137001 --- /dev/null +++ b/pkg/internal/services/status.go @@ -0,0 +1,61 @@ +package services + +import ( + "context" + "fmt" + + "git.solsynth.dev/hydrogen/dealer/pkg/hyper" + "git.solsynth.dev/hydrogen/dealer/pkg/proto" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" + "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" +) + +func SetTypingStatus(channelId uint, userId uint) error { + var account models.Account + if err := database.C.Where("id = ?", userId).First(&account).Error; err != nil { + return fmt.Errorf("account not found: %v", err) + } + + var member models.ChannelMember + if err := database.C. + Where("account_id = ? AND channel_id = ?", userId, channelId). + First(&member).Error; err != nil { + return fmt.Errorf("channel member not found: %v", err) + } else { + member.Account = account + } + + var channel models.Channel + if err := database.C. + Preload("Members"). + Where("id = ?", channelId). + First(&channel).Error; err != nil { + return fmt.Errorf("channel not found: %v", err) + } + + var boardcastTarget []uint64 + for _, item := range channel.Members { + if item.AccountID == member.AccountID { + continue + } + boardcastTarget = append(boardcastTarget, uint64(item.AccountID)) + } + + sc := proto.NewStreamControllerClient(gap.H.GetDealerGrpcConn()) + _, err := sc.PushStreamBatch(context.Background(), &proto.PushStreamBatchRequest{ + UserId: boardcastTarget, + Body: hyper.NetworkPackage{ + Action: "status.typing", + Payload: map[string]any{ + "user_id": userId, + "member_id": member.ID, + "channel_id": channelId, + "member": member, + "channel": channel, + }, + }.Marshal(), + }) + + return err +}