From 4bb2ffa3995858fe4ee19ca03e568d0dbf69171e Mon Sep 17 00:00:00 2001 From: Xinwei Xiong <3293172751@qq.com> Date: Wed, 26 Jul 2023 17:03:17 +0800 Subject: [PATCH 01/11] feat: add test format (#657) * feat: add test format Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * fix: fix github auto gh pr Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: add build Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * build: add build copyright admin Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * docs: design documentation and deployment devops flow Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: add docker file and version appoint Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: add go release Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: add go release Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: add go release Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: add go release Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: add go release Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> feat: add go release Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> feat: add go release Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: set pull request Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: set pull request Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: set pull request Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: set pull request Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> --------- Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> --- .github/.codecov.yml | 14 + .github/labels.yml | 14 + .github/sync.yml | 14 + .github/weekly-digest.yml | 14 + .github/workflows/auto-assign-issue.yml | 14 + .github/workflows/auto-gh-pr.yml | 45 +- .github/workflows/auto-invite.yml | 14 + .github/workflows/bot-auto-cherry-pick.yml | 14 + .github/workflows/bot-cherry-pick.yml | 14 + .github/workflows/build-docker-image.yml | 107 ++- .github/workflows/check-coverage.yml | 14 + .github/workflows/cla.yml | 14 + .github/workflows/codeql-analysis.yml | 16 +- .github/workflows/create_branch_on_tag.yml | 62 ++ .github/workflows/deploy.yml | 14 + .github/workflows/e2e-test.yml | 14 + .github/workflows/gosec.yml | 14 + .github/workflows/help-comment-issue.yml | 14 + .github/workflows/issue-robot.yml | 14 + .github/workflows/link-pr.yml | 17 +- .github/workflows/lock-issue.yml | 14 + .github/workflows/opencommit.yml | 14 + .github/workflows/openimci.yml | 8 + .github/workflows/pull-request.yml | 99 +- .github/workflows/release.yml | 14 + .github/workflows/scripts-test.yml | 14 + .github/workflows/stale.yml | 14 + .goreleaser.yaml | 133 ++- CHANGELOG/CHANGELOG.md | 5 + README.md | 2 +- build/docker/openim-api/Dockerfile | 12 + build/docker/openim-cmdutils/Dockerfile | 0 build/docker/openim-crontask/Dockerfile | 0 build/docker/openim-msggateway/Dockerfile | 0 build/docker/openim-msgtransfer/Dockerfile | 0 build/docker/openim-push/Dockerfile | 0 build/docker/openim-rpc-auth/Dockerfile | 0 .../docker/openim-rpc-conversation/Dockerfile | 0 build/docker/openim-rpc-friend/Dockerfile | 0 build/docker/openim-rpc-group/Dockerfile | 0 build/docker/openim-rpc-msg/Dockerfile | 0 build/docker/openim-rpc-third/Dockerfile | 0 build/docker/openim-rpc-user/Dockerfile | 0 config/config.yaml | 18 +- docs/conversions/README.md | 9 + docs/conversions/api.md | 5 + docs/conversions/commit.md | 9 + docs/conversions/directory.md | 3 + docs/conversions/error_code.md | 22 + docs/conversions/go_code.md | 901 ++++++++++++++++++ docs/conversions/logging.md | 20 + docs/conversions/version.md | 57 ++ scripts/make-rules/common.mk | 10 +- scripts/make-rules/golang.mk | 6 +- 54 files changed, 1739 insertions(+), 107 deletions(-) create mode 100644 .github/workflows/create_branch_on_tag.yml create mode 100644 build/docker/openim-api/Dockerfile create mode 100644 build/docker/openim-cmdutils/Dockerfile create mode 100644 build/docker/openim-crontask/Dockerfile create mode 100644 build/docker/openim-msggateway/Dockerfile create mode 100644 build/docker/openim-msgtransfer/Dockerfile create mode 100644 build/docker/openim-push/Dockerfile create mode 100644 build/docker/openim-rpc-auth/Dockerfile create mode 100644 build/docker/openim-rpc-conversation/Dockerfile create mode 100644 build/docker/openim-rpc-friend/Dockerfile create mode 100644 build/docker/openim-rpc-group/Dockerfile create mode 100644 build/docker/openim-rpc-msg/Dockerfile create mode 100644 build/docker/openim-rpc-third/Dockerfile create mode 100644 build/docker/openim-rpc-user/Dockerfile create mode 100644 docs/conversions/README.md create mode 100644 docs/conversions/api.md create mode 100644 docs/conversions/commit.md create mode 100644 docs/conversions/directory.md create mode 100644 docs/conversions/error_code.md create mode 100644 docs/conversions/go_code.md create mode 100644 docs/conversions/logging.md create mode 100644 docs/conversions/version.md diff --git a/.github/.codecov.yml b/.github/.codecov.yml index 45e213180..9e262e0e3 100644 --- a/.github/.codecov.yml +++ b/.github/.codecov.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + coverage: status: project: diff --git a/.github/labels.yml b/.github/labels.yml index 037790523..b85a824b4 100644 --- a/.github/labels.yml +++ b/.github/labels.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + # Refer to Kubernetes for size/* Settings # https://github.com/Kubernetes/Kubernetes XS: diff --git a/.github/sync.yml b/.github/sync.yml index 4e9f9d17e..413e5fcc7 100644 --- a/.github/sync.yml +++ b/.github/sync.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + # https://github.com/BetaHuhn/repo-file-sync-action # Synchronization for the.github repository OpenIMSDK/.github: diff --git a/.github/weekly-digest.yml b/.github/weekly-digest.yml index 9deb72e7b..fb3614ad8 100644 --- a/.github/weekly-digest.yml +++ b/.github/weekly-digest.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + # https://github.com/apps/weekly-digest/installations/new publishDay: sun canPublishIssues: true diff --git a/.github/workflows/auto-assign-issue.yml b/.github/workflows/auto-assign-issue.yml index 06fee28a1..d87a571dc 100644 --- a/.github/workflows/auto-assign-issue.yml +++ b/.github/workflows/auto-assign-issue.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM open source community. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: Assign issue to comment author on: issue_comment: diff --git a/.github/workflows/auto-gh-pr.yml b/.github/workflows/auto-gh-pr.yml index efa77ee29..1d8c90d33 100644 --- a/.github/workflows/auto-gh-pr.yml +++ b/.github/workflows/auto-gh-pr.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM open source community. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: Auto PR to release on: @@ -5,16 +19,10 @@ on: # types: # - closed -env: - GH_TOKEN: ${{ secrets.REDBOT_GITHUB_TOKEN }} - ISSUE: ${{ github.event.issue.html_url }} - OWNER: ${{ github.repository_owner }} - REPO: ${{ github.event.repository.name }} - jobs: create-pr: runs-on: ubuntu-latest - if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main' + if: github.event.pull_request.base.ref == 'main' steps: - name: Check out code uses: actions/checkout@v3 @@ -29,14 +37,14 @@ jobs: sudo apt-get install gh continue-on-error: true - - name: Configure GitHub CLI - run: | - git config --global user.email "3293172751ysy@gmail.com" - git config --global user.name "kubbot" - echo "${{ secrets.BOT_GITHUB_TOKEN }}" | gh auth login --with-token + # - name: Configure GitHub CLI + # run: | + # git config --global user.email "3293172751ysy@gmail.com" + # git config --global user.name "kubbot" + # echo "${{ secrets.BOT_GITHUB_TOKEN }}" | gh auth login --with-token - name: Create PR to release branch run: | - ISSUEID=$(gh pr view 642 --repo $OWNER/$REPO | grep -oP 'Fixes #\K\d+') + ISSUEID=$(gh pr view ${{ github.event.pull_request.number }} --repo $OWNER/$REPO | grep -oP 'Fixes #\K\d+') echo "===========> $ISSUEID" ISSUE=$(gh issue view $ISSUEID --repo $OWNER/$REPO --json labels,assignees,milestone,title) echo "===========> $ISSUE" @@ -46,12 +54,17 @@ jobs: MILESTONE=$(echo $ISSUE | jq -r '.milestone | select(.title) | .title') TITLE=$(echo $ISSUE | jq -r '.title') - echo $ISSUE | jq - gh pr edit ${{ github.event.pull_request.number }} --repo $OWNER/$REPO --add-label "$LABELS" --add-assignee "$ASSIGNEES" --milestone "$MILESTONE" git checkout -b bot/merge-to-release-$ISSUEID git push origin bot/merge-to-release-$ISSUEID gh pr create --base release --head bot/merge-to-release-$ISSUEID --title "Merge main to release" --body "" - # gh pr create --base main --head feat/auto-release-pr-624 --title "The bug is fixed" --body "$x" --repo OpenIMSDK/Open-IM-Server --reviewer "cubxxw" \ No newline at end of file + # gh pr create --base main --head feat/auto-release-pr-624 --title "The bug is fixed" --body "$x" --repo OpenIMSDK/Open-IM-Server --reviewer "cubxxw" + continue-on-error: true + env: + GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }} + GH_TOKEN: ${{ github.token }} + ISSUE: ${{ github.event.issue.html_url }} + OWNER: ${{ github.repository_owner }} + REPO: ${{ github.event.repository.name }} \ No newline at end of file diff --git a/.github/workflows/auto-invite.yml b/.github/workflows/auto-invite.yml index 9d8effb51..2bc644bfb 100644 --- a/.github/workflows/auto-invite.yml +++ b/.github/workflows/auto-invite.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM open source community. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: Invite users to join our group on: issue_comment: diff --git a/.github/workflows/bot-auto-cherry-pick.yml b/.github/workflows/bot-auto-cherry-pick.yml index 4cb2cf0ec..0d545813a 100644 --- a/.github/workflows/bot-auto-cherry-pick.yml +++ b/.github/workflows/bot-auto-cherry-pick.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: 'Github Rebot for Cherry Pick when PR is merged' on: pull_request_target: diff --git a/.github/workflows/bot-cherry-pick.yml b/.github/workflows/bot-cherry-pick.yml index 9b5ff49bb..59a4979cf 100644 --- a/.github/workflows/bot-cherry-pick.yml +++ b/.github/workflows/bot-cherry-pick.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM open source community. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: Github Rebot for Cherry Pick On Comment on: issue_comment: diff --git a/.github/workflows/build-docker-image.yml b/.github/workflows/build-docker-image.yml index 31e0a5b65..ff58d8d8f 100644 --- a/.github/workflows/build-docker-image.yml +++ b/.github/workflows/build-docker-image.yml @@ -12,38 +12,99 @@ # See the License for the specific language governing permissions and # limitations under the License. -name: OpenIM Build Docker Images +name: Publish Docker image + on: push: + branches: + - main tags: - v* + workflow_dispatch: + jobs: build: runs-on: ubuntu-latest - strategy: - matrix: - bin: - - openim-server steps: - - name: Checkout + - name: Check out the repo uses: actions/checkout@v3 - - name: Setup Docker Buildx - uses: docker/setup-buildx-action@v2 - - name: Login to GitHub Container Registry - uses: docker/login-action@v2 + + - name: Log in to Docker Hub + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 with: - registry: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ secrets.GITHUB_TOKEN }} - - name: Docker metadata - id: metadata - uses: docker/metadata-action@v4 + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_PASSWORD }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 with: - images: ghcr.io/${{ github.repository_owner }}/openim-${{ matrix.bin }} - - name: Build and release Docker images - uses: docker/build-push-action@v3 + images: openim/openim-server + tags: latest + + - name: Build and push Docker image + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc with: - platforms: linux/386,linux/amd64,linux/arm64/v8 - target: ${{ matrix.bin }} - tags: ${{ steps.metadata.outputs.tags }} - push: true \ No newline at end of file + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + + + - name: Log in to AliYun Docker Hub + uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 + with: + registry: registry.cn-hangzhou.aliyuncs.com + username: ${{ secrets.ALIREGISTRY_USERNAME }} + password: ${{ secrets.ALIREGISTRY_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta2 + uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 + with: + images: registry.cn-hangzhou.aliyuncs.com/openimsdk/openim-server + tags: latest + + - name: Build and push Docker image + uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc + with: + context: . + push: true + tags: ${{ steps.meta2.outputs.tags }} + labels: ${{ steps.meta2.outputs.labels }} + +# name: OpenIM Build Docker Images +# on: +# push: +# tags: +# - v* +# jobs: +# build: +# runs-on: ubuntu-latest +# strategy: +# matrix: +# bin: +# - openim-server +# steps: +# - name: Checkout +# uses: actions/checkout@v3 +# - name: Setup Docker Buildx +# uses: docker/setup-buildx-action@v2 +# - name: Login to GitHub Container Registry +# uses: docker/login-action@v2 +# with: +# registry: ghcr.io +# username: ${{ github.repository_owner }} +# password: ${{ secrets.GITHUB_TOKEN }} +# - name: Docker metadata +# id: metadata +# uses: docker/metadata-action@v4 +# with: +# images: ghcr.io/${{ github.repository_owner }}/openim-${{ matrix.bin }} +# - name: Build and release Docker images +# uses: docker/build-push-action@v3 +# with: +# platforms: linux/386,linux/amd64,linux/arm64/v8 +# target: ${{ matrix.bin }} +# tags: ${{ steps.metadata.outputs.tags }} +# push: true \ No newline at end of file diff --git a/.github/workflows/check-coverage.yml b/.github/workflows/check-coverage.yml index cc596a0e0..937af89d6 100644 --- a/.github/workflows/check-coverage.yml +++ b/.github/workflows/check-coverage.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM open source community. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: Check-Coverage on: diff --git a/.github/workflows/cla.yml b/.github/workflows/cla.yml index 603a49399..3f658284a 100644 --- a/.github/workflows/cla.yml +++ b/.github/workflows/cla.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM open source community. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: "OpenIM CLA Assistant" on: issue_comment: diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 6233ffcad..a76979ab9 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -13,7 +13,7 @@ # limitations under the License. -name: "Code Scanning - Action" +name: "OpenIM Code Scanning - Action" on: push: @@ -42,8 +42,8 @@ jobs: security-events: write # only required for workflows in private repositories - actions: read - contents: read + actions: write + contents: write steps: - name: Checkout repository @@ -53,8 +53,8 @@ jobs: - name: Initialize CodeQL uses: github/codeql-action/init@v2 # Override language selection by uncommenting this and choosing your languages - # with: - # languages: go, javascript, csharp, python, cpp, java, ruby + with: + languages: go # Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java). # If this step fails, then you should remove it and run the build manually (see below). @@ -68,9 +68,9 @@ jobs: # three lines and modify them (or add more) to build your code if your # project uses a compiled language - #- run: | - # make bootstrap - # make release + # - run: | + # make bootstrap + # make release - name: Perform CodeQL Analysis uses: github/codeql-action/analyze@v2 \ No newline at end of file diff --git a/.github/workflows/create_branch_on_tag.yml b/.github/workflows/create_branch_on_tag.yml new file mode 100644 index 000000000..bdb53e80a --- /dev/null +++ b/.github/workflows/create_branch_on_tag.yml @@ -0,0 +1,62 @@ +name: Create Branch on Tag + +on: + push: + tags: + - 'v*.*.0' + +jobs: + create-branch: + runs-on: ubuntu-latest + steps: + - name: Check out code + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Set up Git + run: | + git config --global user.name 'kubbot' + git config --global user.email '3293172751yxy@gmail.com' + + - name: Install git-chglog + run: make install.git-chglog + + - name: Create Branch + run: | + TAG_NAME=${GITHUB_REF/refs\/tags\//} + IFS='.' read -ra VERSION_PARTS <<< "$TAG_NAME" + if [[ "${VERSION_PARTS[2]}" = "0" ]]; then + BRANCH_NAME="release-v${VERSION_PARTS[0]}.${VERSION_PARTS[1]}" + echo "Creating branch $BRANCH_NAME" + git checkout -b "$BRANCH_NAME" + git push origin "$BRANCH_NAME" + fi + + - name: Push Changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: "Auto Commit CHANGELOG" + branch: release-v${VERSION_PARTS[0]}.${VERSION_PARTS[1]} + env: + GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }} + + - name: Create and Push CHANGELOG + run: | + git checkout main + TAG_NAME=${GITHUB_REF/refs\/tags\//} + IFS='.' read -ra VERSION_PARTS <<< "$TAG_NAME" + if [[ "${VERSION_PARTS[2]}" = "0" ]]; then + cd CHANGELOG + git-chglog --tag-filter-pattern "v${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.*" -o "CHANGELOG-${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.md" + git add "CHANGELOG-${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.md" + git commit -m "Update CHANGELOG for $TAG_NAME" + fi + + - name: Push Changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: "Auto Commit CHANGELOG" + branch: main + env: + GITHUB_TOKEN: ${{ secrets.BOT_GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 3fbad1e5f..ef50743a1 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM open source community. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: deploy for dev on: diff --git a/.github/workflows/e2e-test.yml b/.github/workflows/e2e-test.yml index a3451d6aa..b9fa29aef 100644 --- a/.github/workflows/e2e-test.yml +++ b/.github/workflows/e2e-test.yml @@ -1,2 +1,16 @@ +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + # name: OpenIM e2e Test diff --git a/.github/workflows/gosec.yml b/.github/workflows/gosec.yml index 43fba5c0c..708eca4f8 100644 --- a/.github/workflows/gosec.yml +++ b/.github/workflows/gosec.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: OpenIM Run Gosec # gosec is a source code security audit tool for the Go language. It performs a static diff --git a/.github/workflows/help-comment-issue.yml b/.github/workflows/help-comment-issue.yml index 5f52e638f..755fb035d 100644 --- a/.github/workflows/help-comment-issue.yml +++ b/.github/workflows/help-comment-issue.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: Good frist issue add comment on: issues: diff --git a/.github/workflows/issue-robot.yml b/.github/workflows/issue-robot.yml index 74927a0c2..b421a5314 100644 --- a/.github/workflows/issue-robot.yml +++ b/.github/workflows/issue-robot.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM open source community. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: 'issue translator' on: issue_comment: diff --git a/.github/workflows/link-pr.yml b/.github/workflows/link-pr.yml index c6da4cb5f..07b9d46a9 100644 --- a/.github/workflows/link-pr.yml +++ b/.github/workflows/link-pr.yml @@ -1,12 +1,23 @@ +# Copyright © 2023 OpenIM open source community. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: Github Rebot for Link check error # Every Monday at 12:30 p.m on: schedule: - cron: '30 12 * * 1' - push: - branches: - - main jobs: linkChecker: diff --git a/.github/workflows/lock-issue.yml b/.github/workflows/lock-issue.yml index 8cf0433d6..6a9aed9ba 100644 --- a/.github/workflows/lock-issue.yml +++ b/.github/workflows/lock-issue.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM open source community. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: 'Lock Threads' on: diff --git a/.github/workflows/opencommit.yml b/.github/workflows/opencommit.yml index 69f7d7f23..36307c00f 100644 --- a/.github/workflows/opencommit.yml +++ b/.github/workflows/opencommit.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: 'OpenIM Commit Action' on: diff --git a/.github/workflows/openimci.yml b/.github/workflows/openimci.yml index cd055b5b7..07133126b 100644 --- a/.github/workflows/openimci.yml +++ b/.github/workflows/openimci.yml @@ -128,6 +128,14 @@ jobs: # commit_author: Kubbot # defaults to author of the commit that triggered the run continue-on-error: true + - name: Commit Changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: "chore(fmt): robot automated format and lint Change" + commit_options: '--no-verify --signoff' + branch: main + continue-on-error: true + - name: Set Current Directory id: set_directory run: | diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 726d57e14..3405e479e 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -1,11 +1,24 @@ +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: Github Pull Request on: push: branches: [main] paths-ignore: - 'README.md' - - 'CONTRIBUTING.md' - + - 'CONTRIBUTING.md' - 'docs/**' pull_request: branches: [main] @@ -22,18 +35,80 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - name: checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Get League branch Patch + run: | + git config user.name 'openimbot' + git config user.email 'openimsdk@qq.com' + git checkout -b cicd/patch-${{ github.event.number }} - uses: actions/setup-node@v3 + - name: Setup Go + uses: actions/setup-go@v2 + - name: Run go modules tidy + run: | + sudo make tidy + sudo make tools.verify.go-gitlint + echo "Run go modules tidy successfully" + + - name: Run go format + run: | + sudo make format + echo "Run go format successfully" + continue-on-error: true + + - name: Generate all necessary files, such as error code files + run: | + make generate + echo "Generate all necessary files successfully" + continue-on-error: true + + - name: Run unit test and get test coverage + run: | + make cover + echo "Run unit test and get test coverage successfully" + continue-on-error: true + + - name: Initialize CodeQL + uses: github/codeql-action/init@v2 + with: + languages: go + + - name: Build source code for host platform + run: | + sudo make build + echo "Build source code for host platform successfully" + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v2 + + - name: OpenIM verify copyright + run: | + sudo make verify-copyright + sudo make add-copyright + echo "OpenIM verify successfully" + continue-on-error: true + + - name: Commit code + run: | + git add . + - name: Create Pull Request uses: peter-evans/create-pull-request@v5 with: - token: ${{ secrets.BOT_GITHUB_TOKEN }} - commit-message: 'build: update distribution' - title: 'build: update distribution' + commit-message: "cicd: bump League Patch to cicd/patch-${{ github.event.number }}" + title: Bump League Patch to cicd/patch-${{ github.event.number }} body: | - - Updates the distribution for changes on `main` - - Auto-generated by [create-pull-request][1] - - [1]: https://github.com/OpenIMSDK/Open-IM-Sever - branch: 'bot/update-distribution' \ No newline at end of file + Review criteria: + + - [ ] Disenchanter can connect and issue actions + + This is an automated PR. + [workflow](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/.github/workflows/pull-request.yml). + base: main + branch: cicd/patch-${{ github.event.number }} + labels: kind/documentation, area/test, ok-to-test + token: ${{ secrets.REDBOT_GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c4f190766..45ed79df2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: OpenIM Server Release on: diff --git a/.github/workflows/scripts-test.yml b/.github/workflows/scripts-test.yml index badb33012..e59e696f8 100644 --- a/.github/workflows/scripts-test.yml +++ b/.github/workflows/scripts-test.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: OpenIM Start Execute Scripts on: diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml index 2cad62c85..7790aa944 100644 --- a/.github/workflows/stale.yml +++ b/.github/workflows/stale.yml @@ -1,3 +1,17 @@ +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + # This workflow warns and then closes issues and PRs that have had no activity for a specified amount of time. # # You can adjust the behavior by modifying this file. diff --git a/.goreleaser.yaml b/.goreleaser.yaml index f9ca78474..cbb9ba483 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -36,8 +36,9 @@ builds: - ppc64le - arm64 goarm: - - 6 - - 7 + - "6" + - "7" + - binary: openim-cmdutils id: openim-cmdutils main: ./cmd/openim-cmdutils/main.go @@ -53,8 +54,9 @@ builds: - ppc64le - arm64 goarm: - - 6 - - 7 + - "6" + - "7" + - binary: openim-crontask id: openim-crontask main: ./cmd/openim-crontask/main.go @@ -70,8 +72,9 @@ builds: - ppc64le - arm64 goarm: - - 6 - - 7 + - "6" + - "7" + - binary: openim-msggateway id: openim-msggateway main: ./cmd/openim-msggateway/main.go @@ -87,8 +90,9 @@ builds: - ppc64le - arm64 goarm: - - 6 - - 7 + - "6" + - "7" + - binary: openim-msgtransfer id: openim-msgtransfer main: ./cmd/openim-msgtransfer/main.go @@ -104,8 +108,9 @@ builds: - ppc64le - arm64 goarm: - - 6 - - 7 + - "6" + - "7" + - binary: openim-push id: openim-push main: ./cmd/openim-push/main.go @@ -121,8 +126,9 @@ builds: - ppc64le - arm64 goarm: - - 6 - - 7 + - "6" + - "7" + - binary: openim-rpc-auth id: openim-rpc-auth main: ./cmd/openim-rpc/openim-rpc-auth/main.go @@ -138,8 +144,9 @@ builds: - ppc64le - arm64 goarm: - - 6 - - 7 + - "6" + - "7" + - binary: openim-rpc-conversation id: openim-rpc-conversation main: ./cmd/openim-rpc/openim-rpc-conversation/main.go @@ -155,8 +162,9 @@ builds: - ppc64le - arm64 goarm: - - 6 - - 7 + - "6" + - "7" + - binary: openim-rpc-friend id: openim-rpc-friend main: ./cmd/openim-rpc/openim-rpc-friend/main.go @@ -172,8 +180,9 @@ builds: - ppc64le - arm64 goarm: - - 6 - - 7 + - "6" + - "7" + - binary: openim-rpc-group id: openim-rpc-group main: ./cmd/openim-rpc/openim-rpc-group/main.go @@ -189,8 +198,9 @@ builds: - ppc64le - arm64 goarm: - - 6 - - 7 + - "6" + - "7" + - binary: openim-rpc-msg id: openim-rpc-msg main: ./cmd/openim-rpc/openim-rpc-msg/main.go @@ -206,8 +216,9 @@ builds: - ppc64le - arm64 goarm: - - 6 - - 7 + - "6" + - "7" + - binary: openim-rpc-third id: openim-rpc-third main: ./cmd/openim-rpc/openim-rpc-third/main.go @@ -223,8 +234,9 @@ builds: - ppc64le - arm64 goarm: - - 6 - - 7 + - "6" + - "7" + - binary: openim-rpc-user id: openim-rpc-user main: ./cmd/openim-rpc/openim-rpc-user/main.go @@ -240,8 +252,8 @@ builds: - ppc64le - arm64 goarm: - - 6 - - 7 + - "6" + - "7" archives: - format: tar.gz @@ -323,6 +335,66 @@ changelog: - title: Other work order: 9999 +dockers: + - image_templates: + - "openimsdk/open-im-server:{{ .Tag }}-amd64" + - "ghcr.io/goreleaser/goreleaser:{{ .Tag }}-amd64" + dockerfile: Dockerfile + use: buildx + build_flag_templates: + - "--pull" + - "--label=io.artifacthub.package.readme-url=https://raw.githubusercontent.com/goreleaser/goreleaser/main/README.md" + - "--label=io.artifacthub.package.logo-url=https://goreleaser.com/static/avatar.png" + - '--label=io.artifacthub.package.maintainers=[{"name":"Carlos Alexandro Becker","email":"carlos@carlosbecker.dev"}]' + - "--label=io.artifacthub.package.license=MIT" + - "--label=org.opencontainers.image.description=Deliver Go binaries as fast and easily as possible" + - "--label=org.opencontainers.image.created={{.Date}}" + - "--label=org.opencontainers.image.name={{.ProjectName}}" + - "--label=org.opencontainers.image.revision={{.FullCommit}}" + - "--label=org.opencontainers.image.version={{.Version}}" + - "--label=org.opencontainers.image.source={{.GitURL}}" + - "--platform=linux/amd64" + extra_files: + - scripts/entrypoint.sh + - image_templates: + - "goreleaser/goreleaser:{{ .Tag }}-arm64" + - "ghcr.io/goreleaser/goreleaser:{{ .Tag }}-arm64" + dockerfile: Dockerfile + use: buildx + build_flag_templates: + - "--pull" + - "--label=io.artifacthub.package.readme-url=https://raw.githubusercontent.com/goreleaser/goreleaser/main/README.md" + - "--label=io.artifacthub.package.logo-url=https://goreleaser.com/static/avatar.png" + - '--label=io.artifacthub.package.maintainers=[{"name":"Carlos Alexandro Becker","email":"carlos@carlosbecker.dev"}]' + - "--label=io.artifacthub.package.license=MIT" + - "--label=org.opencontainers.image.description=Deliver Go binaries as fast and easily as possible" + - "--label=org.opencontainers.image.created={{.Date}}" + - "--label=org.opencontainers.image.name={{.ProjectName}}" + - "--label=org.opencontainers.image.revision={{.FullCommit}}" + - "--label=org.opencontainers.image.version={{.Version}}" + - "--label=org.opencontainers.image.source={{.GitURL}}" + - "--platform=linux/arm64" + goarch: arm64 + extra_files: + - scripts/entrypoint.sh + +docker_manifests: + - name_template: "goreleaser/goreleaser:{{ .Tag }}" + image_templates: + - "goreleaser/goreleaser:{{ .Tag }}-amd64" + - "goreleaser/goreleaser:{{ .Tag }}-arm64" + - name_template: "ghcr.io/goreleaser/goreleaser:{{ .Tag }}" + image_templates: + - "ghcr.io/goreleaser/goreleaser:{{ .Tag }}-amd64" + - "ghcr.io/goreleaser/goreleaser:{{ .Tag }}-arm64" + - name_template: "goreleaser/goreleaser:latest" + image_templates: + - "goreleaser/goreleaser:{{ .Tag }}-amd64" + - "goreleaser/goreleaser:{{ .Tag }}-arm64" + - name_template: "ghcr.io/goreleaser/goreleaser:latest" + image_templates: + - "ghcr.io/goreleaser/goreleaser:{{ .Tag }}-amd64" + - "ghcr.io/goreleaser/goreleaser:{{ .Tag }}-arm64" nfpms: - id: packages @@ -412,19 +484,22 @@ release: ## Helping out + We release logs are recorded on [✨ CHANGELOG](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/CHANGELOG/CHANGELOG.md) + This release is only possible thanks to **all** the support of some **awesome people**! - Want to be one of them? + **Want to be one of them 😘?** + Contributions to this project are welcome! Please see [CONTRIBUTING.md](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/CONTRIBUTING.md) for details.

- + - +

diff --git a/CHANGELOG/CHANGELOG.md b/CHANGELOG/CHANGELOG.md index 2d788d4c4..822e9bc92 100644 --- a/CHANGELOG/CHANGELOG.md +++ b/CHANGELOG/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog - [Changelog](#changelog) + - [OpenIM versioning policy](#openim-versioning-policy) - [command](#command) - [create next tag](#create-next-tag) - [Release version logs](#release-version-logs) @@ -15,6 +16,10 @@ All notable changes to this project will be documented in this file. + [https://github.com/OpenIMSDK/Open-IM-Server/releases](https://github.com/OpenIMSDK/Open-IM-Server/releases) +## OpenIM versioning policy + ++ [OpenIM Version](../docs/conversions/version.md) + ## command ```bash diff --git a/README.md b/README.md index c4a4318b2..11471ac0f 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ DATA_DIR=./ #designate large disk directory 1. Deploy and start -Note: This command can only be executed once. It will modify the component passwords in docker-compose based on the PASSWORD variable in .env, and modify the component passwords in config/config.yaml. If the password in .env changes, you need to first execute docker-compose down; rm components -rf and then execute this command. +> **Note**: This command can only be executed once. It will modify the component passwords in docker-compose based on the PASSWORD variable in .env, and modify the component passwords in config/config.yaml. If the password in .env changes, you need to first execute docker-compose down; rm components -rf and then execute this command. ``` chmod +x install_im_server.sh; diff --git a/build/docker/openim-api/Dockerfile b/build/docker/openim-api/Dockerfile new file mode 100644 index 000000000..1bf965ea4 --- /dev/null +++ b/build/docker/openim-api/Dockerfile @@ -0,0 +1,12 @@ +FROM golang:1.20.0 as build + +WORKDIR /openim + +RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ + echo "Asia/Shanghai" > /etc/timezone && \ + mkdir -p /var/log/miniblog + +COPY miniblog /openim + +EXPOSE 10002 +CMD ["./bin/openim-api","--port", "10002"] \ No newline at end of file diff --git a/build/docker/openim-cmdutils/Dockerfile b/build/docker/openim-cmdutils/Dockerfile new file mode 100644 index 000000000..e69de29bb diff --git a/build/docker/openim-crontask/Dockerfile b/build/docker/openim-crontask/Dockerfile new file mode 100644 index 000000000..e69de29bb diff --git a/build/docker/openim-msggateway/Dockerfile b/build/docker/openim-msggateway/Dockerfile new file mode 100644 index 000000000..e69de29bb diff --git a/build/docker/openim-msgtransfer/Dockerfile b/build/docker/openim-msgtransfer/Dockerfile new file mode 100644 index 000000000..e69de29bb diff --git a/build/docker/openim-push/Dockerfile b/build/docker/openim-push/Dockerfile new file mode 100644 index 000000000..e69de29bb diff --git a/build/docker/openim-rpc-auth/Dockerfile b/build/docker/openim-rpc-auth/Dockerfile new file mode 100644 index 000000000..e69de29bb diff --git a/build/docker/openim-rpc-conversation/Dockerfile b/build/docker/openim-rpc-conversation/Dockerfile new file mode 100644 index 000000000..e69de29bb diff --git a/build/docker/openim-rpc-friend/Dockerfile b/build/docker/openim-rpc-friend/Dockerfile new file mode 100644 index 000000000..e69de29bb diff --git a/build/docker/openim-rpc-group/Dockerfile b/build/docker/openim-rpc-group/Dockerfile new file mode 100644 index 000000000..e69de29bb diff --git a/build/docker/openim-rpc-msg/Dockerfile b/build/docker/openim-rpc-msg/Dockerfile new file mode 100644 index 000000000..e69de29bb diff --git a/build/docker/openim-rpc-third/Dockerfile b/build/docker/openim-rpc-third/Dockerfile new file mode 100644 index 000000000..e69de29bb diff --git a/build/docker/openim-rpc-user/Dockerfile b/build/docker/openim-rpc-user/Dockerfile new file mode 100644 index 000000000..e69de29bb diff --git a/config/config.yaml b/config/config.yaml index 10d43bd3e..1c01031dc 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -24,8 +24,8 @@ zookeeper: mysql: address: [ 127.0.0.1:13306 ] #目前仅支持单机 - username: root - password: openIM123 + username: root #用户名 + password: openIM123 #密码 database: openIM_v3 #不建议修改 maxOpenConn: 1000 #最大连接数 maxIdleConn: 100 #最大空闲连接数 @@ -37,14 +37,14 @@ mongo: uri: #不为空则直接使用该值 address: [ 127.0.0.1:37017 ] #单机时为mongo地址,使用分片集群时,为mongos地址 database: openIM_v3 #mongo db 默认即可 - username: root - password: openIM123 + username: root #用户名 + password: openIM123 #密码 maxPoolSize: 100 redis: address: [ 127.0.0.1:16379 ] # username: #only redis version 6.0+ need username - password: openIM123 + password: openIM123 #密码 kafka: username: #用户名 @@ -77,9 +77,9 @@ object: apiURL: "http://127.0.0.1:10002/object/" #地址需要app能访问到 minio: bucket: "openim" #不建议修改 - endpoint: http://127.0.0.1:10005 #地址需要app能够访问 - accessKeyID: root - secretAccessKey: openIM123 + endpoint: "http://127.0.0.1:10005" #minio对外服务的ip和端口,app要能访问此ip和端口 + accessKeyID: "root" #ID + secretAccessKey: "openIM123" #秘钥 sessionToken: "" #token cos: #tencent cos bucketURL: "https://temp-1252357374.cos.ap-chengdu.myqcloud.com" @@ -167,7 +167,7 @@ retainChatRecords: 365 #mongo保存离线消息 chatRecordsClearTime: "0 2 * * 3" #每周三凌晨2点清理mongo中的过期(超过retainChatRecords时间)消息,这个删除是为了清理满足上个配置retainChatRecords的过期消息,不会发送通知,仅仅作为清理磁盘使用 msgDestructTime: "0 2 * * *" #消息自动删除时间,每天凌晨2点删除过期消息,这个删除是为了删除保留时间超过超过会话字段msg_destruct_time(秒)的消息。 -secret: openIM123 +secret: tuoyun #秘钥,获取token时校验 tokenPolicy: expire: 90 #过期时间(天) diff --git a/docs/conversions/README.md b/docs/conversions/README.md new file mode 100644 index 000000000..05fd7dcb1 --- /dev/null +++ b/docs/conversions/README.md @@ -0,0 +1,9 @@ +## OpenIM Project Development Standards + +- [Code Standards](https://chat.openai.com/go_code.md) +- [Directory Standards](https://chat.openai.com/directory.md) +- [Commit Standards](https://chat.openai.com/commit.md) +- [Versioning Standards](https://chat.openai.com/version.md) +- [Interface Standards](https://chat.openai.com/api.md) +- [Log Standards](https://chat.openai.com/log.md) +- [Error Code Standards](https://chat.openai.com/error_code.md) \ No newline at end of file diff --git a/docs/conversions/api.md b/docs/conversions/api.md new file mode 100644 index 000000000..1dd389258 --- /dev/null +++ b/docs/conversions/api.md @@ -0,0 +1,5 @@ +## Interface Standards + +Our project, OpenIM, adheres to the [OpenAPI 3.0](https://spec.openapis.org/oas/latest.html) interface standards. + +> Chinese translation: [OpenAPI Specification Chinese Translation](https://fishead.gitbook.io/openapi-specification-zhcn-translation/3.0.0.zhcn) \ No newline at end of file diff --git a/docs/conversions/commit.md b/docs/conversions/commit.md new file mode 100644 index 000000000..661661fe9 --- /dev/null +++ b/docs/conversions/commit.md @@ -0,0 +1,9 @@ +## Commit Standards + +Our project, OpenIM, follows the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0) standards. + +> Chinese translation: [Conventional Commits: A Specification Making Commit Logs More Human and Machine-friendly](https://tool.lu/en_US/article/2ac/preview) + +In addition to adhering to these standards, we encourage all contributors to the OpenIM project to ensure that their commit messages are clear and descriptive. This helps in maintaining a clean and meaningful project history. Each commit message should succinctly describe the changes made and, where necessary, the reasoning behind those changes. + +To facilitate a streamlined process, we also recommend using appropriate commit type based on Conventional Commits guidelines such as `fix:` for bug fixes, `feat:` for new features, and so forth. Understanding and using these conventions helps in generating automatic release notes, making versioning easier, and improving overall readability of commit history. \ No newline at end of file diff --git a/docs/conversions/directory.md b/docs/conversions/directory.md new file mode 100644 index 000000000..4e30d3854 --- /dev/null +++ b/docs/conversions/directory.md @@ -0,0 +1,3 @@ +## Catalog Service Interface Specification + ++ [https://github.com/kubecub/go-project-layout](https://github.com/kubecub/go-project-layout) \ No newline at end of file diff --git a/docs/conversions/error_code.md b/docs/conversions/error_code.md new file mode 100644 index 000000000..169567b33 --- /dev/null +++ b/docs/conversions/error_code.md @@ -0,0 +1,22 @@ +## Error Code Standards + +Error codes are one of the important means for users to locate and solve problems. When an application encounters an exception, users can quickly locate and resolve the problem based on the error code and the description and solution of the error code in the documentation. + +### Error Code Naming Standards + +- Follow CamelCase notation; +- Error codes are divided into two levels. For example, `InvalidParameter.BindError`, separated by a `.`. The first-level error code is platform-level, and the second-level error code is resource-level, which can be customized according to the scenario; +- The second-level error code can only use English letters or numbers ([a-zA-Z0-9]), and should use standard English word spelling, standard abbreviations, RFC term abbreviations, etc.; +- The error code should avoid multiple definitions of the same semantics, for example: `InvalidParameter.ErrorBind`, `InvalidParameter.BindError`. + +### First-Level Common Error Codes + +| Error Code | Error Description | Error Type | +| ---------------- | ------------------------------------------------------------ | ---------- | +| InternalError | Internal error | 1 | +| InvalidParameter | Parameter error (including errors in parameter type, format, value, etc.) | 0 | +| AuthFailure | Authentication / Authorization error | 0 | +| ResourceNotFound | Resource does not exist | 0 | +| FailedOperation | Operation failed | 2 | + +> Error Type: 0 represents the client, 1 represents the server, 2 represents both the client / server. \ No newline at end of file diff --git a/docs/conversions/go_code.md b/docs/conversions/go_code.md new file mode 100644 index 000000000..85d099f9c --- /dev/null +++ b/docs/conversions/go_code.md @@ -0,0 +1,901 @@ +## Go 代码开发规范 +在Go 项目开发中,一个好的编码规范可以极大的提高代码质量。为了帮你节省时间和精力,这里我整理了一份清晰、可直接套用的 Go 编码规范,供你参考。 + +这份规范,是我参考了 Go 官方提供的编码规范,以及 Go 社区沉淀的一些比较合理的规范之后,加入自己的理解总结出的,它比很多公司内部的规范更全面,你掌握了,以后在面试大厂的时候,或者在大厂里写代码的时候,都会让人高看你一眼,觉得你code很专业。 + +这份编码规范中包含代码风格、命名规范、注释规范、类型、控制结构、函数、GOPATH 设置规范、依赖管理和最佳实践九类规范。如果你觉得这些规范内容太多了,看完一遍也记不住,这完全没关系。你可以多看几遍,也可以在用到时把它翻出来,在实际应用中掌握。这篇特别放送的内容,更多是作为写代码时候的一个参考手册。 + +## 1. 代码风格 + +### 1.1 代码格式 + +- 代码都必须用 `gofmt` 进行格式化。 +- 运算符和操作数之间要留空格。 +- 建议一行代码不超过120个字符,超过部分,请采用合适的换行方式换行。但也有些例外场景,例如import行、工具自动生成的代码、带tag的struct字段。 +- 文件长度不能超过800行。 +- 函数长度不能超过80行。 +- import规范 + - 代码都必须用`goimports`进行格式化(建议将代码Go代码编辑器设置为:保存时运行 `goimports`)。 + - 不要使用相对路径引入包,例如 `import ../util/net` 。 + - 包名称与导入路径的最后一个目录名不匹配时,或者多个相同包名冲突时,则必须使用导入别名。 + +```go + // bad + "github.com/dgrijalva/jwt-go/v4" + + //good + jwt "github.com/dgrijalva/jwt-go/v4" +``` + - 导入的包建议进行分组,匿名包的引用使用一个新的分组,并对匿名包引用进行说明。 + +```go + import ( + // go 标准包 + "fmt" + + // 第三方包 + "github.com/jinzhu/gorm" + "github.com/spf13/cobra" + "github.com/spf13/viper" + + // 匿名包单独分组,并对匿名包引用进行说明 + // import mysql driver + _ "github.com/jinzhu/gorm/dialects/mysql" + + // 内部包 + v1 "github.com/marmotedu/api/apiserver/v1" + metav1 "github.com/marmotedu/apimachinery/pkg/meta/v1" + "github.com/marmotedu/iam/pkg/cli/genericclioptions" + ) +``` + +### 1.2 声明、初始化和定义 + +当函数中需要使用到多个变量时,可以在函数开始处使用`var`声明。在函数外部声明必须使用 `var` ,不要采用 `:=` ,容易踩到变量的作用域的问题。 + +```go +var ( + Width int + Height int +) +``` + +- 在初始化结构引用时,请使用`&T{}`代替`new(T)`,以使其与结构体初始化一致。 + +```go +// bad +sptr := new(T) +sptr.Name = "bar" + +// good +sptr := &T{Name: "bar"} +``` + +- struct 声明和初始化格式采用多行,定义如下。 + +```go +type User struct{ + Username string + Email string +} + +user := User{ + Username: "belm", + Email: "nosbelm@qq.com", +} +``` + +- 相似的声明放在一组,同样适用于常量、变量和类型声明。 + +```go +// bad +import "a" +import "b" + +// good +import ( + "a" + "b" +) +``` + +- 尽可能指定容器容量,以便为容器预先分配内存,例如: + +```go +v := make(map[int]string, 4) +v := make([]string, 0, 4) +``` + +- 在顶层,使用标准var关键字。请勿指定类型,除非它与表达式的类型不同。 + +```go +// bad +var _s string = F() + +func F() string { return "A" } + +// good +var _s = F() +// 由于 F 已经明确了返回一个字符串类型,因此我们没有必要显式指定_s 的类型 +// 还是那种类型 + +func F() string { return "A" } +``` + +- 对于未导出的顶层常量和变量,使用`_`作为前缀。 + +```go +// bad +const ( + defaultHost = "127.0.0.1" + defaultPort = 8080 +) + +// good +const ( + _defaultHost = "127.0.0.1" + _defaultPort = 8080 +) +``` + +- 嵌入式类型(例如 mutex)应位于结构体内的字段列表的顶部,并且必须有一个空行将嵌入式字段与常规字段分隔开。 + +```go +// bad +type Client struct { + version int + http.Client +} + +// good +type Client struct { + http.Client + + version int +} +``` + +### 1.3 错误处理 + +- `error`作为函数的值返回,必须对`error`进行处理,或将返回值赋值给明确忽略。对于`defer xx.Close()`可以不用显式处理。 + +```go +func load() error { + // normal code +} + +// bad +load() + +// good + _ = load() +``` + +- `error`作为函数的值返回且有多个返回值的时候,`error`必须是最后一个参数。 + +```go +// bad +func load() (error, int) { + // normal code +} + +// good +func load() (int, error) { + // normal code +} +``` + +- 尽早进行错误处理,并尽早返回,减少嵌套。 + +```go +// bad +if err != nil { + // error code +} else { + // normal code +} + +// good +if err != nil { + // error handling + return err +} +// normal code +``` + +- 如果需要在 if 之外使用函数调用的结果,则应采用下面的方式。 + +```go +// bad +if v, err := foo(); err != nil { + // error handling +} + +// good +v, err := foo() +if err != nil { + // error handling +} +``` + +- 错误要单独判断,不与其他逻辑组合判断。 + +```go +// bad +v, err := foo() +if err != nil || v == nil { + // error handling + return err +} + +// good +v, err := foo() +if err != nil { + // error handling + return err +} + +if v == nil { + // error handling + return errors.New("invalid value v") +} +``` + +- 如果返回值需要初始化,则采用下面的方式。 + +```go +v, err := f() +if err != nil { + // error handling + return // or continue. +} +``` + +- 错误描述建议 + - 错误描述用小写字母开头,结尾不要加标点符号,例如: +```go + // bad + errors.New("Redis connection failed") + errors.New("redis connection failed.") + + // good + errors.New("redis connection failed") +``` + - 告诉用户他们可以做什么,而不是告诉他们不能做什么。 + - 当声明一个需求时,用must 而不是should。例如,`must be greater than 0、must match regex '[a-z]+'`。 + - 当声明一个格式不对时,用must not。例如,`must not contain`。 + - 当声明一个动作时用may not。例如,`may not be specified when otherField is empty、only name may be specified`。 + - 引用文字字符串值时,请在单引号中指示文字。例如,`ust not contain '..'`。 + - 当引用另一个字段名称时,请在反引号中指定该名称。例如,must be greater than `request`。 + - 指定不等时,请使用单词而不是符号。例如,`must be less than 256、must be greater than or equal to 0 (不要用 larger than、bigger than、more than、higher than)`。 + - 指定数字范围时,请尽可能使用包含范围。 + - 建议 Go 1.13 以上,error 生成方式为 `fmt.Errorf("module xxx: %w", err)`。 + +### 1.4 panic处理 + +- 在业务逻辑处理中禁止使用panic。 +- 在main包中,只有当程序完全不可运行时使用panic,例如无法打开文件、无法连接数据库导致程序无法正常运行。 +- 在main包中,使用 `log.Fatal` 来记录错误,这样就可以由log来结束程序,或者将panic抛出的异常记录到日志文件中,方便排查问题。 +- 可导出的接口一定不能有panic。 +- 包内建议采用error而不是panic来传递错误。 + +### 1.5 单元测试 + +- 单元测试文件名命名规范为 `example_test.go`。 +- 每个重要的可导出函数都要编写测试用例。 +- 因为单元测试文件内的函数都是不对外的,所以可导出的结构体、函数等可以不带注释。 +- 如果存在 `func (b *Bar) Foo` ,单测函数可以为 `func TestBar_Foo`。 + +### 1.6 类型断言失败处理 + +- type assertion 的单个返回值针对不正确的类型将产生 panic。请始终使用 “comma ok”的惯用法。 + +```go +// bad +t := n.(int) + +// good +t, ok := n.(int) +if !ok { + // error handling +} +``` + +## 2. 命名规范 + +命名规范是代码规范中非常重要的一部分,一个统一的、短小的、精确的命名规范可以大大提高代码的可读性,也可以借此规避一些不必要的Bug。 + +### 2.1 包命名 + +- 包名必须和目录名一致,尽量采取有意义、简短的包名,不要和标准库冲突。 +- 包名全部小写,没有大写或下划线,使用多级目录来划分层级。 +- 项目名可以通过中划线来连接多个单词。 +- 包名以及包所在的目录名,不要使用复数,例如,是`net/url`,而不是`net/urls`。 +- 不要用 common、util、shared 或者 lib 这类宽泛的、无意义的包名。 +- 包名要简单明了,例如 net、time、log。 + +### 2.2 函数命名 + +- 函数名采用驼峰式,首字母根据访问控制决定使用大写或小写,例如:`MixedCaps`或者`mixedCaps`。 +- 代码生成工具自动生成的代码(如`xxxx.pb.go`)和为了对相关测试用例进行分组,而采用的下划线(如`TestMyFunction_WhatIsBeingTested`)排除此规则。 + +### 2.3 文件命名 + +- 文件名要简短有意义。 +- 文件名应小写,并使用下划线分割单词。 + +### 2.4 结构体命名 + +- 采用驼峰命名方式,首字母根据访问控制决定使用大写或小写,例如`MixedCaps`或者`mixedCaps`。 +- 结构体名不应该是动词,应该是名词,比如 `Node`、`NodeSpec`。 +- 避免使用Data、Info这类无意义的结构体名。 +- 结构体的声明和初始化应采用多行,例如: + +```go +// User 多行声明 +type User struct { + Name string + Email string +} + +// 多行初始化 +u := User{ + UserName: "belm", + Email: "nosbelm@qq.com", +} +``` + +### 2.5 接口命名 + +- 接口命名的规则,基本和结构体命名规则保持一致: + - 单个函数的接口名以 “er"”作为后缀(例如Reader,Writer),有时候可能导致蹩脚的英文,但是没关系。 + - 两个函数的接口名以两个函数名命名,例如ReadWriter。 + - 三个以上函数的接口名,类似于结构体名。 + +例如: + +``` + // Seeking to an offset before the start of the file is an error. + // Seeking to any positive offset is legal, but the behavior of subsequent + // I/O operations on the underlying object is implementation-dependent. + type Seeker interface { + Seek(offset int64, whence int) (int64, error) + } + + // ReadWriter is the interface that groups the basic Read and Write methods. + type ReadWriter interface { + Reader + Writer + } +``` + +### 2.6 变量命名 + +- 变量名必须遵循驼峰式,首字母根据访问控制决定使用大写或小写。 +- 在相对简单(对象数量少、针对性强)的环境中,可以将一些名称由完整单词简写为单个字母,例如: + - user 可以简写为 u; + - userID 可以简写 uid。 +- 特有名词时,需要遵循以下规则: + - 如果变量为私有,且特有名词为首个单词,则使用小写,如 apiClient。 + - 其他情况都应当使用该名词原有的写法,如 APIClient、repoID、UserID。 + +下面列举了一些常见的特有名词。 + +``` +// A GonicMapper that contains a list of common initialisms taken from golang/lint +var LintGonicMapper = GonicMapper{ + "API": true, + "ASCII": true, + "CPU": true, + "CSS": true, + "DNS": true, + "EOF": true, + "GUID": true, + "HTML": true, + "HTTP": true, + "HTTPS": true, + "ID": true, + "IP": true, + "JSON": true, + "LHS": true, + "QPS": true, + "RAM": true, + "RHS": true, + "RPC": true, + "SLA": true, + "SMTP": true, + "SSH": true, + "TLS": true, + "TTL": true, + "UI": true, + "UID": true, + "UUID": true, + "URI": true, + "URL": true, + "UTF8": true, + "VM": true, + "XML": true, + "XSRF": true, + "XSS": true, +} +``` + +- 若变量类型为bool类型,则名称应以Has,Is,Can或Allow开头,例如: + +```go +var hasConflict bool +var isExist bool +var canManage bool +var allowGitHook bool +``` + +- 局部变量应当尽可能短小,比如使用buf指代buffer,使用i指代index。 +- 代码生成工具自动生成的代码可排除此规则(如`xxx.pb.go`里面的Id) + +### 2.7 常量命名 + +- 常量名必须遵循驼峰式,首字母根据访问控制决定使用大写或小写。 +- 如果是枚举类型的常量,需要先创建相应类型: + +```go +// Code defines an error code type. +type Code int + +// Internal errors. +const ( + // ErrUnknown - 0: An unknown error occurred. + ErrUnknown Code = iota + // ErrFatal - 1: An fatal error occurred. + ErrFatal +) +``` + +### 2.8 Error的命名 + +- Error类型应该写成FooError的形式。 + +```go +type ExitError struct { + // .... +} +``` + +- Error变量写成ErrFoo的形式。 + +```go +var ErrFormat = errors.New("unknown format") +``` + +## 3. 注释规范 + +- 每个可导出的名字都要有注释,该注释对导出的变量、函数、结构体、接口等进行简要介绍。 +- 全部使用单行注释,禁止使用多行注释。 +- 和代码的规范一样,单行注释不要过长,禁止超过 120 字符,超过的请使用换行展示,尽量保持格式优雅。 +- 注释必须是完整的句子,以需要注释的内容作为开头,句点作为结尾,`格式为 // 名称 描述.`。例如: + +```go +// bad +// logs the flags in the flagset. +func PrintFlags(flags *pflag.FlagSet) { + // normal code +} + +// good +// PrintFlags logs the flags in the flagset. +func PrintFlags(flags *pflag.FlagSet) { + // normal code +} +``` + +- 所有注释掉的代码在提交code review前都应该被删除,否则应该说明为什么不删除,并给出后续处理建议。 + +- 在多段注释之间可以使用空行分隔加以区分,如下所示: + +```go +// Package superman implements methods for saving the world. +// +// Experience has shown that a small number of procedures can prove +// helpful when attempting to save the world. +package superman +``` + +### 3.1 包注释 + +- 每个包都有且仅有一个包级别的注释。 +- 包注释统一用 // 进行注释,格式为 `// Package 包名 包描述`,例如: + +```go +// Package genericclioptions contains flags which can be added to you command, bound, completed, and produce +// useful helper functions. +package genericclioptions +``` + +### 3.2 变量/常量注释 + +- 每个可导出的变量/常量都必须有注释说明,`格式为// 变量名 变量描述`,例如: + +```go +// ErrSigningMethod defines invalid signing method error. +var ErrSigningMethod = errors.New("Invalid signing method") +``` +- 出现大块常量或变量定义时,可在前面注释一个总的说明,然后在每一行常量的前一行或末尾详细注释该常量的定义,例如: +```go +// Code must start with 1xxxxx. +const ( + // ErrSuccess - 200: OK. + ErrSuccess int = iota + 100001 + + // ErrUnknown - 500: Internal server error. + ErrUnknown + + // ErrBind - 400: Error occurred while binding the request body to the struct. + ErrBind + + // ErrValidation - 400: Validation failed. + ErrValidation +) +``` +### 3.3 结构体注释 + +- 每个需要导出的结构体或者接口都必须有注释说明,格式为 `// 结构体名 结构体描述.`。 +- 结构体内的可导出成员变量名,如果意义不明确,必须要给出注释,放在成员变量的前一行或同一行的末尾。例如: + +```go +// User represents a user restful resource. It is also used as gorm model. +type User struct { + // Standard object's metadata. + metav1.ObjectMeta `json:"metadata,omitempty"` + + Nickname string `json:"nickname" gorm:"column:nickname"` + Password string `json:"password" gorm:"column:password"` + Email string `json:"email" gorm:"column:email"` + Phone string `json:"phone" gorm:"column:phone"` + IsAdmin int `json:"isAdmin,omitempty" gorm:"column:isAdmin"` +} +``` + +### 3.4 方法注释 + +每个需要导出的函数或者方法都必须有注释,格式为// 函数名 函数描述.,例如: + +```go +// BeforeUpdate run before update database record. +func (p *Policy) BeforeUpdate() (err error) { + // normal code + return nil +} +``` + +### 3.5 类型注释 + +- 每个需要导出的类型定义和类型别名都必须有注释说明,格式为 `// 类型名 类型描述.`,例如: + +```go +// Code defines an error code type. +type Code int +``` + +## 4. 类型 + +### 4.1 字符串 + +- 空字符串判断。 + +```go +// bad +if s == "" { + // normal code +} + +// good +if len(s) == 0 { + // normal code +} +``` + +- `[]byte`/`string`相等比较。 + +```go +// bad +var s1 []byte +var s2 []byte +... +bytes.Equal(s1, s2) == 0 +bytes.Equal(s1, s2) != 0 + +// good +var s1 []byte +var s2 []byte +... +bytes.Compare(s1, s2) == 0 +bytes.Compare(s1, s2) != 0 +``` + +- 复杂字符串使用raw字符串避免字符转义。 + +```go +// bad +regexp.MustCompile("\\.") + +// good +regexp.MustCompile(`\.`) +``` + +### 4.2 切片 + +- 空slice判断。 + +```go +// bad +if len(slice) = 0 { + // normal code +} + +// good +if slice != nil && len(slice) == 0 { + // normal code +} +``` + +上面判断同样适用于map、channel。 + +- 声明slice。 + +```go +// bad +s := []string{} +s := make([]string, 0) + +// good +var s []string +``` + +- slice复制。 + +```go +// bad +var b1, b2 []byte +for i, v := range b1 { + b2[i] = v +} +for i := range b1 { + b2[i] = b1[i] +} + +// good +copy(b2, b1) +``` + +- slice新增。 + +```go +// bad +var a, b []int +for _, v := range a { + b = append(b, v) +} + +// good +var a, b []int +b = append(b, a...) +``` + +### 4.3 结构体 + +- struct初始化。 + +struct以多行格式初始化。 + +```go +type user struct { + Id int64 + Name string +} + +u1 := user{100, "Colin"} + +u2 := user{ + Id: 200, + Name: "Lex", +} +``` + +## 5. 控制结构 + +### 5.1 if + +- if 接受初始化语句,约定如下方式建立局部变量。 + +```go +if err := loadConfig(); err != nil { + // error handling + return err +} +``` + +- if 对于bool类型的变量,应直接进行真假判断。 + +```go +var isAllow bool +if isAllow { + // normal code +} +``` + +### 5.2 for + +- 采用短声明建立局部变量。 + +```go +sum := 0 +for i := 0; i < 10; i++ { + sum += 1 +} +``` + +- 不要在 for 循环里面使用 defer,defer只有在函数退出时才会执行。 + +```go +// bad +for file := range files { + fd, err := os.Open(file) + if err != nil { + return err + } + defer fd.Close() + // normal code +} + +// good +for file := range files { + func() { + fd, err := os.Open(file) + if err != nil { + return err + } + defer fd.Close() + // normal code + }() +} +``` + +### 5.3 range + +- 如果只需要第一项(key),就丢弃第二个。 + +```go +for key := range keys { +// normal code +} +``` + +- 如果只需要第二项,则把第一项置为下划线。 + +```go +sum := 0 +for _, value := range array { + sum += value +} +``` + +### 5.4 switch + +- 必须要有default。 + +```go +switch os := runtime.GOOS; os { + case "linux": + fmt.Println("Linux.") + case "darwin": + fmt.Println("OS X.") + default: + fmt.Printf("%s.\n", os) +} +``` + +### 5.5 goto +- 业务代码禁止使用 goto 。 +- 框架或其他底层源码尽量不用。 + +## 6. 函数 + +- 传入变量和返回变量以小写字母开头。 +- 函数参数个数不能超过5个。 +- 函数分组与顺序 +- 函数应按粗略的调用顺序排序。 +- 同一文件中的函数应按接收者分组。 +- 尽量采用值传递,而非指针传递。 +- 传入参数是 map、slice、chan、interface ,不要传递指针。 + +### 6.1 函数参数 + +- 如果函数返回相同类型的两个或三个参数,或者如果从上下文中不清楚结果的含义,使用命名返回,其他情况不建议使用命名返回,例如: + +```go +func coordinate() (x, y float64, err error) { + // normal code +} +``` +- 传入变量和返回变量都以小写字母开头。 +- 尽量用值传递,非指针传递。 +- 参数数量均不能超过5个。 +- 多返回值最多返回三个,超过三个请使用 struct。 + +### 6.2 defer + +- 当存在资源创建时,应紧跟defer释放资源(可以大胆使用defer,defer在Go1.14版本中,性能大幅提升,defer的性能损耗即使在性能敏感型的业务中,也可以忽略)。 +- 先判断是否错误,再defer释放资源,例如: + +```go +rep, err := http.Get(url) +if err != nil { + return err +} + +defer resp.Body.Close() +``` + +### 6.3 方法的接收器 + +- 推荐以类名第一个英文首字母的小写作为接收器的命名。 +- 接收器的命名在函数超过20行的时候不要用单字符。 +- 接收器的命名不能采用me、this、self这类易混淆名称。 + +### 6.4 嵌套 +- 嵌套深度不能超过4层。 + +### 6.5 变量命名 +- 变量声明尽量放在变量第一次使用的前面,遵循就近原则。 +- 如果魔法数字出现超过两次,则禁止使用,改用一个常量代替,例如: + +```go +// PI ... +const Prise = 3.14 + +func getAppleCost(n float64) float64 { + return Prise * n +} + +func getOrangeCost(n float64) float64 { + return Prise * n +} +``` + +## 7. GOPATH 设置规范 +- Go 1.11 之后,弱化了 GOPATH 规则,已有代码(很多库肯定是在1.11之前建立的)肯定符合这个规则,建议保留 GOPATH 规则,便于维护代码。 +- 建议只使用一个 GOPATH,不建议使用多个 GOPATH。如果使用多个GOPATH,编译生效的 bin 目录是在第一个 GOPATH 下。 + +## 8. 依赖管理 + +- Go 1.11 以上必须使用 Go Modules。 +- 使用Go Modules作为依赖管理的项目时,不建议提交vendor目录。 +- 使用Go Modules作为依赖管理的项目时,必须提交go.sum文件。 + +### 9. 最佳实践 + +- 尽量少用全局变量,而是通过参数传递,使每个函数都是“无状态”的。这样可以减少耦合,也方便分工和单元测试。 +- 在编译时验证接口的符合性,例如: + +```go +type LogHandler struct { + h http.Handler + log *zap.Logger +} +var _ http.Handler = LogHandler{} +``` +- 服务器处理请求时,应该创建一个context,保存该请求的相关信息(如requestID),并在函数调用链中传递。 + +### 9.1 性能 +- string 表示的是不可变的字符串变量,对 string 的修改是比较重的操作,基本上都需要重新申请内存。所以,如果没有特殊需要,需要修改时多使用 []byte。 +- 优先使用 strconv 而不是 fmt。 + +### 9.2 注意事项 + +- append 要小心自动分配内存,append 返回的可能是新分配的地址。 +- 如果要直接修改 map 的 value 值,则 value 只能是指针,否则要覆盖原来的值。 +- map 在并发中需要加锁。 +- 编译过程无法检查 interface{} 的转换,只能在运行时检查,小心引起 panic。 + +## 总结 + +这里向你介绍了九类常用的编码规范。但今天的最后,我要在这里提醒你一句:规范是人定的,你也可以根据需要,制定符合你项目的规范,但同时我也建议你采纳这些业界沉淀下来的规范,并通过工具来确保规范的执行。 diff --git a/docs/conversions/logging.md b/docs/conversions/logging.md new file mode 100644 index 000000000..82f74c9aa --- /dev/null +++ b/docs/conversions/logging.md @@ -0,0 +1,20 @@ +## Log Standards + +### Log Standards + +- The unified log package `github.com/OpenIMSDK/Open-IM-Server/internal/pkg/log` should be used for all logging; +- Use structured logging formats: `log.Infow`, `log.Warnw`, `log.Errorw`, etc. For example: `log.Infow("Update post function called")`; +- All logs should start with an uppercase letter and should not end with a `.`. For example: `log.Infow("Update post function called")`; +- Use past tense. For example, use `Could not delete B` instead of `Cannot delete B`; +- Adhere to log level standards: + - Debug level logs use `log.Debugw`; + - Info level logs use `log.Infow`; + - Warning level logs use `log.Warnw`; + - Error level logs use `log.Errorw`; + - Panic level logs use `log.Panicw`; + - Fatal level logs use `log.Fatalw`. +- Log settings: + - Development and test environments: The log level is set to `debug`, the log format can be set to `console` / `json` as needed, and caller is enabled; + - Production environment: The log level is set to `info`, the log format is set to `json`, and caller is enabled. (**Note**: In the early stages of going online, to facilitate troubleshooting, the log level can be set to `debug`) +- When logging, avoid outputting sensitive information, such as passwords, keys, etc. +- If you are calling a logging function in a function/method with a `context.Context` parameter, it is recommended to use `log.L(ctx).Infow()` for logging. \ No newline at end of file diff --git a/docs/conversions/version.md b/docs/conversions/version.md new file mode 100644 index 000000000..a421dd549 --- /dev/null +++ b/docs/conversions/version.md @@ -0,0 +1,57 @@ +# OpenIM Branch Management and Versioning + +Our project, OpenIM, follows the [Semantic Versioning 2.0.0](https://semver.org/lang/zh-CN/) standards. + +## OpenIM version + +OpenIM manages two primary branches: `main` and `release`. The project uses Semantic Versioning 2.0.0 to tag different versions of the software, each indicating a significant milestone in the software's development. + +In the OpenIM repository, the versioning adheres to the `MAJOR.MINOR.PATCH` format, where: + +- `MAJOR` version changes when there are incompatible changes to the API, +- `MINOR` version changes when features are added in a backward-compatible manner, and +- `PATCH` version changes when backward-compatible bugs are fixed. + +## Milestones and Branching + +When a significant milestone like v3.1.0 is achieved, a new branch `release-v3.1` is created. This branch contains all the code pertaining to this stable release. All bug fixes and features intended for the next version, v3.2.0, are merged into this branch. + +The release of `PATCH` versions (Z in `X.Y.Z`) are driven by bug fixes, and these can be rolled out depending on the bug's priority or over a scheduled time. On the other hand, `MINOR` versions (Y in `X.Y.Z`) are released based on the project's roadmap, milestone completion, or on a scheduled timeline. Importantly, the API of minor versions is always backward-compatible. + +## Dealing with Major Bugs + +In the event of a major bug discovery, the fix would selectively be merged into the previous version (e.g., v3.1 or the `release-v3.1` branch), as well as into the `main` branch. This is to ensure that users relying on the older version can still receive important bug fixes, while also keeping the main branch updated. + +It's worth noting that a robust testing regime should be in place to ensure the integrity of all branches at any given time. Automated tests and code review sessions are crucial components of maintaining a healthy codebase. + +To summarize, OpenIM's approach to branch management and versioning ensures a balance between introducing new features, fixing bugs, and maintaining backward compatibility. This strategy is vital for managing user expectations, supporting older versions, and paving the way for the project's continuous growth. + +## Git Workflow Example + +To put the above principles into practice, here's a Git workflow example that you might follow when working on a bug fix: + +``` +bashCopy code# Checkout the branch for the version that needs the bug fix +git checkout release-v3.1 + +# Create a new branch for the bug fix +git checkout -b bug/bug-name + +# ... Make changes, commit your work ... + +# Push the branch to your remote repository +git push origin bug/bug-name + +# After the pull request is merged into the release-v3.1 branch, +# checkout and update your main branch +git checkout main +git pull origin main + +# Merge or rebase the changes from release-v3.1 into main +git merge release-v3.1 + +# Push the updates to the main branch +git push origin main +``` + +Remember, communication with your team is key throughout this process, keeping everyone up-to-date with the changes being made. \ No newline at end of file diff --git a/scripts/make-rules/common.mk b/scripts/make-rules/common.mk index d471ec112..93057fef5 100644 --- a/scripts/make-rules/common.mk +++ b/scripts/make-rules/common.mk @@ -60,6 +60,7 @@ endif ifeq ($(origin VERSION), undefined) # VERSION := $(shell git describe --abbrev=0 --dirty --always --tags | sed 's/-/./g') #v2.3.3.dirty +# git describe --tags --always --match="v*" --dirty VERSION := $(shell git describe --tags --always --match="v*" --dirty | sed 's/-/./g') #v2.3.3.631.g00abdc9b.dirty # v2.3.3: git tag endif @@ -110,13 +111,16 @@ else IMAGE_PLAT := $(PLATFORM) endif +# Protobuf file storage path +APIROOT=$(ROOT_DIR)/pkg/proto + # Linux command settings # TODO: Whether you need to join utils? -FIND := find . ! -path './utils/*' ! -path './vendor/*' -XARGS := xargs -r +FIND := find . ! -path './utils/*' ! -path './vendor/*' ! -path './third_party/*' +XARGS := xargs -r --no-run-if-empty # Linux command settings-CODE DIRS Copyright -CODE_DIRS := $(ROOT_DIR)/pkg $(ROOT_DIR)/cmd $(ROOT_DIR)/config $(ROOT_DIR)/db $(ROOT_DIR)/.docker-compose_cfg $(ROOT_DIR)/internal $(ROOT_DIR)/scripts $(ROOT_DIR)/test +CODE_DIRS := $(ROOT_DIR)/pkg $(ROOT_DIR)/cmd $(ROOT_DIR)/config $(ROOT_DIR)/db $(ROOT_DIR)/.docker-compose_cfg $(ROOT_DIR)/internal $(ROOT_DIR)/scripts $(ROOT_DIR)/test $(ROOT_DIR)/.github $(ROOT_DIR)/build $(ROOT_DIR)/build $(ROOT_DIR)/deployments FINDS := find $(CODE_DIRS) # Makefile settings: Select different behaviors by determining whether V option is set diff --git a/scripts/make-rules/golang.mk b/scripts/make-rules/golang.mk index cf07f0cda..282649d7e 100644 --- a/scripts/make-rules/golang.mk +++ b/scripts/make-rules/golang.mk @@ -173,9 +173,9 @@ go.test.cover: go.test.junit-report .PHONY: go.format go.format: tools.verify.golines tools.verify.goimports @echo "===========> Formating codes" - @$(FIND) -type f -name '*.go' | $(XARGS) gofmt -s -w - @$(FIND) -type f -name '*.go' | $(XARGS) $(TOOLS_DIR)/goimports -w -local $(ROOT_PACKAGE) - @$(FIND) -type f -name '*.go' | $(XARGS) $(TOOLS_DIR)/golines -w --max-len=200 --reformat-tags --shorten-comments --ignore-generated . + @$(FIND) -type f -name '*.go' -not -name '*pb*' | $(XARGS) gofmt -s -w + @$(FIND) -type f -name '*.go' -not -name '*pb*' | $(XARGS) $(TOOLS_DIR)/goimports -w -local $(ROOT_PACKAGE) + @$(FIND) -type f -name '*.go' -not -name '*pb*' | $(XARGS) $(TOOLS_DIR)/golines -w --max-len=200 --reformat-tags --shorten-comments --ignore-generated . @$(GO) mod edit -fmt ## imports: task to automatically handle import packages in Go files using goimports tool From 3dc19585253fb5095e1550468b9f5072572a4402 Mon Sep 17 00:00:00 2001 From: Xinwei Xiong <3293172751@qq.com> Date: Wed, 26 Jul 2023 17:28:35 +0800 Subject: [PATCH 02/11] Update README.md (#669) --- README.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 11471ac0f..37aedb0ee 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ A+ good first - +

@@ -69,7 +69,8 @@ DATA_DIR=./ #designate large disk directory 1. Deploy and start -> **Note**: This command can only be executed once. It will modify the component passwords in docker-compose based on the PASSWORD variable in .env, and modify the component passwords in config/config.yaml. If the password in .env changes, you need to first execute docker-compose down; rm components -rf and then execute this command. +> **Note** +> This command can only be executed once. It will modify the component passwords in docker-compose based on the PASSWORD variable in .env, and modify the component passwords in config/config.yaml. If the password in .env changes, you need to first execute docker-compose down; rm components -rf and then execute this command. ``` chmod +x install_im_server.sh; @@ -94,18 +95,19 @@ cd scripts; 2. Clone ``` - git clone https://github.com/OpenIMSDK/Open-IM-Server - cd Open-IM-Server - git checkout release-v3.0 #or other release branch + git clone https://github.com/OpenIMSDK/Open-IM-Server openim && export openim=$(pwd)/openim && cd $openim && make + git checkout release-v3.0 # or other tag ``` +Read about the [OpenIM Version Policy](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/version.md) + 3. Compile ``` - cd Open-IM-server/scripts - chmod +x *.sh - ./build_all_service.sh + cd $openim + make build ``` +`make help` to help you see the instructions supported by OpenIM. All services have been successfully built as shown in the figure From 28732a29cd6d2f27bd162cc838fcc78917dcd7a5 Mon Sep 17 00:00:00 2001 From: Xinwei Xiong <3293172751@qq.com> Date: Wed, 26 Jul 2023 17:43:18 +0800 Subject: [PATCH 03/11] Update CODEOWNERS (#670) --- docs/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/CODEOWNERS b/docs/CODEOWNERS index a05a1866b..0d9b017f8 100644 --- a/docs/CODEOWNERS +++ b/docs/CODEOWNERS @@ -65,7 +65,7 @@ cmd/* config/* @skiffer-git # db directory -db/sdk @BanTanger @cubxxw @Gordon +db/sdk @cubxxw @FGadvancer # internal directory internal/ @openimsdk/openim @skiffer-git @FGadvancer From d7218dd5be348e286b2e7188c179bd2952252274 Mon Sep 17 00:00:00 2001 From: Xinwei Xiong <3293172751@qq.com> Date: Wed, 26 Jul 2023 18:50:50 +0800 Subject: [PATCH 04/11] docs: update README.md install (#673) --- README.md | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 37aedb0ee..3c52475f3 100644 --- a/README.md +++ b/README.md @@ -51,15 +51,15 @@ Open-IM-Server is not a standalone product and does not include account registra 1. Clone the project -``` -clone https://github.com/OpenIMSDK/Open-IM-Server -cd Open-IM-Server -git checkout release-v3.0 #or other release branch +```bash +# choose what you need +BRANCH=release-v3.0 +git clone -b $BRANCH https://github.com/OpenIMSDK/Open-IM-Server openim && export openim=$(pwd)/openim && cd $openim && make build ``` -1. Modify .env +2. Modify .env -``` +```bash USER=root #no need to modify PASSWORD=openIM123 #A combination of 8 or more numbers and letters, this password applies to redis, mysql, mongo, as well as accessSecret in config/config.yaml ENDPOINT=http://127.0.0.1:10005 #minio's external service IP and port, or use the domain name storage.xx.xx, the app must be able to access this IP and port or domain, @@ -67,21 +67,19 @@ API_URL=http://127.0.0.1:10002/object/ #the app must be able to access this IP a DATA_DIR=./ #designate large disk directory ``` -1. Deploy and start +3. Deploy and start > **Note** > This command can only be executed once. It will modify the component passwords in docker-compose based on the PASSWORD variable in .env, and modify the component passwords in config/config.yaml. If the password in .env changes, you need to first execute docker-compose down; rm components -rf and then execute this command. ``` -chmod +x install_im_server.sh; -./install_im_server.sh; +make install ``` -1. Check the service +4. Check the service -``` -cd scripts; -./docker_check_service.sh +```bash +make check ``` ![https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/images/docker_build.png](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/images/docker_build.png) @@ -90,23 +88,14 @@ cd scripts; ### Compile from source -1. Go 1.18 or higher version. - -2. Clone - - ``` - git clone https://github.com/OpenIMSDK/Open-IM-Server openim && export openim=$(pwd)/openim && cd $openim && make - git checkout release-v3.0 # or other tag - ``` +Ur need `Go 1.18` or higher version, and `make`. +```bash +# choose what you need +BRANCH=release-v3.0 +git clone -b $BRANCH https://github.com/OpenIMSDK/Open-IM-Server openim && export openim=$(pwd)/openim && cd $openim && make build +``` Read about the [OpenIM Version Policy](https://github.com/OpenIMSDK/Open-IM-Server/blob/main/docs/conversions/version.md) - -3. Compile - - ``` - cd $openim - make build - ``` `make help` to help you see the instructions supported by OpenIM. All services have been successfully built as shown in the figure From 5b85fa763a5c314d80a9d0982be330d6af395699 Mon Sep 17 00:00:00 2001 From: Gordon <46924906+FGadvancer@users.noreply.github.com> Date: Wed, 26 Jul 2023 19:45:23 +0800 Subject: [PATCH 05/11] Feat: Refactor project's references to shared modules (#672) * fix: to start im or chat, ZooKeeper must be started first. * fix: msg gateway start output err info Signed-off-by: Gordon <1432970085@qq.com> * fix: msg gateway start output err info Signed-off-by: Gordon <1432970085@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * fix: go mod update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * chore: package path changes Signed-off-by: withchao <993506633@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> * fix: token update Signed-off-by: Gordon <1432970085@qq.com> --------- Signed-off-by: Gordon <1432970085@qq.com> Signed-off-by: withchao <993506633@qq.com> Co-authored-by: withchao <993506633@qq.com> --- cmd/openim-api/main.go | 4 +- cmd/openim-push/main.go | 2 +- cmd/openim-rpc/openim-rpc-auth/main.go | 2 +- .../openim-rpc-conversation/main.go | 2 +- cmd/openim-rpc/openim-rpc-friend/main.go | 2 +- cmd/openim-rpc/openim-rpc-group/main.go | 2 +- cmd/openim-rpc/openim-rpc-msg/main.go | 2 +- cmd/openim-rpc/openim-rpc-third/main.go | 2 +- cmd/openim-rpc/openim-rpc-user/main.go | 2 +- docker-compose.yaml | 1 + go.mod | 6 +- go.sum | 8 +- internal/api/custom_validator.go | 2 +- internal/api/msg.go | 8 +- internal/api/route.go | 7 +- internal/api/user.go | 4 +- internal/msggateway/callback.go | 4 +- internal/msggateway/client.go | 7 +- internal/msggateway/context.go | 2 +- internal/msggateway/hub_server.go | 11 +- internal/msggateway/init.go | 12 +- internal/msggateway/n_ws_server.go | 8 +- internal/msgtransfer/init.go | 2 +- .../msgtransfer/online_history_msg_handler.go | 31 +- .../online_msg_to_mongo_handler.go | 2 +- .../msgtransfer/persistent_msg_handler.go | 4 +- internal/push/callback.go | 4 +- internal/push/offlinepush/fcm/push.go | 4 +- internal/push/offlinepush/getui/body.go | 2 +- internal/push/offlinepush/getui/push.go | 2 +- .../offlinepush/jpush/body/notification.go | 2 +- .../push/offlinepush/jpush/body/platform.go | 2 +- internal/push/offlinepush/jpush/push.go | 2 +- internal/push/push_handler.go | 4 +- internal/push/push_rpc_server.go | 2 +- internal/push/push_to_client.go | 9 +- internal/push/tools.go | 2 +- internal/rpc/auth/auth.go | 9 +- internal/rpc/conversation/conversaion.go | 5 +- internal/rpc/friend/black.go | 4 +- internal/rpc/friend/callback.go | 4 +- internal/rpc/friend/friend.go | 10 +- internal/rpc/group/callback.go | 4 +- internal/rpc/group/group.go | 35 +- internal/rpc/group/super_group.go | 2 +- internal/rpc/msg/as_read.go | 2 +- internal/rpc/msg/callback.go | 4 +- internal/rpc/msg/delete.go | 12 +- internal/rpc/msg/message_interceptor.go | 4 +- internal/rpc/msg/msg_status.go | 2 +- internal/rpc/msg/revoke.go | 10 +- internal/rpc/msg/send.go | 9 +- internal/rpc/msg/server.go | 2 +- internal/rpc/msg/sync_msg.go | 9 +- internal/rpc/msg/utils.go | 4 +- internal/rpc/msg/verify.go | 4 +- internal/rpc/third/third.go | 2 +- internal/rpc/third/tool.go | 4 +- internal/rpc/user/user.go | 15 +- internal/tools/cron_task.go | 2 +- internal/tools/msg.go | 2 +- internal/tools/msg_test.go | 355 ------------------ pkg/authverify/token.go | 62 +++ pkg/common/cmd/msg_gateway.go | 2 +- pkg/common/cmd/root.go | 6 +- pkg/common/cmd/rpc.go | 2 +- pkg/common/config/config.go | 333 ++++++++++++++++ pkg/common/config/parse.go | 27 +- pkg/common/db/cache/init_redis.go | 2 +- pkg/common/db/cache/msg.go | 11 +- pkg/common/db/controller/auth.go | 5 +- pkg/common/db/controller/conversation.go | 5 +- pkg/common/db/controller/friend.go | 2 +- pkg/common/db/controller/group.go | 2 +- pkg/common/db/controller/msg.go | 2 +- pkg/common/db/controller/msg_test.go | 4 +- pkg/common/db/relation/chat_log_model.go | 2 +- pkg/common/db/relation/conversation_model.go | 2 +- pkg/common/db/relation/group_member_model.go | 2 +- pkg/common/db/relation/group_model.go | 2 +- pkg/common/db/relation/mysql_init.go | 2 +- pkg/common/db/s3/cos/cos.go | 2 +- pkg/common/db/s3/minio/minio.go | 2 +- pkg/common/db/s3/oss/oss.go | 2 +- pkg/common/db/unrelation/mongo.go | 2 +- pkg/common/db/unrelation/msg.go | 2 +- pkg/common/http/http_client.go | 4 +- pkg/common/kafka/consumer.go | 2 +- pkg/common/kafka/producer.go | 4 +- pkg/common/prome/prometheus.go | 2 +- pkg/common/startrpc/start.go | 112 ++++++ pkg/msgprocessor/conversation.go | 158 ++++++++ pkg/msgprocessor/options.go | 181 +++++++++ pkg/rpcclient/auth.go | 2 +- pkg/rpcclient/conversation.go | 2 +- pkg/rpcclient/friend.go | 2 +- pkg/rpcclient/group.go | 4 +- pkg/rpcclient/msg.go | 4 +- pkg/rpcclient/notification/conevrsation.go | 2 +- pkg/rpcclient/notification/friend.go | 2 +- pkg/rpcclient/notification/group.go | 2 +- pkg/rpcclient/notification/msg.go | 2 +- pkg/rpcclient/push.go | 2 +- pkg/rpcclient/third.go | 2 +- pkg/rpcclient/user.go | 6 +- 105 files changed, 1088 insertions(+), 581 deletions(-) delete mode 100644 internal/tools/msg_test.go create mode 100644 pkg/authverify/token.go create mode 100644 pkg/common/config/config.go create mode 100644 pkg/common/startrpc/start.go create mode 100644 pkg/msgprocessor/conversation.go create mode 100644 pkg/msgprocessor/options.go diff --git a/cmd/openim-api/main.go b/cmd/openim-api/main.go index c967906e9..8350f4d05 100644 --- a/cmd/openim-api/main.go +++ b/cmd/openim-api/main.go @@ -28,9 +28,9 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/internal/api" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/cmd" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/discoveryregistry" openKeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" "github.com/OpenIMSDK/tools/log" diff --git a/cmd/openim-push/main.go b/cmd/openim-push/main.go index adc3a7cb7..03db8ae03 100644 --- a/cmd/openim-push/main.go +++ b/cmd/openim-push/main.go @@ -17,7 +17,7 @@ package main import ( "github.com/OpenIMSDK/Open-IM-Server/internal/push" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/cmd" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" ) func main() { diff --git a/cmd/openim-rpc/openim-rpc-auth/main.go b/cmd/openim-rpc/openim-rpc-auth/main.go index 62bb65fab..524804988 100644 --- a/cmd/openim-rpc/openim-rpc-auth/main.go +++ b/cmd/openim-rpc/openim-rpc-auth/main.go @@ -17,7 +17,7 @@ package main import ( "github.com/OpenIMSDK/Open-IM-Server/internal/rpc/auth" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/cmd" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" ) func main() { diff --git a/cmd/openim-rpc/openim-rpc-conversation/main.go b/cmd/openim-rpc/openim-rpc-conversation/main.go index 0465de868..fec8226f8 100644 --- a/cmd/openim-rpc/openim-rpc-conversation/main.go +++ b/cmd/openim-rpc/openim-rpc-conversation/main.go @@ -17,7 +17,7 @@ package main import ( "github.com/OpenIMSDK/Open-IM-Server/internal/rpc/conversation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/cmd" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" ) func main() { diff --git a/cmd/openim-rpc/openim-rpc-friend/main.go b/cmd/openim-rpc/openim-rpc-friend/main.go index 7a0631a20..fbd44038e 100644 --- a/cmd/openim-rpc/openim-rpc-friend/main.go +++ b/cmd/openim-rpc/openim-rpc-friend/main.go @@ -17,7 +17,7 @@ package main import ( "github.com/OpenIMSDK/Open-IM-Server/internal/rpc/friend" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/cmd" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" ) func main() { diff --git a/cmd/openim-rpc/openim-rpc-group/main.go b/cmd/openim-rpc/openim-rpc-group/main.go index af78868d2..06baac155 100644 --- a/cmd/openim-rpc/openim-rpc-group/main.go +++ b/cmd/openim-rpc/openim-rpc-group/main.go @@ -17,7 +17,7 @@ package main import ( "github.com/OpenIMSDK/Open-IM-Server/internal/rpc/group" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/cmd" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" ) func main() { diff --git a/cmd/openim-rpc/openim-rpc-msg/main.go b/cmd/openim-rpc/openim-rpc-msg/main.go index 6bdd4842d..356081d33 100644 --- a/cmd/openim-rpc/openim-rpc-msg/main.go +++ b/cmd/openim-rpc/openim-rpc-msg/main.go @@ -17,7 +17,7 @@ package main import ( "github.com/OpenIMSDK/Open-IM-Server/internal/rpc/msg" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/cmd" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" ) func main() { diff --git a/cmd/openim-rpc/openim-rpc-third/main.go b/cmd/openim-rpc/openim-rpc-third/main.go index 2136bc157..c070e6811 100644 --- a/cmd/openim-rpc/openim-rpc-third/main.go +++ b/cmd/openim-rpc/openim-rpc-third/main.go @@ -17,7 +17,7 @@ package main import ( "github.com/OpenIMSDK/Open-IM-Server/internal/rpc/third" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/cmd" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" ) func main() { diff --git a/cmd/openim-rpc/openim-rpc-user/main.go b/cmd/openim-rpc/openim-rpc-user/main.go index 16a24c812..6d6d9008d 100644 --- a/cmd/openim-rpc/openim-rpc-user/main.go +++ b/cmd/openim-rpc/openim-rpc-user/main.go @@ -17,7 +17,7 @@ package main import ( "github.com/OpenIMSDK/Open-IM-Server/internal/rpc/user" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/cmd" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" ) func main() { diff --git a/docker-compose.yaml b/docker-compose.yaml index 23565c23a..cd80dbf19 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -110,6 +110,7 @@ services: - ./scripts:/Open-IM-Server/scripts restart: always depends_on: + - zookeeper - kafka - mysql - mongodb diff --git a/go.mod b/go.mod index db50056c4..7acda0e12 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/golang-jwt/jwt/v4 v4.5.0 github.com/golang/protobuf v1.5.3 github.com/gorilla/websocket v1.4.2 - github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect + github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 github.com/jinzhu/copier v0.3.5 github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect github.com/minio/minio-go/v7 v7.0.59 @@ -37,8 +37,8 @@ require ( require github.com/google/uuid v1.3.0 require ( - github.com/OpenIMSDK/protocol v0.0.0-20230725064305-11f0568fe5e9 - github.com/OpenIMSDK/tools v0.0.1 + github.com/OpenIMSDK/protocol v0.0.1 + github.com/OpenIMSDK/tools v0.0.4 github.com/aliyun/aliyun-oss-go-sdk v2.2.7+incompatible github.com/go-redis/redis v6.15.9+incompatible github.com/go-sql-driver/mysql v1.6.0 diff --git a/go.sum b/go.sum index 0c585411e..28b2c5c32 100644 --- a/go.sum +++ b/go.sum @@ -16,10 +16,10 @@ cloud.google.com/go/storage v1.28.1/go.mod h1:Qnisd4CqDdo6BGs2AD5LLnEsmSQ80wQ5og firebase.google.com/go v3.13.0+incompatible h1:3TdYC3DDi6aHn20qoRkxwGqNgdjtblwVAyRLQwGn/+4= firebase.google.com/go v3.13.0+incompatible/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIwjt8toICdV5Wh9ptHs= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/OpenIMSDK/protocol v0.0.0-20230725064305-11f0568fe5e9 h1:ZtW+z2j2kjk51awH9OFRXbgNI5Vcfq0Lu9//ax86ktc= -github.com/OpenIMSDK/protocol v0.0.0-20230725064305-11f0568fe5e9/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y= -github.com/OpenIMSDK/tools v0.0.1 h1:lxTzjv0vOCH890K9C5LxMq3UCvioDMw0ZZQQtioauuI= -github.com/OpenIMSDK/tools v0.0.1/go.mod h1:/iSkny1+7i4Z09yddGa4o4fTu9dzJdOLxxe4pWuqI1A= +github.com/OpenIMSDK/protocol v0.0.1 h1:Q6J1jCU00dfqmguxw2XI+IGcVfBAkb5Tz8LgvyeNkk0= +github.com/OpenIMSDK/protocol v0.0.1/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y= +github.com/OpenIMSDK/tools v0.0.4 h1:aSC5FWO/0ccxuLHs+E1Eeyj+XWL/2CzLy1XGmQgpaU4= +github.com/OpenIMSDK/tools v0.0.4/go.mod h1:eg+q4A34Qmu73xkY0mt37FHGMCMfC6CtmOnm0kFEGFI= github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mod h1:1pk82RBxDY/JZnPQrtqHlUFfCctgdorsd9M06fMynOM= github.com/Shopify/sarama v1.29.0 h1:ARid8o8oieau9XrHI55f/L3EoRAhm9px6sonbD7yuUE= github.com/Shopify/sarama v1.29.0/go.mod h1:2QpgD79wpdAESqNQMxNc0KYMkycd4slxGdV3TWSVqrU= diff --git a/internal/api/custom_validator.go b/internal/api/custom_validator.go index 7425ecb38..8c5890501 100644 --- a/internal/api/custom_validator.go +++ b/internal/api/custom_validator.go @@ -17,7 +17,7 @@ package api import ( "github.com/go-playground/validator/v10" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" ) func RequiredIf(fl validator.FieldLevel) bool { diff --git a/internal/api/msg.go b/internal/api/msg.go index d79be716b..8480ac7d3 100644 --- a/internal/api/msg.go +++ b/internal/api/msg.go @@ -15,6 +15,7 @@ package api import ( + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "github.com/mitchellh/mapstructure" @@ -22,14 +23,13 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/apistruct" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/msg" "github.com/OpenIMSDK/protocol/sdkws" "github.com/OpenIMSDK/tools/a2r" "github.com/OpenIMSDK/tools/apiresp" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" - "github.com/OpenIMSDK/tools/tokenverify" "github.com/OpenIMSDK/tools/utils" ) @@ -205,7 +205,7 @@ func (m *MessageApi) SendMessage(c *gin.Context) { return } log.ZInfo(c, "SendMessage", "req", req) - if !tokenverify.IsAppManagerUid(c) { + if !authverify.IsAppManagerUid(c) { apiresp.GinError(c, errs.ErrNoPermission.Wrap("only app manager can send message")) return } @@ -245,7 +245,7 @@ func (m *MessageApi) BatchSendMsg(c *gin.Context) { return } log.ZInfo(c, "BatchSendMsg", "req", req) - if err := tokenverify.CheckAdmin(c); err != nil { + if err := authverify.CheckAdmin(c); err != nil { apiresp.GinError(c, errs.ErrNoPermission.Wrap("only app manager can send message")) return } diff --git a/internal/api/route.go b/internal/api/route.go index 453224263..262283019 100644 --- a/internal/api/route.go +++ b/internal/api/route.go @@ -16,10 +16,11 @@ package api import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/apiresp" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/tokenverify" "net/http" @@ -31,9 +32,9 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" - "github.com/OpenIMSDK/tools/config" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mw" @@ -210,7 +211,7 @@ func GinParseToken(rdb redis.UniversalClient) gin.HandlerFunc { c.Abort() return } - claims, err := tokenverify.GetClaimFromToken(token) + claims, err := tokenverify.GetClaimFromToken(token, authverify.Secret()) if err != nil { log.ZWarn(c, "jwt get token error", errs.ErrTokenUnknown.Wrap()) apiresp.GinError(c, errs.ErrTokenUnknown.Wrap()) diff --git a/internal/api/user.go b/internal/api/user.go index c7bcf7862..8595b3501 100644 --- a/internal/api/user.go +++ b/internal/api/user.go @@ -17,13 +17,13 @@ package api import ( "github.com/gin-gonic/gin" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/msggateway" "github.com/OpenIMSDK/protocol/user" "github.com/OpenIMSDK/tools/a2r" "github.com/OpenIMSDK/tools/apiresp" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" ) diff --git a/internal/msggateway/callback.go b/internal/msggateway/callback.go index 530c18102..5ede3c854 100644 --- a/internal/msggateway/callback.go +++ b/internal/msggateway/callback.go @@ -19,9 +19,9 @@ import ( "time" cbapi "github.com/OpenIMSDK/Open-IM-Server/pkg/callbackstruct" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/http" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/mcontext" ) diff --git a/internal/msggateway/client.go b/internal/msggateway/client.go index bc5fa1511..096fd1463 100644 --- a/internal/msggateway/client.go +++ b/internal/msggateway/client.go @@ -18,14 +18,15 @@ import ( "context" "errors" "fmt" + "github.com/OpenIMSDK/Open-IM-Server/pkg/msgprocessor" "runtime/debug" "sync" "google.golang.org/protobuf/proto" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/sdkws" "github.com/OpenIMSDK/tools/apiresp" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mcontext" "github.com/OpenIMSDK/tools/utils" @@ -241,9 +242,9 @@ func (c *Client) replyMessage(ctx context.Context, binaryReq *Req, err error, re func (c *Client) PushMessage(ctx context.Context, msgData *sdkws.MsgData) error { var msg sdkws.PushMessages - conversationID := utils.GetConversationIDByMsg(msgData) + conversationID := msgprocessor.GetConversationIDByMsg(msgData) m := map[string]*sdkws.PullMsgs{conversationID: {Msgs: []*sdkws.MsgData{msgData}}} - if utils.IsNotification(conversationID) { + if msgprocessor.IsNotification(conversationID) { msg.NotificationMsgs = m } else { msg.Msgs = m diff --git a/internal/msggateway/context.go b/internal/msggateway/context.go index 672ba1dbe..601c28a34 100644 --- a/internal/msggateway/context.go +++ b/internal/msggateway/context.go @@ -19,7 +19,7 @@ import ( "strconv" "time" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/utils" ) diff --git a/internal/msggateway/hub_server.go b/internal/msggateway/hub_server.go index dc440a7c8..e9a53cbf7 100644 --- a/internal/msggateway/hub_server.go +++ b/internal/msggateway/hub_server.go @@ -16,20 +16,19 @@ package msggateway import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" "github.com/OpenIMSDK/tools/errs" - "github.com/OpenIMSDK/tools/tokenverify" - "google.golang.org/grpc" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/startrpc" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/msggateway" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/log" - "github.com/OpenIMSDK/tools/startrpc" "github.com/OpenIMSDK/tools/utils" ) @@ -84,7 +83,7 @@ func (s *Server) GetUsersOnlineStatus( ctx context.Context, req *msggateway.GetUsersOnlineStatusReq, ) (*msggateway.GetUsersOnlineStatusResp, error) { - if !tokenverify.IsAppManagerUid(ctx) { + if !authverify.IsAppManagerUid(ctx) { return nil, errs.ErrNoPermission.Wrap("only app manager") } var resp msggateway.GetUsersOnlineStatusResp diff --git a/internal/msggateway/init.go b/internal/msggateway/init.go index b6d4d3b58..03fe9f581 100644 --- a/internal/msggateway/init.go +++ b/internal/msggateway/init.go @@ -18,7 +18,7 @@ import ( "fmt" "time" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" ) func RunWsAndServer(rpcPort, wsPort, prometheusPort int) error { @@ -39,7 +39,11 @@ func RunWsAndServer(rpcPort, wsPort, prometheusPort int) error { return err } hubServer := NewServer(rpcPort, longServer) - go hubServer.Start() - hubServer.LongConnServer.Run() - return nil + go func() { + err := hubServer.Start() + if err != nil { + panic(err.Error()) + } + }() + return hubServer.LongConnServer.Run() } diff --git a/internal/msggateway/n_ws_server.go b/internal/msggateway/n_ws_server.go index 9715f21b6..c324f8a22 100644 --- a/internal/msggateway/n_ws_server.go +++ b/internal/msggateway/n_ws_server.go @@ -17,15 +17,16 @@ package msggateway import ( "context" "errors" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" "net/http" "strconv" "sync" "sync/atomic" "time" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" "github.com/redis/go-redis/v9" @@ -35,7 +36,6 @@ import ( "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" - "github.com/OpenIMSDK/tools/tokenverify" "github.com/OpenIMSDK/tools/utils" ) @@ -338,7 +338,7 @@ func (ws *WsServer) wsHandler(w http.ResponseWriter, r *http.Request) { httpError(connContext, errs.ErrConnArgsErr) return } - if err := tokenverify.WsVerifyToken(token, userID, platformID); err != nil { + if err := authverify.WsVerifyToken(token, userID, platformID); err != nil { httpError(connContext, err) return } diff --git a/internal/msgtransfer/init.go b/internal/msgtransfer/init.go index 037564f7a..d54e81240 100644 --- a/internal/msgtransfer/init.go +++ b/internal/msgtransfer/init.go @@ -22,6 +22,7 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation" @@ -29,7 +30,6 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" - "github.com/OpenIMSDK/tools/config" openKeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mw" diff --git a/internal/msgtransfer/online_history_msg_handler.go b/internal/msgtransfer/online_history_msg_handler.go index 19121334a..282f50119 100644 --- a/internal/msgtransfer/online_history_msg_handler.go +++ b/internal/msgtransfer/online_history_msg_handler.go @@ -16,6 +16,7 @@ package msgtransfer import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/msgprocessor" "strconv" "strings" "sync" @@ -27,12 +28,12 @@ import ( "github.com/go-redis/redis" "google.golang.org/protobuf/proto" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/kafka" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mcontext" "github.com/OpenIMSDK/tools/utils" @@ -141,8 +142,8 @@ func (och *OnlineHistoryRedisConsumerHandler) Run(channelID int) { "modifyMsgList", len(modifyMsgList), ) - conversationIDMsg := utils.GetChatConversationIDByMsg(ctxMsgList[0].message) - conversationIDNotification := utils.GetNotificationConversationID(ctxMsgList[0].message) + conversationIDMsg := msgprocessor.GetChatConversationIDByMsg(ctxMsgList[0].message) + conversationIDNotification := msgprocessor.GetNotificationConversationID(ctxMsgList[0].message) och.handleMsg(ctx, msgChannelValue.uniqueKey, conversationIDMsg, storageMsgList, notStorageMsgList) och.handleNotification( ctx, @@ -172,7 +173,7 @@ func (och *OnlineHistoryRedisConsumerHandler) getPushStorageMsgList( totalMsgs []*ContextMsg, ) (storageMsgList, notStorageMsgList, storageNotificatoinList, notStorageNotificationList, modifyMsgList []*sdkws.MsgData) { isStorage := func(msg *sdkws.MsgData) bool { - options2 := utils.Options(msg.Options) + options2 := msgprocessor.Options(msg.Options) if options2.IsHistory() { return true } else { @@ -183,28 +184,28 @@ func (och *OnlineHistoryRedisConsumerHandler) getPushStorageMsgList( } } for _, v := range totalMsgs { - options := utils.Options(v.message.Options) + options := msgprocessor.Options(v.message.Options) if !options.IsNotNotification() { // clone msg from notificationMsg if options.IsSendMsg() { msg := proto.Clone(v.message).(*sdkws.MsgData) // 消息 if v.message.Options != nil { - msg.Options = utils.NewMsgOptions() + msg.Options = msgprocessor.NewMsgOptions() } if options.IsOfflinePush() { - v.message.Options = utils.WithOptions( - utils.Options(v.message.Options), - utils.WithOfflinePush(false), + v.message.Options = msgprocessor.WithOptions( + v.message.Options, + msgprocessor.WithOfflinePush(false), ) - msg.Options = utils.WithOptions(utils.Options(msg.Options), utils.WithOfflinePush(true)) + msg.Options = msgprocessor.WithOptions(msg.Options, msgprocessor.WithOfflinePush(true)) } if options.IsUnreadCount() { - v.message.Options = utils.WithOptions( - utils.Options(v.message.Options), - utils.WithUnreadCount(false), + v.message.Options = msgprocessor.WithOptions( + v.message.Options, + msgprocessor.WithUnreadCount(false), ) - msg.Options = utils.WithOptions(utils.Options(msg.Options), utils.WithUnreadCount(true)) + msg.Options = msgprocessor.WithOptions(msg.Options, msgprocessor.WithUnreadCount(true)) } storageMsgList = append(storageMsgList, msg) } diff --git a/internal/msgtransfer/online_msg_to_mongo_handler.go b/internal/msgtransfer/online_msg_to_mongo_handler.go index 9ba4cb7b9..4a8b5e63e 100644 --- a/internal/msgtransfer/online_msg_to_mongo_handler.go +++ b/internal/msgtransfer/online_msg_to_mongo_handler.go @@ -20,10 +20,10 @@ import ( "github.com/Shopify/sarama" "google.golang.org/protobuf/proto" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" kfk "github.com/OpenIMSDK/Open-IM-Server/pkg/common/kafka" pbMsg "github.com/OpenIMSDK/protocol/msg" - "github.com/OpenIMSDK/tools/config" "github.com/OpenIMSDK/tools/log" ) diff --git a/internal/msgtransfer/persistent_msg_handler.go b/internal/msgtransfer/persistent_msg_handler.go index 7256a3442..b67e07cc0 100644 --- a/internal/msgtransfer/persistent_msg_handler.go +++ b/internal/msgtransfer/persistent_msg_handler.go @@ -17,11 +17,11 @@ package msgtransfer import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" kfk "github.com/OpenIMSDK/Open-IM-Server/pkg/common/kafka" + "github.com/OpenIMSDK/protocol/constant" pbMsg "github.com/OpenIMSDK/protocol/msg" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/utils" diff --git a/internal/push/callback.go b/internal/push/callback.go index bbc392a6e..d5e678301 100644 --- a/internal/push/callback.go +++ b/internal/push/callback.go @@ -18,10 +18,10 @@ import ( "context" "github.com/OpenIMSDK/Open-IM-Server/pkg/callbackstruct" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/http" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/mcontext" "github.com/OpenIMSDK/tools/utils" diff --git a/internal/push/offlinepush/fcm/push.go b/internal/push/offlinepush/fcm/push.go index 8f29f0258..cea90f865 100644 --- a/internal/push/offlinepush/fcm/push.go +++ b/internal/push/offlinepush/fcm/push.go @@ -25,9 +25,9 @@ import ( "google.golang.org/api/option" "github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" ) const SinglePushCountLimit = 400 diff --git a/internal/push/offlinepush/getui/body.go b/internal/push/offlinepush/getui/body.go index 43fcc84cc..915c6f603 100644 --- a/internal/push/offlinepush/getui/body.go +++ b/internal/push/offlinepush/getui/body.go @@ -17,7 +17,7 @@ package getui import ( "fmt" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" ) type Resp struct { diff --git a/internal/push/offlinepush/getui/push.go b/internal/push/offlinepush/getui/push.go index b440ef71b..a2f887a41 100644 --- a/internal/push/offlinepush/getui/push.go +++ b/internal/push/offlinepush/getui/push.go @@ -26,9 +26,9 @@ import ( "github.com/go-redis/redis" "github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" http2 "github.com/OpenIMSDK/Open-IM-Server/pkg/common/http" - "github.com/OpenIMSDK/tools/config" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mcontext" diff --git a/internal/push/offlinepush/jpush/body/notification.go b/internal/push/offlinepush/jpush/body/notification.go index 85581cb1f..cec725784 100644 --- a/internal/push/offlinepush/jpush/body/notification.go +++ b/internal/push/offlinepush/jpush/body/notification.go @@ -15,7 +15,7 @@ package body import ( - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" ) type Notification struct { diff --git a/internal/push/offlinepush/jpush/body/platform.go b/internal/push/offlinepush/jpush/body/platform.go index c779c8bdc..9de2b8711 100644 --- a/internal/push/offlinepush/jpush/body/platform.go +++ b/internal/push/offlinepush/jpush/body/platform.go @@ -17,7 +17,7 @@ package body import ( "errors" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" ) const ( diff --git a/internal/push/offlinepush/jpush/push.go b/internal/push/offlinepush/jpush/push.go index d661daee9..64933db50 100644 --- a/internal/push/offlinepush/jpush/push.go +++ b/internal/push/offlinepush/jpush/push.go @@ -21,8 +21,8 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush" "github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush/jpush/body" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" http2 "github.com/OpenIMSDK/Open-IM-Server/pkg/common/http" - "github.com/OpenIMSDK/tools/config" ) type JPush struct{} diff --git a/internal/push/push_handler.go b/internal/push/push_handler.go index f0a67faac..8e307c360 100644 --- a/internal/push/push_handler.go +++ b/internal/push/push_handler.go @@ -20,11 +20,11 @@ import ( "github.com/Shopify/sarama" "google.golang.org/protobuf/proto" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" kfk "github.com/OpenIMSDK/Open-IM-Server/pkg/common/kafka" + "github.com/OpenIMSDK/protocol/constant" pbChat "github.com/OpenIMSDK/protocol/msg" pbPush "github.com/OpenIMSDK/protocol/push" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/utils" ) diff --git a/internal/push/push_rpc_server.go b/internal/push/push_rpc_server.go index dfcf07469..965f890a5 100644 --- a/internal/push/push_rpc_server.go +++ b/internal/push/push_rpc_server.go @@ -24,8 +24,8 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/localcache" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" + "github.com/OpenIMSDK/protocol/constant" pbPush "github.com/OpenIMSDK/protocol/push" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/log" ) diff --git a/internal/push/push_to_client.go b/internal/push/push_to_client.go index de6b78782..b787268d0 100644 --- a/internal/push/push_to_client.go +++ b/internal/push/push_to_client.go @@ -18,20 +18,21 @@ import ( "context" "encoding/json" "errors" + "github.com/OpenIMSDK/Open-IM-Server/pkg/msgprocessor" "github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush" "github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush/fcm" "github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush/getui" "github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush/jpush" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/localcache" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/msggateway" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mcontext" @@ -82,7 +83,7 @@ func NewOfflinePusher(cache cache.MsgModel) offlinepush.OfflinePusher { } func (p *Pusher) DeleteMemberAndSetConversationSeq(ctx context.Context, groupID string, userIDs []string) error { - conevrsationID := utils.GetConversationIDBySessionType(constant.SuperGroupChatType, groupID) + conevrsationID := msgprocessor.GetConversationIDBySessionType(constant.SuperGroupChatType, groupID) maxSeq, err := p.msgRpcClient.GetConversationMaxSeq(ctx, conevrsationID) if err != nil { return err @@ -170,7 +171,7 @@ func (p *Pusher) Push2SuperGroup(ctx context.Context, groupID string, msg *sdkws }(groupID, kickedUsers) pushToUserIDs = append(pushToUserIDs, kickedUsers...) case constant.GroupDismissedNotification: - if utils.IsNotification(utils.GetConversationIDByMsg(msg)) { // 消息先到,通知后到 + if msgprocessor.IsNotification(msgprocessor.GetConversationIDByMsg(msg)) { // 消息先到,通知后到 var tips sdkws.GroupDismissedTips if p.UnmarshalNotificationElem(msg.Content, &tips) != nil { return err diff --git a/internal/push/tools.go b/internal/push/tools.go index ea8d160b1..2135c901e 100644 --- a/internal/push/tools.go +++ b/internal/push/tools.go @@ -1,8 +1,8 @@ package push import ( + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/constant" "google.golang.org/protobuf/proto" ) diff --git a/internal/rpc/auth/auth.go b/internal/rpc/auth/auth.go index 48e4e5922..1abfc9771 100644 --- a/internal/rpc/auth/auth.go +++ b/internal/rpc/auth/auth.go @@ -16,16 +16,17 @@ package auth import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" "google.golang.org/grpc" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" pbAuth "github.com/OpenIMSDK/protocol/auth" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/msggateway" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" @@ -76,7 +77,7 @@ func (s *authServer) UserToken(ctx context.Context, req *pbAuth.UserTokenReq) (* } func (s *authServer) parseToken(ctx context.Context, tokensString string) (claims *tokenverify.Claims, err error) { - claims, err = tokenverify.GetClaimFromToken(tokensString) + claims, err = tokenverify.GetClaimFromToken(tokensString, authverify.Secret()) if err != nil { return nil, utils.Wrap(err, "") } @@ -116,7 +117,7 @@ func (s *authServer) ParseToken( } func (s *authServer) ForceLogout(ctx context.Context, req *pbAuth.ForceLogoutReq) (*pbAuth.ForceLogoutResp, error) { - if err := tokenverify.CheckAdmin(ctx); err != nil { + if err := authverify.CheckAdmin(ctx); err != nil { return nil, err } if err := s.forceKickOff(ctx, req.UserID, req.PlatformID, mcontext.GetOperationID(ctx)); err != nil { diff --git a/internal/rpc/conversation/conversaion.go b/internal/rpc/conversation/conversaion.go index e27756749..753a45722 100644 --- a/internal/rpc/conversation/conversaion.go +++ b/internal/rpc/conversation/conversaion.go @@ -16,6 +16,7 @@ package conversation import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/msgprocessor" "google.golang.org/grpc" @@ -26,8 +27,8 @@ import ( tableRelation "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification" + "github.com/OpenIMSDK/protocol/constant" pbConversation "github.com/OpenIMSDK/protocol/conversation" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" @@ -197,7 +198,7 @@ func (c *conversationServer) GetRecvMsgNotNotifyUserIDs(ctx context.Context, req // create conversation without notification for msg redis transfer. func (c *conversationServer) CreateSingleChatConversations(ctx context.Context, req *pbConversation.CreateSingleChatConversationsReq) (*pbConversation.CreateSingleChatConversationsResp, error) { var conversation tableRelation.ConversationModel - conversation.ConversationID = utils.GetConversationIDBySessionType(constant.SingleChatType, req.RecvID, req.SendID) + conversation.ConversationID = msgprocessor.GetConversationIDBySessionType(constant.SingleChatType, req.RecvID, req.SendID) conversation.ConversationType = constant.SingleChatType conversation.OwnerUserID = req.SendID conversation.UserID = req.RecvID diff --git a/internal/rpc/friend/black.go b/internal/rpc/friend/black.go index 6f7ed58dd..9df5abb33 100644 --- a/internal/rpc/friend/black.go +++ b/internal/rpc/friend/black.go @@ -16,13 +16,13 @@ package friend import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" "time" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/convert" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" pbFriend "github.com/OpenIMSDK/protocol/friend" "github.com/OpenIMSDK/tools/mcontext" - "github.com/OpenIMSDK/tools/tokenverify" ) func (s *friendServer) GetPaginationBlacks( @@ -76,7 +76,7 @@ func (s *friendServer) RemoveBlack( } func (s *friendServer) AddBlack(ctx context.Context, req *pbFriend.AddBlackReq) (*pbFriend.AddBlackResp, error) { - if err := tokenverify.CheckAccessV3(ctx, req.OwnerUserID); err != nil { + if err := authverify.CheckAccessV3(ctx, req.OwnerUserID); err != nil { return nil, err } _, err := s.userRpcClient.GetUsersInfo(ctx, []string{req.OwnerUserID, req.BlackUserID}) diff --git a/internal/rpc/friend/callback.go b/internal/rpc/friend/callback.go index d3a793a6f..bd7716090 100644 --- a/internal/rpc/friend/callback.go +++ b/internal/rpc/friend/callback.go @@ -18,10 +18,10 @@ import ( "context" cbapi "github.com/OpenIMSDK/Open-IM-Server/pkg/callbackstruct" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/http" + "github.com/OpenIMSDK/protocol/constant" pbfriend "github.com/OpenIMSDK/protocol/friend" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/mcontext" ) diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index 5792fdb38..9ef38ef3c 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -16,6 +16,7 @@ package friend import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/convert" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" @@ -28,11 +29,10 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation" tablerelation "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification" + "github.com/OpenIMSDK/protocol/constant" pbfriend "github.com/OpenIMSDK/protocol/friend" - "github.com/OpenIMSDK/tools/constant" registry "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/errs" - "github.com/OpenIMSDK/tools/tokenverify" "github.com/OpenIMSDK/tools/tx" "github.com/OpenIMSDK/tools/utils" ) @@ -95,7 +95,7 @@ func (s *friendServer) ApplyToAddFriend( ) (resp *pbfriend.ApplyToAddFriendResp, err error) { defer log.ZInfo(ctx, utils.GetFuncName()+" Return") resp = &pbfriend.ApplyToAddFriendResp{} - if err := tokenverify.CheckAccessV3(ctx, req.FromUserID); err != nil { + if err := authverify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } if req.ToUserID == req.FromUserID { @@ -127,7 +127,7 @@ func (s *friendServer) ImportFriends( req *pbfriend.ImportFriendReq, ) (resp *pbfriend.ImportFriendResp, err error) { defer log.ZInfo(ctx, utils.GetFuncName()+" Return") - if err := tokenverify.CheckAdmin(ctx); err != nil { + if err := authverify.CheckAdmin(ctx); err != nil { return nil, err } if _, err := s.userRpcClient.GetUsersInfo(ctx, append([]string{req.OwnerUserID}, req.FriendUserIDs...)); err != nil { @@ -154,7 +154,7 @@ func (s *friendServer) RespondFriendApply( ) (resp *pbfriend.RespondFriendApplyResp, err error) { defer log.ZInfo(ctx, utils.GetFuncName()+" Return") resp = &pbfriend.RespondFriendApplyResp{} - if err := tokenverify.CheckAccessV3(ctx, req.ToUserID); err != nil { + if err := authverify.CheckAccessV3(ctx, req.ToUserID); err != nil { return nil, err } diff --git a/internal/rpc/group/callback.go b/internal/rpc/group/callback.go index 2f85a6674..72661d16b 100644 --- a/internal/rpc/group/callback.go +++ b/internal/rpc/group/callback.go @@ -20,12 +20,12 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/apistruct" "github.com/OpenIMSDK/Open-IM-Server/pkg/callbackstruct" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/http" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/group" "github.com/OpenIMSDK/protocol/wrapperspb" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/mcontext" "github.com/OpenIMSDK/tools/utils" diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index 4c041e13a..bcadc61a3 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -17,6 +17,8 @@ package group import ( "context" "fmt" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" + "github.com/OpenIMSDK/Open-IM-Server/pkg/msgprocessor" "math/big" "math/rand" "strconv" @@ -39,14 +41,13 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation" relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation" + "github.com/OpenIMSDK/protocol/constant" pbGroup "github.com/OpenIMSDK/protocol/group" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mcontext" - "github.com/OpenIMSDK/tools/tokenverify" "github.com/OpenIMSDK/tools/utils" ) @@ -100,7 +101,7 @@ func (s *groupServer) GetGroupUsersReqApplicationList(ctx context.Context, req * } func (s *groupServer) CheckGroupAdmin(ctx context.Context, groupID string) error { - if !tokenverify.IsAppManagerUid(ctx) { + if !authverify.IsAppManagerUid(ctx) { groupMember, err := s.GroupDatabase.TakeGroupMember(ctx, groupID, mcontext.GetOpUserID(ctx)) if err != nil { return err @@ -162,7 +163,7 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupR if req.OwnerUserID == "" { return nil, errs.ErrArgs.Wrap("no group owner") } - if err := tokenverify.CheckAccessV3(ctx, req.OwnerUserID); err != nil { + if err := authverify.CheckAccessV3(ctx, req.OwnerUserID); err != nil { return nil, err } userIDs := append(append(req.MemberUserIDs, req.AdminUserIDs...), req.OwnerUserID) @@ -257,7 +258,7 @@ func (s *groupServer) CreateGroup(ctx context.Context, req *pbGroup.CreateGroupR func (s *groupServer) GetJoinedGroupList(ctx context.Context, req *pbGroup.GetJoinedGroupListReq) (*pbGroup.GetJoinedGroupListResp, error) { resp := &pbGroup.GetJoinedGroupListResp{} - if err := tokenverify.CheckAccessV3(ctx, req.FromUserID); err != nil { + if err := authverify.CheckAccessV3(ctx, req.FromUserID); err != nil { return nil, err } var pageNumber, showNumber int32 @@ -325,7 +326,7 @@ func (s *groupServer) InviteUserToGroup(ctx context.Context, req *pbGroup.Invite } var groupMember *relationTb.GroupMemberModel var opUserID string - if !tokenverify.IsAppManagerUid(ctx) { + if !authverify.IsAppManagerUid(ctx) { opUserID = mcontext.GetOpUserID(ctx) groupMembers, err := s.FindGroupMember(ctx, []string{req.GroupID}, []string{opUserID}, nil) if err != nil { @@ -337,7 +338,7 @@ func (s *groupServer) InviteUserToGroup(ctx context.Context, req *pbGroup.Invite groupMember = groupMembers[0] } if group.NeedVerification == constant.AllNeedVerification { - if !tokenverify.IsAppManagerUid(ctx) { + if !authverify.IsAppManagerUid(ctx) { if !(groupMember.RoleLevel == constant.GroupOwner || groupMember.RoleLevel == constant.GroupAdmin) { var requests []*relationTb.GroupRequestModel for _, userID := range req.InvitedUserIDs { @@ -480,7 +481,7 @@ func (s *groupServer) KickGroupMember(ctx context.Context, req *pbGroup.KickGrou for i, member := range members { memberMap[member.UserID] = members[i] } - isAppManagerUid := tokenverify.IsAppManagerUid(ctx) + isAppManagerUid := authverify.IsAppManagerUid(ctx) opMember := memberMap[opUserID] for _, userID := range req.KickedUserIDs { member, ok := memberMap[userID] @@ -670,7 +671,7 @@ func (s *groupServer) GroupApplicationResponse(ctx context.Context, req *pbGroup if !utils.Contain(req.HandleResult, constant.GroupResponseAgree, constant.GroupResponseRefuse) { return nil, errs.ErrArgs.Wrap("HandleResult unknown") } - if !tokenverify.IsAppManagerUid(ctx) { + if !authverify.IsAppManagerUid(ctx) { groupMember, err := s.GroupDatabase.TakeGroupMember(ctx, req.GroupID, mcontext.GetOpUserID(ctx)) if err != nil { return nil, err @@ -829,7 +830,7 @@ func (s *groupServer) QuitGroup(ctx context.Context, req *pbGroup.QuitGroupReq) } func (s *groupServer) deleteMemberAndSetConversationSeq(ctx context.Context, groupID string, userIDs []string) error { - conevrsationID := utils.GetConversationIDBySessionType(constant.SuperGroupChatType, groupID) + conevrsationID := msgprocessor.GetConversationIDBySessionType(constant.SuperGroupChatType, groupID) maxSeq, err := s.msgRpcClient.GetConversationMaxSeq(ctx, conevrsationID) if err != nil { return err @@ -839,7 +840,7 @@ func (s *groupServer) deleteMemberAndSetConversationSeq(ctx context.Context, gro func (s *groupServer) SetGroupInfo(ctx context.Context, req *pbGroup.SetGroupInfoReq) (*pbGroup.SetGroupInfoResp, error) { var opMember *relationTb.GroupMemberModel - if !tokenverify.IsAppManagerUid(ctx) { + if !authverify.IsAppManagerUid(ctx) { var err error opMember, err = s.TakeGroupMember(ctx, req.GroupInfoForSet.GroupID, mcontext.GetOpUserID(ctx)) if err != nil { @@ -889,7 +890,7 @@ func (s *groupServer) SetGroupInfo(ctx context.Context, req *pbGroup.SetGroupInf go func() { nctx := mcontext.NewCtx("@@@" + mcontext.GetOperationID(ctx)) conversation := &pbConversation.ConversationReq{ - ConversationID: utils.GetConversationIDBySessionType(constant.SuperGroupChatType, req.GroupInfoForSet.GroupID), + ConversationID: msgprocessor.GetConversationIDBySessionType(constant.SuperGroupChatType, req.GroupInfoForSet.GroupID), ConversationType: constant.SuperGroupChatType, GroupID: req.GroupInfoForSet.GroupID, } @@ -948,7 +949,7 @@ func (s *groupServer) TransferGroupOwner(ctx context.Context, req *pbGroup.Trans if newOwner == nil { return nil, errs.ErrArgs.Wrap("NewOwnerUser not in group " + req.NewOwnerUserID) } - if !tokenverify.IsAppManagerUid(ctx) { + if !authverify.IsAppManagerUid(ctx) { if !(mcontext.GetOpUserID(ctx) == oldOwner.UserID && oldOwner.RoleLevel == constant.GroupOwner) { return nil, errs.ErrNoPermission.Wrap("no permission transfer group owner") } @@ -1080,7 +1081,7 @@ func (s *groupServer) DismissGroup(ctx context.Context, req *pbGroup.DismissGrou if err != nil { return nil, err } - if !tokenverify.IsAppManagerUid(ctx) { + if !authverify.IsAppManagerUid(ctx) { if owner.UserID != mcontext.GetOpUserID(ctx) { return nil, errs.ErrNoPermission.Wrap("not group owner") } @@ -1131,7 +1132,7 @@ func (s *groupServer) MuteGroupMember(ctx context.Context, req *pbGroup.MuteGrou if err != nil { return nil, err } - if !tokenverify.IsAppManagerUid(ctx) { + if !authverify.IsAppManagerUid(ctx) { opMember, err := s.TakeGroupMember(ctx, req.GroupID, mcontext.GetOpUserID(ctx)) if err != nil { return nil, err @@ -1179,7 +1180,7 @@ func (s *groupServer) CancelMuteGroupMember(ctx context.Context, req *pbGroup.Ca if err != nil { return nil, err } - if !tokenverify.IsAppManagerUid(ctx) { + if !authverify.IsAppManagerUid(ctx) { opMember, err := s.TakeGroupMember(ctx, req.GroupID, mcontext.GetOpUserID(ctx)) if err != nil { return nil, err @@ -1263,7 +1264,7 @@ func (s *groupServer) SetGroupMemberInfo(ctx context.Context, req *pbGroup.SetGr memberMap := utils.SliceToMap(members, func(e *relationTb.GroupMemberModel) [2]string { return [...]string{e.GroupID, e.UserID} }) - if !tokenverify.IsAppManagerUid(ctx) { + if !authverify.IsAppManagerUid(ctx) { opUserID := mcontext.GetOpUserID(ctx) for _, member := range req.Members { if member.RoleLevel != nil { diff --git a/internal/rpc/group/super_group.go b/internal/rpc/group/super_group.go index bdd810ba9..070e154bc 100644 --- a/internal/rpc/group/super_group.go +++ b/internal/rpc/group/super_group.go @@ -22,9 +22,9 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/convert" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation" + "github.com/OpenIMSDK/protocol/constant" pbGroup "github.com/OpenIMSDK/protocol/group" sdkws "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/utils" ) diff --git a/internal/rpc/msg/as_read.go b/internal/rpc/msg/as_read.go index c2f07bd8e..d17b2b43a 100644 --- a/internal/rpc/msg/as_read.go +++ b/internal/rpc/msg/as_read.go @@ -19,9 +19,9 @@ import ( "github.com/redis/go-redis/v9" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/msg" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" ) diff --git a/internal/rpc/msg/callback.go b/internal/rpc/msg/callback.go index 47bd1f0f1..5ed6a5f09 100644 --- a/internal/rpc/msg/callback.go +++ b/internal/rpc/msg/callback.go @@ -20,10 +20,10 @@ import ( "google.golang.org/protobuf/proto" cbapi "github.com/OpenIMSDK/Open-IM-Server/pkg/callbackstruct" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/http" + "github.com/OpenIMSDK/protocol/constant" pbChat "github.com/OpenIMSDK/protocol/msg" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mcontext" diff --git a/internal/rpc/msg/delete.go b/internal/rpc/msg/delete.go index 8c20a5bc3..cdee8bdb5 100644 --- a/internal/rpc/msg/delete.go +++ b/internal/rpc/msg/delete.go @@ -16,13 +16,13 @@ package msg import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/conversation" "github.com/OpenIMSDK/protocol/msg" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/log" - "github.com/OpenIMSDK/tools/tokenverify" "github.com/OpenIMSDK/tools/utils" ) @@ -45,7 +45,7 @@ func (m *msgServer) ClearConversationsMsg( ctx context.Context, req *msg.ClearConversationsMsgReq, ) (*msg.ClearConversationsMsgResp, error) { - if err := tokenverify.CheckAccessV3(ctx, req.UserID); err != nil { + if err := authverify.CheckAccessV3(ctx, req.UserID); err != nil { return nil, err } if err := m.clearConversation(ctx, req.ConversationIDs, req.UserID, req.DeleteSyncOpt); err != nil { @@ -58,7 +58,7 @@ func (m *msgServer) UserClearAllMsg( ctx context.Context, req *msg.UserClearAllMsgReq, ) (*msg.UserClearAllMsgResp, error) { - if err := tokenverify.CheckAccessV3(ctx, req.UserID); err != nil { + if err := authverify.CheckAccessV3(ctx, req.UserID); err != nil { return nil, err } conversationIDs, err := m.ConversationLocalCache.GetConversationIDs(ctx, req.UserID) @@ -73,7 +73,7 @@ func (m *msgServer) UserClearAllMsg( } func (m *msgServer) DeleteMsgs(ctx context.Context, req *msg.DeleteMsgsReq) (*msg.DeleteMsgsResp, error) { - if err := tokenverify.CheckAccessV3(ctx, req.UserID); err != nil { + if err := authverify.CheckAccessV3(ctx, req.UserID); err != nil { return nil, err } isSyncSelf, isSyncOther := m.validateDeleteSyncOpt(req.DeleteSyncOpt) @@ -121,7 +121,7 @@ func (m *msgServer) DeleteMsgPhysical( ctx context.Context, req *msg.DeleteMsgPhysicalReq, ) (*msg.DeleteMsgPhysicalResp, error) { - if err := tokenverify.CheckAdmin(ctx); err != nil { + if err := authverify.CheckAdmin(ctx); err != nil { return nil, err } remainTime := utils.GetCurrentTimestampBySecond() - req.Timestamp diff --git a/internal/rpc/msg/message_interceptor.go b/internal/rpc/msg/message_interceptor.go index 915a04ab1..3c8bf965d 100644 --- a/internal/rpc/msg/message_interceptor.go +++ b/internal/rpc/msg/message_interceptor.go @@ -17,10 +17,10 @@ package msg import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/msg" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/errs" ) diff --git a/internal/rpc/msg/msg_status.go b/internal/rpc/msg/msg_status.go index bd89d61f4..68d6d6d4e 100644 --- a/internal/rpc/msg/msg_status.go +++ b/internal/rpc/msg/msg_status.go @@ -17,8 +17,8 @@ package msg import ( "context" + "github.com/OpenIMSDK/protocol/constant" pbMsg "github.com/OpenIMSDK/protocol/msg" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/mcontext" ) diff --git a/internal/rpc/msg/revoke.go b/internal/rpc/msg/revoke.go index 5651e79e0..18066c1a2 100644 --- a/internal/rpc/msg/revoke.go +++ b/internal/rpc/msg/revoke.go @@ -17,15 +17,15 @@ package msg import ( "context" "encoding/json" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" "time" unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/msg" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" - "github.com/OpenIMSDK/tools/tokenverify" "github.com/OpenIMSDK/tools/utils" ) @@ -40,7 +40,7 @@ func (m *msgServer) RevokeMsg(ctx context.Context, req *msg.RevokeMsgReq) (*msg. if req.Seq < 0 { return nil, errs.ErrArgs.Wrap("seq is invalid") } - if err := tokenverify.CheckAccessV3(ctx, req.UserID); err != nil { + if err := authverify.CheckAccessV3(ctx, req.UserID); err != nil { return nil, err } user, err := m.User.GetUserInfo(ctx, req.UserID) @@ -60,10 +60,10 @@ func (m *msgServer) RevokeMsg(ctx context.Context, req *msg.RevokeMsgReq) (*msg. data, _ := json.Marshal(msgs[0]) log.ZInfo(ctx, "GetMsgBySeqs", "conversationID", req.ConversationID, "seq", req.Seq, "msg", string(data)) var role int32 - if !tokenverify.IsAppManagerUid(ctx) { + if !authverify.IsAppManagerUid(ctx) { switch msgs[0].SessionType { case constant.SingleChatType: - if err := tokenverify.CheckAccessV3(ctx, msgs[0].SendID); err != nil { + if err := authverify.CheckAccessV3(ctx, msgs[0].SendID); err != nil { return nil, err } role = user.AppMangerLevel diff --git a/internal/rpc/msg/send.go b/internal/rpc/msg/send.go index 2c5df6408..8ec54e7d8 100644 --- a/internal/rpc/msg/send.go +++ b/internal/rpc/msg/send.go @@ -16,13 +16,14 @@ package msg import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/msgprocessor" promePkg "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome" + "github.com/OpenIMSDK/protocol/constant" pbConversation "github.com/OpenIMSDK/protocol/conversation" pbMsg "github.com/OpenIMSDK/protocol/msg" "github.com/OpenIMSDK/protocol/sdkws" "github.com/OpenIMSDK/protocol/wrapperspb" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mcontext" @@ -90,7 +91,7 @@ func (m *msgServer) setConversationAtInfo(nctx context.Context, msg *sdkws.MsgDa ctx := mcontext.NewCtx("@@@" + mcontext.GetOperationID(nctx)) var atUserID []string conversation := &pbConversation.ConversationReq{ - ConversationID: utils.GetConversationIDByMsg(msg), + ConversationID: msgprocessor.GetConversationIDByMsg(msg), ConversationType: msg.SessionType, GroupID: msg.GroupID, } @@ -148,8 +149,8 @@ func (m *msgServer) sendMsgSingleChat(ctx context.Context, req *pbMsg.SendMsgReq if err := m.messageVerification(ctx, req); err != nil { return nil, err } - var isSend bool = true - isNotification := utils.IsNotificationByMsg(req.MsgData) + var isSend = true + isNotification := msgprocessor.IsNotificationByMsg(req.MsgData) if !isNotification { isSend, err = m.modifyMessageByUserMessageReceiveOpt( ctx, diff --git a/internal/rpc/msg/server.go b/internal/rpc/msg/server.go index 542f59d4b..fe795e0eb 100644 --- a/internal/rpc/msg/server.go +++ b/internal/rpc/msg/server.go @@ -25,9 +25,9 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/conversation" "github.com/OpenIMSDK/protocol/msg" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/discoveryregistry" ) diff --git a/internal/rpc/msg/sync_msg.go b/internal/rpc/msg/sync_msg.go index edcb4ff89..e91138d6c 100644 --- a/internal/rpc/msg/sync_msg.go +++ b/internal/rpc/msg/sync_msg.go @@ -16,13 +16,14 @@ package msg import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" + "github.com/OpenIMSDK/Open-IM-Server/pkg/msgprocessor" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/msg" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/protocol/sdkws" "github.com/OpenIMSDK/tools/log" - "github.com/OpenIMSDK/tools/tokenverify" "github.com/OpenIMSDK/tools/utils" ) @@ -34,7 +35,7 @@ func (m *msgServer) PullMessageBySeqs( resp.Msgs = make(map[string]*sdkws.PullMsgs) resp.NotificationMsgs = make(map[string]*sdkws.PullMsgs) for _, seq := range req.SeqRanges { - if !utils.IsNotification(seq.ConversationID) { + if !msgprocessor.IsNotification(seq.ConversationID) { conversation, err := m.Conversation.GetConversation(ctx, req.UserID, seq.ConversationID) if err != nil { log.ZError(ctx, "GetConversation error", err, "conversationID", seq.ConversationID) @@ -85,7 +86,7 @@ func (m *msgServer) PullMessageBySeqs( } func (m *msgServer) GetMaxSeq(ctx context.Context, req *sdkws.GetMaxSeqReq) (*sdkws.GetMaxSeqResp, error) { - if err := tokenverify.CheckAccessV3(ctx, req.UserID); err != nil { + if err := authverify.CheckAccessV3(ctx, req.UserID); err != nil { return nil, err } conversationIDs, err := m.ConversationLocalCache.GetConversationIDs(ctx, req.UserID) diff --git a/internal/rpc/msg/utils.go b/internal/rpc/msg/utils.go index 48f7b03f3..1579bcbef 100644 --- a/internal/rpc/msg/utils.go +++ b/internal/rpc/msg/utils.go @@ -18,9 +18,9 @@ import ( "github.com/redis/go-redis/v9" "gorm.io/gorm" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/utils" ) diff --git a/internal/rpc/msg/verify.go b/internal/rpc/msg/verify.go index 32c05ec82..c588d6cf7 100644 --- a/internal/rpc/msg/verify.go +++ b/internal/rpc/msg/verify.go @@ -20,10 +20,10 @@ import ( "strconv" "time" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/msg" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/utils" ) diff --git a/internal/rpc/third/third.go b/internal/rpc/third/third.go index 5db6c39e4..555e9371c 100644 --- a/internal/rpc/third/third.go +++ b/internal/rpc/third/third.go @@ -27,13 +27,13 @@ import ( "google.golang.org/grpc" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation" relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" "github.com/OpenIMSDK/protocol/third" - "github.com/OpenIMSDK/tools/config" "github.com/OpenIMSDK/tools/discoveryregistry" ) diff --git a/internal/rpc/third/tool.go b/internal/rpc/third/tool.go index 5a7d1697b..3034de674 100644 --- a/internal/rpc/third/tool.go +++ b/internal/rpc/third/tool.go @@ -18,13 +18,13 @@ import ( "context" "errors" "fmt" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" "strings" "unicode/utf8" "github.com/OpenIMSDK/protocol/third" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/mcontext" - "github.com/OpenIMSDK/tools/tokenverify" ) func toPbMapArray(m map[string][]string) []*third.KeyValues { @@ -52,7 +52,7 @@ func checkUploadName(ctx context.Context, name string) error { if opUserID == "" { return errs.ErrNoPermission.Wrap("opUserID is empty") } - if !tokenverify.IsManagerUserID(opUserID) { + if !authverify.IsManagerUserID(opUserID) { if !strings.HasPrefix(name, opUserID+"/") { return errs.ErrNoPermission.Wrap(fmt.Sprintf("name must start with `%s/`", opUserID)) } diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index 65e977274..f95e66c0d 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -17,11 +17,13 @@ package user import ( "context" "errors" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" "strings" "time" "github.com/OpenIMSDK/tools/log" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/convert" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" @@ -29,13 +31,11 @@ import ( tablerelation "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/sdkws" pbuser "github.com/OpenIMSDK/protocol/user" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" registry "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/errs" - "github.com/OpenIMSDK/tools/tokenverify" "github.com/OpenIMSDK/tools/tx" "google.golang.org/grpc" @@ -50,6 +50,11 @@ type userServer struct { RegisterCenter registry.SvcDiscoveryRegistry } +func (s *userServer) SubscribeOrCancelUsersStatus(ctx context.Context, req *pbuser.SubscribeOrCancelUsersStatusReq) (*pbuser.SubscribeOrCancelUsersStatusResp, error) { + //TODO implement me + panic("implement me") +} + func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error { db, err := relation.NewGormDB() if err != nil { @@ -99,7 +104,7 @@ func (s *userServer) GetDesignateUsers(ctx context.Context, req *pbuser.GetDesig func (s *userServer) UpdateUserInfo(ctx context.Context, req *pbuser.UpdateUserInfoReq) (resp *pbuser.UpdateUserInfoResp, err error) { resp = &pbuser.UpdateUserInfoResp{} - err = tokenverify.CheckAccessV3(ctx, req.UserInfo.UserID) + err = authverify.CheckAccessV3(ctx, req.UserInfo.UserID) if err != nil { return nil, err } @@ -141,7 +146,7 @@ func (s *userServer) AccountCheck(ctx context.Context, req *pbuser.AccountCheckR if utils.Duplicate(req.CheckUserIDs) { return nil, errs.ErrArgs.Wrap("userID repeated") } - err = tokenverify.CheckAdmin(ctx) + err = authverify.CheckAdmin(ctx) if err != nil { return nil, err } diff --git a/internal/tools/cron_task.go b/internal/tools/cron_task.go index 354682f8e..a7333a5d4 100644 --- a/internal/tools/cron_task.go +++ b/internal/tools/cron_task.go @@ -21,7 +21,7 @@ import ( "github.com/robfig/cron/v3" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/tools/log" ) diff --git a/internal/tools/msg.go b/internal/tools/msg.go index 8e67bc350..972877516 100644 --- a/internal/tools/msg.go +++ b/internal/tools/msg.go @@ -24,13 +24,13 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient/notification" - "github.com/OpenIMSDK/tools/config" "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" diff --git a/internal/tools/msg_test.go b/internal/tools/msg_test.go deleted file mode 100644 index f4ce4d054..000000000 --- a/internal/tools/msg_test.go +++ /dev/null @@ -1,355 +0,0 @@ -// Copyright © 2023 OpenIM. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package tools - -import ( - "context" - "strconv" - - "go.mongodb.org/mongo-driver/bson" - - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" - "github.com/OpenIMSDK/tools/constant" - "github.com/OpenIMSDK/tools/mcontext" - "github.com/OpenIMSDK/tools/utils" - - unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation" - "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation" - - "testing" - "time" -) - -func GenMsgDoc(startSeq, stopSeq, delSeq, index int64, conversationID string) *unRelationTb.MsgDocModel { - msgDoc := &unRelationTb.MsgDocModel{DocID: conversationID + strconv.Itoa(int(index))} - for i := 0; i < 5000; i++ { - msgDoc.Msg = append(msgDoc.Msg, &unRelationTb.MsgInfoModel{}) - } - for i := startSeq; i <= stopSeq; i++ { - msg := &unRelationTb.MsgDataModel{ - SendID: "sendID1", - RecvID: "recvID1", - GroupID: "", - ClientMsgID: "xxx", - ServerMsgID: "xxx", - SenderPlatformID: 1, - SenderNickname: "testNickName", - SenderFaceURL: "testFaceURL", - SessionType: 1, - MsgFrom: 100, - ContentType: 101, - Content: "testContent", - Seq: i, - CreateTime: time.Now().Unix(), - Status: 1, - } - if i <= delSeq { - msg.SendTime = 10000 - } else { - msg.SendTime = utils.GetCurrentTimestampByMill() - } - msgDoc.Msg[i-1] = &unRelationTb.MsgInfoModel{Msg: msg} - } - return msgDoc -} - -func TestDeleteMongoMsgAndResetRedisSeq(t *testing.T) { - operationID := "test" - rdb, err := cache.NewRedis() - if err != nil { - return - } - mgo, err := unrelation.NewMongo() - if err != nil { - return - } - cacheModel := cache.NewMsgCacheModel(rdb) - mongoClient := mgo.GetDatabase().Collection(unRelationTb.MsgDocModel{}.TableName()) - ctx := context.Background() - ctx = mcontext.SetOperationID(ctx, operationID) - - testUID1 := "test_del_id1" - var conversationID string - conversationID = utils.GetConversationIDBySessionType(constant.SuperGroupChatType, testUID1) - _, err = mongoClient.DeleteOne(ctx, bson.M{"doc_id": conversationID + ":" + strconv.Itoa(0)}) - if err != nil { - t.Error("DeleteOne failed") - return - } - - err = cacheModel.SetMaxSeq(ctx, conversationID, 600) - if err != nil { - t.Error("SetUserMaxSeq failed") - } - msgDoc := GenMsgDoc(1, 600, 200, 0, conversationID) - if _, err := mongoClient.InsertOne(ctx, msgDoc); err != nil { - t.Error("InsertOne failed", conversationID) - } - - msgTools, err := InitMsgTool() - if err != nil { - t.Error("init failed") - return - } - msgTools.ClearConversationsMsg(ctx, []string{conversationID}) - minSeqMongo, maxSeqMongo, minSeqCache, maxSeqCache, err := msgTools.msgDatabase.GetConversationMinMaxSeqInMongoAndCache( - ctx, - conversationID, - ) - if err != nil { - t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed") - return - } - if maxSeqCache != maxSeqMongo { - t.Error("checkMaxSeqWithMongo failed", conversationID) - } - if minSeqMongo != minSeqCache { - t.Error("minSeqMongo != minSeqCache", minSeqMongo, minSeqCache) - } - if minSeqCache != 201 { - t.Error("test1 is not the same", "minSeq:", minSeqCache, "targetSeq", 201) - } - - /////// uid2 - - testUID2 := "test_del_id2" - conversationID = utils.GetConversationIDBySessionType(constant.SuperGroupChatType, testUID2) - - _, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(0)}) - if err != nil { - t.Error("delete failed") - } - _, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(1)}) - if err != nil { - t.Error("delete failed") - } - - err = cacheModel.SetMaxSeq(ctx, conversationID, 7000) - if err != nil { - t.Error("SetUserMaxSeq failed") - } - msgDoc = GenMsgDoc(1, 4999, 5000, 0, conversationID) - msgDoc2 := GenMsgDoc(5000, 7000, 6000, 1, conversationID) - if _, err := mongoClient.InsertOne(ctx, msgDoc); err != nil { - t.Error("InsertOne failed", testUID1) - } - if _, err := mongoClient.InsertOne(ctx, msgDoc2); err != nil { - t.Error("InsertOne failed", testUID1) - } - - msgTools.ClearConversationsMsg(ctx, []string{conversationID}) - minSeqMongo, maxSeqMongo, minSeqCache, maxSeqCache, err = msgTools.msgDatabase.GetConversationMinMaxSeqInMongoAndCache( - ctx, - conversationID, - ) - if err != nil { - t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed") - return - } - if maxSeqCache != maxSeqMongo { - t.Error("checkMaxSeqWithMongo failed", conversationID) - } - if minSeqMongo != minSeqCache { - t.Error("minSeqMongo != minSeqCache", minSeqMongo, minSeqCache) - } - if minSeqCache != 6001 { - t.Error("test1 is not the same", "minSeq:", minSeqCache, "targetSeq", 201) - } - - /////// uid3 - testUID3 := "test_del_id3" - conversationID = utils.GetConversationIDBySessionType(constant.SuperGroupChatType, testUID3) - _, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(0)}) - if err != nil { - t.Error("delete failed") - } - err = cacheModel.SetMaxSeq(ctx, conversationID, 4999) - if err != nil { - t.Error("SetUserMaxSeq failed") - } - msgDoc = GenMsgDoc(1, 4999, 5000, 0, conversationID) - if _, err := mongoClient.InsertOne(ctx, msgDoc); err != nil { - t.Error("InsertOne failed", conversationID) - } - - msgTools.ClearConversationsMsg(ctx, []string{conversationID}) - minSeqMongo, maxSeqMongo, minSeqCache, maxSeqCache, err = msgTools.msgDatabase.GetConversationMinMaxSeqInMongoAndCache( - ctx, - conversationID, - ) - if err != nil { - t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed") - return - } - if maxSeqCache != maxSeqMongo { - t.Error("checkMaxSeqWithMongo failed", conversationID) - } - if minSeqMongo != minSeqCache { - t.Error("minSeqMongo != minSeqCache", minSeqMongo, minSeqCache) - } - if minSeqCache != 5000 { - t.Error("test1 is not the same", "minSeq:", minSeqCache, "targetSeq", 201) - } - - //// uid4 - testUID4 := "test_del_id4" - conversationID = utils.GetConversationIDBySessionType(constant.SuperGroupChatType, testUID4) - _, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(0)}) - if err != nil { - t.Error("delete failed") - } - _, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(1)}) - if err != nil { - t.Error("delete failed") - } - _, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(2)}) - if err != nil { - t.Error("delete failed") - } - - err = cacheModel.SetMaxSeq(ctx, conversationID, 12000) - msgDoc = GenMsgDoc(1, 4999, 5000, 0, conversationID) - msgDoc2 = GenMsgDoc(5000, 9999, 10000, 1, conversationID) - msgDoc3 := GenMsgDoc(10000, 12000, 11000, 2, conversationID) - if _, err := mongoClient.InsertOne(ctx, msgDoc); err != nil { - t.Error("InsertOne failed", conversationID) - } - if _, err := mongoClient.InsertOne(ctx, msgDoc2); err != nil { - t.Error("InsertOne failed", conversationID) - } - if _, err := mongoClient.InsertOne(ctx, msgDoc3); err != nil { - t.Error("InsertOne failed", conversationID) - } - - msgTools.ClearConversationsMsg(ctx, []string{conversationID}) - if err != nil { - t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed") - return - } - minSeqMongo, maxSeqMongo, minSeqCache, maxSeqCache, err = msgTools.msgDatabase.GetConversationMinMaxSeqInMongoAndCache( - ctx, - conversationID, - ) - if err != nil { - t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed") - return - } - if maxSeqCache != maxSeqMongo { - t.Error("checkMaxSeqWithMongo failed", conversationID) - } - if minSeqMongo != minSeqCache { - t.Error("minSeqMongo != minSeqCache", minSeqMongo, minSeqCache) - } - if minSeqCache != 5000 { - t.Error("test1 is not the same", "minSeq:", minSeqCache) - } - - testUID5 := "test_del_id5" - conversationID = utils.GetConversationIDBySessionType(constant.SuperGroupChatType, testUID5) - - _, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(0)}) - if err != nil { - t.Error("delete failed") - } - _, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(1)}) - if err != nil { - t.Error("delete failed") - } - err = cacheModel.SetMaxSeq(ctx, conversationID, 9999) - msgDoc = GenMsgDoc(1, 4999, 5000, 0, conversationID) - msgDoc2 = GenMsgDoc(5000, 9999, 10000, 1, conversationID) - if _, err := mongoClient.InsertOne(ctx, msgDoc); err != nil { - t.Error("InsertOne failed", conversationID) - } - if _, err := mongoClient.InsertOne(ctx, msgDoc2); err != nil { - t.Error("InsertOne failed", conversationID) - } - - msgTools.ClearConversationsMsg(ctx, []string{conversationID}) - if err != nil { - t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed") - return - } - minSeqMongo, maxSeqMongo, minSeqCache, maxSeqCache, err = msgTools.msgDatabase.GetConversationMinMaxSeqInMongoAndCache( - ctx, - conversationID, - ) - if err != nil { - t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed") - return - } - if maxSeqCache != maxSeqMongo { - t.Error("checkMaxSeqWithMongo failed", conversationID) - } - if minSeqMongo != minSeqCache { - t.Error("minSeqMongo != minSeqCache", minSeqMongo, minSeqCache) - } - if minSeqCache != 10000 { - t.Error("test1 is not the same", "minSeq:", minSeqCache) - } - - testUID6 := "test_del_id6" - conversationID = utils.GetConversationIDBySessionType(constant.SuperGroupChatType, testUID6) - - _, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(0)}) - if err != nil { - t.Error("delete failed") - } - _, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(1)}) - if err != nil { - t.Error("delete failed") - } - _, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(2)}) - if err != nil { - t.Error("delete failed") - } - _, err = mongoClient.DeleteOne(ctx, bson.M{"uid": conversationID + ":" + strconv.Itoa(3)}) - if err != nil { - t.Error("delete failed") - } - msgDoc = GenMsgDoc(1, 4999, 5000, 0, conversationID) - msgDoc2 = GenMsgDoc(5000, 9999, 10000, 1, conversationID) - msgDoc3 = GenMsgDoc(10000, 14999, 13000, 2, conversationID) - msgDoc4 := GenMsgDoc(15000, 19999, 0, 3, conversationID) - if _, err := mongoClient.InsertOne(ctx, msgDoc); err != nil { - t.Error("InsertOne failed", testUID4) - } - if _, err := mongoClient.InsertOne(ctx, msgDoc2); err != nil { - t.Error("InsertOne failed", testUID4) - } - if _, err := mongoClient.InsertOne(ctx, msgDoc3); err != nil { - t.Error("InsertOne failed", testUID4) - } - if _, err := mongoClient.InsertOne(ctx, msgDoc4); err != nil { - t.Error("InsertOne failed", testUID4) - } - minSeqMongo, maxSeqMongo, minSeqCache, maxSeqCache, err = msgTools.msgDatabase.GetConversationMinMaxSeqInMongoAndCache( - ctx, - conversationID, - ) - if err != nil { - t.Error("GetSuperGroupMinMaxSeqInMongoAndCache failed") - return - } - if maxSeqCache != maxSeqMongo { - t.Error("checkMaxSeqWithMongo failed", conversationID) - } - if minSeqMongo != minSeqCache { - t.Error("minSeqMongo != minSeqCache", minSeqMongo, minSeqCache) - } - if minSeqCache != 13001 { - t.Error("test1 is not the same", "minSeq:", minSeqCache) - } -} diff --git a/pkg/authverify/token.go b/pkg/authverify/token.go new file mode 100644 index 000000000..0d26b6b04 --- /dev/null +++ b/pkg/authverify/token.go @@ -0,0 +1,62 @@ +package authverify + +import ( + "context" + "fmt" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/OpenIMSDK/tools/errs" + "github.com/OpenIMSDK/tools/mcontext" + "github.com/OpenIMSDK/tools/tokenverify" + "github.com/OpenIMSDK/tools/utils" + "github.com/golang-jwt/jwt/v4" +) + +func Secret() jwt.Keyfunc { + return func(token *jwt.Token) (interface{}, error) { + return []byte(config.Config.Secret), nil + } +} + +func CheckAccessV3(ctx context.Context, ownerUserID string) (err error) { + opUserID := mcontext.GetOpUserID(ctx) + if utils.IsContain(opUserID, config.Config.Manager.UserID) { + return nil + } + if opUserID == ownerUserID { + return nil + } + return errs.ErrNoPermission.Wrap(utils.GetSelfFuncName()) +} + +func IsAppManagerUid(ctx context.Context) bool { + return utils.IsContain(mcontext.GetOpUserID(ctx), config.Config.Manager.UserID) +} + +func CheckAdmin(ctx context.Context) error { + if utils.IsContain(mcontext.GetOpUserID(ctx), config.Config.Manager.UserID) { + return nil + } + return errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s is not admin userID", mcontext.GetOpUserID(ctx))) +} + +func ParseRedisInterfaceToken(redisToken interface{}) (*tokenverify.Claims, error) { + return tokenverify.GetClaimFromToken(string(redisToken.([]uint8)), Secret()) +} + +func IsManagerUserID(opUserID string) bool { + return utils.IsContain(opUserID, config.Config.Manager.UserID) +} + +func WsVerifyToken(token, userID string, platformID int) error { + claim, err := tokenverify.GetClaimFromToken(token, Secret()) + if err != nil { + return err + } + if claim.UserID != userID { + return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token uid %s != userID %s", claim.UserID, userID)) + } + if claim.PlatformID != platformID { + return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %d != %d", claim.PlatformID, platformID)) + } + return nil +} diff --git a/pkg/common/cmd/msg_gateway.go b/pkg/common/cmd/msg_gateway.go index f7b7a03ea..aae63351d 100644 --- a/pkg/common/cmd/msg_gateway.go +++ b/pkg/common/cmd/msg_gateway.go @@ -19,7 +19,7 @@ import ( //"github.com/OpenIMSDK/Open-IM-Server/internal/msggateway". "github.com/spf13/cobra" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" ) type MsgGatewayCmd struct { diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index 718d9bf7b..39865b9f8 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -20,8 +20,8 @@ import ( "github.com/spf13/cobra" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/log" ) @@ -65,7 +65,7 @@ func NewRootCmd(name string, opts ...func(*CmdOpts)) (rootCmd *RootCmd) { if cmdOpts.loggerPrefixName == "" { cmdOpts.loggerPrefixName = "OpenIM.log.all" } - if err := log.InitFromConfig(cmdOpts.loggerPrefixName, name, config.Config.Log.RemainLogLevel, config.Config.Log.IsStdout, config.Config.Log.IsJson, config.Config.Log.StorageLocation, config.Config.Log.RemainRotationCount); err != nil { + if err := log.InitFromConfig(cmdOpts.loggerPrefixName, name, config.Config.Log.RemainLogLevel, config.Config.Log.IsStdout, config.Config.Log.IsJson, config.Config.Log.StorageLocation, config.Config.Log.RemainRotationCount, config.Config.Log.RotationTime); err != nil { panic(err) } return nil diff --git a/pkg/common/cmd/rpc.go b/pkg/common/cmd/rpc.go index a2fbea34c..9a00353be 100644 --- a/pkg/common/cmd/rpc.go +++ b/pkg/common/cmd/rpc.go @@ -20,8 +20,8 @@ import ( "github.com/spf13/cobra" "google.golang.org/grpc" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/startrpc" "github.com/OpenIMSDK/tools/discoveryregistry" - "github.com/OpenIMSDK/tools/startrpc" ) type RpcCmd struct { diff --git a/pkg/common/config/config.go b/pkg/common/config/config.go new file mode 100644 index 000000000..1e0bbba16 --- /dev/null +++ b/pkg/common/config/config.go @@ -0,0 +1,333 @@ +// Copyright © 2023 OpenIM. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package config + +import ( + "bytes" + "github.com/OpenIMSDK/tools/discoveryregistry" + "gopkg.in/yaml.v3" +) + +var Config configStruct + +const ConfKey = "conf" + +type CallBackConfig struct { + Enable bool `yaml:"enable"` + CallbackTimeOut int `yaml:"timeout"` + CallbackFailedContinue *bool `yaml:"failedContinue"` +} + +type NotificationConf struct { + IsSendMsg bool `yaml:"isSendMsg"` + ReliabilityLevel int `yaml:"reliabilityLevel"` // 1 online 2 persistent + UnreadCount bool `yaml:"unreadCount"` + OfflinePush POfflinePush `yaml:"offlinePush"` +} + +type POfflinePush struct { + Enable bool `yaml:"enable"` + Title string `yaml:"title"` + Desc string `yaml:"desc"` + Ext string `yaml:"ext"` +} + +type configStruct struct { + Zookeeper struct { + Schema string `yaml:"schema"` + ZkAddr []string `yaml:"address"` + Username string `yaml:"username"` + Password string `yaml:"password"` + } `yaml:"zookeeper"` + + Mysql struct { + Address []string `yaml:"address"` + Username string `yaml:"username"` + Password string `yaml:"password"` + Database string `yaml:"database"` + MaxOpenConn int `yaml:"maxOpenConn"` + MaxIdleConn int `yaml:"maxIdleConn"` + MaxLifeTime int `yaml:"maxLifeTime"` + LogLevel int `yaml:"logLevel"` + SlowThreshold int `yaml:"slowThreshold"` + } `yaml:"mysql"` + + Mongo struct { + Uri string `yaml:"uri"` + Address []string `yaml:"address"` + Database string `yaml:"database"` + Username string `yaml:"username"` + Password string `yaml:"password"` + MaxPoolSize int `yaml:"maxPoolSize"` + } `yaml:"mongo"` + + Redis struct { + Address []string `yaml:"address"` + Username string `yaml:"username"` + Password string `yaml:"password"` + } `yaml:"redis"` + + Kafka struct { + Username string `yaml:"username"` + Password string `yaml:"password"` + Addr []string `yaml:"addr"` + LatestMsgToRedis struct { + Topic string `yaml:"topic"` + } `yaml:"latestMsgToRedis"` + MsgToMongo struct { + Topic string `yaml:"topic"` + } `yaml:"offlineMsgToMongo"` + MsgToPush struct { + Topic string `yaml:"topic"` + } `yaml:"msgToPush"` + ConsumerGroupID struct { + MsgToRedis string `yaml:"msgToRedis"` + MsgToMongo string `yaml:"msgToMongo"` + MsgToMySql string `yaml:"msgToMySql"` + MsgToPush string `yaml:"msgToPush"` + } `yaml:"consumerGroupID"` + } `yaml:"kafka"` + + Rpc struct { + RegisterIP string `yaml:"registerIP"` + ListenIP string `yaml:"listenIP"` + } `yaml:"rpc"` + + Api struct { + OpenImApiPort []int `yaml:"openImApiPort"` + ListenIP string `yaml:"listenIP"` + } `yaml:"api"` + + Object struct { + Enable string `yaml:"enable"` + ApiURL string `yaml:"apiURL"` + Minio struct { + Bucket string `yaml:"bucket"` + Endpoint string `yaml:"endpoint"` + AccessKeyID string `yaml:"accessKeyID"` + SecretAccessKey string `yaml:"secretAccessKey"` + SessionToken string `yaml:"sessionToken"` + } `yaml:"minio"` + Cos struct { + BucketURL string `yaml:"bucketURL"` + SecretID string `yaml:"secretID"` + SecretKey string `yaml:"secretKey"` + SessionToken string `yaml:"sessionToken"` + } `yaml:"cos"` + Oss struct { + Endpoint string `yaml:"endpoint"` + Bucket string `yaml:"bucket"` + BucketURL string `yaml:"bucketURL"` + AccessKeyID string `yaml:"accessKeyID"` + AccessKeySecret string `yaml:"accessKeySecret"` + SessionToken string `yaml:"sessionToken"` + } `yaml:"oss"` + } `yaml:"object"` + + RpcPort struct { + OpenImUserPort []int `yaml:"openImUserPort"` + OpenImFriendPort []int `yaml:"openImFriendPort"` + OpenImMessagePort []int `yaml:"openImMessagePort"` + OpenImMessageGatewayPort []int `yaml:"openImMessageGatewayPort"` + OpenImGroupPort []int `yaml:"openImGroupPort"` + OpenImAuthPort []int `yaml:"openImAuthPort"` + OpenImPushPort []int `yaml:"openImPushPort"` + OpenImConversationPort []int `yaml:"openImConversationPort"` + OpenImRtcPort []int `yaml:"openImRtcPort"` + OpenImThirdPort []int `yaml:"openImThirdPort"` + } `yaml:"rpcPort"` + + RpcRegisterName struct { + OpenImUserName string `yaml:"openImUserName"` + OpenImFriendName string `yaml:"openImFriendName"` + OpenImMsgName string `yaml:"openImMsgName"` + OpenImPushName string `yaml:"openImPushName"` + OpenImMessageGatewayName string `yaml:"openImMessageGatewayName"` + OpenImGroupName string `yaml:"openImGroupName"` + OpenImAuthName string `yaml:"openImAuthName"` + OpenImConversationName string `yaml:"openImConversationName"` + OpenImThirdName string `yaml:"openImThirdName"` + } `yaml:"rpcRegisterName"` + + Log struct { + StorageLocation string `yaml:"storageLocation"` + RotationTime uint `yaml:"rotationTime"` + RemainRotationCount uint `yaml:"remainRotationCount"` + RemainLogLevel int `yaml:"remainLogLevel"` + IsStdout bool `yaml:"isStdout"` + IsJson bool `yaml:"isJson"` + WithStack bool `yaml:"withStack"` + } `yaml:"log"` + + LongConnSvr struct { + OpenImWsPort []int `yaml:"openImWsPort"` + WebsocketMaxConnNum int `yaml:"websocketMaxConnNum"` + WebsocketMaxMsgLen int `yaml:"websocketMaxMsgLen"` + WebsocketTimeout int `yaml:"websocketTimeout"` + } `yaml:"longConnSvr"` + + Push struct { + Enable string `yaml:"enable"` + GeTui struct { + PushUrl string `yaml:"pushUrl"` + AppKey string `yaml:"appKey"` + Intent string `yaml:"intent"` + MasterSecret string `yaml:"masterSecret"` + ChannelID string `yaml:"channelID"` + ChannelName string `yaml:"channelName"` + } `yaml:"geTui"` + Fcm struct { + ServiceAccount string `yaml:"serviceAccount"` + } `yaml:"fcm"` + Jpns struct { + AppKey string `yaml:"appKey"` + MasterSecret string `yaml:"masterSecret"` + PushUrl string `yaml:"pushUrl"` + PushIntent string `yaml:"pushIntent"` + } `yaml:"jpns"` + } + Manager struct { + UserID []string `yaml:"userID"` + Nickname []string `yaml:"nickname"` + } `yaml:"manager"` + + MultiLoginPolicy int `yaml:"multiLoginPolicy"` + ChatPersistenceMysql bool `yaml:"chatPersistenceMysql"` + MsgCacheTimeout int `yaml:"msgCacheTimeout"` + GroupMessageHasReadReceiptEnable bool `yaml:"groupMessageHasReadReceiptEnable"` + SingleMessageHasReadReceiptEnable bool `yaml:"singleMessageHasReadReceiptEnable"` + RetainChatRecords int `yaml:"retainChatRecords"` + ChatRecordsClearTime string `yaml:"chatRecordsClearTime"` + MsgDestructTime string `yaml:"msgDestructTime"` + Secret string `yaml:"secret"` + TokenPolicy struct { + Expire int64 `yaml:"expire"` + } `yaml:"tokenPolicy"` + MessageVerify struct { + FriendVerify *bool `yaml:"friendVerify"` + } `yaml:"messageVerify"` + + IOSPush struct { + PushSound string `yaml:"pushSound"` + BadgeCount bool `yaml:"badgeCount"` + Production bool `yaml:"production"` + } `yaml:"iosPush"` + Callback struct { + CallbackUrl string `yaml:"url"` + CallbackBeforeSendSingleMsg CallBackConfig `yaml:"beforeSendSingleMsg"` + CallbackAfterSendSingleMsg CallBackConfig `yaml:"afterSendSingleMsg"` + CallbackBeforeSendGroupMsg CallBackConfig `yaml:"beforeSendGroupMsg"` + CallbackAfterSendGroupMsg CallBackConfig `yaml:"afterSendGroupMsg"` + CallbackMsgModify CallBackConfig `yaml:"msgModify"` + CallbackUserOnline CallBackConfig `yaml:"userOnline"` + CallbackUserOffline CallBackConfig `yaml:"userOffline"` + CallbackUserKickOff CallBackConfig `yaml:"userKickOff"` + CallbackOfflinePush CallBackConfig `yaml:"offlinePush"` + CallbackOnlinePush CallBackConfig `yaml:"onlinePush"` + CallbackBeforeSuperGroupOnlinePush CallBackConfig `yaml:"superGroupOnlinePush"` + CallbackBeforeAddFriend CallBackConfig `yaml:"beforeAddFriend"` + CallbackBeforeCreateGroup CallBackConfig `yaml:"beforeCreateGroup"` + CallbackBeforeMemberJoinGroup CallBackConfig `yaml:"beforeMemberJoinGroup"` + CallbackBeforeSetGroupMemberInfo CallBackConfig `yaml:"beforeSetGroupMemberInfo"` + } `yaml:"callback"` + + Prometheus struct { + Enable bool `yaml:"enable"` + UserPrometheusPort []int `yaml:"userPrometheusPort"` + FriendPrometheusPort []int `yaml:"friendPrometheusPort"` + MessagePrometheusPort []int `yaml:"messagePrometheusPort"` + MessageGatewayPrometheusPort []int `yaml:"messageGatewayPrometheusPort"` + GroupPrometheusPort []int `yaml:"groupPrometheusPort"` + AuthPrometheusPort []int `yaml:"authPrometheusPort"` + PushPrometheusPort []int `yaml:"pushPrometheusPort"` + ConversationPrometheusPort []int `yaml:"conversationPrometheusPort"` + RtcPrometheusPort []int `yaml:"rtcPrometheusPort"` + MessageTransferPrometheusPort []int `yaml:"messageTransferPrometheusPort"` + ThirdPrometheusPort []int `yaml:"thirdPrometheusPort"` + } `yaml:"prometheus"` + Notification notification `yaml:"notification"` +} + +type notification struct { + GroupCreated NotificationConf `yaml:"groupCreated"` + GroupInfoSet NotificationConf `yaml:"groupInfoSet"` + JoinGroupApplication NotificationConf `yaml:"joinGroupApplication"` + MemberQuit NotificationConf `yaml:"memberQuit"` + GroupApplicationAccepted NotificationConf `yaml:"groupApplicationAccepted"` + GroupApplicationRejected NotificationConf `yaml:"groupApplicationRejected"` + GroupOwnerTransferred NotificationConf `yaml:"groupOwnerTransferred"` + MemberKicked NotificationConf `yaml:"memberKicked"` + MemberInvited NotificationConf `yaml:"memberInvited"` + MemberEnter NotificationConf `yaml:"memberEnter"` + GroupDismissed NotificationConf `yaml:"groupDismissed"` + GroupMuted NotificationConf `yaml:"groupMuted"` + GroupCancelMuted NotificationConf `yaml:"groupCancelMuted"` + GroupMemberMuted NotificationConf `yaml:"groupMemberMuted"` + GroupMemberCancelMuted NotificationConf `yaml:"groupMemberCancelMuted"` + GroupMemberInfoSet NotificationConf `yaml:"groupMemberInfoSet"` + GroupMemberSetToAdmin NotificationConf `yaml:"groupMemberSetToAdmin"` + GroupMemberSetToOrdinary NotificationConf `yaml:"groupMemberSetToOrdinaryUser"` + GroupInfoSetAnnouncement NotificationConf `yaml:"groupInfoSetAnnouncement"` + GroupInfoSetName NotificationConf `yaml:"groupInfoSetName"` + ////////////////////////user/////////////////////// + UserInfoUpdated NotificationConf `yaml:"userInfoUpdated"` + //////////////////////friend/////////////////////// + FriendApplicationAdded NotificationConf `yaml:"friendApplicationAdded"` + FriendApplicationApproved NotificationConf `yaml:"friendApplicationApproved"` + FriendApplicationRejected NotificationConf `yaml:"friendApplicationRejected"` + FriendAdded NotificationConf `yaml:"friendAdded"` + FriendDeleted NotificationConf `yaml:"friendDeleted"` + FriendRemarkSet NotificationConf `yaml:"friendRemarkSet"` + BlackAdded NotificationConf `yaml:"blackAdded"` + BlackDeleted NotificationConf `yaml:"blackDeleted"` + FriendInfoUpdated NotificationConf `yaml:"friendInfoUpdated"` + //////////////////////conversation/////////////////////// + ConversationChanged NotificationConf `yaml:"conversationChanged"` + ConversationSetPrivate NotificationConf `yaml:"conversationSetPrivate"` +} + +func (c *configStruct) GetServiceNames() []string { + return []string{ + c.RpcRegisterName.OpenImUserName, + c.RpcRegisterName.OpenImFriendName, + c.RpcRegisterName.OpenImMsgName, + c.RpcRegisterName.OpenImPushName, + c.RpcRegisterName.OpenImMessageGatewayName, + c.RpcRegisterName.OpenImGroupName, + c.RpcRegisterName.OpenImAuthName, + c.RpcRegisterName.OpenImConversationName, + c.RpcRegisterName.OpenImThirdName, + } +} + +func (c *configStruct) RegisterConf2Registry(registry discoveryregistry.SvcDiscoveryRegistry) error { + data, err := yaml.Marshal(c) + if err != nil { + return err + } + return registry.RegisterConf2Registry(ConfKey, data) +} + +func (c *configStruct) GetConfFromRegistry(registry discoveryregistry.SvcDiscoveryRegistry) ([]byte, error) { + return registry.GetConfFromRegistry(ConfKey) +} + +func (c *configStruct) EncodeConfig() []byte { + buf := bytes.NewBuffer(nil) + if err := yaml.NewEncoder(buf).Encode(c); err != nil { + panic(err) + } + return buf.Bytes() +} diff --git a/pkg/common/config/parse.go b/pkg/common/config/parse.go index f31df39d2..e3fd99b9f 100644 --- a/pkg/common/config/parse.go +++ b/pkg/common/config/parse.go @@ -17,23 +17,18 @@ package config import ( _ "embed" "fmt" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/msgprocessor" "os" "path/filepath" "runtime" "gopkg.in/yaml.v3" - "github.com/OpenIMSDK/tools/constant" - "github.com/OpenIMSDK/tools/utils" + "github.com/OpenIMSDK/protocol/constant" ) //go:embed version -var version string - -func init() { - config.Version = version -} +var Version string var ( _, b, _, _ = runtime.Caller(0) @@ -47,20 +42,20 @@ const ( DefaultFolderPath = "../config/" ) -func GetOptionsByNotification(cfg config.NotificationConf) utils.Options { - opts := utils.NewOptions() +func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options { + opts := msgprocessor.NewOptions() if cfg.UnreadCount { - opts = utils.WithOptions(opts, utils.WithUnreadCount(true)) + opts = msgprocessor.WithOptions(opts, msgprocessor.WithUnreadCount(true)) } if cfg.OfflinePush.Enable { - opts = utils.WithOptions(opts, utils.WithOfflinePush(true)) + opts = msgprocessor.WithOptions(opts, msgprocessor.WithOfflinePush(true)) } switch cfg.ReliabilityLevel { case constant.UnreliableNotification: case constant.ReliableNotificationNoMsg: - opts = utils.WithOptions(opts, utils.WithHistory(true), utils.WithPersistent()) + opts = msgprocessor.WithOptions(opts, msgprocessor.WithHistory(true), msgprocessor.WithPersistent()) } - opts = utils.WithOptions(opts, utils.WithSendMsg(cfg.IsSendMsg)) + opts = msgprocessor.WithOptions(opts, msgprocessor.WithSendMsg(cfg.IsSendMsg)) return opts } @@ -92,11 +87,11 @@ func initConfig(config interface{}, configName, configFolderPath string) error { } func InitConfig(configFolderPath string) error { - err := initConfig(&config.Config, FileName, configFolderPath) + err := initConfig(&Config, FileName, configFolderPath) if err != nil { return err } - err = initConfig(&config.Config.Notification, NotificationFileName, configFolderPath) + err = initConfig(&Config.Notification, NotificationFileName, configFolderPath) if err != nil { return err } diff --git a/pkg/common/db/cache/init_redis.go b/pkg/common/db/cache/init_redis.go index 307e40327..69bc0d484 100644 --- a/pkg/common/db/cache/init_redis.go +++ b/pkg/common/db/cache/init_redis.go @@ -22,7 +22,7 @@ import ( "github.com/redis/go-redis/v9" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/mw/specialerror" ) diff --git a/pkg/common/db/cache/msg.go b/pkg/common/db/cache/msg.go index 66f3cacee..953396fbe 100644 --- a/pkg/common/db/cache/msg.go +++ b/pkg/common/db/cache/msg.go @@ -16,6 +16,7 @@ package cache import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/msgprocessor" "strconv" "time" @@ -25,10 +26,10 @@ import ( "github.com/gogo/protobuf/jsonpb" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/utils" @@ -374,7 +375,7 @@ func (c *msgCache) GetMessagesBySeq( failedSeqs = append(failedSeqs, seqs[i]) } else { msg := sdkws.MsgData{} - err = utils.String2Pb(cmd.Val(), &msg) + err = msgprocessor.String2Pb(cmd.Val(), &msg) if err == nil { if msg.Status != constant.MsgDeleted { seqMsgs = append(seqMsgs, &msg) @@ -394,7 +395,7 @@ func (c *msgCache) SetMessageToCache(ctx context.Context, conversationID string, var failedMsgs []*sdkws.MsgData for _, msg := range msgs { key := c.getMessageCacheKey(conversationID, msg.Seq) - s, err := utils.Pb2String(msg) + s, err := msgprocessor.Pb2String(msg) if err != nil { return 0, errs.Wrap(err) } @@ -535,7 +536,7 @@ func (c *msgCache) DelMsgFromCache(ctx context.Context, userID string, seqs []in return err } msg.Status = constant.MsgDeleted - s, err := utils.Pb2String(&msg) + s, err := msgprocessor.Pb2String(&msg) if err != nil { return errs.Wrap(err) } diff --git a/pkg/common/db/controller/auth.go b/pkg/common/db/controller/auth.go index 491a1394b..d864e078b 100644 --- a/pkg/common/db/controller/auth.go +++ b/pkg/common/db/controller/auth.go @@ -16,11 +16,12 @@ package controller import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" "github.com/golang-jwt/jwt/v4" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/tokenverify" "github.com/OpenIMSDK/tools/utils" ) @@ -60,7 +61,7 @@ func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformI } var deleteTokenKey []string for k, v := range tokens { - _, err = tokenverify.GetClaimFromToken(k) + _, err = tokenverify.GetClaimFromToken(k, authverify.Secret()) if err != nil || v != constant.NormalToken { deleteTokenKey = append(deleteTokenKey, k) } diff --git a/pkg/common/db/controller/conversation.go b/pkg/common/db/controller/conversation.go index 0f3403084..2829f963e 100644 --- a/pkg/common/db/controller/conversation.go +++ b/pkg/common/db/controller/conversation.go @@ -16,11 +16,12 @@ package controller import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/msgprocessor" "time" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/tx" "github.com/OpenIMSDK/tools/utils" @@ -236,7 +237,7 @@ func (c *conversationDatabase) FindRecvMsgNotNotifyUserIDs(ctx context.Context, func (c *conversationDatabase) CreateGroupChatConversation(ctx context.Context, groupID string, userIDs []string) error { cache := c.cache.NewCache() - conversationID := utils.GetConversationIDBySessionType(constant.SuperGroupChatType, groupID) + conversationID := msgprocessor.GetConversationIDBySessionType(constant.SuperGroupChatType, groupID) if err := c.tx.Transaction(func(tx any) error { existConversationUserIDs, err := c.conversationDB.FindUserID(ctx, userIDs, []string{conversationID}) if err != nil { diff --git a/pkg/common/db/controller/friend.go b/pkg/common/db/controller/friend.go index 3c8d61a40..c44c1662e 100644 --- a/pkg/common/db/controller/friend.go +++ b/pkg/common/db/controller/friend.go @@ -22,7 +22,7 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/mcontext" "github.com/OpenIMSDK/tools/tx" diff --git a/pkg/common/db/controller/group.go b/pkg/common/db/controller/group.go index ad0b4e548..af97d87ca 100644 --- a/pkg/common/db/controller/group.go +++ b/pkg/common/db/controller/group.go @@ -29,7 +29,7 @@ import ( relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/tx" "github.com/OpenIMSDK/tools/utils" ) diff --git a/pkg/common/db/controller/msg.go b/pkg/common/db/controller/msg.go index 555828c22..7c03eb8b6 100644 --- a/pkg/common/db/controller/msg.go +++ b/pkg/common/db/controller/msg.go @@ -21,13 +21,13 @@ import ( "github.com/redis/go-redis/v9" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/convert" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache" unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/kafka" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome" - "github.com/OpenIMSDK/tools/config" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" diff --git a/pkg/common/db/controller/msg_test.go b/pkg/common/db/controller/msg_test.go index e322e6218..73226fa1b 100644 --- a/pkg/common/db/controller/msg_test.go +++ b/pkg/common/db/controller/msg_test.go @@ -27,9 +27,9 @@ import ( "go.mongodb.org/mongo-driver/bson" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" unRelationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/unrelation" - "github.com/OpenIMSDK/tools/config" ) func Test_BatchInsertChat2DB(t *testing.T) { @@ -203,7 +203,7 @@ func Test_Revoke(t *testing.T) { } func Test_FindBySeq(t *testing.T) { - if err := log.InitFromConfig("", "", 6, true, false, "", 2); err != nil { + if err := log.InitFromConfig("", "", 6, true, false, "", 2, 1); err != nil { t.Fatal(err) } db := GetDB() diff --git a/pkg/common/db/relation/chat_log_model.go b/pkg/common/db/relation/chat_log_model.go index 056295e15..76ed9be62 100644 --- a/pkg/common/db/relation/chat_log_model.go +++ b/pkg/common/db/relation/chat_log_model.go @@ -21,9 +21,9 @@ import ( "gorm.io/gorm" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" + "github.com/OpenIMSDK/protocol/constant" pbMsg "github.com/OpenIMSDK/protocol/msg" sdkws "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/utils" ) diff --git a/pkg/common/db/relation/conversation_model.go b/pkg/common/db/relation/conversation_model.go index 716d2cbe4..0efca79aa 100644 --- a/pkg/common/db/relation/conversation_model.go +++ b/pkg/common/db/relation/conversation_model.go @@ -20,7 +20,7 @@ import ( "gorm.io/gorm" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/utils" ) diff --git a/pkg/common/db/relation/group_member_model.go b/pkg/common/db/relation/group_member_model.go index 7c2ded83f..da6a58398 100644 --- a/pkg/common/db/relation/group_member_model.go +++ b/pkg/common/db/relation/group_member_model.go @@ -20,7 +20,7 @@ import ( "gorm.io/gorm" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/ormutil" "github.com/OpenIMSDK/tools/utils" ) diff --git a/pkg/common/db/relation/group_model.go b/pkg/common/db/relation/group_model.go index c95d51861..697427e04 100644 --- a/pkg/common/db/relation/group_model.go +++ b/pkg/common/db/relation/group_model.go @@ -18,7 +18,7 @@ import ( "context" "time" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" "gorm.io/gorm" diff --git a/pkg/common/db/relation/mysql_init.go b/pkg/common/db/relation/mysql_init.go index 29873cc89..9b8188ac4 100644 --- a/pkg/common/db/relation/mysql_init.go +++ b/pkg/common/db/relation/mysql_init.go @@ -21,7 +21,7 @@ import ( mysqlDriver "github.com/go-sql-driver/mysql" "gorm.io/driver/mysql" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mw/specialerror" diff --git a/pkg/common/db/s3/cos/cos.go b/pkg/common/db/s3/cos/cos.go index 29d74ceb4..cfdaaa5e4 100644 --- a/pkg/common/db/s3/cos/cos.go +++ b/pkg/common/db/s3/cos/cos.go @@ -26,8 +26,8 @@ import ( "github.com/tencentyun/cos-go-sdk-v5" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/s3" - "github.com/OpenIMSDK/tools/config" ) const ( diff --git a/pkg/common/db/s3/minio/minio.go b/pkg/common/db/s3/minio/minio.go index d5ac7de40..bb6e6ce24 100644 --- a/pkg/common/db/s3/minio/minio.go +++ b/pkg/common/db/s3/minio/minio.go @@ -29,8 +29,8 @@ import ( "github.com/minio/minio-go/v7/pkg/credentials" "github.com/minio/minio-go/v7/pkg/signer" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/s3" - "github.com/OpenIMSDK/tools/config" ) const ( diff --git a/pkg/common/db/s3/oss/oss.go b/pkg/common/db/s3/oss/oss.go index ee4d19464..774150389 100644 --- a/pkg/common/db/s3/oss/oss.go +++ b/pkg/common/db/s3/oss/oss.go @@ -26,8 +26,8 @@ import ( "github.com/aliyun/aliyun-oss-go-sdk/oss" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/s3" - "github.com/OpenIMSDK/tools/config" ) const ( diff --git a/pkg/common/db/unrelation/mongo.go b/pkg/common/db/unrelation/mongo.go index 44077a838..b580f4dbd 100644 --- a/pkg/common/db/unrelation/mongo.go +++ b/pkg/common/db/unrelation/mongo.go @@ -24,8 +24,8 @@ import ( "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/unrelation" - "github.com/OpenIMSDK/tools/config" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/mw/specialerror" "github.com/OpenIMSDK/tools/utils" diff --git a/pkg/common/db/unrelation/msg.go b/pkg/common/db/unrelation/msg.go index 2f4a166e9..27f7cf5ce 100644 --- a/pkg/common/db/unrelation/msg.go +++ b/pkg/common/db/unrelation/msg.go @@ -23,7 +23,7 @@ import ( "github.com/OpenIMSDK/protocol/msg" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/protocol/constant" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" diff --git a/pkg/common/http/http_client.go b/pkg/common/http/http_client.go index 46d5f23c3..a646f774e 100644 --- a/pkg/common/http/http_client.go +++ b/pkg/common/http/http_client.go @@ -25,8 +25,8 @@ import ( "time" "github.com/OpenIMSDK/Open-IM-Server/pkg/callbackstruct" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" ) diff --git a/pkg/common/kafka/consumer.go b/pkg/common/kafka/consumer.go index e04abe944..67bc3977b 100644 --- a/pkg/common/kafka/consumer.go +++ b/pkg/common/kafka/consumer.go @@ -17,7 +17,7 @@ package kafka import ( "sync" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/Shopify/sarama" ) diff --git a/pkg/common/kafka/producer.go b/pkg/common/kafka/producer.go index 8eeaa6825..754f2166d 100644 --- a/pkg/common/kafka/producer.go +++ b/pkg/common/kafka/producer.go @@ -19,8 +19,8 @@ import ( "errors" "time" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/OpenIMSDK/protocol/constant" log "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mcontext" "github.com/OpenIMSDK/tools/utils" diff --git a/pkg/common/prome/prometheus.go b/pkg/common/prome/prometheus.go index 26efb9dae..d66b1df70 100644 --- a/pkg/common/prome/prometheus.go +++ b/pkg/common/prome/prometheus.go @@ -19,7 +19,7 @@ import ( "net/http" "strconv" - "github.com/OpenIMSDK/tools/config" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/gin-gonic/gin" "github.com/prometheus/client_golang/prometheus" diff --git a/pkg/common/startrpc/start.go b/pkg/common/startrpc/start.go new file mode 100644 index 000000000..d78aa1ff4 --- /dev/null +++ b/pkg/common/startrpc/start.go @@ -0,0 +1,112 @@ +// Copyright © 2023 OpenIM. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package startrpc + +import ( + "fmt" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "net" + "strconv" + "time" + + grpcPrometheus "github.com/grpc-ecosystem/go-grpc-prometheus" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + + "github.com/OpenIMSDK/tools/discoveryregistry" + "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper" + "github.com/OpenIMSDK/tools/log" + "github.com/OpenIMSDK/tools/mw" + "github.com/OpenIMSDK/tools/network" + "github.com/OpenIMSDK/tools/prome" + "github.com/OpenIMSDK/tools/utils" +) + +func Start( + rpcPort int, + rpcRegisterName string, + prometheusPort int, + rpcFn func(client discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error, + options ...grpc.ServerOption, +) error { + fmt.Println( + "start", + rpcRegisterName, + "server, port: ", + rpcPort, + "prometheusPort:", + prometheusPort, + ", OpenIM version: ", + config.Version, + ) + listener, err := net.Listen( + "tcp", + net.JoinHostPort(network.GetListenIP(config.Config.Rpc.ListenIP), strconv.Itoa(rpcPort)), + ) + if err != nil { + return err + } + defer listener.Close() + zkClient, err := zookeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema, + zookeeper.WithFreq(time.Hour), zookeeper.WithUserNameAndPassword( + config.Config.Zookeeper.Username, + config.Config.Zookeeper.Password, + ), zookeeper.WithRoundRobin(), zookeeper.WithTimeout(10), zookeeper.WithLogger(log.NewZkLogger())) + if err != nil { + return utils.Wrap1(err) + } + defer zkClient.CloseZK() + zkClient.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials())) + registerIP, err := network.GetRpcRegisterIP(config.Config.Rpc.RegisterIP) + if err != nil { + return err + } + // ctx 中间件 + if config.Config.Prometheus.Enable { + prome.NewGrpcRequestCounter() + prome.NewGrpcRequestFailedCounter() + prome.NewGrpcRequestSuccessCounter() + unaryInterceptor := mw.InterceptChain(grpcPrometheus.UnaryServerInterceptor, mw.RpcServerInterceptor) + options = append(options, []grpc.ServerOption{ + grpc.StreamInterceptor(grpcPrometheus.StreamServerInterceptor), + grpc.UnaryInterceptor(unaryInterceptor), + }...) + } else { + options = append(options, mw.GrpcServer()) + } + srv := grpc.NewServer(options...) + defer srv.GracefulStop() + err = rpcFn(zkClient, srv) + if err != nil { + return utils.Wrap1(err) + } + err = zkClient.Register( + rpcRegisterName, + registerIP, + rpcPort, + grpc.WithTransportCredentials(insecure.NewCredentials()), + ) + if err != nil { + return utils.Wrap1(err) + } + go func() { + if config.Config.Prometheus.Enable && prometheusPort != 0 { + if err := prome.StartPrometheusSrv(prometheusPort); err != nil { + panic(err.Error()) + } + } + }() + return utils.Wrap1(srv.Serve(listener)) +} diff --git a/pkg/msgprocessor/conversation.go b/pkg/msgprocessor/conversation.go new file mode 100644 index 000000000..b24794af8 --- /dev/null +++ b/pkg/msgprocessor/conversation.go @@ -0,0 +1,158 @@ +package msgprocessor + +import ( + "github.com/OpenIMSDK/protocol/constant" + "github.com/OpenIMSDK/protocol/sdkws" + "google.golang.org/protobuf/proto" + "sort" + "strings" +) + +func GetNotificationConversationID(msg *sdkws.MsgData) string { + switch msg.SessionType { + case constant.SingleChatType: + l := []string{msg.SendID, msg.RecvID} + sort.Strings(l) + return "n_" + strings.Join(l, "_") + case constant.GroupChatType: + return "n_" + msg.GroupID + case constant.SuperGroupChatType: + return "n_" + msg.GroupID + case constant.NotificationChatType: + return "n_" + msg.SendID + "_" + msg.RecvID + } + return "" +} + +func GetChatConversationIDByMsg(msg *sdkws.MsgData) string { + switch msg.SessionType { + case constant.SingleChatType: + l := []string{msg.SendID, msg.RecvID} + sort.Strings(l) + return "si_" + strings.Join(l, "_") + case constant.GroupChatType: + return "g_" + msg.GroupID + case constant.SuperGroupChatType: + return "sg_" + msg.GroupID + case constant.NotificationChatType: + return "sn_" + msg.SendID + "_" + msg.RecvID + } + return "" +} + +func GenConversationUniqueKey(msg *sdkws.MsgData) string { + switch msg.SessionType { + case constant.SingleChatType, constant.NotificationChatType: + l := []string{msg.SendID, msg.RecvID} + sort.Strings(l) + return strings.Join(l, "_") + case constant.SuperGroupChatType: + return msg.GroupID + } + return "" +} + +func GetConversationIDByMsg(msg *sdkws.MsgData) string { + options := Options(msg.Options) + switch msg.SessionType { + case constant.SingleChatType: + l := []string{msg.SendID, msg.RecvID} + sort.Strings(l) + if !options.IsNotNotification() { + return "n_" + strings.Join(l, "_") + } + return "si_" + strings.Join(l, "_") // single chat + case constant.GroupChatType: + if !options.IsNotNotification() { + return "n_" + msg.GroupID // group chat + } + return "g_" + msg.GroupID // group chat + case constant.SuperGroupChatType: + if !options.IsNotNotification() { + return "n_" + msg.GroupID // super group chat + } + return "sg_" + msg.GroupID // super group chat + case constant.NotificationChatType: + if !options.IsNotNotification() { + return "n_" + msg.SendID + "_" + msg.RecvID // super group chat + } + return "sn_" + msg.SendID + "_" + msg.RecvID // server notification chat + } + return "" +} + +func GetConversationIDBySessionType(sessionType int, ids ...string) string { + sort.Strings(ids) + if len(ids) > 2 || len(ids) < 1 { + return "" + } + switch sessionType { + case constant.SingleChatType: + return "si_" + strings.Join(ids, "_") // single chat + case constant.GroupChatType: + return "g_" + ids[0] // group chat + case constant.SuperGroupChatType: + return "sg_" + ids[0] // super group chat + case constant.NotificationChatType: + return "sn_" + ids[0] // server notification chat + } + return "" +} + +func IsNotification(conversationID string) bool { + return strings.HasPrefix(conversationID, "n_") +} + +func IsNotificationByMsg(msg *sdkws.MsgData) bool { + return !Options(msg.Options).IsNotNotification() +} + +func ParseConversationID(msg *sdkws.MsgData) (isNotification bool, conversationID string) { + options := Options(msg.Options) + switch msg.SessionType { + case constant.SingleChatType: + l := []string{msg.SendID, msg.RecvID} + sort.Strings(l) + if !options.IsNotNotification() { + return true, "n_" + strings.Join(l, "_") + } + return false, "si_" + strings.Join(l, "_") // single chat + case constant.SuperGroupChatType: + if !options.IsNotNotification() { + return true, "n_" + msg.GroupID // super group chat + } + return false, "sg_" + msg.GroupID // super group chat + case constant.NotificationChatType: + if !options.IsNotNotification() { + return true, "n_" + msg.SendID + "_" + msg.RecvID // super group chat + } + return false, "sn_" + msg.SendID + "_" + msg.RecvID // server notification chat + } + return false, "" +} + +type MsgBySeq []*sdkws.MsgData + +func (s MsgBySeq) Len() int { + return len(s) +} + +func (s MsgBySeq) Less(i, j int) bool { + return s[i].Seq < s[j].Seq +} + +func (s MsgBySeq) Swap(i, j int) { + s[i], s[j] = s[j], s[i] +} + +func Pb2String(pb proto.Message) (string, error) { + s, err := proto.Marshal(pb) + if err != nil { + return "", err + } + return string(s), nil +} + +func String2Pb(s string, pb proto.Message) error { + return proto.Unmarshal([]byte(s), pb) +} diff --git a/pkg/msgprocessor/options.go b/pkg/msgprocessor/options.go new file mode 100644 index 000000000..920a6a2ea --- /dev/null +++ b/pkg/msgprocessor/options.go @@ -0,0 +1,181 @@ +// Copyright © 2023 OpenIM. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package msgprocessor + +import "github.com/OpenIMSDK/protocol/constant" + +type Options map[string]bool +type OptionsOpt func(Options) + +func NewOptions(opts ...OptionsOpt) Options { + options := make(map[string]bool, 11) + options[constant.IsNotNotification] = false + options[constant.IsSendMsg] = false + options[constant.IsHistory] = false + options[constant.IsPersistent] = false + options[constant.IsOfflinePush] = false + options[constant.IsUnreadCount] = false + options[constant.IsConversationUpdate] = false + options[constant.IsSenderSync] = false + options[constant.IsNotPrivate] = false + options[constant.IsSenderConversationUpdate] = false + options[constant.IsSenderNotificationPush] = false + options[constant.IsReactionFromCache] = false + for _, opt := range opts { + opt(options) + } + return options +} + +func NewMsgOptions() Options { + options := make(map[string]bool, 11) + options[constant.IsOfflinePush] = false + return make(map[string]bool) +} + +func WithOptions(options Options, opts ...OptionsOpt) Options { + for _, opt := range opts { + opt(options) + } + return options +} + +func WithNotNotification(b bool) OptionsOpt { + return func(options Options) { + options[constant.IsNotNotification] = b + } +} + +func WithSendMsg(b bool) OptionsOpt { + return func(options Options) { + options[constant.IsSendMsg] = b + } +} + +func WithHistory(b bool) OptionsOpt { + return func(options Options) { + options[constant.IsHistory] = b + } +} + +func WithPersistent() OptionsOpt { + return func(options Options) { + options[constant.IsPersistent] = true + } +} + +func WithOfflinePush(b bool) OptionsOpt { + return func(options Options) { + options[constant.IsOfflinePush] = b + } +} + +func WithUnreadCount(b bool) OptionsOpt { + return func(options Options) { + options[constant.IsUnreadCount] = b + } +} + +func WithConversationUpdate() OptionsOpt { + return func(options Options) { + options[constant.IsConversationUpdate] = true + } +} + +func WithSenderSync() OptionsOpt { + return func(options Options) { + options[constant.IsSenderSync] = true + } +} + +func WithNotPrivate() OptionsOpt { + return func(options Options) { + options[constant.IsNotPrivate] = true + } +} + +func WithSenderConversationUpdate() OptionsOpt { + return func(options Options) { + options[constant.IsSenderConversationUpdate] = true + } +} + +func WithSenderNotificationPush() OptionsOpt { + return func(options Options) { + options[constant.IsSenderNotificationPush] = true + } +} + +func WithReactionFromCache() OptionsOpt { + return func(options Options) { + options[constant.IsReactionFromCache] = true + } +} + +func (o Options) Is(notification string) bool { + v, ok := o[notification] + if !ok || v { + return true + } + return false +} + +func (o Options) IsNotNotification() bool { + return o.Is(constant.IsNotNotification) +} + +func (o Options) IsSendMsg() bool { + return o.Is(constant.IsSendMsg) +} + +func (o Options) IsHistory() bool { + return o.Is(constant.IsHistory) +} + +func (o Options) IsPersistent() bool { + return o.Is(constant.IsPersistent) +} + +func (o Options) IsOfflinePush() bool { + return o.Is(constant.IsOfflinePush) +} + +func (o Options) IsUnreadCount() bool { + return o.Is(constant.IsUnreadCount) +} + +func (o Options) IsConversationUpdate() bool { + return o.Is(constant.IsConversationUpdate) +} + +func (o Options) IsSenderSync() bool { + return o.Is(constant.IsSenderSync) +} + +func (o Options) IsNotPrivate() bool { + return o.Is(constant.IsNotPrivate) +} + +func (o Options) IsSenderConversationUpdate() bool { + return o.Is(constant.IsSenderConversationUpdate) +} + +func (o Options) IsSenderNotificationPush() bool { + return o.Is(constant.IsSenderNotificationPush) +} + +func (o Options) IsReactionFromCache() bool { + return o.Is(constant.IsReactionFromCache) +} diff --git a/pkg/rpcclient/auth.go b/pkg/rpcclient/auth.go index 9d667f1ad..cbf1324e0 100644 --- a/pkg/rpcclient/auth.go +++ b/pkg/rpcclient/auth.go @@ -19,8 +19,8 @@ import ( "google.golang.org/grpc" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/protocol/auth" - "github.com/OpenIMSDK/tools/config" "github.com/OpenIMSDK/tools/discoveryregistry" ) diff --git a/pkg/rpcclient/conversation.go b/pkg/rpcclient/conversation.go index d4116e9f9..2b7d2c392 100644 --- a/pkg/rpcclient/conversation.go +++ b/pkg/rpcclient/conversation.go @@ -20,8 +20,8 @@ import ( "google.golang.org/grpc" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" pbConversation "github.com/OpenIMSDK/protocol/conversation" - "github.com/OpenIMSDK/tools/config" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/errs" ) diff --git a/pkg/rpcclient/friend.go b/pkg/rpcclient/friend.go index c27f4ef3f..f1aaaa319 100644 --- a/pkg/rpcclient/friend.go +++ b/pkg/rpcclient/friend.go @@ -19,9 +19,9 @@ import ( "google.golang.org/grpc" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/protocol/friend" sdkws "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/config" "github.com/OpenIMSDK/tools/discoveryregistry" ) diff --git a/pkg/rpcclient/group.go b/pkg/rpcclient/group.go index ce624a085..57dcd0fa5 100644 --- a/pkg/rpcclient/group.go +++ b/pkg/rpcclient/group.go @@ -20,10 +20,10 @@ import ( "google.golang.org/grpc" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/group" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/utils" diff --git a/pkg/rpcclient/msg.go b/pkg/rpcclient/msg.go index bfc7bf73f..8e9416af9 100644 --- a/pkg/rpcclient/msg.go +++ b/pkg/rpcclient/msg.go @@ -21,10 +21,10 @@ import ( "google.golang.org/grpc" "google.golang.org/protobuf/proto" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/msg" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/config" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/utils" diff --git a/pkg/rpcclient/notification/conevrsation.go b/pkg/rpcclient/notification/conevrsation.go index ac40eb887..9c8066d00 100644 --- a/pkg/rpcclient/notification/conevrsation.go +++ b/pkg/rpcclient/notification/conevrsation.go @@ -18,8 +18,8 @@ import ( "context" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/constant" ) type ConversationNotificationSender struct { diff --git a/pkg/rpcclient/notification/friend.go b/pkg/rpcclient/notification/friend.go index 05c2b1e6f..ddce86c8c 100644 --- a/pkg/rpcclient/notification/friend.go +++ b/pkg/rpcclient/notification/friend.go @@ -23,9 +23,9 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" relationTb "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" + "github.com/OpenIMSDK/protocol/constant" pbFriend "github.com/OpenIMSDK/protocol/friend" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/constant" ) type FriendNotificationSender struct { diff --git a/pkg/rpcclient/notification/group.go b/pkg/rpcclient/notification/group.go index e70bf38e1..fdf77500f 100644 --- a/pkg/rpcclient/notification/group.go +++ b/pkg/rpcclient/notification/group.go @@ -21,9 +21,9 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/controller" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" + "github.com/OpenIMSDK/protocol/constant" pbGroup "github.com/OpenIMSDK/protocol/group" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/constant" "github.com/OpenIMSDK/tools/errs" "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mcontext" diff --git a/pkg/rpcclient/notification/msg.go b/pkg/rpcclient/notification/msg.go index f9cf68bcb..ba5aef684 100644 --- a/pkg/rpcclient/notification/msg.go +++ b/pkg/rpcclient/notification/msg.go @@ -18,8 +18,8 @@ import ( "context" "github.com/OpenIMSDK/Open-IM-Server/pkg/rpcclient" + "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/sdkws" - "github.com/OpenIMSDK/tools/constant" ) type MsgNotificationSender struct { diff --git a/pkg/rpcclient/push.go b/pkg/rpcclient/push.go index a4b122a29..4401221c3 100644 --- a/pkg/rpcclient/push.go +++ b/pkg/rpcclient/push.go @@ -19,8 +19,8 @@ import ( "google.golang.org/grpc" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/protocol/push" - "github.com/OpenIMSDK/tools/config" "github.com/OpenIMSDK/tools/discoveryregistry" ) diff --git a/pkg/rpcclient/third.go b/pkg/rpcclient/third.go index 9822f48fa..c01ab4d0c 100644 --- a/pkg/rpcclient/third.go +++ b/pkg/rpcclient/third.go @@ -23,8 +23,8 @@ import ( "google.golang.org/grpc" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/protocol/third" - "github.com/OpenIMSDK/tools/config" "github.com/OpenIMSDK/tools/discoveryregistry" ) diff --git a/pkg/rpcclient/user.go b/pkg/rpcclient/user.go index 7e5079f8c..1ce4fd53c 100644 --- a/pkg/rpcclient/user.go +++ b/pkg/rpcclient/user.go @@ -16,16 +16,16 @@ package rpcclient import ( "context" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" "strings" "google.golang.org/grpc" + "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" "github.com/OpenIMSDK/protocol/sdkws" "github.com/OpenIMSDK/protocol/user" - "github.com/OpenIMSDK/tools/config" "github.com/OpenIMSDK/tools/discoveryregistry" "github.com/OpenIMSDK/tools/errs" - "github.com/OpenIMSDK/tools/tokenverify" "github.com/OpenIMSDK/tools/utils" ) @@ -144,7 +144,7 @@ func (u *UserRpcClient) Access(ctx context.Context, ownerUserID string) error { if err != nil { return err } - return tokenverify.CheckAccessV3(ctx, ownerUserID) + return authverify.CheckAccessV3(ctx, ownerUserID) } func (u *UserRpcClient) GetAllUserIDs(ctx context.Context, pageNumber, showNumber int32) ([]string, error) { From 8a77306de7aa319a8030cdb8f41f4baad72da187 Mon Sep 17 00:00:00 2001 From: Xinwei Xiong <3293172751@qq.com> Date: Thu, 27 Jul 2023 12:31:06 +0800 Subject: [PATCH 06/11] feat: add openim log (#679) * feat: add openim log Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * feat: add openim install scripts Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * fix: config env command Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * fix: config env command Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> --------- Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> --- .goreleaser.yaml | 37 +-- Makefile | 10 + config/config.yaml | 20 +- install.sh | 560 ++++++++++++++++++++++++++++++++ scripts/advertise.sh | 122 +++++++ scripts/make-rules/copyright.mk | 6 + scripts/make-rules/golang.mk | 6 + scripts/style_info.sh | 2 + 8 files changed, 733 insertions(+), 30 deletions(-) create mode 100755 install.sh create mode 100755 scripts/advertise.sh diff --git a/.goreleaser.yaml b/.goreleaser.yaml index cbb9ba483..996a53745 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -343,11 +343,11 @@ dockers: use: buildx build_flag_templates: - "--pull" - - "--label=io.artifacthub.package.readme-url=https://raw.githubusercontent.com/goreleaser/goreleaser/main/README.md" - - "--label=io.artifacthub.package.logo-url=https://goreleaser.com/static/avatar.png" - - '--label=io.artifacthub.package.maintainers=[{"name":"Carlos Alexandro Becker","email":"carlos@carlosbecker.dev"}]' - - "--label=io.artifacthub.package.license=MIT" - - "--label=org.opencontainers.image.description=Deliver Go binaries as fast and easily as possible" + - "--label=io.artifacthub.package.readme-url=https://raw.githubusercontent.com/OpenIMSDK/Open-IM-Server/main/README.md" + - "--label=io.artifacthub.package.logo-url=hhttps://github.com/OpenIMSDK/Open-IM-Server/blob/main/assets/logo/openim-logo-green.png" + - '--label=io.artifacthub.package.maintainers=[{"name":"Xinwei Xiong","email":"3293172751nss@gmail.com"}]' + - "--label=io.artifacthub.package.license=Apace-2.0" + - "--label=org.opencontainers.image.description=OpenIM Open source top instant messaging system" - "--label=org.opencontainers.image.created={{.Date}}" - "--label=org.opencontainers.image.name={{.ProjectName}}" - "--label=org.opencontainers.image.revision={{.FullCommit}}" @@ -363,11 +363,11 @@ dockers: use: buildx build_flag_templates: - "--pull" - - "--label=io.artifacthub.package.readme-url=https://raw.githubusercontent.com/goreleaser/goreleaser/main/README.md" - - "--label=io.artifacthub.package.logo-url=https://goreleaser.com/static/avatar.png" - - '--label=io.artifacthub.package.maintainers=[{"name":"Carlos Alexandro Becker","email":"carlos@carlosbecker.dev"}]' - - "--label=io.artifacthub.package.license=MIT" - - "--label=org.opencontainers.image.description=Deliver Go binaries as fast and easily as possible" + - "--label=io.artifacthub.package.readme-url=https://raw.githubusercontent.com/OpenIMSDK/Open-IM-Server/main/README.md" + - "--label=io.artifacthub.package.logo-url=hhttps://github.com/OpenIMSDK/Open-IM-Server/blob/main/assets/logo/openim-logo-green.png" + - '--label=io.artifacthub.package.maintainers=[{"name":"Xinwei Xiong","email":"3293172751nss@gmail.com"}]' + - "--label=io.artifacthub.package.license=Apace-2.0" + - "--label=org.opencontainers.image.description=OpenIM Open source top instant messaging system" - "--label=org.opencontainers.image.created={{.Date}}" - "--label=org.opencontainers.image.name={{.ProjectName}}" - "--label=org.opencontainers.image.revision={{.FullCommit}}" @@ -505,19 +505,16 @@ release:

- ## Contact Us + ## Get Involved with OpenIM! - We value close connections with our users, developers, and contributors here at OpenIMSDK. With a large community and maintainer team, we're always here to help and support you. Whether you're looking to join our community or have any questions or suggestions, we welcome you to get in touch with us. + **Here are some ways to get involved with the OpenIM community:** - Our most recommended way to get in touch is through [Slack](https://join.slack.com/t/openimsdk/shared_invite/zt-1tmoj26uf-_FDy3dowVHBiGvLk9e5Xkg). Even if you're in China, Slack is usually not blocked by firewalls, making it an easy way to connect with us. Our Slack community is the ideal place to discuss and share ideas and suggestions with other users and developers of OpenIMSDK. You can ask technical questions, seek help, or share your experiences with other users of OpenIMSDK. + 📢 **Slack Channel**: Join our Slack channels for discussions, communication, and support. Click [here](https://join.slack.com/t/openimsdk/shared_invite/zt-1tmoj26uf-_FDy3dowVHBiGvLk9e5Xkg) to join the Open-IM-Server Slack team channel. - In [OpenIM community is recruiting new members! discussion](https://github.com/orgs/OpenIMSDK/discussions/426) please leave your information, convenient we will better developers around the small gift to send to your hands. + 📧 **Gmail Contact**: If you have any questions, suggestions, or feedback for our open-source projects, please feel free to [contact us via email](https://mail.google.com/mail/?view=cm&fs=1&tf=1&to=winxu81@gmail.com). - In addition to Slack, we also offer the following ways to get in touch: + 📖 **Blog**: Stay up-to-date with OpenIM-Server projects and trends by reading our [blog](https://doc.rentsoft.cn/). We share the latest developments, tech trends, and other interesting information related to OpenIM. - + : We also have Slack channels for you to communicate and discuss. To join, visit https://slack.com/ and join our [👀 Open-IM-Server slack](https://join.slack.com/t/openimsdk/shared_invite/zt-1tmoj26uf-_FDy3dowVHBiGvLk9e5Xkg) team channel. - + : Get in touch with us on [Gmail](winxu81@gmail.com). If you have any questions or issues that need resolving, or any suggestions and feedback for our open source projects, please feel free to contact us via email. - + : Read our [blog](https://doc.rentsoft.cn/). Our blog is a great place to stay up-to-date with Open-IM-Server projects and trends. On the blog, we share our latest developments, tech trends, and other interesting information. - + : Add [Wechat](https://github.com/OpenIMSDK/OpenIM-Docs/blob/main/docs/images/WechatIMG20.jpeg) and indicate that you are a user or developer of Open-IM-Server. We will process your request as soon as possible. + 📱 **WeChat**: Add us on WeChat (QR Code) and indicate that you are a user or developer of Open-IM-Server. We'll process your request as soon as possible. - Whether you're looking to join our community or have any questions or suggestions, we welcome you to get in touch with us. + Remember, your contributions play a vital role in making OpenIM successful, and we look forward to your active participation in our community! 🙌 \ No newline at end of file diff --git a/Makefile b/Makefile index 74e183543..de6570dd9 100644 --- a/Makefile +++ b/Makefile @@ -68,6 +68,11 @@ multiarch: install: @$(MAKE) go.install +## check: Check OpenIM deployment ✨ +.PHONY: check +check: + @$(MAKE) go.check + ## tidy: tidy go.mod ✨ .PHONY: tidy tidy: @@ -176,6 +181,11 @@ verify-copyright: add-copyright: @$(MAKE) copyright.add +## advertise: Project introduction, become a contributor ✨ +.PHONY: advertise +advertise: + @$(MAKE) copyright.advertise + ## release: release the project ✨ .PHONY: release release: release.verify release.ensure-tag diff --git a/config/config.yaml b/config/config.yaml index 1c01031dc..3f43a7a69 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -24,8 +24,8 @@ zookeeper: mysql: address: [ 127.0.0.1:13306 ] #目前仅支持单机 - username: root #用户名 - password: openIM123 #密码 + username: root + password: openIM123 database: openIM_v3 #不建议修改 maxOpenConn: 1000 #最大连接数 maxIdleConn: 100 #最大空闲连接数 @@ -37,14 +37,14 @@ mongo: uri: #不为空则直接使用该值 address: [ 127.0.0.1:37017 ] #单机时为mongo地址,使用分片集群时,为mongos地址 database: openIM_v3 #mongo db 默认即可 - username: root #用户名 - password: openIM123 #密码 + username: root + password: openIM123 maxPoolSize: 100 redis: address: [ 127.0.0.1:16379 ] # username: #only redis version 6.0+ need username - password: openIM123 #密码 + password: openIM123 kafka: username: #用户名 @@ -77,9 +77,9 @@ object: apiURL: "http://127.0.0.1:10002/object/" #地址需要app能访问到 minio: bucket: "openim" #不建议修改 - endpoint: "http://127.0.0.1:10005" #minio对外服务的ip和端口,app要能访问此ip和端口 - accessKeyID: "root" #ID - secretAccessKey: "openIM123" #秘钥 + endpoint: http://127.0.0.1:10005 #地址需要app能够访问 + accessKeyID: root + secretAccessKey: openIM123 sessionToken: "" #token cos: #tencent cos bucketURL: "https://temp-1252357374.cos.ap-chengdu.myqcloud.com" @@ -167,7 +167,7 @@ retainChatRecords: 365 #mongo保存离线消息 chatRecordsClearTime: "0 2 * * 3" #每周三凌晨2点清理mongo中的过期(超过retainChatRecords时间)消息,这个删除是为了清理满足上个配置retainChatRecords的过期消息,不会发送通知,仅仅作为清理磁盘使用 msgDestructTime: "0 2 * * *" #消息自动删除时间,每天凌晨2点删除过期消息,这个删除是为了删除保留时间超过超过会话字段msg_destruct_time(秒)的消息。 -secret: tuoyun #秘钥,获取token时校验 +secret: openIM123 tokenPolicy: expire: 90 #过期时间(天) @@ -257,4 +257,4 @@ prometheus: #prometheus每个服务的端口数量需要和rpcPort保持对应 conversationPrometheusPort: [ 20230 ] rtcPrometheusPort: [ 21300 ] thirdPrometheusPort: [ 21301 ] - messageTransferPrometheusPort: [ 21400, 21401, 21402, 21403 ] #端口数量需要和script/path_info.sh中的msg_transfer_service_num保持一致 + messageTransferPrometheusPort: [ 21400, 21401, 21402, 21403 ] #端口数量需要和script/path_info.sh中的msg_transfer_service_num保持一致 \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100755 index 000000000..0c3a3945e --- /dev/null +++ b/install.sh @@ -0,0 +1,560 @@ +#!/bin/bash +# Copyright © 2023 OpenIM. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# https://gist.github.com/cubxxw/28f997f2c9aff408630b072f010c1d64 +# + +set -e +set -o pipefail + + +############### OpenIM Github ############### +# ... rest of the script ... + +# TODO +# You can configure this script in three ways. +# 1. First, set the variables in this column with more comments. +# 2. The second is to pass an environment variable via a flag such as --help. +# 3. The third way is to set the variable externally, or pass it in as an environment variable + +# Default configuration for OpenIM Repo +# The OpenIM Repo settings can be customized according to your needs. + +# OpenIM Repo owner, by default it's set to "OpenIMSDK". If you're using a different owner, replace accordingly. +OWNER="OpenIMSDK" + +# The repository name, by default it's "Open-IM-Server". If you're using a different repository, replace accordingly. +REPO="Open-IM-Server" + +# Version of Go you want to use, make sure it is compatible with your OpenIM-Server requirements. +# Default is 1.18, if you want to use a different version, replace accordingly. +GO_VERSION="1.18" + +# Default HTTP_PORT is 80. If you want to use a different port, uncomment and replace the value. +# HTTP_PORT=80 + +# CPU core number for concurrent execution. By default it's determined automatically. +# Uncomment the next line if you want to set it manually. +# CPU=$(grep -c ^processor /proc/cpuinfo) + +# By default, the script uses the latest tag from OpenIM-Server releases. +# If you want to use a specific tag, uncomment and replace "v3.0.0" with the desired tag. +# LATEST_TAG=v3.0.0 + +# Default OpenIM install directory is /tmp. If you want to use a different directory, uncomment and replace "/test". +# DOWNLOAD_OPENIM_DIR="/test" + +# GitHub proxy settings. If you are using a proxy, uncomment and replace the empty field with your proxy URL. +PROXY= + +# If you have a GitHub token, replace the empty field with your token. +GITHUB_TOKEN= + +# Default user is "root". If you need to modify it, uncomment and replace accordingly. +# USER=root + +# Default password for redis, mysql, mongo, as well as accessSecret in config/config.yaml. +# Remember, it should be a combination of 8 or more numbers and letters. If you want to set a different password, uncomment and replace "openIM123". +# PASSWORD=openIM123 + +# Default endpoint for minio's external service IP and port. If you want to use a different endpoint, uncomment and replace. +# ENDPOINT=http://127.0.0.1:10005 + +# Default API_URL, replace if necessary. +# API_URL=http://127.0.0.1:10002/object/ + +# Default data directory. If you want to specify a different directory, uncomment and replace "./". +# DATA_DIR=./ + +############### OpenIM Functions ############### +# Install horizon of the script +# +# Pre-requisites: +# - git +# - make +# - jq +# - docker +# - docker-compose +# - go +# + +# Check if the script is run as root +function check_isroot() { + if [ "$EUID" -ne 0 ]; then + fatal "Please run the script as root or use sudo." + fi +} + +# check if the current directory is a OpenIM git repository +function check_git_repo() { + if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then + # Inside a git repository + for remote in $(git remote); do + repo_url=$(git remote get-url $remote) + if [[ $repo_url == "https://github.com/OpenIMSDK/Open-IM-Server.git" || \ + $repo_url == "https://github.com/OpenIMSDK/Open-IM-Server" || \ + $repo_url == "git@github.com:OpenIMSDK/Open-IM-Server.git" ]]; then + # If it's OpenIMSDK repository + info "Current directory is OpenIMSDK git repository." + info "Executing installation directly." + install_openim + exit 0 + fi + debug "Remote: $remote, URL: $repo_url" + done + # If it's not OpenIMSDK repository + debug "Current directory is not OpenIMSDK git repository." + fi + info "Current directory is not a git repository." +} + +# Function to update and install necessary tools +function install_tools() { + info "Checking and installing necessary tools, about git, make, jq, docker, docker-compose." + local tools=("git" "make" "jq" "docker" "docker-compose") + local install_cmd update_cmd os + + if grep -qEi "debian|buntu|mint" /etc/os-release; then + os="Ubuntu" + install_cmd="sudo apt install -y" + update_cmd="sudo apt update" + elif grep -qEi "fedora|rhel" /etc/os-release; then + os="CentOS" + install_cmd="sudo yum install -y" + update_cmd="sudo yum update" + else + fatal "Unsupported OS, please use Ubuntu or CentOS." + fi + + debug "Detected OS: $os" + info "Updating system package repositories..." + $update_cmd + + for tool in "${tools[@]}"; do + if ! command -v $tool &> /dev/null; then + warn "$tool is not installed. Installing now..." + $install_cmd $tool + success "$tool has been installed successfully." + else + info "$tool is already installed." + fi + done +} + +# Function to check if Docker and Docker Compose are installed +function check_docker() { + if ! command -v docker &> /dev/null; then + fatal "Docker is not installed. Please install Docker first." + fi + if ! command -v docker-compose &> /dev/null; then + fatal "Docker Compose is not installed. Please install Docker Compose first." + fi +} + +# Function to download and install Go if it's not already installed +function install_go() { + command -v go >/dev/null 2>&1 + # Determines if GO_VERSION is defined + if [ -z "$GO_VERSION" ]; then + GO_VERSION="1.18" + fi + + if [[ $? -ne 0 ]]; then + warn "Go is not installed. Installing now..." + curl -LO "https://golang.org/dl/go${GO_VERSION}.linux-amd64.tar.gz" + if [ $? -ne 0 ]; then + fatal "Download failed! Please check your network connectivity." + fi + sudo tar -C /usr/local -xzf "go${GO_VERSION}.linux-amd64.tar.gz" + echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc + source ~/.bashrc + success "Go has been installed successfully." + else + info "Go is already installed." + fi +} + +function download_source_code() { + + # If LATEST_TAG was not defined outside the function, get it here example: v3.0.1-beta.1 + if [ -z "$LATEST_TAG" ]; then + LATEST_TAG=$(curl -s "https://api.github.com/repos/$OWNER/$REPO/tags" | jq -r '.[0].name') + fi + + # If LATEST_TAG is still empty, set a default value + local DEFAULT_TAG="v3.0.0" + + LATEST_TAG="${LATEST_TAG:-$DEFAULT_TAG}" + + debug "DEFAULT_TAG: $DEFAULT_TAG" + info "Use OpenIM Version LATEST_TAG: $LATEST_TAG" + + # If MODIFIED_TAG was not defined outside the function, modify it here,example: 3.0.1-beta.1 + if [ -z "$MODIFIED_TAG" ]; then + MODIFIED_TAG=$(echo $LATEST_TAG | sed 's/v//') + fi + + # If MODIFIED_TAG is still empty, set a default value + local DEFAULT_MODIFIED_TAG="${DEFAULT_TAG#v}" + MODIFIED_TAG="${MODIFIED_TAG:-$DEFAULT_MODIFIED_TAG}" + + debug "MODIFIED_TAG: $MODIFIED_TAG" + + # Construct the tarball URL + TARBALL_URL="${PROXY}https://github.com/$OWNER/$REPO/archive/refs/tags/$LATEST_TAG.tar.gz" + + info "Downloaded OpenIM TARBALL_URL: $TARBALL_URL" + + info "Starting the OpenIM automated one-click deployment script." + + # Set the download and extract directory to /tmp + if [ -z "$DOWNLOAD_OPENIM_DIR" ]; then + DOWNLOAD_OPENIM_DIR="/tmp" + fi + + # Check if /tmp directory exists + if [ ! -d "$DOWNLOAD_OPENIM_DIR" ]; then + warn "$DOWNLOAD_OPENIM_DIR does not exist. Creating it..." + mkdir -p "$DOWNLOAD_OPENIM_DIR" + fi + + info "Downloading OpenIM source code from $TARBALL_URL to $DOWNLOAD_OPENIM_DIR" + + curl -L -o "${DOWNLOAD_OPENIM_DIR}/${MODIFIED_TAG}.tar.gz" $TARBALL_URL + + tar -xzvf "${DOWNLOAD_OPENIM_DIR}/${MODIFIED_TAG}.tar.gz" -C "$DOWNLOAD_OPENIM_DIR" + cd "$DOWNLOAD_OPENIM_DIR/$REPO-$MODIFIED_TAG" + git init && git add . && git commit -m "init" --no-verify + + success "Source code downloaded and extracted to $REPO-$MODIFIED_TAG" +} + +function set_openim_env() { + warn "This command can only be executed once. It will modify the component passwords in docker-compose based on the PASSWORD variable in .env, and modify the component passwords in config/config.yaml. If the password in .env changes, you need to first execute docker-compose down; rm components -rf and then execute this command." + # Set default values for user input + # If the USER environment variable is not set, it defaults to 'root' + if [ -z "$USER" ]; then + USER="root" + debug "USER is not set. Defaulting to 'root'." + fi + + # If the PASSWORD environment variable is not set, it defaults to 'openIM123' + # This password applies to redis, mysql, mongo, as well as accessSecret in config/config.yaml + if [ -z "$PASSWORD" ]; then + PASSWORD="openIM123" + debug "PASSWORD is not set. Defaulting to 'openIM123'." + fi + + # If the ENDPOINT environment variable is not set, it defaults to 'http://127.0.0.1:10005' + # This is minio's external service IP and port, or it could be a domain like storage.xx.xx + # The app must be able to access this IP and port or domain + if [ -z "$ENDPOINT" ]; then + ENDPOINT="http://127.0.0.1:10005" + debug "ENDPOINT is not set. Defaulting to 'http://127.0.0.1:10005'." + fi + + # If the API_URL environment variable is not set, it defaults to 'http://127.0.0.1:10002/object/' + # The app must be able to access this IP and port or domain + if [ -z "$API_URL" ]; then + API_URL="http://127.0.0.1:10002/object/" + debug "API_URL is not set. Defaulting to 'http://127.0.0.1:10002/object/'." + fi + + # If the DATA_DIR environment variable is not set, it defaults to the current directory './' + # This can be set to a directory with large disk space + if [ -z "$DATA_DIR" ]; then + DATA_DIR="./" + debug "DATA_DIR is not set. Defaulting to './'." + fi +} + +function install_openim() { + info "Installing OpenIM" + make -j${CPU} install V=1 + + info "Checking installation" + make check + + success "OpenIM installation completed successfully. Happy chatting!" +} + +############### OpenIM Help ############### + +# Function to display help message +function cmd_help() { + openim_color + color_echo ${BRIGHT_GREEN_PREFIX} "Usage: $0 [options]" + color_echo ${BRIGHT_GREEN_PREFIX} "Options:" + echo + color_echo ${BLUE_PREFIX} "-i, --install ${CYAN_PREFIX}Execute the installation logic of the script${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-u, --user ${CYAN_PREFIX}set user (default: root)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-p, --password ${CYAN_PREFIX}set password (default: openIM123)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-e, --endpoint ${CYAN_PREFIX}set endpoint (default: http://127.0.0.1:10005)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-a, --api ${CYAN_PREFIX}set API URL (default: http://127.0.0.1:10002/object/)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-d, --directory ${CYAN_PREFIX}set directory for large disk space (default: ./)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-h, --help ${CYAN_PREFIX}display this help message and exit${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-cn, --china ${CYAN_PREFIX}set to use the Chinese domestic proxy${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-t, --tag ${CYAN_PREFIX}specify the tag (default option, set to latest if not specified)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-r, --release ${CYAN_PREFIX}specify the release branch (cannot be used with the tag option)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-g, --go-version ${CYAN_PREFIX}set the Go language version (default: GO_VERSION=\"1.18\")${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-i, --install-dir ${CYAN_PREFIX}set the OpenIM installation directory (default: /tmp)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-gt, --github-token ${CYAN_PREFIX}set the GITHUB_TOKEN (default: not set)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "--cpu ${CYAN_PREFIX}set the number of concurrent processes${COLOR_SUFFIX}" + echo + color_echo ${RED_PREFIX} "Note: Only one of the -t/--tag or -r/--release options can be used at a time.${COLOR_SUFFIX}" + color_echo ${RED_PREFIX} "If both are used or none of them are used, the -t/--tag option will be prioritized.${COLOR_SUFFIX}" + echo + exit 1 +} + +function parseinput() { + # set default values + # USER=root + # PASSWORD=openIM123 + # ENDPOINT=http://127.0.0.1:10005 + # API=http://127.0.0.1:10002/object/ + # DIRECTORY=./ + # CHINA=false + # TAG=latest + # RELEASE="" + # GO_VERSION=1.18 + # INSTALL_DIR=/tmp + # GITHUB_TOKEN="" + # CPU=$(nproc) + + if [ $# -eq 0 ]; then + cmd_help + exit 1 + fi + + while [ $# -gt 0 ]; do + case $1 in + -h|--help) + cmd_help + exit + ;; + -u|--user) + shift + USER=$1 + ;; + -p|--password) + shift + PASSWORD=$1 + ;; + -e|--endpoint) + shift + ENDPOINT=$1 + ;; + -a|--api) + shift + API=$1 + ;; + -d|--directory) + shift + DIRECTORY=$1 + ;; + -cn|--china) + CHINA=true + ;; + -t|--tag) + shift + TAG=$1 + ;; + -r|--release) + shift + RELEASE=$1 + ;; + -g|--go-version) + shift + GO_VERSION=$1 + ;; + -i|--install-dir) + shift + INSTALL_DIR=$1 + ;; + -gt|--github-token) + shift + GITHUB_TOKEN=$1 + ;; + --cpu) + shift + CPU=$1 + ;; + -i|--install) + openim_main + exit + ;; + *) + echo "Unknown option: $1" + cmd_help + exit 1 + ;; + esac + shift + done +} + +############### OpenIM LOGO ############### +# Set text color to cyan for header and URL +print_with_delay() { + text="$1" + delay="$2" + + for i in $(seq 0 $((${#text}-1))); do + printf "${text:$i:1}" + sleep $delay + done + printf "\n" +} + +print_progress() { + total="$1" + delay="$2" + + printf "[" + for i in $(seq 1 $total); do + printf "#" + sleep $delay + done + printf "]\n" +} + +# Function for colored echo +color_echo() { + COLOR=$1 + shift + echo -e "${COLOR}===> $* ${COLOR_SUFFIX}" +} + +# Color definitions +function openim_color() { + COLOR_SUFFIX="\033[0m" # End all colors and special effects + + BLACK_PREFIX="\033[30m" # Black prefix + RED_PREFIX="\033[31m" # Red prefix + GREEN_PREFIX="\033[32m" # Green prefix + YELLOW_PREFIX="\033[33m" # Yellow prefix + BLUE_PREFIX="\033[34m" # Blue prefix + SKY_BLUE_PREFIX="\033[36m" # Sky blue prefix + WHITE_PREFIX="\033[37m" # White prefix + BOLD_PREFIX="\033[1m" # Bold prefix + UNDERLINE_PREFIX="\033[4m" # Underline prefix + ITALIC_PREFIX="\033[3m" # Italic prefix + BRIGHT_GREEN_PREFIX='\033[1;32m' # Bright green prefix + + CYAN_PREFIX="\033[0;36m" # Cyan prefix +} + +# --- helper functions for logs --- +info() +{ + echo -e "[${GREEN_PREFIX}INFO${COLOR_SUFFIX}] " "$@" +} +warn() +{ + echo -e "[${YELLOW_PREFIX}WARN${COLOR_SUFFIX}] " "$@" >&2 +} +fatal() +{ + echo -e "[${RED_PREFIX}ERROR${COLOR_SUFFIX}] " "$@" >&2 + exit 1 +} +debug() +{ + echo -e "[${BLUE_PREFIX}DEBUG${COLOR_SUFFIX}]===> " "$@" +} +success() +{ + echo -e "${BRIGHT_GREEN_PREFIX}=== [SUCCESS] ===${COLOR_SUFFIX}\n=> " "$@" +} + +function openim_logo() { + # Set text color to cyan for header and URL + echo -e "\033[0;36m" + + # Display fancy ASCII Art logo + # look http://patorjk.com/software/taag/#p=display&h=1&v=1&f=Doh&t=OpenIM + print_with_delay ' + + + OOOOOOOOO IIIIIIIIIIMMMMMMMM MMMMMMMM + OO:::::::::OO I::::::::IM:::::::M M:::::::M + OO:::::::::::::OO I::::::::IM::::::::M M::::::::M +O:::::::OOO:::::::O II::::::IIM:::::::::M M:::::::::M +O::::::O O::::::Oppppp ppppppppp eeeeeeeeeeee nnnn nnnnnnnn I::::I M::::::::::M M::::::::::M +O:::::O O:::::Op::::ppp:::::::::p ee::::::::::::ee n:::nn::::::::nn I::::I M:::::::::::M M:::::::::::M +O:::::O O:::::Op:::::::::::::::::p e::::::eeeee:::::een::::::::::::::nn I::::I M:::::::M::::M M::::M:::::::M +O:::::O O:::::Opp::::::ppppp::::::pe::::::e e:::::enn:::::::::::::::n I::::I M::::::M M::::M M::::M M::::::M +O:::::O O:::::O p:::::p p:::::pe:::::::eeeee::::::e n:::::nnnn:::::n I::::I M::::::M M::::M::::M M::::::M +O:::::O O:::::O p:::::p p:::::pe:::::::::::::::::e n::::n n::::n I::::I M::::::M M:::::::M M::::::M +O:::::O O:::::O p:::::p p:::::pe::::::eeeeeeeeeee n::::n n::::n I::::I M::::::M M:::::M M::::::M +O::::::O O::::::O p:::::p p::::::pe:::::::e n::::n n::::n I::::I M::::::M MMMMM M::::::M +O:::::::OOO:::::::O p:::::ppppp:::::::pe::::::::e n::::n n::::nII::::::IIM::::::M M::::::M + OO:::::::::::::OO p::::::::::::::::p e::::::::eeeeeeee n::::n n::::nI::::::::IM::::::M M::::::M + OO:::::::::OO p::::::::::::::pp ee:::::::::::::e n::::n n::::nI::::::::IM::::::M M::::::M + OOOOOOOOO p::::::pppppppp eeeeeeeeeeeeee nnnnnn nnnnnnIIIIIIIIIIMMMMMMMM MMMMMMMM + p:::::p + p:::::p + p:::::::p + p:::::::p + p:::::::p + ppppppppp + + ' 0.0001 + + # Display product URL + print_with_delay "Discover more and contribute at: https://github.com/OpenIMSDK/Open-IM-Server" 0.01 + + # Reset text color back to normal + echo -e "\033[0m" + + # Set text color to green for product description + echo -e "\033[1;32m" + + print_with_delay "Open-IM-Server: Reinventing Instant Messaging" 0.01 + print_progress 50 0.02 + + print_with_delay "Open-IM-Server is not just a product; it's a revolution. It's about bringing the power of seamless, real-time messaging to your fingertips. And it's about joining a global community of developers, dedicated to pushing the boundaries of what's possible." 0.01 + + print_progress 50 0.02 + + # Reset text color back to normal + echo -e "\033[0m" + + # Set text color to yellow for the Slack link + echo -e "\033[1;33m" + + print_with_delay "Join our developer community on Slack: https://join.slack.com/t/openimsdk/shared_invite/zt-1tmoj26uf-_FDy3dowVHBiGvLk9e5Xkg" 0.01 + + # Reset text color back to normal + echo -e "\033[0m" +} + +# Main function to run the script +function openim_main() { + check_git_repo + check_isroot + openim_color + install_tools + check_docker + install_go + download_source_code + set_openim_env + install_openim + openim_logo + +} + +parseinput "$@" \ No newline at end of file diff --git a/scripts/advertise.sh b/scripts/advertise.sh new file mode 100755 index 000000000..c1cd63f56 --- /dev/null +++ b/scripts/advertise.sh @@ -0,0 +1,122 @@ +#!/bin/bash + +set -e +set -o pipefail + +trap 'echo "Script interrupted."; exit 1' INT + +# Function for colored echo +function color_echo() { + COLOR=$1 + shift + echo -e "${COLOR}===> $* ${COLOR_SUFFIX}" +} + +# Color definitions +function openim_color() { + COLOR_SUFFIX="\033[0m" # End all colors and special effects + + BLACK_PREFIX="\033[30m" # Black prefix + RED_PREFIX="\033[31m" # Red prefix + GREEN_PREFIX="\033[32m" # Green prefix + YELLOW_PREFIX="\033[33m" # Yellow prefix + BLUE_PREFIX="\033[34m" # Blue prefix + SKY_BLUE_PREFIX="\033[36m" # Sky blue prefix + WHITE_PREFIX="\033[37m" # White prefix + BOLD_PREFIX="\033[1m" # Bold prefix + UNDERLINE_PREFIX="\033[4m" # Underline prefix + ITALIC_PREFIX="\033[3m" # Italic prefix + + CYAN_PREFIX="\033[0;36m" # Cyan prefix +} + +function print_with_delay() { + text="$1" + delay="$2" + color="$3" + + for i in $(seq 0 $((${#text}-1))); do + printf "${color}${text:$i:1}${COLOR_SUFFIX}" + sleep $delay + done + printf "\n" +} + +function print_progress() { + total="$1" + delay="$2" + color="$3" + + printf "${color}[" + for i in $(seq 1 $total); do + printf "#" + sleep $delay + done + printf "]${COLOR_SUFFIX}\n" +} +function openim_logo() { + # Set text color to cyan for header and URL + echo -e "\033[0;36m" + + # Display fancy ASCII Art logo + # look http://patorjk.com/software/taag/#p=display&h=1&v=1&f=Doh&t=OpenIM + print_with_delay ' + + + OOOOOOOOO IIIIIIIIIIMMMMMMMM MMMMMMMM + OO:::::::::OO I::::::::IM:::::::M M:::::::M + OO:::::::::::::OO I::::::::IM::::::::M M::::::::M +O:::::::OOO:::::::O II::::::IIM:::::::::M M:::::::::M +O::::::O O::::::Oppppp ppppppppp eeeeeeeeeeee nnnn nnnnnnnn I::::I M::::::::::M M::::::::::M +O:::::O O:::::Op::::ppp:::::::::p ee::::::::::::ee n:::nn::::::::nn I::::I M:::::::::::M M:::::::::::M +O:::::O O:::::Op:::::::::::::::::p e::::::eeeee:::::een::::::::::::::nn I::::I M:::::::M::::M M::::M:::::::M +O:::::O O:::::Opp::::::ppppp::::::pe::::::e e:::::enn:::::::::::::::n I::::I M::::::M M::::M M::::M M::::::M +O:::::O O:::::O p:::::p p:::::pe:::::::eeeee::::::e n:::::nnnn:::::n I::::I M::::::M M::::M::::M M::::::M +O:::::O O:::::O p:::::p p:::::pe:::::::::::::::::e n::::n n::::n I::::I M::::::M M:::::::M M::::::M +O:::::O O:::::O p:::::p p:::::pe::::::eeeeeeeeeee n::::n n::::n I::::I M::::::M M:::::M M::::::M +O::::::O O::::::O p:::::p p::::::pe:::::::e n::::n n::::n I::::I M::::::M MMMMM M::::::M +O:::::::OOO:::::::O p:::::ppppp:::::::pe::::::::e n::::n n::::nII::::::IIM::::::M M::::::M + OO:::::::::::::OO p::::::::::::::::p e::::::::eeeeeeee n::::n n::::nI::::::::IM::::::M M::::::M + OO:::::::::OO p::::::::::::::pp ee:::::::::::::e n::::n n::::nI::::::::IM::::::M M::::::M + OOOOOOOOO p::::::pppppppp eeeeeeeeeeeeee nnnnnn nnnnnnIIIIIIIIIIMMMMMMMM MMMMMMMM + p:::::p + p:::::p + p:::::::p + p:::::::p + p:::::::p + ppppppppp + + ' 0.0001 + + # Display product URL + print_with_delay "Discover more and contribute at: https://github.com/OpenIMSDK/Open-IM-Server" 0.01 + + # Reset text color back to normal + echo -e "\033[0m" + + # Set text color to green for product description + echo -e "\033[1;32m" + + print_with_delay "Open-IM-Server: Reinventing Instant Messaging" 0.01 + print_progress 50 0.02 + + print_with_delay "Open-IM-Server is not just a product; it's a revolution. It's about bringing the power of seamless, real-time messaging to your fingertips. And it's about joining a global community of developers, dedicated to pushing the boundaries of what's possible." 0.01 + + print_progress 50 0.02 + + # Reset text color back to normal + echo -e "\033[0m" + + # Set text color to yellow for the Slack link + echo -e "\033[1;33m" + + print_with_delay "Join our developer community on Slack: https://join.slack.com/t/openimsdk/shared_invite/zt-1tmoj26uf-_FDy3dowVHBiGvLk9e5Xkg" 0.01 + + # Reset text color back to normal + echo -e "\033[0m" +} + +function main() { + openim_logo +} +main "$@" diff --git a/scripts/make-rules/copyright.mk b/scripts/make-rules/copyright.mk index 319e46945..2b7a04082 100644 --- a/scripts/make-rules/copyright.mk +++ b/scripts/make-rules/copyright.mk @@ -51,6 +51,12 @@ copyright.add: tools.verify.addlicense # -y string # copyright year(s) (default "2023") +## copyright.advertise: Advertise the license of the project +.PHONY: copyright.advertise +copyright.advertise: + @chmod +x $(ROOT_DIR)/scripts/advertise.sh + @$(ROOT_DIR)/scripts/advertise.sh + ## copyright.help: Show copyright help .PHONY: copyright.help copyright.help: scripts/make-rules/copyright.mk diff --git a/scripts/make-rules/golang.mk b/scripts/make-rules/golang.mk index 282649d7e..00e0fee47 100644 --- a/scripts/make-rules/golang.mk +++ b/scripts/make-rules/golang.mk @@ -137,6 +137,12 @@ go.install: @echo "===========> Installing deployment openim" @$(ROOT_DIR)/scripts/install_im_server.sh +## go.check: Check OpenIM deployment +.PHONY: go.check +go.check: + @echo "===========> Checking OpenIM deployment" + @$(ROOT_DIR)/scripts/check_all.sh + ## go.multiarch: Build multi-arch binaries .PHONY: go.build.multiarch go.build.multiarch: go.build.verify $(foreach p,$(PLATFORMS),$(addprefix go.build., $(addprefix $(p)., $(BINS)))) diff --git a/scripts/style_info.sh b/scripts/style_info.sh index 3c747dd0c..b56eceeff 100755 --- a/scripts/style_info.sh +++ b/scripts/style_info.sh @@ -28,6 +28,8 @@ BOLD_PREFIX="\033[1m" # Bold prefix UNDERLINE_PREFIX="\033[4m" # Underline prefix ITALIC_PREFIX="\033[3m" # Italic prefix +CYAN_PREFIX="033[0;36m" # Cyan prefix + BACKGROUND_BLACK="\033[40m" # Black background BACKGROUND_RED="\033[41m" # Red background BACKGROUND_GREEN="\033[42m" # Green background From 8a4be458041a4604cadfac788952d17961292c0a Mon Sep 17 00:00:00 2001 From: withchao <48119764+withchao@users.noreply.github.com> Date: Thu, 27 Jul 2023 12:31:27 +0800 Subject: [PATCH 07/11] fix: sql injection risk (#678) Signed-off-by: withchao <993506633@qq.com> --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 7acda0e12..e4bc8edc6 100644 --- a/go.mod +++ b/go.mod @@ -38,7 +38,7 @@ require github.com/google/uuid v1.3.0 require ( github.com/OpenIMSDK/protocol v0.0.1 - github.com/OpenIMSDK/tools v0.0.4 + github.com/OpenIMSDK/tools v0.0.5 github.com/aliyun/aliyun-oss-go-sdk v2.2.7+incompatible github.com/go-redis/redis v6.15.9+incompatible github.com/go-sql-driver/mysql v1.6.0 diff --git a/go.sum b/go.sum index 28b2c5c32..0089e1924 100644 --- a/go.sum +++ b/go.sum @@ -18,8 +18,8 @@ firebase.google.com/go v3.13.0+incompatible/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIw github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/OpenIMSDK/protocol v0.0.1 h1:Q6J1jCU00dfqmguxw2XI+IGcVfBAkb5Tz8LgvyeNkk0= github.com/OpenIMSDK/protocol v0.0.1/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y= -github.com/OpenIMSDK/tools v0.0.4 h1:aSC5FWO/0ccxuLHs+E1Eeyj+XWL/2CzLy1XGmQgpaU4= -github.com/OpenIMSDK/tools v0.0.4/go.mod h1:eg+q4A34Qmu73xkY0mt37FHGMCMfC6CtmOnm0kFEGFI= +github.com/OpenIMSDK/tools v0.0.5 h1:yBVHJ3EpIDcp8VFKPjuGr6MQvFa3t4JByZ+vmeC06/Q= +github.com/OpenIMSDK/tools v0.0.5/go.mod h1:eg+q4A34Qmu73xkY0mt37FHGMCMfC6CtmOnm0kFEGFI= github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mod h1:1pk82RBxDY/JZnPQrtqHlUFfCctgdorsd9M06fMynOM= github.com/Shopify/sarama v1.29.0 h1:ARid8o8oieau9XrHI55f/L3EoRAhm9px6sonbD7yuUE= github.com/Shopify/sarama v1.29.0/go.mod h1:2QpgD79wpdAESqNQMxNc0KYMkycd4slxGdV3TWSVqrU= From 0bc8b9259e942bf378c68150dfe5a9bc7f5e190f Mon Sep 17 00:00:00 2001 From: WangchuXiao Date: Thu, 27 Jul 2023 12:32:08 +0800 Subject: [PATCH 08/11] new feat: sync designated model data (#668) * new feature: add batch send msg * new feature: add batch send msg * new feature: add batch send msg * new feature: add batch send msg * new feature: add batch send msg * new feature: add batch send msg * fix bug: multiple gateway kick user * fix bug: multiple gateway kick user * fix bug: multiple gateway kick user * fix bug: multiple gateway kick user * fix bug: multiple gateway kick user * MsgDestructTime * fix bug: msg destruct sql * fix bug: msg destruct * fix bug: msg destruct * fix bug: msg destruct sql * fix bug: msg destruct sql * fix bug: msg destruct sql * fix bug: msg destruct sql * debug: print stack * debug: print stack * debug: print stack * fix bug: msg destruct sql Signed-off-by: wangchuxiao * fix bug: msg notification self 2 self push twice * fix bug: heartbeat get self notification * fix bug: init grpc conn in one process * fix bug: grpc conn Signed-off-by: wangchuxiao * fix bug: zk client recreate node when reconn * fix bug: set friend mark args error * fix bug: rpc client intercepter called twice Signed-off-by: wangchuxiao * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * test: document msg num set 100 * new feat: sync designated model * new feat: sync designated model * new feat: sync designated model * new feat: sync designated model * new feat: sync designated model * new feat: sync designated model * new feat: sync designated model * merge code * merge code --------- Signed-off-by: wangchuxiao Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: wangchuxiao-dev --- internal/api/friend.go | 8 ++++ internal/api/group.go | 4 ++ internal/api/route.go | 3 ++ internal/rpc/conversation/conversaion.go | 6 +-- internal/rpc/friend/friend.go | 19 +++++--- internal/rpc/group/group.go | 48 +++++++++++++++++-- internal/rpc/user/user.go | 12 ++--- pkg/apistruct/manage.go | 2 +- pkg/common/db/controller/friend.go | 5 ++ pkg/common/db/controller/group.go | 5 ++ .../db/relation/friend_request_model.go | 11 +++++ pkg/common/db/relation/group_request_model.go | 5 ++ .../db/table/relation/friend_request.go | 2 +- pkg/common/db/table/relation/group_request.go | 1 + pkg/rpcclient/notification/conevrsation.go | 19 ++++---- 15 files changed, 118 insertions(+), 32 deletions(-) diff --git a/internal/api/friend.go b/internal/api/friend.go index 7af614fa4..f64a99ef3 100644 --- a/internal/api/friend.go +++ b/internal/api/friend.go @@ -44,6 +44,10 @@ func (o *FriendApi) GetFriendApplyList(c *gin.Context) { a2r.Call(friend.FriendClient.GetPaginationFriendsApplyTo, o.Client, c) } +func (o *FriendApi) GetDesignatedFriendsApply(c *gin.Context) { + a2r.Call(friend.FriendClient.GetDesignatedFriendsApply, o.Client, c) +} + func (o *FriendApi) GetSelfApplyList(c *gin.Context) { a2r.Call(friend.FriendClient.GetPaginationFriendsApplyFrom, o.Client, c) } @@ -52,6 +56,10 @@ func (o *FriendApi) GetFriendList(c *gin.Context) { a2r.Call(friend.FriendClient.GetPaginationFriends, o.Client, c) } +func (o *FriendApi) GetDesignatedFriends(c *gin.Context) { + a2r.Call(friend.FriendClient.GetDesignatedFriends, o.Client, c) +} + func (o *FriendApi) SetFriendRemark(c *gin.Context) { a2r.Call(friend.FriendClient.SetFriendRemark, o.Client, c) } diff --git a/internal/api/group.go b/internal/api/group.go index 2603bb858..ab397a8ac 100644 --- a/internal/api/group.go +++ b/internal/api/group.go @@ -60,6 +60,10 @@ func (o *GroupApi) GetUserReqGroupApplicationList(c *gin.Context) { a2r.Call(group.GroupClient.GetUserReqApplicationList, o.Client, c) } +func (o *GroupApi) GetGroupUsersReqApplicationList(c *gin.Context) { + a2r.Call(group.GroupClient.GetGroupUsersReqApplicationList, o.Client, c) +} + func (o *GroupApi) GetGroupsInfo(c *gin.Context) { a2r.Call(group.GroupClient.GetGroupsInfo, o.Client, c) } diff --git a/internal/api/route.go b/internal/api/route.go index 262283019..4a4f92cc0 100644 --- a/internal/api/route.go +++ b/internal/api/route.go @@ -86,8 +86,10 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive f := NewFriendApi(*friendRpc) friendRouterGroup.POST("/delete_friend", f.DeleteFriend) friendRouterGroup.POST("/get_friend_apply_list", f.GetFriendApplyList) + friendRouterGroup.POST("/get_designated_friend_apply", f.GetDesignatedFriendsApply) friendRouterGroup.POST("/get_self_friend_apply_list", f.GetSelfApplyList) friendRouterGroup.POST("/get_friend_list", f.GetFriendList) + friendRouterGroup.POST("/get_designated_friends", f.GetDesignatedFriends) friendRouterGroup.POST("/add_friend", f.ApplyToAddFriend) friendRouterGroup.POST("/add_friend_response", f.RespondFriendApply) friendRouterGroup.POST("/set_friend_remark", f.SetFriendRemark) @@ -108,6 +110,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive groupRouterGroup.POST("/transfer_group", g.TransferGroupOwner) groupRouterGroup.POST("/get_recv_group_applicationList", g.GetRecvGroupApplicationList) groupRouterGroup.POST("/get_user_req_group_applicationList", g.GetUserReqGroupApplicationList) + groupRouterGroup.POST("/get_group_users_req_application_list", g.GetGroupUsersReqApplicationList) groupRouterGroup.POST("/get_groups_info", g.GetGroupsInfo) groupRouterGroup.POST("/kick_group", g.KickGroupMember) groupRouterGroup.POST("/get_group_members_info", g.GetGroupMembersInfo) diff --git a/internal/rpc/conversation/conversaion.go b/internal/rpc/conversation/conversaion.go index 753a45722..6030bfa55 100644 --- a/internal/rpc/conversation/conversaion.go +++ b/internal/rpc/conversation/conversaion.go @@ -107,7 +107,7 @@ func (c *conversationServer) SetConversation(ctx context.Context, req *pbConvers if err != nil { return nil, err } - _ = c.conversationNotificationSender.ConversationChangeNotification(ctx, req.Conversation.OwnerUserID) + _ = c.conversationNotificationSender.ConversationChangeNotification(ctx, req.Conversation.OwnerUserID, []string{req.Conversation.ConversationID}) resp := &pbConversation.SetConversationResp{} return resp, nil } @@ -170,7 +170,7 @@ func (c *conversationServer) SetConversations(ctx context.Context, req *pbConver return nil, err } for _, userID := range req.UserIDs { - c.conversationNotificationSender.ConversationSetPrivateNotification(ctx, userID, req.Conversation.UserID, req.Conversation.IsPrivateChat.Value) + c.conversationNotificationSender.ConversationSetPrivateNotification(ctx, userID, req.Conversation.UserID, req.Conversation.IsPrivateChat.Value, req.Conversation.ConversationID) } } if req.Conversation.BurnDuration != nil { @@ -181,7 +181,7 @@ func (c *conversationServer) SetConversations(ctx context.Context, req *pbConver return nil, err } for _, v := range req.UserIDs { - c.conversationNotificationSender.ConversationChangeNotification(ctx, v) + c.conversationNotificationSender.ConversationChangeNotification(ctx, v, []string{req.Conversation.ConversationID}) } return &pbConversation.SetConversationsResp{}, nil } diff --git a/internal/rpc/friend/friend.go b/internal/rpc/friend/friend.go index 9ef38ef3c..7f3be3439 100644 --- a/internal/rpc/friend/friend.go +++ b/internal/rpc/friend/friend.go @@ -83,11 +83,6 @@ func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error { return nil } -func (s *friendServer) GetDesignatedFriendsApply(ctx context.Context, req *pbfriend.GetDesignatedFriendsApplyReq) (*pbfriend.GetDesignatedFriendsApplyResp, error) { - //TODO implement me - panic("implement me") -} - // ok. func (s *friendServer) ApplyToAddFriend( ctx context.Context, @@ -245,6 +240,19 @@ func (s *friendServer) GetDesignatedFriends( return resp, nil } +func (s *friendServer) GetDesignatedFriendsApply(ctx context.Context, req *pbfriend.GetDesignatedFriendsApplyReq) (resp *pbfriend.GetDesignatedFriendsApplyResp, err error) { + friendRequests, err := s.friendDatabase.FindBothFriendRequests(ctx, req.FromUserID, req.ToUserID) + if err != nil { + return nil, err + } + resp = &pbfriend.GetDesignatedFriendsApplyResp{} + resp.FriendRequests, err = convert.FriendRequestDB2Pb(ctx, friendRequests, s.userRpcClient.GetUsersInfoMap) + if err != nil { + return nil, err + } + return resp, nil +} + // ok 获取接收到的好友申请(即别人主动申请的). func (s *friendServer) GetPaginationFriendsApplyTo( ctx context.Context, @@ -303,7 +311,6 @@ func (s *friendServer) IsFriend( return resp, nil } -// ok. func (s *friendServer) GetPaginationFriends( ctx context.Context, req *pbfriend.GetPaginationFriendsReq, diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index bcadc61a3..4ce3d4216 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -95,11 +95,6 @@ type groupServer struct { msgRpcClient rpcclient.MessageRpcClient } -func (s *groupServer) GetGroupUsersReqApplicationList(ctx context.Context, req *pbGroup.GetGroupUsersReqApplicationListReq) (*pbGroup.GetGroupUsersReqApplicationListResp, error) { - //TODO implement me - panic("implement me") -} - func (s *groupServer) CheckGroupAdmin(ctx context.Context, groupID string) error { if !authverify.IsAppManagerUid(ctx) { groupMember, err := s.GroupDatabase.TakeGroupMember(ctx, groupID, mcontext.GetOpUserID(ctx)) @@ -1445,3 +1440,46 @@ func (s *groupServer) GetGroupMemberRoleLevel(ctx context.Context, req *pbGroup. }) return resp, nil } + +func (s *groupServer) GetGroupUsersReqApplicationList(ctx context.Context, req *pbGroup.GetGroupUsersReqApplicationListReq) (*pbGroup.GetGroupUsersReqApplicationListResp, error) { + resp := &pbGroup.GetGroupUsersReqApplicationListResp{} + total, requests, err := s.GroupDatabase.FindGroupRequests(ctx, req.GroupID, req.UserIDs) + if err != nil { + return nil, err + } + if len(requests) == 0 { + return resp, nil + } + groupIDs := utils.Distinct(utils.Slice(requests, func(e *relationTb.GroupRequestModel) string { + return e.GroupID + })) + groups, err := s.GroupDatabase.FindGroup(ctx, groupIDs) + if err != nil { + return nil, err + } + groupMap := utils.SliceToMap(groups, func(e *relationTb.GroupModel) string { + return e.GroupID + }) + if ids := utils.Single(groupIDs, utils.Keys(groupMap)); len(ids) > 0 { + return nil, errs.ErrGroupIDNotFound.Wrap(strings.Join(ids, ",")) + } + owners, err := s.FindGroupMember(ctx, groupIDs, nil, []int32{constant.GroupOwner}) + if err != nil { + return nil, err + } + ownerMap := utils.SliceToMap(owners, func(e *relationTb.GroupMemberModel) string { + return e.GroupID + }) + if ids := utils.Single(groupIDs, utils.Keys(ownerMap)); len(ids) > 0 { + return nil, errs.ErrData.Wrap("group no owner", strings.Join(ids, ",")) + } + groupMemberNum, err := s.GroupDatabase.MapGroupMemberNum(ctx, groupIDs) + if err != nil { + return nil, err + } + resp.GroupRequests = utils.Slice(requests, func(e *relationTb.GroupRequestModel) *sdkws.GroupRequest { + return convert.Db2PbGroupRequest(e, nil, convert.Db2PbGroupInfo(groupMap[e.GroupID], ownerMap[e.GroupID].UserID, uint32(groupMemberNum[e.GroupID]))) + }) + resp.Total = total + return resp, nil +} diff --git a/internal/rpc/user/user.go b/internal/rpc/user/user.go index f95e66c0d..fb6518a7e 100644 --- a/internal/rpc/user/user.go +++ b/internal/rpc/user/user.go @@ -17,10 +17,11 @@ package user import ( "context" "errors" - "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" "strings" "time" + "github.com/OpenIMSDK/Open-IM-Server/pkg/authverify" + "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/Open-IM-Server/pkg/common/config" @@ -50,11 +51,6 @@ type userServer struct { RegisterCenter registry.SvcDiscoveryRegistry } -func (s *userServer) SubscribeOrCancelUsersStatus(ctx context.Context, req *pbuser.SubscribeOrCancelUsersStatusReq) (*pbuser.SubscribeOrCancelUsersStatusResp, error) { - //TODO implement me - panic("implement me") -} - func Start(client registry.SvcDiscoveryRegistry, server *grpc.Server) error { db, err := relation.NewGormDB() if err != nil { @@ -246,3 +242,7 @@ func (s *userServer) GetAllUserID(ctx context.Context, req *pbuser.GetAllUserIDR } return &pbuser.GetAllUserIDResp{UserIDs: userIDs}, nil } + +func (s *userServer) SubscribeOrCancelUsersStatus(ctx context.Context, req *pbuser.SubscribeOrCancelUsersStatusReq) (resp *pbuser.SubscribeOrCancelUsersStatusResp, err error) { + panic("implement me") +} diff --git a/pkg/apistruct/manage.go b/pkg/apistruct/manage.go index 3a2945b37..eef38e403 100644 --- a/pkg/apistruct/manage.go +++ b/pkg/apistruct/manage.go @@ -33,7 +33,7 @@ type SendMsg struct { } type SendMsgReq struct { - RecvID string `json:"recvID" binding:"required_if" message:"recvID is required if sessionType is SingleChatType or NotificationChatType"` + RecvID string `json:"recvID" binding:"required_if" message:"recvID is required if sessionType is SingleChatType or NotificationChatType"` SendMsg } diff --git a/pkg/common/db/controller/friend.go b/pkg/common/db/controller/friend.go index c44c1662e..cab838bb4 100644 --- a/pkg/common/db/controller/friend.go +++ b/pkg/common/db/controller/friend.go @@ -75,6 +75,7 @@ type FriendDatabase interface { friendUserIDs []string, ) (friends []*relation.FriendModel, err error) FindFriendUserIDs(ctx context.Context, ownerUserID string) (friendUserIDs []string, err error) + FindBothFriendRequests(ctx context.Context, fromUserID, toUserID string) (friends []*relation.FriendRequestModel, err error) } type friendDatabase struct { @@ -363,3 +364,7 @@ func (f *friendDatabase) FindFriendUserIDs( ) (friendUserIDs []string, err error) { return f.cache.GetFriendIDs(ctx, ownerUserID) } + +func (f *friendDatabase) FindBothFriendRequests(ctx context.Context, fromUserID, toUserID string) (friends []*relation.FriendRequestModel, err error) { + return f.friendRequest.FindBothFriendRequests(ctx, fromUserID, toUserID) +} diff --git a/pkg/common/db/controller/group.go b/pkg/common/db/controller/group.go index af97d87ca..e2b5f90b3 100644 --- a/pkg/common/db/controller/group.go +++ b/pkg/common/db/controller/group.go @@ -110,6 +110,7 @@ type GroupDatabase interface { // GroupRequest CreateGroupRequest(ctx context.Context, requests []*relationTb.GroupRequestModel) error TakeGroupRequest(ctx context.Context, groupID string, userID string) (*relationTb.GroupRequestModel, error) + FindGroupRequests(ctx context.Context, groupID string, userIDs []string) (int64, []*relationTb.GroupRequestModel, error) PageGroupRequestUser( ctx context.Context, userID string, @@ -576,3 +577,7 @@ func (g *groupDatabase) CountTotal(ctx context.Context, before *time.Time) (coun func (g *groupDatabase) CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error) { return g.groupDB.CountRangeEverydayTotal(ctx, start, end) } + +func (g *groupDatabase) FindGroupRequests(ctx context.Context, groupID string, userIDs []string) (int64, []*relationTb.GroupRequestModel, error) { + return g.groupRequestDB.FindGroupRequests(ctx, groupID, userIDs) +} diff --git a/pkg/common/db/relation/friend_request_model.go b/pkg/common/db/relation/friend_request_model.go index 9f5cbbfee..d17cc680b 100644 --- a/pkg/common/db/relation/friend_request_model.go +++ b/pkg/common/db/relation/friend_request_model.go @@ -147,3 +147,14 @@ func (f *FriendRequestGorm) FindFromUserID( ) return } + +func (f *FriendRequestGorm) FindBothFriendRequests(ctx context.Context, fromUserID, toUserID string) (friends []*relation.FriendRequestModel, err error) { + err = utils.Wrap( + f.db(ctx). + Where("(from_user_id = ? AND to_user_id = ?) OR (from_user_id = ? AND to_user_id = ?)", fromUserID, toUserID, toUserID, fromUserID). + Find(&friends). + Error, + "", + ) + return +} diff --git a/pkg/common/db/relation/group_request_model.go b/pkg/common/db/relation/group_request_model.go index a38f1f782..7c0497aaf 100644 --- a/pkg/common/db/relation/group_request_model.go +++ b/pkg/common/db/relation/group_request_model.go @@ -110,3 +110,8 @@ func (g *GroupRequestGorm) PageGroup( showNumber, ) } + +func (g *GroupRequestGorm) FindGroupRequests(ctx context.Context, groupID string, userIDs []string) (total int64, groupRequests []*relation.GroupRequestModel, err error) { + err = g.DB.WithContext(ctx).Where("group_id = ? and user_id in ?", groupID, userIDs).Find(&groupRequests).Error + return int64(len(groupRequests)), groupRequests, utils.Wrap(err, utils.GetSelfFuncName()) +} diff --git a/pkg/common/db/table/relation/friend_request.go b/pkg/common/db/table/relation/friend_request.go index 794f33aaf..51ea0ef6e 100644 --- a/pkg/common/db/table/relation/friend_request.go +++ b/pkg/common/db/table/relation/friend_request.go @@ -61,6 +61,6 @@ type FriendRequestModelInterface interface { fromUserID string, pageNumber, showNumber int32, ) (friendRequests []*FriendRequestModel, total int64, err error) - + FindBothFriendRequests(ctx context.Context, fromUserID, toUserID string) (friends []*FriendRequestModel, err error) NewTx(tx any) FriendRequestModelInterface } diff --git a/pkg/common/db/table/relation/group_request.go b/pkg/common/db/table/relation/group_request.go index ba68bcd7b..063b83938 100644 --- a/pkg/common/db/table/relation/group_request.go +++ b/pkg/common/db/table/relation/group_request.go @@ -47,6 +47,7 @@ type GroupRequestModelInterface interface { Delete(ctx context.Context, groupID string, userID string) (err error) UpdateHandler(ctx context.Context, groupID string, userID string, handledMsg string, handleResult int32) (err error) Take(ctx context.Context, groupID string, userID string) (groupRequest *GroupRequestModel, err error) + FindGroupRequests(ctx context.Context, groupID string, userIDs []string) (int64, []*GroupRequestModel, error) Page( ctx context.Context, userID string, diff --git a/pkg/rpcclient/notification/conevrsation.go b/pkg/rpcclient/notification/conevrsation.go index 9c8066d00..a5f57c570 100644 --- a/pkg/rpcclient/notification/conevrsation.go +++ b/pkg/rpcclient/notification/conevrsation.go @@ -31,23 +31,22 @@ func NewConversationNotificationSender(msgRpcClient *rpcclient.MessageRpcClient) } // SetPrivate调用. -func (c *ConversationNotificationSender) ConversationSetPrivateNotification( - ctx context.Context, - sendID, recvID string, - isPrivateChat bool, -) error { +func (c *ConversationNotificationSender) ConversationSetPrivateNotification(ctx context.Context, sendID, recvID string, + isPrivateChat bool, conversationID string) error { tips := &sdkws.ConversationSetPrivateTips{ - RecvID: recvID, - SendID: sendID, - IsPrivate: isPrivateChat, + RecvID: recvID, + SendID: sendID, + IsPrivate: isPrivateChat, + ConversationID: conversationID, } return c.Notification(ctx, sendID, recvID, constant.ConversationPrivateChatNotification, tips) } // 会话改变. -func (c *ConversationNotificationSender) ConversationChangeNotification(ctx context.Context, userID string) error { +func (c *ConversationNotificationSender) ConversationChangeNotification(ctx context.Context, userID string, conversationIDs []string) error { tips := &sdkws.ConversationUpdateTips{ - UserID: userID, + UserID: userID, + ConversationIDList: conversationIDs, } return c.Notification(ctx, userID, userID, constant.ConversationChangeNotification, tips) } From a86d24cfd105874417a215cfb6bc15b7dc0e644d Mon Sep 17 00:00:00 2001 From: Xinwei Xiong <3293172751@qq.com> Date: Thu, 27 Jul 2023 14:38:46 +0800 Subject: [PATCH 09/11] feat: config env optimize (#680) Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> --- .env | 6 +- config/config.yaml | 282 +++++++++++++++++++++++++++++++-------------- install.sh | 8 +- 3 files changed, 205 insertions(+), 91 deletions(-) diff --git a/.env b/.env index e7971a07a..910d864b8 100644 --- a/.env +++ b/.env @@ -1,5 +1,5 @@ USER=root PASSWORD=openIM123 -MINIO_ENDPOINT=http://116.30.3.80:10005 -API_URL=http://116.30.3.80:10002/object/ -DATA_DIR=./ +MINIO_ENDPOINT=http://113.99.98.99:10005 +API_URL=http://113.99.98.99:10002/object/ +DATA_DIR=./ \ No newline at end of file diff --git a/config/config.yaml b/config/config.yaml index 3f43a7a69..74c864fc3 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -12,81 +12,133 @@ # See the License for the specific language governing permissions and # limitations under the License. -#OpenIM config - - -#---------------Infrastructure configuration---------------------# +# ----------------------------------------------------------------- +# Infrastructural configurations, please modify based on your setup +# ----------------------------------------------------------------- + +###################### Zookeeper ###################### +# Zookeeper configuration +# It's not recommended to modify the schema +# +# Zookeeper address +# Zookeeper username +# Zookeeper password zookeeper: - schema: openim #不建议修改 - address: [ 127.0.0.1:2181 ] # - username: #用户名 - password: #密码 - + schema: openim + address: [ 127.0.0.1:2181 ] + username: + password: + +###################### Mysql ###################### +# MySQL configuration +# Currently, only single machine setup is supported +# +# Maximum number of open connections +# Maximum number of idle connections +# Maximum lifetime in seconds a connection can be reused +# Log level: 1=slient, 2=error, 3=warn, 4=info +# Slow query threshold in milliseconds mysql: - address: [ 127.0.0.1:13306 ] #目前仅支持单机 + address: [ 127.0.0.1:13306 ] username: root password: openIM123 - database: openIM_v3 #不建议修改 - maxOpenConn: 1000 #最大连接数 - maxIdleConn: 100 #最大空闲连接数 - maxLifeTime: 60 #连接可以重复使用的最长时间(秒) - logLevel: 4 #日志级别 1=slient 2=error 3=warn 4=info - slowThreshold: 500 #慢语句阈值 (毫秒) - + database: openIM_v3 + maxOpenConn: 1000 + maxIdleConn: 100 + maxLifeTime: 60 + logLevel: 4 + slowThreshold: 500 + +###################### Mongo ###################### +# MongoDB configuration +# If uri is not empty, it will be used directly +# +# MongoDB address for standalone setup, Mongos address for sharded cluster setup +# Default MongoDB database name +# Maximum connection pool size mongo: - uri: #不为空则直接使用该值 - address: [ 127.0.0.1:37017 ] #单机时为mongo地址,使用分片集群时,为mongos地址 - database: openIM_v3 #mongo db 默认即可 + uri: + address: [ 127.0.0.1:37017 ] + database: openIM_v3 username: root password: openIM123 - maxPoolSize: 100 + maxPoolSize: 100 +###################### Redis ###################### +# Redis configuration +# +# Username is required only for Redis version 6.0+ redis: - address: [ 127.0.0.1:16379 ] # - username: #only redis version 6.0+ need username + address: [ 127.0.0.1:16379 ] + username: password: openIM123 +###################### Kafka ###################### +# Kafka configuration +# +# Kafka username +# Kafka password +# It's not recommended to modify this topic name +# Consumer group ID, it's not recommended to modify kafka: - username: #用户名 - password: #密码 - addr: [ 127.0.0.1:9092 ] # + username: + password: + addr: [ 127.0.0.1:9092 ] latestMsgToRedis: - topic: "latestMsgToRedis" #不建议修改 + topic: "latestMsgToRedis" offlineMsgToMongo: - topic: "offlineMsgToMongoMysql" #不建议修改 + topic: "offlineMsgToMongoMysql" msgToPush: - topic: "msgToPush" #不建议修改 - consumerGroupID: #消费者组,不建议修改 - msgToRedis: redis # - msgToMongo: mongo # - msgToMySql: mysql # - msgToPush: push # - - + topic: "msgToPush" + consumerGroupID: + msgToRedis: redis + msgToMongo: mongo + msgToMySql: mysql + msgToPush: push + +###################### RPC ###################### +# RPC configuration +# +# IP address to register with zookeeper when starting RPC, the IP and corresponding rpcPort should be accessible by api/gateway +# Default listen IP is 0.0.0.0 rpc: - registerIP: #作为rpc启动时,注册到zookeeper的IP,api/gateway能访问到此ip和对应的rpcPort中的端口 - listenIP: #默认为0.0.0.0 - + registerIP: + listenIP: 0.0.0.0 +###################### API ###################### +# API configuration +# +# API service port +# Default listen IP is 0.0.0.0 api: - openImApiPort: [ 10002 ] #api服务端口 - listenIP: #默认为0.0.0.0 + openImApiPort: [ 10002 ] + listenIP: 0.0.0.0 +###################### Gateway ###################### +# Object storage configuration +# +# Use minio for object storage +# API URL should be accessible by the app +# It's not recommended to modify the bucket name +# Endpoint should be accessible by the app +# Session token +# Configuration for Tencent COS +# Configuration for Aliyun OSS object: - enable: "minio" #使用minio - apiURL: "http://127.0.0.1:10002/object/" #地址需要app能访问到 + enable: "minio" + apiURL: "http://127.0.0.1:10002/object/" minio: - bucket: "openim" #不建议修改 - endpoint: http://127.0.0.1:10005 #地址需要app能够访问 + bucket: "openim" + endpoint: http://127.0.0.1:10005 accessKeyID: root secretAccessKey: openIM123 - sessionToken: "" #token - cos: #tencent cos + sessionToken: "" + cos: bucketURL: "https://temp-1252357374.cos.ap-chengdu.myqcloud.com" secretID: "" secretKey: "" sessionToken: "" - oss: #ali oss + oss: endpoint: "https://oss-cn-chengdu.aliyuncs.com" bucket: "demo-9999999" bucketURL: "https://demo-9999999.oss-cn-chengdu.aliyuncs.com" @@ -94,7 +146,11 @@ object: accessKeySecret: "" sessionToken: "" -rpcPort: #rpc服务端口,不建议修改,端口由脚本读取后传入程序,如启动多个程序,只需要填入多个端口,用逗号隔开,如 [10110, 10111] +# RPC service ports +# These ports are passed into the program by the script and are not recommended to modify +# For launching multiple programs, just fill in multiple ports separated by commas +# For example, [10110, 10111] +rpcPort: openImUserPort: [ 10110 ] openImFriendPort: [ 10120 ] openImMessagePort: [ 10130 ] @@ -105,7 +161,8 @@ rpcPort: #rpc服务端口,不建议修改,端口由脚本读取后传入程 openImConversationPort: [ 10180 ] openImThirdPort: [ 10190 ] -rpcRegisterName: #rpc注册服务名,不建议修改 +# RPC service names for registration, it's not recommended to modify these +rpcRegisterName: openImUserName: User openImFriendName: Friend openImMsgName: Msg @@ -116,78 +173,132 @@ rpcRegisterName: #rpc注册服务名,不建议修改 openImConversationName: Conversation openImThirdName: Third +# Log configuration +# +# Storage directory +# Log rotation time +# Maximum number of logs to retain +# Log level, 6 means all levels +# Whether to output to stdout +# Whether to output in json format +# Whether to include stack trace in logs log: - storageLocation: ../../../../../logs/ #存放目录 - rotationTime: 24 #日志旋转时间 - remainRotationCount: 2 #日志数量 - remainLogLevel: 6 #日志级别 6表示全都打印, - isStdout: false - isJson: false + storageLocation: ../../../../../logs/ + rotationTime: 24 + remainRotationCount: 2 + remainLogLevel: 6 + isStdout: false + isJson: false withStack: false +# Long connection server configuration +# +# Websocket port for msg_gateway +# Maximum number of websocket connections +# Maximum length of websocket request package +# Websocket connection handshake timeout longConnSvr: - openImWsPort: [ 10001 ] #msg_gateway的websocket端口 - websocketMaxConnNum: 100000 #websocket最大连接数 - websocketMaxMsgLen: 4096 #websocket请求包最大长度 - websocketTimeout: 10 #websocket连接握手超时时间 + openImWsPort: [ 10001 ] + websocketMaxConnNum: 100000 + websocketMaxMsgLen: 4096 + websocketTimeout: 10 +# Push notification service configuration +# +# Use GeTui for push notifications +# GeTui offline push configuration +# FCM offline push configuration +# Account file, place it in the config directory +# JPush configuration, modify these after applying in JPush backend push: enable: getui - geTui: #个推离线推送 + geTui: pushUrl: "https://restapi.getui.com/v2/$appId" masterSecret: "" appKey: "" intent: "" channelID: "" channelName: "" - fcm: #fcm离线推送 - serviceAccount: "x.json" #帐号文件,并放在 config目录下 - jpns: #极光推送 在极光后台申请后,修改以下四项 + fcm: + serviceAccount: "x.json" + jpns: appKey: masterSecret: pushUrl: pushIntent: +# App manager configuration +# +# Built-in app manager user IDs +# Built-in app manager nicknames manager: - userID: [ "openIM123456","openIM654321","openIMAdmin" ] #内置的app管理员userID - nickname: [ "system1","system2", "system3" ] #内置的app管理员nickname + userID: [ "openIM123456","openIM654321","openIMAdmin" ] + nickname: [ "system1","system2", "system3" ] +# Multi-platform login policy +# For each platform(Android, iOS, Windows, Mac, web), only one can be online at a time +multiLoginPolicy: 1 +# Whether to store messages in MySQL, messages in MySQL are only used for management background +chatPersistenceMysql: true +# Message cache timeout in seconds, it's not recommended to modify +msgCacheTimeout: 86400 -multiLoginPolicy: 1 #多平台登录:Android、iOS、Windows、Mac、web 每种平台只能有一个在线 +# Whether to enable read receipts for group chat +groupMessageHasReadReceiptEnable: true +# Whether to enable read receipts for single chat +singleMessageHasReadReceiptEnable: true -chatPersistenceMysql: true #消息是否存入mysql,mysql中的消息仅用于管理后台使用 -msgCacheTimeout: 86400 #信消息缓存时间秒,不建议修改 -groupMessageHasReadReceiptEnable: true #群聊已读是否开启 -singleMessageHasReadReceiptEnable: true #单聊已读是否开启 +# MongoDB offline message retention period in days +retainChatRecords: 365 -retainChatRecords: 365 #mongo保存离线消息时间(天) -chatRecordsClearTime: "0 2 * * 3" #每周三凌晨2点清理mongo中的过期(超过retainChatRecords时间)消息,这个删除是为了清理满足上个配置retainChatRecords的过期消息,不会发送通知,仅仅作为清理磁盘使用 -msgDestructTime: "0 2 * * *" #消息自动删除时间,每天凌晨2点删除过期消息,这个删除是为了删除保留时间超过超过会话字段msg_destruct_time(秒)的消息。 +# Schedule to clear expired messages(older than retainChatRecords days) in MongoDB every Wednesday at 2am +# This deletion is just for cleaning up disk usage according to previous configuration retainChatRecords, no notification will be sent +chatRecordsClearTime: "0 2 * * 3" +# Schedule to auto delete messages every day at 2am +# This deletion is for messages that have been retained for more than msg_destruct_time (seconds) in the conversation field +msgDestructTime: "0 2 * * *" + +# Secret key secret: openIM123 +# Token policy +# +# Token expiration period in days tokenPolicy: - expire: 90 #过期时间(天) + expire: 90 +# Message verification policy +# +# Whether to verify friendship when sending messages messageVerify: - friendVerify: false #发送消息时是否验证好友关系 + friendVerify: false -#ios系统推送声音以及标记计数 +# iOS push notification configuration +# +# iOS push notification sound +# Whether to count badge +# Whether it's production environment iosPush: pushSound: "xxx" badgeCount: true production: false +# Callback configuration +# +# Callback URL +# Whether to enable this callback event +# Timeout in seconds +# Whether to continue execution if callback fails callback: - # 回调callback url: beforeSendSingleMsg: - enable: false #是否启用此回调事件 - timeout: 5 #超时时间(秒) - failedContinue: true #如回调失败是否继续往后执行 + enable: false + timeout: 5 + failedContinue: true afterSendSingleMsg: enable: false timeout: 5 @@ -244,8 +355,11 @@ callback: timeout: 5 failedContinue: true - -prometheus: #prometheus每个服务的端口数量需要和rpcPort保持对应 +###################### Prometheus ###################### +# Prometheus configuration +# The number of Prometheus ports per service needs to correspond to rpcPort +# The number of ports needs to be consistent with msg_transfer_service_num in script/path_info.sh +prometheus: enable: false userPrometheusPort: [ 20110 ] friendPrometheusPort: [ 20120 ] @@ -257,4 +371,4 @@ prometheus: #prometheus每个服务的端口数量需要和rpcPort保持对应 conversationPrometheusPort: [ 20230 ] rtcPrometheusPort: [ 21300 ] thirdPrometheusPort: [ 21301 ] - messageTransferPrometheusPort: [ 21400, 21401, 21402, 21403 ] #端口数量需要和script/path_info.sh中的msg_transfer_service_num保持一致 \ No newline at end of file + messageTransferPrometheusPort: [ 21400, 21401, 21402, 21403 ] \ No newline at end of file diff --git a/install.sh b/install.sh index 0c3a3945e..f9b9b7ec4 100755 --- a/install.sh +++ b/install.sh @@ -308,9 +308,9 @@ function cmd_help() { color_echo ${BLUE_PREFIX} "-cn, --china ${CYAN_PREFIX}set to use the Chinese domestic proxy${COLOR_SUFFIX}" color_echo ${BLUE_PREFIX} "-t, --tag ${CYAN_PREFIX}specify the tag (default option, set to latest if not specified)${COLOR_SUFFIX}" color_echo ${BLUE_PREFIX} "-r, --release ${CYAN_PREFIX}specify the release branch (cannot be used with the tag option)${COLOR_SUFFIX}" - color_echo ${BLUE_PREFIX} "-g, --go-version ${CYAN_PREFIX}set the Go language version (default: GO_VERSION=\"1.18\")${COLOR_SUFFIX}" - color_echo ${BLUE_PREFIX} "-i, --install-dir ${CYAN_PREFIX}set the OpenIM installation directory (default: /tmp)${COLOR_SUFFIX}" color_echo ${BLUE_PREFIX} "-gt, --github-token ${CYAN_PREFIX}set the GITHUB_TOKEN (default: not set)${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "-g, --go-version ${CYAN_PREFIX}set the Go language version (default: GO_VERSION=\"1.18\")${COLOR_SUFFIX}" + color_echo ${BLUE_PREFIX} "--install-dir ${CYAN_PREFIX}set the OpenIM installation directory (default: /tmp)${COLOR_SUFFIX}" color_echo ${BLUE_PREFIX} "--cpu ${CYAN_PREFIX}set the number of concurrent processes${COLOR_SUFFIX}" echo color_echo ${RED_PREFIX} "Note: Only one of the -t/--tag or -r/--release options can be used at a time.${COLOR_SUFFIX}" @@ -380,7 +380,7 @@ function parseinput() { shift GO_VERSION=$1 ;; - -i|--install-dir) + --install-dir) shift INSTALL_DIR=$1 ;; @@ -435,7 +435,7 @@ print_progress() { color_echo() { COLOR=$1 shift - echo -e "${COLOR}===> $* ${COLOR_SUFFIX}" + echo -e "${COLOR} $* ${COLOR_SUFFIX}" } # Color definitions From 68e521d198a6292514766548c32aa6b416a9a4ea Mon Sep 17 00:00:00 2001 From: Xinwei Xiong <3293172751@qq.com> Date: Thu, 27 Jul 2023 15:00:33 +0800 Subject: [PATCH 10/11] feat: config env optimize (#683) * feat: config env optimize Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> * docs: add readme docs Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> --------- Signed-off-by: Xinwei Xiong(cubxxw-openim) <3293172751nss@gmail.com> --- config/config.yaml | 2 +- scripts/README.md | 70 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/config/config.yaml b/config/config.yaml index 74c864fc3..6bf3cf06e 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -142,7 +142,7 @@ object: endpoint: "https://oss-cn-chengdu.aliyuncs.com" bucket: "demo-9999999" bucketURL: "https://demo-9999999.oss-cn-chengdu.aliyuncs.com" - accessKeyID: "" + accessKeyID: root accessKeySecret: "" sessionToken: "" diff --git a/scripts/README.md b/scripts/README.md index 04bda00ae..da21596d6 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -1,4 +1,72 @@ -# `/scripts` +# OpenIM Scripts Directory Structure + +This document outlines the directory structure for scripts in the OpenIM Server project. These scripts play a critical role in various areas like building, deploying, running and managing the services of OpenIM. + +```bash +scripts/ +├── LICENSE # License related files +│ ├── LICENSE # The license file +│ └── LICENSE_TEMPLATES # Template for license file +├── README.md # Readme file for scripts directory +├── advertise.sh # Script for advertisement services +├── batch_start_all.sh # Script to start all services in batch +├── build.cmd # Windows build command script +├── build_all_service.sh # Script to build all services +├── build_push_k8s_images.sh # Script to build and push images for Kubernetes +├── check_all.sh # Script to check status of all services +├── common.sh # Contains common functions used by other scripts +├── coverage.awk # AWK script for coverage report generation +├── coverage.sh # Script for generating coverage reports +├── docker_check_service.sh # Docker specific service check script +├── docker_start_all.sh # Script to start all services in a docker environment +├── ensure_tag.sh # Script to ensure proper tagging of docker images +├── enterprise # Scripts specific to enterprise version +│ ├── check_all.sh # Check status of all enterprise services +│ ├── function.sh # Functions specific to enterprise version +│ └── path_info.cfg # Path information configuration for enterprise version +├── env_check.sh # Script to check the environment +├── function.sh # Contains functions used by other scripts +├── githooks # Git hook scripts +│ ├── commit-msg # Script to validate commit message +│ ├── pre-commit # Script to run before each commit +│ └── pre-push # Script to run before each push +├── init_pwd.sh # Script to initialize password +├── install_im_compose.sh # Script to install IM with Docker Compose +├── install_im_server.sh # Script to install IM server +├── lib # Library scripts +│ ├── color.sh # Script for console color manipulation +│ ├── golang.sh # Script for golang related utility functions +│ ├── init.sh # Script for initialization tasks +│ ├── logging.sh # Script for logging related utility functions +│ ├── release.sh # Script for release related utility functions +│ ├── util.sh # Script for generic utility functions +│ └── version.sh # Script for versioning related tasks +├── make-rules # Makefile rules +│ ├── common.mk # Common Make rules +│ ├── copyright.mk # Copyright related Make rules +│ ├── dependencies.mk # Dependencies related Make rules +│ ├── gen.mk # Make rules for code generation +│ ├── golang.mk # Golang specific Make rules +│ ├── image.mk # Make rules for image building +│ ├── release.mk # Make rules for release process +│ ├── swagger.mk # Make rules for swagger documentation +│ └── tools.mk # Make rules for tools and utilities +├── mongo-init.sh # Script to initialize MongoDB +├── msg_gateway_start.sh # Script to start message gateway service +├── msg_transfer_start.sh # Script to start message transfer service +├── path_info.sh # Script containing path information +├── push_start.sh # Script to start push service +├── release.sh # Script to perform release process +├── start_all.sh # Script to start all services +├── start_cron.sh # Script to start cron jobs +├── start_rpc_service.sh # Script to start RPC service +├── stop_all.sh # Script to stop all services +└── style_info.sh # Script containing style related information +``` + +The purpose of having a structured scripts directory like this is to make the operations of OpenIM Server clear and easy to manage. Each script has its own responsibility, making it easier to maintain and update. It's also helpful for newcomers who can easily understand what each part of the system is doing by just looking at this directory structure. + +Each directory and script in the structure should be understood as a part of a larger whole. All scripts work together to ensure the smooth operation and maintenance of the OpenIM Server. ## Supported platforms From df710495333b6ce8e6783a551bccba4273269c09 Mon Sep 17 00:00:00 2001 From: WangchuXiao Date: Thu, 27 Jul 2023 15:04:50 +0800 Subject: [PATCH 11/11] fix bug: delete friend and repeat add friend not effect (#682) * new feature: add batch send msg * new feature: add batch send msg * new feature: add batch send msg * new feature: add batch send msg * new feature: add batch send msg * new feature: add batch send msg * fix bug: multiple gateway kick user * fix bug: multiple gateway kick user * fix bug: multiple gateway kick user * fix bug: multiple gateway kick user * fix bug: multiple gateway kick user * MsgDestructTime * fix bug: msg destruct sql * fix bug: msg destruct * fix bug: msg destruct * fix bug: msg destruct sql * fix bug: msg destruct sql * fix bug: msg destruct sql * fix bug: msg destruct sql * debug: print stack * debug: print stack * debug: print stack * fix bug: msg destruct sql Signed-off-by: wangchuxiao * fix bug: msg notification self 2 self push twice * fix bug: heartbeat get self notification * fix bug: init grpc conn in one process * fix bug: grpc conn Signed-off-by: wangchuxiao * fix bug: zk client recreate node when reconn * fix bug: set friend mark args error * fix bug: rpc client intercepter called twice Signed-off-by: wangchuxiao * cicd: robot automated Change Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * test: document msg num set 100 * new feat: sync designated model * new feat: sync designated model * new feat: sync designated model * new feat: sync designated model * new feat: sync designated model * new feat: sync designated model * new feat: sync designated model * merge code * merge code * fix bug: repeat add friend not effect --------- Signed-off-by: wangchuxiao Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: wangchuxiao-dev --- pkg/common/db/controller/friend.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/common/db/controller/friend.go b/pkg/common/db/controller/friend.go index cab838bb4..0f9315c20 100644 --- a/pkg/common/db/controller/friend.go +++ b/pkg/common/db/controller/friend.go @@ -24,6 +24,7 @@ import ( "github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/table/relation" "github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/tools/errs" + "github.com/OpenIMSDK/tools/log" "github.com/OpenIMSDK/tools/mcontext" "github.com/OpenIMSDK/tools/tx" "github.com/OpenIMSDK/tools/utils" @@ -222,6 +223,7 @@ func (f *friendDatabase) AgreeFriendRequest( friendRequest *relation.FriendRequestModel, ) (err error) { return f.tx.Transaction(func(tx any) error { + defer log.ZDebug(ctx, "return line") now := time.Now() fr, err := f.friendRequest.NewTx(tx).Take(ctx, friendRequest.FromUserID, friendRequest.ToUserID) if err != nil { @@ -247,7 +249,7 @@ func (f *friendDatabase) AgreeFriendRequest( if err != nil { return err } - } else if errs.Unwrap(err) != gorm.ErrRecordNotFound { + } else if err != nil && errs.Unwrap(err) != gorm.ErrRecordNotFound { return err }