Merge branch 'main' into style/addlicense

pull/773/head
Xinwei Xiong 2 years ago committed by GitHub
commit 0a41dbf0e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -27,7 +27,7 @@ jobs:
steps: steps:
- name: Invite user to join our group - name: Invite user to join our group
uses: peter-evans/create-or-update-comment@v1 uses: peter-evans/create-or-update-comment@v3
with: with:
token: ${{ secrets.BOT_GITHUB_TOKEN }} token: ${{ secrets.BOT_GITHUB_TOKEN }}
issue-number: ${{ github.event.issue.number }} issue-number: ${{ github.event.issue.number }}

@ -23,7 +23,7 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Comment cherry-pick command - name: Comment cherry-pick command
uses: actions/github-script@v5 uses: actions/github-script@v6
with: with:
script: | script: |
const pr = context.payload.pull_request; const pr = context.payload.pull_request;

@ -23,7 +23,7 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: Checkout the latest code - name: Checkout the latest code
uses: actions/checkout@v2 uses: actions/checkout@v3
with: with:
token: ${{ secrets.BOT_GITHUB_TOKEN }} token: ${{ secrets.BOT_GITHUB_TOKEN }}
fetch-depth: 0 # otherwise, you will fail to push refs to dest repo fetch-depth: 0 # otherwise, you will fail to push refs to dest repo

@ -44,7 +44,7 @@ jobs:
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Setup Golang with cache - name: Setup Golang with cache
uses: magnetikonline/action-golang-cache@v3 uses: magnetikonline/action-golang-cache@v4
with: with:
go-version: ${{ env.GO_VERSION }} go-version: ${{ env.GO_VERSION }}

@ -27,7 +27,7 @@ jobs:
- name: Link Checker - name: Link Checker
id: lychee id: lychee
uses: lycheeverse/lychee-action@v1.7.0 uses: lycheeverse/lychee-action@v1.8.0
with: with:
# For parameter description, see https://github.com/lycheeverse/lychee#commandline-parameters # For parameter description, see https://github.com/lycheeverse/lychee#commandline-parameters
# Actions Link address -> https://github.com/lycheeverse/lychee-action # Actions Link address -> https://github.com/lycheeverse/lychee-action

