mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-03 00:25:59 +08:00
Merge remote-tracking branch 'origin/errcode' into errcode
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
package callback
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
server_api_params "Open_IM/pkg/proto/sdk_ws"
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
func GetContent(msg *server_api_params.MsgData) string {
|
||||
if msg.ContentType >= constant.NotificationBegin && msg.ContentType <= constant.NotificationEnd {
|
||||
var tips server_api_params.TipsComm
|
||||
_ = proto.Unmarshal(msg.Content, &tips)
|
||||
//marshaler := jsonpb.Marshaler{
|
||||
// OrigName: true,
|
||||
// EnumsAsInts: false,
|
||||
// EmitDefaults: false,
|
||||
//}
|
||||
content := tips.JsonDetail
|
||||
return content
|
||||
} else {
|
||||
return string(msg.Content)
|
||||
}
|
||||
}
|
||||
@@ -158,13 +158,15 @@ func (g *GroupDataBase) FindGroupsByID(ctx context.Context, groupIDs []string) (
|
||||
return g.cache.GetGroupsInfo(ctx, groupIDs)
|
||||
}
|
||||
|
||||
func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relation.Group, groupMember []*relation.GroupMember) error {
|
||||
func (g *GroupDataBase) CreateGroup(ctx context.Context, groups []*relation.Group, groupMembers []*relation.GroupMember) error {
|
||||
return g.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := g.groupDB.Create(ctx, groups, tx); err != nil {
|
||||
return err
|
||||
if len(groups) > 0 {
|
||||
if err := g.groupDB.Create(ctx, groups, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(groupMember) > 0 {
|
||||
if err := g.groupMemberDB.Create(ctx, groupMember, tx); err != nil {
|
||||
if len(groupMembers) > 0 {
|
||||
if err := g.groupMemberDB.Create(ctx, groupMembers, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,11 @@ type Friend struct {
|
||||
DB *gorm.DB `gorm:"-"`
|
||||
}
|
||||
|
||||
type FriendUser struct {
|
||||
Friend
|
||||
Nickname string `gorm:"column:name;size:255"`
|
||||
}
|
||||
|
||||
func NewFriendDB(db *gorm.DB) *Friend {
|
||||
var friend Friend
|
||||
friend.DB = initModel(db, friend)
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/common/token_verify"
|
||||
"Open_IM/pkg/utils"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -8,6 +12,44 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func JWTAuth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
ok, userID, errInfo := token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), "")
|
||||
// log.NewInfo("0", utils.GetSelfFuncName(), "userID: ", userID)
|
||||
c.Set("userID", userID)
|
||||
if !ok {
|
||||
log.NewError("", "GetUserIDFromToken false ", c.Request.Header.Get("token"))
|
||||
c.Abort()
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": 400, "errMsg": errInfo})
|
||||
return
|
||||
} else {
|
||||
if !utils.IsContain(userID, config.Config.Manager.AppManagerUid) {
|
||||
c.Abort()
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": 400, "errMsg": "user is not admin"})
|
||||
return
|
||||
}
|
||||
log.NewInfo("0", utils.GetSelfFuncName(), "failed: ", errInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CorsHandler() gin.HandlerFunc {
|
||||
return func(context *gin.Context) {
|
||||
context.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
context.Header("Access-Control-Allow-Methods", "*")
|
||||
context.Header("Access-Control-Allow-Headers", "*")
|
||||
context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar") // 跨域关键设置 让浏览器可以解析
|
||||
context.Header("Access-Control-Max-Age", "172800") // 缓存请求信息 单位为秒
|
||||
context.Header("Access-Control-Allow-Credentials", "false") // 跨域请求是否需要带cookie信息 默认设置为true
|
||||
context.Header("content-type", "application/json") // 设置返回格式是json
|
||||
//Release all option pre-requests
|
||||
if context.Request.Method == http.MethodOptions {
|
||||
context.JSON(http.StatusOK, "Options Request!")
|
||||
}
|
||||
context.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func GinParseOperationID(c *gin.Context) {
|
||||
if c.Request.Method == http.MethodPost {
|
||||
operationID := c.Request.Header.Get("operationID")
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
commonDB "Open_IM/pkg/common/db"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/common/tools"
|
||||
"Open_IM/pkg/common/tracelog"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
@@ -160,7 +159,7 @@ func CheckAccess(ctx context.Context, OpUserID string, OwnerUserID string) bool
|
||||
}
|
||||
|
||||
func CheckAccessV3(ctx context.Context, ownerUserID string) (err error) {
|
||||
opUserID := tools.OpUserID(ctx)
|
||||
opUserID := utils.OpUserID(ctx)
|
||||
defer func() {
|
||||
tracelog.SetCtxInfo(ctx, utils.GetFuncName(1), err, "OpUserID", opUserID, "ownerUserID", ownerUserID)
|
||||
}()
|
||||
@@ -174,11 +173,11 @@ func CheckAccessV3(ctx context.Context, ownerUserID string) (err error) {
|
||||
}
|
||||
|
||||
func IsAppManagerUid(ctx context.Context) bool {
|
||||
return utils.IsContain(tools.OpUserID(ctx), config.Config.Manager.AppManagerUid)
|
||||
return utils.IsContain(utils.OpUserID(ctx), config.Config.Manager.AppManagerUid)
|
||||
}
|
||||
|
||||
func CheckAdmin(ctx context.Context) error {
|
||||
if utils.IsContain(tools.OpUserID(ctx), config.Config.Manager.AppManagerUid) {
|
||||
if utils.IsContain(utils.OpUserID(ctx), config.Config.Manager.AppManagerUid) {
|
||||
return nil
|
||||
}
|
||||
return constant.ErrIdentity.Wrap()
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package tools
|
||||
|
||||
import "context"
|
||||
|
||||
func OperationID(ctx context.Context) string {
|
||||
s, _ := ctx.Value("operationID").(string)
|
||||
return s
|
||||
}
|
||||
|
||||
func OpUserID(ctx context.Context) string {
|
||||
s, _ := ctx.Value("opUserID").(string)
|
||||
return s
|
||||
}
|
||||
@@ -1,28 +1,20 @@
|
||||
package tracelog
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/constant"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"github.com/sirupsen/logrus"
|
||||
"google.golang.org/grpc/status"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
//"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const TraceLogKey = "tracelog"
|
||||
|
||||
func NewCtx(c *gin.Context, api string) context.Context {
|
||||
req := &ApiInfo{ApiName: api, GinCtx: c, Funcs: &[]FuncInfo{}}
|
||||
return context.WithValue(c, TraceLogKey, req)
|
||||
}
|
||||
|
||||
func NewCtx1(c *gin.Context, api string) context.Context {
|
||||
req := &ApiInfo{ApiName: api, GinCtx: c, OperationID: c.GetHeader("operationID"), Funcs: &[]FuncInfo{}}
|
||||
return context.WithValue(c, TraceLogKey, req)
|
||||
}
|
||||
@@ -42,77 +34,11 @@ func GetOperationID(ctx context.Context) string {
|
||||
return ctx.Value(TraceLogKey).(*ApiInfo).OperationID
|
||||
}
|
||||
|
||||
//func ShowLog(ctx context.Context) {
|
||||
// t := ctx.Value(TraceLogKey).(*ApiInfo)
|
||||
// if ctx.Value(TraceLogKey).(*ApiInfo).GinCtx != nil {
|
||||
// log.Info(t.OperationID, "api: ", t.ApiName)
|
||||
// } else {
|
||||
// log.Info(t.OperationID, "rpc: ", t.ApiName)
|
||||
// }
|
||||
// for _, v := range *t.Funcs {
|
||||
// if v.Err != nil {
|
||||
// log.Error(t.OperationID, "func: ", v.FuncName, " args: ", v.Args, v.Err.Error())
|
||||
// } else {
|
||||
// switch v.LogLevel {
|
||||
// case logrus.InfoLevel:
|
||||
// log.Info(t.OperationID, "func: ", v.FuncName, " args: ", v.Args)
|
||||
// case logrus.DebugLevel:
|
||||
// log.Debug(t.OperationID, "func: ", v.FuncName, " args: ", v.Args)
|
||||
// case logrus.WarnLevel:
|
||||
// log.Debug(t.OperationID, "func: ", v.FuncName, " args: ", v.Args)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
func WriteErrorResponse(ctx context.Context, funcName string, err error, args ...interface{}) {
|
||||
SetCtxInfo(ctx, funcName, err, args)
|
||||
e := Unwrap(err)
|
||||
switch t := e.(type) {
|
||||
case *constant.ErrInfo:
|
||||
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, baseResp{ErrCode: t.ErrCode, ErrMsg: t.ErrMsg, ErrDtl: t.DetailErrMsg})
|
||||
//ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": t.ErrCode, "errMsg": t.ErrMsg, "errDtl": t.DetailErrMsg})
|
||||
return
|
||||
default:
|
||||
s, ok := status.FromError(e)
|
||||
if !ok {
|
||||
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, &baseResp{ErrCode: constant.ErrDefaultOther.ErrCode, ErrMsg: err.Error(), ErrDtl: fmt.Sprintf("%+v", err)})
|
||||
//ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": constant.ErrDefaultOther.ErrCode, "errMsg": err.Error(), "errDtl": fmt.Sprintf("%+v", err)})
|
||||
return
|
||||
}
|
||||
var details []string
|
||||
if err != e {
|
||||
details = append(details, fmt.Sprintf("%+v", err))
|
||||
}
|
||||
for _, s := range s.Details() {
|
||||
details = append(details, fmt.Sprintf("%+v", s))
|
||||
}
|
||||
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, &baseResp{ErrCode: int32(s.Code()), ErrMsg: s.Message(), ErrDtl: strings.Join(details, "\n")})
|
||||
//ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": s.Code(), "errMsg": s.Message(), "errDtl": strings.Join(details, "\n")})
|
||||
return
|
||||
}
|
||||
func GetOpUserID(ctx context.Context) string {
|
||||
s, _ := ctx.Value("opUserID").(string)
|
||||
return s
|
||||
}
|
||||
|
||||
type baseResp struct {
|
||||
ErrCode int32 `json:"errCode"`
|
||||
ErrMsg string `json:"errMsg"`
|
||||
ErrDtl string `json:"errDtl"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
//func WriteErrorResponse(ctx context.Context, funcName string, err error, args ...interface{}) {
|
||||
// SetCtxInfo(ctx, funcName, err, args)
|
||||
// e := new(constant.ErrInfo)
|
||||
// switch {
|
||||
// case errors.As(err, &e):
|
||||
// ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": e.ErrCode, "errMsg": e.ErrMsg})
|
||||
// return
|
||||
// default:
|
||||
// ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": constant.ErrDefaultOther.ErrCode, "errMsg": constant.ErrDefaultOther.ErrMsg, "errDtl": err.Error()})
|
||||
// return
|
||||
// }
|
||||
//}
|
||||
|
||||
func Unwrap(err error) error {
|
||||
for err != nil {
|
||||
unwrap, ok := err.(interface {
|
||||
@@ -211,11 +137,6 @@ func SetRpcRespInfo(ctx context.Context, funcName string, resp string) {
|
||||
*t.Funcs = append(*t.Funcs, funcInfo)
|
||||
}
|
||||
|
||||
func SetSuccess(ctx context.Context, funcName string, data interface{}) {
|
||||
SetCtxInfo(ctx, funcName, nil, "data", data)
|
||||
ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "errDtl": "", "data": data})
|
||||
}
|
||||
|
||||
func argsHandle(args []interface{}, fields map[string]interface{}) {
|
||||
for i := 0; i < len(args); i += 2 {
|
||||
if i+1 < len(args) {
|
||||
|
||||
Reference in New Issue
Block a user