parent
ba5b7c0e7e
commit
d7ba884ca2
@ -0,0 +1,39 @@
|
||||
package filters
|
||||
|
||||
import (
|
||||
rpcChat "Open_IM/internal/rpc/msg"
|
||||
"Open_IM/pkg/common/constant"
|
||||
pbChat "Open_IM/pkg/proto/chat"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func MockBeforeSendFilter1(ctx *rpcChat.SendContext, pb *pbChat.SendMsgReq) (*pbChat.SendMsgResp, bool, error) {
|
||||
ctxKey := "test_key"
|
||||
v := true
|
||||
fmt.Printf("MockBeforeSendFilter1:%s set value to ctx,value is :%v\n", ctxKey, v)
|
||||
ctx.WithValue(ctxKey, v)
|
||||
|
||||
return nil, true, nil
|
||||
}
|
||||
|
||||
// MockBeforeSendFilter is a mock handle that handles custom logic before send msg.
|
||||
func MockBeforeSendFilter2(ctx *rpcChat.SendContext, pb *pbChat.SendMsgReq) (*pbChat.SendMsgResp, bool, error) {
|
||||
ctxKey := "test_key"
|
||||
v, ok := ctx.Value(ctxKey).(bool)
|
||||
if ok {
|
||||
fmt.Printf("MockBeforeSendFilter2:%s selected from ctx,value is :%v\n", ctxKey, v)
|
||||
}
|
||||
|
||||
fmt.Printf("MockBeforeSendHandler trigger,contentType:%d\n", pb.MsgData.GetContentType())
|
||||
if pb.MsgData.ContentType == constant.Text {
|
||||
msg := string(pb.MsgData.Content)
|
||||
fmt.Printf("text msg:%s", msg)
|
||||
if msg == "this is a m..m..mock msg" {
|
||||
fmt.Println(".==>msg had banned")
|
||||
return nil, false, errors.New("BANG! This msg has been banned by MockBeforeSendHandler")
|
||||
}
|
||||
}
|
||||
|
||||
return nil, true, nil
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package widget
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
pbChat "Open_IM/pkg/proto/chat"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// MockBeforeSendHandler is a mock handle that handles custom logic before send msg.
|
||||
func MockBeforeSendHandler(ctx context.Context, pb *pbChat.SendMsgReq) (*pbChat.SendMsgResp, bool, error) {
|
||||
fmt.Printf("MockBeforeSendHandler trigger,contentType:%d\n", pb.MsgData.GetContentType())
|
||||
if pb.MsgData.ContentType == constant.Text {
|
||||
msg := string(pb.MsgData.Content)
|
||||
fmt.Printf("text msg:%s", msg)
|
||||
if msg == "this is a m..m..mock msg" {
|
||||
fmt.Println(".==>msg had banned")
|
||||
return nil, false, errors.New("BANG! This msg has been banned by MockBeforeSendHandler")
|
||||
}
|
||||
}
|
||||
|
||||
return nil, true, nil
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package msg
|
||||
|
||||
import (
|
||||
pbChat "Open_IM/pkg/proto/chat"
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// SendContext is the most important part of RPC SendMsg. It allows us to pass variables between middleware
|
||||
type SendContext struct {
|
||||
ctx context.Context
|
||||
rpc *rpcChat
|
||||
// beforeFilters are filters which will be triggered before send msg
|
||||
beforeFilters []BeforeSendFilter
|
||||
}
|
||||
|
||||
func NewSendContext(ctx context.Context, rpc *rpcChat) *SendContext {
|
||||
return &SendContext{
|
||||
ctx: ctx,
|
||||
rpc: rpc,
|
||||
beforeFilters: rpc.beforeSenders,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *SendContext) SetCtx(ctx context.Context) {
|
||||
c.ctx = ctx
|
||||
}
|
||||
|
||||
func (c *SendContext) Value(key interface{}) interface{} {
|
||||
return c.ctx.Value(key)
|
||||
}
|
||||
|
||||
func (c *SendContext) WithValue(key, val interface{}) {
|
||||
ctx := context.WithValue(c.ctx, key, val)
|
||||
c.SetCtx(ctx)
|
||||
}
|
||||
|
||||
// doBeforeFilters executes the pending filters in the chain inside the calling handler.
|
||||
func (c *SendContext) doBeforeFilters(pb *pbChat.SendMsgReq) (*pbChat.SendMsgResp, bool, error) {
|
||||
for _, handler := range c.beforeFilters {
|
||||
res, ok, err := handler(c, pb)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if !ok {
|
||||
return res, ok, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, true, nil
|
||||
}
|
||||
|
||||
func (c *SendContext) SendMsg(pb *pbChat.SendMsgReq) (*pbChat.SendMsgResp, error) {
|
||||
replay := pbChat.SendMsgResp{}
|
||||
res, ok, err := c.doBeforeFilters(pb)
|
||||
if err != nil {
|
||||
return returnMsg(&replay, pb, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError), err.Error(), 0)
|
||||
}
|
||||
if !ok {
|
||||
return res, nil
|
||||
}
|
||||
|
||||
res, err = c.rpc.doSendMsg(c.ctx, pb)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package msg
|
||||
|
||||
import pbChat "Open_IM/pkg/proto/chat"
|
||||
|
||||
// BeforeSendFilter handles custom logic before send msg.
|
||||
type BeforeSendFilter func(ctx *SendContext, pb *pbChat.SendMsgReq) (*pbChat.SendMsgResp, bool, error)
|
Loading…
Reference in new issue