From 4efdabc1ffca43f821da7ca630d7b28052fce8ae Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Fri, 8 Apr 2022 15:40:07 +0800 Subject: [PATCH] getui --- config/config.yaml | 10 ++ internal/cms_api/group/group.go | 5 +- internal/push/getui/push.go | 146 +++++++++++++++++++++++++- internal/push/jpush/push.go | 33 ++++-- internal/push/logic/push_to_client.go | 19 +++- internal/push/push_interface.go | 5 +- internal/rpc/group/callback.go | 4 + internal/rpc/group/group.go | 5 + internal/rpc/msg/tag_send_msg.go | 2 +- pkg/cms_api_struct/group.go | 28 +++-- pkg/common/config/config.go | 9 ++ pkg/common/constant/constant.go | 2 +- pkg/common/db/redisModel.go | 11 ++ pkg/common/http/http_resp.go | 4 +- 14 files changed, 240 insertions(+), 43 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index 452d529a6..003d6f969 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -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"] diff --git a/internal/cms_api/group/group.go b/internal/cms_api/group/group.go index c7013c2bd..740fd5821 100644 --- a/internal/cms_api/group/group.go +++ b/internal/cms_api/group/group.go @@ -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, }) diff --git a/internal/push/getui/push.go b/internal/push/getui/push.go index 18d6fd0fc..f834337c8 100644 --- a/internal/push/getui/push.go +++ b/internal/push/getui/push.go @@ -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 } diff --git a/internal/push/jpush/push.go b/internal/push/jpush/push.go index fbcdb9884..439ee845f 100644 --- a/internal/push/jpush/push.go +++ b/internal/push/jpush/push.go @@ -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 } diff --git a/internal/push/logic/push_to_client.go b/internal/push/logic/push_to_client.go index a5a6397f2..e527448ce 100644 --- a/internal/push/logic/push_to_client.go +++ b/internal/push/logic/push_to_client.go @@ -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)) } } diff --git a/internal/push/push_interface.go b/internal/push/push_interface.go index ea752d149..f89fcf664 100644 --- a/internal/push/push_interface.go +++ b/internal/push/push_interface.go @@ -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) } diff --git a/internal/rpc/group/callback.go b/internal/rpc/group/callback.go index 8e0fcfb7b..1e9e612a6 100644 --- a/internal/rpc/group/callback.go +++ b/internal/rpc/group/callback.go @@ -7,3 +7,7 @@ import ( func callbackBeforeCreateGroup(req *pbGroup.CreateGroupReq) (bool, error) { return true, nil } + +func callbackAfterCreateGroup(req *pbGroup.CreateGroupReq) { + +} diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index edece9829..6fa270394 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -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 } diff --git a/internal/rpc/msg/tag_send_msg.go b/internal/rpc/msg/tag_send_msg.go index 7f0550d62..12da1dd44 100644 --- a/internal/rpc/msg/tag_send_msg.go +++ b/internal/rpc/msg/tag_send_msg.go @@ -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 diff --git a/pkg/cms_api_struct/group.go b/pkg/cms_api_struct/group.go index 9d161b622..7d3333127 100644 --- a/pkg/cms_api_struct/group.go +++ b/pkg/cms_api_struct/group.go @@ -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 { - } diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go index 8b174f993..5e8eb1e21 100644 --- a/pkg/common/config/config.go +++ b/pkg/common/config/config.go @@ -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 { diff --git a/pkg/common/constant/constant.go b/pkg/common/constant/constant.go index 98dfa6092..fdee4f2b7 100644 --- a/pkg/common/constant/constant.go +++ b/pkg/common/constant/constant.go @@ -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 diff --git a/pkg/common/db/redisModel.go b/pkg/common/db/redisModel.go index c5594da9a..2faa9a6d8 100644 --- a/pkg/common/db/redisModel.go +++ b/pkg/common/db/redisModel.go @@ -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 +} diff --git a/pkg/common/http/http_resp.go b/pkg/common/http/http_resp.go index cdbf04463..8292b6884 100644 --- a/pkg/common/http/http_resp.go +++ b/pkg/common/http/http_resp.go @@ -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]) }