This commit is contained in:
wangchuxiao 2022-04-08 15:40:07 +08:00
parent 59cc45eaab
commit 4efdabc1ff
14 changed files with 240 additions and 43 deletions

View File

@ -165,6 +165,7 @@ longconnsvr:
websocketMaxMsgLen: 4096
websocketTimeOut: 10
## 推送只能开启一个
push:
tpns: #腾讯推送,暂未测试 暂不要使用
ios:
@ -173,11 +174,20 @@ push:
android:
accessID: 111
secretKey: 111
enable: false
jpns: #极光推送 在极光后台申请后,修改以下四项,必须修改
appKey: cf47465a368f24c659608e7e
masterSecret: 02204efe3f3832947a236ee5
pushUrl: "https://api.jpush.cn/v3/push"
pushIntent: "intent:#Intent;component=io.openim.app.enterprisechat/io.openim.app.enterprisechat.MainActivity;end"
enable: false
getui: #个推推送,暂未测试 暂不要使用
pushUrl: "https://restapi.getui.com/v2/$appId"
sign: ""
appKey: ""
enable: true
intent: ""
manager:
#app管理员userID和对应的secret 建议修改。 用于管理后台登录也可以用户管理后台对应的api
appManagerUid: [ "openIM123456","openIM654321", "openIM333", "openIMAdmin"]

View File

@ -43,6 +43,7 @@ func GetGroupById(c *gin.Context) {
resp.ProfilePhoto = respPb.CMSGroup.GroupInfo.FaceURL
resp.GroupMasterName = respPb.CMSGroup.GroupMasterName
resp.GroupMasterId = respPb.CMSGroup.GroupMasterId
resp.IsBanChat = constant.GroupIsBanChat(respPb.CMSGroup.GroupInfo.Status)
openIMHttp.RespHttp200(c, constant.OK, resp)
}
@ -74,7 +75,7 @@ func GetGroups(c *gin.Context) {
GroupMasterName: v.GroupMasterName,
GroupMasterId: v.GroupMasterId,
CreateTime: (utils.UnixSecondToTime(int64(v.GroupInfo.CreateTime))).String(),
IsBanChat: false,
IsBanChat: constant.GroupIsBanChat(v.GroupInfo.Status),
IsBanPrivateChat: false,
ProfilePhoto: v.GroupInfo.FaceURL,
})
@ -114,7 +115,7 @@ func GetGroupByName(c *gin.Context) {
GroupMasterName: v.GroupMasterName,
GroupMasterId: v.GroupMasterId,
CreateTime: (utils.UnixSecondToTime(int64(v.GroupInfo.CreateTime))).String(),
IsBanChat: false,
IsBanChat: constant.GroupIsBanChat(v.GroupInfo.Status),
IsBanPrivateChat: false,
ProfilePhoto: v.GroupInfo.FaceURL,
})

View File

