2024-08-23 11:32:24 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-11-02 05:23:27 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
2024-08-23 11:32:24 +00:00
|
|
|
|
2024-11-02 05:24:37 +00:00
|
|
|
"git.solsynth.dev/hypernet/messaging/pkg/internal/gap"
|
|
|
|
"git.solsynth.dev/hypernet/messaging/pkg/internal/http/exts"
|
|
|
|
"git.solsynth.dev/hypernet/messaging/pkg/internal/services"
|
2024-11-02 05:23:27 +00:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
2024-08-23 11:32:24 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
|
|
)
|
|
|
|
|
2024-11-02 05:23:27 +00:00
|
|
|
func (v *Server) PushStream(_ context.Context, request *proto.PushStreamRequest) (*proto.PushStreamResponse, error) {
|
|
|
|
sc := proto.NewStreamServiceClient(gap.Nx.GetNexusGrpcConn())
|
2024-08-23 11:32:24 +00:00
|
|
|
|
2024-11-02 05:23:27 +00:00
|
|
|
var in nex.WebSocketPackage
|
|
|
|
if err := jsoniter.Unmarshal(request.GetBody(), &in); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch in.Action {
|
2024-08-23 11:32:24 +00:00
|
|
|
case "status.typing":
|
|
|
|
var data struct {
|
2024-08-23 13:01:47 +00:00
|
|
|
ChannelID uint `json:"channel_id" validate:"required"`
|
2024-08-23 11:32:24 +00:00
|
|
|
}
|
|
|
|
|
2024-11-02 05:23:27 +00:00
|
|
|
err := jsoniter.Unmarshal(in.RawPayload(), &data)
|
2024-08-23 11:32:24 +00:00
|
|
|
if err == nil {
|
|
|
|
err = exts.ValidateStruct(data)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2024-08-23 13:01:47 +00:00
|
|
|
_, _ = sc.PushStream(context.Background(), &proto.PushStreamRequest{
|
2024-11-02 05:23:27 +00:00
|
|
|
ClientId: request.ClientId,
|
|
|
|
Body: nex.WebSocketPackage{
|
2024-08-23 11:32:24 +00:00
|
|
|
Action: "error",
|
|
|
|
Message: fmt.Sprintf("unable parse payload: %v", err),
|
|
|
|
}.Marshal(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-02 05:23:27 +00:00
|
|
|
err = services.SetTypingStatus(data.ChannelID, uint(request.GetUserId()))
|
2024-08-23 11:32:24 +00:00
|
|
|
if err != nil {
|
2024-08-23 13:01:47 +00:00
|
|
|
_, _ = sc.PushStream(context.Background(), &proto.PushStreamRequest{
|
2024-11-02 05:23:27 +00:00
|
|
|
ClientId: request.ClientId,
|
|
|
|
Body: nex.WebSocketPackage{
|
2024-08-23 11:32:24 +00:00
|
|
|
Action: "error",
|
|
|
|
Message: fmt.Sprintf("unable boardcast status: %v", err),
|
|
|
|
}.Marshal(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-02 05:23:27 +00:00
|
|
|
return &proto.PushStreamResponse{}, nil
|
2024-08-23 11:32:24 +00:00
|
|
|
}
|