@ -39,7 +39,7 @@ jobs:
statuses: none statuses: none
steps: steps:
- uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6 - uses: actions/github-script@v6 # v6
with: with:
script: | script: |
if (!context.payload.pull_request.merged) { if (!context.payload.pull_request.merged) {

@ -27,7 +27,7 @@ jobs:
permissions: write-all permissions: write-all
steps: steps:
- name: Setup Node.js Environment - name: Setup Node.js Environment
uses: actions/setup-node@v2 uses: actions/setup-node@v3
with: with:
node-version: '16' node-version: '16'
- uses: actions/checkout@v3 - uses: actions/checkout@v3

@ -53,7 +53,7 @@ jobs:
steps: steps:
- name: Set up Go ${{ matrix.go_version }} - name: Set up Go ${{ matrix.go_version }}
uses: actions/setup-go@v2 uses: actions/setup-go@v4
with: with:
go-version: ${{ matrix.go_version }} go-version: ${{ matrix.go_version }}
id: go id: go
@ -149,7 +149,7 @@ jobs:
continue-on-error: true continue-on-error: true
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1 uses: docker/setup-buildx-action@v2
continue-on-error: true continue-on-error: true
openim-start: openim-start:
@ -163,13 +163,13 @@ jobs:
os: ["ubuntu-latest"] os: ["ubuntu-latest"]
steps: steps:
- name: Set up Go ${{ matrix.go_version }} - name: Set up Go ${{ matrix.go_version }}
uses: actions/setup-go@v2 uses: actions/setup-go@v4
with: with:
go-version: ${{ matrix.go_version }} go-version: ${{ matrix.go_version }}
id: go id: go
- name: Check out code into the Go module directory - name: Check out code into the Go module directory
uses: actions/checkout@v2 uses: actions/checkout@v3
- name: Run OpenIM make install start - name: Run OpenIM make install start
run: | run: |

@ -47,7 +47,7 @@ jobs:
git checkout -b cicd/patch-${{ github.event.number }} git checkout -b cicd/patch-${{ github.event.number }}
- uses: actions/setup-node@v3 - uses: actions/setup-node@v3
- name: Setup Go - name: Setup Go
uses: actions/setup-go@v2 uses: actions/setup-go@v4
- name: Run go modules tidy - name: Run go modules tidy
run: | run: |
sudo make tidy sudo make tidy

@ -60,7 +60,7 @@ jobs:
matrix: matrix:
format: [ deb, rpm, apk ] format: [ deb, rpm, apk ]
steps: steps:
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3 - uses: actions/checkout@v3 # v3
with: with:
fetch-depth: 0 fetch-depth: 0
- uses: arduino/setup-task@e26d8975574116b0097a1161e0fe16ba75d84c1c # v1 - uses: arduino/setup-task@e26d8975574116b0097a1161e0fe16ba75d84c1c # v1

@ -38,7 +38,7 @@ jobs:
steps: steps:
- name: Checkout code - name: Checkout code
uses: actions/checkout@v2 uses: actions/checkout@v3
- name: Start Docker Compose - name: Start Docker Compose
run: | run: |

@ -32,7 +32,7 @@ jobs:
pull-requests: write pull-requests: write
steps: steps:
- uses: actions/stale@v5 - uses: actions/stale@v8
with: with:
repo-token: ${{ secrets.BOT_GITHUB_TOKEN }} repo-token: ${{ secrets.BOT_GITHUB_TOKEN }}
days-before-stale: 60 days-before-stale: 60

@ -39,7 +39,7 @@ require github.com/google/uuid v1.3.0
require ( require (
github.com/OpenIMSDK/protocol v0.0.4 github.com/OpenIMSDK/protocol v0.0.4
github.com/OpenIMSDK/tools v0.0.13 github.com/OpenIMSDK/tools v0.0.13
github.com/aliyun/aliyun-oss-go-sdk v2.2.7+incompatible github.com/aliyun/aliyun-oss-go-sdk v2.2.8+incompatible
github.com/go-redis/redis v6.15.9+incompatible github.com/go-redis/redis v6.15.9+incompatible
github.com/go-sql-driver/mysql v1.7.1 github.com/go-sql-driver/mysql v1.7.1
github.com/redis/go-redis/v9 v9.0.5 github.com/redis/go-redis/v9 v9.0.5

@ -26,8 +26,8 @@ github.com/Shopify/sarama v1.29.0 h1:ARid8o8oieau9XrHI55f/L3EoRAhm9px6sonbD7yuUE
github.com/Shopify/sarama v1.29.0/go.mod h1:2QpgD79wpdAESqNQMxNc0KYMkycd4slxGdV3TWSVqrU= github.com/Shopify/sarama v1.29.0/go.mod h1:2QpgD79wpdAESqNQMxNc0KYMkycd4slxGdV3TWSVqrU=
github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWsokNbMijUGhmcoBJc= github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWsokNbMijUGhmcoBJc=
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
github.com/aliyun/aliyun-oss-go-sdk v2.2.7+incompatible h1:KpbJFXwhVeuxNtBJ74MCGbIoaBok2uZvkD7QXp2+Wis= github.com/aliyun/aliyun-oss-go-sdk v2.2.8+incompatible h1:6JF1bjhT0WN2srEmijfOFtVWwV91KZ6dJY1/JbdtGrI=
github.com/aliyun/aliyun-oss-go-sdk v2.2.7+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8= github.com/aliyun/aliyun-oss-go-sdk v2.2.8+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=

@ -253,18 +253,27 @@ func (s *userServer) GetAllUserID(ctx context.Context, req *pbuser.GetAllUserIDR
// SubscribeOrCancelUsersStatus Subscribe online or cancel online users. // SubscribeOrCancelUsersStatus Subscribe online or cancel online users.
func (s *userServer) SubscribeOrCancelUsersStatus(ctx context.Context, req *pbuser.SubscribeOrCancelUsersStatusReq) (resp *pbuser.SubscribeOrCancelUsersStatusResp, err error) { func (s *userServer) SubscribeOrCancelUsersStatus(ctx context.Context, req *pbuser.SubscribeOrCancelUsersStatusReq) (resp *pbuser.SubscribeOrCancelUsersStatusResp, err error) {
err = s.UserDatabase.SubscribeOrCancelUsersStatus(ctx, req.UserID, req.UserIDs, req.Genre) if req.Genre == constant.SubscriberUser {
err = s.UserDatabase.SubscribeUsersStatus(ctx, req.UserID, req.UserIDs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// var status map[string][]string var status []*pbuser.OnlineStatus
// TODO 获取用户在线列表,返回订阅的用户的在线列表 status, err = s.UserDatabase.GetUserStatus(ctx, req.UserIDs)
if err != nil {
return nil, err
}
return &pbuser.SubscribeOrCancelUsersStatusResp{StatusList: status}, nil
} else if req.Genre == constant.Unsubscribe {
err = s.UserDatabase.UnsubscribeUsersStatus(ctx, req.UserID, req.UserIDs)
if err != nil {
return nil, err
}
}
return &pbuser.SubscribeOrCancelUsersStatusResp{}, nil return &pbuser.SubscribeOrCancelUsersStatusResp{}, nil
} }
func (s *userServer) GetUserStatus(ctx context.Context, req *pbuser.GetUserStatusReq) (resp *pbuser.GetUserStatusResp, err error) { func (s *userServer) GetUserStatus(ctx context.Context, req *pbuser.GetUserStatusReq) (resp *pbuser.GetUserStatusResp, err error) {
// TODO 是否加一个参数校验-判断req.userID的数量每一个获取加一个限制一次请求限制500
onlineStatusList, err := s.UserDatabase.GetUserStatus(ctx, req.UserIDs) onlineStatusList, err := s.UserDatabase.GetUserStatus(ctx, req.UserIDs)
if err != nil { if err != nil {
return nil, err return nil, err

@ -16,9 +16,7 @@ package controller
import ( import (
"context" "context"
"time" unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/protocol/constant"
"github.com/OpenIMSDK/protocol/user" "github.com/OpenIMSDK/protocol/user"
unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation" unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
@ -54,8 +52,10 @@ type UserDatabase interface {
CountTotal(ctx context.Context, before *time.Time) (int64, error) CountTotal(ctx context.Context, before *time.Time) (int64, error)
// CountRangeEverydayTotal Get the user increment in the range // CountRangeEverydayTotal Get the user increment in the range
CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error) CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error)
// SubscribeOrCancelUsersStatus Subscribe or unsubscribe a user's presence status //SubscribeUsersStatus Subscribe a user's presence status
SubscribeOrCancelUsersStatus(ctx context.Context, userID string, userIDs []string, genre int32) error SubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error
// UnsubscribeUsersStatus unsubscribe a user's presence status
UnsubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error
// GetAllSubscribeList Get a list of all subscriptions // GetAllSubscribeList Get a list of all subscriptions
GetAllSubscribeList(ctx context.Context, userID string) ([]string, error) GetAllSubscribeList(ctx context.Context, userID string) ([]string, error)
// GetSubscribedList Get all subscribed lists // GetSubscribedList Get all subscribed lists
@ -179,27 +179,34 @@ func (u *userDatabase) CountRangeEverydayTotal(ctx context.Context, start time.T
return u.userDB.CountRangeEverydayTotal(ctx, start, end) return u.userDB.CountRangeEverydayTotal(ctx, start, end)
} }
// SubscribeOrCancelUsersStatus Subscribe or unsubscribe a user's presence status. // SubscribeUsersStatus Subscribe or unsubscribe a user's presence status
func (u *userDatabase) SubscribeOrCancelUsersStatus(ctx context.Context, userID string, userIDs []string, genre int32) error { func (u *userDatabase) SubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error {
var err error err := u.mongoDB.AddSubscriptionList(ctx, userID, userIDs)
if genre == constant.SubscriberUser { return err
err = u.mongoDB.AddSubscriptionList(ctx, userID, userIDs) }
} else if genre == constant.Unsubscribe {
err = u.mongoDB.UnsubscriptionList(ctx, userID, userIDs) // UnsubscribeUsersStatus unsubscribe a user's presence status
} func (u *userDatabase) UnsubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error {
err := u.mongoDB.UnsubscriptionList(ctx, userID, userIDs)
return err return err
} }
// GetAllSubscribeList Get a list of all subscriptions. // GetAllSubscribeList Get a list of all subscriptions.
func (u *userDatabase) GetAllSubscribeList(ctx context.Context, userID string) ([]string, error) { func (u *userDatabase) GetAllSubscribeList(ctx context.Context, userID string) ([]string, error) {
//TODO 获取所有订阅 list, err := u.mongoDB.GetAllSubscribeList(ctx, userID)
return nil, nil if err != nil {
return nil, err
}
return list, nil
} }
// GetSubscribedList Get all subscribed lists. // GetSubscribedList Get all subscribed lists.
func (u *userDatabase) GetSubscribedList(ctx context.Context, userID string) ([]string, error) { func (u *userDatabase) GetSubscribedList(ctx context.Context, userID string) ([]string, error) {
//TODO 获取所有被订阅 list, err := u.mongoDB.GetSubscribedList(ctx, userID)
return nil, nil if err != nil {
return nil, err
}
return list, nil
} }
// GetUserStatus get user status. // GetUserStatus get user status.

@ -39,4 +39,8 @@ type UserModelInterface interface {
UnsubscriptionList(ctx context.Context, userID string, userIDList []string) error UnsubscriptionList(ctx context.Context, userID string, userIDList []string) error
// RemoveSubscribedListFromUser Among the unsubscribed users, delete the user from the subscribed list. // RemoveSubscribedListFromUser Among the unsubscribed users, delete the user from the subscribed list.
RemoveSubscribedListFromUser(ctx context.Context, userID string, userIDList []string) error RemoveSubscribedListFromUser(ctx context.Context, userID string, userIDList []string) error
// GetAllSubscribeList Get all users subscribed by this user
GetAllSubscribeList(ctx context.Context, id string) (userIDList []string, err error)
// GetSubscribedList Get the user subscribed by those users
GetSubscribedList(ctx context.Context, id string) (userIDList []string, err error)
} }

@ -16,14 +16,12 @@ package unrelation
import ( import (
"context" "context"
"log" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
"github.com/OpenIMSDK/tools/errs"
"github.com/OpenIMSDK/tools/utils" "github.com/OpenIMSDK/tools/utils"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation"
) )
// prefixes and suffixes. // prefixes and suffixes.
@ -50,22 +48,35 @@ type UserMongoDriver struct {
// AddSubscriptionList Subscriber's handling of thresholds. // AddSubscriptionList Subscriber's handling of thresholds.
func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string, userIDList []string) error { func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string, userIDList []string) error {
// Check the number of lists in the key. // Check the number of lists in the key.
filter := bson.M{SubscriptionPrefix + userID: bson.M{"$size": 1}} pipeline := mongo.Pipeline{
result, err := u.userCollection.Find(context.Background(), filter) {{"$match", bson.D{{"user_id", SubscriptionPrefix + userID}}}},
{{"$project", bson.D{{"count", bson.D{{"$size", "$user_id_list"}}}}}},
}
// perform aggregate operations
cursor, err := u.userCollection.Aggregate(ctx, pipeline)
if err != nil { if err != nil {
return err return errs.Wrap(err)
} }
var newUserIDList []string defer cursor.Close(ctx)
for result.Next(context.Background()) { var cnt struct {
err := result.Decode(&newUserIDList) Count int `bson:"count"`
}
// iterate over aggregated results
for cursor.Next(ctx) {
err := cursor.Decode(&cnt)
if err != nil { if err != nil {
log.Fatal(err) return errs.Wrap(err)
} }
} }
var newUserIDList []string
// If the threshold is exceeded, pop out the previous MaximumSubscription - len(userIDList) and insert it. // If the threshold is exceeded, pop out the previous MaximumSubscription - len(userIDList) and insert it.
if len(newUserIDList)+len(userIDList) > MaximumSubscription { if cnt.Count+len(userIDList) > MaximumSubscription {
newUserIDList, err = u.GetAllSubscribeList(ctx, userID)
if err != nil {
return err
}
newUserIDList = newUserIDList[MaximumSubscription-len(userIDList):] newUserIDList = newUserIDList[MaximumSubscription-len(userIDList):]
_, err := u.userCollection.UpdateOne( _, err = u.userCollection.UpdateOne(
ctx, ctx,
bson.M{"user_id": SubscriptionPrefix + userID}, bson.M{"user_id": SubscriptionPrefix + userID},
bson.M{"$set": bson.M{"user_id_list": newUserIDList}}, bson.M{"$set": bson.M{"user_id_list": newUserIDList}},
@ -73,16 +84,17 @@ func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string
if err != nil { if err != nil {
return err return err
} }
//for i := 1; i <= MaximumSubscription-len(userIDList); i++ { // Another way to subscribe to N before pop,Delete after testing
// _, err := u.userCollection.UpdateOne( /*for i := 1; i <= MaximumSubscription-len(userIDList); i++ {
// ctx, _, err := u.userCollection.UpdateOne(
// bson.M{"user_id": SubscriptionPrefix + userID}, ctx,
// bson.M{SubscriptionPrefix + userID: bson.M{"$pop": -1}}, bson.M{"user_id": SubscriptionPrefix + userID},
// ) bson.M{SubscriptionPrefix + userID: bson.M{"$pop": -1}},
// if err != nil { )
// return err if err != nil {
// } return err
//} }
}*/
} }
upsert := true upsert := true
opts := &options.UpdateOptions{ opts := &options.UpdateOptions{
@ -95,7 +107,7 @@ func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string
opts, opts,
) )
if err != nil { if err != nil {
return err return errs.Wrap(err)
} }
for _, user := range userIDList { for _, user := range userIDList {
_, err = u.userCollection.UpdateOne( _, err = u.userCollection.UpdateOne(
@ -119,25 +131,50 @@ func (u *UserMongoDriver) UnsubscriptionList(ctx context.Context, userID string,
bson.M{"$pull": bson.M{"user_id_list": bson.M{"$in": userIDList}}}, bson.M{"$pull": bson.M{"user_id_list": bson.M{"$in": userIDList}}},
) )
if err != nil { if err != nil {
return err return errs.Wrap(err)
} }
err = u.RemoveSubscribedListFromUser(ctx, userID, userIDList) err = u.RemoveSubscribedListFromUser(ctx, userID, userIDList)
if err != nil { if err != nil {
return err return errs.Wrap(err)
} }
return nil return nil
} }
// RemoveSubscribedListFromUser Among the unsubscribed users, delete the user from the subscribed list. // RemoveSubscribedListFromUser Among the unsubscribed users, delete the user from the subscribed list.
func (u *UserMongoDriver) RemoveSubscribedListFromUser(ctx context.Context, userID string, userIDList []string) error { func (u *UserMongoDriver) RemoveSubscribedListFromUser(ctx context.Context, userID string, userIDList []string) error {
var newUserIDList []string var err error
for _, value := range userIDList { for _, userIDTemp := range userIDList {
newUserIDList = append(newUserIDList, SubscribedPrefix+value) _, err = u.userCollection.UpdateOne(
}
_, err := u.userCollection.UpdateOne(
ctx, ctx,
bson.M{"user_id": bson.M{"$in": newUserIDList}}, bson.M{"user_id": SubscribedPrefix + userIDTemp},
bson.M{"$pull": bson.M{"user_id_list": userID}}, bson.M{"$pull": bson.M{"user_id_list": userID}},
) )
return utils.Wrap(err, "") }
return errs.Wrap(err)
}
// GetAllSubscribeList Get all users subscribed by this user
func (u *UserMongoDriver) GetAllSubscribeList(ctx context.Context, userID string) (userIDList []string, err error) {
var user unrelation.UserModel
cursor := u.userCollection.FindOne(
ctx,
bson.M{"user_id": SubscriptionPrefix + userID})
err = cursor.Decode(&user)
if err != nil {
return nil, errs.Wrap(err)
}
return user.UserIDList, nil
}
// GetSubscribedList Get the user subscribed by those users
func (u *UserMongoDriver) GetSubscribedList(ctx context.Context, userID string) (userIDList []string, err error) {
var user unrelation.UserModel
cursor := u.userCollection.FindOne(
ctx,
bson.M{"user_id": SubscribedPrefix + userID})
err = cursor.Decode(&user)
if err != nil {
return nil, errs.Wrap(err)
}
return user.UserIDList, nil
} }

Loading…
Cancel
Save