@ -1,12 +1,148 @@
package getui
type Getui struct {
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/db"
"Open_IM/pkg/common/log"
"Open_IM/pkg/utils"
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"time"
)
var (
GetuiClient *Getui
)
func init() {
GetuiClient = newGetuiClient()
}
func (g *Getui) Push(userIDList []string, alert, detailContent, platform string) (resp string, err error) {
return "", nil
type Getui struct{}
type GetuiCommonResp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func (g *Getui) Auth(apiKey, secretKey string, timeStamp int64) (token string, err error) {
return "", nil
type AuthReq struct {
Sign string `json:"sign"`
Timestamp string `json:"timestamp"`
Appkey string `json:"appkey"`
}
type AuthResp struct {
ExpireTime string `json:"expire_time"`
Token string `json:"token"`
}
type PushReq struct {
RequestID string `json:"request_id"`
Audience struct {
Cid []string `json:"cid"`
} `json:"audience"`
PushMssage struct {
Notification Notification `json:"notification"`
} `json:"push_message"`
}
type Notification struct {
Title string `json:"title"`
Body string `json:"body"`
ClickType string `json:"click_type"`
Intent string `json:"intent"`
Url string `json:"url"`
}
type PushResp struct {
}
func newGetuiClient() *Getui {
return &Getui{}
}
func (g *Getui) Push(userIDList []string, alert, detailContent, platform, operationID string) (resp string, err error) {
token, err := db.DB.GetGetuiToken()
if err != nil {
log.NewError(operationID, utils.OperationIDGenerator(), "GetGetuiToken", err.Error())
}
if token == "" || err != nil {
token, expireTime, err := g.Auth(config.Config.Push.Getui.AppKey, config.Config.Push.Getui.Sign, operationID, time.Now().Unix())
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
log.NewInfo(operationID, "getui", utils.GetSelfFuncName(), token, expireTime, err)
err = db.DB.SetGetuiToken(token, expireTime-time.Now().Unix()-20)
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
}
pushReq := PushReq{
RequestID: utils.OperationIDGenerator(),
Audience: struct {
Cid []string `json:"cid"`
}{Cid: []string{userIDList[0]}},
}
pushReq.PushMssage.Notification = Notification{
Title: alert,
Body: alert,
ClickType: "none",
}
if config.Config.Push.Getui.Intent != "" {
pushReq.PushMssage.Notification.Intent = config.Config.Push.Getui.Intent
pushReq.PushMssage.Notification.ClickType = "intent"
}
pushResp := PushResp{}
err = g.request(pushReq, token, &pushResp, operationID)
if err != nil {
return "", utils.Wrap(err, "")
}
respBytes, err := json.Marshal(pushResp)
return string(respBytes), err
}
func (g *Getui) Auth(appKey, sign, operationID string, timeStamp int64) (token string, expireTime int64, err error) {
log.NewInfo(operationID, utils.GetSelfFuncName(), appKey, sign, timeStamp)
reqAuth := AuthReq{
Sign: sign,
Timestamp: strconv.Itoa(int(timeStamp)),
Appkey: appKey,
}
respAuth := AuthResp{}
err = g.request(reqAuth, "", &respAuth, operationID)
expire, err := strconv.Atoi(respAuth.ExpireTime)
return respAuth.Token, int64(expire), err
}
func (g *Getui) request(content interface{}, token string, returnStruct interface{}, operationID string) error {
con, err := json.Marshal(content)
if err != nil {
return err
}
client := &http.Client{}
req, err := http.NewRequest("POST", config.Config.Push.Getui.PushUrl, bytes.NewBuffer(con))
if err != nil {
return err
}
req.Header.Set(token, token)
req.Header.Set("content-type", "application/json")
req.Header.Set("content-type", "charset=utf-8")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
log.NewInfo(operationID, "getui", utils.GetSelfFuncName(), string(result))
if err := json.Unmarshal(result, returnStruct); err != nil {
return err
}
return nil
}

View File

@ -10,14 +10,29 @@ import (
"net/http"
)
type JPushResp struct {
var (
JPushClient *JPush
)
func init() {
JPushClient = newGetuiClient()
}
type JPush struct {
type JPush struct{}
func newGetuiClient() *JPush {
return &JPush{}
}
func JGAccountListPush(accounts []string, alert, detailContent, platform string) ([]byte, error) {
func (j *JPush) Auth(apiKey, secretKey string, timeStamp int64) (token string, err error) {
return token, nil
}
func (j *JPush) SetAlias(cid, alias string) (resp string, err error) {
return resp, nil
}
func (j *JPush) Push(accounts []string, alert, detailContent, platform, operationID string) (string, error) {
var pf requestBody.Platform
_ = pf.SetPlatform(platform)
var au requestBody.Audience
@ -37,25 +52,23 @@ func JGAccountListPush(accounts []string, alert, detailContent, platform string)
con, err := json.Marshal(po)
if err != nil {
return nil, err
return "", err
}
client := &http.Client{}
req, err := http.NewRequest("POST", config.Config.Push.Jpns.PushUrl, bytes.NewBuffer(con))
if err != nil {
return nil, err
return "", err
}
req.Header.Set("Authorization", common.GetAuthorization(config.Config.Push.Jpns.AppKey, config.Config.Push.Jpns.MasterSecret))
resp, err := client.Do(req)
if err != nil {
return nil, err
return "", err
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
return "", err
}
return result, nil
return string(result), nil
}

View File

@ -7,7 +7,9 @@
package logic
import (
push "Open_IM/internal/push/jpush"
pusher "Open_IM/internal/push"
"Open_IM/internal/push/getui"
jpush "Open_IM/internal/push/jpush"
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/log"
@ -99,12 +101,21 @@ func MsgToUser(pushMsg *pbPush.PushMsgReq) {
content = constant.ContentType2PushContent[constant.Common]
}
}
pushResult, err := push.JGAccountListPush(UIDList, content, jsonCustomContent, constant.PlatformIDToName(t))
var offlinePusher pusher.OfflinePusher
if config.Config.Push.Getui.Enable {
offlinePusher = getui.GetuiClient
}
if config.Config.Push.Jpns.Enable {
offlinePusher = jpush.JPushClient
}
if offlinePusher == nil {
offlinePusher = jpush.JPushClient
}
pushResult, err := offlinePusher.Push(UIDList, content, jsonCustomContent, constant.PlatformIDToName(t), pushMsg.OperationID)
if err != nil {
log.NewError(pushMsg.OperationID, "offline push error", pushMsg.String(), err.Error(), constant.PlatformIDToName(t))
} else {
log.NewDebug(pushMsg.OperationID, "offline push return result is ", string(pushResult), pushMsg.MsgData, constant.PlatformIDToName(t))
log.NewDebug(pushMsg.OperationID, "offline push return result is ", pushResult, pushMsg.MsgData, constant.PlatformIDToName(t))
}
}

View File

@ -1,6 +1,5 @@
package push
type offlinePusher interface {
auth(apiKey, secretKey string, timeStamp int64) (token string, err error)
push(userIDList []string, alert, detailContent, platform string) (resp string, err error)
type OfflinePusher interface {
Push(userIDList []string, alert, detailContent, platform, operationID string) (resp string, err error)
}

View File

@ -7,3 +7,7 @@ import (
func callbackBeforeCreateGroup(req *pbGroup.CreateGroupReq) (bool, error) {
return true, nil
}
func callbackAfterCreateGroup(req *pbGroup.CreateGroupReq) {
}

View File

@ -668,6 +668,7 @@ func (s *groupServer) GetGroupById(_ context.Context, req *pbGroup.GetGroupByIdR
Status: group.Status,
CreatorUserID: group.CreatorUserID,
GroupType: group.GroupType,
CreateTime: uint32(group.CreateTime.Unix()),
}
groupMember, err := imdb.GetGroupMaster(group.GroupID)
if err != nil {
@ -677,6 +678,7 @@ func (s *groupServer) GetGroupById(_ context.Context, req *pbGroup.GetGroupByIdR
resp.CMSGroup.GroupMasterName = groupMember.Nickname
resp.CMSGroup.GroupMasterId = groupMember.UserID
resp.CMSGroup.GroupInfo.CreatorUserID = group.CreatorUserID
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return resp, nil
}
@ -704,6 +706,7 @@ func (s *groupServer) GetGroup(_ context.Context, req *pbGroup.GetGroupReq) (*pb
groupMember, err := imdb.GetGroupMaster(v.GroupID)
if err != nil {
log.NewError(req.OperationID, utils.GetSelfFuncName(), "GetGroupMaster error", err.Error())
continue
}
resp.CMSGroups = append(resp.CMSGroups, &pbGroup.CMSGroup{
GroupInfo: &open_im_sdk.GroupInfo{
@ -713,11 +716,13 @@ func (s *groupServer) GetGroup(_ context.Context, req *pbGroup.GetGroupReq) (*pb
OwnerUserID: v.CreatorUserID,
Status: v.Status,
CreatorUserID: v.CreatorUserID,
CreateTime: uint32(v.CreateTime.Unix()),
},
GroupMasterName: groupMember.Nickname,
GroupMasterId: groupMember.UserID,
})
}
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "resp: ", resp.String())
return resp, nil
}

View File

@ -13,7 +13,7 @@ import (
)
func TagSendMessage(operationID, sendID, recvID, content string, senderPlatformID int32) {
log.NewInfo(operationID, utils.GetSelfFuncName(), "args: ", sendID, recvID, content)
log.NewInfo(operationID, utils.GetSelfFuncName(), "args: ", sendID, recvID, content, senderPlatformID)
var req pbChat.SendMsgReq
var msgData pbCommon.MsgData
msgData.SendID = sendID

View File

@ -8,7 +8,7 @@ type GroupResponse struct {
CreateTime string `json:"create_time"`
IsBanChat bool `json:"is_ban_chat"`
IsBanPrivateChat bool `json:"is_ban_private_chat"`
ProfilePhoto string `json:"profile_photo"`
ProfilePhoto string `json:"profile_photo"`
}
type GetGroupByIdRequest struct {
@ -63,7 +63,6 @@ type SetGroupMemberRequest struct {
}
type SetGroupMemberRespones struct {
}
type BanGroupChatRequest struct {
@ -88,7 +87,7 @@ type DeleteGroupResponse struct {
}
type GetGroupMembersRequest struct {
GroupId string `form:"group_id" binding:"required"`
GroupId string `form:"group_id" binding:"required"`
UserName string `form:"user_name"`
RequestPagination
}
@ -96,24 +95,24 @@ type GetGroupMembersRequest struct {
type GroupMemberResponse struct {
MemberPosition int `json:"member_position"`
MemberNickName string `json:"member_nick_name"`
MemberId string `json:"member_id"`
MemberId string `json:"member_id"`
JoinTime string `json:"join_time"`
}
type GetGroupMembersResponse struct {
GroupMembers []GroupMemberResponse `json:"group_members"`
GroupMembers []GroupMemberResponse `json:"group_members"`
ResponsePagination
MemberNums int `json:"member_nums"`
}
type GroupMemberRequest struct {
GroupId string `json:"group_id" binding:"required"`
GroupId string `json:"group_id" binding:"required"`
Members []string `json:"members" binding:"required"`
}
type GroupMemberOperateResponse struct {
Success []string `json:"success"`
Failed []string `json:"failed"`
Failed []string `json:"failed"`
}
type AddGroupMembersRequest struct {
@ -128,19 +127,18 @@ type RemoveGroupMembersRequest struct {
GroupMemberRequest
}
type RemoveGroupMembersResponse struct{
type RemoveGroupMembersResponse struct {
GroupMemberOperateResponse
}
type AlterGroupInfoRequest struct {
GroupID string `json:"group_id"`
GroupName string `json:"group_name"`
Notification string `json:"notification"`
Introduction string `json:"introduction"`
ProfilePhoto string `json:"profile_photo"`
GroupType int `json:"group_type"`
GroupID string `json:"group_id"`
GroupName string `json:"group_name"`
Notification string `json:"notification"`
Introduction string `json:"introduction"`
ProfilePhoto string `json:"profile_photo"`
GroupType int `json:"group_type"`
}
type AlterGroupInfoResponse struct {
}

View File

@ -152,12 +152,21 @@ type config struct {
AccessID string `yaml:"accessID"`
SecretKey string `yaml:"secretKey"`
}
Enable bool `yaml:"enable"`
}
Jpns struct {
AppKey string `yaml:"appKey"`
MasterSecret string `yaml:"masterSecret"`
PushUrl string `yaml:"pushUrl"`
PushIntent string `yaml:"pushIntent"`
Enable bool `yaml:"enable"`
}
Getui struct {
PushUrl string `yaml:"pushUrl"`
Sign string `yaml:"sign"`
AppKey string `yaml:"appkey"`
Enable bool `yaml:"enable"`
Intent string `yaml:"intent"`
}
}
Manager struct {

View File

@ -217,7 +217,7 @@ const (
const FriendAcceptTip = "You have successfully become friends, so start chatting"
func GroupIsBanChat(status int32) bool {
if status != GroupBanChat {
if status != GroupStatusMuted {
return false
}
return true

View File

@ -14,6 +14,7 @@ const (
userMinSeq = "REDIS_USER_MIN_SEQ:"
uidPidToken = "UID_PID_TOKEN_STATUS:"
conversationReceiveMessageOpt = "CON_RECV_MSG_OPT:"
GetuiToken = "GETUI"
)
func (d *DataBases) Exec(cmd string, key interface{}, args ...interface{}) (interface{}, error) {
@ -144,3 +145,13 @@ func (d *DataBases) GetMultiConversationMsgOpt(userID string, conversationIDs []
return m, nil
}
func (d *DataBases) SetGetuiToken(token string, expireTime int64) error {
_, err := d.Exec("SET", GetuiToken, token, "ex", expireTime)
return err
}
func (d *DataBases) GetGetuiToken() (string, error) {
result, err := redis.String(d.Exec("GET", GetuiToken))
return result, err
}

View File

@ -38,6 +38,6 @@ func RespHttp200(ctx *gin.Context, err error, data interface{}) {
}
// warp error
func WrapError(err constant.ErrInfo) error {
return status.Error(codes.Code(err.ErrCode), err.ErrMsg)
func WrapError(err constant.ErrInfo, msg ...string) error {
return status.Error(codes.Code(err.ErrCode), err.ErrMsg+msg[0])
}