github.com/glide-im/glide@v1.6.0/im_service/client/subscription_rpc_impl.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"github.com/glide-im/glide/im_service/proto"
     7  	"github.com/glide-im/glide/pkg/rpc"
     8  	"github.com/glide-im/glide/pkg/subscription"
     9  )
    10  
    11  type SubscriptionRpcImpl struct {
    12  	rpcCli *subscriptionRpcClient
    13  }
    14  
    15  func NewSubscriptionRpcImplWithClient(cli *rpc.BaseClient) *SubscriptionRpcImpl {
    16  	return &SubscriptionRpcImpl{
    17  		rpcCli: &subscriptionRpcClient{cli: cli},
    18  	}
    19  }
    20  
    21  func NewSubscriptionRpcImpl(opts *rpc.ClientOptions) (*SubscriptionRpcImpl, error) {
    22  	c, err := rpc.NewBaseClient(opts)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	return NewSubscriptionRpcImplWithClient(c), nil
    27  }
    28  
    29  func (s *SubscriptionRpcImpl) Close() error {
    30  	return s.rpcCli.Close()
    31  }
    32  
    33  func (s *SubscriptionRpcImpl) Subscribe(ch subscription.ChanID, id subscription.SubscriberID, extra interface{}) error {
    34  
    35  	marshal, err := json.Marshal(extra)
    36  	if err != nil {
    37  		return err
    38  	}
    39  	request := &proto.SubscribeRequest{
    40  		ChannelID:    string(ch),
    41  		SubscriberID: string(id),
    42  		Extra:        marshal,
    43  	}
    44  	reply := &proto.Response{}
    45  	err = s.rpcCli.Subscribe(context.Background(), request, reply)
    46  	if err != nil {
    47  		return err
    48  	}
    49  	return getResponseError(reply)
    50  }
    51  
    52  func (s *SubscriptionRpcImpl) UnSubscribe(ch subscription.ChanID, id subscription.SubscriberID) error {
    53  	request := &proto.UnsubscribeRequest{
    54  		ChannelID:    string(ch),
    55  		SubscriberID: string(id),
    56  	}
    57  	reply := &proto.Response{}
    58  	err := s.rpcCli.UnSubscribe(context.Background(), request, reply)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	return getResponseError(reply)
    63  }
    64  
    65  func (s *SubscriptionRpcImpl) UpdateSubscriber(ch subscription.ChanID, id subscription.SubscriberID, extra interface{}) error {
    66  	marshal, err := json.Marshal(extra)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	request := &proto.UpdateSubscriberRequest{
    72  		ChannelID:    string(ch),
    73  		SubscriberID: string(id),
    74  		Extra:        marshal,
    75  	}
    76  	reply := &proto.Response{}
    77  	err = s.rpcCli.UpdateSubscriber(context.Background(), request, reply)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	return getResponseError(reply)
    82  }
    83  
    84  func (s *SubscriptionRpcImpl) RemoveChannel(ch subscription.ChanID) error {
    85  	request := &proto.RemoveChannelRequest{
    86  		ChannelID: string(ch),
    87  	}
    88  	reply := &proto.Response{}
    89  	err := s.rpcCli.RemoveChannel(context.Background(), request, reply)
    90  	if err != nil {
    91  		return err
    92  	}
    93  	return getResponseError(reply)
    94  }
    95  
    96  func (s *SubscriptionRpcImpl) CreateChannel(ch subscription.ChanID, update *subscription.ChanInfo) error {
    97  
    98  	var children []string
    99  	for _, child := range update.Child {
   100  		children = append(children, string(child))
   101  	}
   102  	parent := ""
   103  	if update.Parent != nil {
   104  		parent = string(*update.Parent)
   105  	}
   106  	channelInfo := &proto.ChannelInfo{
   107  		ID:       string(update.ID),
   108  		Type:     int32(update.Type),
   109  		Muted:    update.Muted,
   110  		Blocked:  update.Blocked,
   111  		Closed:   update.Closed,
   112  		Parent:   parent,
   113  		Children: children,
   114  	}
   115  
   116  	request := &proto.CreateChannelRequest{
   117  		ChannelID:   string(ch),
   118  		ChannelInfo: channelInfo,
   119  	}
   120  	reply := &proto.Response{}
   121  	err := s.rpcCli.CreateChannel(context.Background(), request, reply)
   122  	if err != nil {
   123  		return err
   124  	}
   125  	return getResponseError(reply)
   126  }
   127  
   128  func (s *SubscriptionRpcImpl) UpdateChannel(ch subscription.ChanID, update *subscription.ChanInfo) error {
   129  	var children []string
   130  	for _, child := range update.Child {
   131  		children = append(children, string(child))
   132  	}
   133  	parent := ""
   134  	if update.Parent != nil {
   135  		parent = string(*update.Parent)
   136  	}
   137  	channelInfo := &proto.ChannelInfo{
   138  		ID:       string(update.ID),
   139  		Type:     int32(update.Type),
   140  		Muted:    update.Muted,
   141  		Blocked:  update.Blocked,
   142  		Closed:   update.Closed,
   143  		Parent:   parent,
   144  		Children: children,
   145  	}
   146  
   147  	request := &proto.UpdateChannelRequest{
   148  		ChannelID:   string(ch),
   149  		ChannelInfo: channelInfo,
   150  	}
   151  	reply := &proto.Response{}
   152  	err := s.rpcCli.UpdateChannel(context.Background(), request, reply)
   153  	if err != nil {
   154  		return err
   155  	}
   156  	return getResponseError(reply)
   157  }
   158  
   159  func (s *SubscriptionRpcImpl) Publish(ch subscription.ChanID, msg subscription.Message) error {
   160  
   161  	marshal, err := json.Marshal(msg)
   162  	if err != nil {
   163  		return err
   164  	}
   165  	request := &proto.PublishRequest{
   166  		ChannelID: string(ch),
   167  		Message:   marshal,
   168  	}
   169  	reply := &proto.Response{}
   170  	err = s.rpcCli.Publish(context.Background(), request, reply)
   171  	if err != nil {
   172  		return err
   173  	}
   174  	return getResponseError(reply)
   175  }