From a7b6923b745a8223fc7810b3b593b5dcd8a8b534 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 4 Aug 2023 21:21:30 +0800 Subject: [PATCH 1/3] feat(deps): bump the gomod-deps group with 1 update (#771) Bumps the gomod-deps group with 1 update: [github.com/aliyun/aliyun-oss-go-sdk](https://github.com/aliyun/aliyun-oss-go-sdk). - [Release notes](https://github.com/aliyun/aliyun-oss-go-sdk/releases) - [Changelog](https://github.com/aliyun/aliyun-oss-go-sdk/blob/master/CHANGELOG.md) - [Commits](https://github.com/aliyun/aliyun-oss-go-sdk/compare/v2.2.7...v2.2.8) --- updated-dependencies: - dependency-name: github.com/aliyun/aliyun-oss-go-sdk dependency-type: direct:production update-type: version-update:semver-patch dependency-group: gomod-deps ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 686cadce4..79fb13e5b 100644 --- a/go.mod +++ b/go.mod @@ -39,7 +39,7 @@ require github.com/google/uuid v1.3.0 require ( github.com/OpenIMSDK/protocol v0.0.4 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-sql-driver/mysql v1.7.1 github.com/redis/go-redis/v9 v9.0.5 diff --git a/go.sum b/go.sum index 9a820bfd2..02ba48c79 100644 --- a/go.sum +++ b/go.sum @@ -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/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWsokNbMijUGhmcoBJc= 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.7+incompatible/go.mod h1:T/Aws4fEfogEE9v+HPhhw+CntffsBHJ8nXQCwKr0/g8= +github.com/aliyun/aliyun-oss-go-sdk v2.2.8+incompatible h1:6JF1bjhT0WN2srEmijfOFtVWwV91KZ6dJY1/JbdtGrI= +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/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= From ef32a6bce9393e64765d7bea57b61c72e83e76ab Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 4 Aug 2023 21:21:37 +0800 Subject: [PATCH 2/3] chore(deps): bump the github-actions group with 9 updates (#772) Bumps the github-actions group with 9 updates: | Package | Update | | --- | --- | | [actions/checkout](https://github.com/actions/checkout) | 2 to 3 | | [peter-evans/create-or-update-comment](https://github.com/peter-evans/create-or-update-comment) | 1 to 3 | | [actions/github-script](https://github.com/actions/github-script) | 5 to 6 | | [docker/setup-buildx-action](https://github.com/docker/setup-buildx-action) | 1 to 2 | | [magnetikonline/action-golang-cache](https://github.com/magnetikonline/action-golang-cache) | 3 to 4 | | [actions/setup-go](https://github.com/actions/setup-go) | 2 to 4 | | [lycheeverse/lychee-action](https://github.com/lycheeverse/lychee-action) | 1.7.0 to 1.8.0 | | [actions/setup-node](https://github.com/actions/setup-node) | 2 to 3 | | [actions/stale](https://github.com/actions/stale) | 5 to 8 | Updates `actions/checkout` from 2 to 3 - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/v2...v3) Updates `peter-evans/create-or-update-comment` from 1 to 3 - [Release notes](https://github.com/peter-evans/create-or-update-comment/releases) - [Commits](https://github.com/peter-evans/create-or-update-comment/compare/v1...v3) Updates `actions/github-script` from 5 to 6 - [Release notes](https://github.com/actions/github-script/releases) - [Commits](https://github.com/actions/github-script/compare/v5...v6) Updates `docker/setup-buildx-action` from 1 to 2 - [Release notes](https://github.com/docker/setup-buildx-action/releases) - [Commits](https://github.com/docker/setup-buildx-action/compare/v1...v2) Updates `magnetikonline/action-golang-cache` from 3 to 4 - [Release notes](https://github.com/magnetikonline/action-golang-cache/releases) - [Commits](https://github.com/magnetikonline/action-golang-cache/compare/v3...v4) Updates `actions/setup-go` from 2 to 4 - [Release notes](https://github.com/actions/setup-go/releases) - [Commits](https://github.com/actions/setup-go/compare/v2...v4) Updates `lycheeverse/lychee-action` from 1.7.0 to 1.8.0 - [Release notes](https://github.com/lycheeverse/lychee-action/releases) - [Commits](https://github.com/lycheeverse/lychee-action/compare/v1.7.0...v1.8.0) Updates `actions/setup-node` from 2 to 3 - [Release notes](https://github.com/actions/setup-node/releases) - [Commits](https://github.com/actions/setup-node/compare/v2...v3) Updates `actions/stale` from 5 to 8 - [Release notes](https://github.com/actions/stale/releases) - [Changelog](https://github.com/actions/stale/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/stale/compare/v5...v8) --- updated-dependencies: - dependency-name: actions/checkout dependency-type: direct:production update-type: version-update:semver-major dependency-group: github-actions - dependency-name: peter-evans/create-or-update-comment dependency-type: direct:production update-type: version-update:semver-major dependency-group: github-actions - dependency-name: actions/github-script dependency-type: direct:production update-type: version-update:semver-major dependency-group: github-actions - dependency-name: docker/setup-buildx-action dependency-type: direct:production update-type: version-update:semver-major dependency-group: github-actions - dependency-name: magnetikonline/action-golang-cache dependency-type: direct:production update-type: version-update:semver-major dependency-group: github-actions - dependency-name: actions/setup-go dependency-type: direct:production update-type: version-update:semver-major dependency-group: github-actions - dependency-name: lycheeverse/lychee-action dependency-type: direct:production update-type: version-update:semver-minor dependency-group: github-actions - dependency-name: actions/setup-node dependency-type: direct:production update-type: version-update:semver-major dependency-group: github-actions - dependency-name: actions/stale dependency-type: direct:production update-type: version-update:semver-major dependency-group: github-actions ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/auto-invite.yml | 2 +- .github/workflows/bot-auto-cherry-pick.yml | 2 +- .github/workflows/bot-cherry-pick.yml | 2 +- .github/workflows/check-coverage.yml | 2 +- .github/workflows/link-pr.yml | 2 +- .github/workflows/milestone.yml | 2 +- .github/workflows/opencommit.yml | 2 +- .github/workflows/openimci.yml | 8 ++++---- .github/workflows/pull-request.yml | 2 +- .github/workflows/release.yml | 2 +- .github/workflows/scripts-test.yml | 2 +- .github/workflows/stale.yml | 2 +- 12 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.github/workflows/auto-invite.yml b/.github/workflows/auto-invite.yml index 2bc644bfb..0fd9a4eab 100644 --- a/.github/workflows/auto-invite.yml +++ b/.github/workflows/auto-invite.yml @@ -27,7 +27,7 @@ jobs: steps: - name: Invite user to join our group - uses: peter-evans/create-or-update-comment@v1 + uses: peter-evans/create-or-update-comment@v3 with: token: ${{ secrets.BOT_GITHUB_TOKEN }} issue-number: ${{ github.event.issue.number }} diff --git a/.github/workflows/bot-auto-cherry-pick.yml b/.github/workflows/bot-auto-cherry-pick.yml index 0d545813a..6861872e4 100644 --- a/.github/workflows/bot-auto-cherry-pick.yml +++ b/.github/workflows/bot-auto-cherry-pick.yml @@ -23,7 +23,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Comment cherry-pick command - uses: actions/github-script@v5 + uses: actions/github-script@v6 with: script: | const pr = context.payload.pull_request; diff --git a/.github/workflows/bot-cherry-pick.yml b/.github/workflows/bot-cherry-pick.yml index 59a4979cf..2e69e0d0a 100644 --- a/.github/workflows/bot-cherry-pick.yml +++ b/.github/workflows/bot-cherry-pick.yml @@ -23,7 +23,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout the latest code - uses: actions/checkout@v2 + uses: actions/checkout@v3 with: token: ${{ secrets.BOT_GITHUB_TOKEN }} fetch-depth: 0 # otherwise, you will fail to push refs to dest repo diff --git a/.github/workflows/check-coverage.yml b/.github/workflows/check-coverage.yml index 937af89d6..7080ef7be 100644 --- a/.github/workflows/check-coverage.yml +++ b/.github/workflows/check-coverage.yml @@ -44,7 +44,7 @@ jobs: uses: actions/checkout@v3 - name: Setup Golang with cache - uses: magnetikonline/action-golang-cache@v3 + uses: magnetikonline/action-golang-cache@v4 with: go-version: ${{ env.GO_VERSION }} diff --git a/.github/workflows/link-pr.yml b/.github/workflows/link-pr.yml index 07b9d46a9..356394f94 100644 --- a/.github/workflows/link-pr.yml +++ b/.github/workflows/link-pr.yml @@ -27,7 +27,7 @@ jobs: - name: Link Checker id: lychee - uses: lycheeverse/lychee-action@v1.7.0 + uses: lycheeverse/lychee-action@v1.8.0 with: # For parameter description, see https://github.com/lycheeverse/lychee#commandline-parameters # Actions Link address -> https://github.com/lycheeverse/lychee-action diff --git a/.github/workflows/milestone.yml b/.github/workflows/milestone.yml index db39943e6..a31eaa150 100644 --- a/.github/workflows/milestone.yml +++ b/.github/workflows/milestone.yml @@ -25,7 +25,7 @@ jobs: statuses: none steps: - - uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6 + - uses: actions/github-script@v6 # v6 with: script: | if (!context.payload.pull_request.merged) { diff --git a/.github/workflows/opencommit.yml b/.github/workflows/opencommit.yml index 36307c00f..a07b5b8b2 100644 --- a/.github/workflows/opencommit.yml +++ b/.github/workflows/opencommit.yml @@ -27,7 +27,7 @@ jobs: permissions: write-all steps: - name: Setup Node.js Environment - uses: actions/setup-node@v2 + uses: actions/setup-node@v3 with: node-version: '16' - uses: actions/checkout@v3 diff --git a/.github/workflows/openimci.yml b/.github/workflows/openimci.yml index 0e69089f9..d172eb251 100644 --- a/.github/workflows/openimci.yml +++ b/.github/workflows/openimci.yml @@ -53,7 +53,7 @@ jobs: steps: - name: Set up Go ${{ matrix.go_version }} - uses: actions/setup-go@v2 + uses: actions/setup-go@v4 with: go-version: ${{ matrix.go_version }} id: go @@ -149,7 +149,7 @@ jobs: continue-on-error: true - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v1 + uses: docker/setup-buildx-action@v2 continue-on-error: true openim-start: @@ -163,13 +163,13 @@ jobs: os: ["ubuntu-latest"] steps: - name: Set up Go ${{ matrix.go_version }} - uses: actions/setup-go@v2 + uses: actions/setup-go@v4 with: go-version: ${{ matrix.go_version }} id: go - name: Check out code into the Go module directory - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Run OpenIM make install start run: | diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 3405e479e..ac8904450 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -47,7 +47,7 @@ jobs: git checkout -b cicd/patch-${{ github.event.number }} - uses: actions/setup-node@v3 - name: Setup Go - uses: actions/setup-go@v2 + uses: actions/setup-go@v4 - name: Run go modules tidy run: | sudo make tidy diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 45ed79df2..fd019c4df 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -60,7 +60,7 @@ jobs: matrix: format: [ deb, rpm, apk ] steps: - - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3 + - uses: actions/checkout@v3 # v3 with: fetch-depth: 0 - uses: arduino/setup-task@e26d8975574116b0097a1161e0fe16ba75d84c1c # v1 diff --git a/.github/workflows/scripts-test.yml b/.github/workflows/scripts-test.yml index e59e696f8..0f98197e0 100644 --- a/.github/workflows/scripts-test.yml +++ b/.github/workflows/scripts-test.yml @@ -38,7 +38,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Start Docker Compose run: | diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml index 7790aa944..ba767f167 100644 --- a/.github/workflows/stale.yml +++ b/.github/workflows/stale.yml @@ -32,7 +32,7 @@ jobs: pull-requests: write steps: - - uses: actions/stale@v5 + - uses: actions/stale@v8 with: repo-token: ${{ secrets.BOT_GITHUB_TOKEN }} days-before-stale: 60 From 863c52c7bb360e1b09ff282fbf3596755ced561c Mon Sep 17 00:00:00 2001 From: pluto <2631223275@qq.com> Date: Fri, 4 Aug 2023 21:35:33 +0800 Subject: [PATCH 3/3] Improve user subscription and unsubscription (#770) * Improve user subscription and unsubscription * Modification only does not delete all subscribed documents when unsubscribing * fix build * update * update * update * update * add log * update * update * update * delete simple log --- internal/rpc/user/user.go | 23 ++++-- pkg/common/db/controller/user.go | 40 ++++++---- pkg/common/db/table/unrelation/user.go | 4 + pkg/common/db/unrelation/user.go | 103 +++++++++++++++++-------- 4 files changed, 114 insertions(+), 56 deletions(-) diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index 53598db0b..5425a9c13 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -250,18 +250,27 @@ func (s *userServer) GetAllUserID(ctx context.Context, req *pbuser.GetAllUserIDR // SubscribeOrCancelUsersStatus Subscribe online or cancel online users. 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 err != nil { - return nil, err + if req.Genre == constant.SubscriberUser { + err = s.UserDatabase.SubscribeUsersStatus(ctx, req.UserID, req.UserIDs) + if err != nil { + return nil, err + } + var status []*pbuser.OnlineStatus + 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 + } } - //var status map[string][]string - //TODO 获取用户在线列表,返回订阅的用户的在线列表 - return &pbuser.SubscribeOrCancelUsersStatusResp{}, nil } 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) if err != nil { return nil, err diff --git a/pkg/common/db/controller/user.go b/pkg/common/db/controller/user.go index 5b303ebd7..a867899d9 100644 --- a/pkg/common/db/controller/user.go +++ b/pkg/common/db/controller/user.go @@ -17,7 +17,6 @@ package controller import ( "context" unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation" - "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/user" "time" @@ -51,8 +50,10 @@ type UserDatabase interface { CountTotal(ctx context.Context, before *time.Time) (int64, error) // CountRangeEverydayTotal Get the user increment in the range CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error) - //SubscribeOrCancelUsersStatus Subscribe or unsubscribe a user's presence status - SubscribeOrCancelUsersStatus(ctx context.Context, userID string, userIDs []string, genre int32) error + //SubscribeUsersStatus Subscribe a user's presence status + 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(ctx context.Context, userID string) ([]string, error) // GetSubscribedList Get all subscribed lists @@ -176,29 +177,34 @@ func (u *userDatabase) CountRangeEverydayTotal(ctx context.Context, start time.T return u.userDB.CountRangeEverydayTotal(ctx, start, end) } -//SubscribeOrCancelUsersStatus Subscribe or unsubscribe a user's presence status -func (u *userDatabase) SubscribeOrCancelUsersStatus(ctx context.Context, userID string, userIDs []string, genre int32) error { - var err error - if genre == constant.SubscriberUser { - err = u.mongoDB.AddSubscriptionList(ctx, userID, userIDs) - } else if genre == constant.Unsubscribe { - err = u.mongoDB.UnsubscriptionList(ctx, userID, userIDs) - } +// SubscribeUsersStatus Subscribe or unsubscribe a user's presence status +func (u *userDatabase) SubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error { + err := u.mongoDB.AddSubscriptionList(ctx, userID, userIDs) + return err +} + +// 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 } // GetAllSubscribeList Get a list of all subscriptions. func (u *userDatabase) GetAllSubscribeList(ctx context.Context, userID string) ([]string, error) { - - //TODO 获取所有订阅 - return nil, nil + list, err := u.mongoDB.GetAllSubscribeList(ctx, userID) + if err != nil { + return nil, err + } + return list, nil } // GetSubscribedList Get all subscribed lists func (u *userDatabase) GetSubscribedList(ctx context.Context, userID string) ([]string, error) { - - //TODO 获取所有被订阅 - return nil, nil + list, err := u.mongoDB.GetSubscribedList(ctx, userID) + if err != nil { + return nil, err + } + return list, nil } // GetUserStatus get user status diff --git a/pkg/common/db/table/unrelation/user.go b/pkg/common/db/table/unrelation/user.go index d264da467..8664df262 100644 --- a/pkg/common/db/table/unrelation/user.go +++ b/pkg/common/db/table/unrelation/user.go @@ -39,4 +39,8 @@ type UserModelInterface interface { UnsubscriptionList(ctx context.Context, userID string, userIDList []string) error // RemoveSubscribedListFromUser Among the unsubscribed users, delete the user from the subscribed list. 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) } diff --git a/pkg/common/db/unrelation/user.go b/pkg/common/db/unrelation/user.go index feec8aa21..4f1bbd017 100644 --- a/pkg/common/db/unrelation/user.go +++ b/pkg/common/db/unrelation/user.go @@ -17,14 +17,14 @@ package unrelation import ( "context" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation" + "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/utils" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" - "log" ) -// prefixes and suffixes. +// prefixes and suffixes. const ( SubscriptionPrefix = "subscription_prefix" SubscribedPrefix = "subscribed_prefix" @@ -48,22 +48,35 @@ type UserMongoDriver struct { // AddSubscriptionList Subscriber's handling of thresholds. func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string, userIDList []string) error { // Check the number of lists in the key. - filter := bson.M{SubscriptionPrefix + userID: bson.M{"$size": 1}} - result, err := u.userCollection.Find(context.Background(), filter) + pipeline := mongo.Pipeline{ + {{"$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 { - return err + return errs.Wrap(err) } - var newUserIDList []string - for result.Next(context.Background()) { - err := result.Decode(&newUserIDList) + defer cursor.Close(ctx) + var cnt struct { + Count int `bson:"count"` + } + // iterate over aggregated results + for cursor.Next(ctx) { + err := cursor.Decode(&cnt) 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 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):] - _, err := u.userCollection.UpdateOne( + _, err = u.userCollection.UpdateOne( ctx, bson.M{"user_id": SubscriptionPrefix + userID}, bson.M{"$set": bson.M{"user_id_list": newUserIDList}}, @@ -71,16 +84,17 @@ func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string if err != nil { return err } - //for i := 1; i <= MaximumSubscription-len(userIDList); i++ { - // _, err := u.userCollection.UpdateOne( - // ctx, - // bson.M{"user_id": SubscriptionPrefix + userID}, - // bson.M{SubscriptionPrefix + userID: bson.M{"$pop": -1}}, - // ) - // if err != nil { - // return err - // } - //} + // Another way to subscribe to N before pop,Delete after testing + /*for i := 1; i <= MaximumSubscription-len(userIDList); i++ { + _, err := u.userCollection.UpdateOne( + ctx, + bson.M{"user_id": SubscriptionPrefix + userID}, + bson.M{SubscriptionPrefix + userID: bson.M{"$pop": -1}}, + ) + if err != nil { + return err + } + }*/ } upsert := true opts := &options.UpdateOptions{ @@ -93,7 +107,7 @@ func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string opts, ) if err != nil { - return err + return errs.Wrap(err) } for _, user := range userIDList { _, err = u.userCollection.UpdateOne( @@ -117,25 +131,50 @@ func (u *UserMongoDriver) UnsubscriptionList(ctx context.Context, userID string, bson.M{"$pull": bson.M{"user_id_list": bson.M{"$in": userIDList}}}, ) if err != nil { - return err + return errs.Wrap(err) } err = u.RemoveSubscribedListFromUser(ctx, userID, userIDList) if err != nil { - return err + return errs.Wrap(err) } return nil } // RemoveSubscribedListFromUser Among the unsubscribed users, delete the user from the subscribed list. func (u *UserMongoDriver) RemoveSubscribedListFromUser(ctx context.Context, userID string, userIDList []string) error { - var newUserIDList []string - for _, value := range userIDList { - newUserIDList = append(newUserIDList, SubscribedPrefix+value) + var err error + for _, userIDTemp := range userIDList { + _, err = u.userCollection.UpdateOne( + ctx, + bson.M{"user_id": SubscribedPrefix + userIDTemp}, + bson.M{"$pull": bson.M{"user_id_list": userID}}, + ) } - _, err := u.userCollection.UpdateOne( + 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": bson.M{"$in": newUserIDList}}, - bson.M{"$pull": bson.M{"user_id_list": userID}}, - ) - return utils.Wrap(err, "") + 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 }