github.com/glide-im/glide@v1.6.0/internal/im_server/rpc_service.go (about)

     1  package im_server
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"errors"
     7  	"github.com/glide-im/glide/im_service/proto"
     8  	"github.com/glide-im/glide/pkg/gate"
     9  	"github.com/glide-im/glide/pkg/messages"
    10  	"github.com/glide-im/glide/pkg/rpc"
    11  	"github.com/glide-im/glide/pkg/subscription"
    12  	"github.com/glide-im/glide/pkg/subscription/subscription_impl"
    13  )
    14  
    15  type RpcServer struct {
    16  	gateway gate.Server
    17  	sub     subscription_impl.SubscribeWrap
    18  }
    19  
    20  func RunRpcServer(options *rpc.ServerOptions, gate gate.Server, subscribe subscription.Subscribe) error {
    21  	server := rpc.NewBaseServer(options)
    22  	rpcServer := RpcServer{
    23  		gateway: gate,
    24  		sub:     subscription_impl.NewSubscribeWrap(subscribe),
    25  	}
    26  	server.Register(options.Name, &rpcServer)
    27  	return server.Run()
    28  }
    29  
    30  func (r *RpcServer) UpdateClient(ctx context.Context, request *proto.UpdateClient, response *proto.Response) error {
    31  	id := gate.ID(request.GetId())
    32  
    33  	var err error
    34  	switch request.Type {
    35  	case proto.UpdateClient_UpdateID:
    36  		err = r.gateway.SetClientID(id, gate.ID(request.GetNewId()))
    37  		break
    38  	case proto.UpdateClient_Close:
    39  		err = r.gateway.ExitClient(id)
    40  		break
    41  	case proto.UpdateClient_UpdateSecret:
    42  		secrets := &gate.ClientSecrets{
    43  			MessageDeliverSecret: request.GetSecret(),
    44  		}
    45  		gt := r.gateway
    46  		err2 := gt.UpdateClient(id, secrets)
    47  		if err2 != nil {
    48  			err = err2
    49  		}
    50  		break
    51  	default:
    52  		err = errors.New("unknown update type")
    53  	}
    54  	if err != nil {
    55  		response.Code = int32(proto.Response_ERROR)
    56  		response.Msg = err.Error()
    57  	} else {
    58  		response.Code = int32(proto.Response_OK)
    59  	}
    60  	return err
    61  }
    62  
    63  func (r *RpcServer) EnqueueMessage(ctx context.Context, request *proto.EnqueueMessageRequest, response *proto.Response) error {
    64  
    65  	msg := messages.GlideMessage{}
    66  	err := json.Unmarshal(request.Msg, &msg)
    67  	if err != nil {
    68  		response.Code = int32(proto.Response_ERROR)
    69  		response.Msg = err.Error()
    70  		return nil
    71  	}
    72  
    73  	err = r.gateway.EnqueueMessage(gate.ID(request.Id), &msg)
    74  	if err != nil {
    75  		response.Code = int32(proto.Response_ERROR)
    76  		response.Msg = err.Error()
    77  	}
    78  	return err
    79  }
    80  
    81  ////////////////////////////////////// Subscription //////////////////////////////////////////////
    82  
    83  func (r *RpcServer) Subscribe(ctx context.Context, request *proto.SubscribeRequest, response *proto.Response) error {
    84  
    85  	subscriberID := subscription.SubscriberID(request.SubscriberID)
    86  	channelID := subscription.ChanID(request.ChannelID)
    87  
    88  	info := subscription_impl.SubscriberOptions{}
    89  	err := json.Unmarshal(request.GetExtra(), &info)
    90  	if err != nil {
    91  		response.Code = int32(proto.Response_ERROR)
    92  		response.Msg = err.Error()
    93  		return nil
    94  	}
    95  
    96  	err = r.sub.Subscribe(channelID, subscriberID, &info)
    97  	if err != nil {
    98  		response.Code = int32(proto.Response_ERROR)
    99  		response.Msg = err.Error()
   100  	}
   101  	return nil
   102  }
   103  
   104  func (r *RpcServer) UnSubscribe(ctx context.Context, request *proto.UnsubscribeRequest, response *proto.Response) error {
   105  	chanId := subscription.ChanID(request.ChannelID)
   106  	subscriberId := subscription.SubscriberID(request.SubscriberID)
   107  	err := r.sub.UnSubscribe(chanId, subscriberId)
   108  	if err != nil {
   109  		response.Code = int32(proto.Response_ERROR)
   110  		response.Msg = err.Error()
   111  	}
   112  	return nil
   113  }
   114  
   115  func (r *RpcServer) UpdateSubscriber(ctx context.Context, request *proto.UpdateSubscriberRequest, response *proto.Response) error {
   116  
   117  	chanId := subscription.ChanID(request.ChannelID)
   118  	subscriberID := subscription.SubscriberID(request.SubscriberID)
   119  	info := subscription_impl.SubscriberOptions{}
   120  	err := json.Unmarshal(request.GetExtra(), &info)
   121  	if err != nil {
   122  		response.Code = int32(proto.Response_ERROR)
   123  		response.Msg = err.Error()
   124  		return nil
   125  	}
   126  	err = r.sub.UpdateSubscriber(chanId, subscriberID, &info)
   127  	if err != nil {
   128  		response.Code = int32(proto.Response_ERROR)
   129  		response.Msg = err.Error()
   130  	}
   131  	return nil
   132  }
   133  
   134  func (r *RpcServer) RemoveChannel(ctx context.Context, request *proto.RemoveChannelRequest, response *proto.Response) error {
   135  	chanId := subscription.ChanID(request.ChannelID)
   136  	err := r.sub.RemoveChannel(chanId)
   137  	if err != nil {
   138  		response.Code = int32(proto.Response_ERROR)
   139  		response.Msg = err.Error()
   140  	}
   141  	return nil
   142  }
   143  
   144  func (r *RpcServer) CreateChannel(ctx context.Context, request *proto.CreateChannelRequest, response *proto.Response) error {
   145  	chanId := subscription.ChanID(request.ChannelID)
   146  	cInfo := request.GetChannelInfo()
   147  
   148  	info := subscription.ChanInfo{
   149  		ID:      chanId,
   150  		Type:    subscription.ChanType(cInfo.Type),
   151  		Muted:   cInfo.Muted,
   152  		Blocked: cInfo.Blocked,
   153  		Closed:  cInfo.Closed,
   154  	}
   155  	err := r.sub.CreateChannel(chanId, &info)
   156  	if err != nil {
   157  		response.Code = int32(proto.Response_ERROR)
   158  		response.Msg = err.Error()
   159  	}
   160  	return nil
   161  }
   162  
   163  func (r *RpcServer) UpdateChannel(ctx context.Context, request *proto.UpdateChannelRequest, response *proto.Response) error {
   164  	chanId := subscription.ChanID(request.ChannelID)
   165  	cInfo := request.GetChannelInfo()
   166  	info := subscription.ChanInfo{
   167  		ID:      chanId,
   168  		Type:    subscription.ChanType(cInfo.Type),
   169  		Muted:   cInfo.Muted,
   170  		Blocked: cInfo.Blocked,
   171  		Closed:  cInfo.Closed,
   172  	}
   173  	err := r.sub.UpdateChannel(chanId, &info)
   174  	if err != nil {
   175  		response.Code = int32(proto.Response_ERROR)
   176  		response.Msg = err.Error()
   177  	}
   178  	return nil
   179  }
   180  
   181  func (r *RpcServer) Publish(ctx context.Context, request *proto.PublishRequest, response *proto.Response) error {
   182  	chanId := subscription.ChanID(request.ChannelID)
   183  	msg := subscription_impl.PublishMessage{}
   184  	err := json.Unmarshal(request.GetMessage(), &msg)
   185  	if err != nil {
   186  		response.Code = int32(proto.Response_ERROR)
   187  		response.Msg = err.Error()
   188  		return nil
   189  	}
   190  	err = r.sub.Publish(chanId, &msg)
   191  	if err != nil {
   192  		response.Code = int32(proto.Response_ERROR)
   193  		response.Msg = err.Error()
   194  	}
   195  	return nil
   196  }