Skip to content

Commit

Permalink
opt msg interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwnloyblog committed Jan 7, 2025
1 parent e357e09 commit db58aeb
Show file tree
Hide file tree
Showing 29 changed files with 637 additions and 377 deletions.
4 changes: 4 additions & 0 deletions commons/errs/errorcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ var (
IMErrorCode_APP_FRIEND_APPLY_DECLINE IMErrorCode = 17101
IMErrorCode_APP_FRIEND_APPLY_REPEATED IMErrorCode = 17102
IMErrorCode_APP_FRIEND_CONFIRM_EXPIRED IMErrorCode = 17103

//group
IMErrorCode_APP_GROUP_DEFAULT IMErrorCode = 17200
IMErrorCode_APP_GROUP_MEMBEREXISTED IMErrorCode = 17201
)

var imCode2ApiErrorMap map[IMErrorCode]*ApiErrorMsg = map[IMErrorCode]*ApiErrorMsg{
Expand Down
1 change: 1 addition & 0 deletions commons/pbdefines/appbusiness.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ enum GrpMemberRole{
GrpMember = 0;
GrpCreator = 1;
GrpAdmin = 2;
GrpNotMember = 3;
}

message GroupManagement{
Expand Down
44 changes: 24 additions & 20 deletions commons/pbdefines/pbobjs/appbusiness.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions docs/jim.sql
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ CREATE TABLE `interceptors` (
`request_template` text COMMENT '请求模板',
`succ_template` varchar(200) DEFAULT NULL COMMENT '成功模板',
`is_async` tinyint DEFAULT '0' COMMENT '是否异步',
`conf` varchar(2000) DEFAULT NULL,
`intercept_type` tinyint DEFAULT '0',
`created_time` datetime(3) DEFAULT CURRENT_TIMESTAMP(3) COMMENT '创建时间',
`updated_time` datetime(3) DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) COMMENT '更新时间',
`app_key` varchar(20) NOT NULL COMMENT '应用key',
Expand Down
7 changes: 4 additions & 3 deletions services/apigateway/apis/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"im-server/services/apigateway/models"
"im-server/services/apigateway/services"
"im-server/services/commonservices"
"im-server/services/commonservices/msgtypes"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -233,11 +234,11 @@ func handleFlag(sendMsgReq models.SendMsgReq) int32 {
func handleMentionInfo(mentionInfo *models.MentionInfo) *pbobjs.MentionInfo {
retMention := &pbobjs.MentionInfo{}
if mentionInfo != nil {
if mentionInfo.MentionType == commonservices.MentionType_All {
if mentionInfo.MentionType == msgtypes.MentionType_All {
retMention.MentionType = pbobjs.MentionType_All
} else if mentionInfo.MentionType == commonservices.MentionType_Someone {
} else if mentionInfo.MentionType == msgtypes.MentionType_Someone {
retMention.MentionType = pbobjs.MentionType_Someone
} else if mentionInfo.MentionType == commonservices.MentionType_AllSomeone {
} else if mentionInfo.MentionType == msgtypes.MentionType_AllSomeone {
retMention.MentionType = pbobjs.MentionType_AllAndSomeone
}
if len(mentionInfo.TargetUserIds) > 0 {
Expand Down
1 change: 1 addition & 0 deletions services/apigateway/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ApiGateway struct {
func (ser *ApiGateway) RegisterActors(register gmicro.IActorRegister) {

}

func (ser *ApiGateway) Startup(args map[string]interface{}) {
ser.httpServer = gin.Default()
ser.httpServer.GET("/", func(ctx *gin.Context) {
Expand Down
33 changes: 33 additions & 0 deletions services/appbusiness/apis/group.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package apis

import (
"bytes"
"encoding/base64"
"im-server/commons/errs"
"im-server/commons/pbdefines/pbobjs"
"im-server/commons/tools"
"im-server/services/appbusiness/httputils"
"im-server/services/appbusiness/models"
"im-server/services/appbusiness/services"
"image/png"
"strconv"

"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
)

func CreateGroup(ctx *httputils.HttpContext) {
Expand Down Expand Up @@ -247,3 +254,29 @@ func SetGrpDisplayName(ctx *httputils.HttpContext) {
}
ctx.ResponseSucc(nil)
}

func QryGrpQrCode(ctx *httputils.HttpContext) {
grpId := ctx.Query("group_id")
if grpId == "" {
ctx.ResponseErr(errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
return
}
userId := ctx.CurrentUserId

m := map[string]interface{}{
"action": "join_group",
"group_id": grpId,
"user_id": userId,
}
buf := bytes.NewBuffer([]byte{})
qrCode, _ := qr.Encode(tools.ToJson(m), qr.M, qr.Auto)
qrCode, _ = barcode.Scale(qrCode, 400, 400)
err := png.Encode(buf, qrCode)
if err != nil {
ctx.ResponseErr(errs.IMErrorCode_APP_DEFAULT)
return
}
ctx.ResponseSucc(map[string]string{
"qr_code": base64.StdEncoding.EncodeToString(buf.Bytes()),
})
}
14 changes: 12 additions & 2 deletions services/appbusiness/apis/groupapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@ import (
)

func GroupApply(ctx *httputils.HttpContext) {

req := pbobjs.GroupInviteReq{}
if err := ctx.BindJson(&req); err != nil || req.GroupId == "" {
ctx.ResponseErr(errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
return
}
code := services.GrpJoinApply(ctx.ToRpcCtx(), &req)
if code != errs.IMErrorCode_SUCCESS {
ctx.ResponseErr(code)
return
}
ctx.ResponseSucc(nil)
}

func GroupInvite(ctx *httputils.HttpContext) {
req := pbobjs.GroupInviteReq{}
if err := ctx.BindJson(&req); err != nil {
if err := ctx.BindJson(&req); err != nil || req.GroupId == "" || len(req.MemberIds) <= 0 {
ctx.ResponseErr(errs.IMErrorCode_APP_REQ_BODY_ILLEGAL)
return
}
Expand Down
6 changes: 5 additions & 1 deletion services/appbusiness/apis/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ func SmsLogin(ctx *httputils.HttpContext) {

func GenerateQrCode(ctx *httputils.HttpContext) {
uuidStr := tools.GenerateUUIDString()
qrCode, _ := qr.Encode(uuidStr, qr.M, qr.Auto)
m := map[string]interface{}{
"action": "login",
"code": uuidStr,
}
qrCode, _ := qr.Encode(tools.ToJson(m), qr.M, qr.Auto)
qrCode, _ = barcode.Scale(qrCode, 400, 400)
buf := bytes.NewBuffer([]byte{})
err := png.Encode(buf, qrCode)
Expand Down
2 changes: 2 additions & 0 deletions services/appbusiness/apis/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func LoadAppApis(mux *http.ServeMux) {
RouteRegiste(mux, http.MethodPost, "/jim/users/updsettings", UpdateUserSettings)
RouteRegiste(mux, http.MethodPost, "/jim/users/search", SearchByPhone)
RouteRegiste(mux, http.MethodGet, "/jim/users/info", QryUserInfo)
RouteRegiste(mux, http.MethodGet, "/jim/users/qrcode", QryUserQrCode)

RouteRegiste(mux, http.MethodPost, "/jim/groups/add", CreateGroup)
RouteRegiste(mux, http.MethodPost, "/jim/groups/create", CreateGroup)
Expand All @@ -38,6 +39,7 @@ func LoadAppApis(mux *http.ServeMux) {
RouteRegiste(mux, http.MethodPost, "/jim/groups/members/del", DelGrpMembers)
RouteRegiste(mux, http.MethodGet, "/jim/groups/members/list", QryGrpMembers)
RouteRegiste(mux, http.MethodGet, "/jim/groups/info", QryGroupInfo)
RouteRegiste(mux, http.MethodGet, "/jim/groups/qrcode", QryGrpQrCode)
RouteRegiste(mux, http.MethodPost, "/jim/groups/setgrpannouncement", SetGrpAnnouncement)
RouteRegiste(mux, http.MethodGet, "/jim/groups/getgrpannouncement", GetGrpAnnouncement)
RouteRegiste(mux, http.MethodPost, "/jim/groups/setdisplayname", SetGrpDisplayName)
Expand Down
27 changes: 27 additions & 0 deletions services/appbusiness/apis/user.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package apis

import (
"bytes"
"encoding/base64"
"im-server/commons/errs"
"im-server/commons/pbdefines/pbobjs"
"im-server/commons/tools"
"im-server/services/appbusiness/httputils"
"im-server/services/appbusiness/services"
"image/png"

"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
)

func QryUserInfo(ctx *httputils.HttpContext) {
Expand Down Expand Up @@ -57,3 +64,23 @@ func SearchByPhone(ctx *httputils.HttpContext) {
}
ctx.ResponseSucc(users)
}

func QryUserQrCode(ctx *httputils.HttpContext) {
userId := ctx.CurrentUserId

m := map[string]interface{}{
"action": "add_friend",
"user_id": userId,
}
buf := bytes.NewBuffer([]byte{})
qrCode, _ := qr.Encode(tools.ToJson(m), qr.M, qr.Auto)
qrCode, _ = barcode.Scale(qrCode, 400, 400)
err := png.Encode(buf, qrCode)
if err != nil {
ctx.ResponseErr(errs.IMErrorCode_APP_DEFAULT)
return
}
ctx.ResponseSucc(map[string]string{
"qr_code": base64.StdEncoding.EncodeToString(buf.Bytes()),
})
}
43 changes: 41 additions & 2 deletions services/appbusiness/services/groupservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"im-server/services/appbusiness/storages"
storeModels "im-server/services/appbusiness/storages/models"
"im-server/services/commonservices"
"im-server/services/commonservices/msgtypes"
"time"

"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -69,7 +70,7 @@ func QryGroupInfo(ctx context.Context, groupId string) (errs.IMErrorCode, *pbobj
}
}
// my role
myRole := pbobjs.GrpMemberRole_GrpMember // 0: 群成员;1:群主;2:群管理员
myRole := pbobjs.GrpMemberRole_GrpMember // 0: 群成员;1:群主;2:群管理员;3:非群成员;
if requestId == creator {
myRole = pbobjs.GrpMemberRole_GrpCreator
} else if _, exist := administrators[requestId]; exist {
Expand Down Expand Up @@ -286,6 +287,44 @@ func GrpInviteMembers(ctx context.Context, req *pbobjs.GroupInviteReq) (errs.IME
return errs.IMErrorCode_SUCCESS, results
}

func GrpJoinApply(ctx context.Context, req *pbobjs.GroupInviteReq) errs.IMErrorCode {
userId := bases.GetRequesterIdFromCtx(ctx)
groupId := req.GroupId
//check grp member exists
code, respObj, err := bases.SyncRpcCall(ctx, "g_check_members", groupId, &pbobjs.CheckGroupMembersReq{
GroupId: groupId,
MemberIds: []string{userId},
}, func() proto.Message {
return &pbobjs.CheckGroupMembersResp{}
})
if err == nil && code == errs.IMErrorCode_SUCCESS && respObj != nil {
checkResp, ok := respObj.(*pbobjs.CheckGroupMembersResp)
if ok {
if _, exist := checkResp.MemberIdMap[userId]; exist {
return errs.IMErrorCode_APP_GROUP_MEMBEREXISTED
}
}
}
//add group
code, _, err = bases.SyncRpcCall(ctx, "g_add_members", groupId, &pbobjs.GroupMembersReq{
GroupId: groupId,
MemberIds: []string{userId},
}, nil)
if err != nil || code != errs.IMErrorCode_SUCCESS {
return code
}
//send notify msg
targetUsers := []*pbobjs.UserObj{}
targetUsers = append(targetUsers, GetUser(ctx, userId))
notify := &models.GroupNotify{
Operator: GetUser(ctx, userId),
Members: targetUsers,
Type: models.GroupNotifyType_AddMember,
}
SendGrpNotify(ctx, groupId, notify)
return errs.IMErrorCode_SUCCESS
}

func DelGrpMembers(ctx context.Context, req *pbobjs.GroupMembersReq) errs.IMErrorCode {
requestId := bases.GetRequesterIdFromCtx(ctx)
code, _, err := bases.SyncRpcCall(ctx, "g_del_members", req.GroupId, &pbobjs.GroupMembersReq{
Expand Down Expand Up @@ -358,7 +397,7 @@ func SetGrpAnnouncement(ctx context.Context, req *pbobjs.GrpAnnouncement) errs.I
//send announce msg
flag := commonservices.SetStoreMsg(0)
flag = commonservices.SetCountMsg(flag)
txtMsg := &commonservices.TextMsg{
txtMsg := &msgtypes.TextMsg{
Content: req.Content,
}
commonservices.AsyncGroupMsgOverUpstream(ctx, requestId, req.GroupId, &pbobjs.UpMsg{
Expand Down
2 changes: 2 additions & 0 deletions services/appbusiness/services/userservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func QryUserInfo(ctx context.Context, userId string) (errs.IMErrorCode, *pbobjs.
}
if userId == requestId {
ret.Settings = GetUserSettings(userInfo)
} else {
ret.IsFriend = checkFriend(ctx, requestId, userId)
}
return errs.IMErrorCode_SUCCESS, ret
}
Expand Down
3 changes: 2 additions & 1 deletion services/botmsg/services/botservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"im-server/services/botmsg/storages/models"
"im-server/services/commonservices"
"im-server/services/commonservices/logs"
"im-server/services/commonservices/msgtypes"
"strings"
"time"
)
Expand Down Expand Up @@ -87,7 +88,7 @@ func HandleBotMsg(ctx context.Context, msg *pbobjs.DownMsg) {
if msg.MsgType != "jg:text" || msg.ChannelType != pbobjs.ChannelType_Private {
return
}
txtMsg := &commonservices.TextMsg{}
txtMsg := &msgtypes.TextMsg{}
err := tools.JsonUnMarshal(msg.MsgContent, txtMsg)
if err != nil {
logs.WithContext(ctx).Errorf("text msg illigal. content:%s", string(msg.MsgContent))
Expand Down
1 change: 1 addition & 0 deletions services/commonservices/appinfocache.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type AppInfo struct {
KickMode int `default:"0"`
OpenVisualLog bool `default:"false"`
RecordGlobalConvers bool `default:"false"`
OpenSensitive bool `default:"false"`

//group
IsHideMsgBeforeJoinGroup bool `default:"false"`
Expand Down
5 changes: 5 additions & 0 deletions services/commonservices/dbs/interceptordao.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"time"
)

var (
InterceptorType_Custom int = 0
InterceptorType_Baidu int = 1
)

type InterceptorDao struct {
ID int64 `gorm:"primary_key"`
Name string `gorm:"name"`
Expand Down
Loading

0 comments on commit db58aeb

Please sign in to comment.