Compare commits

..

1 Commits

Author SHA1 Message Date
Xinwei Xiong (cubxxw) 6cf3b321ec fix: install-im-server
Signed-off-by: Xinwei Xiong (cubxxw) <3293172751nss@gmail.com>
2024-01-02 10:23:25 +08:00
7 changed files with 63 additions and 113 deletions
+1 -25
View File
@@ -3,36 +3,12 @@
before: before:
hooks: hooks:
- make clean
# You may remove this if you don't use go modules. # You may remove this if you don't use go modules.
- make tidy - make tidy
- make copyright.add - make copyright.add
# you may remove this if you don't need go generate # you may remove this if you don't need go generate
- go generate ./... - go generate ./...
git:
# What should be used to sort tags when gathering the current and previous
# tags if there are more than one tag in the same commit.
#
# Default: '-version:refname'
tag_sort: -version:creatordate
# What should be used to specify prerelease suffix while sorting tags when gathering
# the current and previous tags if there are more than one tag in the same commit.
#
# Since: v1.17
prerelease_suffix: "-"
# Tags to be ignored by GoReleaser.
# This means that GoReleaser will not pick up tags that match any of the
# provided values as either previous or current tags.
#
# Templates: allowed.
# Since: v1.21.
ignore_tags:
- nightly
# - "{{.Env.IGNORE_TAG}}"
snapshot: snapshot:
name_template: "{{ incpatch .Version }}-next" name_template: "{{ incpatch .Version }}-next"
@@ -519,4 +495,4 @@ checksum:
algorithm: sha256 algorithm: sha256
release: release:
prerelease: auto prerelease: auto
-1
View File
@@ -26,7 +26,6 @@ const (
Compression = "compression" Compression = "compression"
GzipCompressionProtocol = "gzip" GzipCompressionProtocol = "gzip"
BackgroundStatus = "isBackground" BackgroundStatus = "isBackground"
MsgResp = "isMsgResp"
) )
const ( const (
+55 -76
View File
@@ -16,10 +16,7 @@ package msggateway
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt"
"github.com/OpenIMSDK/tools/apiresp"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
@@ -425,102 +422,84 @@ func (ws *WsServer) unregisterClient(client *Client) {
) )
} }
func (ws *WsServer) ParseWSArgs(r *http.Request) (args *WSArgs, err error) { func (ws *WsServer) wsHandler(w http.ResponseWriter, r *http.Request) {
var v WSArgs connContext := newContext(w, r)
defer func() {
args = &v
}()
query := r.URL.Query()
v.MsgResp, _ = strconv.ParseBool(query.Get(MsgResp))
if ws.onlineUserConnNum.Load() >= ws.wsMaxConnNum { if ws.onlineUserConnNum.Load() >= ws.wsMaxConnNum {
return nil, errs.ErrConnOverMaxNumLimit.Wrap("over max conn num limit") httpError(connContext, errs.ErrConnOverMaxNumLimit)
return
} }
if v.Token = query.Get(Token); v.Token == "" { var (
return nil, errs.ErrConnArgsErr.Wrap("token is empty") token string
userID string
platformIDStr string
exists bool
compression bool
)
token, exists = connContext.Query(Token)
if !exists {
httpError(connContext, errs.ErrConnArgsErr)
return
} }
if v.UserID = query.Get(WsUserID); v.UserID == "" { userID, exists = connContext.Query(WsUserID)
return nil, errs.ErrConnArgsErr.Wrap("sendID is empty") if !exists {
httpError(connContext, errs.ErrConnArgsErr)
return
} }
platformIDStr := query.Get(PlatformID) platformIDStr, exists = connContext.Query(PlatformID)
if platformIDStr == "" { if !exists {
return nil, errs.ErrConnArgsErr.Wrap("platformID is empty") httpError(connContext, errs.ErrConnArgsErr)
return
} }
platformID, err := strconv.Atoi(platformIDStr) platformID, err := strconv.Atoi(platformIDStr)
if err != nil { if err != nil {
return nil, errs.ErrConnArgsErr.Wrap("platformID is not int") httpError(connContext, errs.ErrConnArgsErr)
return
} }
v.PlatformID = platformID if err = authverify.WsVerifyToken(token, userID, platformID); err != nil {
if err = authverify.WsVerifyToken(v.Token, v.UserID, platformID); err != nil { httpError(connContext, err)
return nil, err return
} }
if query.Get(Compression) == GzipCompressionProtocol { m, err := ws.cache.GetTokensWithoutError(context.Background(), userID, platformID)
v.Compression = true
}
if r.Header.Get(Compression) == GzipCompressionProtocol {
v.Compression = true
}
m, err := ws.cache.GetTokensWithoutError(context.Background(), v.UserID, platformID)
if err != nil { if err != nil {
return nil, err httpError(connContext, err)
return
} }
if v, ok := m[v.Token]; ok { if v, ok := m[token]; ok {
switch v { switch v {
case constant.NormalToken: case constant.NormalToken:
case constant.KickedToken: case constant.KickedToken:
return nil, errs.ErrTokenKicked.Wrap() httpError(connContext, errs.ErrTokenKicked.Wrap())
return
default: default:
return nil, errs.ErrTokenUnknown.Wrap(fmt.Sprintf("token status is %d", v)) httpError(connContext, errs.ErrTokenUnknown.Wrap())
return
} }
} else { } else {
return nil, errs.ErrTokenNotExist.Wrap() httpError(connContext, errs.ErrTokenNotExist.Wrap())
return
} }
return &v, nil
}
type WSArgs struct { wsLongConn := newGWebSocket(WebSocket, ws.handshakeTimeout, ws.writeBufferSize)
Token string err = wsLongConn.GenerateLongConn(w, r)
UserID string if err != nil {
PlatformID int httpError(connContext, err)
Compression bool return
MsgResp bool }
} compressProtoc, exists := connContext.Query(Compression)
if exists {
func (ws *WsServer) wsHandler(w http.ResponseWriter, r *http.Request) { if compressProtoc == GzipCompressionProtocol {
connContext := newContext(w, r) compression = true
args, pErr := ws.ParseWSArgs(r)
var wsLongConn *GWebSocket
if args.MsgResp {
wsLongConn = newGWebSocket(WebSocket, ws.handshakeTimeout, ws.writeBufferSize)
if err := wsLongConn.GenerateLongConn(w, r); err != nil {
httpError(connContext, err)
return
} }
data, err := json.Marshal(apiresp.ParseError(pErr)) }
if err != nil { compressProtoc, exists = connContext.GetHeader(Compression)
_ = wsLongConn.Close() if exists {
return if compressProtoc == GzipCompressionProtocol {
} compression = true
if err := wsLongConn.WriteMessage(MessageText, data); err != nil {
_ = wsLongConn.Close()
return
}
if pErr != nil {
_ = wsLongConn.Close()
return
}
} else {
if pErr != nil {
httpError(connContext, pErr)
return
}
wsLongConn = newGWebSocket(WebSocket, ws.handshakeTimeout, ws.writeBufferSize)
if err := wsLongConn.GenerateLongConn(w, r); err != nil {
httpError(connContext, err)
return
} }
} }
client := ws.clientPool.Get().(*Client) client := ws.clientPool.Get().(*Client)
client.ResetClient(connContext, wsLongConn, connContext.GetBackground(), args.Compression, ws, args.Token) client.ResetClient(connContext, wsLongConn, connContext.GetBackground(), compression, ws, token)
ws.registerChan <- client ws.registerChan <- client
go client.readMessage() go client.readMessage()
} }
+1 -1
View File
@@ -197,7 +197,7 @@ func (g *groupDatabase) UpdateGroup(ctx context.Context, groupID string, data ma
func (g *groupDatabase) DismissGroup(ctx context.Context, groupID string, deleteMember bool) error { func (g *groupDatabase) DismissGroup(ctx context.Context, groupID string, deleteMember bool) error {
return g.ctxTx.Transaction(ctx, func(ctx context.Context) error { return g.ctxTx.Transaction(ctx, func(ctx context.Context) error {
c := g.cache.NewCache() c := g.cache.NewCache()
if err := g.groupDB.UpdateStatus(ctx, groupID, constant.GroupStatusDismissed); err != nil { if err := g.groupDB.UpdateState(ctx, groupID, constant.GroupStatusDismissed); err != nil {
return err return err
} }
if deleteMember { if deleteMember {
+2 -2
View File
@@ -49,8 +49,8 @@ func (g *GroupMgo) Create(ctx context.Context, groups []*relation.GroupModel) (e
return mgoutil.InsertMany(ctx, g.coll, groups) return mgoutil.InsertMany(ctx, g.coll, groups)
} }
func (g *GroupMgo) UpdateStatus(ctx context.Context, groupID string, status int32) (err error) { func (g *GroupMgo) UpdateState(ctx context.Context, groupID string, state int32) (err error) {
return g.UpdateMap(ctx, groupID, map[string]any{"status": status}) return g.UpdateMap(ctx, groupID, map[string]any{"state": state})
} }
func (g *GroupMgo) UpdateMap(ctx context.Context, groupID string, args map[string]any) (err error) { func (g *GroupMgo) UpdateMap(ctx context.Context, groupID string, args map[string]any) (err error) {
+3 -7
View File
@@ -51,11 +51,7 @@ func (g *GroupMemberMgo) Create(ctx context.Context, groupMembers []*relation.Gr
} }
func (g *GroupMemberMgo) Delete(ctx context.Context, groupID string, userIDs []string) (err error) { func (g *GroupMemberMgo) Delete(ctx context.Context, groupID string, userIDs []string) (err error) {
filter := bson.M{"group_id": groupID} return mgoutil.DeleteMany(ctx, g.coll, bson.M{"group_id": groupID, "user_id": bson.M{"$in": userIDs}})
if len(userIDs) > 0 {
filter["user_id"] = bson.M{"$in": userIDs}
}
return mgoutil.DeleteMany(ctx, g.coll, filter)
} }
func (g *GroupMemberMgo) UpdateRoleLevel(ctx context.Context, groupID string, userID string, roleLevel int32) error { func (g *GroupMemberMgo) UpdateRoleLevel(ctx context.Context, groupID string, userID string, roleLevel int32) error {
@@ -88,8 +84,8 @@ func (g *GroupMemberMgo) FindRoleLevelUserIDs(ctx context.Context, groupID strin
} }
func (g *GroupMemberMgo) SearchMember(ctx context.Context, keyword string, groupID string, pagination pagination.Pagination) (total int64, groupList []*relation.GroupMemberModel, err error) { func (g *GroupMemberMgo) SearchMember(ctx context.Context, keyword string, groupID string, pagination pagination.Pagination) (total int64, groupList []*relation.GroupMemberModel, err error) {
filter := bson.M{"group_id": groupID, "nickname": bson.M{"$regex": keyword}} //TODO implement me
return mgoutil.FindPage[*relation.GroupMemberModel](ctx, g.coll, filter, pagination) panic("implement me")
} }
func (g *GroupMemberMgo) FindUserJoinedGroupID(ctx context.Context, userID string) (groupIDs []string, err error) { func (g *GroupMemberMgo) FindUserJoinedGroupID(ctx context.Context, userID string) (groupIDs []string, err error) {
+1 -1
View File
@@ -42,7 +42,7 @@ type GroupModel struct {
type GroupModelInterface interface { type GroupModelInterface interface {
Create(ctx context.Context, groups []*GroupModel) (err error) Create(ctx context.Context, groups []*GroupModel) (err error)
UpdateMap(ctx context.Context, groupID string, args map[string]any) (err error) UpdateMap(ctx context.Context, groupID string, args map[string]any) (err error)
UpdateStatus(ctx context.Context, groupID string, status int32) (err error) UpdateState(ctx context.Context, groupID string, state int32) (err error)
Find(ctx context.Context, groupIDs []string) (groups []*GroupModel, err error) Find(ctx context.Context, groupIDs []string) (groups []*GroupModel, err error)
Take(ctx context.Context, groupID string) (group *GroupModel, err error) Take(ctx context.Context, groupID string) (group *GroupModel, err error)
Search(ctx context.Context, keyword string, pagination pagination.Pagination) (total int64, groups []*GroupModel, err error) Search(ctx context.Context, keyword string, pagination pagination.Pagination) (total int64, groups []*GroupModel, err error)