You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/pkg/common/mcontext/ctx.go

77 lines
1.8 KiB

2 years ago
package mcontext
import (
"context"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
)
2 years ago
func NewCtx(operationID string) context.Context {
2 years ago
c := context.Background()
2 years ago
ctx := context.WithValue(c, constant.OperationID, operationID)
SetOperationID(ctx, operationID)
return ctx
}
func SetOperationID(ctx context.Context, operationID string) {
2 years ago
ctx = context.WithValue(ctx, constant.OperationID, operationID)
}
func SetOpUserID(ctx context.Context, opUserID string) {
ctx = context.WithValue(ctx, constant.OpUserID, opUserID)
}
func SetConnID(ctx context.Context, connID string) {
ctx = context.WithValue(ctx, constant.ConnID, connID)
}
2 years ago
func GetOperationID(ctx context.Context) string {
2 years ago
if ctx.Value(constant.OperationID) != nil {
s, ok := ctx.Value(constant.OperationID).(string)
2 years ago
if ok {
2 years ago
return s
2 years ago
}
2 years ago
}
2 years ago
return ""
2 years ago
}
2 years ago
func GetMustCtxInfo(ctx context.Context) (operationID, opUserID, platform, connID string, err error) {
operationID, ok := ctx.Value(constant.OperationID).(string)
if !ok {
err = errs.ErrArgs.Wrap("ctx missing operationID")
return
}
opUserID, ok1 := ctx.Value(constant.OpUserID).(string)
if !ok1 {
err = errs.ErrArgs.Wrap("ctx missing opUserID")
return
}
platform, ok2 := ctx.Value(constant.OpUserPlatform).(string)
if !ok2 {
err = errs.ErrArgs.Wrap("ctx missing platform")
return
}
connID, _ = ctx.Value(constant.ConnID).(string)
return
}
2 years ago
2 years ago
func GetOpUserID(ctx context.Context) string {
2 years ago
if ctx.Value(constant.OpUserID) != "" {
s, ok := ctx.Value(constant.OpUserID).(string)
if ok {
return s
}
}
return ""
2 years ago
}
2 years ago
func GetConnID(ctx context.Context) string {
if ctx.Value(constant.ConnID) != "" {
s, ok := ctx.Value(constant.ConnID).(string)
if ok {
return s
}
}
return ""
}