mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-16 06:49:02 +08:00
superGroupMaxSeq
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package apiThird
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/common/db"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/common/token_verify"
|
||||
"Open_IM/pkg/utils"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
/**
|
||||
* FCM第三方上报Token
|
||||
*/
|
||||
type FcmUpdateTokenReq struct {
|
||||
OperationID string `json:"operationID"`
|
||||
Platform int `json:"platform" binding:"required,min=1,max=2"` //only for ios + android
|
||||
FcmToken string `json:"fcmToken"`
|
||||
}
|
||||
|
||||
func FcmUpdateToken(c *gin.Context) {
|
||||
var (
|
||||
req FcmUpdateTokenReq
|
||||
)
|
||||
if err := c.Bind(&req); err != nil {
|
||||
log.NewError("0", utils.GetSelfFuncName(), "BindJSON failed ", err.Error())
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), req)
|
||||
|
||||
ok, UserId, errInfo := token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID)
|
||||
if !ok {
|
||||
errMsg := req.OperationID + " " + "GetUserIDFromToken failed " + errInfo + " token:" + c.Request.Header.Get("token")
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
return
|
||||
}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), req, UserId)
|
||||
//逻辑处理开始
|
||||
err := db.DB.SetFcmToken(UserId, int(req.Platform), req.FcmToken, 0)
|
||||
if err != nil {
|
||||
errMsg := req.OperationID + " " + "SetFcmToken failed " + err.Error() + " token:" + c.Request.Header.Get("token")
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
return
|
||||
}
|
||||
//逻辑处理完毕
|
||||
c.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": ""})
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package push
|
||||
|
||||
import (
|
||||
"Open_IM/internal/push"
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/db"
|
||||
"context"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
firebase "firebase.google.com/go"
|
||||
"firebase.google.com/go/messaging"
|
||||
"google.golang.org/api/option"
|
||||
)
|
||||
|
||||
type Fcm struct {
|
||||
}
|
||||
|
||||
var (
|
||||
FcmClient *Fcm
|
||||
FcmMsgCli *messaging.Client
|
||||
)
|
||||
|
||||
func init() {
|
||||
//FcmClient = newFcmClient()
|
||||
}
|
||||
|
||||
func newFcmClient() *Fcm {
|
||||
opt := option.WithCredentialsFile(filepath.Join(config.Root, "config", config.Config.Push.Fcm.ServiceAccount))
|
||||
fcmApp, err := firebase.NewApp(context.Background(), nil, opt)
|
||||
if err != nil {
|
||||
log.Println("error initializing app: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
//授权
|
||||
// fcmClient, err := fcmApp.Auth(context.Background())
|
||||
// if err != nil {
|
||||
// log.Println("error getting Auth client: %v\n", err)
|
||||
// return
|
||||
// }
|
||||
// log.Printf("%#v\r\n", fcmClient)
|
||||
ctx := context.Background()
|
||||
FcmMsgCli, err = fcmApp.Messaging(ctx)
|
||||
if err != nil {
|
||||
log.Fatalf("error getting Messaging client: %v\n", err)
|
||||
return nil
|
||||
}
|
||||
log.Println(FcmMsgCli)
|
||||
return &Fcm{}
|
||||
}
|
||||
|
||||
func (f *Fcm) Push(accounts []string, alert, detailContent, operationID string, opts push.PushOpts) (string, error) {
|
||||
//需要一个客户端的Token
|
||||
// accounts->registrationToken
|
||||
Tokens := make([]string, 0)
|
||||
for _, account := range accounts {
|
||||
IosfcmToken, IosErr := db.DB.GetFcmToken(account, 1)
|
||||
AndroidfcmToken, AndroidErr := db.DB.GetFcmToken(account, 2)
|
||||
if IosErr == nil {
|
||||
Tokens = append(Tokens, IosfcmToken)
|
||||
}
|
||||
if AndroidErr == nil {
|
||||
Tokens = append(Tokens, AndroidfcmToken)
|
||||
}
|
||||
}
|
||||
tokenlen := len(Tokens)
|
||||
// 500组为一个推送,我们用400好了
|
||||
limit := 400
|
||||
pages := int((tokenlen-1)/limit + 1)
|
||||
Success := 0
|
||||
Fail := 0
|
||||
for i := 0; i < pages; i++ {
|
||||
Msg := new(messaging.MulticastMessage)
|
||||
Msg.Notification.Body = detailContent
|
||||
Msg.Notification.Title = alert
|
||||
ctx := context.Background()
|
||||
max := (i+1)*limit - 1
|
||||
if max >= tokenlen {
|
||||
max = tokenlen - 1
|
||||
}
|
||||
Msg.Tokens = Tokens[i*limit : max]
|
||||
//SendMulticast sends the given multicast message to all the FCM registration tokens specified.
|
||||
//The tokens array in MulticastMessage may contain up to 500 tokens.
|
||||
//SendMulticast uses the `SendAll()` function to send the given message to all the target recipients.
|
||||
//The responses list obtained from the return value corresponds to the order of the input tokens.
|
||||
//An error from SendMulticast indicates a total failure -- i.e.
|
||||
//the message could not be sent to any of the recipients.
|
||||
//Partial failures are indicated by a `BatchResponse` return value.
|
||||
response, err := FcmMsgCli.SendMulticast(ctx, Msg)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
Success = Success + response.SuccessCount
|
||||
Fail = Fail + response.FailureCount
|
||||
}
|
||||
return strconv.Itoa(Success) + " Success," + strconv.Itoa(Fail) + " Fail", nil
|
||||
}
|
||||
@@ -8,6 +8,7 @@ package logic
|
||||
|
||||
import (
|
||||
pusher "Open_IM/internal/push"
|
||||
fcm "Open_IM/internal/push/fcm"
|
||||
"Open_IM/internal/push/getui"
|
||||
jpush "Open_IM/internal/push/jpush"
|
||||
"Open_IM/pkg/common/config"
|
||||
@@ -41,6 +42,10 @@ func init() {
|
||||
if config.Config.Push.Jpns.Enable {
|
||||
offlinePusher = jpush.JPushClient
|
||||
}
|
||||
|
||||
if config.Config.Push.Fcm.Enable {
|
||||
offlinePusher = fcm.FcmClient
|
||||
}
|
||||
}
|
||||
|
||||
func Run() {
|
||||
|
||||
@@ -68,13 +68,14 @@ func isMessageHasReadEnabled(pb *pbChat.SendMsgReq) (bool, int32, string) {
|
||||
return true, 0, ""
|
||||
}
|
||||
|
||||
func userRelationshipVerification(data *pbChat.SendMsgReq) (bool, int32, string) {
|
||||
if data.MsgData.SessionType == constant.SingleChatType {
|
||||
func messageVerification(data *pbChat.SendMsgReq) (bool, int32, string, []string) {
|
||||
switch data.MsgData.SessionType {
|
||||
case constant.SingleChatType:
|
||||
if utils.IsContain(data.MsgData.SendID, config.Config.Manager.AppManagerUid) {
|
||||
return true, 0, ""
|
||||
return true, 0, "", nil
|
||||
}
|
||||
if data.MsgData.ContentType <= constant.FriendApplicationNotification && data.MsgData.ContentType >= constant.FriendApplicationApprovedNotification {
|
||||
return true, 0, ""
|
||||
return true, 0, "", nil
|
||||
}
|
||||
log.NewDebug(data.OperationID, config.Config.MessageVerify.FriendVerify)
|
||||
reqGetBlackIDListFromCache := &cacheRpc.GetBlackIDListFromCacheReq{UserID: data.MsgData.RecvID, OperationID: data.OperationID}
|
||||
@@ -82,7 +83,7 @@ func userRelationshipVerification(data *pbChat.SendMsgReq) (bool, int32, string)
|
||||
if etcdConn == nil {
|
||||
errMsg := data.OperationID + "getcdv3.GetConn == nil"
|
||||
log.NewError(data.OperationID, errMsg)
|
||||
return true, 0, ""
|
||||
return true, 0, "", nil
|
||||
}
|
||||
|
||||
cacheClient := cacheRpc.NewCacheClient(etcdConn)
|
||||
@@ -94,7 +95,7 @@ func userRelationshipVerification(data *pbChat.SendMsgReq) (bool, int32, string)
|
||||
log.NewError(data.OperationID, "GetBlackIDListFromCache rpc logic call failed ", cacheResp.String())
|
||||
} else {
|
||||
if utils.IsContain(data.MsgData.SendID, cacheResp.UserIDList) {
|
||||
return false, 600, "in black list"
|
||||
return false, 600, "in black list", nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,7 +106,7 @@ func userRelationshipVerification(data *pbChat.SendMsgReq) (bool, int32, string)
|
||||
if etcdConn == nil {
|
||||
errMsg := data.OperationID + "getcdv3.GetConn == nil"
|
||||
log.NewError(data.OperationID, errMsg)
|
||||
return true, 0, ""
|
||||
return true, 0, "", nil
|
||||
}
|
||||
cacheClient := cacheRpc.NewCacheClient(etcdConn)
|
||||
cacheResp, err := cacheClient.GetFriendIDListFromCache(context.Background(), reqGetFriendIDListFromCache)
|
||||
@@ -116,18 +117,49 @@ func userRelationshipVerification(data *pbChat.SendMsgReq) (bool, int32, string)
|
||||
log.NewError(data.OperationID, "GetFriendIDListFromCache rpc logic call failed ", cacheResp.String())
|
||||
} else {
|
||||
if !utils.IsContain(data.MsgData.SendID, cacheResp.UserIDList) {
|
||||
return false, 601, "not friend"
|
||||
return false, 601, "not friend", nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return true, 0, ""
|
||||
return true, 0, "", nil
|
||||
} else {
|
||||
return true, 0, ""
|
||||
return true, 0, "", nil
|
||||
}
|
||||
case constant.GroupChatType:
|
||||
fallthrough
|
||||
case constant.SuperGroupChatType:
|
||||
getGroupMemberIDListFromCacheReq := &pbCache.GetGroupMemberIDListFromCacheReq{OperationID: data.OperationID, GroupID: data.MsgData.GroupID}
|
||||
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName, data.OperationID)
|
||||
if etcdConn == nil {
|
||||
errMsg := data.OperationID + "getcdv3.GetConn == nil"
|
||||
log.NewError(data.OperationID, errMsg)
|
||||
//return returnMsg(&replay, pb, 201, errMsg, "", 0)
|
||||
return false, 201, errMsg, nil
|
||||
}
|
||||
client := pbCache.NewCacheClient(etcdConn)
|
||||
cacheResp, err := client.GetGroupMemberIDListFromCache(context.Background(), getGroupMemberIDListFromCacheReq)
|
||||
if err != nil {
|
||||
log.NewError(data.OperationID, "GetGroupMemberIDListFromCache rpc call failed ", err.Error())
|
||||
//return returnMsg(&replay, pb, 201, "GetGroupMemberIDListFromCache failed", "", 0)
|
||||
return false, 201, err.Error(), nil
|
||||
}
|
||||
if cacheResp.CommonResp.ErrCode != 0 {
|
||||
log.NewError(data.OperationID, "GetGroupMemberIDListFromCache rpc logic call failed ", cacheResp.String())
|
||||
//return returnMsg(&replay, pb, 201, "GetGroupMemberIDListFromCache logic failed", "", 0)
|
||||
return false, cacheResp.CommonResp.ErrCode, cacheResp.CommonResp.ErrMsg, nil
|
||||
}
|
||||
if !token_verify.IsManagerUserID(data.MsgData.SendID) {
|
||||
if !utils.IsContain(data.MsgData.SendID, cacheResp.UserIDList) {
|
||||
//return returnMsg(&replay, pb, 202, "you are not in group", "", 0)
|
||||
return false, 202, "you are not in group", nil
|
||||
}
|
||||
}
|
||||
return true, 0, "", cacheResp.UserIDList
|
||||
|
||||
} else {
|
||||
return true, 0, ""
|
||||
default:
|
||||
return true, 0, "", nil
|
||||
}
|
||||
|
||||
}
|
||||
func (rpc *rpcChat) encapsulateMsgData(msg *sdk_ws.MsgData) {
|
||||
msg.ServerMsgID = GetMsgID(msg.SendID)
|
||||
@@ -186,7 +218,7 @@ func (rpc *rpcChat) SendMsg(_ context.Context, pb *pbChat.SendMsgReq) (*pbChat.S
|
||||
if !flag {
|
||||
return returnMsg(&replay, pb, errCode, errMsg, "", 0)
|
||||
}
|
||||
flag, errCode, errMsg = userRelationshipVerification(pb)
|
||||
flag, errCode, errMsg, _ = messageVerification(pb)
|
||||
log.Info(pb.OperationID, "userRelationshipVerification ", flag)
|
||||
if !flag {
|
||||
return returnMsg(&replay, pb, errCode, errMsg, "", 0)
|
||||
@@ -257,25 +289,11 @@ func (rpc *rpcChat) SendMsg(_ context.Context, pb *pbChat.SendMsgReq) (*pbChat.S
|
||||
log.NewDebug(pb.OperationID, utils.GetSelfFuncName(), "callbackBeforeSendSingleMsg result", "end rpc and return", callbackResp)
|
||||
return returnMsg(&replay, pb, int32(callbackResp.ErrCode), callbackResp.ErrMsg, "", 0)
|
||||
}
|
||||
getGroupMemberIDListFromCacheReq := &pbCache.GetGroupMemberIDListFromCacheReq{OperationID: pb.OperationID, GroupID: pb.MsgData.GroupID}
|
||||
etcdConn := getcdv3.GetConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImCacheName, msgToMQSingle.OperationID)
|
||||
if etcdConn == nil {
|
||||
errMsg := msgToMQSingle.OperationID + "getcdv3.GetConn == nil"
|
||||
log.NewError(msgToMQSingle.OperationID, errMsg)
|
||||
return returnMsg(&replay, pb, 201, errMsg, "", 0)
|
||||
var memberUserIDList []string
|
||||
if flag, errCode, errMsg, memberUserIDList = messageVerification(pb); !flag {
|
||||
return returnMsg(&replay, pb, errCode, errMsg, "", 0)
|
||||
}
|
||||
client := pbCache.NewCacheClient(etcdConn)
|
||||
cacheResp, err := client.GetGroupMemberIDListFromCache(context.Background(), getGroupMemberIDListFromCacheReq)
|
||||
if err != nil {
|
||||
log.NewError(pb.OperationID, "GetGroupMemberIDListFromCache rpc call failed ", err.Error())
|
||||
return returnMsg(&replay, pb, 201, "GetGroupMemberIDListFromCache failed", "", 0)
|
||||
}
|
||||
if cacheResp.CommonResp.ErrCode != 0 {
|
||||
log.NewError(pb.OperationID, "GetGroupMemberIDListFromCache rpc logic call failed ", cacheResp.String())
|
||||
return returnMsg(&replay, pb, 201, "GetGroupMemberIDListFromCache logic failed", "", 0)
|
||||
}
|
||||
memberUserIDList := cacheResp.UserIDList
|
||||
log.Debug(pb.OperationID, "GetGroupAllMember userID list", cacheResp.UserIDList, "len: ", len(cacheResp.UserIDList))
|
||||
log.Debug(pb.OperationID, "GetGroupAllMember userID list", memberUserIDList, "len: ", len(memberUserIDList))
|
||||
var addUidList []string
|
||||
switch pb.MsgData.ContentType {
|
||||
case constant.MemberKickedNotification:
|
||||
@@ -301,11 +319,6 @@ func (rpc *rpcChat) SendMsg(_ context.Context, pb *pbChat.SendMsgReq) (*pbChat.S
|
||||
if len(addUidList) > 0 {
|
||||
memberUserIDList = append(memberUserIDList, addUidList...)
|
||||
}
|
||||
if !token_verify.IsManagerUserID(pb.MsgData.SendID) {
|
||||
if !utils.IsContain(pb.MsgData.SendID, memberUserIDList) {
|
||||
return returnMsg(&replay, pb, 202, "you are not in group", "", 0)
|
||||
}
|
||||
}
|
||||
m := make(map[string][]string, 2)
|
||||
m[constant.OnlineStatus] = memberUserIDList
|
||||
log.Debug(pb.OperationID, "send msg cost time1 ", db.GetCurrentTimestampByMill()-newTime, pb.MsgData.ClientMsgID)
|
||||
@@ -440,6 +453,9 @@ func (rpc *rpcChat) SendMsg(_ context.Context, pb *pbChat.SendMsgReq) (*pbChat.S
|
||||
log.NewDebug(pb.OperationID, utils.GetSelfFuncName(), "callbackBeforeSendSuperGroupMsg result", "end rpc and return", callbackResp)
|
||||
return returnMsg(&replay, pb, int32(callbackResp.ErrCode), callbackResp.ErrMsg, "", 0)
|
||||
}
|
||||
if flag, errCode, errMsg, _ = messageVerification(pb); !flag {
|
||||
return returnMsg(&replay, pb, errCode, errMsg, "", 0)
|
||||
}
|
||||
msgToMQSingle.MsgData = pb.MsgData
|
||||
log.NewInfo(msgToMQSingle.OperationID, msgToMQSingle)
|
||||
err1 := rpc.sendMsgToKafka(&msgToMQSingle, msgToMQSingle.MsgData.GroupID, constant.OnlineStatus)
|
||||
@@ -447,7 +463,6 @@ func (rpc *rpcChat) SendMsg(_ context.Context, pb *pbChat.SendMsgReq) (*pbChat.S
|
||||
log.NewError(msgToMQSingle.OperationID, "kafka send msg err:RecvID", msgToMQSingle.MsgData.RecvID, msgToMQSingle.String())
|
||||
return returnMsg(&replay, pb, 201, "kafka send msg err", "", 0)
|
||||
}
|
||||
|
||||
// callback
|
||||
callbackResp = callbackAfterSendSingleMsg(pb)
|
||||
if callbackResp.ErrCode != 0 {
|
||||
|
||||
Reference in New Issue
Block a user