Merge remote-tracking branch 'origin/errcode' into errcode

This commit is contained in:
withchao 2023-03-17 16:08:21 +08:00
commit 58206f983e
10 changed files with 68 additions and 39 deletions

View File

@ -162,7 +162,7 @@ log:
#日志级别 6表示全都打印测试阶段建议设置为6
remainLogLevel: -1
stderr: true
withStack: false
elasticSearchSwitch: false
elasticSearchAddr: [ 127.0.0.1:9201 ]
elasticSearchUser: ""

View File

@ -2,6 +2,7 @@ package api
import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/a2r"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"

View File

@ -2,6 +2,7 @@ package friend
import (
"context"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation"
@ -177,7 +178,8 @@ func (s *friendServer) GetPaginationFriendsApplyTo(ctx context.Context, req *pbf
if err := s.userCheck.Access(ctx, req.UserID); err != nil {
return nil, err
}
friendRequests, total, err := s.FriendDatabase.PageFriendRequestToMe(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
pageNumber, showNumber := utils.GetPage(req.Pagination)
friendRequests, total, err := s.FriendDatabase.PageFriendRequestToMe(ctx, req.UserID, pageNumber, showNumber)
if err != nil {
return nil, err
}
@ -195,7 +197,8 @@ func (s *friendServer) GetPaginationFriendsApplyFrom(ctx context.Context, req *p
if err := s.userCheck.Access(ctx, req.UserID); err != nil {
return nil, err
}
friendRequests, total, err := s.FriendDatabase.PageFriendRequestFromMe(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
pageNumber, showNumber := utils.GetPage(req.Pagination)
friendRequests, total, err := s.FriendDatabase.PageFriendRequestFromMe(ctx, req.UserID, pageNumber, showNumber)
if err != nil {
return nil, err
}
@ -222,7 +225,8 @@ func (s *friendServer) GetPaginationFriends(ctx context.Context, req *pbfriend.G
if err := s.userCheck.Access(ctx, req.UserID); err != nil {
return nil, err
}
friends, total, err := s.FriendDatabase.PageOwnerFriends(ctx, req.UserID, req.Pagination.PageNumber, req.Pagination.ShowNumber)
pageNumber, showNumber := utils.GetPage(req.Pagination)
friends, total, err := s.FriendDatabase.PageOwnerFriends(ctx, req.UserID, pageNumber, showNumber)
if err != nil {
return nil, err
}

View File

@ -187,6 +187,7 @@ type config struct {
RemainRotationCount uint `yaml:"remainRotationCount"`
RemainLogLevel int `yaml:"remainLogLevel"`
Stderr bool `yaml:"stderr"`
WithStack bool `yaml:"withStack"`
ElasticSearchSwitch bool `yaml:"elasticSearchSwitch"`
ElasticSearchAddr []string `yaml:"elasticSearchAddr"`
ElasticSearchUser string `yaml:"elasticSearchUser"`

View File

@ -2,7 +2,7 @@ package controller
import (
"context"
"errors"
"time"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
@ -73,7 +73,7 @@ func (f *friendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUse
return f.tx.Transaction(func(tx any) error {
_, err := f.friendRequest.NewTx(tx).Take(ctx, fromUserID, toUserID)
//有db错误
if err != nil && errors.Unwrap(err) != gorm.ErrRecordNotFound {
if err != nil && errs.Unwrap(err) != gorm.ErrRecordNotFound {
return err
}
//无错误 则更新
@ -89,7 +89,7 @@ func (f *friendDatabase) AddFriendRequest(ctx context.Context, fromUserID, toUse
return nil
}
//gorm.ErrRecordNotFound 错误,则新增
if err := f.friendRequest.NewTx(tx).Create(ctx, []*relation.FriendRequestModel{{FromUserID: fromUserID, ToUserID: toUserID, ReqMsg: reqMsg, Ex: ex}}); err != nil {
if err := f.friendRequest.NewTx(tx).Create(ctx, []*relation.FriendRequestModel{{FromUserID: fromUserID, ToUserID: toUserID, ReqMsg: reqMsg, Ex: ex, CreateTime: time.Now(), HandleTime: time.Unix(0, 0)}}); err != nil {
return err
}
return nil
@ -142,7 +142,8 @@ func (f *friendDatabase) RefuseFriendRequest(ctx context.Context, friendRequest
return err
}
friendRequest.HandleResult = constant.FriendResponseRefuse
err = f.friendRequest.Update(ctx, []*relation.FriendRequestModel{friendRequest})
friendRequest.HandleTime = time.Now()
err = f.friendRequest.Update(ctx, friendRequest)
if err != nil {
return err
}
@ -158,7 +159,8 @@ func (f *friendDatabase) AgreeFriendRequest(ctx context.Context, friendRequest *
}
friendRequest.HandlerUserID = friendRequest.FromUserID
friendRequest.HandleResult = constant.FriendResponseAgree
err = f.friendRequest.NewTx(tx).Update(ctx, []*relation.FriendRequestModel{friendRequest})
friendRequest.HandleTime = time.Now()
err = f.friendRequest.NewTx(tx).Update(ctx, friendRequest)
if err != nil {
return err
}

View File

@ -31,13 +31,13 @@ func (f *FriendRequestGorm) Delete(ctx context.Context, fromUserID, toUserID str
}
// 更新零值
func (f *FriendRequestGorm) UpdateByMap(ctx context.Context, formUserID string, toUserID string, args map[string]interface{}) (err error) {
return utils.Wrap(f.db(ctx).Model(&relation.FriendRequestModel{}).Where("from_user_id = ? AND to_user_id =?", formUserID, toUserID).Updates(args).Error, "")
func (f *FriendRequestGorm) UpdateByMap(ctx context.Context, fromUserID string, toUserID string, args map[string]interface{}) (err error) {
return utils.Wrap(f.db(ctx).Model(&relation.FriendRequestModel{}).Where("from_user_id = ? AND to_user_id =?", fromUserID, toUserID).Updates(args).Error, "")
}
// 更新多条记录 (非零值)
func (f *FriendRequestGorm) Update(ctx context.Context, friendRequests []*relation.FriendRequestModel) (err error) {
return utils.Wrap(f.db(ctx).Updates(&friendRequests).Error, "")
// 更新记录 (非零值)
func (f *FriendRequestGorm) Update(ctx context.Context, friendRequest *relation.FriendRequestModel) (err error) {
return utils.Wrap(f.db(ctx).Where("from_user_id = ? AND to_user_id =?", friendRequest.FromUserID, friendRequest.ToUserID).Updates(friendRequest).Error, "")
}
// 获取来指定用户的好友申请 未找到 不返回错误

View File

@ -31,7 +31,7 @@ type FriendRequestModelInterface interface {
// 更新零值
UpdateByMap(ctx context.Context, formUserID string, toUserID string, args map[string]interface{}) (err error)
// 更新多条记录 (非零值)
Update(ctx context.Context, friendRequests []*FriendRequestModel) (err error)
Update(ctx context.Context, friendRequest *FriendRequestModel) (err error)
// 获取来指定用户的好友申请 未找到 不返回错误
Find(ctx context.Context, fromUserID, toUserID string) (friendRequest *FriendRequestModel, err error)
Take(ctx context.Context, fromUserID, toUserID string) (friendRequest *FriendRequestModel, err error)

View File

@ -3,6 +3,12 @@ package mw
import (
"context"
"fmt"
"math"
"runtime"
"runtime/debug"
"strings"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/mw/specialerror"
"github.com/OpenIMSDK/Open-IM-Server/pkg/errs"
@ -12,10 +18,6 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"math"
"runtime"
"runtime/debug"
"strings"
)
const OperationID = "operationID"
@ -87,23 +89,25 @@ func rpcServerInterceptor(ctx context.Context, req interface{}, info *grpc.Unary
}
grpcStatus := status.New(codes.Code(code), codeErr.Msg())
var errInfo *errinfo.ErrorInfo
if unwrap != err {
sti, ok := err.(interface{ StackTrace() errors.StackTrace })
if ok {
log.ZWarn(ctx, "rpc server resp", err, "funcName", funcName, "unwrap", unwrap.Error(), "stack", fmt.Sprintf("%+v", err))
if fs := sti.StackTrace(); len(fs) > 0 {
pc := uintptr(fs[0])
fn := runtime.FuncForPC(pc)
file, line := fn.FileLine(pc)
errInfo = &errinfo.ErrorInfo{
Path: file,
Line: uint32(line),
Name: fn.Name(),
Cause: unwrap.Error(),
Warp: nil,
}
if arr := strings.Split(err.Error(), ": "); len(arr) > 1 {
errInfo.Warp = arr[:len(arr)-1]
if config.Config.Log.WithStack {
if unwrap != err {
sti, ok := err.(interface{ StackTrace() errors.StackTrace })
if ok {
log.ZWarn(ctx, "rpc server resp", err, "funcName", funcName, "unwrap", unwrap.Error(), "stack", fmt.Sprintf("%+v", err))
if fs := sti.StackTrace(); len(fs) > 0 {
pc := uintptr(fs[0])
fn := runtime.FuncForPC(pc)
file, line := fn.FileLine(pc)
errInfo = &errinfo.ErrorInfo{
Path: file,
Line: uint32(line),
Name: fn.Name(),
Cause: unwrap.Error(),
Warp: nil,
}
if arr := strings.Split(err.Error(), ": "); len(arr) > 1 {
errInfo.Warp = arr[:len(arr)-1]
}
}
}
}

View File

@ -2,13 +2,14 @@ package convert
import (
"context"
"time"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation"
discoveryRegistry "github.com/OpenIMSDK/Open-IM-Server/pkg/discoveryregistry"
sdk "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
"github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/check"
utils2 "github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
utils "github.com/OpenIMSDK/open_utils"
"time"
)
type DBFriend struct {
@ -110,20 +111,26 @@ func (db *DBFriendRequest) DB2PB(ctx context.Context, friendRequests []*relation
if len(friendRequests) > 0 {
userIDs = append(userIDs, friendRequests[0].FromUserID)
}
for _, v := range friendRequests {
userIDs = append(userIDs, v.ToUserID)
}
users, err := db.userCheck.GetUsersInfoMap(ctx, userIDs, true)
if err != nil {
return nil, err
}
for _, v := range friendRequests {
pbFriendRequest := &sdk.FriendRequest{}
pbFriendRequest.FromUserID = users[v.FromUserID].UserID
pbFriendRequest.FromNickname = users[v.FromUserID].Nickname
pbFriendRequest.FromFaceURL = users[v.FromUserID].FaceURL
pbFriendRequest.FromGender = users[v.FromUserID].Gender
pbFriendRequest.ToUserID = users[v.ToUserID].UserID
pbFriendRequest.ToNickname = users[v.ToUserID].Nickname
pbFriendRequest.ToFaceURL = users[v.ToUserID].FaceURL
pbFriendRequest.ToGender = users[v.ToUserID].Gender
pbFriendRequest.CreateTime = db.CreateTime.Unix()
pbFriendRequest.HandleTime = db.HandleTime.Unix()
pbFriendRequest.CreateTime = v.CreateTime.Unix()
pbFriendRequest.HandleTime = v.HandleTime.Unix()
pbFriendRequest.HandlerUserID = v.HandlerUserID
PBFriendRequests = append(PBFriendRequests, pbFriendRequest)
}
return

10
pkg/utils/page.go Normal file
View File

@ -0,0 +1,10 @@
package utils
import "github.com/OpenIMSDK/Open-IM-Server/pkg/proto/sdkws"
func GetPage(pagination *sdkws.RequestPagination) (pageNumber, showNumber int32) {
if pagination != nil {
return pagination.PageNumber, pagination.ShowNumber
}
return
}