Compare commits
3 Commits
v3.3.3-rc.
...
v3.3.3-bat
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
035afff03e | ||
|
|
7e63800dee | ||
|
|
df50971744 |
3
.github/workflows/bot-cherry-pick.yml
vendored
3
.github/workflows/bot-cherry-pick.yml
vendored
@@ -19,8 +19,7 @@ on:
|
|||||||
jobs:
|
jobs:
|
||||||
cherry-pick:
|
cherry-pick:
|
||||||
name: Cherry Pick
|
name: Cherry Pick
|
||||||
# && github.event.comment.user.login=='kubbot'
|
if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '/cherry-pick') && github.event.comment.user.login=='kubbot'
|
||||||
if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '/cherry-pick')
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout the latest code
|
- name: Checkout the latest code
|
||||||
|
|||||||
9
.github/workflows/build-docker-image.yml
vendored
9
.github/workflows/build-docker-image.yml
vendored
@@ -120,15 +120,6 @@ jobs:
|
|||||||
uses: docker/metadata-action@v5.0.0
|
uses: docker/metadata-action@v5.0.0
|
||||||
with:
|
with:
|
||||||
images: ghcr.io/openimsdk/openim-server
|
images: ghcr.io/openimsdk/openim-server
|
||||||
# generate Docker tags based on the following events/attributes
|
|
||||||
tags: |
|
|
||||||
type=schedule
|
|
||||||
type=ref,event=branch
|
|
||||||
type=ref,event=pr
|
|
||||||
type=semver,pattern={{version}}
|
|
||||||
type=semver,pattern={{major}}.{{minor}}
|
|
||||||
type=semver,pattern={{major}}
|
|
||||||
type=sha
|
|
||||||
|
|
||||||
- name: Log in to GitHub Container Registry
|
- name: Log in to GitHub Container Registry
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v3
|
||||||
|
|||||||
43
.github/workflows/build-openim-web-image.yml
vendored
43
.github/workflows/build-openim-web-image.yml
vendored
@@ -114,12 +114,15 @@ jobs:
|
|||||||
uses: docker/setup-qemu-action@v3
|
uses: docker/setup-qemu-action@v3
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
# ghcr.io/openimsdk/openim-web:latest
|
|
||||||
- name: Extract metadata (tags, labels) for Docker
|
|
||||||
id: meta2
|
|
||||||
uses: docker/metadata-action@v5.0.0
|
|
||||||
with:
|
with:
|
||||||
images: ghcr.io/openimsdk/openim-web
|
install: true
|
||||||
|
- name: Cache Docker layers
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: /tmp/.buildx-cache
|
||||||
|
key: ${{ runner.os }}-buildx-${{ github.sha }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-buildx-
|
||||||
|
|
||||||
- name: Log in to GitHub Container Registry
|
- name: Log in to GitHub Container Registry
|
||||||
uses: docker/login-action@v3
|
uses: docker/login-action@v3
|
||||||
@@ -128,12 +131,38 @@ jobs:
|
|||||||
username: ${{ github.repository_owner }}
|
username: ${{ github.repository_owner }}
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
- name: Build and push Docker image
|
- name: Extract metadata (tags, labels) for Docker openim-web
|
||||||
|
id: meta1
|
||||||
|
uses: docker/metadata-action@v5.0.0
|
||||||
|
with:
|
||||||
|
images: ghcr.io/openimsdk/openim-web
|
||||||
|
|
||||||
|
- name: Build and push Docker image for openim-web
|
||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v5
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
file: ./build/images/openim-tools/openim-web/Dockerfile
|
file: ./build/images/openim-tools/openim-web/Dockerfile
|
||||||
platforms: linux/amd64,linux/arm64
|
platforms: linux/amd64,linux/arm64
|
||||||
push: ${{ github.event_name != 'pull_request' }}
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
|
tags: ${{ steps.meta1.outputs.tags }}
|
||||||
|
labels: ${{ steps.meta1.outputs.labels }}
|
||||||
|
cache-from: type=local,src=/tmp/.buildx-cache
|
||||||
|
cache-to: type=local,dest=/tmp/.buildx-cache
|
||||||
|
|
||||||
|
- name: Extract metadata (tags, labels) for Docker openim-web
|
||||||
|
id: meta2
|
||||||
|
uses: docker/metadata-action@v5.0.0
|
||||||
|
with:
|
||||||
|
images: ghcr.io/openimsdk/component
|
||||||
|
|
||||||
|
- name: Build and push Docker image for component
|
||||||
|
uses: docker/build-push-action@v5
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./build/images/openim-tools/component/Dockerfile
|
||||||
|
platforms: linux/amd64,linux/arm64
|
||||||
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
tags: ${{ steps.meta2.outputs.tags }}
|
tags: ${{ steps.meta2.outputs.tags }}
|
||||||
labels: ${{ steps.meta2.outputs.labels }}
|
labels: ${{ steps.meta2.outputs.labels }}
|
||||||
|
cache-from: type=local,src=/tmp/.buildx-cache
|
||||||
|
cache-to: type=local,dest=/tmp/.buildx-cache
|
||||||
|
|||||||
@@ -171,7 +171,7 @@ linters-settings:
|
|||||||
# exclude_godoc_examples: false
|
# exclude_godoc_examples: false
|
||||||
funlen:
|
funlen:
|
||||||
lines: 150
|
lines: 150
|
||||||
statements: 80
|
statements: 50
|
||||||
gci:
|
gci:
|
||||||
# put imports beginning with prefix after 3rd-party packages;
|
# put imports beginning with prefix after 3rd-party packages;
|
||||||
# only support one prefix
|
# only support one prefix
|
||||||
@@ -510,7 +510,7 @@ linters-settings:
|
|||||||
|
|
||||||
nestif:
|
nestif:
|
||||||
# minimal complexity of if statements to report, 5 by default
|
# minimal complexity of if statements to report, 5 by default
|
||||||
min-complexity: 6
|
min-complexity: 4
|
||||||
|
|
||||||
nilnil:
|
nilnil:
|
||||||
# By default, nilnil checks all returned types below.
|
# By default, nilnil checks all returned types below.
|
||||||
|
|||||||
@@ -98,11 +98,6 @@ OpenIM 我们的目标是建立一个顶级的开源社区。我们有一套标
|
|||||||
|
|
||||||
+ [接口标准](https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/interface.md)
|
+ [接口标准](https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/interface.md)
|
||||||
|
|
||||||
+ [OpenIM配置和环境变量设置](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md)
|
|
||||||
|
|
||||||
> **Note**
|
|
||||||
> 针对中国的用户,阅读我们的 [Docker 镜像标准](https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/images.md) 以便使用国内 aliyun 的镜像地址。OpenIM 也有针对中国的 gitee 同步仓库,你可以在 [gitee.com](https://gitee.com/openimsdk) 上找到它。
|
|
||||||
|
|
||||||
## :link: 链接
|
## :link: 链接
|
||||||
|
|
||||||
+ **[完整文档](https://doc.rentsoft.cn/)**
|
+ **[完整文档](https://doc.rentsoft.cn/)**
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ It is recommended to use Docker Compose for deployment, which can easily and qui
|
|||||||
<details> <summary>Compile from Source</summary>
|
<details> <summary>Compile from Source</summary>
|
||||||
|
|
||||||
|
|
||||||
Ur need `Go 1.20` or higher version, and `make`.
|
Ur need `Go 1.18` or higher version, and `make`.
|
||||||
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -212,8 +212,6 @@ Before you start, please make sure your changes are in demand. The best for that
|
|||||||
- [Interface Standards](https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/api.md)
|
- [Interface Standards](https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/api.md)
|
||||||
- [Log Standards](https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/logging.md)
|
- [Log Standards](https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/logging.md)
|
||||||
- [Error Code Standards](https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/error_code.md)
|
- [Error Code Standards](https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/error_code.md)
|
||||||
- [OpenIM configuration and environment variable Settings](https://github.com/openimsdk/open-im-server/blob/main/docs/contrib/environment.md)
|
|
||||||
|
|
||||||
|
|
||||||
## :busts_in_silhouette: Community
|
## :busts_in_silhouette: Community
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
FROM BASE_IMAGE
|
|
||||||
|
|
||||||
WORKDIR ${SERVER_WORKDIR}
|
|
||||||
|
|
||||||
# Set HTTP proxy
|
|
||||||
ARG BINARY_NAME
|
|
||||||
|
|
||||||
COPY BINARY_NAME ./bin/BINARY_NAME
|
|
||||||
|
|
||||||
ENTRYPOINT ["./bin/BINARY_NAME"]
|
|
||||||
@@ -26,75 +26,68 @@ import (
|
|||||||
"github.com/OpenIMSDK/protocol/constant"
|
"github.com/OpenIMSDK/protocol/constant"
|
||||||
"github.com/OpenIMSDK/tools/discoveryregistry"
|
"github.com/OpenIMSDK/tools/discoveryregistry"
|
||||||
"github.com/OpenIMSDK/tools/log"
|
"github.com/OpenIMSDK/tools/log"
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/internal/api"
|
"github.com/openimsdk/open-im-server/v3/internal/api"
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
|
"github.com/openimsdk/open-im-server/v3/pkg/common/cmd"
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/db/cache"
|
"github.com/openimsdk/open-im-server/v3/pkg/common/db/cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var logger log.Logger // Assuming Logger is globally accessible and properly initialized
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
apiCmd := cmd.NewApiCmd()
|
apiCmd := cmd.NewApiCmd()
|
||||||
apiCmd.AddPortFlag()
|
apiCmd.AddPortFlag()
|
||||||
apiCmd.AddApi(run)
|
apiCmd.AddApi(run)
|
||||||
if err := apiCmd.Execute(); err != nil {
|
if err := apiCmd.Execute(); err != nil {
|
||||||
log.ZError(context.Background(), "API command execution failed", err)
|
logger.Error(context.Background(), "API command execution failed", err)
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(port int) error {
|
func run(port int) error {
|
||||||
log.ZInfo(context.Background(), "Openim api port:", "port", port)
|
logger.Info(context.Background(), "Openim api port:", "port", port)
|
||||||
|
|
||||||
if port == 0 {
|
if port == 0 {
|
||||||
err := "port is empty"
|
err := "port is empty"
|
||||||
log.ZError(context.Background(), err, nil)
|
logger.Error(context.Background(), err, nil)
|
||||||
|
|
||||||
return fmt.Errorf(err)
|
return fmt.Errorf(err)
|
||||||
}
|
}
|
||||||
rdb, err := cache.NewRedis()
|
rdb, err := cache.NewRedis()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ZError(context.Background(), "Failed to initialize Redis", err)
|
logger.Error(context.Background(), "Failed to initialize Redis", err)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.ZInfo(context.Background(), "api start init discov client")
|
logger.Info(context.Background(), "api start init discov client")
|
||||||
|
|
||||||
var client discoveryregistry.SvcDiscoveryRegistry
|
var client discoveryregistry.SvcDiscoveryRegistry
|
||||||
|
|
||||||
// Determine whether zk is passed according to whether it is a clustered deployment
|
|
||||||
client, err = discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery)
|
client, err = discovery_register.NewDiscoveryRegister(config.Config.Envs.Discovery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ZError(context.Background(), "Failed to initialize discovery register", err)
|
logger.Error(context.Background(), "Failed to initialize discovery register", err)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = client.CreateRpcRootNodes(config.Config.GetServiceNames()); err != nil {
|
if err = client.CreateRpcRootNodes(config.Config.GetServiceNames()); err != nil {
|
||||||
log.ZError(context.Background(), "Failed to create RPC root nodes", err)
|
logger.Error(context.Background(), "Failed to create RPC root nodes", err)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.ZInfo(context.Background(), "api register public config to discov")
|
logger.Info(context.Background(), "api register public config to discov")
|
||||||
if err = client.RegisterConf2Registry(constant.OpenIMCommonConfigKey, config.Config.EncodeConfig()); err != nil {
|
if err = client.RegisterConf2Registry(constant.OpenIMCommonConfigKey, config.Config.EncodeConfig()); err != nil {
|
||||||
log.ZError(context.Background(), "Failed to register public config to discov", err)
|
logger.Error(context.Background(), "Failed to register public config to discov", err)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.ZInfo(context.Background(), "api register public config to discov success")
|
logger.Info(context.Background(), "api register public config to discov success")
|
||||||
router := api.NewGinRouter(client, rdb)
|
router := api.NewGinRouter(client, rdb)
|
||||||
log.ZInfo(context.Background(), "api init router success")
|
logger.Info(context.Background(), "api init router success")
|
||||||
var address string
|
var address string
|
||||||
if config.Config.Api.ListenIP != "" {
|
if config.Config.Api.ListenIP != "" {
|
||||||
address = net.JoinHostPort(config.Config.Api.ListenIP, strconv.Itoa(port))
|
address = net.JoinHostPort(config.Config.Api.ListenIP, strconv.Itoa(port))
|
||||||
} else {
|
} else {
|
||||||
address = net.JoinHostPort("0.0.0.0", strconv.Itoa(port))
|
address = net.JoinHostPort("0.0.0.0", strconv.Itoa(port))
|
||||||
}
|
}
|
||||||
log.ZInfo(context.Background(), "start api server", "address", address, "OpenIM version", config.Version)
|
logger.Info(context.Background(), "start api server", "address", address, "OpenIM version", config.Version)
|
||||||
|
|
||||||
err = router.Run(address)
|
err = router.Run(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ZError(context.Background(), "api run failed", err, "address", address)
|
logger.Error(context.Background(), "api run failed", err, "address", address)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -103,16 +103,6 @@ $ curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bas
|
|||||||
$ helm repo add brigade https://openimsdk.github.io/openim-charts
|
$ helm repo add brigade https://openimsdk.github.io/openim-charts
|
||||||
```
|
```
|
||||||
|
|
||||||
### OpenIM 的镜像策略
|
|
||||||
|
|
||||||
自动化提供的 aliyun, ghcr, docker hub: https://github.com/openimsdk/open-im-server/blob/main/docs/conversions/images.md
|
|
||||||
|
|
||||||
**本地化测试构建方法:**
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ make image
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
### 容器化安装
|
### 容器化安装
|
||||||
|
|
||||||
|
|||||||
@@ -38,38 +38,6 @@
|
|||||||
* 2.20.1. [General Configuration](#GeneralConfiguration)
|
* 2.20.1. [General Configuration](#GeneralConfiguration)
|
||||||
* 2.20.2. [Service-Specific Prometheus Ports](#Service-SpecificPrometheusPorts)
|
* 2.20.2. [Service-Specific Prometheus Ports](#Service-SpecificPrometheusPorts)
|
||||||
|
|
||||||
## 0. <a name='TableofContents'></a>OpenIM Config File
|
|
||||||
|
|
||||||
Ensuring that OpenIM operates smoothly requires clear direction on the configuration file's location. Here's a detailed step-by-step guide on how to provide this essential path to OpenIM:
|
|
||||||
|
|
||||||
1. **Using the Command-line Argument**:
|
|
||||||
|
|
||||||
+ **For Configuration Path**: When initializing OpenIM, you can specify the path to the configuration file directly using the `-c` or `--config_folder_path` option.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
❯ _output/bin/platforms/linux/amd64/openim-api --config_folder_path="/your/config/folder/path"
|
|
||||||
```
|
|
||||||
|
|
||||||
+ **For Port Specification**: Similarly, if you wish to designate a particular port, utilize the `-p` option followed by the desired port number.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
❯ _output/bin/platforms/linux/amd64/openim-api -p 1234
|
|
||||||
```
|
|
||||||
|
|
||||||
Note: If the port is not specified here, OpenIM will fetch it from the configuration file. Setting the port via environment variables isn't supported. We recommend consolidating settings in the configuration file for a more consistent and streamlined setup.
|
|
||||||
|
|
||||||
2. **Leveraging the Environment Variable**:
|
|
||||||
|
|
||||||
You have the flexibility to determine OpenIM's configuration path by setting an `OPENIMCONFIG` environment variable. This method provides a seamless way to instruct OpenIM without command-line parameters every time.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
export OPENIMCONFIG="/path/to/your/config"
|
|
||||||
```
|
|
||||||
|
|
||||||
3. **Relying on the Default Path**:
|
|
||||||
|
|
||||||
In scenarios where neither command-line arguments nor environment variables are provided, OpenIM will intuitively revert to the `config/` directory to locate its configuration.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## 1. <a name='OpenIMDeploymentGuide'></a>OpenIM Deployment Guide
|
## 1. <a name='OpenIMDeploymentGuide'></a>OpenIM Deployment Guide
|
||||||
|
|||||||
6
go.mod
6
go.mod
@@ -25,7 +25,7 @@ require (
|
|||||||
github.com/stretchr/testify v1.8.4
|
github.com/stretchr/testify v1.8.4
|
||||||
go.mongodb.org/mongo-driver v1.12.1
|
go.mongodb.org/mongo-driver v1.12.1
|
||||||
golang.org/x/image v0.13.0
|
golang.org/x/image v0.13.0
|
||||||
google.golang.org/api v0.148.0
|
google.golang.org/api v0.147.0
|
||||||
google.golang.org/grpc v1.59.0
|
google.golang.org/grpc v1.59.0
|
||||||
google.golang.org/protobuf v1.31.0
|
google.golang.org/protobuf v1.31.0
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
@@ -37,7 +37,7 @@ require github.com/google/uuid v1.3.1
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/IBM/sarama v1.41.3
|
github.com/IBM/sarama v1.41.3
|
||||||
github.com/OpenIMSDK/protocol v0.0.30
|
github.com/OpenIMSDK/protocol v0.0.27
|
||||||
github.com/OpenIMSDK/tools v0.0.14
|
github.com/OpenIMSDK/tools v0.0.14
|
||||||
github.com/aliyun/aliyun-oss-go-sdk v2.2.9+incompatible
|
github.com/aliyun/aliyun-oss-go-sdk v2.2.9+incompatible
|
||||||
github.com/go-redis/redis v6.15.9+incompatible
|
github.com/go-redis/redis v6.15.9+incompatible
|
||||||
@@ -128,7 +128,7 @@ require (
|
|||||||
google.golang.org/appengine v1.6.7 // indirect
|
google.golang.org/appengine v1.6.7 // indirect
|
||||||
google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97 // indirect
|
google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97 // indirect
|
||||||
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 // indirect
|
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 // indirect
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20231012201019-e917dd12ba7a // indirect
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c // indirect
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
14
go.sum
14
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/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
github.com/IBM/sarama v1.41.3 h1:MWBEJ12vHC8coMjdEXFq/6ftO6DUZnQlFYcxtOJFa7c=
|
github.com/IBM/sarama v1.41.3 h1:MWBEJ12vHC8coMjdEXFq/6ftO6DUZnQlFYcxtOJFa7c=
|
||||||
github.com/IBM/sarama v1.41.3/go.mod h1:Xxho9HkHd4K/MDUo/T/sOqwtX/17D33++E9Wib6hUdQ=
|
github.com/IBM/sarama v1.41.3/go.mod h1:Xxho9HkHd4K/MDUo/T/sOqwtX/17D33++E9Wib6hUdQ=
|
||||||
github.com/OpenIMSDK/protocol v0.0.30 h1:MiHO6PyQMR9ojBHNnSFxCHLmsoE2xZqaiYj975JiZnM=
|
github.com/OpenIMSDK/protocol v0.0.27 h1:ee1Y6idLahgJrTHkgWPboKIvCT9zvf7jitavVpffpPw=
|
||||||
github.com/OpenIMSDK/protocol v0.0.30/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y=
|
github.com/OpenIMSDK/protocol v0.0.27/go.mod h1:F25dFrwrIx3lkNoiuf6FkCfxuwf8L4Z8UIsdTHP/r0Y=
|
||||||
github.com/OpenIMSDK/tools v0.0.14 h1:WLof/+WxyPyRST+QkoTKubYCiV73uCLiL8pgnpH/yKQ=
|
github.com/OpenIMSDK/tools v0.0.14 h1:WLof/+WxyPyRST+QkoTKubYCiV73uCLiL8pgnpH/yKQ=
|
||||||
github.com/OpenIMSDK/tools v0.0.14/go.mod h1:eg+q4A34Qmu73xkY0mt37FHGMCMfC6CtmOnm0kFEGFI=
|
github.com/OpenIMSDK/tools v0.0.14/go.mod h1:eg+q4A34Qmu73xkY0mt37FHGMCMfC6CtmOnm0kFEGFI=
|
||||||
github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mod h1:1pk82RBxDY/JZnPQrtqHlUFfCctgdorsd9M06fMynOM=
|
github.com/QcloudApi/qcloud_sign_golang v0.0.0-20141224014652-e4130a326409/go.mod h1:1pk82RBxDY/JZnPQrtqHlUFfCctgdorsd9M06fMynOM=
|
||||||
@@ -131,7 +131,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
|
|||||||
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||||
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
||||||
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
|
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
|
||||||
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
|
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
|
||||||
@@ -428,8 +428,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
|
|||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
|
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
|
||||||
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
|
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
|
||||||
google.golang.org/api v0.148.0 h1:HBq4TZlN4/1pNcu0geJZ/Q50vIwIXT532UIMYoo0vOs=
|
google.golang.org/api v0.147.0 h1:Can3FaQo9LlVqxJCodNmeZW/ib3/qKAY3rFeXiHo5gc=
|
||||||
google.golang.org/api v0.148.0/go.mod h1:8/TBgwaKjfqTdacOJrOv2+2Q6fBDU1uHKK06oGSkxzU=
|
google.golang.org/api v0.147.0/go.mod h1:pQ/9j83DcmPd/5C9e2nFOdjjNkDZ1G+zkbK2uvdkJMs=
|
||||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||||
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
|
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
|
||||||
@@ -441,8 +441,8 @@ google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97 h1:SeZZZx0cP0fqUyA
|
|||||||
google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97/go.mod h1:t1VqOqqvce95G3hIDCT5FeO3YUc6Q4Oe24L/+rNMxRk=
|
google.golang.org/genproto v0.0.0-20231002182017-d307bd883b97/go.mod h1:t1VqOqqvce95G3hIDCT5FeO3YUc6Q4Oe24L/+rNMxRk=
|
||||||
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 h1:W18sezcAYs+3tDZX4F80yctqa12jcP1PUS2gQu1zTPU=
|
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97 h1:W18sezcAYs+3tDZX4F80yctqa12jcP1PUS2gQu1zTPU=
|
||||||
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97/go.mod h1:iargEX0SFPm3xcfMI0d1domjg0ZF4Aa0p2awqyxhvF0=
|
google.golang.org/genproto/googleapis/api v0.0.0-20231002182017-d307bd883b97/go.mod h1:iargEX0SFPm3xcfMI0d1domjg0ZF4Aa0p2awqyxhvF0=
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20231012201019-e917dd12ba7a h1:a2MQQVoTo96JC9PMGtGBymLp7+/RzpFc2yX/9WfFg1c=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c h1:jHkCUWkseRf+W+edG5hMzr/Uh1xkDREY4caybAq4dpY=
|
||||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20231012201019-e917dd12ba7a/go.mod h1:4cYg8o5yUbm77w8ZX00LhMVNl/YVBFJRYWDc0uYWMs0=
|
google.golang.org/genproto/googleapis/rpc v0.0.0-20231009173412-8bfb1ae86b6c/go.mod h1:4cYg8o5yUbm77w8ZX00LhMVNl/YVBFJRYWDc0uYWMs0=
|
||||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||||
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
||||||
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
|
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
|
||||||
|
|||||||
8
go.work
8
go.work
@@ -1,18 +1,16 @@
|
|||||||
go 1.19
|
go 1.19
|
||||||
|
|
||||||
|
|
||||||
use (
|
use (
|
||||||
.
|
.
|
||||||
./test/typecheck
|
./test/typecheck
|
||||||
./tools/changelog
|
./tools/changelog
|
||||||
./tools/component
|
|
||||||
./tools/data-conversion
|
|
||||||
./tools/imctl
|
|
||||||
//./tools/imctl
|
//./tools/imctl
|
||||||
./tools/infra
|
./tools/infra
|
||||||
./tools/ncpu
|
./tools/ncpu
|
||||||
./tools/openim-web
|
./tools/openim-web
|
||||||
./tools/url2im
|
|
||||||
./tools/versionchecker
|
./tools/versionchecker
|
||||||
./tools/yamlfmt
|
./tools/yamlfmt
|
||||||
|
./tools/component
|
||||||
|
./tools/url2im
|
||||||
|
./tools/data-conversion
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ REPO="Open-IM-Server"
|
|||||||
|
|
||||||
# Version of Go you want to use, make sure it is compatible with your OpenIM-Server requirements.
|
# 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.
|
# Default is 1.18, if you want to use a different version, replace accordingly.
|
||||||
GO_VERSION="1.20"
|
GO_VERSION="1.18"
|
||||||
|
|
||||||
# Default HTTP_PORT is 80. If you want to use a different port, uncomment and replace the value.
|
# Default HTTP_PORT is 80. If you want to use a different port, uncomment and replace the value.
|
||||||
# HTTP_PORT=80
|
# HTTP_PORT=80
|
||||||
@@ -168,7 +168,7 @@ function install_go() {
|
|||||||
command -v go >/dev/null 2>&1
|
command -v go >/dev/null 2>&1
|
||||||
# Determines if GO_VERSION is defined
|
# Determines if GO_VERSION is defined
|
||||||
if [ -z "$GO_VERSION" ]; then
|
if [ -z "$GO_VERSION" ]; then
|
||||||
GO_VERSION="1.20"
|
GO_VERSION="1.18"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $? -ne 0 ]]; then
|
if [[ $? -ne 0 ]]; then
|
||||||
@@ -309,7 +309,7 @@ function cmd_help() {
|
|||||||
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} "-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} "-r, --release ${CYAN_PREFIX}specify the release branch (cannot be used with the tag option)${COLOR_SUFFIX}"
|
||||||
color_echo ${BLUE_PREFIX} "-gt, --github-token ${CYAN_PREFIX}set the GITHUB_TOKEN (default: not set)${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.20\")${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} "--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}"
|
color_echo ${BLUE_PREFIX} "--cpu ${CYAN_PREFIX}set the number of concurrent processes${COLOR_SUFFIX}"
|
||||||
echo
|
echo
|
||||||
@@ -329,7 +329,7 @@ function parseinput() {
|
|||||||
# CHINA=false
|
# CHINA=false
|
||||||
# TAG=latest
|
# TAG=latest
|
||||||
# RELEASE=""
|
# RELEASE=""
|
||||||
# GO_VERSION=1.20
|
# GO_VERSION=1.18
|
||||||
# INSTALL_DIR=/tmp
|
# INSTALL_DIR=/tmp
|
||||||
# GITHUB_TOKEN=""
|
# GITHUB_TOKEN=""
|
||||||
# CPU=$(nproc)
|
# CPU=$(nproc)
|
||||||
|
|||||||
@@ -57,10 +57,6 @@ func (m MessageApi) newUserSendMsgReq(c *gin.Context, params *apistruct.SendMsg)
|
|||||||
var newContent string
|
var newContent string
|
||||||
options := make(map[string]bool, 5)
|
options := make(map[string]bool, 5)
|
||||||
switch params.ContentType {
|
switch params.ContentType {
|
||||||
case constant.OANotification:
|
|
||||||
notification := sdkws.NotificationElem{}
|
|
||||||
notification.Detail = utils.StructToJsonString(params.Content)
|
|
||||||
newContent = utils.StructToJsonString(¬ification)
|
|
||||||
case constant.Text:
|
case constant.Text:
|
||||||
fallthrough
|
fallthrough
|
||||||
case constant.Picture:
|
case constant.Picture:
|
||||||
@@ -73,6 +69,10 @@ func (m MessageApi) newUserSendMsgReq(c *gin.Context, params *apistruct.SendMsg)
|
|||||||
fallthrough
|
fallthrough
|
||||||
case constant.File:
|
case constant.File:
|
||||||
fallthrough
|
fallthrough
|
||||||
|
case constant.CustomNotTriggerConversation:
|
||||||
|
fallthrough
|
||||||
|
case constant.CustomOnlineOnly:
|
||||||
|
fallthrough
|
||||||
default:
|
default:
|
||||||
newContent = utils.StructToJsonString(params.Content)
|
newContent = utils.StructToJsonString(params.Content)
|
||||||
}
|
}
|
||||||
@@ -82,6 +82,11 @@ func (m MessageApi) newUserSendMsgReq(c *gin.Context, params *apistruct.SendMsg)
|
|||||||
if params.NotOfflinePush {
|
if params.NotOfflinePush {
|
||||||
utils.SetSwitchFromOptions(options, constant.IsOfflinePush, false)
|
utils.SetSwitchFromOptions(options, constant.IsOfflinePush, false)
|
||||||
}
|
}
|
||||||
|
if params.ContentType == constant.CustomOnlineOnly {
|
||||||
|
m.SetOptions(options, false)
|
||||||
|
} else if params.ContentType == constant.CustomNotTriggerConversation {
|
||||||
|
utils.SetSwitchFromOptions(options, constant.IsConversationUpdate, false)
|
||||||
|
}
|
||||||
pbData := msg.SendMsgReq{
|
pbData := msg.SendMsgReq{
|
||||||
MsgData: &sdkws.MsgData{
|
MsgData: &sdkws.MsgData{
|
||||||
SendID: params.SendID,
|
SendID: params.SendID,
|
||||||
@@ -100,6 +105,14 @@ func (m MessageApi) newUserSendMsgReq(c *gin.Context, params *apistruct.SendMsg)
|
|||||||
OfflinePushInfo: params.OfflinePushInfo,
|
OfflinePushInfo: params.OfflinePushInfo,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
//if params.ContentType == constant.OANotification {
|
||||||
|
// var tips sdkws.TipsComm
|
||||||
|
// tips.JsonDetail = utils.StructToJsonString(params.Content)
|
||||||
|
// pbData.MsgData.Content, err = proto.Marshal(&tips)
|
||||||
|
// if err != nil {
|
||||||
|
// log.ZError(c, "Marshal failed ", err, "tips", tips.String())
|
||||||
|
// }
|
||||||
|
//}
|
||||||
return &pbData
|
return &pbData
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,13 +180,15 @@ func (m *MessageApi) getSendMsgReq(c *gin.Context, req apistruct.SendMsg) (sendM
|
|||||||
data = apistruct.FileElem{}
|
data = apistruct.FileElem{}
|
||||||
case constant.Custom:
|
case constant.Custom:
|
||||||
data = apistruct.CustomElem{}
|
data = apistruct.CustomElem{}
|
||||||
|
case constant.Revoke:
|
||||||
|
data = apistruct.RevokeElem{}
|
||||||
case constant.OANotification:
|
case constant.OANotification:
|
||||||
data = apistruct.OANotificationElem{}
|
data = apistruct.OANotificationElem{}
|
||||||
req.SessionType = constant.NotificationChatType
|
req.SessionType = constant.NotificationChatType
|
||||||
if !authverify.IsManagerUserID(req.SendID) {
|
case constant.CustomNotTriggerConversation:
|
||||||
return nil, errs.ErrNoPermission.
|
data = apistruct.CustomElem{}
|
||||||
Wrap("only app manager can as sender send OANotificationElem")
|
case constant.CustomOnlineOnly:
|
||||||
}
|
data = apistruct.CustomElem{}
|
||||||
default:
|
default:
|
||||||
return nil, errs.ErrArgs.WithDetail("not support err contentType")
|
return nil, errs.ErrArgs.WithDetail("not support err contentType")
|
||||||
}
|
}
|
||||||
@@ -197,6 +212,7 @@ func (m *MessageApi) SendMessage(c *gin.Context) {
|
|||||||
apiresp.GinError(c, errs.ErrNoPermission.Wrap("only app manager can send message"))
|
apiresp.GinError(c, errs.ErrNoPermission.Wrap("only app manager can send message"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMsgReq, err := m.getSendMsgReq(c, req.SendMsg)
|
sendMsgReq, err := m.getSendMsgReq(c, req.SendMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ZError(c, "decodeData failed", err)
|
log.ZError(c, "decodeData failed", err)
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ func NewGinRouter(discov discoveryregistry.SvcDiscoveryRegistry, rdb redis.Unive
|
|||||||
userRouterGroup.POST("/get_users_online_status", ParseToken, u.GetUsersOnlineStatus)
|
userRouterGroup.POST("/get_users_online_status", ParseToken, u.GetUsersOnlineStatus)
|
||||||
userRouterGroup.POST("/get_users_online_token_detail", ParseToken, u.GetUsersOnlineTokenDetail)
|
userRouterGroup.POST("/get_users_online_token_detail", ParseToken, u.GetUsersOnlineTokenDetail)
|
||||||
userRouterGroup.POST("/subscribe_users_status", ParseToken, u.SubscriberStatus)
|
userRouterGroup.POST("/subscribe_users_status", ParseToken, u.SubscriberStatus)
|
||||||
|
userRouterGroup.POST("/unsubscribe_users_status", ParseToken, u.UnSubscriberStatus)
|
||||||
userRouterGroup.POST("/get_users_status", ParseToken, u.GetUserStatus)
|
userRouterGroup.POST("/get_users_status", ParseToken, u.GetUserStatus)
|
||||||
userRouterGroup.POST("/get_subscribe_users_status", ParseToken, u.GetSubscribeUsersStatus)
|
userRouterGroup.POST("/get_subscribe_users_status", ParseToken, u.GetSubscribeUsersStatus)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -190,6 +190,11 @@ func (u *UserApi) SubscriberStatus(c *gin.Context) {
|
|||||||
a2r.Call(user.UserClient.SubscribeOrCancelUsersStatus, u.Client, c)
|
a2r.Call(user.UserClient.SubscribeOrCancelUsersStatus, u.Client, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UnSubscriberStatus Unsubscribe a user's presence.
|
||||||
|
func (u *UserApi) UnSubscriberStatus(c *gin.Context) {
|
||||||
|
a2r.Call(user.UserClient.SubscribeOrCancelUsersStatus, u.Client, c)
|
||||||
|
}
|
||||||
|
|
||||||
// GetUserStatus Get the online status of the user.
|
// GetUserStatus Get the online status of the user.
|
||||||
func (u *UserApi) GetUserStatus(c *gin.Context) {
|
func (u *UserApi) GetUserStatus(c *gin.Context) {
|
||||||
a2r.Call(user.UserClient.GetUserStatus, u.Client, c)
|
a2r.Call(user.UserClient.GetUserStatus, u.Client, c)
|
||||||
|
|||||||
@@ -18,12 +18,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/tools/utils"
|
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RunWsAndServer run ws server
|
|
||||||
func RunWsAndServer(rpcPort, wsPort, prometheusPort int) error {
|
func RunWsAndServer(rpcPort, wsPort, prometheusPort int) error {
|
||||||
fmt.Println(
|
fmt.Println(
|
||||||
"start rpc/msg_gateway server, port: ",
|
"start rpc/msg_gateway server, port: ",
|
||||||
@@ -45,7 +42,7 @@ func RunWsAndServer(rpcPort, wsPort, prometheusPort int) error {
|
|||||||
go func() {
|
go func() {
|
||||||
err := hubServer.Start()
|
err := hubServer.Start()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(utils.Wrap1(err))
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
return hubServer.LongConnServer.Run()
|
return hubServer.LongConnServer.Run()
|
||||||
|
|||||||
@@ -16,12 +16,10 @@ package msgtransfer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register"
|
||||||
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/credentials/insecure"
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
|
"sync"
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register"
|
|
||||||
|
|
||||||
"github.com/OpenIMSDK/tools/mw"
|
"github.com/OpenIMSDK/tools/mw"
|
||||||
|
|
||||||
@@ -65,7 +63,8 @@ func StartTransfer(prometheusPort int) error {
|
|||||||
/*
|
/*
|
||||||
client, err := openkeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema,
|
client, err := openkeeper.NewClient(config.Config.Zookeeper.ZkAddr, config.Config.Zookeeper.Schema,
|
||||||
openkeeper.WithFreq(time.Hour), openkeeper.WithRoundRobin(), openkeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username,
|
openkeeper.WithFreq(time.Hour), openkeeper.WithRoundRobin(), openkeeper.WithUserNameAndPassword(config.Config.Zookeeper.Username,
|
||||||
config.Config.Zookeeper.Password), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger()))*/if err != nil {
|
config.Config.Zookeeper.Password), openkeeper.WithTimeout(10), openkeeper.WithLogger(log.NewZkLogger()))*/
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := client.CreateRpcRootNodes(config.Config.GetServiceNames()); err != nil {
|
if err := client.CreateRpcRootNodes(config.Config.GetServiceNames()); err != nil {
|
||||||
|
|||||||
@@ -283,30 +283,20 @@ func (och *OnlineHistoryRedisConsumerHandler) handleMsg(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if isNewConversation {
|
if isNewConversation {
|
||||||
switch storageList[0].SessionType {
|
if storageList[0].SessionType == constant.SuperGroupChatType {
|
||||||
case constant.SuperGroupChatType:
|
log.ZInfo(ctx, "group chat first create conversation", "conversationID", conversationID)
|
||||||
log.ZInfo(ctx, "group chat first create conversation", "conversationID",
|
|
||||||
conversationID)
|
|
||||||
userIDs, err := och.groupRpcClient.GetGroupMemberIDs(ctx, storageList[0].GroupID)
|
userIDs, err := och.groupRpcClient.GetGroupMemberIDs(ctx, storageList[0].GroupID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ZWarn(ctx, "get group member ids error", err, "conversationID",
|
log.ZWarn(ctx, "get group member ids error", err, "conversationID", conversationID)
|
||||||
conversationID)
|
|
||||||
} else {
|
} else {
|
||||||
if err := och.conversationRpcClient.GroupChatFirstCreateConversation(ctx,
|
if err := och.conversationRpcClient.GroupChatFirstCreateConversation(ctx, storageList[0].GroupID, userIDs); err != nil {
|
||||||
storageList[0].GroupID, userIDs); err != nil {
|
log.ZWarn(ctx, "single chat first create conversation error", err, "conversationID", conversationID)
|
||||||
log.ZWarn(ctx, "single chat first create conversation error", err,
|
|
||||||
"conversationID", conversationID)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case constant.SingleChatType, constant.NotificationChatType:
|
} else {
|
||||||
if err := och.conversationRpcClient.SingleChatFirstCreateConversation(ctx, storageList[0].RecvID,
|
if err := och.conversationRpcClient.SingleChatFirstCreateConversation(ctx, storageList[0].RecvID, storageList[0].SendID); err != nil {
|
||||||
storageList[0].SendID, conversationID, storageList[0].SessionType); err != nil {
|
log.ZWarn(ctx, "single chat first create conversation error", err, "conversationID", conversationID)
|
||||||
log.ZWarn(ctx, "single chat or notification first create conversation error", err,
|
|
||||||
"conversationID", conversationID, "sessionType", storageList[0].SessionType)
|
|
||||||
}
|
}
|
||||||
default:
|
|
||||||
log.ZWarn(ctx, "unknown session type", nil, "sessionType",
|
|
||||||
storageList[0].SessionType)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ func (mc *OnlineHistoryMongoConsumerHandler) handleChatWs2Mongo(
|
|||||||
log.ZError(ctx, "msgFromMQ.MsgData is empty", nil, "cMsg", cMsg)
|
log.ZError(ctx, "msgFromMQ.MsgData is empty", nil, "cMsg", cMsg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.ZInfo(ctx, "mongo consumer recv msg", "msgs", msgFromMQ.String())
|
log.ZInfo(ctx, "mongo consumer recv msg", "msgs", msgFromMQ.MsgData)
|
||||||
err = mc.msgDatabase.BatchInsertChat2DB(ctx, msgFromMQ.ConversationID, msgFromMQ.MsgData, msgFromMQ.LastSeq)
|
err = mc.msgDatabase.BatchInsertChat2DB(ctx, msgFromMQ.ConversationID, msgFromMQ.MsgData, msgFromMQ.LastSeq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ZError(
|
log.ZError(
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
|
|
||||||
firebase "firebase.google.com/go"
|
firebase "firebase.google.com/go"
|
||||||
"firebase.google.com/go/messaging"
|
"firebase.google.com/go/messaging"
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
@@ -40,17 +42,20 @@ type Fcm struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(cache cache.MsgModel) *Fcm {
|
func NewClient(cache cache.MsgModel) *Fcm {
|
||||||
projectRoot := config.GetProjectRoot()
|
opt := option.WithCredentialsFile(filepath.Join(config2.Root, "config", config.Config.Push.Fcm.ServiceAccount))
|
||||||
credentialsFilePath := filepath.Join(projectRoot, "config", config.Config.Push.Fcm.ServiceAccount)
|
|
||||||
opt := option.WithCredentialsFile(credentialsFilePath)
|
|
||||||
fcmApp, err := firebase.NewApp(context.Background(), nil, opt)
|
fcmApp, err := firebase.NewApp(context.Background(), nil, opt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
// auth
|
||||||
|
// fcmClient, err := fcmApp.Auth(context.Background())
|
||||||
|
// if err != nil {
|
||||||
|
// return
|
||||||
|
// }
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
fcmMsgClient, err := fcmApp.Messaging(ctx)
|
fcmMsgClient, err := fcmApp.Messaging(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
panic(err.Error())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &Fcm{fcmMsgCli: fcmMsgClient, cache: cache}
|
return &Fcm{fcmMsgCli: fcmMsgClient, cache: cache}
|
||||||
|
|||||||
@@ -58,7 +58,6 @@ func (c *ConsumerHandler) handleMs2PsChat(ctx context.Context, msg []byte) {
|
|||||||
}
|
}
|
||||||
sec := msgFromMQ.MsgData.SendTime / 1000
|
sec := msgFromMQ.MsgData.SendTime / 1000
|
||||||
nowSec := utils.GetCurrentTimestampBySecond()
|
nowSec := utils.GetCurrentTimestampBySecond()
|
||||||
log.ZDebug(ctx, "push msg", "msg", pbData.String(), "sec", sec, "nowSec", nowSec)
|
|
||||||
if nowSec-sec > 10 {
|
if nowSec-sec > 10 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -126,12 +126,13 @@ func (p *Pusher) Push2User(ctx context.Context, userIDs []string, msg *sdkws.Msg
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pusher) UnmarshalNotificationElem(bytes []byte, t interface{}) error {
|
func (p *Pusher) UnmarshalNotificationElem(bytes []byte, t interface{}) error {
|
||||||
var notification sdkws.NotificationElem
|
var notificationElem struct {
|
||||||
if err := json.Unmarshal(bytes, ¬ification); err != nil {
|
Detail string `json:"detail,omitempty"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(bytes, ¬ificationElem); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return json.Unmarshal([]byte(notificationElem.Detail), t)
|
||||||
return json.Unmarshal([]byte(notification.Detail), t)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pusher) Push2SuperGroup(ctx context.Context, groupID string, msg *sdkws.MsgData) (err error) {
|
func (p *Pusher) Push2SuperGroup(ctx context.Context, groupID string, msg *sdkws.MsgData) (err error) {
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ package conversation
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
||||||
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/protocol/constant"
|
"github.com/OpenIMSDK/protocol/constant"
|
||||||
@@ -112,10 +114,7 @@ func (c *conversationServer) SetConversation(ctx context.Context, req *pbconvers
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint
|
func (c *conversationServer) SetConversations(ctx context.Context, req *pbconversation.SetConversationsReq) (*pbconversation.SetConversationsResp, error) {
|
||||||
func (c *conversationServer) SetConversations(ctx context.Context,
|
|
||||||
req *pbconversation.SetConversationsReq,
|
|
||||||
) (*pbconversation.SetConversationsResp, error) {
|
|
||||||
if req.Conversation == nil {
|
if req.Conversation == nil {
|
||||||
return nil, errs.ErrArgs.Wrap("conversation must not be nil")
|
return nil, errs.ErrArgs.Wrap("conversation must not be nil")
|
||||||
}
|
}
|
||||||
@@ -125,8 +124,14 @@ func (c *conversationServer) SetConversations(ctx context.Context,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if groupInfo.Status == constant.GroupStatusDismissed {
|
if groupInfo.Status == constant.GroupStatusDismissed {
|
||||||
return nil, errs.ErrDismissedAlready.Wrap("group dismissed")
|
return nil, err
|
||||||
}
|
}
|
||||||
|
// for _, userID := range req.UserIDs {
|
||||||
|
// if _, err := c.groupRpcClient.GetGroupMemberCache(ctx, req.Conversation.GroupID, userID); err != nil {
|
||||||
|
// log.ZError(ctx, "user not in group", err, "userID", userID, "groupID", req.Conversation.GroupID)
|
||||||
|
// return nil, err
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
var unequal int
|
var unequal int
|
||||||
var conv tablerelation.ConversationModel
|
var conv tablerelation.ConversationModel
|
||||||
@@ -200,14 +205,7 @@ func (c *conversationServer) SetConversations(ctx context.Context,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, userID := range req.UserIDs {
|
for _, userID := range req.UserIDs {
|
||||||
err := c.conversationNotificationSender.ConversationSetPrivateNotification(ctx, userID, req.Conversation.UserID,
|
c.conversationNotificationSender.ConversationSetPrivateNotification(ctx, userID, req.Conversation.UserID, req.Conversation.IsPrivateChat.Value, req.Conversation.ConversationID)
|
||||||
req.Conversation.IsPrivateChat.Value, req.Conversation.ConversationID)
|
|
||||||
if err != nil {
|
|
||||||
log.ZWarn(ctx, "send conversation set private notification failed", err,
|
|
||||||
"userID", userID, "conversationID", req.Conversation.ConversationID)
|
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if req.Conversation.BurnDuration != nil {
|
if req.Conversation.BurnDuration != nil {
|
||||||
@@ -237,40 +235,24 @@ func (c *conversationServer) GetRecvMsgNotNotifyUserIDs(ctx context.Context, req
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create conversation without notification for msg redis transfer.
|
// create conversation without notification for msg redis transfer.
|
||||||
func (c *conversationServer) CreateSingleChatConversations(ctx context.Context,
|
func (c *conversationServer) CreateSingleChatConversations(ctx context.Context, req *pbconversation.CreateSingleChatConversationsReq) (*pbconversation.CreateSingleChatConversationsResp, error) {
|
||||||
req *pbconversation.CreateSingleChatConversationsReq,
|
var conversation tablerelation.ConversationModel
|
||||||
) (*pbconversation.CreateSingleChatConversationsResp, error) {
|
conversation.ConversationID = msgprocessor.GetConversationIDBySessionType(constant.SingleChatType, req.RecvID, req.SendID)
|
||||||
switch req.ConversationType {
|
conversation.ConversationType = constant.SingleChatType
|
||||||
case constant.SingleChatType:
|
conversation.OwnerUserID = req.SendID
|
||||||
var conversation tablerelation.ConversationModel
|
conversation.UserID = req.RecvID
|
||||||
conversation.ConversationID = req.ConversationID
|
err := c.conversationDatabase.CreateConversation(ctx, []*tablerelation.ConversationModel{&conversation})
|
||||||
conversation.ConversationType = req.ConversationType
|
if err != nil {
|
||||||
conversation.OwnerUserID = req.SendID
|
log.ZWarn(ctx, "create conversation failed", err, "conversation", conversation)
|
||||||
conversation.UserID = req.RecvID
|
|
||||||
err := c.conversationDatabase.CreateConversation(ctx, []*tablerelation.ConversationModel{&conversation})
|
|
||||||
if err != nil {
|
|
||||||
log.ZWarn(ctx, "create conversation failed", err, "conversation", conversation)
|
|
||||||
}
|
|
||||||
|
|
||||||
conversation2 := conversation
|
|
||||||
conversation2.OwnerUserID = req.RecvID
|
|
||||||
conversation2.UserID = req.SendID
|
|
||||||
err = c.conversationDatabase.CreateConversation(ctx, []*tablerelation.ConversationModel{&conversation2})
|
|
||||||
if err != nil {
|
|
||||||
log.ZWarn(ctx, "create conversation failed", err, "conversation2", conversation)
|
|
||||||
}
|
|
||||||
case constant.NotificationChatType:
|
|
||||||
var conversation tablerelation.ConversationModel
|
|
||||||
conversation.ConversationID = req.ConversationID
|
|
||||||
conversation.ConversationType = req.ConversationType
|
|
||||||
conversation.OwnerUserID = req.RecvID
|
|
||||||
conversation.UserID = req.SendID
|
|
||||||
err := c.conversationDatabase.CreateConversation(ctx, []*tablerelation.ConversationModel{&conversation})
|
|
||||||
if err != nil {
|
|
||||||
log.ZWarn(ctx, "create conversation failed", err, "conversation2", conversation)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conversation2 := conversation
|
||||||
|
conversation2.OwnerUserID = req.RecvID
|
||||||
|
conversation2.UserID = req.SendID
|
||||||
|
err = c.conversationDatabase.CreateConversation(ctx, []*tablerelation.ConversationModel{&conversation2})
|
||||||
|
if err != nil {
|
||||||
|
log.ZWarn(ctx, "create conversation failed", err, "conversation2", conversation)
|
||||||
|
}
|
||||||
return &pbconversation.CreateSingleChatConversationsResp{}, nil
|
return &pbconversation.CreateSingleChatConversationsResp{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -252,8 +252,7 @@ func (s *friendServer) GetDesignatedFriends(
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *friendServer) GetDesignatedFriendsApply(ctx context.Context,
|
func (s *friendServer) GetDesignatedFriendsApply(ctx context.Context, req *pbfriend.GetDesignatedFriendsApplyReq) (resp *pbfriend.GetDesignatedFriendsApplyResp, err error) {
|
||||||
req *pbfriend.GetDesignatedFriendsApplyReq) (resp *pbfriend.GetDesignatedFriendsApplyResp, err error) {
|
|
||||||
friendRequests, err := s.friendDatabase.FindBothFriendRequests(ctx, req.FromUserID, req.ToUserID)
|
friendRequests, err := s.friendDatabase.FindBothFriendRequests(ctx, req.FromUserID, req.ToUserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -154,16 +154,16 @@ func (s *groupServer) CheckGroupAdmin(ctx context.Context, groupID string) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *groupServer) GetPublicUserInfoMap(ctx context.Context, userIDs []string, complete bool) (map[string]*sdkws.PublicUserInfo, error) {
|
func (s *groupServer) GetUsernameMap(ctx context.Context, userIDs []string, complete bool) (map[string]string, error) {
|
||||||
if len(userIDs) == 0 {
|
if len(userIDs) == 0 {
|
||||||
return map[string]*sdkws.PublicUserInfo{}, nil
|
return map[string]string{}, nil
|
||||||
}
|
}
|
||||||
users, err := s.User.GetPublicUserInfos(ctx, userIDs, complete)
|
users, err := s.User.GetPublicUserInfos(ctx, userIDs, complete)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return utils.SliceToMapAny(users, func(e *sdkws.PublicUserInfo) (string, *sdkws.PublicUserInfo) {
|
return utils.SliceToMapAny(users, func(e *sdkws.PublicUserInfo) (string, string) {
|
||||||
return e.UserID, e
|
return e.UserID, e.Nickname
|
||||||
}), nil
|
}), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -468,18 +468,15 @@ func (s *groupServer) GetGroupAllMember(ctx context.Context, req *pbgroup.GetGro
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
publicUserInfoMap, err := s.GetPublicUserInfoMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) {
|
nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) {
|
||||||
return e.UserID, e.Nickname == "" || e.FaceURL == ""
|
return e.UserID, e.Nickname == ""
|
||||||
}), true)
|
}), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
|
resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
|
||||||
if e.Nickname == "" {
|
if e.Nickname == "" {
|
||||||
e.Nickname = publicUserInfoMap[e.UserID].Nickname
|
e.Nickname = nameMap[e.UserID]
|
||||||
}
|
|
||||||
if e.FaceURL == "" {
|
|
||||||
e.FaceURL = publicUserInfoMap[e.UserID].FaceURL
|
|
||||||
}
|
}
|
||||||
return convert.Db2PbGroupMember(e)
|
return convert.Db2PbGroupMember(e)
|
||||||
})
|
})
|
||||||
@@ -619,18 +616,15 @@ func (s *groupServer) GetGroupMembersInfo(ctx context.Context, req *pbgroup.GetG
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
publicUserInfoMap, err := s.GetPublicUserInfoMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) {
|
nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) {
|
||||||
return e.UserID, e.Nickname == "" || e.FaceURL == ""
|
return e.UserID, e.Nickname == ""
|
||||||
}), true)
|
}), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
|
resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
|
||||||
if e.Nickname == "" {
|
if e.Nickname == "" {
|
||||||
e.Nickname = publicUserInfoMap[e.UserID].Nickname
|
e.Nickname = nameMap[e.UserID]
|
||||||
}
|
|
||||||
if e.FaceURL == "" {
|
|
||||||
e.FaceURL = publicUserInfoMap[e.UserID].FaceURL
|
|
||||||
}
|
}
|
||||||
return convert.Db2PbGroupMember(e)
|
return convert.Db2PbGroupMember(e)
|
||||||
})
|
})
|
||||||
@@ -858,40 +852,32 @@ func (s *groupServer) JoinGroup(ctx context.Context, req *pbgroup.JoinGroupReq)
|
|||||||
|
|
||||||
func (s *groupServer) QuitGroup(ctx context.Context, req *pbgroup.QuitGroupReq) (*pbgroup.QuitGroupResp, error) {
|
func (s *groupServer) QuitGroup(ctx context.Context, req *pbgroup.QuitGroupReq) (*pbgroup.QuitGroupResp, error) {
|
||||||
resp := &pbgroup.QuitGroupResp{}
|
resp := &pbgroup.QuitGroupResp{}
|
||||||
if req.UserID == "" {
|
|
||||||
req.UserID = mcontext.GetOpUserID(ctx)
|
|
||||||
} else {
|
|
||||||
if err := authverify.CheckAccessV3(ctx, req.UserID); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
group, err := s.GroupDatabase.TakeGroup(ctx, req.GroupID)
|
group, err := s.GroupDatabase.TakeGroup(ctx, req.GroupID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if group.GroupType == constant.SuperGroup {
|
if group.GroupType == constant.SuperGroup {
|
||||||
if err := s.GroupDatabase.DeleteSuperGroupMember(ctx, req.GroupID, []string{req.UserID}); err != nil {
|
if err := s.GroupDatabase.DeleteSuperGroupMember(ctx, req.GroupID, []string{mcontext.GetOpUserID(ctx)}); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
_ = s.Notification.SuperGroupNotification(ctx, req.UserID, req.UserID)
|
s.Notification.SuperGroupNotification(ctx, mcontext.GetOpUserID(ctx), mcontext.GetOpUserID(ctx))
|
||||||
} else {
|
} else {
|
||||||
info, err := s.TakeGroupMember(ctx, req.GroupID, req.UserID)
|
info, err := s.TakeGroupMember(ctx, req.GroupID, mcontext.GetOpUserID(ctx))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if info.RoleLevel == constant.GroupOwner {
|
if info.RoleLevel == constant.GroupOwner {
|
||||||
return nil, errs.ErrNoPermission.Wrap("group owner can't quit")
|
return nil, errs.ErrNoPermission.Wrap("group owner can't quit")
|
||||||
}
|
}
|
||||||
err = s.GroupDatabase.DeleteGroupMember(ctx, req.GroupID, []string{req.UserID})
|
err = s.GroupDatabase.DeleteGroupMember(ctx, req.GroupID, []string{mcontext.GetOpUserID(ctx)})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
_ = s.Notification.MemberQuitNotification(ctx, s.groupMemberDB2PB(info, 0))
|
s.Notification.MemberQuitNotification(ctx, s.groupMemberDB2PB(info, 0))
|
||||||
}
|
}
|
||||||
if err := s.deleteMemberAndSetConversationSeq(ctx, req.GroupID, []string{req.UserID}); err != nil {
|
if err := s.deleteMemberAndSetConversationSeq(ctx, req.GroupID, []string{mcontext.GetOpUserID(ctx)}); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1073,18 +1059,15 @@ func (s *groupServer) GetGroupMembersCMS(ctx context.Context, req *pbgroup.GetGr
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resp.Total = total
|
resp.Total = total
|
||||||
nameMap, err := s.GetPublicUserInfoMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) {
|
nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) {
|
||||||
return e.UserID, e.Nickname == "" || e.FaceURL == ""
|
return e.UserID, e.Nickname == ""
|
||||||
}), true)
|
}), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
|
resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
|
||||||
if e.Nickname == "" {
|
if e.Nickname == "" {
|
||||||
e.Nickname = nameMap[e.UserID].Nickname
|
e.Nickname = nameMap[e.UserID]
|
||||||
}
|
|
||||||
if e.FaceURL == "" {
|
|
||||||
e.FaceURL = nameMap[e.UserID].FaceURL
|
|
||||||
}
|
}
|
||||||
return convert.Db2PbGroupMember(e)
|
return convert.Db2PbGroupMember(e)
|
||||||
})
|
})
|
||||||
@@ -1470,7 +1453,7 @@ func (s *groupServer) GetUserInGroupMembers(ctx context.Context, req *pbgroup.Ge
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
publicUserInfoMap, err := s.GetPublicUserInfoMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) {
|
nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) {
|
||||||
return e.UserID, e.Nickname == ""
|
return e.UserID, e.Nickname == ""
|
||||||
}), true)
|
}), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1478,10 +1461,7 @@ func (s *groupServer) GetUserInGroupMembers(ctx context.Context, req *pbgroup.Ge
|
|||||||
}
|
}
|
||||||
resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
|
resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
|
||||||
if e.Nickname == "" {
|
if e.Nickname == "" {
|
||||||
e.Nickname = publicUserInfoMap[e.UserID].Nickname
|
e.Nickname = nameMap[e.UserID]
|
||||||
}
|
|
||||||
if e.FaceURL == "" {
|
|
||||||
e.FaceURL = publicUserInfoMap[e.UserID].FaceURL
|
|
||||||
}
|
}
|
||||||
return convert.Db2PbGroupMember(e)
|
return convert.Db2PbGroupMember(e)
|
||||||
})
|
})
|
||||||
@@ -1506,18 +1486,15 @@ func (s *groupServer) GetGroupMemberRoleLevel(ctx context.Context, req *pbgroup.
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
publicUserInfoMap, err := s.GetPublicUserInfoMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) {
|
nameMap, err := s.GetUsernameMap(ctx, utils.Filter(members, func(e *relationtb.GroupMemberModel) (string, bool) {
|
||||||
return e.UserID, e.Nickname == "" || e.FaceURL == ""
|
return e.UserID, e.Nickname == ""
|
||||||
}), true)
|
}), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
|
resp.Members = utils.Slice(members, func(e *relationtb.GroupMemberModel) *sdkws.GroupMemberFullInfo {
|
||||||
if e.Nickname == "" {
|
if e.Nickname == "" {
|
||||||
e.Nickname = publicUserInfoMap[e.UserID].Nickname
|
e.Nickname = nameMap[e.UserID]
|
||||||
}
|
|
||||||
if e.FaceURL == "" {
|
|
||||||
e.FaceURL = publicUserInfoMap[e.UserID].FaceURL
|
|
||||||
}
|
}
|
||||||
return convert.Db2PbGroupMember(e)
|
return convert.Db2PbGroupMember(e)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -147,6 +147,7 @@ func (m *msgServer) MarkConversationAsRead(
|
|||||||
for i := hasReadSeq + 1; i <= req.HasReadSeq; i++ {
|
for i := hasReadSeq + 1; i <= req.HasReadSeq; i++ {
|
||||||
seqs = append(seqs, i)
|
seqs = append(seqs, i)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(seqs) > 0 {
|
if len(seqs) > 0 {
|
||||||
log.ZDebug(ctx, "MarkConversationAsRead", "seqs", seqs, "conversationID", req.ConversationID)
|
log.ZDebug(ctx, "MarkConversationAsRead", "seqs", seqs, "conversationID", req.ConversationID)
|
||||||
if err = m.MsgDatabase.MarkSingleChatMsgsAsRead(ctx, req.UserID, req.ConversationID, seqs); err != nil {
|
if err = m.MsgDatabase.MarkSingleChatMsgsAsRead(ctx, req.UserID, req.ConversationID, seqs); err != nil {
|
||||||
@@ -165,8 +166,7 @@ func (m *msgServer) MarkConversationAsRead(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if conversation.ConversationType == constant.SuperGroupChatType ||
|
} else if conversation.ConversationType == constant.SuperGroupChatType {
|
||||||
conversation.ConversationType == constant.NotificationChatType {
|
|
||||||
if req.HasReadSeq > hasReadSeq {
|
if req.HasReadSeq > hasReadSeq {
|
||||||
err = m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, req.HasReadSeq)
|
err = m.MsgDatabase.SetHasReadSeq(ctx, req.UserID, req.ConversationID, req.HasReadSeq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -178,6 +178,7 @@ func (m *msgServer) MarkConversationAsRead(
|
|||||||
req.UserID, seqs, hasReadSeq); err != nil {
|
req.UserID, seqs, hasReadSeq); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &msg.MarkConversationAsReadResp{}, nil
|
return &msg.MarkConversationAsReadResp{}, nil
|
||||||
|
|||||||
@@ -290,8 +290,7 @@ func (s *userServer) SubscribeOrCancelUsersStatus(ctx context.Context, req *pbus
|
|||||||
|
|
||||||
// GetUserStatus Get the online status of the user.
|
// GetUserStatus Get the online status of the user.
|
||||||
func (s *userServer) GetUserStatus(ctx context.Context, req *pbuser.GetUserStatusReq) (resp *pbuser.GetUserStatusResp,
|
func (s *userServer) GetUserStatus(ctx context.Context, req *pbuser.GetUserStatusReq) (resp *pbuser.GetUserStatusResp,
|
||||||
err error,
|
err error) {
|
||||||
) {
|
|
||||||
onlineStatusList, err := s.UserDatabase.GetUserStatus(ctx, req.UserIDs)
|
onlineStatusList, err := s.UserDatabase.GetUserStatus(ctx, req.UserIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -301,8 +300,7 @@ func (s *userServer) GetUserStatus(ctx context.Context, req *pbuser.GetUserStatu
|
|||||||
|
|
||||||
// SetUserStatus Synchronize user's online status.
|
// SetUserStatus Synchronize user's online status.
|
||||||
func (s *userServer) SetUserStatus(ctx context.Context, req *pbuser.SetUserStatusReq) (resp *pbuser.SetUserStatusResp,
|
func (s *userServer) SetUserStatus(ctx context.Context, req *pbuser.SetUserStatusReq) (resp *pbuser.SetUserStatusResp,
|
||||||
err error,
|
err error) {
|
||||||
) {
|
|
||||||
err = s.UserDatabase.SetUserStatus(ctx, req.UserID, req.Status, req.PlatformID)
|
err = s.UserDatabase.SetUserStatus(ctx, req.UserID, req.Status, req.PlatformID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -326,8 +324,7 @@ func (s *userServer) SetUserStatus(ctx context.Context, req *pbuser.SetUserStatu
|
|||||||
|
|
||||||
// GetSubscribeUsersStatus Get the online status of subscribers.
|
// GetSubscribeUsersStatus Get the online status of subscribers.
|
||||||
func (s *userServer) GetSubscribeUsersStatus(ctx context.Context,
|
func (s *userServer) GetSubscribeUsersStatus(ctx context.Context,
|
||||||
req *pbuser.GetSubscribeUsersStatusReq,
|
req *pbuser.GetSubscribeUsersStatusReq) (*pbuser.GetSubscribeUsersStatusResp, error) {
|
||||||
) (*pbuser.GetSubscribeUsersStatusResp, error) {
|
|
||||||
userList, err := s.UserDatabase.GetAllSubscribeList(ctx, req.UserID)
|
userList, err := s.UserDatabase.GetAllSubscribeList(ctx, req.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -17,13 +17,11 @@ package tools
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
"google.golang.org/grpc/credentials/insecure"
|
"google.golang.org/grpc/credentials/insecure"
|
||||||
|
"math"
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/discovery_register"
|
|
||||||
|
|
||||||
"github.com/OpenIMSDK/tools/errs"
|
"github.com/OpenIMSDK/tools/errs"
|
||||||
"github.com/OpenIMSDK/tools/log"
|
"github.com/OpenIMSDK/tools/log"
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ type SendMsgReq struct {
|
|||||||
type BatchSendMsgReq struct {
|
type BatchSendMsgReq struct {
|
||||||
SendMsg
|
SendMsg
|
||||||
IsSendAll bool `json:"isSendAll"`
|
IsSendAll bool `json:"isSendAll"`
|
||||||
RecvIDs []string `json:"recvIDs" binding:"required"`
|
RecvIDs []string `json:"recvIDs"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type BatchSendMsgResp struct {
|
type BatchSendMsgResp struct {
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ func CheckAccessV3(ctx context.Context, ownerUserID string) (err error) {
|
|||||||
if opUserID == ownerUserID {
|
if opUserID == ownerUserID {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return errs.ErrNoPermission.Wrap(utils.GetSelfFuncName())
|
return errs.ErrNoPermission.Wrap(utils.GetSelfFuncName())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +52,6 @@ func CheckAdmin(ctx context.Context) error {
|
|||||||
if utils.IsContain(mcontext.GetOpUserID(ctx), config.Config.Manager.UserID) {
|
if utils.IsContain(mcontext.GetOpUserID(ctx), config.Config.Manager.UserID) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s is not admin userID", mcontext.GetOpUserID(ctx)))
|
return errs.ErrNoPermission.Wrap(fmt.Sprintf("user %s is not admin userID", mcontext.GetOpUserID(ctx)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,6 +74,5 @@ func WsVerifyToken(token, userID string, platformID int) error {
|
|||||||
if claim.PlatformID != platformID {
|
if claim.PlatformID != platformID {
|
||||||
return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %d != %d", claim.PlatformID, platformID))
|
return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %d != %d", claim.PlatformID, platformID))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,7 +61,6 @@ func (c CommonCallbackResp) Parse() error {
|
|||||||
if c.ActionCode != errs.NoError || c.ErrCode != errs.NoError {
|
if c.ActionCode != errs.NoError || c.ErrCode != errs.NoError {
|
||||||
return errs.NewCodeError(int(c.ErrCode), c.ErrMsg).WithDetail(c.ErrDlt)
|
return errs.NewCodeError(int(c.ErrCode), c.ErrMsg).WithDetail(c.ErrDlt)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,11 +16,9 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/protocol/constant"
|
"github.com/OpenIMSDK/protocol/constant"
|
||||||
"github.com/spf13/cobra"
|
|
||||||
|
|
||||||
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ApiCmd struct {
|
type ApiCmd struct {
|
||||||
@@ -30,7 +28,6 @@ type ApiCmd struct {
|
|||||||
func NewApiCmd() *ApiCmd {
|
func NewApiCmd() *ApiCmd {
|
||||||
ret := &ApiCmd{NewRootCmd("api")}
|
ret := &ApiCmd{NewRootCmd("api")}
|
||||||
ret.SetRootCmdPt(ret)
|
ret.SetRootCmdPt(ret)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,12 +36,11 @@ func (a *ApiCmd) AddApi(f func(port int) error) {
|
|||||||
return f(a.getPortFlag(cmd))
|
return f(a.getPortFlag(cmd))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *ApiCmd) GetPortFromConfig(portType string) int {
|
func (a *ApiCmd) GetPortFromConfig(portType string) int {
|
||||||
fmt.Println("GetPortFromConfig:", portType)
|
fmt.Println("GetPortFromConfig:", portType)
|
||||||
if portType == constant.FlagPort {
|
if portType == constant.FlagPort {
|
||||||
return config2.Config.Api.OpenImApiPort[0]
|
return config2.Config.Api.OpenImApiPort[0]
|
||||||
|
} else {
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ type CronTaskCmd struct {
|
|||||||
func NewCronTaskCmd() *CronTaskCmd {
|
func NewCronTaskCmd() *CronTaskCmd {
|
||||||
ret := &CronTaskCmd{NewRootCmd("cronTask", WithCronTaskLogName())}
|
ret := &CronTaskCmd{NewRootCmd("cronTask", WithCronTaskLogName())}
|
||||||
ret.SetRootCmdPt(ret)
|
ret.SetRootCmdPt(ret)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,6 +34,5 @@ func (c *CronTaskCmd) addRunE(f func() error) {
|
|||||||
|
|
||||||
func (c *CronTaskCmd) Exec(f func() error) error {
|
func (c *CronTaskCmd) Exec(f func() error) error {
|
||||||
c.addRunE(f)
|
c.addRunE(f)
|
||||||
|
|
||||||
return c.Execute()
|
return c.Execute()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ type MsgGatewayCmd struct {
|
|||||||
func NewMsgGatewayCmd() *MsgGatewayCmd {
|
func NewMsgGatewayCmd() *MsgGatewayCmd {
|
||||||
ret := &MsgGatewayCmd{NewRootCmd("msgGateway")}
|
ret := &MsgGatewayCmd{NewRootCmd("msgGateway")}
|
||||||
ret.SetRootCmdPt(ret)
|
ret.SetRootCmdPt(ret)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +43,6 @@ func (m *MsgGatewayCmd) getWsPortFlag(cmd *cobra.Command) int {
|
|||||||
if port == 0 {
|
if port == 0 {
|
||||||
port = m.PortFromConfig(constant.FlagWsPort)
|
port = m.PortFromConfig(constant.FlagWsPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
return port
|
return port
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,10 +54,8 @@ func (m *MsgGatewayCmd) addRunE() {
|
|||||||
|
|
||||||
func (m *MsgGatewayCmd) Exec() error {
|
func (m *MsgGatewayCmd) Exec() error {
|
||||||
m.addRunE()
|
m.addRunE()
|
||||||
|
|
||||||
return m.Execute()
|
return m.Execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgGatewayCmd) GetPortFromConfig(portType string) int {
|
func (m *MsgGatewayCmd) GetPortFromConfig(portType string) int {
|
||||||
if portType == constant.FlagWsPort {
|
if portType == constant.FlagWsPort {
|
||||||
return config2.Config.LongConnSvr.OpenImWsPort[0]
|
return config2.Config.LongConnSvr.OpenImWsPort[0]
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ type MsgTransferCmd struct {
|
|||||||
func NewMsgTransferCmd() *MsgTransferCmd {
|
func NewMsgTransferCmd() *MsgTransferCmd {
|
||||||
ret := &MsgTransferCmd{NewRootCmd("msgTransfer")}
|
ret := &MsgTransferCmd{NewRootCmd("msgTransfer")}
|
||||||
ret.SetRootCmdPt(ret)
|
ret.SetRootCmdPt(ret)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,6 +38,5 @@ func (m *MsgTransferCmd) addRunE() {
|
|||||||
|
|
||||||
func (m *MsgTransferCmd) Exec() error {
|
func (m *MsgTransferCmd) Exec() error {
|
||||||
m.addRunE()
|
m.addRunE()
|
||||||
|
|
||||||
return m.Execute()
|
return m.Execute()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import (
|
|||||||
|
|
||||||
type MsgUtilsCmd struct {
|
type MsgUtilsCmd struct {
|
||||||
cobra.Command
|
cobra.Command
|
||||||
// msgTool *tools.MsgTool
|
msgTool *tools.MsgTool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgUtilsCmd) AddUserIDFlag() {
|
func (m *MsgUtilsCmd) AddUserIDFlag() {
|
||||||
@@ -31,7 +31,6 @@ func (m *MsgUtilsCmd) AddUserIDFlag() {
|
|||||||
|
|
||||||
func (m *MsgUtilsCmd) getUserIDFlag(cmdLines *cobra.Command) string {
|
func (m *MsgUtilsCmd) getUserIDFlag(cmdLines *cobra.Command) string {
|
||||||
userID, _ := cmdLines.Flags().GetString("userID")
|
userID, _ := cmdLines.Flags().GetString("userID")
|
||||||
|
|
||||||
return userID
|
return userID
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,17 +38,26 @@ func (m *MsgUtilsCmd) AddFixAllFlag() {
|
|||||||
m.Command.PersistentFlags().BoolP("fixAll", "f", false, "openIM fix all seqs")
|
m.Command.PersistentFlags().BoolP("fixAll", "f", false, "openIM fix all seqs")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MsgUtilsCmd) getFixAllFlag(cmdLines *cobra.Command) bool {
|
||||||
|
fixAll, _ := cmdLines.Flags().GetBool("fixAll")
|
||||||
|
return fixAll
|
||||||
|
}
|
||||||
|
|
||||||
func (m *MsgUtilsCmd) AddClearAllFlag() {
|
func (m *MsgUtilsCmd) AddClearAllFlag() {
|
||||||
m.Command.PersistentFlags().BoolP("clearAll", "c", false, "openIM clear all seqs")
|
m.Command.PersistentFlags().BoolP("clearAll", "c", false, "openIM clear all seqs")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MsgUtilsCmd) getClearAllFlag(cmdLines *cobra.Command) bool {
|
||||||
|
clearAll, _ := cmdLines.Flags().GetBool("clearAll")
|
||||||
|
return clearAll
|
||||||
|
}
|
||||||
|
|
||||||
func (m *MsgUtilsCmd) AddSuperGroupIDFlag() {
|
func (m *MsgUtilsCmd) AddSuperGroupIDFlag() {
|
||||||
m.Command.PersistentFlags().StringP("superGroupID", "g", "", "openIM superGroupID")
|
m.Command.PersistentFlags().StringP("superGroupID", "g", "", "openIM superGroupID")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgUtilsCmd) getSuperGroupIDFlag(cmdLines *cobra.Command) string {
|
func (m *MsgUtilsCmd) getSuperGroupIDFlag(cmdLines *cobra.Command) string {
|
||||||
superGroupID, _ := cmdLines.Flags().GetString("superGroupID")
|
superGroupID, _ := cmdLines.Flags().GetString("superGroupID")
|
||||||
|
|
||||||
return superGroupID
|
return superGroupID
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,10 +65,20 @@ func (m *MsgUtilsCmd) AddBeginSeqFlag() {
|
|||||||
m.Command.PersistentFlags().Int64P("beginSeq", "b", 0, "openIM beginSeq")
|
m.Command.PersistentFlags().Int64P("beginSeq", "b", 0, "openIM beginSeq")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MsgUtilsCmd) getBeginSeqFlag(cmdLines *cobra.Command) int64 {
|
||||||
|
beginSeq, _ := cmdLines.Flags().GetInt64("beginSeq")
|
||||||
|
return beginSeq
|
||||||
|
}
|
||||||
|
|
||||||
func (m *MsgUtilsCmd) AddLimitFlag() {
|
func (m *MsgUtilsCmd) AddLimitFlag() {
|
||||||
m.Command.PersistentFlags().Int64P("limit", "l", 0, "openIM limit")
|
m.Command.PersistentFlags().Int64P("limit", "l", 0, "openIM limit")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *MsgUtilsCmd) getLimitFlag(cmdLines *cobra.Command) int64 {
|
||||||
|
limit, _ := cmdLines.Flags().GetInt64("limit")
|
||||||
|
return limit
|
||||||
|
}
|
||||||
|
|
||||||
func (m *MsgUtilsCmd) Execute() error {
|
func (m *MsgUtilsCmd) Execute() error {
|
||||||
return m.Command.Execute()
|
return m.Command.Execute()
|
||||||
}
|
}
|
||||||
@@ -113,7 +131,6 @@ func NewSeqCmd() *SeqCmd {
|
|||||||
seqCmd := &SeqCmd{
|
seqCmd := &SeqCmd{
|
||||||
NewMsgUtilsCmd("seq", "seq", nil),
|
NewMsgUtilsCmd("seq", "seq", nil),
|
||||||
}
|
}
|
||||||
|
|
||||||
return seqCmd
|
return seqCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,7 +158,6 @@ func (s *SeqCmd) GetSeqCmd() *cobra.Command {
|
|||||||
// println(seq)
|
// println(seq)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &s.Command
|
return &s.Command
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +173,6 @@ func NewMsgCmd() *MsgCmd {
|
|||||||
msgCmd := &MsgCmd{
|
msgCmd := &MsgCmd{
|
||||||
NewMsgUtilsCmd("msg", "msg", nil),
|
NewMsgUtilsCmd("msg", "msg", nil),
|
||||||
}
|
}
|
||||||
|
|
||||||
return msgCmd
|
return msgCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,11 +17,12 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/protocol/constant"
|
"github.com/OpenIMSDK/protocol/constant"
|
||||||
"github.com/OpenIMSDK/tools/log"
|
"github.com/OpenIMSDK/tools/log"
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -52,10 +53,10 @@ func WithLogName(logName string) func(*CmdOpts) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRootCmd(name string, opts ...func(*CmdOpts)) *RootCmd {
|
func NewRootCmd(name string, opts ...func(*CmdOpts)) (rootCmd *RootCmd) {
|
||||||
rootCmd := &RootCmd{Name: name}
|
rootCmd = &RootCmd{Name: name}
|
||||||
cmd := cobra.Command{
|
c := cobra.Command{
|
||||||
Use: "Start openIM application",
|
Use: "start openIM application",
|
||||||
Short: fmt.Sprintf(`Start %s `, name),
|
Short: fmt.Sprintf(`Start %s `, name),
|
||||||
Long: fmt.Sprintf(`Start %s `, name),
|
Long: fmt.Sprintf(`Start %s `, name),
|
||||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
@@ -69,78 +70,21 @@ func NewRootCmd(name string, opts ...func(*CmdOpts)) *RootCmd {
|
|||||||
if cmdOpts.loggerPrefixName == "" {
|
if cmdOpts.loggerPrefixName == "" {
|
||||||
cmdOpts.loggerPrefixName = "OpenIM.log.all"
|
cmdOpts.loggerPrefixName = "OpenIM.log.all"
|
||||||
}
|
}
|
||||||
err := log.InitFromConfig(cmdOpts.loggerPrefixName, name, config.Config.Log.RemainLogLevel,
|
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 {
|
||||||
config.Config.Log.IsStdout, config.Config.Log.IsJson, config.Config.Log.StorageLocation,
|
|
||||||
config.Config.Log.RemainRotationCount, config.Config.Log.RotationTime)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
rootCmd.Command = cmd
|
rootCmd.Command = c
|
||||||
rootCmd.addConfFlag()
|
rootCmd.addConfFlag()
|
||||||
|
|
||||||
return rootCmd
|
return rootCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (rc *RootCmd) persistentPreRun(cmd *cobra.Command, opts ...func(*CmdOpts)) error {
|
|
||||||
// if err := rc.initializeConfiguration(cmd); err != nil {
|
|
||||||
// return fmt.Errorf("failed to get configuration from command: %w", err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// cmdOpts := rc.applyOptions(opts...)
|
|
||||||
|
|
||||||
// if err := rc.initializeLogger(cmdOpts); err != nil {
|
|
||||||
// return fmt.Errorf("failed to initialize from config: %w", err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
//nolint:unused //unused work wrongly
|
|
||||||
func (rc *RootCmd) initializeConfiguration(cmd *cobra.Command) error {
|
|
||||||
return rc.getConfFromCmdAndInit(cmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
// func (rc *RootCmd) applyOptions(opts ...func(*CmdOpts)) *CmdOpts {
|
|
||||||
// cmdOpts := defaultCmdOpts()
|
|
||||||
// for _, opt := range opts {
|
|
||||||
// opt(cmdOpts)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return cmdOpts
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (rc *RootCmd) initializeLogger(cmdOpts *CmdOpts) error {
|
|
||||||
// logConfig := config.Config.Log
|
|
||||||
|
|
||||||
// return log.InitFromConfig(
|
|
||||||
|
|
||||||
// cmdOpts.loggerPrefixName,
|
|
||||||
// rc.Name,
|
|
||||||
// logConfig.RemainLogLevel,
|
|
||||||
// logConfig.IsStdout,
|
|
||||||
// logConfig.IsJson,
|
|
||||||
// logConfig.StorageLocation,
|
|
||||||
// logConfig.RemainRotationCount,
|
|
||||||
// logConfig.RotationTime,
|
|
||||||
// )
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func defaultCmdOpts() *CmdOpts {
|
|
||||||
// return &CmdOpts{
|
|
||||||
// loggerPrefixName: "OpenIM.log.all",
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
func (r *RootCmd) SetRootCmdPt(cmdItf RootCmdPt) {
|
func (r *RootCmd) SetRootCmdPt(cmdItf RootCmdPt) {
|
||||||
r.cmdItf = cmdItf
|
r.cmdItf = cmdItf
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RootCmd) addConfFlag() {
|
func (r *RootCmd) addConfFlag() {
|
||||||
r.Command.Flags().StringP(constant.FlagConf, "c", "", "path to config file folder")
|
r.Command.Flags().StringP(constant.FlagConf, "c", "", "Path to config file folder")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RootCmd) AddPortFlag() {
|
func (r *RootCmd) AddPortFlag() {
|
||||||
@@ -152,7 +96,6 @@ func (r *RootCmd) getPortFlag(cmd *cobra.Command) int {
|
|||||||
if port == 0 {
|
if port == 0 {
|
||||||
port = r.PortFromConfig(constant.FlagPort)
|
port = r.PortFromConfig(constant.FlagPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
return port
|
return port
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,7 +112,6 @@ func (r *RootCmd) getPrometheusPortFlag(cmd *cobra.Command) int {
|
|||||||
if port == 0 {
|
if port == 0 {
|
||||||
port = r.PortFromConfig(constant.FlagPrometheusPort)
|
port = r.PortFromConfig(constant.FlagPrometheusPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
return port
|
return port
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,8 +122,7 @@ func (r *RootCmd) GetPrometheusPortFlag() int {
|
|||||||
func (r *RootCmd) getConfFromCmdAndInit(cmdLines *cobra.Command) error {
|
func (r *RootCmd) getConfFromCmdAndInit(cmdLines *cobra.Command) error {
|
||||||
configFolderPath, _ := cmdLines.Flags().GetString(constant.FlagConf)
|
configFolderPath, _ := cmdLines.Flags().GetString(constant.FlagConf)
|
||||||
fmt.Println("configFolderPath:", configFolderPath)
|
fmt.Println("configFolderPath:", configFolderPath)
|
||||||
|
return config2.InitConfig(configFolderPath)
|
||||||
return config.InitConfig(configFolderPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RootCmd) Execute() error {
|
func (r *RootCmd) Execute() error {
|
||||||
@@ -194,12 +135,9 @@ func (r *RootCmd) AddCommand(cmds ...*cobra.Command) {
|
|||||||
|
|
||||||
func (r *RootCmd) GetPortFromConfig(portType string) int {
|
func (r *RootCmd) GetPortFromConfig(portType string) int {
|
||||||
fmt.Println("RootCmd.GetPortFromConfig:", portType)
|
fmt.Println("RootCmd.GetPortFromConfig:", portType)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RootCmd) PortFromConfig(portType string) int {
|
func (r *RootCmd) PortFromConfig(portType string) int {
|
||||||
fmt.Println("PortFromConfig:", portType)
|
fmt.Println("PortFromConfig:", portType)
|
||||||
|
|
||||||
return r.cmdItf.GetPortFromConfig(portType)
|
return r.cmdItf.GetPortFromConfig(portType)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,13 +16,11 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/protocol/constant"
|
"github.com/OpenIMSDK/protocol/constant"
|
||||||
|
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
|
||||||
config2 "github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
|
||||||
|
|
||||||
"github.com/OpenIMSDK/tools/discoveryregistry"
|
"github.com/OpenIMSDK/tools/discoveryregistry"
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/startrpc"
|
"github.com/openimsdk/open-im-server/v3/pkg/common/startrpc"
|
||||||
@@ -35,7 +33,6 @@ type RpcCmd struct {
|
|||||||
func NewRpcCmd(name string) *RpcCmd {
|
func NewRpcCmd(name string) *RpcCmd {
|
||||||
ret := &RpcCmd{NewRootCmd(name)}
|
ret := &RpcCmd{NewRootCmd(name)}
|
||||||
ret.SetRootCmdPt(ret)
|
ret.SetRootCmdPt(ret)
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +41,6 @@ func (a *RpcCmd) Exec() error {
|
|||||||
a.port = a.getPortFlag(cmd)
|
a.port = a.getPortFlag(cmd)
|
||||||
a.prometheusPort = a.getPrometheusPortFlag(cmd)
|
a.prometheusPort = a.getPrometheusPortFlag(cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
return a.Execute()
|
return a.Execute()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,10 +51,8 @@ func (a *RpcCmd) StartSvr(
|
|||||||
if a.GetPortFlag() == 0 {
|
if a.GetPortFlag() == 0 {
|
||||||
return errors.New("port is required")
|
return errors.New("port is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
return startrpc.Start(a.GetPortFlag(), name, a.GetPrometheusPortFlag(), rpcFn)
|
return startrpc.Start(a.GetPortFlag(), name, a.GetPrometheusPortFlag(), rpcFn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *RpcCmd) GetPortFromConfig(portType string) int {
|
func (a *RpcCmd) GetPortFromConfig(portType string) int {
|
||||||
switch a.Name {
|
switch a.Name {
|
||||||
case RpcPushServer:
|
case RpcPushServer:
|
||||||
@@ -94,6 +88,5 @@ func (a *RpcCmd) GetPortFromConfig(portType string) int {
|
|||||||
return config2.Config.RpcPort.OpenImUserPort[0]
|
return config2.Config.RpcPort.OpenImUserPort[0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -336,7 +336,6 @@ func (c *configStruct) RegisterConf2Registry(registry discoveryregistry.SvcDisco
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return registry.RegisterConf2Registry(ConfKey, data)
|
return registry.RegisterConf2Registry(ConfKey, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -349,6 +348,5 @@ func (c *configStruct) EncodeConfig() []byte {
|
|||||||
if err := yaml.NewEncoder(buf).Encode(c); err != nil {
|
if err := yaml.NewEncoder(buf).Encode(c); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf.Bytes()
|
return buf.Bytes()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,32 +19,32 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
"github.com/OpenIMSDK/protocol/constant"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
|
"github.com/OpenIMSDK/protocol/constant"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed version
|
//go:embed version
|
||||||
var Version string
|
var Version string
|
||||||
|
|
||||||
|
var (
|
||||||
|
_, b, _, _ = runtime.Caller(0)
|
||||||
|
// Root folder of this project.
|
||||||
|
Root = filepath.Join(filepath.Dir(b), "../../..")
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
FileName = "config.yaml"
|
FileName = "config.yaml"
|
||||||
NotificationFileName = "notification.yaml"
|
NotificationFileName = "notification.yaml"
|
||||||
DefaultFolderPath = "../config/"
|
DefaultFolderPath = "../config/"
|
||||||
)
|
)
|
||||||
|
|
||||||
// getProjectRoot returns the absolute path of the project root directory.
|
|
||||||
func GetProjectRoot() string {
|
|
||||||
b, _ := filepath.Abs(os.Args[0])
|
|
||||||
|
|
||||||
return filepath.Join(filepath.Dir(b), "../../..")
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options {
|
func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options {
|
||||||
opts := msgprocessor.NewOptions()
|
opts := msgprocessor.NewOptions()
|
||||||
|
|
||||||
if cfg.UnreadCount {
|
if cfg.UnreadCount {
|
||||||
opts = msgprocessor.WithOptions(opts, msgprocessor.WithUnreadCount(true))
|
opts = msgprocessor.WithOptions(opts, msgprocessor.WithUnreadCount(true))
|
||||||
}
|
}
|
||||||
@@ -57,44 +57,44 @@ func GetOptionsByNotification(cfg NotificationConf) msgprocessor.Options {
|
|||||||
opts = msgprocessor.WithOptions(opts, msgprocessor.WithHistory(true), msgprocessor.WithPersistent())
|
opts = msgprocessor.WithOptions(opts, msgprocessor.WithHistory(true), msgprocessor.WithPersistent())
|
||||||
}
|
}
|
||||||
opts = msgprocessor.WithOptions(opts, msgprocessor.WithSendMsg(cfg.IsSendMsg))
|
opts = msgprocessor.WithOptions(opts, msgprocessor.WithSendMsg(cfg.IsSendMsg))
|
||||||
|
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func initConfig(config interface{}, configName, configFolderPath string) error {
|
func initConfig(config interface{}, configName, configFolderPath string) error {
|
||||||
configFolderPath = filepath.Join(configFolderPath, configName)
|
if configFolderPath == "" {
|
||||||
_, err := os.Stat(configFolderPath)
|
configFolderPath = DefaultFolderPath
|
||||||
|
}
|
||||||
|
configPath := filepath.Join(configFolderPath, configName)
|
||||||
|
defer func() {
|
||||||
|
fmt.Println("use config", configPath)
|
||||||
|
}()
|
||||||
|
_, err := os.Stat(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
return fmt.Errorf("stat config path error: %w", err)
|
return err
|
||||||
}
|
}
|
||||||
configFolderPath = filepath.Join(GetProjectRoot(), "config", configName)
|
configPath = filepath.Join(Root, "config", configName)
|
||||||
|
} else {
|
||||||
|
Root = filepath.Dir(configPath)
|
||||||
}
|
}
|
||||||
data, err := os.ReadFile(configFolderPath)
|
data, err := os.ReadFile(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("read file error: %w", err)
|
return err
|
||||||
}
|
}
|
||||||
if err = yaml.Unmarshal(data, config); err != nil {
|
if err = yaml.Unmarshal(data, config); err != nil {
|
||||||
return fmt.Errorf("unmarshal yaml error: %w", err)
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println("use config", configFolderPath)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitConfig(configFolderPath string) error {
|
func InitConfig(configFolderPath string) error {
|
||||||
if configFolderPath == "" {
|
err := initConfig(&Config, FileName, configFolderPath)
|
||||||
envConfigPath := os.Getenv("OPENIMCONFIG")
|
if err != nil {
|
||||||
if envConfigPath != "" {
|
return err
|
||||||
configFolderPath = envConfigPath
|
}
|
||||||
} else {
|
err = initConfig(&Config.Notification, NotificationFileName, configFolderPath)
|
||||||
configFolderPath = DefaultFolderPath
|
if err != nil {
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := initConfig(&Config, FileName, configFolderPath); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/protocol/sdkws"
|
"github.com/OpenIMSDK/protocol/sdkws"
|
||||||
|
sdk "github.com/OpenIMSDK/protocol/sdkws"
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
"github.com/openimsdk/open-im-server/v3/pkg/common/db/table/relation"
|
||||||
)
|
)
|
||||||
@@ -26,11 +27,11 @@ func BlackDB2Pb(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
blackDBs []*relation.BlackModel,
|
blackDBs []*relation.BlackModel,
|
||||||
f func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error),
|
f func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error),
|
||||||
) (blackPbs []*sdkws.BlackInfo, err error) {
|
) (blackPbs []*sdk.BlackInfo, err error) {
|
||||||
if len(blackDBs) == 0 {
|
if len(blackDBs) == 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
userIDs := make([]string, 0, len(blackDBs))
|
var userIDs []string
|
||||||
for _, blackDB := range blackDBs {
|
for _, blackDB := range blackDBs {
|
||||||
userIDs = append(userIDs, blackDB.BlockUserID)
|
userIDs = append(userIDs, blackDB.BlockUserID)
|
||||||
}
|
}
|
||||||
@@ -39,7 +40,7 @@ func BlackDB2Pb(
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for _, blackDB := range blackDBs {
|
for _, blackDB := range blackDBs {
|
||||||
blackPb := &sdkws.BlackInfo{
|
blackPb := &sdk.BlackInfo{
|
||||||
OwnerUserID: blackDB.OwnerUserID,
|
OwnerUserID: blackDB.OwnerUserID,
|
||||||
CreateTime: blackDB.CreateTime.Unix(),
|
CreateTime: blackDB.CreateTime.Unix(),
|
||||||
AddSource: blackDB.AddSource,
|
AddSource: blackDB.AddSource,
|
||||||
@@ -54,6 +55,5 @@ func BlackDB2Pb(
|
|||||||
}
|
}
|
||||||
blackPbs = append(blackPbs, blackPb)
|
blackPbs = append(blackPbs, blackPb)
|
||||||
}
|
}
|
||||||
|
|
||||||
return blackPbs, nil
|
return blackPbs, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ func ConversationDB2Pb(conversationDB *relation.ConversationModel) *conversation
|
|||||||
if err := utils.CopyStructFields(conversationPB, conversationDB); err != nil {
|
if err := utils.CopyStructFields(conversationPB, conversationDB); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return conversationPB
|
return conversationPB
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +39,6 @@ func ConversationsDB2Pb(conversationsDB []*relation.ConversationModel) (conversa
|
|||||||
conversationPB.LatestMsgDestructTime = conversationDB.LatestMsgDestructTime.Unix()
|
conversationPB.LatestMsgDestructTime = conversationDB.LatestMsgDestructTime.Unix()
|
||||||
conversationsPB = append(conversationsPB, conversationPB)
|
conversationsPB = append(conversationsPB, conversationPB)
|
||||||
}
|
}
|
||||||
|
|
||||||
return conversationsPB
|
return conversationsPB
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +47,6 @@ func ConversationPb2DB(conversationPB *conversation.Conversation) *relation.Conv
|
|||||||
if err := utils.CopyStructFields(conversationDB, conversationPB); err != nil {
|
if err := utils.CopyStructFields(conversationDB, conversationPB); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return conversationDB
|
return conversationDB
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,6 +58,5 @@ func ConversationsPb2DB(conversationsPB []*conversation.Conversation) (conversat
|
|||||||
}
|
}
|
||||||
conversationsDB = append(conversationsDB, conversationDB)
|
conversationsDB = append(conversationsDB, conversationDB)
|
||||||
}
|
}
|
||||||
|
|
||||||
return conversationsDB
|
return conversationsDB
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ package convert
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/OpenIMSDK/protocol/sdkws"
|
"github.com/OpenIMSDK/protocol/sdkws"
|
||||||
"github.com/OpenIMSDK/tools/utils"
|
"github.com/OpenIMSDK/tools/utils"
|
||||||
@@ -26,13 +25,9 @@ import (
|
|||||||
|
|
||||||
func FriendPb2DB(friend *sdkws.FriendInfo) *relation.FriendModel {
|
func FriendPb2DB(friend *sdkws.FriendInfo) *relation.FriendModel {
|
||||||
dbFriend := &relation.FriendModel{}
|
dbFriend := &relation.FriendModel{}
|
||||||
err := utils.CopyStructFields(dbFriend, friend)
|
utils.CopyStructFields(dbFriend, friend)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
dbFriend.FriendUserID = friend.FriendUser.UserID
|
dbFriend.FriendUserID = friend.FriendUser.UserID
|
||||||
dbFriend.CreateTime = utils.UnixSecondToTime(friend.CreateTime)
|
dbFriend.CreateTime = utils.UnixSecondToTime(friend.CreateTime)
|
||||||
|
|
||||||
return dbFriend
|
return dbFriend
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,10 +37,7 @@ func FriendDB2Pb(
|
|||||||
getUsers func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error),
|
getUsers func(ctx context.Context, userIDs []string) (map[string]*sdkws.UserInfo, error),
|
||||||
) (*sdkws.FriendInfo, error) {
|
) (*sdkws.FriendInfo, error) {
|
||||||
pbfriend := &sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}}
|
pbfriend := &sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}}
|
||||||
err := utils.CopyStructFields(pbfriend, friendDB)
|
utils.CopyStructFields(pbfriend, friendDB)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
users, err := getUsers(ctx, []string{friendDB.FriendUserID})
|
users, err := getUsers(ctx, []string{friendDB.FriendUserID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -55,7 +47,6 @@ func FriendDB2Pb(
|
|||||||
pbfriend.FriendUser.FaceURL = users[friendDB.FriendUserID].FaceURL
|
pbfriend.FriendUser.FaceURL = users[friendDB.FriendUserID].FaceURL
|
||||||
pbfriend.FriendUser.Ex = users[friendDB.FriendUserID].Ex
|
pbfriend.FriendUser.Ex = users[friendDB.FriendUserID].Ex
|
||||||
pbfriend.CreateTime = friendDB.CreateTime.Unix()
|
pbfriend.CreateTime = friendDB.CreateTime.Unix()
|
||||||
|
|
||||||
return pbfriend, nil
|
return pbfriend, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +58,7 @@ func FriendsDB2Pb(
|
|||||||
if len(friendsDB) == 0 {
|
if len(friendsDB) == 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
userID := make([]string, 0, len(friendsDB))
|
var userID []string
|
||||||
for _, friendDB := range friendsDB {
|
for _, friendDB := range friendsDB {
|
||||||
userID = append(userID, friendDB.FriendUserID)
|
userID = append(userID, friendDB.FriendUserID)
|
||||||
}
|
}
|
||||||
@@ -77,8 +68,7 @@ func FriendsDB2Pb(
|
|||||||
}
|
}
|
||||||
for _, friend := range friendsDB {
|
for _, friend := range friendsDB {
|
||||||
friendPb := &sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}}
|
friendPb := &sdkws.FriendInfo{FriendUser: &sdkws.UserInfo{}}
|
||||||
err2 := utils.CopyStructFields(friendPb, friend)
|
utils.CopyStructFields(friendPb, friend)
|
||||||
err = fmt.Errorf("%w, %w", err, err2)
|
|
||||||
friendPb.FriendUser.UserID = users[friend.FriendUserID].UserID
|
friendPb.FriendUser.UserID = users[friend.FriendUserID].UserID
|
||||||
friendPb.FriendUser.Nickname = users[friend.FriendUserID].Nickname
|
friendPb.FriendUser.Nickname = users[friend.FriendUserID].Nickname
|
||||||
friendPb.FriendUser.FaceURL = users[friend.FriendUserID].FaceURL
|
friendPb.FriendUser.FaceURL = users[friend.FriendUserID].FaceURL
|
||||||
@@ -86,8 +76,7 @@ func FriendsDB2Pb(
|
|||||||
friendPb.CreateTime = friend.CreateTime.Unix()
|
friendPb.CreateTime = friend.CreateTime.Unix()
|
||||||
friendsPb = append(friendsPb, friendPb)
|
friendsPb = append(friendsPb, friendPb)
|
||||||
}
|
}
|
||||||
|
return friendsPb, nil
|
||||||
return friendsPb, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func FriendRequestDB2Pb(
|
func FriendRequestDB2Pb(
|
||||||
@@ -127,6 +116,5 @@ func FriendRequestDB2Pb(
|
|||||||
Ex: friendRequest.Ex,
|
Ex: friendRequest.Ex,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ func MsgPb2DB(msg *sdkws.MsgData) *unrelation.MsgDataModel {
|
|||||||
msgDataModel.AtUserIDList = msg.AtUserIDList
|
msgDataModel.AtUserIDList = msg.AtUserIDList
|
||||||
msgDataModel.AttachedInfo = msg.AttachedInfo
|
msgDataModel.AttachedInfo = msg.AttachedInfo
|
||||||
msgDataModel.Ex = msg.Ex
|
msgDataModel.Ex = msg.Ex
|
||||||
|
|
||||||
return &msgDataModel
|
return &msgDataModel
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,6 +95,5 @@ func MsgDB2Pb(msgModel *unrelation.MsgDataModel) *sdkws.MsgData {
|
|||||||
msg.AtUserIDList = msgModel.AtUserIDList
|
msg.AtUserIDList = msgModel.AtUserIDList
|
||||||
msg.AttachedInfo = msgModel.AttachedInfo
|
msg.AttachedInfo = msgModel.AttachedInfo
|
||||||
msg.Ex = msgModel.Ex
|
msg.Ex = msgModel.Ex
|
||||||
|
|
||||||
return &msg
|
return &msg
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,6 @@ func UsersDB2Pb(users []*relationtb.UserModel) (result []*sdkws.UserInfo) {
|
|||||||
userPb.GlobalRecvMsgOpt = user.GlobalRecvMsgOpt
|
userPb.GlobalRecvMsgOpt = user.GlobalRecvMsgOpt
|
||||||
result = append(result, &userPb)
|
result = append(result, &userPb)
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,6 +46,5 @@ func UserPb2DB(user *sdkws.UserInfo) *relationtb.UserModel {
|
|||||||
userDB.CreateTime = time.UnixMilli(user.CreateTime)
|
userDB.CreateTime = time.UnixMilli(user.CreateTime)
|
||||||
userDB.AppMangerLevel = user.AppMangerLevel
|
userDB.AppMangerLevel = user.AppMangerLevel
|
||||||
userDB.GlobalRecvMsgOpt = user.GlobalRecvMsgOpt
|
userDB.GlobalRecvMsgOpt = user.GlobalRecvMsgOpt
|
||||||
|
|
||||||
return &userDB
|
return &userDB
|
||||||
}
|
}
|
||||||
|
|||||||
21
pkg/common/db/cache/black.go
vendored
21
pkg/common/db/cache/black.go
vendored
@@ -52,7 +52,6 @@ func NewBlackCacheRedis(
|
|||||||
options rockscache.Options,
|
options rockscache.Options,
|
||||||
) BlackCache {
|
) BlackCache {
|
||||||
rcClient := rockscache.NewClient(rdb, options)
|
rcClient := rockscache.NewClient(rdb, options)
|
||||||
|
|
||||||
return &BlackCacheRedis{
|
return &BlackCacheRedis{
|
||||||
expireTime: blackExpireTime,
|
expireTime: blackExpireTime,
|
||||||
rcClient: rcClient,
|
rcClient: rcClient,
|
||||||
@@ -62,7 +61,12 @@ func NewBlackCacheRedis(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlackCacheRedis) NewCache() BlackCache {
|
func (b *BlackCacheRedis) NewCache() BlackCache {
|
||||||
return &BlackCacheRedis{expireTime: b.expireTime, rcClient: b.rcClient, blackDB: b.blackDB, metaCache: NewMetaCacheRedis(b.rcClient, b.metaCache.GetPreDelKeys()...)}
|
return &BlackCacheRedis{
|
||||||
|
expireTime: b.expireTime,
|
||||||
|
rcClient: b.rcClient,
|
||||||
|
blackDB: b.blackDB,
|
||||||
|
metaCache: NewMetaCacheRedis(b.rcClient, b.metaCache.GetPreDelKeys()...),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlackCacheRedis) getBlackIDsKey(ownerUserID string) string {
|
func (b *BlackCacheRedis) getBlackIDsKey(ownerUserID string) string {
|
||||||
@@ -70,14 +74,19 @@ func (b *BlackCacheRedis) getBlackIDsKey(ownerUserID string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlackCacheRedis) GetBlackIDs(ctx context.Context, userID string) (blackIDs []string, err error) {
|
func (b *BlackCacheRedis) GetBlackIDs(ctx context.Context, userID string) (blackIDs []string, err error) {
|
||||||
return getCache(ctx, b.rcClient, b.getBlackIDsKey(userID), b.expireTime, func(ctx context.Context) ([]string, error) {
|
return getCache(
|
||||||
return b.blackDB.FindBlackUserIDs(ctx, userID)
|
ctx,
|
||||||
})
|
b.rcClient,
|
||||||
|
b.getBlackIDsKey(userID),
|
||||||
|
b.expireTime,
|
||||||
|
func(ctx context.Context) ([]string, error) {
|
||||||
|
return b.blackDB.FindBlackUserIDs(ctx, userID)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BlackCacheRedis) DelBlackIDs(ctx context.Context, userID string) BlackCache {
|
func (b *BlackCacheRedis) DelBlackIDs(ctx context.Context, userID string) BlackCache {
|
||||||
cache := b.NewCache()
|
cache := b.NewCache()
|
||||||
cache.AddKeys(b.getBlackIDsKey(userID))
|
cache.AddKeys(b.getBlackIDsKey(userID))
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|||||||
268
pkg/common/db/cache/conversation.go
vendored
268
pkg/common/db/cache/conversation.go
vendored
@@ -73,7 +73,7 @@ type ConversationCache interface {
|
|||||||
GetSuperGroupRecvMsgNotNotifyUserIDsHash(ctx context.Context, groupID string) (hash uint64, err error)
|
GetSuperGroupRecvMsgNotNotifyUserIDsHash(ctx context.Context, groupID string) (hash uint64, err error)
|
||||||
DelSuperGroupRecvMsgNotNotifyUserIDsHash(groupID string) ConversationCache
|
DelSuperGroupRecvMsgNotNotifyUserIDsHash(groupID string) ConversationCache
|
||||||
|
|
||||||
//GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error)
|
GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error)
|
||||||
DelUserAllHasReadSeqs(ownerUserID string, conversationIDs ...string) ConversationCache
|
DelUserAllHasReadSeqs(ownerUserID string, conversationIDs ...string) ConversationCache
|
||||||
|
|
||||||
GetConversationsByConversationID(ctx context.Context,
|
GetConversationsByConversationID(ctx context.Context,
|
||||||
@@ -83,9 +83,12 @@ type ConversationCache interface {
|
|||||||
DelConversationNotReceiveMessageUserIDs(conversationIDs ...string) ConversationCache
|
DelConversationNotReceiveMessageUserIDs(conversationIDs ...string) ConversationCache
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConversationRedis(rdb redis.UniversalClient, opts rockscache.Options, db relationtb.ConversationModelInterface) ConversationCache {
|
func NewConversationRedis(
|
||||||
|
rdb redis.UniversalClient,
|
||||||
|
opts rockscache.Options,
|
||||||
|
db relationtb.ConversationModelInterface,
|
||||||
|
) ConversationCache {
|
||||||
rcClient := rockscache.NewClient(rdb, opts)
|
rcClient := rockscache.NewClient(rdb, opts)
|
||||||
|
|
||||||
return &ConversationRedisCache{
|
return &ConversationRedisCache{
|
||||||
rcClient: rcClient,
|
rcClient: rcClient,
|
||||||
metaCache: NewMetaCacheRedis(rcClient),
|
metaCache: NewMetaCacheRedis(rcClient),
|
||||||
@@ -107,7 +110,6 @@ func NewNewConversationRedis(
|
|||||||
options rockscache.Options,
|
options rockscache.Options,
|
||||||
) ConversationCache {
|
) ConversationCache {
|
||||||
rcClient := rockscache.NewClient(rdb, options)
|
rcClient := rockscache.NewClient(rdb, options)
|
||||||
|
|
||||||
return &ConversationRedisCache{
|
return &ConversationRedisCache{
|
||||||
rcClient: rcClient,
|
rcClient: rcClient,
|
||||||
metaCache: NewMetaCacheRedis(rcClient),
|
metaCache: NewMetaCacheRedis(rcClient),
|
||||||
@@ -154,19 +156,24 @@ func (c *ConversationRedisCache) getConversationNotReceiveMessageUserIDsKey(conv
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) GetUserConversationIDs(ctx context.Context, ownerUserID string) ([]string, error) {
|
func (c *ConversationRedisCache) GetUserConversationIDs(ctx context.Context, ownerUserID string) ([]string, error) {
|
||||||
return getCache(ctx, c.rcClient, c.getConversationIDsKey(ownerUserID), c.expireTime, func(ctx context.Context) ([]string, error) {
|
return getCache(
|
||||||
return c.conversationDB.FindUserIDAllConversationID(ctx, ownerUserID)
|
ctx,
|
||||||
})
|
c.rcClient,
|
||||||
|
c.getConversationIDsKey(ownerUserID),
|
||||||
|
c.expireTime,
|
||||||
|
func(ctx context.Context) ([]string, error) {
|
||||||
|
return c.conversationDB.FindUserIDAllConversationID(ctx, ownerUserID)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) DelConversationIDs(userIDs ...string) ConversationCache {
|
func (c *ConversationRedisCache) DelConversationIDs(userIDs ...string) ConversationCache {
|
||||||
keys := make([]string, 0, len(userIDs))
|
var keys []string
|
||||||
for _, userID := range userIDs {
|
for _, userID := range userIDs {
|
||||||
keys = append(keys, c.getConversationIDsKey(userID))
|
keys = append(keys, c.getConversationIDsKey(userID))
|
||||||
}
|
}
|
||||||
cache := c.NewCache()
|
cache := c.NewCache()
|
||||||
cache.AddKeys(keys...)
|
cache.AddKeys(keys...)
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,7 +181,10 @@ func (c *ConversationRedisCache) getUserConversationIDsHashKey(ownerUserID strin
|
|||||||
return conversationIDsHashKey + ownerUserID
|
return conversationIDsHashKey + ownerUserID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) GetUserConversationIDsHash(ctx context.Context, ownerUserID string) (hash uint64, err error) {
|
func (c *ConversationRedisCache) GetUserConversationIDsHash(
|
||||||
|
ctx context.Context,
|
||||||
|
ownerUserID string,
|
||||||
|
) (hash uint64, err error) {
|
||||||
return getCache(
|
return getCache(
|
||||||
ctx,
|
ctx,
|
||||||
c.rcClient,
|
c.rcClient,
|
||||||
@@ -194,180 +204,229 @@ func (c *ConversationRedisCache) GetUserConversationIDsHash(ctx context.Context,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) DelUserConversationIDsHash(ownerUserIDs ...string) ConversationCache {
|
func (c *ConversationRedisCache) DelUserConversationIDsHash(ownerUserIDs ...string) ConversationCache {
|
||||||
keys := make([]string, 0, len(ownerUserIDs))
|
var keys []string
|
||||||
for _, ownerUserID := range ownerUserIDs {
|
for _, ownerUserID := range ownerUserIDs {
|
||||||
keys = append(keys, c.getUserConversationIDsHashKey(ownerUserID))
|
keys = append(keys, c.getUserConversationIDsHashKey(ownerUserID))
|
||||||
}
|
}
|
||||||
cache := c.NewCache()
|
cache := c.NewCache()
|
||||||
cache.AddKeys(keys...)
|
cache.AddKeys(keys...)
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) GetConversation(ctx context.Context, ownerUserID, conversationID string) (*relationtb.ConversationModel, error) {
|
func (c *ConversationRedisCache) GetConversation(
|
||||||
return getCache(ctx, c.rcClient, c.getConversationKey(ownerUserID, conversationID), c.expireTime, func(ctx context.Context) (*relationtb.ConversationModel, error) {
|
ctx context.Context,
|
||||||
return c.conversationDB.Take(ctx, ownerUserID, conversationID)
|
ownerUserID, conversationID string,
|
||||||
})
|
) (*relationtb.ConversationModel, error) {
|
||||||
|
return getCache(
|
||||||
|
ctx,
|
||||||
|
c.rcClient,
|
||||||
|
c.getConversationKey(ownerUserID, conversationID),
|
||||||
|
c.expireTime,
|
||||||
|
func(ctx context.Context) (*relationtb.ConversationModel, error) {
|
||||||
|
return c.conversationDB.Take(ctx, ownerUserID, conversationID)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) DelConversations(ownerUserID string, conversationIDs ...string) ConversationCache {
|
func (c *ConversationRedisCache) DelConversations(ownerUserID string, conversationIDs ...string) ConversationCache {
|
||||||
keys := make([]string, 0, len(conversationIDs))
|
var keys []string
|
||||||
for _, conversationID := range conversationIDs {
|
for _, conversationID := range conversationIDs {
|
||||||
keys = append(keys, c.getConversationKey(ownerUserID, conversationID))
|
keys = append(keys, c.getConversationKey(ownerUserID, conversationID))
|
||||||
}
|
}
|
||||||
cache := c.NewCache()
|
cache := c.NewCache()
|
||||||
cache.AddKeys(keys...)
|
cache.AddKeys(keys...)
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) getConversationIndex(convsation *relationtb.ConversationModel, keys []string) (int, error) {
|
func (c *ConversationRedisCache) getConversationIndex(
|
||||||
|
convsation *relationtb.ConversationModel,
|
||||||
|
keys []string,
|
||||||
|
) (int, error) {
|
||||||
key := c.getConversationKey(convsation.OwnerUserID, convsation.ConversationID)
|
key := c.getConversationKey(convsation.OwnerUserID, convsation.ConversationID)
|
||||||
for _i, _key := range keys {
|
for _i, _key := range keys {
|
||||||
if _key == key {
|
if _key == key {
|
||||||
return _i, nil
|
return _i, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0, errors.New("not found key:" + key + " in keys")
|
return 0, errors.New("not found key:" + key + " in keys")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) GetConversations(ctx context.Context, ownerUserID string, conversationIDs []string) ([]*relationtb.ConversationModel, error) {
|
func (c *ConversationRedisCache) GetConversations(
|
||||||
//var keys []string
|
ctx context.Context,
|
||||||
//for _, conversarionID := range conversationIDs {
|
ownerUserID string,
|
||||||
// keys = append(keys, c.getConversationKey(ownerUserID, conversarionID))
|
conversationIDs []string,
|
||||||
//}
|
) ([]*relationtb.ConversationModel, error) {
|
||||||
//return batchGetCache(
|
var keys []string
|
||||||
// ctx,
|
for _, conversarionID := range conversationIDs {
|
||||||
// c.rcClient,
|
keys = append(keys, c.getConversationKey(ownerUserID, conversarionID))
|
||||||
// keys,
|
}
|
||||||
// c.expireTime,
|
return batchGetCache(
|
||||||
// c.getConversationIndex,
|
ctx,
|
||||||
// func(ctx context.Context) ([]*relationtb.ConversationModel, error) {
|
c.rcClient,
|
||||||
// return c.conversationDB.Find(ctx, ownerUserID, conversationIDs)
|
keys,
|
||||||
// },
|
c.expireTime,
|
||||||
//)
|
c.getConversationIndex,
|
||||||
return batchGetCache2(ctx, c.rcClient, c.expireTime, conversationIDs, func(conversationID string) string {
|
func(ctx context.Context) ([]*relationtb.ConversationModel, error) {
|
||||||
return c.getConversationKey(ownerUserID, conversationID)
|
return c.conversationDB.Find(ctx, ownerUserID, conversationIDs)
|
||||||
}, func(ctx context.Context, conversationID string) (*relationtb.ConversationModel, error) {
|
},
|
||||||
return c.conversationDB.Take(ctx, ownerUserID, conversationID)
|
)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) GetUserAllConversations(ctx context.Context, ownerUserID string) ([]*relationtb.ConversationModel, error) {
|
func (c *ConversationRedisCache) GetUserAllConversations(
|
||||||
|
ctx context.Context,
|
||||||
|
ownerUserID string,
|
||||||
|
) ([]*relationtb.ConversationModel, error) {
|
||||||
conversationIDs, err := c.GetUserConversationIDs(ctx, ownerUserID)
|
conversationIDs, err := c.GetUserConversationIDs(ctx, ownerUserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
//var keys []string
|
var keys []string
|
||||||
//for _, conversarionID := range conversationIDs {
|
for _, conversarionID := range conversationIDs {
|
||||||
// keys = append(keys, c.getConversationKey(ownerUserID, conversarionID))
|
keys = append(keys, c.getConversationKey(ownerUserID, conversarionID))
|
||||||
//}
|
}
|
||||||
//return batchGetCache(
|
return batchGetCache(
|
||||||
// ctx,
|
ctx,
|
||||||
// c.rcClient,
|
c.rcClient,
|
||||||
// keys,
|
keys,
|
||||||
// c.expireTime,
|
c.expireTime,
|
||||||
// c.getConversationIndex,
|
c.getConversationIndex,
|
||||||
// func(ctx context.Context) ([]*relationtb.ConversationModel, error) {
|
func(ctx context.Context) ([]*relationtb.ConversationModel, error) {
|
||||||
// return c.conversationDB.FindUserIDAllConversations(ctx, ownerUserID)
|
return c.conversationDB.FindUserIDAllConversations(ctx, ownerUserID)
|
||||||
// },
|
},
|
||||||
//)
|
)
|
||||||
return c.GetConversations(ctx, ownerUserID, conversationIDs)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) GetUserRecvMsgOpt(ctx context.Context, ownerUserID, conversationID string) (opt int, err error) {
|
func (c *ConversationRedisCache) GetUserRecvMsgOpt(
|
||||||
return getCache(ctx, c.rcClient, c.getRecvMsgOptKey(ownerUserID, conversationID), c.expireTime, func(ctx context.Context) (opt int, err error) {
|
ctx context.Context,
|
||||||
return c.conversationDB.GetUserRecvMsgOpt(ctx, ownerUserID, conversationID)
|
ownerUserID, conversationID string,
|
||||||
})
|
) (opt int, err error) {
|
||||||
|
return getCache(
|
||||||
|
ctx,
|
||||||
|
c.rcClient,
|
||||||
|
c.getRecvMsgOptKey(ownerUserID, conversationID),
|
||||||
|
c.expireTime,
|
||||||
|
func(ctx context.Context) (opt int, err error) {
|
||||||
|
return c.conversationDB.GetUserRecvMsgOpt(ctx, ownerUserID, conversationID)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) GetSuperGroupRecvMsgNotNotifyUserIDs(ctx context.Context, groupID string) (userIDs []string, err error) {
|
func (c *ConversationRedisCache) GetSuperGroupRecvMsgNotNotifyUserIDs(
|
||||||
return getCache(ctx, c.rcClient, c.getSuperGroupRecvNotNotifyUserIDsKey(groupID), c.expireTime, func(ctx context.Context) (userIDs []string, err error) {
|
ctx context.Context,
|
||||||
return c.conversationDB.FindSuperGroupRecvMsgNotNotifyUserIDs(ctx, groupID)
|
groupID string,
|
||||||
})
|
) (userIDs []string, err error) {
|
||||||
|
return getCache(
|
||||||
|
ctx,
|
||||||
|
c.rcClient,
|
||||||
|
c.getSuperGroupRecvNotNotifyUserIDsKey(groupID),
|
||||||
|
c.expireTime,
|
||||||
|
func(ctx context.Context) (userIDs []string, err error) {
|
||||||
|
return c.conversationDB.FindSuperGroupRecvMsgNotNotifyUserIDs(ctx, groupID)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) DelUsersConversation(conversationID string, ownerUserIDs ...string) ConversationCache {
|
func (c *ConversationRedisCache) DelUsersConversation(conversationID string, ownerUserIDs ...string) ConversationCache {
|
||||||
keys := make([]string, 0, len(ownerUserIDs))
|
var keys []string
|
||||||
for _, ownerUserID := range ownerUserIDs {
|
for _, ownerUserID := range ownerUserIDs {
|
||||||
keys = append(keys, c.getConversationKey(ownerUserID, conversationID))
|
keys = append(keys, c.getConversationKey(ownerUserID, conversationID))
|
||||||
}
|
}
|
||||||
cache := c.NewCache()
|
cache := c.NewCache()
|
||||||
cache.AddKeys(keys...)
|
cache.AddKeys(keys...)
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) DelUserRecvMsgOpt(ownerUserID, conversationID string) ConversationCache {
|
func (c *ConversationRedisCache) DelUserRecvMsgOpt(ownerUserID, conversationID string) ConversationCache {
|
||||||
cache := c.NewCache()
|
cache := c.NewCache()
|
||||||
cache.AddKeys(c.getRecvMsgOptKey(ownerUserID, conversationID))
|
cache.AddKeys(c.getRecvMsgOptKey(ownerUserID, conversationID))
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) DelSuperGroupRecvMsgNotNotifyUserIDs(groupID string) ConversationCache {
|
func (c *ConversationRedisCache) DelSuperGroupRecvMsgNotNotifyUserIDs(groupID string) ConversationCache {
|
||||||
cache := c.NewCache()
|
cache := c.NewCache()
|
||||||
cache.AddKeys(c.getSuperGroupRecvNotNotifyUserIDsKey(groupID))
|
cache.AddKeys(c.getSuperGroupRecvNotNotifyUserIDsKey(groupID))
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) GetSuperGroupRecvMsgNotNotifyUserIDsHash(ctx context.Context, groupID string) (hash uint64, err error) {
|
func (c *ConversationRedisCache) GetSuperGroupRecvMsgNotNotifyUserIDsHash(
|
||||||
return getCache(ctx, c.rcClient, c.getSuperGroupRecvNotNotifyUserIDsHashKey(groupID), c.expireTime, func(ctx context.Context) (hash uint64, err error) {
|
ctx context.Context,
|
||||||
userIDs, err := c.GetSuperGroupRecvMsgNotNotifyUserIDs(ctx, groupID)
|
groupID string,
|
||||||
if err != nil {
|
) (hash uint64, err error) {
|
||||||
return 0, err
|
return getCache(
|
||||||
}
|
ctx,
|
||||||
utils.Sort(userIDs, true)
|
c.rcClient,
|
||||||
bi := big.NewInt(0)
|
c.getSuperGroupRecvNotNotifyUserIDsHashKey(groupID),
|
||||||
bi.SetString(utils.Md5(strings.Join(userIDs, ";"))[0:8], 16)
|
c.expireTime,
|
||||||
return bi.Uint64(), nil
|
func(ctx context.Context) (hash uint64, err error) {
|
||||||
},
|
userIDs, err := c.GetSuperGroupRecvMsgNotNotifyUserIDs(ctx, groupID)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
utils.Sort(userIDs, true)
|
||||||
|
bi := big.NewInt(0)
|
||||||
|
bi.SetString(utils.Md5(strings.Join(userIDs, ";"))[0:8], 16)
|
||||||
|
return bi.Uint64(), nil
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) DelSuperGroupRecvMsgNotNotifyUserIDsHash(groupID string) ConversationCache {
|
func (c *ConversationRedisCache) DelSuperGroupRecvMsgNotNotifyUserIDsHash(groupID string) ConversationCache {
|
||||||
cache := c.NewCache()
|
cache := c.NewCache()
|
||||||
cache.AddKeys(c.getSuperGroupRecvNotNotifyUserIDsHashKey(groupID))
|
cache.AddKeys(c.getSuperGroupRecvNotNotifyUserIDsHashKey(groupID))
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) getUserAllHasReadSeqsIndex(conversationID string, conversationIDs []string) (int, error) {
|
func (c *ConversationRedisCache) getUserAllHasReadSeqsIndex(
|
||||||
|
conversationID string,
|
||||||
|
conversationIDs []string,
|
||||||
|
) (int, error) {
|
||||||
for _i, _conversationID := range conversationIDs {
|
for _i, _conversationID := range conversationIDs {
|
||||||
if _conversationID == conversationID {
|
if _conversationID == conversationID {
|
||||||
return _i, nil
|
return _i, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0, errors.New("not found key:" + conversationID + " in keys")
|
return 0, errors.New("not found key:" + conversationID + " in keys")
|
||||||
}
|
}
|
||||||
|
|
||||||
//func (c *ConversationRedisCache) GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error) {
|
func (c *ConversationRedisCache) GetUserAllHasReadSeqs(
|
||||||
// conversationIDs, err := c.GetUserConversationIDs(ctx, ownerUserID)
|
ctx context.Context,
|
||||||
// if err != nil {
|
ownerUserID string,
|
||||||
// return nil, err
|
) (map[string]int64, error) {
|
||||||
// }
|
conversationIDs, err := c.GetUserConversationIDs(ctx, ownerUserID)
|
||||||
// var keys []string
|
if err != nil {
|
||||||
// for _, conversarionID := range conversationIDs {
|
return nil, err
|
||||||
// keys = append(keys, c.getConversationHasReadSeqKey(ownerUserID, conversarionID))
|
}
|
||||||
// }
|
var keys []string
|
||||||
// return batchGetCacheMap(ctx, c.rcClient, keys, conversationIDs, c.expireTime, c.getUserAllHasReadSeqsIndex, func(ctx context.Context) (map[string]int64, error) {
|
for _, conversarionID := range conversationIDs {
|
||||||
// return c.conversationDB.GetUserAllHasReadSeqs(ctx, ownerUserID)
|
keys = append(keys, c.getConversationHasReadSeqKey(ownerUserID, conversarionID))
|
||||||
// })
|
}
|
||||||
//}
|
return batchGetCacheMap(
|
||||||
|
ctx,
|
||||||
|
c.rcClient,
|
||||||
|
keys,
|
||||||
|
conversationIDs,
|
||||||
|
c.expireTime,
|
||||||
|
c.getUserAllHasReadSeqsIndex,
|
||||||
|
func(ctx context.Context) (map[string]int64, error) {
|
||||||
|
return c.conversationDB.GetUserAllHasReadSeqs(ctx, ownerUserID)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) DelUserAllHasReadSeqs(ownerUserID string, conversationIDs ...string) ConversationCache {
|
func (c *ConversationRedisCache) DelUserAllHasReadSeqs(ownerUserID string,
|
||||||
|
conversationIDs ...string,
|
||||||
|
) ConversationCache {
|
||||||
cache := c.NewCache()
|
cache := c.NewCache()
|
||||||
for _, conversationID := range conversationIDs {
|
for _, conversationID := range conversationIDs {
|
||||||
cache.AddKeys(c.getConversationHasReadSeqKey(ownerUserID, conversationID))
|
cache.AddKeys(c.getConversationHasReadSeqKey(ownerUserID, conversationID))
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*relationtb.ConversationModel, error) {
|
func (c *ConversationRedisCache) GetConversationsByConversationID(
|
||||||
|
ctx context.Context,
|
||||||
|
conversationIDs []string,
|
||||||
|
) ([]*relationtb.ConversationModel, error) {
|
||||||
panic("implement me")
|
panic("implement me")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -376,9 +435,15 @@ func (c *ConversationRedisCache) DelConversationByConversationID(conversationIDs
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) GetConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) ([]string, error) {
|
func (c *ConversationRedisCache) GetConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) ([]string, error) {
|
||||||
return getCache(ctx, c.rcClient, c.getConversationNotReceiveMessageUserIDsKey(conversationID), c.expireTime, func(ctx context.Context) ([]string, error) {
|
return getCache(
|
||||||
return c.conversationDB.GetConversationNotReceiveMessageUserIDs(ctx, conversationID)
|
ctx,
|
||||||
})
|
c.rcClient,
|
||||||
|
c.getConversationNotReceiveMessageUserIDsKey(conversationID),
|
||||||
|
c.expireTime,
|
||||||
|
func(ctx context.Context) ([]string, error) {
|
||||||
|
return c.conversationDB.GetConversationNotReceiveMessageUserIDs(ctx, conversationID)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ConversationRedisCache) DelConversationNotReceiveMessageUserIDs(conversationIDs ...string) ConversationCache {
|
func (c *ConversationRedisCache) DelConversationNotReceiveMessageUserIDs(conversationIDs ...string) ConversationCache {
|
||||||
@@ -386,6 +451,5 @@ func (c *ConversationRedisCache) DelConversationNotReceiveMessageUserIDs(convers
|
|||||||
for _, conversationID := range conversationIDs {
|
for _, conversationID := range conversationIDs {
|
||||||
cache.AddKeys(c.getConversationNotReceiveMessageUserIDsKey(conversationID))
|
cache.AddKeys(c.getConversationNotReceiveMessageUserIDsKey(conversationID))
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|||||||
69
pkg/common/db/cache/friend.go
vendored
69
pkg/common/db/cache/friend.go
vendored
@@ -53,9 +53,12 @@ type FriendCacheRedis struct {
|
|||||||
rcClient *rockscache.Client
|
rcClient *rockscache.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFriendCacheRedis(rdb redis.UniversalClient, friendDB relationtb.FriendModelInterface, options rockscache.Options) FriendCache {
|
func NewFriendCacheRedis(
|
||||||
|
rdb redis.UniversalClient,
|
||||||
|
friendDB relationtb.FriendModelInterface,
|
||||||
|
options rockscache.Options,
|
||||||
|
) FriendCache {
|
||||||
rcClient := rockscache.NewClient(rdb, options)
|
rcClient := rockscache.NewClient(rdb, options)
|
||||||
|
|
||||||
return &FriendCacheRedis{
|
return &FriendCacheRedis{
|
||||||
metaCache: NewMetaCacheRedis(rcClient),
|
metaCache: NewMetaCacheRedis(rcClient),
|
||||||
friendDB: friendDB,
|
friendDB: friendDB,
|
||||||
@@ -86,24 +89,32 @@ func (f *FriendCacheRedis) getFriendKey(ownerUserID, friendUserID string) string
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f *FriendCacheRedis) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) {
|
func (f *FriendCacheRedis) GetFriendIDs(ctx context.Context, ownerUserID string) (friendIDs []string, err error) {
|
||||||
return getCache(ctx, f.rcClient, f.getFriendIDsKey(ownerUserID), f.expireTime, func(ctx context.Context) ([]string, error) {
|
return getCache(
|
||||||
return f.friendDB.FindFriendUserIDs(ctx, ownerUserID)
|
ctx,
|
||||||
})
|
f.rcClient,
|
||||||
|
f.getFriendIDsKey(ownerUserID),
|
||||||
|
f.expireTime,
|
||||||
|
func(ctx context.Context) ([]string, error) {
|
||||||
|
return f.friendDB.FindFriendUserIDs(ctx, ownerUserID)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FriendCacheRedis) DelFriendIDs(ownerUserIDs ...string) FriendCache {
|
func (f *FriendCacheRedis) DelFriendIDs(ownerUserID ...string) FriendCache {
|
||||||
newGroupCache := f.NewCache()
|
new := f.NewCache()
|
||||||
keys := make([]string, 0, len(ownerUserIDs))
|
var keys []string
|
||||||
for _, userID := range ownerUserIDs {
|
for _, userID := range ownerUserID {
|
||||||
keys = append(keys, f.getFriendIDsKey(userID))
|
keys = append(keys, f.getFriendIDsKey(userID))
|
||||||
}
|
}
|
||||||
newGroupCache.AddKeys(keys...)
|
new.AddKeys(keys...)
|
||||||
|
return new
|
||||||
return newGroupCache
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo.
|
// todo.
|
||||||
func (f *FriendCacheRedis) GetTwoWayFriendIDs(ctx context.Context, ownerUserID string) (twoWayFriendIDs []string, err error) {
|
func (f *FriendCacheRedis) GetTwoWayFriendIDs(
|
||||||
|
ctx context.Context,
|
||||||
|
ownerUserID string,
|
||||||
|
) (twoWayFriendIDs []string, err error) {
|
||||||
friendIDs, err := f.GetFriendIDs(ctx, ownerUserID)
|
friendIDs, err := f.GetFriendIDs(ctx, ownerUserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -117,26 +128,32 @@ func (f *FriendCacheRedis) GetTwoWayFriendIDs(ctx context.Context, ownerUserID s
|
|||||||
twoWayFriendIDs = append(twoWayFriendIDs, ownerUserID)
|
twoWayFriendIDs = append(twoWayFriendIDs, ownerUserID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return twoWayFriendIDs, nil
|
return twoWayFriendIDs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FriendCacheRedis) DelTwoWayFriendIDs(ctx context.Context, ownerUserID string) FriendCache {
|
func (f *FriendCacheRedis) DelTwoWayFriendIDs(ctx context.Context, ownerUserID string) FriendCache {
|
||||||
newFriendCache := f.NewCache()
|
new := f.NewCache()
|
||||||
newFriendCache.AddKeys(f.getTwoWayFriendsIDsKey(ownerUserID))
|
new.AddKeys(f.getTwoWayFriendsIDsKey(ownerUserID))
|
||||||
|
return new
|
||||||
return newFriendCache
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FriendCacheRedis) GetFriend(ctx context.Context, ownerUserID, friendUserID string) (friend *relationtb.FriendModel, err error) {
|
func (f *FriendCacheRedis) GetFriend(
|
||||||
return getCache(ctx, f.rcClient, f.getFriendKey(ownerUserID, friendUserID), f.expireTime, func(ctx context.Context) (*relationtb.FriendModel, error) {
|
ctx context.Context,
|
||||||
return f.friendDB.Take(ctx, ownerUserID, friendUserID)
|
ownerUserID, friendUserID string,
|
||||||
})
|
) (friend *relationtb.FriendModel, err error) {
|
||||||
|
return getCache(
|
||||||
|
ctx,
|
||||||
|
f.rcClient,
|
||||||
|
f.getFriendKey(ownerUserID, friendUserID),
|
||||||
|
f.expireTime,
|
||||||
|
func(ctx context.Context) (*relationtb.FriendModel, error) {
|
||||||
|
return f.friendDB.Take(ctx, ownerUserID, friendUserID)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FriendCacheRedis) DelFriend(ownerUserID, friendUserID string) FriendCache {
|
func (f *FriendCacheRedis) DelFriend(ownerUserID, friendUserID string) FriendCache {
|
||||||
newFriendCache := f.NewCache()
|
new := f.NewCache()
|
||||||
newFriendCache.AddKeys(f.getFriendKey(ownerUserID, friendUserID))
|
new.AddKeys(f.getFriendKey(ownerUserID, friendUserID))
|
||||||
|
return new
|
||||||
return newFriendCache
|
|
||||||
}
|
}
|
||||||
|
|||||||
325
pkg/common/db/cache/group.go
vendored
325
pkg/common/db/cache/group.go
vendored
@@ -65,10 +65,22 @@ type GroupCache interface {
|
|||||||
GetJoinedGroupIDs(ctx context.Context, userID string) (joinedGroupIDs []string, err error)
|
GetJoinedGroupIDs(ctx context.Context, userID string) (joinedGroupIDs []string, err error)
|
||||||
DelJoinedGroupID(userID ...string) GroupCache
|
DelJoinedGroupID(userID ...string) GroupCache
|
||||||
|
|
||||||
GetGroupMemberInfo(ctx context.Context, groupID, userID string) (groupMember *relationtb.GroupMemberModel, err error)
|
GetGroupMemberInfo(
|
||||||
GetGroupMembersInfo(ctx context.Context, groupID string, userID []string) (groupMembers []*relationtb.GroupMemberModel, err error)
|
ctx context.Context,
|
||||||
|
groupID, userID string,
|
||||||
|
) (groupMember *relationtb.GroupMemberModel, err error)
|
||||||
|
GetGroupMembersInfo(
|
||||||
|
ctx context.Context,
|
||||||
|
groupID string,
|
||||||
|
userID []string,
|
||||||
|
) (groupMembers []*relationtb.GroupMemberModel, err error)
|
||||||
GetAllGroupMembersInfo(ctx context.Context, groupID string) (groupMembers []*relationtb.GroupMemberModel, err error)
|
GetAllGroupMembersInfo(ctx context.Context, groupID string) (groupMembers []*relationtb.GroupMemberModel, err error)
|
||||||
GetGroupMembersPage(ctx context.Context, groupID string, userID []string, showNumber, pageNumber int32) (total uint32, groupMembers []*relationtb.GroupMemberModel, err error)
|
GetGroupMembersPage(
|
||||||
|
ctx context.Context,
|
||||||
|
groupID string,
|
||||||
|
userID []string,
|
||||||
|
showNumber, pageNumber int32,
|
||||||
|
) (total uint32, groupMembers []*relationtb.GroupMemberModel, err error)
|
||||||
|
|
||||||
DelGroupMembersInfo(groupID string, userID ...string) GroupCache
|
DelGroupMembersInfo(groupID string, userID ...string) GroupCache
|
||||||
|
|
||||||
@@ -97,7 +109,6 @@ func NewGroupCacheRedis(
|
|||||||
opts rockscache.Options,
|
opts rockscache.Options,
|
||||||
) GroupCache {
|
) GroupCache {
|
||||||
rcClient := rockscache.NewClient(rdb, opts)
|
rcClient := rockscache.NewClient(rdb, opts)
|
||||||
|
|
||||||
return &GroupCacheRedis{
|
return &GroupCacheRedis{
|
||||||
rcClient: rcClient, expireTime: groupExpireTime,
|
rcClient: rcClient, expireTime: groupExpireTime,
|
||||||
groupDB: groupDB, groupMemberDB: groupMemberDB, groupRequestDB: groupRequestDB,
|
groupDB: groupDB, groupMemberDB: groupMemberDB, groupRequestDB: groupRequestDB,
|
||||||
@@ -158,7 +169,6 @@ func (g *GroupCacheRedis) GetGroupIndex(group *relationtb.GroupModel, keys []str
|
|||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0, errIndex
|
return 0, errIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,98 +179,117 @@ func (g *GroupCacheRedis) GetGroupMemberIndex(groupMember *relationtb.GroupMembe
|
|||||||
return i, nil
|
return i, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0, errIndex
|
return 0, errIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
// / groupInfo.
|
// / groupInfo.
|
||||||
func (g *GroupCacheRedis) GetGroupsInfo(ctx context.Context, groupIDs []string) (groups []*relationtb.GroupModel, err error) {
|
func (g *GroupCacheRedis) GetGroupsInfo(
|
||||||
//var keys []string
|
ctx context.Context,
|
||||||
//for _, group := range groupIDs {
|
groupIDs []string,
|
||||||
// keys = append(keys, g.getGroupInfoKey(group))
|
) (groups []*relationtb.GroupModel, err error) {
|
||||||
//}
|
var keys []string
|
||||||
//return batchGetCache(ctx, g.rcClient, keys, g.expireTime, g.GetGroupIndex, func(ctx context.Context) ([]*relationtb.GroupModel, error) {
|
for _, group := range groupIDs {
|
||||||
// return g.groupDB.Find(ctx, groupIDs)
|
keys = append(keys, g.getGroupInfoKey(group))
|
||||||
//})
|
|
||||||
return batchGetCache2(ctx, g.rcClient, g.expireTime, groupIDs, func(groupID string) string {
|
|
||||||
return g.getGroupInfoKey(groupID)
|
|
||||||
}, func(ctx context.Context, groupID string) (*relationtb.GroupModel, error) {
|
|
||||||
return g.groupDB.Take(ctx, groupID)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupCacheRedis) GetGroupInfo(ctx context.Context, groupID string) (group *relationtb.GroupModel, err error) {
|
|
||||||
return getCache(ctx, g.rcClient, g.getGroupInfoKey(groupID), g.expireTime, func(ctx context.Context) (*relationtb.GroupModel, error) {
|
|
||||||
return g.groupDB.Take(ctx, groupID)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *GroupCacheRedis) DelGroupsInfo(groupIDs ...string) GroupCache {
|
|
||||||
newGroupCache := g.NewCache()
|
|
||||||
keys := make([]string, 0, len(groupIDs))
|
|
||||||
for _, groupID := range groupIDs {
|
|
||||||
keys = append(keys, g.getGroupInfoKey(groupID))
|
|
||||||
}
|
}
|
||||||
newGroupCache.AddKeys(keys...)
|
return batchGetCache(
|
||||||
|
ctx,
|
||||||
return newGroupCache
|
g.rcClient,
|
||||||
}
|
keys,
|
||||||
|
g.expireTime,
|
||||||
func (g *GroupCacheRedis) GetJoinedSuperGroupIDs(ctx context.Context, userID string) (joinedSuperGroupIDs []string, err error) {
|
g.GetGroupIndex,
|
||||||
return getCache(ctx, g.rcClient, g.getJoinedSuperGroupsIDKey(userID), g.expireTime, func(ctx context.Context) ([]string, error) {
|
func(ctx context.Context) ([]*relationtb.GroupModel, error) {
|
||||||
userGroup, err := g.mongoDB.GetSuperGroupByUserID(ctx, userID)
|
return g.groupDB.Find(ctx, groupIDs)
|
||||||
if err != nil {
|
},
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return userGroup.GroupIDs, nil
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) GetSuperGroupMemberIDs(ctx context.Context, groupIDs ...string) (models []*unrelationtb.SuperGroupModel, err error) {
|
func (g *GroupCacheRedis) GetGroupInfo(ctx context.Context, groupID string) (group *relationtb.GroupModel, err error) {
|
||||||
//var keys []string
|
return getCache(
|
||||||
//for _, group := range groupIDs {
|
ctx,
|
||||||
// keys = append(keys, g.getSuperGroupMemberIDsKey(group))
|
g.rcClient,
|
||||||
//}
|
g.getGroupInfoKey(groupID),
|
||||||
//return batchGetCache(ctx, g.rcClient, keys, g.expireTime, func(model *unrelationtb.SuperGroupModel, keys []string) (int, error) {
|
g.expireTime,
|
||||||
// for i, key := range keys {
|
func(ctx context.Context) (*relationtb.GroupModel, error) {
|
||||||
// if g.getSuperGroupMemberIDsKey(model.GroupID) == key {
|
return g.groupDB.Take(ctx, groupID)
|
||||||
// return i, nil
|
},
|
||||||
// }
|
)
|
||||||
// }
|
}
|
||||||
// return 0, errIndex
|
|
||||||
//},
|
func (g *GroupCacheRedis) DelGroupsInfo(groupIDs ...string) GroupCache {
|
||||||
// func(ctx context.Context) ([]*unrelationtb.SuperGroupModel, error) {
|
new := g.NewCache()
|
||||||
// return g.mongoDB.FindSuperGroup(ctx, groupIDs)
|
var keys []string
|
||||||
// })
|
for _, groupID := range groupIDs {
|
||||||
return batchGetCache2(ctx, g.rcClient, g.expireTime, groupIDs, func(groupID string) string {
|
keys = append(keys, g.getGroupInfoKey(groupID))
|
||||||
return g.getSuperGroupMemberIDsKey(groupID)
|
}
|
||||||
}, func(ctx context.Context, groupID string) (*unrelationtb.SuperGroupModel, error) {
|
new.AddKeys(keys...)
|
||||||
return g.mongoDB.TakeSuperGroup(ctx, groupID)
|
return new
|
||||||
})
|
}
|
||||||
|
|
||||||
|
func (g *GroupCacheRedis) GetJoinedSuperGroupIDs(
|
||||||
|
ctx context.Context,
|
||||||
|
userID string,
|
||||||
|
) (joinedSuperGroupIDs []string, err error) {
|
||||||
|
return getCache(
|
||||||
|
ctx,
|
||||||
|
g.rcClient,
|
||||||
|
g.getJoinedSuperGroupsIDKey(userID),
|
||||||
|
g.expireTime,
|
||||||
|
func(ctx context.Context) ([]string, error) {
|
||||||
|
userGroup, err := g.mongoDB.GetSuperGroupByUserID(ctx, userID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return userGroup.GroupIDs, nil
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *GroupCacheRedis) GetSuperGroupMemberIDs(
|
||||||
|
ctx context.Context,
|
||||||
|
groupIDs ...string,
|
||||||
|
) (models []*unrelationtb.SuperGroupModel, err error) {
|
||||||
|
var keys []string
|
||||||
|
for _, group := range groupIDs {
|
||||||
|
keys = append(keys, g.getSuperGroupMemberIDsKey(group))
|
||||||
|
}
|
||||||
|
return batchGetCache(
|
||||||
|
ctx,
|
||||||
|
g.rcClient,
|
||||||
|
keys,
|
||||||
|
g.expireTime,
|
||||||
|
func(model *unrelationtb.SuperGroupModel, keys []string) (int, error) {
|
||||||
|
for i, key := range keys {
|
||||||
|
if g.getSuperGroupMemberIDsKey(model.GroupID) == key {
|
||||||
|
return i, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0, errIndex
|
||||||
|
},
|
||||||
|
func(ctx context.Context) ([]*unrelationtb.SuperGroupModel, error) {
|
||||||
|
return g.mongoDB.FindSuperGroup(ctx, groupIDs)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// userJoinSuperGroup.
|
// userJoinSuperGroup.
|
||||||
func (g *GroupCacheRedis) DelJoinedSuperGroupIDs(userIDs ...string) GroupCache {
|
func (g *GroupCacheRedis) DelJoinedSuperGroupIDs(userIDs ...string) GroupCache {
|
||||||
newGroupCache := g.NewCache()
|
new := g.NewCache()
|
||||||
keys := make([]string, 0, len(userIDs))
|
var keys []string
|
||||||
for _, userID := range userIDs {
|
for _, userID := range userIDs {
|
||||||
keys = append(keys, g.getJoinedSuperGroupsIDKey(userID))
|
keys = append(keys, g.getJoinedSuperGroupsIDKey(userID))
|
||||||
}
|
}
|
||||||
newGroupCache.AddKeys(keys...)
|
new.AddKeys(keys...)
|
||||||
|
return new
|
||||||
return newGroupCache
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) DelSuperGroupMemberIDs(groupIDs ...string) GroupCache {
|
func (g *GroupCacheRedis) DelSuperGroupMemberIDs(groupIDs ...string) GroupCache {
|
||||||
newGroupCache := g.NewCache()
|
new := g.NewCache()
|
||||||
keys := make([]string, 0, len(groupIDs))
|
var keys []string
|
||||||
for _, groupID := range groupIDs {
|
for _, groupID := range groupIDs {
|
||||||
keys = append(keys, g.getSuperGroupMemberIDsKey(groupID))
|
keys = append(keys, g.getSuperGroupMemberIDsKey(groupID))
|
||||||
}
|
}
|
||||||
newGroupCache.AddKeys(keys...)
|
new.AddKeys(keys...)
|
||||||
|
return new
|
||||||
return newGroupCache
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// groupMembersHash.
|
// groupMembersHash.
|
||||||
@@ -322,7 +351,10 @@ func (g *GroupCacheRedis) GetGroupMembersHash(ctx context.Context, groupID strin
|
|||||||
//)
|
//)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) GetGroupMemberHashMap(ctx context.Context, groupIDs []string) (map[string]*relationtb.GroupSimpleUserID, error) {
|
func (g *GroupCacheRedis) GetGroupMemberHashMap(
|
||||||
|
ctx context.Context,
|
||||||
|
groupIDs []string,
|
||||||
|
) (map[string]*relationtb.GroupSimpleUserID, error) {
|
||||||
res := make(map[string]*relationtb.GroupSimpleUserID)
|
res := make(map[string]*relationtb.GroupSimpleUserID)
|
||||||
for _, groupID := range groupIDs {
|
for _, groupID := range groupIDs {
|
||||||
hash, err := g.GetGroupMembersHash(ctx, groupID)
|
hash, err := g.GetGroupMembersHash(ctx, groupID)
|
||||||
@@ -336,22 +368,26 @@ func (g *GroupCacheRedis) GetGroupMemberHashMap(ctx context.Context, groupIDs []
|
|||||||
}
|
}
|
||||||
res[groupID] = &relationtb.GroupSimpleUserID{Hash: hash, MemberNum: uint32(num)}
|
res[groupID] = &relationtb.GroupSimpleUserID{Hash: hash, MemberNum: uint32(num)}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) DelGroupMembersHash(groupID string) GroupCache {
|
func (g *GroupCacheRedis) DelGroupMembersHash(groupID string) GroupCache {
|
||||||
cache := g.NewCache()
|
cache := g.NewCache()
|
||||||
cache.AddKeys(g.getGroupMembersHashKey(groupID))
|
cache.AddKeys(g.getGroupMembersHashKey(groupID))
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
// groupMemberIDs.
|
// groupMemberIDs.
|
||||||
func (g *GroupCacheRedis) GetGroupMemberIDs(ctx context.Context, groupID string) (groupMemberIDs []string, err error) {
|
func (g *GroupCacheRedis) GetGroupMemberIDs(ctx context.Context, groupID string) (groupMemberIDs []string, err error) {
|
||||||
return getCache(ctx, g.rcClient, g.getGroupMemberIDsKey(groupID), g.expireTime, func(ctx context.Context) ([]string, error) {
|
return getCache(
|
||||||
return g.groupMemberDB.FindMemberUserID(ctx, groupID)
|
ctx,
|
||||||
})
|
g.rcClient,
|
||||||
|
g.getGroupMemberIDsKey(groupID),
|
||||||
|
g.expireTime,
|
||||||
|
func(ctx context.Context) ([]string, error) {
|
||||||
|
return g.groupMemberDB.FindMemberUserID(ctx, groupID)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) GetGroupsMemberIDs(ctx context.Context, groupIDs []string) (map[string][]string, error) {
|
func (g *GroupCacheRedis) GetGroupsMemberIDs(ctx context.Context, groupIDs []string) (map[string][]string, error) {
|
||||||
@@ -363,56 +399,79 @@ func (g *GroupCacheRedis) GetGroupsMemberIDs(ctx context.Context, groupIDs []str
|
|||||||
}
|
}
|
||||||
m[groupID] = userIDs
|
m[groupID] = userIDs
|
||||||
}
|
}
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) DelGroupMemberIDs(groupID string) GroupCache {
|
func (g *GroupCacheRedis) DelGroupMemberIDs(groupID string) GroupCache {
|
||||||
cache := g.NewCache()
|
cache := g.NewCache()
|
||||||
cache.AddKeys(g.getGroupMemberIDsKey(groupID))
|
cache.AddKeys(g.getGroupMemberIDsKey(groupID))
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) GetJoinedGroupIDs(ctx context.Context, userID string) (joinedGroupIDs []string, err error) {
|
func (g *GroupCacheRedis) GetJoinedGroupIDs(ctx context.Context, userID string) (joinedGroupIDs []string, err error) {
|
||||||
return getCache(ctx, g.rcClient, g.getJoinedGroupsKey(userID), g.expireTime, func(ctx context.Context) ([]string, error) {
|
return getCache(
|
||||||
return g.groupMemberDB.FindUserJoinedGroupID(ctx, userID)
|
ctx,
|
||||||
})
|
g.rcClient,
|
||||||
|
g.getJoinedGroupsKey(userID),
|
||||||
|
g.expireTime,
|
||||||
|
func(ctx context.Context) ([]string, error) {
|
||||||
|
return g.groupMemberDB.FindUserJoinedGroupID(ctx, userID)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) DelJoinedGroupID(userIDs ...string) GroupCache {
|
func (g *GroupCacheRedis) DelJoinedGroupID(userIDs ...string) GroupCache {
|
||||||
keys := make([]string, 0, len(userIDs))
|
var keys []string
|
||||||
for _, userID := range userIDs {
|
for _, userID := range userIDs {
|
||||||
keys = append(keys, g.getJoinedGroupsKey(userID))
|
keys = append(keys, g.getJoinedGroupsKey(userID))
|
||||||
}
|
}
|
||||||
cache := g.NewCache()
|
cache := g.NewCache()
|
||||||
cache.AddKeys(keys...)
|
cache.AddKeys(keys...)
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) GetGroupMemberInfo(ctx context.Context, groupID, userID string) (groupMember *relationtb.GroupMemberModel, err error) {
|
func (g *GroupCacheRedis) GetGroupMemberInfo(
|
||||||
return getCache(ctx, g.rcClient, g.getGroupMemberInfoKey(groupID, userID), g.expireTime, func(ctx context.Context) (*relationtb.GroupMemberModel, error) {
|
ctx context.Context,
|
||||||
return g.groupMemberDB.Take(ctx, groupID, userID)
|
groupID, userID string,
|
||||||
})
|
) (groupMember *relationtb.GroupMemberModel, err error) {
|
||||||
|
return getCache(
|
||||||
|
ctx,
|
||||||
|
g.rcClient,
|
||||||
|
g.getGroupMemberInfoKey(groupID, userID),
|
||||||
|
g.expireTime,
|
||||||
|
func(ctx context.Context) (*relationtb.GroupMemberModel, error) {
|
||||||
|
return g.groupMemberDB.Take(ctx, groupID, userID)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) GetGroupMembersInfo(ctx context.Context, groupID string, userIDs []string) ([]*relationtb.GroupMemberModel, error) {
|
func (g *GroupCacheRedis) GetGroupMembersInfo(
|
||||||
//var keys []string
|
ctx context.Context,
|
||||||
//for _, userID := range userIDs {
|
groupID string,
|
||||||
// keys = append(keys, g.getGroupMemberInfoKey(groupID, userID))
|
userIDs []string,
|
||||||
//}
|
) ([]*relationtb.GroupMemberModel, error) {
|
||||||
//return batchGetCache(ctx, g.rcClient, keys, g.expireTime, g.GetGroupMemberIndex, func(ctx context.Context) ([]*relationtb.GroupMemberModel, error) {
|
var keys []string
|
||||||
// return g.groupMemberDB.Find(ctx, []string{groupID}, userIDs, nil)
|
for _, userID := range userIDs {
|
||||||
//})
|
keys = append(keys, g.getGroupMemberInfoKey(groupID, userID))
|
||||||
return batchGetCache2(ctx, g.rcClient, g.expireTime, userIDs, func(userID string) string {
|
}
|
||||||
return g.getGroupMemberInfoKey(groupID, userID)
|
return batchGetCache(
|
||||||
}, func(ctx context.Context, userID string) (*relationtb.GroupMemberModel, error) {
|
ctx,
|
||||||
return g.groupMemberDB.Take(ctx, groupID, userID)
|
g.rcClient,
|
||||||
})
|
keys,
|
||||||
|
g.expireTime,
|
||||||
|
g.GetGroupMemberIndex,
|
||||||
|
func(ctx context.Context) ([]*relationtb.GroupMemberModel, error) {
|
||||||
|
return g.groupMemberDB.Find(ctx, []string{groupID}, userIDs, nil)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) GetGroupMembersPage(ctx context.Context, groupID string, userIDs []string, showNumber, pageNumber int32) (total uint32, groupMembers []*relationtb.GroupMemberModel, err error) {
|
func (g *GroupCacheRedis) GetGroupMembersPage(
|
||||||
|
ctx context.Context,
|
||||||
|
groupID string,
|
||||||
|
userIDs []string,
|
||||||
|
showNumber, pageNumber int32,
|
||||||
|
) (total uint32, groupMembers []*relationtb.GroupMemberModel, err error) {
|
||||||
groupMemberIDs, err := g.GetGroupMemberIDs(ctx, groupID)
|
groupMemberIDs, err := g.GetGroupMemberIDs(ctx, groupID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil, err
|
return 0, nil, err
|
||||||
@@ -423,58 +482,72 @@ func (g *GroupCacheRedis) GetGroupMembersPage(ctx context.Context, groupID strin
|
|||||||
userIDs = groupMemberIDs
|
userIDs = groupMemberIDs
|
||||||
}
|
}
|
||||||
groupMembers, err = g.GetGroupMembersInfo(ctx, groupID, utils.Paginate(userIDs, int(showNumber), int(showNumber)))
|
groupMembers, err = g.GetGroupMembersInfo(ctx, groupID, utils.Paginate(userIDs, int(showNumber), int(showNumber)))
|
||||||
|
|
||||||
return uint32(len(userIDs)), groupMembers, err
|
return uint32(len(userIDs)), groupMembers, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) GetAllGroupMembersInfo(ctx context.Context, groupID string) (groupMembers []*relationtb.GroupMemberModel, err error) {
|
func (g *GroupCacheRedis) GetAllGroupMembersInfo(
|
||||||
|
ctx context.Context,
|
||||||
|
groupID string,
|
||||||
|
) (groupMembers []*relationtb.GroupMemberModel, err error) {
|
||||||
groupMemberIDs, err := g.GetGroupMemberIDs(ctx, groupID)
|
groupMemberIDs, err := g.GetGroupMemberIDs(ctx, groupID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return g.GetGroupMembersInfo(ctx, groupID, groupMemberIDs)
|
return g.GetGroupMembersInfo(ctx, groupID, groupMemberIDs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) GetAllGroupMemberInfo(ctx context.Context, groupID string) ([]*relationtb.GroupMemberModel, error) {
|
func (g *GroupCacheRedis) GetAllGroupMemberInfo(
|
||||||
|
ctx context.Context,
|
||||||
|
groupID string,
|
||||||
|
) ([]*relationtb.GroupMemberModel, error) {
|
||||||
groupMemberIDs, err := g.GetGroupMemberIDs(ctx, groupID)
|
groupMemberIDs, err := g.GetGroupMemberIDs(ctx, groupID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
//var keys []string
|
var keys []string
|
||||||
//for _, groupMemberID := range groupMemberIDs {
|
for _, groupMemberID := range groupMemberIDs {
|
||||||
// keys = append(keys, g.getGroupMemberInfoKey(groupID, groupMemberID))
|
keys = append(keys, g.getGroupMemberInfoKey(groupID, groupMemberID))
|
||||||
//}
|
}
|
||||||
//return batchGetCache(ctx, g.rcClient, keys, g.expireTime, g.GetGroupMemberIndex, func(ctx context.Context) ([]*relationtb.GroupMemberModel, error) {
|
return batchGetCache(
|
||||||
// return g.groupMemberDB.Find(ctx, []string{groupID}, groupMemberIDs, nil)
|
ctx,
|
||||||
//})
|
g.rcClient,
|
||||||
return g.GetGroupMembersInfo(ctx, groupID, groupMemberIDs)
|
keys,
|
||||||
|
g.expireTime,
|
||||||
|
g.GetGroupMemberIndex,
|
||||||
|
func(ctx context.Context) ([]*relationtb.GroupMemberModel, error) {
|
||||||
|
return g.groupMemberDB.Find(ctx, []string{groupID}, groupMemberIDs, nil)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) DelGroupMembersInfo(groupID string, userIDs ...string) GroupCache {
|
func (g *GroupCacheRedis) DelGroupMembersInfo(groupID string, userIDs ...string) GroupCache {
|
||||||
keys := make([]string, 0, len(userIDs))
|
var keys []string
|
||||||
for _, userID := range userIDs {
|
for _, userID := range userIDs {
|
||||||
keys = append(keys, g.getGroupMemberInfoKey(groupID, userID))
|
keys = append(keys, g.getGroupMemberInfoKey(groupID, userID))
|
||||||
}
|
}
|
||||||
cache := g.NewCache()
|
cache := g.NewCache()
|
||||||
cache.AddKeys(keys...)
|
cache.AddKeys(keys...)
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) GetGroupMemberNum(ctx context.Context, groupID string) (memberNum int64, err error) {
|
func (g *GroupCacheRedis) GetGroupMemberNum(ctx context.Context, groupID string) (memberNum int64, err error) {
|
||||||
return getCache(ctx, g.rcClient, g.getGroupMemberNumKey(groupID), g.expireTime, func(ctx context.Context) (int64, error) {
|
return getCache(
|
||||||
return g.groupMemberDB.TakeGroupMemberNum(ctx, groupID)
|
ctx,
|
||||||
})
|
g.rcClient,
|
||||||
|
g.getGroupMemberNumKey(groupID),
|
||||||
|
g.expireTime,
|
||||||
|
func(ctx context.Context) (int64, error) {
|
||||||
|
return g.groupMemberDB.TakeGroupMemberNum(ctx, groupID)
|
||||||
|
},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupCacheRedis) DelGroupsMemberNum(groupID ...string) GroupCache {
|
func (g *GroupCacheRedis) DelGroupsMemberNum(groupID ...string) GroupCache {
|
||||||
keys := make([]string, 0, len(groupID))
|
var keys []string
|
||||||
for _, groupID := range groupID {
|
for _, groupID := range groupID {
|
||||||
keys = append(keys, g.getGroupMemberNumKey(groupID))
|
keys = append(keys, g.getGroupMemberNumKey(groupID))
|
||||||
}
|
}
|
||||||
cache := g.NewCache()
|
cache := g.NewCache()
|
||||||
cache.AddKeys(keys...)
|
cache.AddKeys(keys...)
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|||||||
231
pkg/common/db/cache/meta_cache.go
vendored
231
pkg/common/db/cache/meta_cache.go
vendored
@@ -18,6 +18,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/dtm-labs/rockscache"
|
"github.com/dtm-labs/rockscache"
|
||||||
@@ -58,40 +59,27 @@ type metaCacheRedis struct {
|
|||||||
func (m *metaCacheRedis) ExecDel(ctx context.Context) error {
|
func (m *metaCacheRedis) ExecDel(ctx context.Context) error {
|
||||||
if len(m.keys) > 0 {
|
if len(m.keys) > 0 {
|
||||||
log.ZDebug(ctx, "delete cache", "keys", m.keys)
|
log.ZDebug(ctx, "delete cache", "keys", m.keys)
|
||||||
for _, key := range m.keys {
|
retryTimes := 0
|
||||||
for i := 0; i < m.maxRetryTimes; i++ {
|
for {
|
||||||
if err := m.rcClient.TagAsDeleted(key); err != nil {
|
if err := m.rcClient.TagAsDeletedBatch2(ctx, m.keys); err != nil {
|
||||||
log.ZError(ctx, "delete cache failed", err, "key", key)
|
if retryTimes >= m.maxRetryTimes {
|
||||||
time.Sleep(m.retryInterval)
|
err = errs.ErrInternalServer.Wrap(
|
||||||
continue
|
fmt.Sprintf(
|
||||||
|
"delete cache error: %v, keys: %v, retry times %d, please check redis server",
|
||||||
|
err,
|
||||||
|
m.keys,
|
||||||
|
retryTimes,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
log.ZWarn(ctx, "delete cache failed, please handle keys", err, "keys", m.keys)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
retryTimes++
|
||||||
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
//retryTimes := 0
|
|
||||||
//for {
|
|
||||||
// m.rcClient.TagAsDeleted()
|
|
||||||
// if err := m.rcClient.TagAsDeletedBatch2(ctx, []string{key}); err != nil {
|
|
||||||
// if retryTimes >= m.maxRetryTimes {
|
|
||||||
// err = errs.ErrInternalServer.Wrap(
|
|
||||||
// fmt.Sprintf(
|
|
||||||
// "delete cache error: %v, keys: %v, retry times %d, please check redis server",
|
|
||||||
// err,
|
|
||||||
// key,
|
|
||||||
// retryTimes,
|
|
||||||
// ),
|
|
||||||
// )
|
|
||||||
// log.ZWarn(ctx, "delete cache failed, please handle keys", err, "keys", key)
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// retryTimes++
|
|
||||||
// } else {
|
|
||||||
// break
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,11 +103,16 @@ func GetDefaultOpt() rockscache.Options {
|
|||||||
opts := rockscache.NewDefaultOptions()
|
opts := rockscache.NewDefaultOptions()
|
||||||
opts.StrongConsistency = true
|
opts.StrongConsistency = true
|
||||||
opts.RandomExpireAdjustment = 0.2
|
opts.RandomExpireAdjustment = 0.2
|
||||||
|
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCache[T any](ctx context.Context, rcClient *rockscache.Client, key string, expire time.Duration, fn func(ctx context.Context) (T, error)) (T, error) {
|
func getCache[T any](
|
||||||
|
ctx context.Context,
|
||||||
|
rcClient *rockscache.Client,
|
||||||
|
key string,
|
||||||
|
expire time.Duration,
|
||||||
|
fn func(ctx context.Context) (T, error),
|
||||||
|
) (T, error) {
|
||||||
var t T
|
var t T
|
||||||
var write bool
|
var write bool
|
||||||
v, err := rcClient.Fetch2(ctx, key, expire, func() (s string, err error) {
|
v, err := rcClient.Fetch2(ctx, key, expire, func() (s string, err error) {
|
||||||
@@ -132,7 +125,6 @@ func getCache[T any](ctx context.Context, rcClient *rockscache.Client, key strin
|
|||||||
return "", utils.Wrap(err, "")
|
return "", utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
write = true
|
write = true
|
||||||
|
|
||||||
return string(bs), nil
|
return string(bs), nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -147,108 +139,95 @@ func getCache[T any](ctx context.Context, rcClient *rockscache.Client, key strin
|
|||||||
err = json.Unmarshal([]byte(v), &t)
|
err = json.Unmarshal([]byte(v), &t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ZError(ctx, "cache json.Unmarshal failed", err, "key", key, "value", v, "expire", expire)
|
log.ZError(ctx, "cache json.Unmarshal failed", err, "key", key, "value", v, "expire", expire)
|
||||||
|
|
||||||
return t, utils.Wrap(err, "")
|
return t, utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//func batchGetCache[T any](ctx context.Context, rcClient *rockscache.Client, keys []string, expire time.Duration, keyIndexFn func(t T, keys []string) (int, error), fn func(ctx context.Context) ([]T, error)) ([]T, error) {
|
func batchGetCache[T any](
|
||||||
// batchMap, err := rcClient.FetchBatch2(ctx, keys, expire, func(idxs []int) (m map[int]string, err error) {
|
ctx context.Context,
|
||||||
// values := make(map[int]string)
|
rcClient *rockscache.Client,
|
||||||
// tArrays, err := fn(ctx)
|
keys []string,
|
||||||
// if err != nil {
|
expire time.Duration,
|
||||||
// return nil, err
|
keyIndexFn func(t T, keys []string) (int, error),
|
||||||
// }
|
fn func(ctx context.Context) ([]T, error),
|
||||||
// for _, v := range tArrays {
|
) ([]T, error) {
|
||||||
// index, err := keyIndexFn(v, keys)
|
batchMap, err := rcClient.FetchBatch2(ctx, keys, expire, func(idxs []int) (m map[int]string, err error) {
|
||||||
// if err != nil {
|
values := make(map[int]string)
|
||||||
// continue
|
tArrays, err := fn(ctx)
|
||||||
// }
|
|
||||||
// bs, err := json.Marshal(v)
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, utils.Wrap(err, "marshal failed")
|
|
||||||
// }
|
|
||||||
// values[index] = string(bs)
|
|
||||||
// }
|
|
||||||
// return values, nil
|
|
||||||
// })
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
// var tArrays []T
|
|
||||||
// for _, v := range batchMap {
|
|
||||||
// if v != "" {
|
|
||||||
// var t T
|
|
||||||
// err = json.Unmarshal([]byte(v), &t)
|
|
||||||
// if err != nil {
|
|
||||||
// return nil, utils.Wrap(err, "unmarshal failed")
|
|
||||||
// }
|
|
||||||
// tArrays = append(tArrays, t)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// return tArrays, nil
|
|
||||||
//}
|
|
||||||
|
|
||||||
func batchGetCache2[T any, K comparable](ctx context.Context, rcClient *rockscache.Client, expire time.Duration, keys []K, keyFn func(key K) string, fns func(ctx context.Context, key K) (T, error)) ([]T, error) {
|
|
||||||
if len(keys) == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
res := make([]T, 0, len(keys))
|
|
||||||
for _, key := range keys {
|
|
||||||
val, err := getCache(ctx, rcClient, keyFn(key), expire, func(ctx context.Context) (T, error) {
|
|
||||||
return fns(ctx, key)
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
res = append(res, val)
|
for _, v := range tArrays {
|
||||||
|
index, err := keyIndexFn(v, keys)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
bs, err := json.Marshal(v)
|
||||||
|
if err != nil {
|
||||||
|
return nil, utils.Wrap(err, "marshal failed")
|
||||||
|
}
|
||||||
|
values[index] = string(bs)
|
||||||
|
}
|
||||||
|
return values, nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
var tArrays []T
|
||||||
return res, nil
|
for _, v := range batchMap {
|
||||||
|
if v != "" {
|
||||||
|
var t T
|
||||||
|
err = json.Unmarshal([]byte(v), &t)
|
||||||
|
if err != nil {
|
||||||
|
return nil, utils.Wrap(err, "unmarshal failed")
|
||||||
|
}
|
||||||
|
tArrays = append(tArrays, t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tArrays, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//func batchGetCacheMap[T any](
|
func batchGetCacheMap[T any](
|
||||||
// ctx context.Context,
|
ctx context.Context,
|
||||||
// rcClient *rockscache.Client,
|
rcClient *rockscache.Client,
|
||||||
// keys, originKeys []string,
|
keys, originKeys []string,
|
||||||
// expire time.Duration,
|
expire time.Duration,
|
||||||
// keyIndexFn func(s string, keys []string) (int, error),
|
keyIndexFn func(s string, keys []string) (int, error),
|
||||||
// fn func(ctx context.Context) (map[string]T, error),
|
fn func(ctx context.Context) (map[string]T, error),
|
||||||
//) (map[string]T, error) {
|
) (map[string]T, error) {
|
||||||
// batchMap, err := rcClient.FetchBatch2(ctx, keys, expire, func(idxs []int) (m map[int]string, err error) {
|
batchMap, err := rcClient.FetchBatch2(ctx, keys, expire, func(idxs []int) (m map[int]string, err error) {
|
||||||
// tArrays, err := fn(ctx)
|
tArrays, err := fn(ctx)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return nil, err
|
return nil, err
|
||||||
// }
|
}
|
||||||
// values := make(map[int]string)
|
values := make(map[int]string)
|
||||||
// for k, v := range tArrays {
|
for k, v := range tArrays {
|
||||||
// index, err := keyIndexFn(k, originKeys)
|
index, err := keyIndexFn(k, originKeys)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// continue
|
continue
|
||||||
// }
|
}
|
||||||
// bs, err := json.Marshal(v)
|
bs, err := json.Marshal(v)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return nil, utils.Wrap(err, "marshal failed")
|
return nil, utils.Wrap(err, "marshal failed")
|
||||||
// }
|
}
|
||||||
// values[index] = string(bs)
|
values[index] = string(bs)
|
||||||
// }
|
}
|
||||||
// return values, nil
|
return values, nil
|
||||||
// })
|
})
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return nil, err
|
return nil, err
|
||||||
// }
|
}
|
||||||
// tMap := make(map[string]T)
|
tMap := make(map[string]T)
|
||||||
// for i, v := range batchMap {
|
for i, v := range batchMap {
|
||||||
// if v != "" {
|
if v != "" {
|
||||||
// var t T
|
var t T
|
||||||
// err = json.Unmarshal([]byte(v), &t)
|
err = json.Unmarshal([]byte(v), &t)
|
||||||
// if err != nil {
|
if err != nil {
|
||||||
// return nil, utils.Wrap(err, "unmarshal failed")
|
return nil, utils.Wrap(err, "unmarshal failed")
|
||||||
// }
|
}
|
||||||
// tMap[originKeys[i]] = t
|
tMap[originKeys[i]] = t
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// return tMap, nil
|
return tMap, nil
|
||||||
//}
|
}
|
||||||
|
|||||||
385
pkg/common/db/cache/msg.go
vendored
385
pkg/common/db/cache/msg.go
vendored
@@ -16,14 +16,13 @@ package cache
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"github.com/dtm-labs/rockscache"
|
|
||||||
unrelationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/unrelation"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
"github.com/openimsdk/open-im-server/v3/pkg/msgprocessor"
|
||||||
|
|
||||||
|
"github.com/dtm-labs/rockscache"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/tools/errs"
|
"github.com/OpenIMSDK/tools/errs"
|
||||||
|
|
||||||
"github.com/gogo/protobuf/jsonpb"
|
"github.com/gogo/protobuf/jsonpb"
|
||||||
@@ -34,6 +33,7 @@ import (
|
|||||||
"github.com/OpenIMSDK/tools/utils"
|
"github.com/OpenIMSDK/tools/utils"
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
|
unrelationtb "github.com/openimsdk/open-im-server/v3/pkg/common/db/table/unrelation"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
@@ -105,7 +105,11 @@ type MsgModel interface {
|
|||||||
GetTokensWithoutError(ctx context.Context, userID string, platformID int) (map[string]int, error)
|
GetTokensWithoutError(ctx context.Context, userID string, platformID int) (map[string]int, error)
|
||||||
SetTokenMapByUidPid(ctx context.Context, userID string, platformID int, m map[string]int) error
|
SetTokenMapByUidPid(ctx context.Context, userID string, platformID int, m map[string]int) error
|
||||||
DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error
|
DeleteTokenByUidPid(ctx context.Context, userID string, platformID int, fields []string) error
|
||||||
GetMessagesBySeq(ctx context.Context, conversationID string, seqs []int64) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error)
|
GetMessagesBySeq(
|
||||||
|
ctx context.Context,
|
||||||
|
conversationID string,
|
||||||
|
seqs []int64,
|
||||||
|
) (seqMsg []*sdkws.MsgData, failedSeqList []int64, err error)
|
||||||
SetMessageToCache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (int, error)
|
SetMessageToCache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (int, error)
|
||||||
UserDeleteMsgs(ctx context.Context, conversationID string, seqs []int64, userID string) error
|
UserDeleteMsgs(ctx context.Context, conversationID string, seqs []int64, userID string) error
|
||||||
DelUserDeleteMsgsList(ctx context.Context, conversationID string, seqs []int64)
|
DelUserDeleteMsgsList(ctx context.Context, conversationID string, seqs []int64)
|
||||||
@@ -118,7 +122,12 @@ type MsgModel interface {
|
|||||||
JudgeMessageReactionExist(ctx context.Context, clientMsgID string, sessionType int32) (bool, error)
|
JudgeMessageReactionExist(ctx context.Context, clientMsgID string, sessionType int32) (bool, error)
|
||||||
GetOneMessageAllReactionList(ctx context.Context, clientMsgID string, sessionType int32) (map[string]string, error)
|
GetOneMessageAllReactionList(ctx context.Context, clientMsgID string, sessionType int32) (map[string]string, error)
|
||||||
DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error
|
DeleteOneMessageKey(ctx context.Context, clientMsgID string, sessionType int32, subKey string) error
|
||||||
SetMessageReactionExpire(ctx context.Context, clientMsgID string, sessionType int32, expiration time.Duration) (bool, error)
|
SetMessageReactionExpire(
|
||||||
|
ctx context.Context,
|
||||||
|
clientMsgID string,
|
||||||
|
sessionType int32,
|
||||||
|
expiration time.Duration,
|
||||||
|
) (bool, error)
|
||||||
GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error)
|
GetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey string) (string, error)
|
||||||
SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error
|
SetMessageTypeKeyValue(ctx context.Context, clientMsgID string, sessionType int32, typeKey, value string) error
|
||||||
LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error
|
LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error
|
||||||
@@ -149,51 +158,50 @@ func (c *msgCache) getHasReadSeqKey(conversationID string, userID string) string
|
|||||||
return hasReadSeq + userID + ":" + conversationID
|
return hasReadSeq + userID + ":" + conversationID
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) setSeq(ctx context.Context, conversationID string, seq int64, getkey func(conversationID string) string) error {
|
func (c *msgCache) setSeq(
|
||||||
|
ctx context.Context,
|
||||||
|
conversationID string,
|
||||||
|
seq int64,
|
||||||
|
getkey func(conversationID string) string,
|
||||||
|
) error {
|
||||||
return utils.Wrap1(c.rdb.Set(ctx, getkey(conversationID), seq, 0).Err())
|
return utils.Wrap1(c.rdb.Set(ctx, getkey(conversationID), seq, 0).Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) getSeq(ctx context.Context, conversationID string, getkey func(conversationID string) string) (int64, error) {
|
func (c *msgCache) getSeq(
|
||||||
|
ctx context.Context,
|
||||||
|
conversationID string,
|
||||||
|
getkey func(conversationID string) string,
|
||||||
|
) (int64, error) {
|
||||||
return utils.Wrap2(c.rdb.Get(ctx, getkey(conversationID)).Int64())
|
return utils.Wrap2(c.rdb.Get(ctx, getkey(conversationID)).Int64())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) getSeqs(ctx context.Context, items []string, getkey func(s string) string) (m map[string]int64, err error) {
|
func (c *msgCache) getSeqs(
|
||||||
m = make(map[string]int64, len(items))
|
ctx context.Context,
|
||||||
for i, v := range items {
|
items []string,
|
||||||
res, err := c.rdb.Get(ctx, getkey(v)).Result()
|
getkey func(s string) string,
|
||||||
if err != nil && err != redis.Nil {
|
) (m map[string]int64, err error) {
|
||||||
|
pipe := c.rdb.Pipeline()
|
||||||
|
for _, v := range items {
|
||||||
|
if err := pipe.Get(ctx, getkey(v)).Err(); err != nil && err != redis.Nil {
|
||||||
return nil, errs.Wrap(err)
|
return nil, errs.Wrap(err)
|
||||||
}
|
}
|
||||||
val := utils.StringToInt64(res)
|
}
|
||||||
|
result, err := pipe.Exec(ctx)
|
||||||
|
if err != nil && err != redis.Nil {
|
||||||
|
return nil, errs.Wrap(err)
|
||||||
|
}
|
||||||
|
m = make(map[string]int64, len(items))
|
||||||
|
for i, v := range result {
|
||||||
|
seq := v.(*redis.StringCmd)
|
||||||
|
if seq.Err() != nil && seq.Err() != redis.Nil {
|
||||||
|
return nil, errs.Wrap(v.Err())
|
||||||
|
}
|
||||||
|
val := utils.StringToInt64(seq.Val())
|
||||||
if val != 0 {
|
if val != 0 {
|
||||||
m[items[i]] = val
|
m[items[i]] = val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
|
|
||||||
//pipe := c.rdb.Pipeline()
|
|
||||||
//for _, v := range items {
|
|
||||||
// if err := pipe.Get(ctx, getkey(v)).Err(); err != nil && err != redis.Nil {
|
|
||||||
// return nil, errs.Wrap(err)
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//result, err := pipe.Exec(ctx)
|
|
||||||
//if err != nil && err != redis.Nil {
|
|
||||||
// return nil, errs.Wrap(err)
|
|
||||||
//}
|
|
||||||
//m = make(map[string]int64, len(items))
|
|
||||||
//for i, v := range result {
|
|
||||||
// seq := v.(*redis.StringCmd)
|
|
||||||
// if seq.Err() != nil && seq.Err() != redis.Nil {
|
|
||||||
// return nil, errs.Wrap(v.Err())
|
|
||||||
// }
|
|
||||||
// val := utils.StringToInt64(seq.Val())
|
|
||||||
// if val != 0 {
|
|
||||||
// m[items[i]] = val
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//return m, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) SetMaxSeq(ctx context.Context, conversationID string, maxSeq int64) error {
|
func (c *msgCache) SetMaxSeq(ctx context.Context, conversationID string, maxSeq int64) error {
|
||||||
@@ -213,21 +221,15 @@ func (c *msgCache) SetMinSeq(ctx context.Context, conversationID string, minSeq
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) setSeqs(ctx context.Context, seqs map[string]int64, getkey func(key string) string) error {
|
func (c *msgCache) setSeqs(ctx context.Context, seqs map[string]int64, getkey func(key string) string) error {
|
||||||
for conversationID, seq := range seqs {
|
pipe := c.rdb.Pipeline()
|
||||||
if err := c.rdb.Set(ctx, getkey(conversationID), seq, 0).Err(); err != nil {
|
for k, seq := range seqs {
|
||||||
|
err := pipe.Set(ctx, getkey(k), seq, 0).Err()
|
||||||
|
if err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
_, err := pipe.Exec(ctx)
|
||||||
//pipe := c.rdb.Pipeline()
|
return err
|
||||||
//for k, seq := range seqs {
|
|
||||||
// err := pipe.Set(ctx, getkey(k), seq, 0).Err()
|
|
||||||
// if err != nil {
|
|
||||||
// return errs.Wrap(err)
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//_, err := pipe.Exec(ctx)
|
|
||||||
//return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) SetMinSeqs(ctx context.Context, seqs map[string]int64) error {
|
func (c *msgCache) SetMinSeqs(ctx context.Context, seqs map[string]int64) error {
|
||||||
@@ -250,17 +252,30 @@ func (c *msgCache) GetConversationUserMinSeq(ctx context.Context, conversationID
|
|||||||
return utils.Wrap2(c.rdb.Get(ctx, c.getConversationUserMinSeqKey(conversationID, userID)).Int64())
|
return utils.Wrap2(c.rdb.Get(ctx, c.getConversationUserMinSeqKey(conversationID, userID)).Int64())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) GetConversationUserMinSeqs(ctx context.Context, conversationID string, userIDs []string) (m map[string]int64, err error) {
|
func (c *msgCache) GetConversationUserMinSeqs(
|
||||||
|
ctx context.Context,
|
||||||
|
conversationID string,
|
||||||
|
userIDs []string,
|
||||||
|
) (m map[string]int64, err error) {
|
||||||
return c.getSeqs(ctx, userIDs, func(userID string) string {
|
return c.getSeqs(ctx, userIDs, func(userID string) string {
|
||||||
return c.getConversationUserMinSeqKey(conversationID, userID)
|
return c.getConversationUserMinSeqKey(conversationID, userID)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) SetConversationUserMinSeq(ctx context.Context, conversationID string, userID string, minSeq int64) error {
|
func (c *msgCache) SetConversationUserMinSeq(
|
||||||
|
ctx context.Context,
|
||||||
|
conversationID string,
|
||||||
|
userID string,
|
||||||
|
minSeq int64,
|
||||||
|
) error {
|
||||||
return utils.Wrap1(c.rdb.Set(ctx, c.getConversationUserMinSeqKey(conversationID, userID), minSeq, 0).Err())
|
return utils.Wrap1(c.rdb.Set(ctx, c.getConversationUserMinSeqKey(conversationID, userID), minSeq, 0).Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) SetConversationUserMinSeqs(ctx context.Context, conversationID string, seqs map[string]int64) (err error) {
|
func (c *msgCache) SetConversationUserMinSeqs(
|
||||||
|
ctx context.Context,
|
||||||
|
conversationID string,
|
||||||
|
seqs map[string]int64,
|
||||||
|
) (err error) {
|
||||||
return c.setSeqs(ctx, seqs, func(userID string) string {
|
return c.setSeqs(ctx, seqs, func(userID string) string {
|
||||||
return c.getConversationUserMinSeqKey(conversationID, userID)
|
return c.getConversationUserMinSeqKey(conversationID, userID)
|
||||||
})
|
})
|
||||||
@@ -288,7 +303,11 @@ func (c *msgCache) UserSetHasReadSeqs(ctx context.Context, userID string, hasRea
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) GetHasReadSeqs(ctx context.Context, userID string, conversationIDs []string) (map[string]int64, error) {
|
func (c *msgCache) GetHasReadSeqs(
|
||||||
|
ctx context.Context,
|
||||||
|
userID string,
|
||||||
|
conversationIDs []string,
|
||||||
|
) (map[string]int64, error) {
|
||||||
return c.getSeqs(ctx, conversationIDs, func(conversationID string) string {
|
return c.getSeqs(ctx, conversationIDs, func(conversationID string) string {
|
||||||
return c.getHasReadSeqKey(conversationID, userID)
|
return c.getHasReadSeqKey(conversationID, userID)
|
||||||
})
|
})
|
||||||
@@ -300,7 +319,6 @@ func (c *msgCache) GetHasReadSeq(ctx context.Context, userID string, conversatio
|
|||||||
|
|
||||||
func (c *msgCache) AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error {
|
func (c *msgCache) AddTokenFlag(ctx context.Context, userID string, platformID int, token string, flag int) error {
|
||||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID)
|
||||||
|
|
||||||
return errs.Wrap(c.rdb.HSet(ctx, key, token, flag).Err())
|
return errs.Wrap(c.rdb.HSet(ctx, key, token, flag).Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,7 +332,6 @@ func (c *msgCache) GetTokensWithoutError(ctx context.Context, userID string, pla
|
|||||||
for k, v := range m {
|
for k, v := range m {
|
||||||
mm[k] = utils.StringToInt(v)
|
mm[k] = utils.StringToInt(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
return mm, nil
|
return mm, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -324,13 +341,11 @@ func (c *msgCache) SetTokenMapByUidPid(ctx context.Context, userID string, platf
|
|||||||
for k, v := range m {
|
for k, v := range m {
|
||||||
mm[k] = v
|
mm[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
return errs.Wrap(c.rdb.HSet(ctx, key, mm).Err())
|
return errs.Wrap(c.rdb.HSet(ctx, key, mm).Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) DeleteTokenByUidPid(ctx context.Context, userID string, platform int, fields []string) error {
|
func (c *msgCache) DeleteTokenByUidPid(ctx context.Context, userID string, platform int, fields []string) error {
|
||||||
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platform)
|
key := uidPidToken + userID + ":" + constant.PlatformIDToName(platform)
|
||||||
|
|
||||||
return errs.Wrap(c.rdb.HDel(ctx, key, fields...).Err())
|
return errs.Wrap(c.rdb.HDel(ctx, key, fields...).Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -342,86 +357,58 @@ func (c *msgCache) allMessageCacheKey(conversationID string) string {
|
|||||||
return messageCache + conversationID + "_*"
|
return messageCache + conversationID + "_*"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) GetMessagesBySeq(ctx context.Context, conversationID string, seqs []int64) (seqMsgs []*sdkws.MsgData, failedSeqs []int64, err error) {
|
func (c *msgCache) GetMessagesBySeq(
|
||||||
for _, seq := range seqs {
|
ctx context.Context,
|
||||||
res, err := c.rdb.Get(ctx, c.getMessageCacheKey(conversationID, seq)).Result()
|
conversationID string,
|
||||||
if err != nil {
|
seqs []int64,
|
||||||
log.ZError(ctx, "GetMessagesBySeq failed", err, "conversationID", conversationID, "seq", seq)
|
) (seqMsgs []*sdkws.MsgData, failedSeqs []int64, err error) {
|
||||||
failedSeqs = append(failedSeqs, seq)
|
pipe := c.rdb.Pipeline()
|
||||||
continue
|
for _, v := range seqs {
|
||||||
|
// MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1
|
||||||
|
key := c.getMessageCacheKey(conversationID, v)
|
||||||
|
if err := pipe.Get(ctx, key).Err(); err != nil && err != redis.Nil {
|
||||||
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
msg := sdkws.MsgData{}
|
|
||||||
if err = msgprocessor.String2Pb(res, &msg); err != nil {
|
|
||||||
log.ZError(ctx, "GetMessagesBySeq Unmarshal failed", err, "res", res, "conversationID", conversationID, "seq", seq)
|
|
||||||
failedSeqs = append(failedSeqs, seq)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if msg.Status == constant.MsgDeleted {
|
|
||||||
failedSeqs = append(failedSeqs, seq)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
seqMsgs = append(seqMsgs, &msg)
|
|
||||||
}
|
}
|
||||||
|
result, err := pipe.Exec(ctx)
|
||||||
return
|
for i, v := range result {
|
||||||
//pipe := c.rdb.Pipeline()
|
cmd := v.(*redis.StringCmd)
|
||||||
//for _, v := range seqs {
|
if cmd.Err() != nil {
|
||||||
// // MESSAGE_CACHE:169.254.225.224_reliability1653387820_0_1
|
failedSeqs = append(failedSeqs, seqs[i])
|
||||||
// key := c.getMessageCacheKey(conversationID, v)
|
} else {
|
||||||
// if err := pipe.Get(ctx, key).Err(); err != nil && err != redis.Nil {
|
msg := sdkws.MsgData{}
|
||||||
// return nil, nil, err
|
err = msgprocessor.String2Pb(cmd.Val(), &msg)
|
||||||
// }
|
if err == nil {
|
||||||
//}
|
if msg.Status != constant.MsgDeleted {
|
||||||
//result, err := pipe.Exec(ctx)
|
seqMsgs = append(seqMsgs, &msg)
|
||||||
//for i, v := range result {
|
continue
|
||||||
// cmd := v.(*redis.StringCmd)
|
}
|
||||||
// if cmd.Err() != nil {
|
} else {
|
||||||
// failedSeqs = append(failedSeqs, seqs[i])
|
log.ZWarn(ctx, "UnmarshalString failed", err, "conversationID", conversationID, "seq", seqs[i], "msg", cmd.Val())
|
||||||
// } else {
|
}
|
||||||
// msg := sdkws.MsgData{}
|
failedSeqs = append(failedSeqs, seqs[i])
|
||||||
// err = msgprocessor.String2Pb(cmd.Val(), &msg)
|
}
|
||||||
// if err == nil {
|
}
|
||||||
// if msg.Status != constant.MsgDeleted {
|
return seqMsgs, failedSeqs, err
|
||||||
// seqMsgs = append(seqMsgs, &msg)
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// log.ZWarn(ctx, "UnmarshalString failed", err, "conversationID", conversationID, "seq", seqs[i], "msg", cmd.Val())
|
|
||||||
// }
|
|
||||||
// failedSeqs = append(failedSeqs, seqs[i])
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//return seqMsgs, failedSeqs, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) SetMessageToCache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (int, error) {
|
func (c *msgCache) SetMessageToCache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (int, error) {
|
||||||
|
pipe := c.rdb.Pipeline()
|
||||||
|
var failedMsgs []*sdkws.MsgData
|
||||||
for _, msg := range msgs {
|
for _, msg := range msgs {
|
||||||
|
key := c.getMessageCacheKey(conversationID, msg.Seq)
|
||||||
s, err := msgprocessor.Pb2String(msg)
|
s, err := msgprocessor.Pb2String(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errs.Wrap(err)
|
return 0, errs.Wrap(err)
|
||||||
}
|
}
|
||||||
key := c.getMessageCacheKey(conversationID, msg.Seq)
|
err = pipe.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err()
|
||||||
if err := c.rdb.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
if err != nil {
|
||||||
return 0, errs.Wrap(err)
|
failedMsgs = append(failedMsgs, msg)
|
||||||
|
log.ZWarn(ctx, "set msg 2 cache failed", err, "msg", failedMsgs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return len(msgs), nil
|
_, err := pipe.Exec(ctx)
|
||||||
//pipe := c.rdb.Pipeline()
|
return len(failedMsgs), err
|
||||||
//var failedMsgs []*sdkws.MsgData
|
|
||||||
//for _, msg := range msgs {
|
|
||||||
// key := c.getMessageCacheKey(conversationID, msg.Seq)
|
|
||||||
// s, err := msgprocessor.Pb2String(msg)
|
|
||||||
// if err != nil {
|
|
||||||
// return 0, errs.Wrap(err)
|
|
||||||
// }
|
|
||||||
// err = pipe.Set(ctx, key, s, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err()
|
|
||||||
// if err != nil {
|
|
||||||
// failedMsgs = append(failedMsgs, msg)
|
|
||||||
// log.ZWarn(ctx, "set msg 2 cache failed", err, "msg", failedMsgs)
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//_, err := pipe.Exec(ctx)
|
|
||||||
//return len(failedMsgs), err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) getMessageDelUserListKey(conversationID string, seq int64) string {
|
func (c *msgCache) getMessageDelUserListKey(conversationID string, seq int64) string {
|
||||||
@@ -433,47 +420,27 @@ func (c *msgCache) getUserDelList(conversationID, userID string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) UserDeleteMsgs(ctx context.Context, conversationID string, seqs []int64, userID string) error {
|
func (c *msgCache) UserDeleteMsgs(ctx context.Context, conversationID string, seqs []int64, userID string) error {
|
||||||
|
pipe := c.rdb.Pipeline()
|
||||||
for _, seq := range seqs {
|
for _, seq := range seqs {
|
||||||
delUserListKey := c.getMessageDelUserListKey(conversationID, seq)
|
delUserListKey := c.getMessageDelUserListKey(conversationID, seq)
|
||||||
userDelListKey := c.getUserDelList(conversationID, userID)
|
userDelListKey := c.getUserDelList(conversationID, userID)
|
||||||
err := c.rdb.SAdd(ctx, delUserListKey, userID).Err()
|
err := pipe.SAdd(ctx, delUserListKey, userID).Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
err = c.rdb.SAdd(ctx, userDelListKey, seq).Err()
|
err = pipe.SAdd(ctx, userDelListKey, seq).Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
if err := c.rdb.Expire(ctx, delUserListKey, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
if err := pipe.Expire(ctx, delUserListKey, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
if err := c.rdb.Expire(ctx, userDelListKey, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
if err := pipe.Expire(ctx, userDelListKey, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
_, err := pipe.Exec(ctx)
|
||||||
return nil
|
return errs.Wrap(err)
|
||||||
//pipe := c.rdb.Pipeline()
|
|
||||||
//for _, seq := range seqs {
|
|
||||||
// delUserListKey := c.getMessageDelUserListKey(conversationID, seq)
|
|
||||||
// userDelListKey := c.getUserDelList(conversationID, userID)
|
|
||||||
// err := pipe.SAdd(ctx, delUserListKey, userID).Err()
|
|
||||||
// if err != nil {
|
|
||||||
// return errs.Wrap(err)
|
|
||||||
// }
|
|
||||||
// err = pipe.SAdd(ctx, userDelListKey, seq).Err()
|
|
||||||
// if err != nil {
|
|
||||||
// return errs.Wrap(err)
|
|
||||||
// }
|
|
||||||
// if err := pipe.Expire(ctx, delUserListKey, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
|
||||||
// return errs.Wrap(err)
|
|
||||||
// }
|
|
||||||
// if err := pipe.Expire(ctx, userDelListKey, time.Duration(config.Config.MsgCacheTimeout)*time.Second).Err(); err != nil {
|
|
||||||
// return errs.Wrap(err)
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//_, err := pipe.Exec(ctx)
|
|
||||||
//return errs.Wrap(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) GetUserDelList(ctx context.Context, userID, conversationID string) (seqs []int64, err error) {
|
func (c *msgCache) GetUserDelList(ctx context.Context, userID, conversationID string) (seqs []int64, err error) {
|
||||||
@@ -485,7 +452,6 @@ func (c *msgCache) GetUserDelList(ctx context.Context, userID, conversationID st
|
|||||||
for i, v := range result {
|
for i, v := range result {
|
||||||
seqs[i] = utils.StringToInt64(v)
|
seqs[i] = utils.StringToInt64(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
return seqs, nil
|
return seqs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -494,102 +460,67 @@ func (c *msgCache) DelUserDeleteMsgsList(ctx context.Context, conversationID str
|
|||||||
delUsers, err := c.rdb.SMembers(ctx, c.getMessageDelUserListKey(conversationID, seq)).Result()
|
delUsers, err := c.rdb.SMembers(ctx, c.getMessageDelUserListKey(conversationID, seq)).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq)
|
log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(delUsers) > 0 {
|
if len(delUsers) > 0 {
|
||||||
|
pipe := c.rdb.Pipeline()
|
||||||
var failedFlag bool
|
var failedFlag bool
|
||||||
for _, userID := range delUsers {
|
for _, userID := range delUsers {
|
||||||
err = c.rdb.SRem(ctx, c.getUserDelList(conversationID, userID), seq).Err()
|
err = pipe.SRem(ctx, c.getUserDelList(conversationID, userID), seq).Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
failedFlag = true
|
failedFlag = true
|
||||||
log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq, "userID", userID)
|
log.ZWarn(
|
||||||
|
ctx,
|
||||||
|
"DelUserDeleteMsgsList failed",
|
||||||
|
err,
|
||||||
|
"conversationID",
|
||||||
|
conversationID,
|
||||||
|
"seq",
|
||||||
|
seq,
|
||||||
|
"userID",
|
||||||
|
userID,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !failedFlag {
|
if !failedFlag {
|
||||||
if err := c.rdb.Del(ctx, c.getMessageDelUserListKey(conversationID, seq)).Err(); err != nil {
|
if err := pipe.Del(ctx, c.getMessageDelUserListKey(conversationID, seq)).Err(); err != nil {
|
||||||
log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq)
|
log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if _, err := pipe.Exec(ctx); err != nil {
|
||||||
|
log.ZError(ctx, "pipe exec failed", err, "conversationID", conversationID, "seq", seq)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//for _, seq := range seqs {
|
|
||||||
// delUsers, err := c.rdb.SMembers(ctx, c.getMessageDelUserListKey(conversationID, seq)).Result()
|
|
||||||
// if err != nil {
|
|
||||||
// log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq)
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
// if len(delUsers) > 0 {
|
|
||||||
// pipe := c.rdb.Pipeline()
|
|
||||||
// var failedFlag bool
|
|
||||||
// for _, userID := range delUsers {
|
|
||||||
// err = pipe.SRem(ctx, c.getUserDelList(conversationID, userID), seq).Err()
|
|
||||||
// if err != nil {
|
|
||||||
// failedFlag = true
|
|
||||||
// log.ZWarn(
|
|
||||||
// ctx,
|
|
||||||
// "DelUserDeleteMsgsList failed",
|
|
||||||
// err,
|
|
||||||
// "conversationID",
|
|
||||||
// conversationID,
|
|
||||||
// "seq",
|
|
||||||
// seq,
|
|
||||||
// "userID",
|
|
||||||
// userID,
|
|
||||||
// )
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// if !failedFlag {
|
|
||||||
// if err := pipe.Del(ctx, c.getMessageDelUserListKey(conversationID, seq)).Err(); err != nil {
|
|
||||||
// log.ZWarn(ctx, "DelUserDeleteMsgsList failed", err, "conversationID", conversationID, "seq", seq)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// if _, err := pipe.Exec(ctx); err != nil {
|
|
||||||
// log.ZError(ctx, "pipe exec failed", err, "conversationID", conversationID, "seq", seq)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) DeleteMessages(ctx context.Context, conversationID string, seqs []int64) error {
|
func (c *msgCache) DeleteMessages(ctx context.Context, conversationID string, seqs []int64) error {
|
||||||
|
pipe := c.rdb.Pipeline()
|
||||||
for _, seq := range seqs {
|
for _, seq := range seqs {
|
||||||
if err := c.rdb.Del(ctx, c.getMessageCacheKey(conversationID, seq)).Err(); err != nil {
|
if err := pipe.Del(ctx, c.getMessageCacheKey(conversationID, seq)).Err(); err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
_, err := pipe.Exec(ctx)
|
||||||
//pipe := c.rdb.Pipeline()
|
return errs.Wrap(err)
|
||||||
//for _, seq := range seqs {
|
|
||||||
// if err := pipe.Del(ctx, c.getMessageCacheKey(conversationID, seq)).Err(); err != nil {
|
|
||||||
// return errs.Wrap(err)
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//_, err := pipe.Exec(ctx)
|
|
||||||
//return errs.Wrap(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) CleanUpOneConversationAllMsg(ctx context.Context, conversationID string) error {
|
func (c *msgCache) CleanUpOneConversationAllMsg(ctx context.Context, conversationID string) error {
|
||||||
vals, err := c.rdb.Keys(ctx, c.allMessageCacheKey(conversationID)).Result()
|
vals, err := c.rdb.Keys(ctx, c.allMessageCacheKey(conversationID)).Result()
|
||||||
if errors.Is(err, redis.Nil) {
|
if err == redis.Nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
|
pipe := c.rdb.Pipeline()
|
||||||
for _, v := range vals {
|
for _, v := range vals {
|
||||||
if err := c.rdb.Del(ctx, v).Err(); err != nil {
|
if err := pipe.Del(ctx, v).Err(); err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
_, err = pipe.Exec(ctx)
|
||||||
//pipe := c.rdb.Pipeline()
|
return errs.Wrap(err)
|
||||||
//for _, v := range vals {
|
|
||||||
// if err := pipe.Del(ctx, v).Err(); err != nil {
|
|
||||||
// return errs.Wrap(err)
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
//_, err = pipe.Exec(ctx)
|
|
||||||
//return errs.Wrap(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) DelMsgFromCache(ctx context.Context, userID string, seqs []int64) error {
|
func (c *msgCache) DelMsgFromCache(ctx context.Context, userID string, seqs []int64) error {
|
||||||
@@ -597,15 +528,13 @@ func (c *msgCache) DelMsgFromCache(ctx context.Context, userID string, seqs []in
|
|||||||
key := c.getMessageCacheKey(userID, seq)
|
key := c.getMessageCacheKey(userID, seq)
|
||||||
result, err := c.rdb.Get(ctx, key).Result()
|
result, err := c.rdb.Get(ctx, key).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, redis.Nil) {
|
if err == redis.Nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
var msg sdkws.MsgData
|
var msg sdkws.MsgData
|
||||||
err = jsonpb.UnmarshalString(result, &msg)
|
if err := jsonpb.UnmarshalString(result, &msg); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
msg.Status = constant.MsgDeleted
|
msg.Status = constant.MsgDeleted
|
||||||
@@ -617,7 +546,6 @@ func (c *msgCache) DelMsgFromCache(ctx context.Context, userID string, seqs []in
|
|||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -643,12 +571,20 @@ func (c *msgCache) SetSendMsgStatus(ctx context.Context, id string, status int32
|
|||||||
|
|
||||||
func (c *msgCache) GetSendMsgStatus(ctx context.Context, id string) (int32, error) {
|
func (c *msgCache) GetSendMsgStatus(ctx context.Context, id string) (int32, error) {
|
||||||
result, err := c.rdb.Get(ctx, sendMsgFailedFlag+id).Int()
|
result, err := c.rdb.Get(ctx, sendMsgFailedFlag+id).Int()
|
||||||
|
|
||||||
return int32(result), errs.Wrap(err)
|
return int32(result), errs.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) SetFcmToken(ctx context.Context, account string, platformID int, fcmToken string, expireTime int64) (err error) {
|
func (c *msgCache) SetFcmToken(
|
||||||
return errs.Wrap(c.rdb.Set(ctx, fcmToken+account+":"+strconv.Itoa(platformID), fcmToken, time.Duration(expireTime)*time.Second).Err())
|
ctx context.Context,
|
||||||
|
account string,
|
||||||
|
platformID int,
|
||||||
|
fcmToken string,
|
||||||
|
expireTime int64,
|
||||||
|
) (err error) {
|
||||||
|
return errs.Wrap(
|
||||||
|
c.rdb.Set(ctx, fcmToken+account+":"+strconv.Itoa(platformID), fcmToken, time.Duration(expireTime)*time.Second).
|
||||||
|
Err(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) GetFcmToken(ctx context.Context, account string, platformID int) (string, error) {
|
func (c *msgCache) GetFcmToken(ctx context.Context, account string, platformID int) (string, error) {
|
||||||
@@ -661,7 +597,6 @@ func (c *msgCache) DelFcmToken(ctx context.Context, account string, platformID i
|
|||||||
|
|
||||||
func (c *msgCache) IncrUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) {
|
func (c *msgCache) IncrUserBadgeUnreadCountSum(ctx context.Context, userID string) (int, error) {
|
||||||
seq, err := c.rdb.Incr(ctx, userBadgeUnreadCountSum+userID).Result()
|
seq, err := c.rdb.Incr(ctx, userBadgeUnreadCountSum+userID).Result()
|
||||||
|
|
||||||
return int(seq), errs.Wrap(err)
|
return int(seq), errs.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -675,13 +610,11 @@ func (c *msgCache) GetUserBadgeUnreadCountSum(ctx context.Context, userID string
|
|||||||
|
|
||||||
func (c *msgCache) LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
func (c *msgCache) LockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||||||
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
|
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
|
||||||
|
|
||||||
return errs.Wrap(c.rdb.SetNX(ctx, key, 1, time.Minute).Err())
|
return errs.Wrap(c.rdb.SetNX(ctx, key, 1, time.Minute).Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *msgCache) UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
func (c *msgCache) UnLockMessageTypeKey(ctx context.Context, clientMsgID string, TypeKey string) error {
|
||||||
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
|
key := exTypeKeyLocker + clientMsgID + "_" + TypeKey
|
||||||
|
|
||||||
return errs.Wrap(c.rdb.Del(ctx, key).Err())
|
return errs.Wrap(c.rdb.Del(ctx, key).Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -696,7 +629,6 @@ func (c *msgCache) getMessageReactionExPrefix(clientMsgID string, sessionType in
|
|||||||
case constant.NotificationChatType:
|
case constant.NotificationChatType:
|
||||||
return "EX_NOTIFICATION" + clientMsgID
|
return "EX_NOTIFICATION" + clientMsgID
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -705,7 +637,6 @@ func (c *msgCache) JudgeMessageReactionExist(ctx context.Context, clientMsgID st
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return false, utils.Wrap(err, "")
|
return false, utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return n > 0, nil
|
return n > 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
184
pkg/common/db/cache/user.go
vendored
184
pkg/common/db/cache/user.go
vendored
@@ -17,7 +17,6 @@ package cache
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"hash/crc32"
|
"hash/crc32"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
@@ -71,7 +70,6 @@ func NewUserCacheRedis(
|
|||||||
options rockscache.Options,
|
options rockscache.Options,
|
||||||
) UserCache {
|
) UserCache {
|
||||||
rcClient := rockscache.NewClient(rdb, options)
|
rcClient := rockscache.NewClient(rdb, options)
|
||||||
|
|
||||||
return &UserCacheRedis{
|
return &UserCacheRedis{
|
||||||
rdb: rdb,
|
rdb: rdb,
|
||||||
metaCache: NewMetaCacheRedis(rcClient),
|
metaCache: NewMetaCacheRedis(rcClient),
|
||||||
@@ -99,6 +97,10 @@ func (u *UserCacheRedis) getUserGlobalRecvMsgOptKey(userID string) string {
|
|||||||
return userGlobalRecvMsgOptKey + userID
|
return userGlobalRecvMsgOptKey + userID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *UserCacheRedis) getUserStatusHashKey(userID string, Id int32) string {
|
||||||
|
return userID + "_" + string(Id) + platformID
|
||||||
|
}
|
||||||
|
|
||||||
func (u *UserCacheRedis) GetUserInfo(ctx context.Context, userID string) (userInfo *relationtb.UserModel, err error) {
|
func (u *UserCacheRedis) GetUserInfo(ctx context.Context, userID string) (userInfo *relationtb.UserModel, err error) {
|
||||||
return getCache(
|
return getCache(
|
||||||
ctx,
|
ctx,
|
||||||
@@ -112,42 +114,36 @@ func (u *UserCacheRedis) GetUserInfo(ctx context.Context, userID string) (userIn
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserCacheRedis) GetUsersInfo(ctx context.Context, userIDs []string) ([]*relationtb.UserModel, error) {
|
func (u *UserCacheRedis) GetUsersInfo(ctx context.Context, userIDs []string) ([]*relationtb.UserModel, error) {
|
||||||
//var keys []string
|
var keys []string
|
||||||
//for _, userID := range userIDs {
|
for _, userID := range userIDs {
|
||||||
// keys = append(keys, u.getUserInfoKey(userID))
|
keys = append(keys, u.getUserInfoKey(userID))
|
||||||
//}
|
}
|
||||||
//return batchGetCache(
|
return batchGetCache(
|
||||||
// ctx,
|
ctx,
|
||||||
// u.rcClient,
|
u.rcClient,
|
||||||
// keys,
|
keys,
|
||||||
// u.expireTime,
|
u.expireTime,
|
||||||
// func(user *relationtb.UserModel, keys []string) (int, error) {
|
func(user *relationtb.UserModel, keys []string) (int, error) {
|
||||||
// for i, key := range keys {
|
for i, key := range keys {
|
||||||
// if key == u.getUserInfoKey(user.UserID) {
|
if key == u.getUserInfoKey(user.UserID) {
|
||||||
// return i, nil
|
return i, nil
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// return 0, errIndex
|
return 0, errIndex
|
||||||
// },
|
},
|
||||||
// func(ctx context.Context) ([]*relationtb.UserModel, error) {
|
func(ctx context.Context) ([]*relationtb.UserModel, error) {
|
||||||
// return u.userDB.Find(ctx, userIDs)
|
return u.userDB.Find(ctx, userIDs)
|
||||||
// },
|
},
|
||||||
//)
|
)
|
||||||
return batchGetCache2(ctx, u.rcClient, u.expireTime, userIDs, func(userID string) string {
|
|
||||||
return u.getUserInfoKey(userID)
|
|
||||||
}, func(ctx context.Context, userID string) (*relationtb.UserModel, error) {
|
|
||||||
return u.userDB.Take(ctx, userID)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserCacheRedis) DelUsersInfo(userIDs ...string) UserCache {
|
func (u *UserCacheRedis) DelUsersInfo(userIDs ...string) UserCache {
|
||||||
keys := make([]string, 0, len(userIDs))
|
var keys []string
|
||||||
for _, userID := range userIDs {
|
for _, userID := range userIDs {
|
||||||
keys = append(keys, u.getUserInfoKey(userID))
|
keys = append(keys, u.getUserInfoKey(userID))
|
||||||
}
|
}
|
||||||
cache := u.NewCache()
|
cache := u.NewCache()
|
||||||
cache.AddKeys(keys...)
|
cache.AddKeys(keys...)
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,19 +160,22 @@ func (u *UserCacheRedis) GetUserGlobalRecvMsgOpt(ctx context.Context, userID str
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserCacheRedis) DelUsersGlobalRecvMsgOpt(userIDs ...string) UserCache {
|
func (u *UserCacheRedis) DelUsersGlobalRecvMsgOpt(userIDs ...string) UserCache {
|
||||||
keys := make([]string, 0, len(userIDs))
|
var keys []string
|
||||||
for _, userID := range userIDs {
|
for _, userID := range userIDs {
|
||||||
keys = append(keys, u.getUserGlobalRecvMsgOptKey(userID))
|
keys = append(keys, u.getUserGlobalRecvMsgOptKey(userID))
|
||||||
}
|
}
|
||||||
cache := u.NewCache()
|
cache := u.NewCache()
|
||||||
cache.AddKeys(keys...)
|
cache.AddKeys(keys...)
|
||||||
|
|
||||||
return cache
|
return cache
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *UserCacheRedis) getOnlineStatusKey(userID string) string {
|
||||||
|
return olineStatusKey + userID
|
||||||
|
}
|
||||||
|
|
||||||
// GetUserStatus get user status.
|
// GetUserStatus get user status.
|
||||||
func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error) {
|
func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error) {
|
||||||
userStatus := make([]*user.OnlineStatus, 0, len(userIDs))
|
var res []*user.OnlineStatus
|
||||||
for _, userID := range userIDs {
|
for _, userID := range userIDs {
|
||||||
UserIDNum := crc32.ChecksumIEEE([]byte(userID))
|
UserIDNum := crc32.ChecksumIEEE([]byte(userID))
|
||||||
modKey := strconv.Itoa(int(UserIDNum % statusMod))
|
modKey := strconv.Itoa(int(UserIDNum % statusMod))
|
||||||
@@ -184,14 +183,13 @@ func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([
|
|||||||
key := olineStatusKey + modKey
|
key := olineStatusKey + modKey
|
||||||
result, err := u.rdb.HGet(ctx, key, userID).Result()
|
result, err := u.rdb.HGet(ctx, key, userID).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, redis.Nil) {
|
if err == redis.Nil {
|
||||||
// key or field does not exist
|
// key or field does not exist
|
||||||
userStatus = append(userStatus, &user.OnlineStatus{
|
res = append(res, &user.OnlineStatus{
|
||||||
UserID: userID,
|
UserID: userID,
|
||||||
Status: constant.Offline,
|
Status: constant.Offline,
|
||||||
PlatformIDs: nil,
|
PlatformIDs: nil,
|
||||||
})
|
})
|
||||||
|
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
return nil, errs.Wrap(err)
|
return nil, errs.Wrap(err)
|
||||||
@@ -203,10 +201,9 @@ func (u *UserCacheRedis) GetUserStatus(ctx context.Context, userIDs []string) ([
|
|||||||
}
|
}
|
||||||
onlineStatus.UserID = userID
|
onlineStatus.UserID = userID
|
||||||
onlineStatus.Status = constant.Online
|
onlineStatus.Status = constant.Online
|
||||||
userStatus = append(userStatus, &onlineStatus)
|
res = append(res, &onlineStatus)
|
||||||
}
|
}
|
||||||
|
return res, nil
|
||||||
return userStatus, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetUserStatus Set the user status and save it in redis.
|
// SetUserStatus Set the user status and save it in redis.
|
||||||
@@ -227,16 +224,15 @@ func (u *UserCacheRedis) SetUserStatus(ctx context.Context, userID string, statu
|
|||||||
Status: constant.Online,
|
Status: constant.Online,
|
||||||
PlatformIDs: []int32{platformID},
|
PlatformIDs: []int32{platformID},
|
||||||
}
|
}
|
||||||
jsonData, err2 := json.Marshal(&onlineStatus)
|
jsonData, err := json.Marshal(onlineStatus)
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
return errs.Wrap(err2)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
_, err2 = u.rdb.HSet(ctx, key, userID, string(jsonData)).Result()
|
_, err = u.rdb.HSet(ctx, key, userID, string(jsonData)).Result()
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
return errs.Wrap(err2)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
u.rdb.Expire(ctx, key, userOlineStatusExpireTime)
|
u.rdb.Expire(ctx, key, userOlineStatusExpireTime)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -244,7 +240,7 @@ func (u *UserCacheRedis) SetUserStatus(ctx context.Context, userID string, statu
|
|||||||
isNil := false
|
isNil := false
|
||||||
result, err := u.rdb.HGet(ctx, key, userID).Result()
|
result, err := u.rdb.HGet(ctx, key, userID).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, redis.Nil) {
|
if err == redis.Nil {
|
||||||
isNil = true
|
isNil = true
|
||||||
} else {
|
} else {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
@@ -252,45 +248,51 @@ func (u *UserCacheRedis) SetUserStatus(ctx context.Context, userID string, statu
|
|||||||
}
|
}
|
||||||
|
|
||||||
if status == constant.Offline {
|
if status == constant.Offline {
|
||||||
err = u.refreshStatusOffline(ctx, userID, status, platformID, isNil, err, result, key)
|
if isNil {
|
||||||
if err != nil {
|
log.ZWarn(ctx, "this user not online,maybe trigger order not right",
|
||||||
return err
|
err, "userStatus", status)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
} else {
|
var onlineStatus user.OnlineStatus
|
||||||
err = u.refreshStatusOnline(ctx, userID, platformID, isNil, err, result, key)
|
err = json.Unmarshal([]byte(result), &onlineStatus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
}
|
var newPlatformIDs []int32
|
||||||
|
for _, val := range onlineStatus.PlatformIDs {
|
||||||
return nil
|
if val != platformID {
|
||||||
}
|
newPlatformIDs = append(newPlatformIDs, val)
|
||||||
|
}
|
||||||
func (u *UserCacheRedis) refreshStatusOffline(ctx context.Context, userID string, status, platformID int32, isNil bool, err error, result, key string) error {
|
|
||||||
if isNil {
|
|
||||||
log.ZWarn(ctx, "this user not online,maybe trigger order not right",
|
|
||||||
err, "userStatus", status)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
var onlineStatus user.OnlineStatus
|
|
||||||
err = json.Unmarshal([]byte(result), &onlineStatus)
|
|
||||||
if err != nil {
|
|
||||||
return errs.Wrap(err)
|
|
||||||
}
|
|
||||||
var newPlatformIDs []int32
|
|
||||||
for _, val := range onlineStatus.PlatformIDs {
|
|
||||||
if val != platformID {
|
|
||||||
newPlatformIDs = append(newPlatformIDs, val)
|
|
||||||
}
|
}
|
||||||
}
|
if newPlatformIDs == nil {
|
||||||
if newPlatformIDs == nil {
|
_, err = u.rdb.HDel(ctx, key, userID).Result()
|
||||||
_, err = u.rdb.HDel(ctx, key, userID).Result()
|
if err != nil {
|
||||||
if err != nil {
|
return errs.Wrap(err)
|
||||||
return errs.Wrap(err)
|
}
|
||||||
|
} else {
|
||||||
|
onlineStatus.PlatformIDs = newPlatformIDs
|
||||||
|
newjsonData, err := json.Marshal(&onlineStatus)
|
||||||
|
if err != nil {
|
||||||
|
return errs.Wrap(err)
|
||||||
|
}
|
||||||
|
_, err = u.rdb.HSet(ctx, key, userID, string(newjsonData)).Result()
|
||||||
|
if err != nil {
|
||||||
|
return errs.Wrap(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
onlineStatus.PlatformIDs = newPlatformIDs
|
var onlineStatus user.OnlineStatus
|
||||||
|
if !isNil {
|
||||||
|
err = json.Unmarshal([]byte(result), &onlineStatus)
|
||||||
|
if err != nil {
|
||||||
|
return errs.Wrap(err)
|
||||||
|
}
|
||||||
|
onlineStatus.PlatformIDs = RemoveRepeatedElementsInList(append(onlineStatus.PlatformIDs, platformID))
|
||||||
|
} else {
|
||||||
|
onlineStatus.PlatformIDs = append(onlineStatus.PlatformIDs, platformID)
|
||||||
|
}
|
||||||
|
onlineStatus.Status = constant.Online
|
||||||
|
onlineStatus.UserID = userID
|
||||||
newjsonData, err := json.Marshal(&onlineStatus)
|
newjsonData, err := json.Marshal(&onlineStatus)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
@@ -299,31 +301,7 @@ func (u *UserCacheRedis) refreshStatusOffline(ctx context.Context, userID string
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (u *UserCacheRedis) refreshStatusOnline(ctx context.Context, userID string, platformID int32, isNil bool, err error, result, key string) error {
|
|
||||||
var onlineStatus user.OnlineStatus
|
|
||||||
if !isNil {
|
|
||||||
err2 := json.Unmarshal([]byte(result), &onlineStatus)
|
|
||||||
if err != nil {
|
|
||||||
return errs.Wrap(err2)
|
|
||||||
}
|
|
||||||
onlineStatus.PlatformIDs = RemoveRepeatedElementsInList(append(onlineStatus.PlatformIDs, platformID))
|
|
||||||
} else {
|
|
||||||
onlineStatus.PlatformIDs = append(onlineStatus.PlatformIDs, platformID)
|
|
||||||
}
|
|
||||||
onlineStatus.Status = constant.Online
|
|
||||||
onlineStatus.UserID = userID
|
|
||||||
newjsonData, err := json.Marshal(&onlineStatus)
|
|
||||||
if err != nil {
|
|
||||||
return errs.Wrap(err)
|
|
||||||
}
|
|
||||||
_, err = u.rdb.HSet(ctx, key, userID, string(newjsonData)).Result()
|
|
||||||
if err != nil {
|
|
||||||
return errs.Wrap(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -69,9 +69,9 @@ func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(deleteTokenKey) != 0 {
|
if len(deleteTokenKey) != 0 {
|
||||||
err2 := a.cache.DeleteTokenByUidPid(ctx, userID, platformID, deleteTokenKey)
|
err := a.cache.DeleteTokenByUidPid(ctx, userID, platformID, deleteTokenKey)
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
return "", err2
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
claims := tokenverify.BuildClaims(userID, platformID, a.accessExpire)
|
claims := tokenverify.BuildClaims(userID, platformID, a.accessExpire)
|
||||||
@@ -80,6 +80,5 @@ func (a *authDatabase) CreateToken(ctx context.Context, userID string, platformI
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", utils.Wrap(err, "")
|
return "", utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return tokenString, a.cache.AddTokenFlag(ctx, userID, platformID, tokenString, constant.NormalToken)
|
return tokenString, a.cache.AddTokenFlag(ctx, userID, platformID, tokenString, constant.NormalToken)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,7 +55,6 @@ func (b *blackDatabase) Create(ctx context.Context, blacks []*relation.BlackMode
|
|||||||
if err := b.black.Create(ctx, blacks); err != nil {
|
if err := b.black.Create(ctx, blacks); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return b.deleteBlackIDsCache(ctx, blacks)
|
return b.deleteBlackIDsCache(ctx, blacks)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,7 +63,6 @@ func (b *blackDatabase) Delete(ctx context.Context, blacks []*relation.BlackMode
|
|||||||
if err := b.black.Delete(ctx, blacks); err != nil {
|
if err := b.black.Delete(ctx, blacks); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return b.deleteBlackIDsCache(ctx, blacks)
|
return b.deleteBlackIDsCache(ctx, blacks)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,7 +71,6 @@ func (b *blackDatabase) deleteBlackIDsCache(ctx context.Context, blacks []*relat
|
|||||||
for _, black := range blacks {
|
for _, black := range blacks {
|
||||||
cache = cache.DelBlackIDs(ctx, black.OwnerUserID)
|
cache = cache.DelBlackIDs(ctx, black.OwnerUserID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache.ExecDel(ctx)
|
return cache.ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +97,6 @@ func (b *blackDatabase) CheckIn(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.ZDebug(ctx, "blackIDs", "user1BlackIDs", userID1BlackIDs, "user2BlackIDs", userID2BlackIDs)
|
log.ZDebug(ctx, "blackIDs", "user1BlackIDs", userID1BlackIDs, "user2BlackIDs", userID2BlackIDs)
|
||||||
|
|
||||||
return utils.IsContain(userID2, userID1BlackIDs), utils.IsContain(userID1, userID2BlackIDs), nil
|
return utils.IsContain(userID2, userID1BlackIDs), utils.IsContain(userID1, userID2BlackIDs), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ type ConversationDatabase interface {
|
|||||||
GetConversationIDs(ctx context.Context, userID string) ([]string, error)
|
GetConversationIDs(ctx context.Context, userID string) ([]string, error)
|
||||||
GetUserConversationIDsHash(ctx context.Context, ownerUserID string) (hash uint64, err error)
|
GetUserConversationIDsHash(ctx context.Context, ownerUserID string) (hash uint64, err error)
|
||||||
GetAllConversationIDs(ctx context.Context) ([]string, error)
|
GetAllConversationIDs(ctx context.Context) ([]string, error)
|
||||||
//GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error)
|
GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error)
|
||||||
GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*relationtb.ConversationModel, error)
|
GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*relationtb.ConversationModel, error)
|
||||||
GetConversationIDsNeedDestruct(ctx context.Context) ([]*relationtb.ConversationModel, error)
|
GetConversationIDsNeedDestruct(ctx context.Context) ([]*relationtb.ConversationModel, error)
|
||||||
GetConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) ([]string, error)
|
GetConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) ([]string, error)
|
||||||
@@ -99,8 +99,8 @@ func (c *conversationDatabase) SetUsersConversationFiledTx(ctx context.Context,
|
|||||||
now := time.Now()
|
now := time.Now()
|
||||||
for _, v := range NotUserIDs {
|
for _, v := range NotUserIDs {
|
||||||
temp := new(relationtb.ConversationModel)
|
temp := new(relationtb.ConversationModel)
|
||||||
if err2 := utils.CopyStructFields(temp, conversation); err2 != nil {
|
if err := utils.CopyStructFields(temp, conversation); err != nil {
|
||||||
return err2
|
return err
|
||||||
}
|
}
|
||||||
temp.OwnerUserID = v
|
temp.OwnerUserID = v
|
||||||
temp.CreateTime = now
|
temp.CreateTime = now
|
||||||
@@ -113,12 +113,10 @@ func (c *conversationDatabase) SetUsersConversationFiledTx(ctx context.Context,
|
|||||||
}
|
}
|
||||||
cache = cache.DelConversationIDs(NotUserIDs...).DelUserConversationIDsHash(NotUserIDs...).DelConversations(conversation.ConversationID, NotUserIDs...)
|
cache = cache.DelConversationIDs(NotUserIDs...).DelUserConversationIDsHash(NotUserIDs...).DelConversations(conversation.ConversationID, NotUserIDs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache.ExecDel(ctx)
|
return cache.ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,7 +130,6 @@ func (c *conversationDatabase) UpdateUsersConversationFiled(ctx context.Context,
|
|||||||
if _, ok := args["recv_msg_opt"]; ok {
|
if _, ok := args["recv_msg_opt"]; ok {
|
||||||
cache = cache.DelConversationNotReceiveMessageUserIDs(conversationID)
|
cache = cache.DelConversationNotReceiveMessageUserIDs(conversationID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache.ExecDel(ctx)
|
return cache.ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,14 +137,13 @@ func (c *conversationDatabase) CreateConversation(ctx context.Context, conversat
|
|||||||
if err := c.conversationDB.Create(ctx, conversations); err != nil {
|
if err := c.conversationDB.Create(ctx, conversations); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
userIDs := make([]string, 0, len(conversations))
|
var userIDs []string
|
||||||
cache := c.cache.NewCache()
|
cache := c.cache.NewCache()
|
||||||
for _, conversation := range conversations {
|
for _, conversation := range conversations {
|
||||||
cache = cache.DelConversations(conversation.OwnerUserID, conversation.ConversationID)
|
cache = cache.DelConversations(conversation.OwnerUserID, conversation.ConversationID)
|
||||||
cache = cache.DelConversationNotReceiveMessageUserIDs(conversation.ConversationID)
|
cache = cache.DelConversationNotReceiveMessageUserIDs(conversation.ConversationID)
|
||||||
userIDs = append(userIDs, conversation.OwnerUserID)
|
userIDs = append(userIDs, conversation.OwnerUserID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache.DelConversationIDs(userIDs...).DelUserConversationIDsHash(userIDs...).ExecDel(ctx)
|
return cache.DelConversationIDs(userIDs...).DelUserConversationIDsHash(userIDs...).ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,12 +178,10 @@ func (c *conversationDatabase) SyncPeerUserPrivateConversationTx(ctx context.Con
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache.ExecDel(ctx)
|
return cache.ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,15 +234,12 @@ func (c *conversationDatabase) SetUserConversations(ctx context.Context, ownerUs
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cache = cache.DelConversationIDs(ownerUserID).DelUserConversationIDsHash(ownerUserID)
|
cache = cache.DelConversationIDs(ownerUserID).DelUserConversationIDsHash(ownerUserID).DelConversationNotReceiveMessageUserIDs(utils.Slice(notExistConversations, func(e *relationtb.ConversationModel) string { return e.ConversationID })...)
|
||||||
cache = cache.DelConversationNotReceiveMessageUserIDs(utils.Slice(notExistConversations, func(e *relationtb.ConversationModel) string { return e.ConversationID })...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache.ExecDel(ctx)
|
return cache.ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -285,12 +276,10 @@ func (c *conversationDatabase) CreateGroupChatConversation(ctx context.Context,
|
|||||||
for _, v := range existConversationUserIDs {
|
for _, v := range existConversationUserIDs {
|
||||||
cache = cache.DelConversations(v, conversationID)
|
cache = cache.DelConversations(v, conversationID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache.ExecDel(ctx)
|
return cache.ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -306,9 +295,9 @@ func (c *conversationDatabase) GetAllConversationIDs(ctx context.Context) ([]str
|
|||||||
return c.conversationDB.GetAllConversationIDs(ctx)
|
return c.conversationDB.GetAllConversationIDs(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
//func (c *conversationDatabase) GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error) {
|
func (c *conversationDatabase) GetUserAllHasReadSeqs(ctx context.Context, ownerUserID string) (map[string]int64, error) {
|
||||||
// return c.cache.GetUserAllHasReadSeqs(ctx, ownerUserID)
|
return c.cache.GetUserAllHasReadSeqs(ctx, ownerUserID)
|
||||||
//}
|
}
|
||||||
|
|
||||||
func (c *conversationDatabase) GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*relationtb.ConversationModel, error) {
|
func (c *conversationDatabase) GetConversationsByConversationID(ctx context.Context, conversationIDs []string) ([]*relationtb.ConversationModel, error) {
|
||||||
return c.conversationDB.GetConversationsByConversationID(ctx, conversationIDs)
|
return c.conversationDB.GetConversationsByConversationID(ctx, conversationIDs)
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -110,7 +109,6 @@ func (f *friendDatabase) CheckIn(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return utils.IsContain(userID2, userID1FriendIDs), utils.IsContain(userID1, userID2FriendIDs), nil
|
return utils.IsContain(userID2, userID1FriendIDs), utils.IsContain(userID1, userID2FriendIDs), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,8 +121,8 @@ func (f *friendDatabase) AddFriendRequest(
|
|||||||
) (err error) {
|
) (err error) {
|
||||||
return f.tx.Transaction(func(tx any) error {
|
return f.tx.Transaction(func(tx any) error {
|
||||||
_, err := f.friendRequest.NewTx(tx).Take(ctx, fromUserID, toUserID)
|
_, err := f.friendRequest.NewTx(tx).Take(ctx, fromUserID, toUserID)
|
||||||
// if there is a db error
|
// 有db错误
|
||||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
if err != nil && errs.Unwrap(err) != gorm.ErrRecordNotFound {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// 无错误 则更新
|
// 无错误 则更新
|
||||||
@@ -138,14 +136,12 @@ func (f *friendDatabase) AddFriendRequest(
|
|||||||
if err := f.friendRequest.NewTx(tx).UpdateByMap(ctx, fromUserID, toUserID, m); err != nil {
|
if err := f.friendRequest.NewTx(tx).UpdateByMap(ctx, fromUserID, toUserID, m); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// gorm.ErrRecordNotFound 错误,则新增
|
// gorm.ErrRecordNotFound 错误,则新增
|
||||||
if err := f.friendRequest.NewTx(tx).Create(ctx, []*relation.FriendRequestModel{{FromUserID: fromUserID, ToUserID: toUserID, ReqMsg: reqMsg, Ex: ex, CreateTime: time.Now(), HandleTime: time.Unix(0, 0)}}); err != nil {
|
if err := f.friendRequest.NewTx(tx).Create(ctx, []*relation.FriendRequestModel{{FromUserID: fromUserID, ToUserID: toUserID, ReqMsg: reqMsg, Ex: ex, CreateTime: time.Now(), HandleTime: time.Unix(0, 0)}}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -158,11 +154,11 @@ func (f *friendDatabase) BecomeFriends(
|
|||||||
addSource int32,
|
addSource int32,
|
||||||
) (err error) {
|
) (err error) {
|
||||||
cache := f.cache.NewCache()
|
cache := f.cache.NewCache()
|
||||||
fn := func(tx any) error {
|
if err := f.tx.Transaction(func(tx any) error {
|
||||||
// first,find and drop delete ones
|
// 先find 找出重复的 去掉重复的
|
||||||
fs1, err2 := f.friend.NewTx(tx).FindFriends(ctx, ownerUserID, friendUserIDs)
|
fs1, err := f.friend.NewTx(tx).FindFriends(ctx, ownerUserID, friendUserIDs)
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
return err2
|
return err
|
||||||
}
|
}
|
||||||
opUserID := mcontext.GetOperationID(ctx)
|
opUserID := mcontext.GetOperationID(ctx)
|
||||||
for _, v := range friendUserIDs {
|
for _, v := range friendUserIDs {
|
||||||
@@ -172,13 +168,13 @@ func (f *friendDatabase) BecomeFriends(
|
|||||||
return e.FriendUserID
|
return e.FriendUserID
|
||||||
})
|
})
|
||||||
|
|
||||||
err2 = f.friend.NewTx(tx).Create(ctx, fs11)
|
err = f.friend.NewTx(tx).Create(ctx, fs11)
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
return err2
|
return err
|
||||||
}
|
}
|
||||||
fs2, err2 := f.friend.NewTx(tx).FindReversalFriends(ctx, ownerUserID, friendUserIDs)
|
fs2, err := f.friend.NewTx(tx).FindReversalFriends(ctx, ownerUserID, friendUserIDs)
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
return err2
|
return err
|
||||||
}
|
}
|
||||||
var newFriendIDs []string
|
var newFriendIDs []string
|
||||||
for _, v := range friendUserIDs {
|
for _, v := range friendUserIDs {
|
||||||
@@ -188,20 +184,16 @@ func (f *friendDatabase) BecomeFriends(
|
|||||||
fs22 := utils.DistinctAny(fs2, func(e *relation.FriendModel) string {
|
fs22 := utils.DistinctAny(fs2, func(e *relation.FriendModel) string {
|
||||||
return e.OwnerUserID
|
return e.OwnerUserID
|
||||||
})
|
})
|
||||||
err2 = f.friend.NewTx(tx).Create(ctx, fs22)
|
err = f.friend.NewTx(tx).Create(ctx, fs22)
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
return err2
|
return err
|
||||||
}
|
}
|
||||||
newFriendIDs = append(newFriendIDs, ownerUserID)
|
newFriendIDs = append(newFriendIDs, ownerUserID)
|
||||||
cache = cache.DelFriendIDs(newFriendIDs...)
|
cache = cache.DelFriendIDs(newFriendIDs...)
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err = f.tx.Transaction(fn)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return cache.ExecDel(ctx)
|
return cache.ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,7 +216,6 @@ func (f *friendDatabase) RefuseFriendRequest(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,7 +251,7 @@ func (f *friendDatabase) AgreeFriendRequest(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
} else if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
} else if err != nil && errs.Unwrap(err) != gorm.ErrRecordNotFound {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -299,7 +290,6 @@ func (f *friendDatabase) AgreeFriendRequest(
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return f.cache.DelFriendIDs(friendRequest.ToUserID, friendRequest.FromUserID).ExecDel(ctx)
|
return f.cache.DelFriendIDs(friendRequest.ToUserID, friendRequest.FromUserID).ExecDel(ctx)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -309,7 +299,6 @@ func (f *friendDatabase) Delete(ctx context.Context, ownerUserID string, friendU
|
|||||||
if err := f.friend.Delete(ctx, ownerUserID, friendUserIDs); err != nil {
|
if err := f.friend.Delete(ctx, ownerUserID, friendUserIDs); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return f.cache.DelFriendIDs(append(friendUserIDs, ownerUserID)...).ExecDel(ctx)
|
return f.cache.DelFriendIDs(append(friendUserIDs, ownerUserID)...).ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -318,7 +307,6 @@ func (f *friendDatabase) UpdateRemark(ctx context.Context, ownerUserID, friendUs
|
|||||||
if err := f.friend.UpdateRemark(ctx, ownerUserID, friendUserID, remark); err != nil {
|
if err := f.friend.UpdateRemark(ctx, ownerUserID, friendUserID, remark); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return f.cache.DelFriend(ownerUserID, friendUserID).ExecDel(ctx)
|
return f.cache.DelFriend(ownerUserID, friendUserID).ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -371,7 +359,6 @@ func (f *friendDatabase) FindFriendsWithError(
|
|||||||
if len(friends) != len(friendUserIDs) {
|
if len(friends) != len(friendUserIDs) {
|
||||||
err = errs.ErrRecordNotFound.Wrap()
|
err = errs.ErrRecordNotFound.Wrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -102,7 +102,6 @@ func NewGroupDatabase(
|
|||||||
cache: cache,
|
cache: cache,
|
||||||
mongoDB: superGroup,
|
mongoDB: superGroup,
|
||||||
}
|
}
|
||||||
|
|
||||||
return database
|
return database
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,7 +109,6 @@ func InitGroupDatabase(db *gorm.DB, rdb redis.UniversalClient, database *mongo.D
|
|||||||
rcOptions := rockscache.NewDefaultOptions()
|
rcOptions := rockscache.NewDefaultOptions()
|
||||||
rcOptions.StrongConsistency = true
|
rcOptions.StrongConsistency = true
|
||||||
rcOptions.RandomExpireAdjustment = 0.2
|
rcOptions.RandomExpireAdjustment = 0.2
|
||||||
|
|
||||||
return NewGroupDatabase(
|
return NewGroupDatabase(
|
||||||
relation.NewGroupDB(db),
|
relation.NewGroupDB(db),
|
||||||
relation.NewGroupMemberDB(db),
|
relation.NewGroupMemberDB(db),
|
||||||
@@ -153,7 +151,6 @@ func (g *groupDatabase) FindGroupMemberNum(ctx context.Context, groupID string)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return uint32(num), nil
|
return uint32(num), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,12 +184,10 @@ func (g *groupDatabase) CreateGroup(
|
|||||||
cache = cache.DelJoinedGroupID(groupMember.UserID).DelGroupMembersInfo(groupMember.GroupID, groupMember.UserID)
|
cache = cache.DelJoinedGroupID(groupMember.UserID).DelGroupMembersInfo(groupMember.GroupID, groupMember.UserID)
|
||||||
}
|
}
|
||||||
cache = cache.DelGroupsInfo(createGroupIDs...)
|
cache = cache.DelGroupsInfo(createGroupIDs...)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache.ExecDel(ctx)
|
return cache.ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,7 +211,6 @@ func (g *groupDatabase) UpdateGroup(ctx context.Context, groupID string, data ma
|
|||||||
if err := g.groupDB.UpdateMap(ctx, groupID, data); err != nil {
|
if err := g.groupDB.UpdateMap(ctx, groupID, data); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return g.cache.DelGroupsInfo(groupID).ExecDel(ctx)
|
return g.cache.DelGroupsInfo(groupID).ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,12 +231,10 @@ func (g *groupDatabase) DismissGroup(ctx context.Context, groupID string, delete
|
|||||||
cache = cache.DelJoinedGroupID(userIDs...).DelGroupMemberIDs(groupID).DelGroupsMemberNum(groupID).DelGroupMembersHash(groupID)
|
cache = cache.DelJoinedGroupID(userIDs...).DelGroupMemberIDs(groupID).DelGroupsMemberNum(groupID).DelGroupMembersHash(groupID)
|
||||||
}
|
}
|
||||||
cache = cache.DelGroupsInfo(groupID)
|
cache = cache.DelGroupsInfo(groupID)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache.ExecDel(ctx)
|
return cache.ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -284,7 +276,6 @@ func (g *groupDatabase) FindGroupMember(ctx context.Context, groupIDs []string,
|
|||||||
}
|
}
|
||||||
res = append(res, v)
|
res = append(res, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
if len(roleLevels) == 0 {
|
if len(roleLevels) == 0 {
|
||||||
@@ -295,10 +286,8 @@ func (g *groupDatabase) FindGroupMember(ctx context.Context, groupIDs []string,
|
|||||||
}
|
}
|
||||||
totalGroupMembers = append(totalGroupMembers, groupMembers...)
|
totalGroupMembers = append(totalGroupMembers, groupMembers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return totalGroupMembers, nil
|
return totalGroupMembers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return g.groupMemberDB.Find(ctx, groupIDs, userIDs, roleLevels)
|
return g.groupMemberDB.Find(ctx, groupIDs, userIDs, roleLevels)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -318,7 +307,6 @@ func (g *groupDatabase) PageGetJoinGroup(
|
|||||||
}
|
}
|
||||||
totalGroupMembers = append(totalGroupMembers, groupMembers...)
|
totalGroupMembers = append(totalGroupMembers, groupMembers...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return uint32(len(groupIDs)), totalGroupMembers, nil
|
return uint32(len(groupIDs)), totalGroupMembers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,7 +327,6 @@ func (g *groupDatabase) PageGetGroupMember(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil, err
|
return 0, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return uint32(len(groupMemberIDs)), members, nil
|
return uint32(len(groupMemberIDs)), members, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -391,7 +378,6 @@ func (g *groupDatabase) HandlerGroupRequest(
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -400,7 +386,6 @@ func (g *groupDatabase) DeleteGroupMember(ctx context.Context, groupID string, u
|
|||||||
if err := g.groupMemberDB.Delete(ctx, groupID, userIDs); err != nil {
|
if err := g.groupMemberDB.Delete(ctx, groupID, userIDs); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return g.cache.DelGroupMembersHash(groupID).
|
return g.cache.DelGroupMembersHash(groupID).
|
||||||
DelGroupMemberIDs(groupID).
|
DelGroupMemberIDs(groupID).
|
||||||
DelGroupsMemberNum(groupID).
|
DelGroupsMemberNum(groupID).
|
||||||
@@ -425,7 +410,6 @@ func (g *groupDatabase) MapGroupMemberNum(ctx context.Context, groupIDs []string
|
|||||||
}
|
}
|
||||||
m[groupID] = uint32(num)
|
m[groupID] = uint32(num)
|
||||||
}
|
}
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -445,7 +429,6 @@ func (g *groupDatabase) TransferGroupOwner(ctx context.Context, groupID string,
|
|||||||
if rowsAffected != 1 {
|
if rowsAffected != 1 {
|
||||||
return utils.Wrap(fmt.Errorf("newOwnerUserID %s rowsAffected = %d", newOwnerUserID, rowsAffected), "")
|
return utils.Wrap(fmt.Errorf("newOwnerUserID %s rowsAffected = %d", newOwnerUserID, rowsAffected), "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return g.cache.DelGroupMembersInfo(groupID, oldOwnerUserID, newOwnerUserID).DelGroupMembersHash(groupID).ExecDel(ctx)
|
return g.cache.DelGroupMembersInfo(groupID, oldOwnerUserID, newOwnerUserID).DelGroupMembersHash(groupID).ExecDel(ctx)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -459,7 +442,6 @@ func (g *groupDatabase) UpdateGroupMember(
|
|||||||
if err := g.groupMemberDB.Update(ctx, groupID, userID, data); err != nil {
|
if err := g.groupMemberDB.Update(ctx, groupID, userID, data); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return g.cache.DelGroupMembersInfo(groupID, userID).ExecDel(ctx)
|
return g.cache.DelGroupMembersInfo(groupID, userID).ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -472,12 +454,10 @@ func (g *groupDatabase) UpdateGroupMembers(ctx context.Context, data []*relation
|
|||||||
}
|
}
|
||||||
cache = cache.DelGroupMembersInfo(item.GroupID, item.UserID)
|
cache = cache.DelGroupMembersInfo(item.GroupID, item.UserID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache.ExecDel(ctx)
|
return cache.ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -489,7 +469,6 @@ func (g *groupDatabase) CreateGroupRequest(ctx context.Context, requests []*rela
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return db.Create(ctx, requests)
|
return db.Create(ctx, requests)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -525,7 +504,6 @@ func (g *groupDatabase) CreateSuperGroup(ctx context.Context, groupID string, in
|
|||||||
if err := g.mongoDB.CreateSuperGroup(ctx, groupID, initMemberIDs); err != nil {
|
if err := g.mongoDB.CreateSuperGroup(ctx, groupID, initMemberIDs); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(initMemberIDs...).ExecDel(ctx)
|
return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(initMemberIDs...).ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -543,12 +521,10 @@ func (g *groupDatabase) DeleteSuperGroup(ctx context.Context, groupID string) er
|
|||||||
if len(models) > 0 {
|
if len(models) > 0 {
|
||||||
cache = cache.DelJoinedSuperGroupIDs(models[0].MemberIDs...)
|
cache = cache.DelJoinedSuperGroupIDs(models[0].MemberIDs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return cache.ExecDel(ctx)
|
return cache.ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -556,7 +532,6 @@ func (g *groupDatabase) DeleteSuperGroupMember(ctx context.Context, groupID stri
|
|||||||
if err := g.mongoDB.RemoverUserFromSuperGroup(ctx, groupID, userIDs); err != nil {
|
if err := g.mongoDB.RemoverUserFromSuperGroup(ctx, groupID, userIDs); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(userIDs...).ExecDel(ctx)
|
return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(userIDs...).ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -564,7 +539,6 @@ func (g *groupDatabase) CreateSuperGroupMember(ctx context.Context, groupID stri
|
|||||||
if err := g.mongoDB.AddUserToSuperGroup(ctx, groupID, userIDs); err != nil {
|
if err := g.mongoDB.AddUserToSuperGroup(ctx, groupID, userIDs); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(userIDs...).ExecDel(ctx)
|
return g.cache.DelSuperGroupMemberIDs(groupID).DelJoinedSuperGroupIDs(userIDs...).ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -135,7 +135,6 @@ func InitCommonMsgDatabase(rdb redis.UniversalClient, database *mongo.Database)
|
|||||||
cacheModel := cache.NewMsgCacheModel(rdb)
|
cacheModel := cache.NewMsgCacheModel(rdb)
|
||||||
msgDocModel := unrelation.NewMsgMongoDriver(database)
|
msgDocModel := unrelation.NewMsgMongoDriver(database)
|
||||||
CommonMsgDatabase := NewCommonMsgDatabase(msgDocModel, cacheModel)
|
CommonMsgDatabase := NewCommonMsgDatabase(msgDocModel, cacheModel)
|
||||||
|
|
||||||
return CommonMsgDatabase
|
return CommonMsgDatabase
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,17 +150,14 @@ type commonMsgDatabase struct {
|
|||||||
|
|
||||||
func (db *commonMsgDatabase) MsgToMQ(ctx context.Context, key string, msg2mq *sdkws.MsgData) error {
|
func (db *commonMsgDatabase) MsgToMQ(ctx context.Context, key string, msg2mq *sdkws.MsgData) error {
|
||||||
_, _, err := db.producer.SendMessage(ctx, key, msg2mq)
|
_, _, err := db.producer.SendMessage(ctx, key, msg2mq)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *commonMsgDatabase) MsgToModifyMQ(ctx context.Context, key, conversationID string, messages []*sdkws.MsgData) error {
|
func (db *commonMsgDatabase) MsgToModifyMQ(ctx context.Context, key, conversationID string, messages []*sdkws.MsgData) error {
|
||||||
if len(messages) > 0 {
|
if len(messages) > 0 {
|
||||||
_, _, err := db.producerToModify.SendMessage(ctx, key, &pbmsg.MsgDataToModifyByMQ{ConversationID: conversationID, Messages: messages})
|
_, _, err := db.producerToModify.SendMessage(ctx, key, &pbmsg.MsgDataToModifyByMQ{ConversationID: conversationID, Messages: messages})
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,26 +165,26 @@ func (db *commonMsgDatabase) MsgToPushMQ(ctx context.Context, key, conversationI
|
|||||||
partition, offset, err := db.producerToPush.SendMessage(ctx, key, &pbmsg.PushMsgDataToMQ{MsgData: msg2mq, ConversationID: conversationID})
|
partition, offset, err := db.producerToPush.SendMessage(ctx, key, &pbmsg.PushMsgDataToMQ{MsgData: msg2mq, ConversationID: conversationID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ZError(ctx, "MsgToPushMQ", err, "key", key, "msg2mq", msg2mq)
|
log.ZError(ctx, "MsgToPushMQ", err, "key", key, "msg2mq", msg2mq)
|
||||||
|
|
||||||
return 0, 0, err
|
return 0, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return partition, offset, nil
|
return partition, offset, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *commonMsgDatabase) MsgToMongoMQ(ctx context.Context, key, conversationID string, messages []*sdkws.MsgData, lastSeq int64) error {
|
func (db *commonMsgDatabase) MsgToMongoMQ(ctx context.Context, key, conversationID string, messages []*sdkws.MsgData, lastSeq int64) error {
|
||||||
if len(messages) > 0 {
|
if len(messages) > 0 {
|
||||||
_, _, err := db.producerToMongo.SendMessage(ctx, key, &pbmsg.MsgDataToMongoByMQ{LastSeq: lastSeq, ConversationID: conversationID, MsgData: messages})
|
_, _, err := db.producerToMongo.SendMessage(ctx, key, &pbmsg.MsgDataToMongoByMQ{LastSeq: lastSeq, ConversationID: conversationID, MsgData: messages})
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkTypeForBatchInsertBlock(fields []any, key int8, firstSeq int64) error {
|
func (db *commonMsgDatabase) BatchInsertBlock(ctx context.Context, conversationID string, fields []any, key int8, firstSeq int64) error {
|
||||||
|
if len(fields) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
num := db.msg.GetSingleGocMsgNum()
|
||||||
// num = 100
|
// num = 100
|
||||||
for i, field := range fields { // check type
|
for i, field := range fields { // 检查类型
|
||||||
var ok bool
|
var ok bool
|
||||||
switch key {
|
switch key {
|
||||||
case updateKeyMsg:
|
case updateKeyMsg:
|
||||||
@@ -206,106 +202,80 @@ func checkTypeForBatchInsertBlock(fields []any, key int8, firstSeq int64) error
|
|||||||
return errs.ErrInternalServer.Wrap("field type is invalid")
|
return errs.ErrInternalServer.Wrap("field type is invalid")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 返回值为true表示数据库存在该文档,false表示数据库不存在该文档
|
||||||
return nil
|
updateMsgModel := func(seq int64, i int) (bool, error) {
|
||||||
}
|
var (
|
||||||
|
res *mongo.UpdateResult
|
||||||
func (db *commonMsgDatabase) updateMsgModelForBatchInsertBlock(ctx context.Context, conversationID string, fields []any, key int8, seq int64, i int) (bool, error) {
|
err error
|
||||||
var (
|
)
|
||||||
res *mongo.UpdateResult
|
docID := db.msg.GetDocID(conversationID, seq)
|
||||||
err error
|
index := db.msg.GetMsgIndex(seq)
|
||||||
)
|
field := fields[i]
|
||||||
docID := db.msg.GetDocID(conversationID, seq)
|
|
||||||
index := db.msg.GetMsgIndex(seq)
|
|
||||||
field := fields[i]
|
|
||||||
switch key {
|
|
||||||
case updateKeyMsg:
|
|
||||||
res, err = db.msgDocDatabase.UpdateMsg(ctx, docID, index, "msg", field)
|
|
||||||
case updateKeyRevoke:
|
|
||||||
res, err = db.msgDocDatabase.UpdateMsg(ctx, docID, index, "revoke", field)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return false, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.MatchedCount > 0, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *commonMsgDatabase) newDocForBatchInsertBlock(conversationID string, fields []any, key int8, seq, firstSeq, num int64, i int) (unrelationtb.MsgDocModel, int) {
|
|
||||||
doc := unrelationtb.MsgDocModel{
|
|
||||||
DocID: db.msg.GetDocID(conversationID, seq),
|
|
||||||
Msg: make([]*unrelationtb.MsgInfoModel, num),
|
|
||||||
}
|
|
||||||
var insert int // number of inserted
|
|
||||||
for j := i; j < len(fields); j++ {
|
|
||||||
seq = firstSeq + int64(j)
|
|
||||||
if db.msg.GetDocID(conversationID, seq) != doc.DocID {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
insert++
|
|
||||||
switch key {
|
switch key {
|
||||||
case updateKeyMsg:
|
case updateKeyMsg:
|
||||||
doc.Msg[db.msg.GetMsgIndex(seq)] = &unrelationtb.MsgInfoModel{
|
res, err = db.msgDocDatabase.UpdateMsg(ctx, docID, index, "msg", field)
|
||||||
Msg: fields[j].(*unrelationtb.MsgDataModel),
|
|
||||||
}
|
|
||||||
case updateKeyRevoke:
|
case updateKeyRevoke:
|
||||||
doc.Msg[db.msg.GetMsgIndex(seq)] = &unrelationtb.MsgInfoModel{
|
res, err = db.msgDocDatabase.UpdateMsg(ctx, docID, index, "revoke", field)
|
||||||
Revoke: fields[j].(*unrelationtb.RevokeModel),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
if err != nil {
|
||||||
for i, model := range doc.Msg {
|
return false, err
|
||||||
if model == nil {
|
|
||||||
model = &unrelationtb.MsgInfoModel{}
|
|
||||||
doc.Msg[i] = model
|
|
||||||
}
|
}
|
||||||
if model.DelList == nil {
|
return res.MatchedCount > 0, nil
|
||||||
doc.Msg[i].DelList = []string{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return doc, insert
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *commonMsgDatabase) BatchInsertBlock(ctx context.Context, conversationID string, fields []any, key int8, firstSeq int64) error {
|
|
||||||
if len(fields) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
num := db.msg.GetSingleGocMsgNum()
|
|
||||||
// num = 100
|
|
||||||
err := checkTypeForBatchInsertBlock(fields, key, firstSeq)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
tryUpdate := true
|
tryUpdate := true
|
||||||
for i := 0; i < len(fields); i++ {
|
for i := 0; i < len(fields); i++ {
|
||||||
seq := firstSeq + int64(i) // current seq
|
seq := firstSeq + int64(i) // 当前seq
|
||||||
// try update
|
|
||||||
if tryUpdate {
|
if tryUpdate {
|
||||||
matched, err := db.updateMsgModelForBatchInsertBlock(ctx, conversationID, fields, key, seq, i)
|
matched, err := updateMsgModel(seq, i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if matched {
|
if matched {
|
||||||
continue // if matched,skip
|
continue // 匹配到了,继续下一个(不一定修改)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
doc := unrelationtb.MsgDocModel{
|
||||||
|
DocID: db.msg.GetDocID(conversationID, seq),
|
||||||
|
Msg: make([]*unrelationtb.MsgInfoModel, num),
|
||||||
|
}
|
||||||
|
var insert int // 插入的数量
|
||||||
|
for j := i; j < len(fields); j++ {
|
||||||
|
seq = firstSeq + int64(j)
|
||||||
|
if db.msg.GetDocID(conversationID, seq) != doc.DocID {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
insert++
|
||||||
|
switch key {
|
||||||
|
case updateKeyMsg:
|
||||||
|
doc.Msg[db.msg.GetMsgIndex(seq)] = &unrelationtb.MsgInfoModel{
|
||||||
|
Msg: fields[j].(*unrelationtb.MsgDataModel),
|
||||||
|
}
|
||||||
|
case updateKeyRevoke:
|
||||||
|
doc.Msg[db.msg.GetMsgIndex(seq)] = &unrelationtb.MsgInfoModel{
|
||||||
|
Revoke: fields[j].(*unrelationtb.RevokeModel),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i, model := range doc.Msg {
|
||||||
|
if model == nil {
|
||||||
|
model = &unrelationtb.MsgInfoModel{}
|
||||||
|
doc.Msg[i] = model
|
||||||
|
}
|
||||||
|
if model.DelList == nil {
|
||||||
|
doc.Msg[i].DelList = []string{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
doc, insert := db.newDocForBatchInsertBlock(conversationID, fields, key, seq, firstSeq, num, i)
|
|
||||||
// insert doc into db
|
|
||||||
if err := db.msgDocDatabase.Create(ctx, &doc); err != nil {
|
if err := db.msgDocDatabase.Create(ctx, &doc); err != nil {
|
||||||
if mongo.IsDuplicateKeyError(err) {
|
if mongo.IsDuplicateKeyError(err) {
|
||||||
i-- // exists concurrent,
|
i-- // 存在并发,重试当前数据
|
||||||
tryUpdate = true // try update
|
tryUpdate = true // 以修改模式
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
tryUpdate = false // if insert success,change to insert mode
|
tryUpdate = false // 当前以插入成功,下一块优先插入模式
|
||||||
i += insert - 1 // skip inserted data
|
i += insert - 1 // 跳过已插入的数据
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -352,7 +322,6 @@ func (db *commonMsgDatabase) BatchInsertChat2DB(ctx context.Context, conversatio
|
|||||||
Ex: msg.Ex,
|
Ex: msg.Ex,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return db.BatchInsertBlock(ctx, conversationID, msgs, updateKeyMsg, msgList[0].Seq)
|
return db.BatchInsertBlock(ctx, conversationID, msgs, updateKeyMsg, msgList[0].Seq)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -369,11 +338,9 @@ func (db *commonMsgDatabase) MarkSingleChatMsgsAsRead(ctx context.Context, userI
|
|||||||
log.ZDebug(ctx, "MarkSingleChatMsgsAsRead", "userID", userID, "docID", docID, "indexes", indexes)
|
log.ZDebug(ctx, "MarkSingleChatMsgsAsRead", "userID", userID, "docID", docID, "indexes", indexes)
|
||||||
if err := db.msgDocDatabase.MarkSingleChatMsgsAsRead(ctx, userID, docID, indexes); err != nil {
|
if err := db.msgDocDatabase.MarkSingleChatMsgsAsRead(ctx, userID, docID, indexes); err != nil {
|
||||||
log.ZError(ctx, "MarkSingleChatMsgsAsRead", err, "userID", userID, "docID", docID, "indexes", indexes)
|
log.ZError(ctx, "MarkSingleChatMsgsAsRead", err, "userID", userID, "docID", docID, "indexes", indexes)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -387,9 +354,8 @@ func (db *commonMsgDatabase) DelUserDeleteMsgsList(ctx context.Context, conversa
|
|||||||
|
|
||||||
func (db *commonMsgDatabase) BatchInsertChat2Cache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (seq int64, isNew bool, err error) {
|
func (db *commonMsgDatabase) BatchInsertChat2Cache(ctx context.Context, conversationID string, msgs []*sdkws.MsgData) (seq int64, isNew bool, err error) {
|
||||||
currentMaxSeq, err := db.cache.GetMaxSeq(ctx, conversationID)
|
currentMaxSeq, err := db.cache.GetMaxSeq(ctx, conversationID)
|
||||||
if err != nil && !errors.Is(err, redis.Nil) {
|
if err != nil && errs.Unwrap(err) != redis.Nil {
|
||||||
prome.Inc(prome.SeqGetFailedCounter)
|
prome.Inc(prome.SeqGetFailedCounter)
|
||||||
|
|
||||||
return 0, false, err
|
return 0, false, err
|
||||||
}
|
}
|
||||||
prome.Inc(prome.SeqGetSuccessCounter)
|
prome.Inc(prome.SeqGetSuccessCounter)
|
||||||
@@ -400,7 +366,7 @@ func (db *commonMsgDatabase) BatchInsertChat2Cache(ctx context.Context, conversa
|
|||||||
if lenList < 1 {
|
if lenList < 1 {
|
||||||
return 0, false, errors.New("too short as 0")
|
return 0, false, errors.New("too short as 0")
|
||||||
}
|
}
|
||||||
if errors.Is(err, redis.Nil) {
|
if errs.Unwrap(err) == redis.Nil {
|
||||||
isNew = true
|
isNew = true
|
||||||
}
|
}
|
||||||
lastMaxSeq := currentMaxSeq
|
lastMaxSeq := currentMaxSeq
|
||||||
@@ -430,7 +396,6 @@ func (db *commonMsgDatabase) BatchInsertChat2Cache(ctx context.Context, conversa
|
|||||||
} else {
|
} else {
|
||||||
prome.Inc(prome.SeqSetSuccessCounter)
|
prome.Inc(prome.SeqSetSuccessCounter)
|
||||||
}
|
}
|
||||||
|
|
||||||
return lastMaxSeq, isNew, utils.Wrap(err, "")
|
return lastMaxSeq, isNew, utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -445,7 +410,6 @@ func (db *commonMsgDatabase) getMsgBySeqs(ctx context.Context, userID, conversat
|
|||||||
totalMsgs = append(totalMsgs, convert.MsgDB2Pb(msg.Msg))
|
totalMsgs = append(totalMsgs, convert.MsgDB2Pb(msg.Msg))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return totalMsgs, nil
|
return totalMsgs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -456,7 +420,6 @@ func (db *commonMsgDatabase) findMsgInfoBySeq(ctx context.Context, userID, docID
|
|||||||
msg.Msg.IsRead = true
|
msg.Msg.IsRead = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return msgs, err
|
return msgs, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -475,76 +438,16 @@ func (db *commonMsgDatabase) getMsgBySeqsRange(ctx context.Context, userID strin
|
|||||||
seqMsgs = append(seqMsgs, convert.MsgDB2Pb(msg.Msg))
|
seqMsgs = append(seqMsgs, convert.MsgDB2Pb(msg.Msg))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return seqMsgs, nil
|
return seqMsgs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *commonMsgDatabase) getCacheMsgForGetMsgBySeqsRange(ctx context.Context, userID string, conversationID string, begin int64, seqs []int64) ([]*sdkws.MsgData, []int64, error) {
|
|
||||||
newBegin := seqs[0]
|
|
||||||
newEnd := seqs[len(seqs)-1]
|
|
||||||
log.ZDebug(ctx, "GetMsgBySeqsRange", "first seqs", seqs, "newBegin", newBegin, "newEnd", newEnd)
|
|
||||||
cachedMsgs, failedSeqs, err := db.cache.GetMessagesBySeq(ctx, conversationID, seqs)
|
|
||||||
if err != nil {
|
|
||||||
if !errors.Is(err, redis.Nil) {
|
|
||||||
prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs))
|
|
||||||
log.ZError(ctx, "get message from redis exception", err, "conversationID", conversationID, "seqs", seqs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var successMsgs []*sdkws.MsgData
|
|
||||||
if len(cachedMsgs) == 0 {
|
|
||||||
return successMsgs, failedSeqs, err
|
|
||||||
}
|
|
||||||
// if len(cachedMsgs) > 0
|
|
||||||
delSeqs, err2 := db.cache.GetUserDelList(ctx, userID, conversationID)
|
|
||||||
if err2 != nil && !errors.Is(err2, redis.Nil) {
|
|
||||||
return nil, nil, err2
|
|
||||||
}
|
|
||||||
var cacheDelNum int
|
|
||||||
for _, msg := range cachedMsgs {
|
|
||||||
if !utils.Contain(msg.Seq, delSeqs...) {
|
|
||||||
successMsgs = append(successMsgs, msg)
|
|
||||||
} else {
|
|
||||||
cacheDelNum += 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log.ZDebug(ctx, "get delSeqs from redis", "delSeqs", delSeqs, "userID", userID, "conversationID", conversationID, "cacheDelNum", cacheDelNum)
|
|
||||||
var reGetSeqsCache []int64
|
|
||||||
for i := 1; i <= cacheDelNum; {
|
|
||||||
newSeq := newBegin - int64(i)
|
|
||||||
if newSeq >= begin {
|
|
||||||
if !utils.Contain(newSeq, delSeqs...) {
|
|
||||||
log.ZDebug(ctx, "seq del in cache, a new seq in range append", "new seq", newSeq)
|
|
||||||
reGetSeqsCache = append(reGetSeqsCache, newSeq)
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(reGetSeqsCache) > 0 {
|
|
||||||
log.ZDebug(ctx, "reGetSeqsCache", "reGetSeqsCache", reGetSeqsCache)
|
|
||||||
cachedMsgs, failedSeqs2, err2 := db.cache.GetMessagesBySeq(ctx, conversationID, reGetSeqsCache)
|
|
||||||
if err2 != nil {
|
|
||||||
if !errors.Is(err2, redis.Nil) {
|
|
||||||
prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs2))
|
|
||||||
log.ZError(ctx, "get message from redis exception", err2, "conversationID", conversationID, "seqs", reGetSeqsCache)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
failedSeqs = append(failedSeqs, failedSeqs2...)
|
|
||||||
successMsgs = append(successMsgs, cachedMsgs...)
|
|
||||||
}
|
|
||||||
|
|
||||||
return successMsgs, failedSeqs, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID string, conversationID string, begin, end, num, userMaxSeq int64) (int64, int64, []*sdkws.MsgData, error) {
|
func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID string, conversationID string, begin, end, num, userMaxSeq int64) (int64, int64, []*sdkws.MsgData, error) {
|
||||||
// 从缓存中获取最小和最大序列号,并根据给定的范围值进行调整
|
|
||||||
userMinSeq, err := db.cache.GetConversationUserMinSeq(ctx, conversationID, userID)
|
userMinSeq, err := db.cache.GetConversationUserMinSeq(ctx, conversationID, userID)
|
||||||
if err != nil && !errors.Is(err, redis.Nil) {
|
if err != nil && errs.Unwrap(err) != redis.Nil {
|
||||||
return 0, 0, nil, err
|
return 0, 0, nil, err
|
||||||
}
|
}
|
||||||
minSeq, err := db.cache.GetMinSeq(ctx, conversationID)
|
minSeq, err := db.cache.GetMinSeq(ctx, conversationID)
|
||||||
if err != nil && !errors.Is(err, redis.Nil) {
|
if err != nil && errs.Unwrap(err) != redis.Nil {
|
||||||
return 0, 0, nil, err
|
return 0, 0, nil, err
|
||||||
}
|
}
|
||||||
if userMinSeq > minSeq {
|
if userMinSeq > minSeq {
|
||||||
@@ -552,25 +455,18 @@ func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID strin
|
|||||||
}
|
}
|
||||||
if minSeq > end {
|
if minSeq > end {
|
||||||
log.ZInfo(ctx, "minSeq > end", "minSeq", minSeq, "end", end)
|
log.ZInfo(ctx, "minSeq > end", "minSeq", minSeq, "end", end)
|
||||||
|
|
||||||
return 0, 0, nil, nil
|
return 0, 0, nil, nil
|
||||||
}
|
}
|
||||||
maxSeq, err := db.cache.GetMaxSeq(ctx, conversationID)
|
maxSeq, err := db.cache.GetMaxSeq(ctx, conversationID)
|
||||||
if err != nil && !errors.Is(err, redis.Nil) {
|
if err != nil && errs.Unwrap(err) != redis.Nil {
|
||||||
return 0, 0, nil, err
|
return 0, 0, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// log out debug info
|
|
||||||
log.ZDebug(ctx, "GetMsgBySeqsRange", "userMinSeq", userMinSeq, "conMinSeq", minSeq, "conMaxSeq", maxSeq, "userMaxSeq", userMaxSeq)
|
log.ZDebug(ctx, "GetMsgBySeqsRange", "userMinSeq", userMinSeq, "conMinSeq", minSeq, "conMaxSeq", maxSeq, "userMaxSeq", userMaxSeq)
|
||||||
|
|
||||||
// adjust maxSeq according to userMaxSeq
|
|
||||||
if userMaxSeq != 0 {
|
if userMaxSeq != 0 {
|
||||||
if userMaxSeq < maxSeq {
|
if userMaxSeq < maxSeq {
|
||||||
maxSeq = userMaxSeq
|
maxSeq = userMaxSeq
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// adjust begin and end according to minSeq and maxSeq
|
|
||||||
if begin < minSeq {
|
if begin < minSeq {
|
||||||
begin = minSeq
|
begin = minSeq
|
||||||
}
|
}
|
||||||
@@ -580,8 +476,6 @@ func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID strin
|
|||||||
if end < begin {
|
if end < begin {
|
||||||
return 0, 0, nil, errs.ErrArgs.Wrap("seq end < begin")
|
return 0, 0, nil, errs.ErrArgs.Wrap("seq end < begin")
|
||||||
}
|
}
|
||||||
|
|
||||||
// get seqs to search
|
|
||||||
var seqs []int64
|
var seqs []int64
|
||||||
for i := end; i > end-num; i-- {
|
for i := end; i > end-num; i-- {
|
||||||
if i >= begin {
|
if i >= begin {
|
||||||
@@ -593,24 +487,67 @@ func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID strin
|
|||||||
if len(seqs) == 0 {
|
if len(seqs) == 0 {
|
||||||
return 0, 0, nil, nil
|
return 0, 0, nil, nil
|
||||||
}
|
}
|
||||||
|
newBegin := seqs[0]
|
||||||
// get info from cache,and filter deleted msg
|
newEnd := seqs[len(seqs)-1]
|
||||||
successMsgs, failedSeqs, err := db.getCacheMsgForGetMsgBySeqsRange(ctx, userID, conversationID, begin, seqs)
|
log.ZDebug(ctx, "GetMsgBySeqsRange", "first seqs", seqs, "newBegin", newBegin, "newEnd", newEnd)
|
||||||
|
cachedMsgs, failedSeqs, err := db.cache.GetMessagesBySeq(ctx, conversationID, seqs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, nil, err
|
if err != redis.Nil {
|
||||||
|
prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs))
|
||||||
|
log.ZError(ctx, "get message from redis exception", err, "conversationID", conversationID, "seqs", seqs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var successMsgs []*sdkws.MsgData
|
||||||
|
if len(cachedMsgs) > 0 {
|
||||||
|
delSeqs, err := db.cache.GetUserDelList(ctx, userID, conversationID)
|
||||||
|
if err != nil && errs.Unwrap(err) != redis.Nil {
|
||||||
|
return 0, 0, nil, err
|
||||||
|
}
|
||||||
|
var cacheDelNum int
|
||||||
|
for _, msg := range cachedMsgs {
|
||||||
|
if !utils.Contain(msg.Seq, delSeqs...) {
|
||||||
|
successMsgs = append(successMsgs, msg)
|
||||||
|
} else {
|
||||||
|
cacheDelNum += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.ZDebug(ctx, "get delSeqs from redis", "delSeqs", delSeqs, "userID", userID, "conversationID", conversationID, "cacheDelNum", cacheDelNum)
|
||||||
|
var reGetSeqsCache []int64
|
||||||
|
for i := 1; i <= cacheDelNum; {
|
||||||
|
newSeq := newBegin - int64(i)
|
||||||
|
if newSeq >= begin {
|
||||||
|
if !utils.Contain(newSeq, delSeqs...) {
|
||||||
|
log.ZDebug(ctx, "seq del in cache, a new seq in range append", "new seq", newSeq)
|
||||||
|
reGetSeqsCache = append(reGetSeqsCache, newSeq)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(reGetSeqsCache) > 0 {
|
||||||
|
log.ZDebug(ctx, "reGetSeqsCache", "reGetSeqsCache", reGetSeqsCache)
|
||||||
|
cachedMsgs, failedSeqs2, err := db.cache.GetMessagesBySeq(ctx, conversationID, reGetSeqsCache)
|
||||||
|
if err != nil {
|
||||||
|
if err != redis.Nil {
|
||||||
|
prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs2))
|
||||||
|
log.ZError(ctx, "get message from redis exception", err, "conversationID", conversationID, "seqs", reGetSeqsCache)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
failedSeqs = append(failedSeqs, failedSeqs2...)
|
||||||
|
successMsgs = append(successMsgs, cachedMsgs...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// log out debug info
|
|
||||||
log.ZDebug(ctx, "get msgs from cache", "successMsgs", successMsgs)
|
log.ZDebug(ctx, "get msgs from cache", "successMsgs", successMsgs)
|
||||||
if len(failedSeqs) != 0 {
|
if len(failedSeqs) != 0 {
|
||||||
log.ZDebug(ctx, "msgs not exist in redis", "seqs", failedSeqs)
|
log.ZDebug(ctx, "msgs not exist in redis", "seqs", failedSeqs)
|
||||||
}
|
}
|
||||||
// if not found in cache,find in mongo
|
// get from cache or db
|
||||||
prome.Add(prome.MsgPullFromRedisSuccessCounter, len(successMsgs))
|
prome.Add(prome.MsgPullFromRedisSuccessCounter, len(successMsgs))
|
||||||
if len(failedSeqs) > 0 {
|
if len(failedSeqs) > 0 {
|
||||||
mongoMsgs, err := db.getMsgBySeqsRange(ctx, userID, conversationID, failedSeqs, begin, end)
|
mongoMsgs, err := db.getMsgBySeqsRange(ctx, userID, conversationID, failedSeqs, begin, end)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
prome.Add(prome.MsgPullFromMongoFailedCounter, len(failedSeqs))
|
prome.Add(prome.MsgPullFromMongoFailedCounter, len(failedSeqs))
|
||||||
|
|
||||||
return 0, 0, nil, err
|
return 0, 0, nil, err
|
||||||
}
|
}
|
||||||
prome.Add(prome.MsgPullFromMongoSuccessCounter, len(mongoMsgs))
|
prome.Add(prome.MsgPullFromMongoSuccessCounter, len(mongoMsgs))
|
||||||
@@ -622,15 +559,15 @@ func (db *commonMsgDatabase) GetMsgBySeqsRange(ctx context.Context, userID strin
|
|||||||
|
|
||||||
func (db *commonMsgDatabase) GetMsgBySeqs(ctx context.Context, userID string, conversationID string, seqs []int64) (int64, int64, []*sdkws.MsgData, error) {
|
func (db *commonMsgDatabase) GetMsgBySeqs(ctx context.Context, userID string, conversationID string, seqs []int64) (int64, int64, []*sdkws.MsgData, error) {
|
||||||
userMinSeq, err := db.cache.GetConversationUserMinSeq(ctx, conversationID, userID)
|
userMinSeq, err := db.cache.GetConversationUserMinSeq(ctx, conversationID, userID)
|
||||||
if err != nil && !errors.Is(err, redis.Nil) {
|
if err != nil && errs.Unwrap(err) != redis.Nil {
|
||||||
return 0, 0, nil, err
|
return 0, 0, nil, err
|
||||||
}
|
}
|
||||||
minSeq, err := db.cache.GetMinSeq(ctx, conversationID)
|
minSeq, err := db.cache.GetMinSeq(ctx, conversationID)
|
||||||
if err != nil && !errors.Is(err, redis.Nil) {
|
if err != nil && errs.Unwrap(err) != redis.Nil {
|
||||||
return 0, 0, nil, err
|
return 0, 0, nil, err
|
||||||
}
|
}
|
||||||
maxSeq, err := db.cache.GetMaxSeq(ctx, conversationID)
|
maxSeq, err := db.cache.GetMaxSeq(ctx, conversationID)
|
||||||
if err != nil && !errors.Is(err, redis.Nil) {
|
if err != nil && errs.Unwrap(err) != redis.Nil {
|
||||||
return 0, 0, nil, err
|
return 0, 0, nil, err
|
||||||
}
|
}
|
||||||
if userMinSeq < minSeq {
|
if userMinSeq < minSeq {
|
||||||
@@ -644,7 +581,7 @@ func (db *commonMsgDatabase) GetMsgBySeqs(ctx context.Context, userID string, co
|
|||||||
}
|
}
|
||||||
successMsgs, failedSeqs, err := db.cache.GetMessagesBySeq(ctx, conversationID, newSeqs)
|
successMsgs, failedSeqs, err := db.cache.GetMessagesBySeq(ctx, conversationID, newSeqs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !errors.Is(err, redis.Nil) {
|
if err != redis.Nil {
|
||||||
prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs))
|
prome.Add(prome.MsgPullFromRedisFailedCounter, len(failedSeqs))
|
||||||
log.ZError(ctx, "get message from redis exception", err, "failedSeqs", failedSeqs, "conversationID", conversationID)
|
log.ZError(ctx, "get message from redis exception", err, "failedSeqs", failedSeqs, "conversationID", conversationID)
|
||||||
}
|
}
|
||||||
@@ -670,13 +607,11 @@ func (db *commonMsgDatabase) GetMsgBySeqs(ctx context.Context, userID string, co
|
|||||||
mongoMsgs, err := db.getMsgBySeqs(ctx, userID, conversationID, failedSeqs)
|
mongoMsgs, err := db.getMsgBySeqs(ctx, userID, conversationID, failedSeqs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
prome.Add(prome.MsgPullFromMongoFailedCounter, len(failedSeqs))
|
prome.Add(prome.MsgPullFromMongoFailedCounter, len(failedSeqs))
|
||||||
|
|
||||||
return 0, 0, nil, err
|
return 0, 0, nil, err
|
||||||
}
|
}
|
||||||
prome.Add(prome.MsgPullFromMongoSuccessCounter, len(mongoMsgs))
|
prome.Add(prome.MsgPullFromMongoSuccessCounter, len(mongoMsgs))
|
||||||
successMsgs = append(successMsgs, mongoMsgs...)
|
successMsgs = append(successMsgs, mongoMsgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return minSeq, maxSeq, successMsgs, nil
|
return minSeq, maxSeq, successMsgs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -697,81 +632,61 @@ func (db *commonMsgDatabase) DeleteConversationMsgsAndSetMinSeq(ctx context.Cont
|
|||||||
log.ZWarn(ctx, "CleanUpOneUserAllMsg", err, "conversationID", conversationID)
|
log.ZWarn(ctx, "CleanUpOneUserAllMsg", err, "conversationID", conversationID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return db.cache.SetMinSeq(ctx, conversationID, minSeq)
|
return db.cache.SetMinSeq(ctx, conversationID, minSeq)
|
||||||
}
|
}
|
||||||
|
|
||||||
func processMsgDocModel(
|
func (db *commonMsgDatabase) UserMsgsDestruct(ctx context.Context, userID string, conversationID string, destructTime int64, lastMsgDestructTime time.Time) (seqs []int64, err error) {
|
||||||
ctx context.Context,
|
|
||||||
msgDocModel *unrelationtb.MsgDocModel,
|
|
||||||
userID, conversationID string,
|
|
||||||
index int64,
|
|
||||||
destructTime int64,
|
|
||||||
lastMsgDestructTime time.Time,
|
|
||||||
) (seqs []int64, over bool) {
|
|
||||||
if len(msgDocModel.Msg) > 0 {
|
|
||||||
i := 0
|
|
||||||
for _, msg := range msgDocModel.Msg {
|
|
||||||
i++
|
|
||||||
if msg != nil && msg.Msg != nil && msg.Msg.SendTime+destructTime*1000 <= time.Now().UnixMilli() {
|
|
||||||
if msg.Msg.SendTime+destructTime*1000 > lastMsgDestructTime.UnixMilli() && !utils.Contain(userID, msg.DelList...) {
|
|
||||||
seqs = append(seqs, msg.Msg.Seq)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.ZDebug(ctx, "all msg need destruct is found", "conversationID", conversationID, "userID", userID, "index", index, "stop index", i)
|
|
||||||
over = true
|
|
||||||
|
|
||||||
return seqs, over
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return seqs, over
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *commonMsgDatabase) UserMsgsDestruct(ctx context.Context, userID, conversationID string, destructTime int64, lastMsgDestructTime time.Time) (seqs []int64, err error) {
|
|
||||||
var index int64
|
var index int64
|
||||||
|
|
||||||
// refresh msg list
|
|
||||||
for {
|
for {
|
||||||
// from oldest to newest
|
// from oldest 2 newest
|
||||||
msgDocModel, err2 := db.msgDocDatabase.GetMsgDocModelByIndex(ctx, conversationID, index, 1)
|
msgDocModel, err := db.msgDocDatabase.GetMsgDocModelByIndex(ctx, conversationID, index, 1)
|
||||||
if err2 != nil || msgDocModel.DocID == "" {
|
if err != nil || msgDocModel.DocID == "" {
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
if errors.Is(err2, unrelation.ErrMsgListNotExist) {
|
if err == unrelation.ErrMsgListNotExist {
|
||||||
log.ZDebug(ctx, "not doc find", "conversationID", conversationID, "userID", userID, "index", index)
|
log.ZDebug(ctx, "not doc find", "conversationID", conversationID, "userID", userID, "index", index)
|
||||||
} else {
|
} else {
|
||||||
log.ZError(ctx, "deleteMsgRecursion GetUserMsgListByIndex failed", err2, "conversationID", conversationID, "index", index)
|
log.ZError(ctx, "deleteMsgRecursion GetUserMsgListByIndex failed", err, "conversationID", conversationID, "index", index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If there is an error or no message document is found, delete the message physically and return the sequence number, then end the recursion.
|
// 获取报错,或者获取不到了,物理删除并且返回seq delMongoMsgsPhysical(delStruct.delDocIDList), 结束递归
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
index++
|
index++
|
||||||
//&& msgDocModel.Msg[0].Msg.SendTime > lastMsgDestructTime.UnixMilli()
|
//&& msgDocModel.Msg[0].Msg.SendTime > lastMsgDestructTime.UnixMilli()
|
||||||
curSeqs, over := processMsgDocModel(ctx, msgDocModel, userID, conversationID, index, destructTime, lastMsgDestructTime)
|
if len(msgDocModel.Msg) > 0 {
|
||||||
seqs = append(seqs, curSeqs...)
|
i := 0
|
||||||
if over {
|
var over bool
|
||||||
break
|
for _, msg := range msgDocModel.Msg {
|
||||||
}
|
i++
|
||||||
}
|
if msg != nil && msg.Msg != nil && msg.Msg.SendTime+destructTime*1000 <= time.Now().UnixMilli() {
|
||||||
// Log the result of the function call.
|
if msg.Msg.SendTime+destructTime*1000 > lastMsgDestructTime.UnixMilli() && !utils.Contain(userID, msg.DelList...) {
|
||||||
log.ZDebug(ctx, "UserMsgsDestruct", "conversationID", conversationID, "userID", userID, "seqs", seqs)
|
seqs = append(seqs, msg.Msg.Seq)
|
||||||
if len(seqs) == 0 {
|
}
|
||||||
return seqs, nil
|
} else {
|
||||||
}
|
log.ZDebug(ctx, "all msg need destruct is found", "conversationID", conversationID, "userID", userID, "index", index, "stop index", i)
|
||||||
// if len(seqs) > 0
|
over = true
|
||||||
userMinSeq := seqs[len(seqs)-1] + 1
|
break
|
||||||
currentUserMinSeq, err := db.cache.GetConversationUserMinSeq(ctx, conversationID, userID)
|
}
|
||||||
if err != nil && !errors.Is(err, redis.Nil) {
|
}
|
||||||
return nil, err
|
if over {
|
||||||
}
|
break
|
||||||
if currentUserMinSeq < userMinSeq {
|
}
|
||||||
if err := db.cache.SetConversationUserMinSeq(ctx, conversationID, userID, userMinSeq); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.ZDebug(ctx, "UserMsgsDestruct", "conversationID", conversationID, "userID", userID, "seqs", seqs)
|
||||||
|
if len(seqs) > 0 {
|
||||||
|
userMinSeq := seqs[len(seqs)-1] + 1
|
||||||
|
currentUserMinSeq, err := db.cache.GetConversationUserMinSeq(ctx, conversationID, userID)
|
||||||
|
if err != nil && errs.Unwrap(err) != redis.Nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if currentUserMinSeq < userMinSeq {
|
||||||
|
if err := db.cache.SetConversationUserMinSeq(ctx, conversationID, userID, userMinSeq); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return seqs, nil
|
return seqs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -794,65 +709,45 @@ func (db *commonMsgDatabase) deleteMsgRecursion(ctx context.Context, conversatio
|
|||||||
msgDocModel, err := db.msgDocDatabase.GetMsgDocModelByIndex(ctx, conversationID, index, 1)
|
msgDocModel, err := db.msgDocDatabase.GetMsgDocModelByIndex(ctx, conversationID, index, 1)
|
||||||
if err != nil || msgDocModel.DocID == "" {
|
if err != nil || msgDocModel.DocID == "" {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, unrelation.ErrMsgListNotExist) {
|
if err == unrelation.ErrMsgListNotExist {
|
||||||
log.ZDebug(ctx, "deleteMsgRecursion ErrMsgListNotExist", "conversationID", conversationID, "index:", index)
|
log.ZDebug(ctx, "deleteMsgRecursion ErrMsgListNotExist", "conversationID", conversationID, "index:", index)
|
||||||
} else {
|
} else {
|
||||||
log.ZError(ctx, "deleteMsgRecursion GetUserMsgListByIndex failed", err, "conversationID", conversationID, "index", index)
|
log.ZError(ctx, "deleteMsgRecursion GetUserMsgListByIndex failed", err, "conversationID", conversationID, "index", index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// get error or miss content, delete physically and return minSeq,delMongoMsgsPhysical(delStruct.delDocIDList), end recursion
|
// 获取报错,或者获取不到了,物理删除并且返回seq delMongoMsgsPhysical(delStruct.delDocIDList), 结束递归
|
||||||
err = db.msgDocDatabase.DeleteDocs(ctx, delStruct.delDocIDs)
|
err = db.msgDocDatabase.DeleteDocs(ctx, delStruct.delDocIDs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return delStruct.getSetMinSeq() + 1, nil
|
return delStruct.getSetMinSeq() + 1, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.ZDebug(ctx, "doc info", "conversationID", conversationID, "index", index, "docID", msgDocModel.DocID, "len", len(msgDocModel.Msg))
|
log.ZDebug(ctx, "doc info", "conversationID", conversationID, "index", index, "docID", msgDocModel.DocID, "len", len(msgDocModel.Msg))
|
||||||
if int64(len(msgDocModel.Msg)) > db.msg.GetSingleGocMsgNum() {
|
if int64(len(msgDocModel.Msg)) > db.msg.GetSingleGocMsgNum() {
|
||||||
log.ZWarn(ctx, "msgs too large", nil, "lenth", len(msgDocModel.Msg), "docID:", msgDocModel.DocID)
|
log.ZWarn(ctx, "msgs too large", nil, "lenth", len(msgDocModel.Msg), "docID:", msgDocModel.DocID)
|
||||||
}
|
}
|
||||||
fullAndExpired := msgDocModel.IsFull() && msgDocModel.Msg[len(msgDocModel.Msg)-1].Msg.SendTime+(remainTime*1000) < utils.GetCurrentTimestampByMill()
|
if msgDocModel.IsFull() && msgDocModel.Msg[len(msgDocModel.Msg)-1].Msg.SendTime+(remainTime*1000) < utils.GetCurrentTimestampByMill() {
|
||||||
if fullAndExpired {
|
log.ZDebug(ctx, "doc is full and all msg is expired", "docID", msgDocModel.DocID)
|
||||||
handleFullAndExpiredForDeleteMsgRecursion(ctx, msgDocModel, delStruct)
|
delStruct.delDocIDs = append(delStruct.delDocIDs, msgDocModel.DocID)
|
||||||
|
delStruct.minSeq = msgDocModel.Msg[len(msgDocModel.Msg)-1].Msg.Seq
|
||||||
} else {
|
} else {
|
||||||
handleNotFullAndExpiredForDeleteMsgRecursion(ctx, msgDocModel, remainTime, index, conversationID, delStruct, db)
|
var delMsgIndexs []int
|
||||||
}
|
for i, MsgInfoModel := range msgDocModel.Msg {
|
||||||
seq, err := db.deleteMsgRecursion(ctx, conversationID, index+1, delStruct, remainTime)
|
if MsgInfoModel != nil && MsgInfoModel.Msg != nil {
|
||||||
|
if utils.GetCurrentTimestampByMill() > MsgInfoModel.Msg.SendTime+(remainTime*1000) {
|
||||||
return seq, err
|
delMsgIndexs = append(delMsgIndexs, i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleFullAndExpiredForDeleteMsgRecursion(ctx context.Context, msgDocModel *unrelationtb.MsgDocModel, delStruct *delMsgRecursionStruct) {
|
|
||||||
log.ZDebug(ctx, "doc is full and all msg is expired", "docID", msgDocModel.DocID)
|
|
||||||
delStruct.delDocIDs = append(delStruct.delDocIDs, msgDocModel.DocID)
|
|
||||||
delStruct.minSeq = msgDocModel.Msg[len(msgDocModel.Msg)-1].Msg.Seq
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleNotFullAndExpiredForDeleteMsgRecursion(
|
|
||||||
ctx context.Context,
|
|
||||||
msgDocModel *unrelationtb.MsgDocModel,
|
|
||||||
remainTime, index int64,
|
|
||||||
conversationID string,
|
|
||||||
delStruct *delMsgRecursionStruct,
|
|
||||||
db *commonMsgDatabase,
|
|
||||||
) {
|
|
||||||
var delMsgIndexs []int
|
|
||||||
for i, MsgInfoModel := range msgDocModel.Msg {
|
|
||||||
if MsgInfoModel != nil && MsgInfoModel.Msg != nil {
|
|
||||||
if utils.GetCurrentTimestampByMill() > MsgInfoModel.Msg.SendTime+(remainTime*1000) {
|
|
||||||
delMsgIndexs = append(delMsgIndexs, i)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
if len(delMsgIndexs) > 0 {
|
||||||
if len(delMsgIndexs) > 0 {
|
if err := db.msgDocDatabase.DeleteMsgsInOneDocByIndex(ctx, msgDocModel.DocID, delMsgIndexs); err != nil {
|
||||||
err2 := db.msgDocDatabase.DeleteMsgsInOneDocByIndex(ctx, msgDocModel.DocID, delMsgIndexs)
|
log.ZError(ctx, "deleteMsgRecursion DeleteMsgsInOneDocByIndex failed", err, "conversationID", conversationID, "index", index)
|
||||||
if err2 != nil {
|
}
|
||||||
log.ZError(ctx, "deleteMsgRecursion DeleteMsgsInOneDocByIndex failed", err2, "conversationID", conversationID, "index", index)
|
delStruct.minSeq = int64(msgDocModel.Msg[delMsgIndexs[len(delMsgIndexs)-1]].Msg.Seq)
|
||||||
}
|
}
|
||||||
delStruct.minSeq = msgDocModel.Msg[delMsgIndexs[len(delMsgIndexs)-1]].Msg.Seq
|
|
||||||
}
|
}
|
||||||
|
seq, err := db.deleteMsgRecursion(ctx, conversationID, index+1, delStruct, remainTime)
|
||||||
|
return seq, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *commonMsgDatabase) DeleteMsgsPhysicalBySeqs(ctx context.Context, conversationID string, allSeqs []int64) error {
|
func (db *commonMsgDatabase) DeleteMsgsPhysicalBySeqs(ctx context.Context, conversationID string, allSeqs []int64) error {
|
||||||
@@ -868,15 +763,13 @@ func (db *commonMsgDatabase) DeleteMsgsPhysicalBySeqs(ctx context.Context, conve
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *commonMsgDatabase) DeleteUserMsgsBySeqs(ctx context.Context, userID string, conversationID string, seqs []int64) error {
|
func (db *commonMsgDatabase) DeleteUserMsgsBySeqs(ctx context.Context, userID string, conversationID string, seqs []int64) error {
|
||||||
cachedMsgs, _, err := db.cache.GetMessagesBySeq(ctx, conversationID, seqs)
|
cachedMsgs, _, err := db.cache.GetMessagesBySeq(ctx, conversationID, seqs)
|
||||||
if err != nil && errors.Is(err, redis.Nil) {
|
if err != nil && errs.Unwrap(err) != redis.Nil {
|
||||||
log.ZWarn(ctx, "DeleteUserMsgsBySeqs", err, "conversationID", conversationID, "seqs", seqs)
|
log.ZWarn(ctx, "DeleteUserMsgsBySeqs", err, "conversationID", conversationID, "seqs", seqs)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(cachedMsgs) > 0 {
|
if len(cachedMsgs) > 0 {
|
||||||
@@ -896,7 +789,6 @@ func (db *commonMsgDatabase) DeleteUserMsgsBySeqs(ctx context.Context, userID st
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -908,12 +800,11 @@ func (db *commonMsgDatabase) CleanUpUserConversationsMsgs(ctx context.Context, u
|
|||||||
for _, conversationID := range conversationIDs {
|
for _, conversationID := range conversationIDs {
|
||||||
maxSeq, err := db.cache.GetMaxSeq(ctx, conversationID)
|
maxSeq, err := db.cache.GetMaxSeq(ctx, conversationID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, redis.Nil) {
|
if err == redis.Nil {
|
||||||
log.ZInfo(ctx, "max seq is nil", "conversationID", conversationID)
|
log.ZInfo(ctx, "max seq is nil", "conversationID", conversationID)
|
||||||
} else {
|
} else {
|
||||||
log.ZError(ctx, "get max seq failed", err, "conversationID", conversationID)
|
log.ZError(ctx, "get max seq failed", err, "conversationID", conversationID)
|
||||||
}
|
}
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err := db.cache.SetMinSeq(ctx, conversationID, maxSeq+1); err != nil {
|
if err := db.cache.SetMinSeq(ctx, conversationID, maxSeq+1); err != nil {
|
||||||
@@ -1007,7 +898,6 @@ func (db *commonMsgDatabase) GetConversationMinMaxSeqInMongoAndCache(ctx context
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1026,7 +916,6 @@ func (db *commonMsgDatabase) GetMinMaxSeqMongo(ctx context.Context, conversation
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
maxSeqMongo = newestMsgMongo.Msg.Seq
|
maxSeqMongo = newestMsgMongo.Msg.Seq
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1054,7 +943,7 @@ func (db *commonMsgDatabase) RangeGroupSendCount(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db *commonMsgDatabase) SearchMessage(ctx context.Context, req *pbmsg.SearchMessageReq) (total int32, msgData []*sdkws.MsgData, err error) {
|
func (db *commonMsgDatabase) SearchMessage(ctx context.Context, req *pbmsg.SearchMessageReq) (total int32, msgData []*sdkws.MsgData, err error) {
|
||||||
totalMsgs := make([]*sdkws.MsgData, 0)
|
var totalMsgs []*sdkws.MsgData
|
||||||
total, msgs, err := db.msgDocDatabase.SearchMessage(ctx, req)
|
total, msgs, err := db.msgDocDatabase.SearchMessage(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil, err
|
return 0, nil, err
|
||||||
@@ -1065,7 +954,6 @@ func (db *commonMsgDatabase) SearchMessage(ctx context.Context, req *pbmsg.Searc
|
|||||||
}
|
}
|
||||||
totalMsgs = append(totalMsgs, convert.MsgDB2Pb(msg.Msg))
|
totalMsgs = append(totalMsgs, convert.MsgDB2Pb(msg.Msg))
|
||||||
}
|
}
|
||||||
|
|
||||||
return total, totalMsgs, nil
|
return total, totalMsgs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -162,7 +162,6 @@ func GetDB() *commonMsgDatabase {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &commonMsgDatabase{
|
return &commonMsgDatabase{
|
||||||
msgDocDatabase: unrelation.NewMsgMongoDriver(mongo.GetDatabase()),
|
msgDocDatabase: unrelation.NewMsgMongoDriver(mongo.GetDatabase()),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,6 +89,5 @@ func (s *s3Database) AccessURL(ctx context.Context, name string, expire time.Dur
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return time.Time{}, "", err
|
return time.Time{}, "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return expireTime, rawURL, nil
|
return expireTime, rawURL, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,7 +90,6 @@ func (u *userDatabase) InitOnce(ctx context.Context, users []*relation.UserModel
|
|||||||
if len(miss) > 0 {
|
if len(miss) > 0 {
|
||||||
_ = u.userDB.Create(ctx, miss)
|
_ = u.userDB.Create(ctx, miss)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,35 +102,30 @@ func (u *userDatabase) FindWithError(ctx context.Context, userIDs []string) (use
|
|||||||
if len(users) != len(userIDs) {
|
if len(users) != len(userIDs) {
|
||||||
err = errs.ErrRecordNotFound.Wrap("userID not found")
|
err = errs.ErrRecordNotFound.Wrap("userID not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find Get the information of the specified user. If the userID is not found, no error will be returned.
|
// Find Get the information of the specified user. If the userID is not found, no error will be returned.
|
||||||
func (u *userDatabase) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
|
func (u *userDatabase) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
|
||||||
users, err = u.cache.GetUsersInfo(ctx, userIDs)
|
users, err = u.cache.GetUsersInfo(ctx, userIDs)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create Insert multiple external guarantees that the userID is not repeated and does not exist in the db.
|
// Create Insert multiple external guarantees that the userID is not repeated and does not exist in the db.
|
||||||
func (u *userDatabase) Create(ctx context.Context, users []*relation.UserModel) (err error) {
|
func (u *userDatabase) Create(ctx context.Context, users []*relation.UserModel) (err error) {
|
||||||
err = u.tx.Transaction(func(tx any) error {
|
if err := u.tx.Transaction(func(tx any) error {
|
||||||
err = u.userDB.Create(ctx, users)
|
err = u.userDB.Create(ctx, users)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
}); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
userIDs := make([]string, 0, len(users))
|
var userIDs []string
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
userIDs = append(userIDs, user.UserID)
|
userIDs = append(userIDs, user.UserID)
|
||||||
}
|
}
|
||||||
|
|
||||||
return u.cache.DelUsersInfo(userIDs...).ExecDel(ctx)
|
return u.cache.DelUsersInfo(userIDs...).ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,7 +134,6 @@ func (u *userDatabase) Update(ctx context.Context, user *relation.UserModel) (er
|
|||||||
if err := u.userDB.Update(ctx, user); err != nil {
|
if err := u.userDB.Update(ctx, user); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return u.cache.DelUsersInfo(user.UserID).ExecDel(ctx)
|
return u.cache.DelUsersInfo(user.UserID).ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -149,7 +142,6 @@ func (u *userDatabase) UpdateByMap(ctx context.Context, userID string, args map[
|
|||||||
if err := u.userDB.UpdateByMap(ctx, userID, args); err != nil {
|
if err := u.userDB.UpdateByMap(ctx, userID, args); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return u.cache.DelUsersInfo(userID).ExecDel(ctx)
|
return u.cache.DelUsersInfo(userID).ExecDel(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,7 +162,6 @@ func (u *userDatabase) IsExist(ctx context.Context, userIDs []string) (exist boo
|
|||||||
if len(users) > 0 {
|
if len(users) > 0 {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,14 +183,12 @@ func (u *userDatabase) CountRangeEverydayTotal(ctx context.Context, start time.T
|
|||||||
// SubscribeUsersStatus Subscribe or unsubscribe a user's presence status.
|
// SubscribeUsersStatus Subscribe or unsubscribe a user's presence status.
|
||||||
func (u *userDatabase) SubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error {
|
func (u *userDatabase) SubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error {
|
||||||
err := u.mongoDB.AddSubscriptionList(ctx, userID, userIDs)
|
err := u.mongoDB.AddSubscriptionList(ctx, userID, userIDs)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnsubscribeUsersStatus unsubscribe a user's presence status.
|
// UnsubscribeUsersStatus unsubscribe a user's presence status.
|
||||||
func (u *userDatabase) UnsubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error {
|
func (u *userDatabase) UnsubscribeUsersStatus(ctx context.Context, userID string, userIDs []string) error {
|
||||||
err := u.mongoDB.UnsubscriptionList(ctx, userID, userIDs)
|
err := u.mongoDB.UnsubscriptionList(ctx, userID, userIDs)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,7 +198,6 @@ func (u *userDatabase) GetAllSubscribeList(ctx context.Context, userID string) (
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,14 +207,12 @@ func (u *userDatabase) GetSubscribedList(ctx context.Context, userID string) ([]
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return list, nil
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserStatus get user status.
|
// GetUserStatus get user status.
|
||||||
func (u *userDatabase) GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error) {
|
func (u *userDatabase) GetUserStatus(ctx context.Context, userIDs []string) ([]*user.OnlineStatus, error) {
|
||||||
onlineStatusList, err := u.cache.GetUserStatus(ctx, userIDs)
|
onlineStatusList, err := u.cache.GetUserStatus(ctx, userIDs)
|
||||||
|
|
||||||
return onlineStatusList, err
|
return onlineStatusList, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -50,7 +50,6 @@ func (g *ConversationLocalCache) GetRecvMsgNotNotifyUserIDs(ctx context.Context,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return resp.UserIDs, nil
|
return resp.UserIDs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,7 +57,6 @@ func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string)
|
|||||||
localHashInfo, ok := g.cache[groupID]
|
localHashInfo, ok := g.cache[groupID]
|
||||||
if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash {
|
if ok && localHashInfo.memberListHash == resp.GroupAbstractInfos[0].GroupMemberListHash {
|
||||||
g.lock.Unlock()
|
g.lock.Unlock()
|
||||||
|
|
||||||
return localHashInfo.userIDs, nil
|
return localHashInfo.userIDs, nil
|
||||||
}
|
}
|
||||||
g.lock.Unlock()
|
g.lock.Unlock()
|
||||||
@@ -75,6 +74,5 @@ func (g *GroupLocalCache) GetGroupMemberIDs(ctx context.Context, groupID string)
|
|||||||
memberListHash: resp.GroupAbstractInfos[0].GroupMemberListHash,
|
memberListHash: resp.GroupAbstractInfos[0].GroupMemberListHash,
|
||||||
userIDs: groupMembersResp.UserIDs,
|
userIDs: groupMembersResp.UserIDs,
|
||||||
}
|
}
|
||||||
|
|
||||||
return g.cache[groupID].userIDs, nil
|
return g.cache[groupID].userIDs, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,11 +63,10 @@ func (b *BlackGorm) Find(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
blacks []*relation.BlackModel,
|
blacks []*relation.BlackModel,
|
||||||
) (blackList []*relation.BlackModel, err error) {
|
) (blackList []*relation.BlackModel, err error) {
|
||||||
where := make([][]interface{}, 0, len(blacks))
|
var where [][]interface{}
|
||||||
for _, black := range blacks {
|
for _, black := range blacks {
|
||||||
where = append(where, []interface{}{black.OwnerUserID, black.BlockUserID})
|
where = append(where, []interface{}{black.OwnerUserID, black.BlockUserID})
|
||||||
}
|
}
|
||||||
|
|
||||||
return blackList, utils.Wrap(
|
return blackList, utils.Wrap(
|
||||||
b.db(ctx).Where("(owner_user_id, block_user_id) in ?", where).Find(&blackList).Error,
|
b.db(ctx).Where("(owner_user_id, block_user_id) in ?", where).Find(&blackList).Error,
|
||||||
"",
|
"",
|
||||||
@@ -76,7 +75,6 @@ func (b *BlackGorm) Find(
|
|||||||
|
|
||||||
func (b *BlackGorm) Take(ctx context.Context, ownerUserID, blockUserID string) (black *relation.BlackModel, err error) {
|
func (b *BlackGorm) Take(ctx context.Context, ownerUserID, blockUserID string) (black *relation.BlackModel, err error) {
|
||||||
black = &relation.BlackModel{}
|
black = &relation.BlackModel{}
|
||||||
|
|
||||||
return black, utils.Wrap(
|
return black, utils.Wrap(
|
||||||
b.db(ctx).Where("owner_user_id = ? and block_user_id = ?", ownerUserID, blockUserID).Take(black).Error,
|
b.db(ctx).Where("owner_user_id = ? and block_user_id = ?", ownerUserID, blockUserID).Take(black).Error,
|
||||||
"",
|
"",
|
||||||
@@ -98,7 +96,6 @@ func (b *BlackGorm) FindOwnerBlacks(
|
|||||||
showNumber,
|
showNumber,
|
||||||
)
|
)
|
||||||
total = int64(totalUint32)
|
total = int64(totalUint32)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,8 +15,6 @@
|
|||||||
package relation
|
package relation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|
||||||
//nolint:staticcheck //tofix: SA1019: "github.com/golang/protobuf/jsonpb" is deprecated: Use the "google.golang.org/protobuf/encoding/protojson" package instead.
|
|
||||||
"github.com/golang/protobuf/jsonpb"
|
"github.com/golang/protobuf/jsonpb"
|
||||||
"github.com/jinzhu/copier"
|
"github.com/jinzhu/copier"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
@@ -40,10 +38,7 @@ func NewChatLogGorm(db *gorm.DB) relation.ChatLogModelInterface {
|
|||||||
|
|
||||||
func (c *ChatLogGorm) Create(msg *pbmsg.MsgDataToMQ) error {
|
func (c *ChatLogGorm) Create(msg *pbmsg.MsgDataToMQ) error {
|
||||||
chatLog := new(relation.ChatLogModel)
|
chatLog := new(relation.ChatLogModel)
|
||||||
err := copier.Copy(chatLog, msg.MsgData)
|
copier.Copy(chatLog, msg.MsgData)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
switch msg.MsgData.SessionType {
|
switch msg.MsgData.SessionType {
|
||||||
case constant.GroupChatType, constant.SuperGroupChatType:
|
case constant.GroupChatType, constant.SuperGroupChatType:
|
||||||
chatLog.RecvID = msg.MsgData.GroupID
|
chatLog.RecvID = msg.MsgData.GroupID
|
||||||
@@ -64,6 +59,5 @@ func (c *ChatLogGorm) Create(msg *pbmsg.MsgDataToMQ) error {
|
|||||||
}
|
}
|
||||||
chatLog.CreateTime = utils.UnixMillSecondToTime(msg.MsgData.CreateTime)
|
chatLog.CreateTime = utils.UnixMillSecondToTime(msg.MsgData.CreateTime)
|
||||||
chatLog.SendTime = utils.UnixMillSecondToTime(msg.MsgData.SendTime)
|
chatLog.SendTime = utils.UnixMillSecondToTime(msg.MsgData.SendTime)
|
||||||
|
|
||||||
return c.DB.Create(chatLog).Error
|
return c.DB.Create(chatLog).Error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,7 +54,6 @@ func (c *ConversationGorm) UpdateByMap(
|
|||||||
args map[string]interface{},
|
args map[string]interface{},
|
||||||
) (rows int64, err error) {
|
) (rows int64, err error) {
|
||||||
result := c.db(ctx).Where("owner_user_id IN (?) and conversation_id=?", userIDList, conversationID).Updates(args)
|
result := c.db(ctx).Where("owner_user_id IN (?) and conversation_id=?", userIDList, conversationID).Updates(args)
|
||||||
|
|
||||||
return result.RowsAffected, utils.Wrap(result.Error, "")
|
return result.RowsAffected, utils.Wrap(result.Error, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +79,6 @@ func (c *ConversationGorm) Find(
|
|||||||
Error,
|
Error,
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|
||||||
return conversations, err
|
return conversations, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,7 +87,6 @@ func (c *ConversationGorm) Take(
|
|||||||
userID, conversationID string,
|
userID, conversationID string,
|
||||||
) (conversation *relation.ConversationModel, err error) {
|
) (conversation *relation.ConversationModel, err error) {
|
||||||
cc := &relation.ConversationModel{}
|
cc := &relation.ConversationModel{}
|
||||||
|
|
||||||
return cc, utils.Wrap(
|
return cc, utils.Wrap(
|
||||||
c.db(ctx).Where("conversation_id = ? And owner_user_id = ?", conversationID, userID).Take(cc).Error,
|
c.db(ctx).Where("conversation_id = ? And owner_user_id = ?", conversationID, userID).Take(cc).Error,
|
||||||
"",
|
"",
|
||||||
@@ -172,7 +169,6 @@ func (c *ConversationGorm) GetUserRecvMsgOpt(
|
|||||||
ownerUserID, conversationID string,
|
ownerUserID, conversationID string,
|
||||||
) (opt int, err error) {
|
) (opt int, err error) {
|
||||||
var conversation relation.ConversationModel
|
var conversation relation.ConversationModel
|
||||||
|
|
||||||
return int(
|
return int(
|
||||||
conversation.RecvMsgOpt,
|
conversation.RecvMsgOpt,
|
||||||
), utils.Wrap(
|
), utils.Wrap(
|
||||||
@@ -223,7 +219,6 @@ func (c *ConversationGorm) GetConversationIDsNeedDestruct(
|
|||||||
|
|
||||||
func (c *ConversationGorm) GetConversationRecvMsgOpt(ctx context.Context, userID string, conversationID string) (int32, error) {
|
func (c *ConversationGorm) GetConversationRecvMsgOpt(ctx context.Context, userID string, conversationID string) (int32, error) {
|
||||||
var recvMsgOpt int32
|
var recvMsgOpt int32
|
||||||
|
|
||||||
return recvMsgOpt, errs.Wrap(
|
return recvMsgOpt, errs.Wrap(
|
||||||
c.db(ctx).
|
c.db(ctx).
|
||||||
Model(&relation.ConversationModel{}).
|
Model(&relation.ConversationModel{}).
|
||||||
@@ -235,7 +230,6 @@ func (c *ConversationGorm) GetConversationRecvMsgOpt(ctx context.Context, userID
|
|||||||
|
|
||||||
func (c *ConversationGorm) GetConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) ([]string, error) {
|
func (c *ConversationGorm) GetConversationNotReceiveMessageUserIDs(ctx context.Context, conversationID string) ([]string, error) {
|
||||||
var userIDs []string
|
var userIDs []string
|
||||||
|
|
||||||
return userIDs, errs.Wrap(
|
return userIDs, errs.Wrap(
|
||||||
c.db(ctx).
|
c.db(ctx).
|
||||||
Model(&relation.ConversationModel{}).
|
Model(&relation.ConversationModel{}).
|
||||||
|
|||||||
@@ -50,7 +50,6 @@ func (f *FriendGorm) Delete(ctx context.Context, ownerUserID string, friendUserI
|
|||||||
Error,
|
Error,
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +84,6 @@ func (f *FriendGorm) UpdateRemark(ctx context.Context, ownerUserID, friendUserID
|
|||||||
}
|
}
|
||||||
m := make(map[string]interface{}, 1)
|
m := make(map[string]interface{}, 1)
|
||||||
m["remark"] = ""
|
m["remark"] = ""
|
||||||
|
|
||||||
return utils.Wrap(f.db(ctx).Where("owner_user_id = ?", ownerUserID).Updates(m).Error, "")
|
return utils.Wrap(f.db(ctx).Where("owner_user_id = ?", ownerUserID).Updates(m).Error, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,7 +93,6 @@ func (f *FriendGorm) Take(
|
|||||||
ownerUserID, friendUserID string,
|
ownerUserID, friendUserID string,
|
||||||
) (friend *relation.FriendModel, err error) {
|
) (friend *relation.FriendModel, err error) {
|
||||||
friend = &relation.FriendModel{}
|
friend = &relation.FriendModel{}
|
||||||
|
|
||||||
return friend, utils.Wrap(
|
return friend, utils.Wrap(
|
||||||
f.db(ctx).Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error,
|
f.db(ctx).Where("owner_user_id = ? and friend_user_id", ownerUserID, friendUserID).Take(friend).Error,
|
||||||
"",
|
"",
|
||||||
@@ -159,7 +156,6 @@ func (f *FriendGorm) FindOwnerFriends(
|
|||||||
Error,
|
Error,
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,7 +178,6 @@ func (f *FriendGorm) FindInWhoseFriends(
|
|||||||
Error,
|
Error,
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -74,7 +74,6 @@ func (f *FriendRequestGorm) Update(ctx context.Context, friendRequest *relation.
|
|||||||
fr2 := *friendRequest
|
fr2 := *friendRequest
|
||||||
fr2.FromUserID = ""
|
fr2.FromUserID = ""
|
||||||
fr2.ToUserID = ""
|
fr2.ToUserID = ""
|
||||||
|
|
||||||
return utils.Wrap(
|
return utils.Wrap(
|
||||||
f.db(ctx).
|
f.db(ctx).
|
||||||
Where("from_user_id = ? AND to_user_id =?", friendRequest.FromUserID, friendRequest.ToUserID).
|
Where("from_user_id = ? AND to_user_id =?", friendRequest.FromUserID, friendRequest.ToUserID).
|
||||||
@@ -94,7 +93,6 @@ func (f *FriendRequestGorm) Find(
|
|||||||
f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Find(friendRequest).Error,
|
f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Find(friendRequest).Error,
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|
||||||
return friendRequest, err
|
return friendRequest, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,7 +105,6 @@ func (f *FriendRequestGorm) Take(
|
|||||||
f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Take(friendRequest).Error,
|
f.db(ctx).Where("from_user_id = ? and to_user_id = ?", fromUserID, toUserID).Take(friendRequest).Error,
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|
||||||
return friendRequest, err
|
return friendRequest, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +127,6 @@ func (f *FriendRequestGorm) FindToUserID(
|
|||||||
Error,
|
Error,
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -153,7 +149,6 @@ func (f *FriendRequestGorm) FindFromUserID(
|
|||||||
Error,
|
Error,
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,6 +160,5 @@ func (f *FriendRequestGorm) FindBothFriendRequests(ctx context.Context, fromUser
|
|||||||
Error,
|
Error,
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,7 +68,6 @@ func (g *GroupMemberGorm) UpdateRoleLevel(
|
|||||||
db := g.db(ctx).Where("group_id = ? and user_id = ?", groupID, userID).Updates(map[string]any{
|
db := g.db(ctx).Where("group_id = ? and user_id = ?", groupID, userID).Updates(map[string]any{
|
||||||
"role_level": roleLevel,
|
"role_level": roleLevel,
|
||||||
})
|
})
|
||||||
|
|
||||||
return db.RowsAffected, utils.Wrap(db.Error, "")
|
return db.RowsAffected, utils.Wrap(db.Error, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +87,6 @@ func (g *GroupMemberGorm) Find(
|
|||||||
if len(roleLevels) > 0 {
|
if len(roleLevels) > 0 {
|
||||||
db = db.Where("role_level in (?)", roleLevels)
|
db = db.Where("role_level in (?)", roleLevels)
|
||||||
}
|
}
|
||||||
|
|
||||||
return groupMembers, utils.Wrap(db.Find(&groupMembers).Error, "")
|
return groupMembers, utils.Wrap(db.Find(&groupMembers).Error, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +96,6 @@ func (g *GroupMemberGorm) Take(
|
|||||||
userID string,
|
userID string,
|
||||||
) (groupMember *relation.GroupMemberModel, err error) {
|
) (groupMember *relation.GroupMemberModel, err error) {
|
||||||
groupMember = &relation.GroupMemberModel{}
|
groupMember = &relation.GroupMemberModel{}
|
||||||
|
|
||||||
return groupMember, utils.Wrap(
|
return groupMember, utils.Wrap(
|
||||||
g.db(ctx).Where("group_id = ? and user_id = ?", groupID, userID).Take(groupMember).Error,
|
g.db(ctx).Where("group_id = ? and user_id = ?", groupID, userID).Take(groupMember).Error,
|
||||||
"",
|
"",
|
||||||
@@ -110,7 +107,6 @@ func (g *GroupMemberGorm) TakeOwner(
|
|||||||
groupID string,
|
groupID string,
|
||||||
) (groupMember *relation.GroupMemberModel, err error) {
|
) (groupMember *relation.GroupMemberModel, err error) {
|
||||||
groupMember = &relation.GroupMemberModel{}
|
groupMember = &relation.GroupMemberModel{}
|
||||||
|
|
||||||
return groupMember, utils.Wrap(
|
return groupMember, utils.Wrap(
|
||||||
g.db(ctx).Where("group_id = ? and role_level = ?", groupID, constant.GroupOwner).Take(groupMember).Error,
|
g.db(ctx).Where("group_id = ? and role_level = ?", groupID, constant.GroupOwner).Take(groupMember).Error,
|
||||||
"",
|
"",
|
||||||
@@ -129,7 +125,6 @@ func (g *GroupMemberGorm) SearchMember(
|
|||||||
ormutil.GormIn(&db, "group_id", groupIDs)
|
ormutil.GormIn(&db, "group_id", groupIDs)
|
||||||
ormutil.GormIn(&db, "user_id", userIDs)
|
ormutil.GormIn(&db, "user_id", userIDs)
|
||||||
ormutil.GormIn(&db, "role_level", roleLevels)
|
ormutil.GormIn(&db, "role_level", roleLevels)
|
||||||
|
|
||||||
return ormutil.GormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber)
|
return ormutil.GormSearch[relation.GroupMemberModel](db, []string{"nickname"}, keyword, pageNumber, showNumber)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +152,6 @@ func (g *GroupMemberGorm) FindJoinUserID(
|
|||||||
groupUsers[item.GroupID] = append(v, item.UserID)
|
groupUsers[item.GroupID] = append(v, item.UserID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return groupUsers, nil
|
return groupUsers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,7 +182,6 @@ func (g *GroupMemberGorm) FindUsersJoinedGroupID(ctx context.Context, userIDs []
|
|||||||
result[groupMember.UserID] = append(v, groupMember.GroupID)
|
result[groupMember.UserID] = append(v, groupMember.GroupID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -61,14 +61,12 @@ func (g *GroupGorm) Find(ctx context.Context, groupIDs []string) (groups []*rela
|
|||||||
|
|
||||||
func (g *GroupGorm) Take(ctx context.Context, groupID string) (group *relation.GroupModel, err error) {
|
func (g *GroupGorm) Take(ctx context.Context, groupID string) (group *relation.GroupModel, err error) {
|
||||||
group = &relation.GroupModel{}
|
group = &relation.GroupModel{}
|
||||||
|
|
||||||
return group, utils.Wrap(g.DB.Where("group_id = ?", groupID).Take(group).Error, "")
|
return group, utils.Wrap(g.DB.Where("group_id = ?", groupID).Take(group).Error, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32) (total uint32, groups []*relation.GroupModel, err error) {
|
func (g *GroupGorm) Search(ctx context.Context, keyword string, pageNumber, showNumber int32) (total uint32, groups []*relation.GroupModel, err error) {
|
||||||
db := g.DB
|
db := g.DB
|
||||||
db = db.WithContext(ctx).Where("status!=?", constant.GroupStatusDismissed)
|
db = db.WithContext(ctx).Where("status!=?", constant.GroupStatusDismissed)
|
||||||
|
|
||||||
return ormutil.GormSearch[relation.GroupModel](db, []string{"name"}, keyword, pageNumber, showNumber)
|
return ormutil.GormSearch[relation.GroupModel](db, []string{"name"}, keyword, pageNumber, showNumber)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +82,6 @@ func (g *GroupGorm) CountTotal(ctx context.Context, before *time.Time) (count in
|
|||||||
if err := db.Count(&count).Error; err != nil {
|
if err := db.Count(&count).Error; err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return count, nil
|
return count, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,7 +98,6 @@ func (g *GroupGorm) CountRangeEverydayTotal(ctx context.Context, start time.Time
|
|||||||
for _, r := range res {
|
for _, r := range res {
|
||||||
v[r.Date.Format("2006-01-02")] = r.Count
|
v[r.Date.Format("2006-01-02")] = r.Count
|
||||||
}
|
}
|
||||||
|
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -80,7 +80,6 @@ func (g *GroupRequestGorm) Take(
|
|||||||
userID string,
|
userID string,
|
||||||
) (groupRequest *relation.GroupRequestModel, err error) {
|
) (groupRequest *relation.GroupRequestModel, err error) {
|
||||||
groupRequest = &relation.GroupRequestModel{}
|
groupRequest = &relation.GroupRequestModel{}
|
||||||
|
|
||||||
return groupRequest, utils.Wrap(
|
return groupRequest, utils.Wrap(
|
||||||
g.DB.WithContext(ctx).Where("group_id = ? and user_id = ? ", groupID, userID).Take(groupRequest).Error,
|
g.DB.WithContext(ctx).Where("group_id = ? and user_id = ? ", groupID, userID).Take(groupRequest).Error,
|
||||||
utils.GetSelfFuncName(),
|
utils.GetSelfFuncName(),
|
||||||
@@ -115,6 +114,5 @@ func (g *GroupRequestGorm) PageGroup(
|
|||||||
|
|
||||||
func (g *GroupRequestGorm) FindGroupRequests(ctx context.Context, groupID string, userIDs []string) (total int64, groupRequests []*relation.GroupRequestModel, err error) {
|
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
|
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())
|
return int64(len(groupRequests)), groupRequests, utils.Wrap(err, utils.GetSelfFuncName())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ func (l *LogGorm) Search(ctx context.Context, keyword string, start time.Time, e
|
|||||||
db = l.db.WithContext(ctx).Where("create_time <= ?", end)
|
db = l.db.WithContext(ctx).Where("create_time <= ?", end)
|
||||||
}
|
}
|
||||||
db = db.Order("create_time desc")
|
db = db.Order("create_time desc")
|
||||||
|
|
||||||
return ormutil.GormSearch[relationtb.Log](db, []string{"user_id"}, keyword, pageNumber, showNumber)
|
return ormutil.GormSearch[relationtb.Log](db, []string{"user_id"}, keyword, pageNumber, showNumber)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +32,6 @@ func (l *LogGorm) Delete(ctx context.Context, logIDs []string, userID string) er
|
|||||||
if userID == "" {
|
if userID == "" {
|
||||||
return errs.Wrap(l.db.WithContext(ctx).Where("log_id in ?", logIDs).Delete(&relationtb.Log{}).Error)
|
return errs.Wrap(l.db.WithContext(ctx).Where("log_id in ?", logIDs).Delete(&relationtb.Log{}).Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
return errs.Wrap(l.db.WithContext(ctx).Where("log_id in ? and user_id=?", logIDs, userID).Delete(&relationtb.Log{}).Error)
|
return errs.Wrap(l.db.WithContext(ctx).Where("log_id in ? and user_id=?", logIDs, userID).Delete(&relationtb.Log{}).Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,15 +40,10 @@ func (l *LogGorm) Get(ctx context.Context, logIDs []string, userID string) ([]*r
|
|||||||
if userID == "" {
|
if userID == "" {
|
||||||
return logs, errs.Wrap(l.db.WithContext(ctx).Where("log_id in ?", logIDs).Find(&logs).Error)
|
return logs, errs.Wrap(l.db.WithContext(ctx).Where("log_id in ?", logIDs).Find(&logs).Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
return logs, errs.Wrap(l.db.WithContext(ctx).Where("log_id in ? and user_id=?", logIDs, userID).Find(&logs).Error)
|
return logs, errs.Wrap(l.db.WithContext(ctx).Where("log_id in ? and user_id=?", logIDs, userID).Find(&logs).Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLogGorm(db *gorm.DB) relationtb.LogInterface {
|
func NewLogGorm(db *gorm.DB) relationtb.LogInterface {
|
||||||
err := db.AutoMigrate(&relationtb.Log{})
|
db.AutoMigrate(&relationtb.Log{})
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &LogGorm{db: db}
|
return &LogGorm{db: db}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,6 +34,5 @@ func NewMetaDB(db *gorm.DB, table any) *MetaDB {
|
|||||||
|
|
||||||
func (g *MetaDB) db(ctx context.Context) *gorm.DB {
|
func (g *MetaDB) db(ctx context.Context) *gorm.DB {
|
||||||
db := g.DB.WithContext(ctx).Model(g.table)
|
db := g.DB.WithContext(ctx).Model(g.table)
|
||||||
|
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,6 @@
|
|||||||
package relation
|
package relation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -83,7 +82,6 @@ func newMysqlGormDB() (*gorm.DB, error) {
|
|||||||
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.Config.Mysql.MaxLifeTime))
|
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(config.Config.Mysql.MaxLifeTime))
|
||||||
sqlDB.SetMaxOpenConns(config.Config.Mysql.MaxOpenConn)
|
sqlDB.SetMaxOpenConns(config.Config.Mysql.MaxOpenConn)
|
||||||
sqlDB.SetMaxIdleConns(config.Config.Mysql.MaxIdleConn)
|
sqlDB.SetMaxIdleConns(config.Config.Mysql.MaxIdleConn)
|
||||||
|
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,13 +94,11 @@ func connectToDatabase(dsn string, maxRetry int) (*gorm.DB, error) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
var mysqlErr *mysqldriver.MySQLError
|
if mysqlErr, ok := err.(*mysqldriver.MySQLError); ok && mysqlErr.Number == 1045 {
|
||||||
if errors.As(err, &mysqlErr) && mysqlErr.Number == 1045 {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
time.Sleep(time.Duration(1) * time.Second)
|
time.Sleep(time.Duration(1) * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,7 +106,6 @@ func connectToDatabase(dsn string, maxRetry int) (*gorm.DB, error) {
|
|||||||
func NewGormDB() (*gorm.DB, error) {
|
func NewGormDB() (*gorm.DB, error) {
|
||||||
specialerror.AddReplace(gorm.ErrRecordNotFound, errs.ErrRecordNotFound)
|
specialerror.AddReplace(gorm.ErrRecordNotFound, errs.ErrRecordNotFound)
|
||||||
specialerror.AddErrHandler(replaceDuplicateKey)
|
specialerror.AddErrHandler(replaceDuplicateKey)
|
||||||
|
|
||||||
return newMysqlGormDB()
|
return newMysqlGormDB()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -118,15 +113,12 @@ func replaceDuplicateKey(err error) errs.CodeError {
|
|||||||
if IsMysqlDuplicateKey(err) {
|
if IsMysqlDuplicateKey(err) {
|
||||||
return errs.ErrDuplicateKey
|
return errs.ErrDuplicateKey
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsMysqlDuplicateKey(err error) bool {
|
func IsMysqlDuplicateKey(err error) bool {
|
||||||
var mysqlErr *mysqldriver.MySQLError
|
if mysqlErr, ok := err.(*mysqldriver.MySQLError); ok {
|
||||||
if errors.As(err, &mysqlErr) {
|
|
||||||
return mysqlErr.Number == 1062
|
return mysqlErr.Number == 1062
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,12 +44,10 @@ func (o *ObjectInfoGorm) SetObject(ctx context.Context, obj *relation.ObjectMode
|
|||||||
if err := o.DB.WithContext(ctx).Where("name = ?", obj.Name).FirstOrCreate(obj).Error; err != nil {
|
if err := o.DB.WithContext(ctx).Where("name = ?", obj.Name).FirstOrCreate(obj).Error; err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *ObjectInfoGorm) Take(ctx context.Context, name string) (info *relation.ObjectModel, err error) {
|
func (o *ObjectInfoGorm) Take(ctx context.Context, name string) (info *relation.ObjectModel, err error) {
|
||||||
info = &relation.ObjectModel{}
|
info = &relation.ObjectModel{}
|
||||||
|
|
||||||
return info, errs.Wrap(o.DB.WithContext(ctx).Where("name = ?", name).Take(info).Error)
|
return info, errs.Wrap(o.DB.WithContext(ctx).Where("name = ?", name).Take(info).Error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,6 @@ func (u *UserGorm) Update(ctx context.Context, user *relation.UserModel) (err er
|
|||||||
// 获取指定用户信息 不存在,也不返回错误.
|
// 获取指定用户信息 不存在,也不返回错误.
|
||||||
func (u *UserGorm) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
|
func (u *UserGorm) Find(ctx context.Context, userIDs []string) (users []*relation.UserModel, err error) {
|
||||||
err = utils.Wrap(u.db(ctx).Where("user_id in (?)", userIDs).Find(&users).Error, "")
|
err = utils.Wrap(u.db(ctx).Where("user_id in (?)", userIDs).Find(&users).Error, "")
|
||||||
|
|
||||||
return users, err
|
return users, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,7 +60,6 @@ func (u *UserGorm) Find(ctx context.Context, userIDs []string) (users []*relatio
|
|||||||
func (u *UserGorm) Take(ctx context.Context, userID string) (user *relation.UserModel, err error) {
|
func (u *UserGorm) Take(ctx context.Context, userID string) (user *relation.UserModel, err error) {
|
||||||
user = &relation.UserModel{}
|
user = &relation.UserModel{}
|
||||||
err = utils.Wrap(u.db(ctx).Where("user_id = ?", userID).Take(&user).Error, "")
|
err = utils.Wrap(u.db(ctx).Where("user_id = ?", userID).Take(&user).Error, "")
|
||||||
|
|
||||||
return user, err
|
return user, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +81,6 @@ func (u *UserGorm) Page(
|
|||||||
Error,
|
Error,
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,14 +88,13 @@ func (u *UserGorm) Page(
|
|||||||
func (u *UserGorm) GetAllUserID(ctx context.Context, pageNumber, showNumber int32) (userIDs []string, err error) {
|
func (u *UserGorm) GetAllUserID(ctx context.Context, pageNumber, showNumber int32) (userIDs []string, err error) {
|
||||||
if pageNumber == 0 || showNumber == 0 {
|
if pageNumber == 0 || showNumber == 0 {
|
||||||
return userIDs, errs.Wrap(u.db(ctx).Pluck("user_id", &userIDs).Error)
|
return userIDs, errs.Wrap(u.db(ctx).Pluck("user_id", &userIDs).Error)
|
||||||
|
} else {
|
||||||
|
return userIDs, errs.Wrap(u.db(ctx).Limit(int(showNumber)).Offset(int((pageNumber-1)*showNumber)).Pluck("user_id", &userIDs).Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
return userIDs, errs.Wrap(u.db(ctx).Limit(int(showNumber)).Offset(int((pageNumber-1)*showNumber)).Pluck("user_id", &userIDs).Error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UserGorm) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) {
|
func (u *UserGorm) GetUserGlobalRecvMsgOpt(ctx context.Context, userID string) (opt int, err error) {
|
||||||
err = u.db(ctx).Model(&relation.UserModel{}).Where("user_id = ?", userID).Pluck("global_recv_msg_opt", &opt).Error
|
err = u.db(ctx).Model(&relation.UserModel{}).Where("user_id = ?", userID).Pluck("global_recv_msg_opt", &opt).Error
|
||||||
|
|
||||||
return opt, err
|
return opt, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,7 +106,6 @@ func (u *UserGorm) CountTotal(ctx context.Context, before *time.Time) (count int
|
|||||||
if err := db.Count(&count).Error; err != nil {
|
if err := db.Count(&count).Error; err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return count, nil
|
return count, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,6 +132,5 @@ func (u *UserGorm) CountRangeEverydayTotal(
|
|||||||
for _, r := range res {
|
for _, r := range res {
|
||||||
v[r.Date.Format("2006-01-02")] = r.Count
|
v[r.Date.Format("2006-01-02")] = r.Count
|
||||||
}
|
}
|
||||||
|
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ func (c *Controller) HashPath(md5 string) string {
|
|||||||
|
|
||||||
func (c *Controller) NowPath() string {
|
func (c *Controller) NowPath() string {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
|
||||||
return path.Join(
|
return path.Join(
|
||||||
fmt.Sprintf("%04d", now.Year()),
|
fmt.Sprintf("%04d", now.Year()),
|
||||||
fmt.Sprintf("%02d", now.Month()),
|
fmt.Sprintf("%02d", now.Month()),
|
||||||
@@ -59,7 +58,6 @@ func (c *Controller) NowPath() string {
|
|||||||
|
|
||||||
func (c *Controller) UUID() string {
|
func (c *Controller) UUID() string {
|
||||||
id := uuid.New()
|
id := uuid.New()
|
||||||
|
|
||||||
return hex.EncodeToString(id[:])
|
return hex.EncodeToString(id[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,24 +92,20 @@ func (c *Controller) InitiateUpload(ctx context.Context, hash string, size int64
|
|||||||
partNumber++
|
partNumber++
|
||||||
}
|
}
|
||||||
if maxParts > 0 && partNumber > 0 && partNumber < maxParts {
|
if maxParts > 0 && partNumber > 0 && partNumber < maxParts {
|
||||||
return nil, fmt.Errorf("too few parts: %d", partNumber)
|
return nil, errors.New(fmt.Sprintf("too many parts: %d", partNumber))
|
||||||
}
|
}
|
||||||
info, err := c.impl.StatObject(ctx, c.HashPath(hash))
|
if info, err := c.impl.StatObject(ctx, c.HashPath(hash)); err == nil {
|
||||||
if err == nil {
|
|
||||||
return nil, &HashAlreadyExistsError{Object: info}
|
return nil, &HashAlreadyExistsError{Object: info}
|
||||||
}
|
} else if !c.impl.IsNotFound(err) {
|
||||||
if !c.impl.IsNotFound(err) {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if size <= partSize {
|
if size <= partSize {
|
||||||
// 预签名上传
|
// 预签名上传
|
||||||
key := path.Join(tempPath, c.NowPath(), fmt.Sprintf("%s_%d_%s.presigned", hash, size, c.UUID()))
|
key := path.Join(tempPath, c.NowPath(), fmt.Sprintf("%s_%d_%s.presigned", hash, size, c.UUID()))
|
||||||
rawURL, err2 := c.impl.PresignedPutObject(ctx, key, expire)
|
rawURL, err := c.impl.PresignedPutObject(ctx, key, expire)
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
return nil, err2
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &InitiateUploadResult{
|
return &InitiateUploadResult{
|
||||||
UploadID: newMultipartUploadID(multipartUploadID{
|
UploadID: newMultipartUploadID(multipartUploadID{
|
||||||
Type: UploadTypePresigned,
|
Type: UploadTypePresigned,
|
||||||
@@ -130,39 +124,38 @@ func (c *Controller) InitiateUpload(ctx context.Context, hash string, size int64
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
} else {
|
||||||
|
// 分片上传
|
||||||
// 分片上传
|
upload, err := c.impl.InitiateMultipartUpload(ctx, c.HashPath(hash))
|
||||||
upload, err := c.impl.InitiateMultipartUpload(ctx, c.HashPath(hash))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if maxParts < 0 {
|
|
||||||
maxParts = partNumber
|
|
||||||
}
|
|
||||||
var authSign *s3.AuthSignResult
|
|
||||||
if maxParts > 0 {
|
|
||||||
partNumbers := make([]int, partNumber)
|
|
||||||
for i := 0; i < maxParts; i++ {
|
|
||||||
partNumbers[i] = i + 1
|
|
||||||
}
|
|
||||||
authSign, err = c.impl.AuthSign(ctx, upload.UploadID, upload.Key, time.Hour*24, partNumbers)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if maxParts < 0 {
|
||||||
|
maxParts = partNumber
|
||||||
|
}
|
||||||
|
var authSign *s3.AuthSignResult
|
||||||
|
if maxParts > 0 {
|
||||||
|
partNumbers := make([]int, partNumber)
|
||||||
|
for i := 0; i < maxParts; i++ {
|
||||||
|
partNumbers[i] = i + 1
|
||||||
|
}
|
||||||
|
authSign, err = c.impl.AuthSign(ctx, upload.UploadID, upload.Key, time.Hour*24, partNumbers)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return &InitiateUploadResult{
|
||||||
|
UploadID: newMultipartUploadID(multipartUploadID{
|
||||||
|
Type: UploadTypeMultipart,
|
||||||
|
ID: upload.UploadID,
|
||||||
|
Key: upload.Key,
|
||||||
|
Size: size,
|
||||||
|
Hash: hash,
|
||||||
|
}),
|
||||||
|
PartSize: partSize,
|
||||||
|
Sign: authSign,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return &InitiateUploadResult{
|
|
||||||
UploadID: newMultipartUploadID(multipartUploadID{
|
|
||||||
Type: UploadTypeMultipart,
|
|
||||||
ID: upload.UploadID,
|
|
||||||
Key: upload.Key,
|
|
||||||
Size: size,
|
|
||||||
Hash: hash,
|
|
||||||
}),
|
|
||||||
PartSize: partSize,
|
|
||||||
Sign: authSign,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHashs []string) (*UploadResult, error) {
|
func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHashs []string) (*UploadResult, error) {
|
||||||
@@ -171,10 +164,8 @@ func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHa
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
//nolint:gosec //tofix G401: Use of weak cryptographic primitive
|
|
||||||
if md5Sum := md5.Sum([]byte(strings.Join(partHashs, partSeparator))); hex.EncodeToString(md5Sum[:]) != upload.Hash {
|
if md5Sum := md5.Sum([]byte(strings.Join(partHashs, partSeparator))); hex.EncodeToString(md5Sum[:]) != upload.Hash {
|
||||||
fmt.Println("CompleteUpload sum:", hex.EncodeToString(md5Sum[:]), "upload hash:", upload.Hash)
|
fmt.Println("CompleteUpload sum:", hex.EncodeToString(md5Sum[:]), "upload hash:", upload.Hash)
|
||||||
|
|
||||||
return nil, errors.New("md5 mismatching")
|
return nil, errors.New("md5 mismatching")
|
||||||
}
|
}
|
||||||
if info, err := c.impl.StatObject(ctx, c.HashPath(upload.Hash)); err == nil {
|
if info, err := c.impl.StatObject(ctx, c.HashPath(upload.Hash)); err == nil {
|
||||||
@@ -202,7 +193,7 @@ func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHa
|
|||||||
ETag: part,
|
ETag: part,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// todo: verify size
|
// todo: 验证大小
|
||||||
result, err := c.impl.CompleteMultipartUpload(ctx, upload.ID, upload.Key, parts)
|
result, err := c.impl.CompleteMultipartUpload(ctx, upload.ID, upload.Key, parts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -217,12 +208,11 @@ func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHa
|
|||||||
if uploadInfo.Size != upload.Size {
|
if uploadInfo.Size != upload.Size {
|
||||||
return nil, errors.New("upload size mismatching")
|
return nil, errors.New("upload size mismatching")
|
||||||
}
|
}
|
||||||
//nolint:gosec //G401: Use of weak cryptographic primitive
|
|
||||||
md5Sum := md5.Sum([]byte(strings.Join([]string{uploadInfo.ETag}, partSeparator)))
|
md5Sum := md5.Sum([]byte(strings.Join([]string{uploadInfo.ETag}, partSeparator)))
|
||||||
if md5val := hex.EncodeToString(md5Sum[:]); md5val != upload.Hash {
|
if md5val := hex.EncodeToString(md5Sum[:]); md5val != upload.Hash {
|
||||||
return nil, errs.ErrArgs.Wrap(fmt.Sprintf("md5 mismatching %s != %s", md5val, upload.Hash))
|
return nil, errs.ErrArgs.Wrap(fmt.Sprintf("md5 mismatching %s != %s", md5val, upload.Hash))
|
||||||
}
|
}
|
||||||
// Prevent concurrent operations at this time to avoid file overwrite
|
// 防止在这个时候,并发操作,导致文件被覆盖
|
||||||
copyInfo, err := c.impl.CopyObject(ctx, uploadInfo.Key, upload.Key+"."+c.UUID())
|
copyInfo, err := c.impl.CopyObject(ctx, uploadInfo.Key, upload.Key+"."+c.UUID())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -240,7 +230,6 @@ func (c *Controller) CompleteUpload(ctx context.Context, uploadID string, partHa
|
|||||||
default:
|
default:
|
||||||
return nil, errors.New("invalid upload id type")
|
return nil, errors.New("invalid upload id type")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &UploadResult{
|
return &UploadResult{
|
||||||
Key: targetKey,
|
Key: targetKey,
|
||||||
Size: upload.Size,
|
Size: upload.Size,
|
||||||
@@ -272,6 +261,5 @@ func (c *Controller) AccessURL(ctx context.Context, name string, expire time.Dur
|
|||||||
opt.Filename = ""
|
opt.Filename = ""
|
||||||
opt.ContentType = ""
|
opt.ContentType = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.impl.AccessURL(ctx, name, expire, opt)
|
return c.impl.AccessURL(ctx, name, expire, opt)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ func newMultipartUploadID(id multipartUploadID) string {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return base64.StdEncoding.EncodeToString(data)
|
return base64.StdEncoding.EncodeToString(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,6 +45,5 @@ func parseMultipartUploadID(id string) (*multipartUploadID, error) {
|
|||||||
if err := json.Unmarshal(data, &upload); err != nil {
|
if err := json.Unmarshal(data, &upload); err != nil {
|
||||||
return nil, fmt.Errorf("invalid multipart upload id: %w", err)
|
return nil, fmt.Errorf("invalid multipart upload id: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &upload, nil
|
return &upload, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,6 +44,11 @@ const (
|
|||||||
imageWebp = "webp"
|
imageWebp = "webp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
videoSnapshotImagePng = "png"
|
||||||
|
videoSnapshotImageJpg = "jpg"
|
||||||
|
)
|
||||||
|
|
||||||
func NewCos() (s3.Interface, error) {
|
func NewCos() (s3.Interface, error) {
|
||||||
conf := config.Config.Object.Cos
|
conf := config.Config.Object.Cos
|
||||||
u, err := url.Parse(conf.BucketURL)
|
u, err := url.Parse(conf.BucketURL)
|
||||||
@@ -57,7 +62,6 @@ func NewCos() (s3.Interface, error) {
|
|||||||
SessionToken: conf.SessionToken,
|
SessionToken: conf.SessionToken,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
return &Cos{
|
return &Cos{
|
||||||
copyURL: u.Host + "/",
|
copyURL: u.Host + "/",
|
||||||
client: client,
|
client: client,
|
||||||
@@ -88,7 +92,6 @@ func (c *Cos) InitiateMultipartUpload(ctx context.Context, name string) (*s3.Ini
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &s3.InitiateMultipartUploadResult{
|
return &s3.InitiateMultipartUploadResult{
|
||||||
UploadID: result.UploadID,
|
UploadID: result.UploadID,
|
||||||
Bucket: result.Bucket,
|
Bucket: result.Bucket,
|
||||||
@@ -110,7 +113,6 @@ func (c *Cos) CompleteMultipartUpload(ctx context.Context, uploadID string, name
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &s3.CompleteMultipartUploadResult{
|
return &s3.CompleteMultipartUploadResult{
|
||||||
Location: result.Location,
|
Location: result.Location,
|
||||||
Bucket: result.Bucket,
|
Bucket: result.Bucket,
|
||||||
@@ -133,7 +135,6 @@ func (c *Cos) PartSize(ctx context.Context, size int64) (int64, error) {
|
|||||||
if size%maxNumSize != 0 {
|
if size%maxNumSize != 0 {
|
||||||
partSize++
|
partSize++
|
||||||
}
|
}
|
||||||
|
|
||||||
return partSize, nil
|
return partSize, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +157,6 @@ func (c *Cos) AuthSign(ctx context.Context, uploadID string, name string, expire
|
|||||||
Query: url.Values{"partNumber": {strconv.Itoa(partNumber)}},
|
Query: url.Values{"partNumber": {strconv.Itoa(partNumber)}},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,13 +165,11 @@ func (c *Cos) PresignedPutObject(ctx context.Context, name string, expire time.D
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return rawURL.String(), nil
|
return rawURL.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cos) DeleteObject(ctx context.Context, name string) error {
|
func (c *Cos) DeleteObject(ctx context.Context, name string) error {
|
||||||
_, err := c.client.Object.Delete(ctx, name)
|
_, err := c.client.Object.Delete(ctx, name)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,26 +185,25 @@ func (c *Cos) StatObject(ctx context.Context, name string) (*s3.ObjectInfo, erro
|
|||||||
if res.ETag = strings.ToLower(strings.ReplaceAll(info.Header.Get("ETag"), `"`, "")); res.ETag == "" {
|
if res.ETag = strings.ToLower(strings.ReplaceAll(info.Header.Get("ETag"), `"`, "")); res.ETag == "" {
|
||||||
return nil, errors.New("StatObject etag not found")
|
return nil, errors.New("StatObject etag not found")
|
||||||
}
|
}
|
||||||
contentLengthStr := info.Header.Get("Content-Length")
|
if contentLengthStr := info.Header.Get("Content-Length"); contentLengthStr == "" {
|
||||||
if contentLengthStr == "" {
|
|
||||||
return nil, errors.New("StatObject content-length not found")
|
return nil, errors.New("StatObject content-length not found")
|
||||||
|
} else {
|
||||||
|
res.Size, err = strconv.ParseInt(contentLengthStr, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("StatObject content-length parse error: %w", err)
|
||||||
|
}
|
||||||
|
if res.Size < 0 {
|
||||||
|
return nil, errors.New("StatObject content-length must be greater than 0")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
res.Size, err = strconv.ParseInt(contentLengthStr, 10, 64)
|
if lastModified := info.Header.Get("Last-Modified"); lastModified == "" {
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("StatObject content-length parse error: %w", err)
|
|
||||||
}
|
|
||||||
if res.Size < 0 {
|
|
||||||
return nil, errors.New("StatObject content-length must be greater than 0")
|
|
||||||
}
|
|
||||||
lastModified := info.Header.Get("Last-Modified")
|
|
||||||
if lastModified == "" {
|
|
||||||
return nil, errors.New("StatObject last-modified not found")
|
return nil, errors.New("StatObject last-modified not found")
|
||||||
|
} else {
|
||||||
|
res.LastModified, err = time.Parse(http.TimeFormat, lastModified)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("StatObject last-modified parse error: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
res.LastModified, err = time.Parse(http.TimeFormat, lastModified)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("StatObject last-modified parse error: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,7 +213,6 @@ func (c *Cos) CopyObject(ctx context.Context, src string, dst string) (*s3.CopyO
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &s3.CopyObjectInfo{
|
return &s3.CopyObjectInfo{
|
||||||
Key: dst,
|
Key: dst,
|
||||||
ETag: strings.ReplaceAll(result.ETag, `"`, ``),
|
ETag: strings.ReplaceAll(result.ETag, `"`, ``),
|
||||||
@@ -224,17 +220,16 @@ func (c *Cos) CopyObject(ctx context.Context, src string, dst string) (*s3.CopyO
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cos) IsNotFound(err error) bool {
|
func (c *Cos) IsNotFound(err error) bool {
|
||||||
var cosErr *cos.ErrorResponse
|
switch e := err.(type) {
|
||||||
if errors.As(err, &cosErr) {
|
case *cos.ErrorResponse:
|
||||||
return cosErr.Response.StatusCode == http.StatusNotFound || cosErr.Code == "NoSuchKey"
|
return e.Response.StatusCode == http.StatusNotFound || e.Code == "NoSuchKey"
|
||||||
|
default:
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cos) AbortMultipartUpload(ctx context.Context, uploadID string, name string) error {
|
func (c *Cos) AbortMultipartUpload(ctx context.Context, uploadID string, name string) error {
|
||||||
_, err := c.client.Object.AbortMultipartUpload(ctx, name, uploadID)
|
_, err := c.client.Object.AbortMultipartUpload(ctx, name, uploadID)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,59 +257,46 @@ func (c *Cos) ListUploadedParts(ctx context.Context, uploadID string, name strin
|
|||||||
Size: part.Size,
|
Size: part.Size,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cos) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
|
func (c *Cos) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
|
||||||
var imageMogr string
|
var imageMogr string
|
||||||
var option cos.PresignedURLOptions
|
var option cos.PresignedURLOptions
|
||||||
getImageMogr := func(opt *s3.AccessURLOption) (imageMogr string) {
|
if opt != nil {
|
||||||
if opt.Image == nil {
|
query := make(url.Values)
|
||||||
return imageMogr
|
if opt.Image != nil {
|
||||||
|
// https://cloud.tencent.com/document/product/436/44880
|
||||||
|
style := make([]string, 0, 2)
|
||||||
|
wh := make([]string, 2)
|
||||||
|
if opt.Image.Width > 0 {
|
||||||
|
wh[0] = strconv.Itoa(opt.Image.Width)
|
||||||
|
}
|
||||||
|
if opt.Image.Height > 0 {
|
||||||
|
wh[1] = strconv.Itoa(opt.Image.Height)
|
||||||
|
}
|
||||||
|
if opt.Image.Width > 0 || opt.Image.Height > 0 {
|
||||||
|
style = append(style, strings.Join(wh, "x"))
|
||||||
|
}
|
||||||
|
switch opt.Image.Format {
|
||||||
|
case
|
||||||
|
imagePng,
|
||||||
|
imageJpg,
|
||||||
|
imageJpeg,
|
||||||
|
imageGif,
|
||||||
|
imageWebp:
|
||||||
|
style = append(style, "format/"+opt.Image.Format)
|
||||||
|
}
|
||||||
|
if len(style) > 0 {
|
||||||
|
imageMogr = "imageMogr2/thumbnail/" + strings.Join(style, "/") + "/ignore-error/1"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// https://cloud.tencent.com/document/product/436/44880
|
|
||||||
style := make([]string, 0, 2)
|
|
||||||
wh := make([]string, 2)
|
|
||||||
if opt.Image.Width > 0 {
|
|
||||||
wh[0] = strconv.Itoa(opt.Image.Width)
|
|
||||||
}
|
|
||||||
if opt.Image.Height > 0 {
|
|
||||||
wh[1] = strconv.Itoa(opt.Image.Height)
|
|
||||||
}
|
|
||||||
if opt.Image.Width > 0 || opt.Image.Height > 0 {
|
|
||||||
style = append(style, strings.Join(wh, "x"))
|
|
||||||
}
|
|
||||||
switch opt.Image.Format {
|
|
||||||
case
|
|
||||||
imagePng,
|
|
||||||
imageJpg,
|
|
||||||
imageJpeg,
|
|
||||||
imageGif,
|
|
||||||
imageWebp:
|
|
||||||
style = append(style, "format/"+opt.Image.Format)
|
|
||||||
}
|
|
||||||
if len(style) > 0 {
|
|
||||||
imageMogr = "imageMogr2/thumbnail/" + strings.Join(style, "/") + "/ignore-error/1"
|
|
||||||
}
|
|
||||||
|
|
||||||
return imageMogr
|
|
||||||
}
|
|
||||||
getQuery := func(opt *s3.AccessURLOption) (query url.Values) {
|
|
||||||
query = make(url.Values)
|
|
||||||
if opt.ContentType != "" {
|
if opt.ContentType != "" {
|
||||||
query.Set("response-content-type", opt.ContentType)
|
query.Set("response-content-type", opt.ContentType)
|
||||||
}
|
}
|
||||||
if opt.Filename != "" {
|
if opt.Filename != "" {
|
||||||
query.Set("response-content-disposition", `attachment; filename=`+strconv.Quote(opt.Filename))
|
query.Set("response-content-disposition", `attachment; filename=`+strconv.Quote(opt.Filename))
|
||||||
}
|
}
|
||||||
|
|
||||||
return query
|
|
||||||
}
|
|
||||||
|
|
||||||
if opt != nil {
|
|
||||||
imageMogr = getImageMogr(opt)
|
|
||||||
query := getQuery(opt)
|
|
||||||
if len(query) > 0 {
|
if len(query) > 0 {
|
||||||
option.Query = &query
|
option.Query = &query
|
||||||
}
|
}
|
||||||
@@ -335,7 +317,6 @@ func (c *Cos) AccessURL(ctx context.Context, name string, expire time.Duration,
|
|||||||
rawURL.RawQuery = rawURL.RawQuery + "&" + imageMogr
|
rawURL.RawQuery = rawURL.RawQuery + "&" + imageMogr
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return rawURL.String(), nil
|
return rawURL.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -343,6 +324,5 @@ func (c *Cos) getPresignedURL(ctx context.Context, name string, expire time.Dura
|
|||||||
if !config.Config.Object.Cos.PublicRead {
|
if !config.Config.Object.Cos.PublicRead {
|
||||||
return c.client.Object.GetPresignedURL(ctx, http.MethodGet, name, c.credential.SecretID, c.credential.SecretKey, expire, opt)
|
return c.client.Object.GetPresignedURL(ctx, http.MethodGet, name, c.credential.SecretID, c.credential.SecretKey, expire, opt)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.client.Object.GetObjectURL(name), nil
|
return c.client.Object.GetObjectURL(name), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ func ImageStat(reader io.Reader) (image.Image, string, error) {
|
|||||||
|
|
||||||
func ImageWidthHeight(img image.Image) (int, int) {
|
func ImageWidthHeight(img image.Image) (int, int) {
|
||||||
bounds := img.Bounds().Max
|
bounds := img.Bounds().Max
|
||||||
|
|
||||||
return bounds.X, bounds.Y
|
return bounds.X, bounds.Y
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,27 +47,27 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image {
|
|||||||
imgWidth := bounds.Max.X
|
imgWidth := bounds.Max.X
|
||||||
imgHeight := bounds.Max.Y
|
imgHeight := bounds.Max.Y
|
||||||
|
|
||||||
// Calculate scaling ratio
|
// 计算缩放比例
|
||||||
scaleWidth := float64(maxWidth) / float64(imgWidth)
|
scaleWidth := float64(maxWidth) / float64(imgWidth)
|
||||||
scaleHeight := float64(maxHeight) / float64(imgHeight)
|
scaleHeight := float64(maxHeight) / float64(imgHeight)
|
||||||
|
|
||||||
// If both maxWidth and maxHeight are 0, return the original image
|
// 如果都为0,则不缩放,返回原始图片
|
||||||
if maxWidth == 0 && maxHeight == 0 {
|
if maxWidth == 0 && maxHeight == 0 {
|
||||||
return img
|
return img
|
||||||
}
|
}
|
||||||
|
|
||||||
// If both maxWidth and maxHeight are greater than 0, choose the smaller scaling ratio to maintain aspect ratio
|
// 如果宽度和高度都大于0,则选择较小的缩放比例,以保持宽高比
|
||||||
if maxWidth > 0 && maxHeight > 0 {
|
if maxWidth > 0 && maxHeight > 0 {
|
||||||
scale := scaleWidth
|
scale := scaleWidth
|
||||||
if scaleHeight < scaleWidth {
|
if scaleHeight < scaleWidth {
|
||||||
scale = scaleHeight
|
scale = scaleHeight
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate thumbnail size
|
// 计算缩略图尺寸
|
||||||
thumbnailWidth := int(float64(imgWidth) * scale)
|
thumbnailWidth := int(float64(imgWidth) * scale)
|
||||||
thumbnailHeight := int(float64(imgHeight) * scale)
|
thumbnailHeight := int(float64(imgHeight) * scale)
|
||||||
|
|
||||||
// Generate thumbnail using the Resample method of the "image" library
|
// 使用"image"库的Resample方法生成缩略图
|
||||||
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
||||||
for y := 0; y < thumbnailHeight; y++ {
|
for y := 0; y < thumbnailHeight; y++ {
|
||||||
for x := 0; x < thumbnailWidth; x++ {
|
for x := 0; x < thumbnailWidth; x++ {
|
||||||
@@ -81,12 +80,12 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image {
|
|||||||
return thumbnail
|
return thumbnail
|
||||||
}
|
}
|
||||||
|
|
||||||
// If only maxWidth or maxHeight is specified, generate thumbnail according to the "max not exceed" rule
|
// 如果只指定了宽度或高度,则根据最大不超过的规则生成缩略图
|
||||||
if maxWidth > 0 {
|
if maxWidth > 0 {
|
||||||
thumbnailWidth := maxWidth
|
thumbnailWidth := maxWidth
|
||||||
thumbnailHeight := int(float64(imgHeight) * scaleWidth)
|
thumbnailHeight := int(float64(imgHeight) * scaleWidth)
|
||||||
|
|
||||||
// Generate thumbnail using the Resample method of the "image" library
|
// 使用"image"库的Resample方法生成缩略图
|
||||||
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
||||||
for y := 0; y < thumbnailHeight; y++ {
|
for y := 0; y < thumbnailHeight; y++ {
|
||||||
for x := 0; x < thumbnailWidth; x++ {
|
for x := 0; x < thumbnailWidth; x++ {
|
||||||
@@ -103,7 +102,7 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image {
|
|||||||
thumbnailWidth := int(float64(imgWidth) * scaleHeight)
|
thumbnailWidth := int(float64(imgWidth) * scaleHeight)
|
||||||
thumbnailHeight := maxHeight
|
thumbnailHeight := maxHeight
|
||||||
|
|
||||||
// Generate thumbnail using the Resample method of the "image" library
|
// 使用"image"库的Resample方法生成缩略图
|
||||||
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
thumbnail := image.NewRGBA(image.Rect(0, 0, thumbnailWidth, thumbnailHeight))
|
||||||
for y := 0; y < thumbnailHeight; y++ {
|
for y := 0; y < thumbnailHeight; y++ {
|
||||||
for x := 0; x < thumbnailWidth; x++ {
|
for x := 0; x < thumbnailWidth; x++ {
|
||||||
@@ -116,6 +115,6 @@ func resizeImage(img image.Image, maxWidth, maxHeight int) image.Image {
|
|||||||
return thumbnail
|
return thumbnail
|
||||||
}
|
}
|
||||||
|
|
||||||
// By default, return the original image
|
// 默认情况下,返回原始图片
|
||||||
return img
|
return img
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,12 +63,13 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func NewMinio() (s3.Interface, error) {
|
func NewMinio() (s3.Interface, error) {
|
||||||
u, err := url.Parse(config.Config.Object.Minio.Endpoint)
|
conf := config.Config.Object.Minio
|
||||||
|
u, err := url.Parse(conf.Endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
opts := &minio.Options{
|
opts := &minio.Options{
|
||||||
Creds: credentials.NewStaticV4(config.Config.Object.Minio.AccessKeyID, config.Config.Object.Minio.SecretAccessKey, config.Config.Object.Minio.SessionToken),
|
Creds: credentials.NewStaticV4(conf.AccessKeyID, conf.SecretAccessKey, conf.SessionToken),
|
||||||
Secure: u.Scheme == "https",
|
Secure: u.Scheme == "https",
|
||||||
}
|
}
|
||||||
client, err := minio.New(u.Host, opts)
|
client, err := minio.New(u.Host, opts)
|
||||||
@@ -76,55 +77,47 @@ func NewMinio() (s3.Interface, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
m := &Minio{
|
m := &Minio{
|
||||||
bucket: config.Config.Object.Minio.Bucket,
|
bucket: conf.Bucket,
|
||||||
core: &minio.Core{Client: client},
|
core: &minio.Core{Client: client},
|
||||||
lock: &sync.Mutex{},
|
lock: &sync.Mutex{},
|
||||||
init: false,
|
init: false,
|
||||||
}
|
}
|
||||||
if config.Config.Object.Minio.SignEndpoint == "" || config.Config.Object.Minio.SignEndpoint == config.Config.Object.Minio.Endpoint {
|
if conf.SignEndpoint == "" || conf.SignEndpoint == conf.Endpoint {
|
||||||
m.opts = opts
|
m.opts = opts
|
||||||
m.sign = m.core.Client
|
m.sign = m.core.Client
|
||||||
m.prefix = u.Path
|
m.bucketURL = conf.Endpoint + "/" + conf.Bucket + "/"
|
||||||
u.Path = ""
|
|
||||||
config.Config.Object.Minio.Endpoint = u.String()
|
|
||||||
m.signEndpoint = config.Config.Object.Minio.Endpoint
|
|
||||||
} else {
|
} else {
|
||||||
su, err := url.Parse(config.Config.Object.Minio.SignEndpoint)
|
su, err := url.Parse(conf.SignEndpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
m.opts = &minio.Options{
|
m.opts = &minio.Options{
|
||||||
Creds: credentials.NewStaticV4(config.Config.Object.Minio.AccessKeyID, config.Config.Object.Minio.SecretAccessKey, config.Config.Object.Minio.SessionToken),
|
Creds: credentials.NewStaticV4(conf.AccessKeyID, conf.SecretAccessKey, conf.SessionToken),
|
||||||
Secure: su.Scheme == "https",
|
Secure: su.Scheme == "https",
|
||||||
}
|
}
|
||||||
m.sign, err = minio.New(su.Host, m.opts)
|
m.sign, err = minio.New(su.Host, m.opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
m.prefix = su.Path
|
m.bucketURL = conf.SignEndpoint + "/" + conf.Bucket + "/"
|
||||||
su.Path = ""
|
|
||||||
config.Config.Object.Minio.SignEndpoint = su.String()
|
|
||||||
m.signEndpoint = config.Config.Object.Minio.SignEndpoint
|
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if err := m.initMinio(ctx); err != nil {
|
if err := m.initMinio(ctx); err != nil {
|
||||||
fmt.Println("init minio error:", err)
|
fmt.Println("init minio error:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Minio struct {
|
type Minio struct {
|
||||||
bucket string
|
bucket string
|
||||||
signEndpoint string
|
bucketURL string
|
||||||
location string
|
location string
|
||||||
opts *minio.Options
|
opts *minio.Options
|
||||||
core *minio.Core
|
core *minio.Core
|
||||||
sign *minio.Client
|
sign *minio.Client
|
||||||
lock sync.Locker
|
lock sync.Locker
|
||||||
init bool
|
init bool
|
||||||
prefix string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Minio) initMinio(ctx context.Context) error {
|
func (m *Minio) initMinio(ctx context.Context) error {
|
||||||
@@ -142,9 +135,8 @@ func (m *Minio) initMinio(ctx context.Context) error {
|
|||||||
return fmt.Errorf("check bucket exists error: %w", err)
|
return fmt.Errorf("check bucket exists error: %w", err)
|
||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
err2 := m.core.Client.MakeBucket(ctx, conf.Bucket, minio.MakeBucketOptions{})
|
if err := m.core.Client.MakeBucket(ctx, conf.Bucket, minio.MakeBucketOptions{}); err != nil {
|
||||||
if err2 != nil {
|
return fmt.Errorf("make bucket error: %w", err)
|
||||||
return fmt.Errorf("make bucket error: %w", err2)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if conf.PublicRead {
|
if conf.PublicRead {
|
||||||
@@ -152,9 +144,8 @@ func (m *Minio) initMinio(ctx context.Context) error {
|
|||||||
`{"Version": "2012-10-17","Statement": [{"Action": ["s3:GetObject","s3:PutObject"],"Effect": "Allow","Principal": {"AWS": ["*"]},"Resource": ["arn:aws:s3:::%s/*"],"Sid": ""}]}`,
|
`{"Version": "2012-10-17","Statement": [{"Action": ["s3:GetObject","s3:PutObject"],"Effect": "Allow","Principal": {"AWS": ["*"]},"Resource": ["arn:aws:s3:::%s/*"],"Sid": ""}]}`,
|
||||||
conf.Bucket,
|
conf.Bucket,
|
||||||
)
|
)
|
||||||
err2 := m.core.Client.SetBucketPolicy(ctx, conf.Bucket, policy)
|
if err := m.core.Client.SetBucketPolicy(ctx, conf.Bucket, policy); err != nil {
|
||||||
if err2 != nil {
|
return err
|
||||||
return err2
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m.location, err = m.core.Client.GetBucketLocation(ctx, conf.Bucket)
|
m.location, err = m.core.Client.GetBucketLocation(ctx, conf.Bucket)
|
||||||
@@ -185,7 +176,6 @@ func (m *Minio) initMinio(ctx context.Context) error {
|
|||||||
vblc.Elem().Elem().Interface().(interface{ Set(string, string) }).Set(conf.Bucket, m.location)
|
vblc.Elem().Elem().Interface().(interface{ Set(string, string) }).Set(conf.Bucket, m.location)
|
||||||
}()
|
}()
|
||||||
m.init = true
|
m.init = true
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,7 +199,6 @@ func (m *Minio) InitiateMultipartUpload(ctx context.Context, name string) (*s3.I
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &s3.InitiateMultipartUploadResult{
|
return &s3.InitiateMultipartUploadResult{
|
||||||
Bucket: m.bucket,
|
Bucket: m.bucket,
|
||||||
Key: name,
|
Key: name,
|
||||||
@@ -232,7 +221,6 @@ func (m *Minio) CompleteMultipartUpload(ctx context.Context, uploadID string, na
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &s3.CompleteMultipartUploadResult{
|
return &s3.CompleteMultipartUploadResult{
|
||||||
Location: upload.Location,
|
Location: upload.Location,
|
||||||
Bucket: upload.Bucket,
|
Bucket: upload.Bucket,
|
||||||
@@ -255,7 +243,6 @@ func (m *Minio) PartSize(ctx context.Context, size int64) (int64, error) {
|
|||||||
if size%maxNumSize != 0 {
|
if size%maxNumSize != 0 {
|
||||||
partSize++
|
partSize++
|
||||||
}
|
}
|
||||||
|
|
||||||
return partSize, nil
|
return partSize, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,7 +255,7 @@ func (m *Minio) AuthSign(ctx context.Context, uploadID string, name string, expi
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
result := s3.AuthSignResult{
|
result := s3.AuthSignResult{
|
||||||
URL: m.signEndpoint + "/" + m.bucket + "/" + name,
|
URL: m.bucketURL + name,
|
||||||
Query: url.Values{"uploadId": {uploadID}},
|
Query: url.Values{"uploadId": {uploadID}},
|
||||||
Parts: make([]s3.SignPart, len(partNumbers)),
|
Parts: make([]s3.SignPart, len(partNumbers)),
|
||||||
}
|
}
|
||||||
@@ -282,14 +269,11 @@ func (m *Minio) AuthSign(ctx context.Context, uploadID string, name string, expi
|
|||||||
request = signer.SignV4Trailer(*request, creds.AccessKeyID, creds.SecretAccessKey, creds.SessionToken, m.location, nil)
|
request = signer.SignV4Trailer(*request, creds.AccessKeyID, creds.SecretAccessKey, creds.SessionToken, m.location, nil)
|
||||||
result.Parts[i] = s3.SignPart{
|
result.Parts[i] = s3.SignPart{
|
||||||
PartNumber: partNumber,
|
PartNumber: partNumber,
|
||||||
|
URL: request.URL.String(),
|
||||||
Query: url.Values{"partNumber": {strconv.Itoa(partNumber)}},
|
Query: url.Values{"partNumber": {strconv.Itoa(partNumber)}},
|
||||||
Header: request.Header,
|
Header: request.Header,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if m.prefix != "" {
|
|
||||||
result.URL = m.signEndpoint + m.prefix + "/" + m.bucket + "/" + name
|
|
||||||
}
|
|
||||||
|
|
||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -301,10 +285,6 @@ func (m *Minio) PresignedPutObject(ctx context.Context, name string, expire time
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if m.prefix != "" {
|
|
||||||
rawURL.Path = path.Join(m.prefix, rawURL.Path)
|
|
||||||
}
|
|
||||||
|
|
||||||
return rawURL.String(), nil
|
return rawURL.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -312,7 +292,6 @@ func (m *Minio) DeleteObject(ctx context.Context, name string) error {
|
|||||||
if err := m.initMinio(ctx); err != nil {
|
if err := m.initMinio(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.core.Client.RemoveObject(ctx, m.bucket, name, minio.RemoveObjectOptions{})
|
return m.core.Client.RemoveObject(ctx, m.bucket, name, minio.RemoveObjectOptions{})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -324,7 +303,6 @@ func (m *Minio) StatObject(ctx context.Context, name string) (*s3.ObjectInfo, er
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &s3.ObjectInfo{
|
return &s3.ObjectInfo{
|
||||||
ETag: strings.ToLower(info.ETag),
|
ETag: strings.ToLower(info.ETag),
|
||||||
Key: info.Key,
|
Key: info.Key,
|
||||||
@@ -347,7 +325,6 @@ func (m *Minio) CopyObject(ctx context.Context, src string, dst string) (*s3.Cop
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &s3.CopyObjectInfo{
|
return &s3.CopyObjectInfo{
|
||||||
Key: dst,
|
Key: dst,
|
||||||
ETag: strings.ToLower(result.ETag),
|
ETag: strings.ToLower(result.ETag),
|
||||||
@@ -358,23 +335,20 @@ func (m *Minio) IsNotFound(err error) bool {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
var minioErr minio.ErrorResponse
|
switch e := err.(type) {
|
||||||
if errors.As(err, &minio.ErrorResponse{}) {
|
case minio.ErrorResponse:
|
||||||
return minioErr.StatusCode == http.StatusNotFound || minioErr.Code == "NoSuchKey"
|
return e.StatusCode == http.StatusNotFound || e.Code == "NoSuchKey"
|
||||||
|
case *minio.ErrorResponse:
|
||||||
|
return e.StatusCode == http.StatusNotFound || e.Code == "NoSuchKey"
|
||||||
|
default:
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
var minioErr2 *minio.ErrorResponse
|
|
||||||
if errors.As(err, &minioErr2) {
|
|
||||||
return minioErr2.StatusCode == http.StatusNotFound || minioErr2.Code == "NoSuchKey"
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Minio) AbortMultipartUpload(ctx context.Context, uploadID string, name string) error {
|
func (m *Minio) AbortMultipartUpload(ctx context.Context, uploadID string, name string) error {
|
||||||
if err := m.initMinio(ctx); err != nil {
|
if err := m.initMinio(ctx); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return m.core.AbortMultipartUpload(ctx, m.bucket, name, uploadID)
|
return m.core.AbortMultipartUpload(ctx, m.bucket, name, uploadID)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -401,7 +375,6 @@ func (m *Minio) ListUploadedParts(ctx context.Context, uploadID string, name str
|
|||||||
Size: part.Size,
|
Size: part.Size,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -423,20 +396,14 @@ func (m *Minio) presignedGetObject(ctx context.Context, name string, expire time
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if m.prefix != "" {
|
|
||||||
rawURL.Path = path.Join(m.prefix, rawURL.Path)
|
|
||||||
}
|
|
||||||
|
|
||||||
return rawURL.String(), nil
|
return rawURL.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Minio) getImageInfoForAccessURL(
|
func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
|
||||||
ctx context.Context,
|
if err := m.initMinio(ctx); err != nil {
|
||||||
name string,
|
return "", err
|
||||||
expire time.Duration,
|
}
|
||||||
opt *s3.AccessURLOption,
|
reqParams := make(url.Values)
|
||||||
reqParams url.Values,
|
|
||||||
) (fileInfo *s3.ObjectInfo, objectInfoPath, msg string, err error) {
|
|
||||||
if opt != nil {
|
if opt != nil {
|
||||||
if opt.ContentType != "" {
|
if opt.ContentType != "" {
|
||||||
reqParams.Set("response-content-type", opt.ContentType)
|
reqParams.Set("response-content-type", opt.ContentType)
|
||||||
@@ -446,47 +413,35 @@ func (m *Minio) getImageInfoForAccessURL(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if opt.Image == nil || (opt.Image.Width < 0 && opt.Image.Height < 0 && opt.Image.Format == "") || (opt.Image.Width > maxImageWidth || opt.Image.Height > maxImageHeight) {
|
if opt.Image == nil || (opt.Image.Width < 0 && opt.Image.Height < 0 && opt.Image.Format == "") || (opt.Image.Width > maxImageWidth || opt.Image.Height > maxImageHeight) {
|
||||||
msg, err = m.presignedGetObject(ctx, name, expire, reqParams)
|
return m.presignedGetObject(ctx, name, expire, reqParams)
|
||||||
|
|
||||||
return nil, "", msg, err
|
|
||||||
}
|
}
|
||||||
fileInfo, err = m.StatObject(ctx, name)
|
fileInfo, err := m.StatObject(ctx, name)
|
||||||
objectInfoPath = path.Join(pathInfo, fileInfo.ETag, "image.json")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", msg, err
|
return "", err
|
||||||
}
|
}
|
||||||
if fileInfo.Size > maxImageSize {
|
if fileInfo.Size > maxImageSize {
|
||||||
return nil, "", "", errors.New("file size too large")
|
return "", errors.New("file size too large")
|
||||||
}
|
}
|
||||||
|
objectInfoPath := path.Join(pathInfo, fileInfo.ETag, "image.json")
|
||||||
return fileInfo, objectInfoPath, "", nil
|
var (
|
||||||
}
|
img image.Image
|
||||||
|
info minioImageInfo
|
||||||
func (m *Minio) loadImgDataForAccessURL(objectInfoPath string, ctx context.Context, name string, info *minioImageInfo) (img image.Image, msg string, err error) {
|
)
|
||||||
var data []byte
|
data, err := m.getObjectData(ctx, objectInfoPath, 1024)
|
||||||
data, err = m.getObjectData(ctx, objectInfoPath, 1024)
|
|
||||||
|
|
||||||
//nolint:nestif //easy enough to understand
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = json.Unmarshal(data, &info)
|
if err := json.Unmarshal(data, &info); err != nil {
|
||||||
if err != nil {
|
return "", fmt.Errorf("unmarshal minio image info.json error: %w", err)
|
||||||
return nil, "", fmt.Errorf("unmarshal minio image info.json error: %w", err)
|
|
||||||
}
|
}
|
||||||
if info.NotImage {
|
if info.NotImage {
|
||||||
return nil, "", errors.New("not image")
|
return "", errors.New("not image")
|
||||||
}
|
}
|
||||||
} else if m.IsNotFound(err) {
|
} else if m.IsNotFound(err) {
|
||||||
var reader *minio.Object
|
reader, err := m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{})
|
||||||
reader, err = m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return img, msg, err
|
return "", err
|
||||||
}
|
}
|
||||||
defer reader.Close()
|
defer reader.Close()
|
||||||
var (
|
imageInfo, format, err := ImageStat(reader)
|
||||||
imageInfo image.Image
|
|
||||||
format string
|
|
||||||
)
|
|
||||||
imageInfo, format, err = ImageStat(reader)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
info.NotImage = false
|
info.NotImage = false
|
||||||
info.Format = format
|
info.Format = format
|
||||||
@@ -495,22 +450,16 @@ func (m *Minio) loadImgDataForAccessURL(objectInfoPath string, ctx context.Conte
|
|||||||
} else {
|
} else {
|
||||||
info.NotImage = true
|
info.NotImage = true
|
||||||
}
|
}
|
||||||
|
data, err := json.Marshal(&info)
|
||||||
data, err = json.Marshal(&info)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return img, msg, err
|
return "", err
|
||||||
}
|
}
|
||||||
|
if _, err := m.core.Client.PutObject(ctx, m.bucket, objectInfoPath, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{}); err != nil {
|
||||||
_, err = m.core.Client.PutObject(ctx, m.bucket, objectInfoPath, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{})
|
return "", err
|
||||||
if err != nil {
|
|
||||||
return img, msg, err
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return img, msg, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Minio) formatImgInfoForAccessURL(opt *s3.AccessURLOption, info *minioImageInfo, reqParams url.Values) {
|
|
||||||
if opt.Image.Width > info.Width || opt.Image.Width <= 0 {
|
if opt.Image.Width > info.Width || opt.Image.Width <= 0 {
|
||||||
opt.Image.Width = info.Width
|
opt.Image.Width = info.Width
|
||||||
}
|
}
|
||||||
@@ -533,24 +482,24 @@ func (m *Minio) formatImgInfoForAccessURL(opt *s3.AccessURLOption, info *minioIm
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
reqParams.Set("response-content-type", "image/"+opt.Image.Format)
|
reqParams.Set("response-content-type", "image/"+opt.Image.Format)
|
||||||
}
|
if opt.Image.Width == info.Width && opt.Image.Height == info.Height && opt.Image.Format == info.Format {
|
||||||
|
return m.presignedGetObject(ctx, name, expire, reqParams)
|
||||||
func (m *Minio) cacheImgInfoForAccessURL(ctx context.Context, name, cacheKey string, img image.Image, expire time.Duration, opt *s3.AccessURLOption, reqParams url.Values) (string, error) {
|
}
|
||||||
_, err := m.core.Client.StatObject(ctx, m.bucket, cacheKey, minio.StatObjectOptions{})
|
cacheKey := filepath.Join(pathInfo, fileInfo.ETag, fmt.Sprintf("image_w%d_h%d.%s", opt.Image.Width, opt.Image.Height, opt.Image.Format))
|
||||||
if err == nil {
|
if _, err := m.core.Client.StatObject(ctx, m.bucket, cacheKey, minio.StatObjectOptions{}); err == nil {
|
||||||
return m.presignedGetObject(ctx, cacheKey, expire, reqParams)
|
return m.presignedGetObject(ctx, cacheKey, expire, reqParams)
|
||||||
} else if !m.IsNotFound(err) {
|
} else if !m.IsNotFound(err) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if img == nil {
|
if img == nil {
|
||||||
reader, err2 := m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{})
|
reader, err := m.core.Client.GetObject(ctx, m.bucket, name, minio.GetObjectOptions{})
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
return "", err2
|
return "", err
|
||||||
}
|
}
|
||||||
defer reader.Close()
|
defer reader.Close()
|
||||||
img, _, err2 = ImageStat(reader)
|
img, _, err = ImageStat(reader)
|
||||||
if err2 != nil {
|
if err != nil {
|
||||||
return "", err2
|
return "", err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
thumbnail := resizeImage(img, opt.Image.Width, opt.Image.Height)
|
thumbnail := resizeImage(img, opt.Image.Width, opt.Image.Height)
|
||||||
@@ -563,48 +512,9 @@ func (m *Minio) cacheImgInfoForAccessURL(ctx context.Context, name, cacheKey str
|
|||||||
case formatGif:
|
case formatGif:
|
||||||
err = gif.Encode(buf, thumbnail, nil)
|
err = gif.Encode(buf, thumbnail, nil)
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if _, err := m.core.Client.PutObject(ctx, m.bucket, cacheKey, buf, int64(buf.Len()), minio.PutObjectOptions{}); err != nil {
|
if _, err := m.core.Client.PutObject(ctx, m.bucket, cacheKey, buf, int64(buf.Len()), minio.PutObjectOptions{}); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
|
|
||||||
errInit := m.initMinio(ctx)
|
|
||||||
if errInit != nil {
|
|
||||||
return "", errInit
|
|
||||||
}
|
|
||||||
reqParams := make(url.Values)
|
|
||||||
fileInfo, objectInfoPath, msg, err := m.getImageInfoForAccessURL(ctx, name, expire, opt, reqParams)
|
|
||||||
if err != nil {
|
|
||||||
return msg, err
|
|
||||||
}
|
|
||||||
// load-cache img data
|
|
||||||
var (
|
|
||||||
img image.Image
|
|
||||||
info minioImageInfo
|
|
||||||
)
|
|
||||||
img, msg, err = m.loadImgDataForAccessURL(objectInfoPath, ctx, name, &info)
|
|
||||||
if err != nil {
|
|
||||||
return msg, err
|
|
||||||
}
|
|
||||||
// format img info
|
|
||||||
m.formatImgInfoForAccessURL(opt, &info, reqParams)
|
|
||||||
// no need resize
|
|
||||||
if opt.Image.Width == info.Width && opt.Image.Height == info.Height && opt.Image.Format == info.Format {
|
|
||||||
return m.presignedGetObject(ctx, name, expire, reqParams)
|
|
||||||
}
|
|
||||||
// cache img
|
|
||||||
cacheKey := filepath.Join(pathInfo, fileInfo.ETag, fmt.Sprintf("image_w%d_h%d.%s", opt.Image.Width, opt.Image.Height, opt.Image.Format))
|
|
||||||
msg, err = m.cacheImgInfoForAccessURL(ctx, name, cacheKey, img, expire, opt, reqParams)
|
|
||||||
if err != nil {
|
|
||||||
return msg, err
|
|
||||||
}
|
|
||||||
// return cache img
|
|
||||||
return m.presignedGetObject(ctx, cacheKey, expire, reqParams)
|
return m.presignedGetObject(ctx, cacheKey, expire, reqParams)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -617,6 +527,5 @@ func (m *Minio) getObjectData(ctx context.Context, name string, limit int64) ([]
|
|||||||
if limit < 0 {
|
if limit < 0 {
|
||||||
return io.ReadAll(object)
|
return io.ReadAll(object)
|
||||||
}
|
}
|
||||||
|
|
||||||
return io.ReadAll(io.LimitReader(object, 1024))
|
return io.ReadAll(io.LimitReader(object, 1024))
|
||||||
}
|
}
|
||||||
|
|||||||
119
pkg/common/db/s3/oss/oss.go
Executable file → Normal file
119
pkg/common/db/s3/oss/oss.go
Executable file → Normal file
@@ -45,6 +45,11 @@ const (
|
|||||||
imageWebp = "webp"
|
imageWebp = "webp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
videoSnapshotImagePng = "png"
|
||||||
|
videoSnapshotImageJpg = "jpg"
|
||||||
|
)
|
||||||
|
|
||||||
func NewOSS() (s3.Interface, error) {
|
func NewOSS() (s3.Interface, error) {
|
||||||
conf := config.Config.Object.Oss
|
conf := config.Config.Object.Oss
|
||||||
if conf.BucketURL == "" {
|
if conf.BucketURL == "" {
|
||||||
@@ -61,7 +66,6 @@ func NewOSS() (s3.Interface, error) {
|
|||||||
if conf.BucketURL[len(conf.BucketURL)-1] != '/' {
|
if conf.BucketURL[len(conf.BucketURL)-1] != '/' {
|
||||||
conf.BucketURL += "/"
|
conf.BucketURL += "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
return &OSS{
|
return &OSS{
|
||||||
bucketURL: conf.BucketURL,
|
bucketURL: conf.BucketURL,
|
||||||
bucket: bucket,
|
bucket: bucket,
|
||||||
@@ -94,7 +98,6 @@ func (o *OSS) InitiateMultipartUpload(ctx context.Context, name string) (*s3.Ini
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &s3.InitiateMultipartUploadResult{
|
return &s3.InitiateMultipartUploadResult{
|
||||||
UploadID: result.UploadID,
|
UploadID: result.UploadID,
|
||||||
Bucket: result.Bucket,
|
Bucket: result.Bucket,
|
||||||
@@ -118,7 +121,6 @@ func (o *OSS) CompleteMultipartUpload(ctx context.Context, uploadID string, name
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &s3.CompleteMultipartUploadResult{
|
return &s3.CompleteMultipartUploadResult{
|
||||||
Location: result.Location,
|
Location: result.Location,
|
||||||
Bucket: result.Bucket,
|
Bucket: result.Bucket,
|
||||||
@@ -141,7 +143,6 @@ func (o *OSS) PartSize(ctx context.Context, size int64) (int64, error) {
|
|||||||
if size%maxNumSize != 0 {
|
if size%maxNumSize != 0 {
|
||||||
partSize++
|
partSize++
|
||||||
}
|
}
|
||||||
|
|
||||||
return partSize, nil
|
return partSize, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -154,7 +155,7 @@ func (o *OSS) AuthSign(ctx context.Context, uploadID string, name string, expire
|
|||||||
}
|
}
|
||||||
for i, partNumber := range partNumbers {
|
for i, partNumber := range partNumbers {
|
||||||
rawURL := fmt.Sprintf(`%s%s?partNumber=%d&uploadId=%s`, o.bucketURL, name, partNumber, uploadID)
|
rawURL := fmt.Sprintf(`%s%s?partNumber=%d&uploadId=%s`, o.bucketURL, name, partNumber, uploadID)
|
||||||
request, err := http.NewRequestWithContext(context.Background(), http.MethodPut, rawURL, nil)
|
request, err := http.NewRequest(http.MethodPut, rawURL, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -174,7 +175,6 @@ func (o *OSS) AuthSign(ctx context.Context, uploadID string, name string, expire
|
|||||||
Header: request.Header,
|
Header: request.Header,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &result, nil
|
return &result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,26 +191,25 @@ func (o *OSS) StatObject(ctx context.Context, name string) (*s3.ObjectInfo, erro
|
|||||||
if res.ETag = strings.ToLower(strings.ReplaceAll(header.Get("ETag"), `"`, ``)); res.ETag == "" {
|
if res.ETag = strings.ToLower(strings.ReplaceAll(header.Get("ETag"), `"`, ``)); res.ETag == "" {
|
||||||
return nil, errors.New("StatObject etag not found")
|
return nil, errors.New("StatObject etag not found")
|
||||||
}
|
}
|
||||||
contentLengthStr := header.Get("Content-Length")
|
if contentLengthStr := header.Get("Content-Length"); contentLengthStr == "" {
|
||||||
if contentLengthStr == "" {
|
|
||||||
return nil, errors.New("StatObject content-length not found")
|
return nil, errors.New("StatObject content-length not found")
|
||||||
|
} else {
|
||||||
|
res.Size, err = strconv.ParseInt(contentLengthStr, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("StatObject content-length parse error: %w", err)
|
||||||
|
}
|
||||||
|
if res.Size < 0 {
|
||||||
|
return nil, errors.New("StatObject content-length must be greater than 0")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
res.Size, err = strconv.ParseInt(contentLengthStr, 10, 64)
|
if lastModified := header.Get("Last-Modified"); lastModified == "" {
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("StatObject content-length parse error: %w", err)
|
|
||||||
}
|
|
||||||
if res.Size < 0 {
|
|
||||||
return nil, errors.New("StatObject content-length must be greater than 0")
|
|
||||||
}
|
|
||||||
lastModified := header.Get("Last-Modified")
|
|
||||||
if lastModified == "" {
|
|
||||||
return nil, errors.New("StatObject last-modified not found")
|
return nil, errors.New("StatObject last-modified not found")
|
||||||
|
} else {
|
||||||
|
res.LastModified, err = time.Parse(http.TimeFormat, lastModified)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("StatObject last-modified parse error: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
res.LastModified, err = time.Parse(http.TimeFormat, lastModified)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("StatObject last-modified parse error: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,7 +222,6 @@ func (o *OSS) CopyObject(ctx context.Context, src string, dst string) (*s3.CopyO
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &s3.CopyObjectInfo{
|
return &s3.CopyObjectInfo{
|
||||||
Key: dst,
|
Key: dst,
|
||||||
ETag: strings.ToLower(strings.ReplaceAll(result.ETag, `"`, ``)),
|
ETag: strings.ToLower(strings.ReplaceAll(result.ETag, `"`, ``)),
|
||||||
@@ -231,7 +229,6 @@ func (o *OSS) CopyObject(ctx context.Context, src string, dst string) (*s3.CopyO
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (o *OSS) IsNotFound(err error) bool {
|
func (o *OSS) IsNotFound(err error) bool {
|
||||||
//nolint:errorlint //this is exactly what we want,there is no risk for no wrapped errors
|
|
||||||
switch e := err.(type) {
|
switch e := err.(type) {
|
||||||
case oss.ServiceError:
|
case oss.ServiceError:
|
||||||
return e.StatusCode == http.StatusNotFound || e.Code == "NoSuchKey"
|
return e.StatusCode == http.StatusNotFound || e.Code == "NoSuchKey"
|
||||||
@@ -274,7 +271,6 @@ func (o *OSS) ListUploadedParts(ctx context.Context, uploadID string, name strin
|
|||||||
Size: int64(part.Size),
|
Size: int64(part.Size),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -282,7 +278,39 @@ func (o *OSS) AccessURL(ctx context.Context, name string, expire time.Duration,
|
|||||||
publicRead := config.Config.Object.Oss.PublicRead
|
publicRead := config.Config.Object.Oss.PublicRead
|
||||||
var opts []oss.Option
|
var opts []oss.Option
|
||||||
if opt != nil {
|
if opt != nil {
|
||||||
opts = optsForAccessURL(opt, opts, publicRead)
|
if opt.Image != nil {
|
||||||
|
// 文档地址: https://help.aliyun.com/zh/oss/user-guide/resize-images-4?spm=a2c4g.11186623.0.0.4b3b1e4fWW6yji
|
||||||
|
var format string
|
||||||
|
switch opt.Image.Format {
|
||||||
|
case
|
||||||
|
imagePng,
|
||||||
|
imageJpg,
|
||||||
|
imageJpeg,
|
||||||
|
imageGif,
|
||||||
|
imageWebp:
|
||||||
|
format = opt.Image.Format
|
||||||
|
default:
|
||||||
|
opt.Image.Format = imageJpg
|
||||||
|
}
|
||||||
|
// https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,h_100,m_lfit
|
||||||
|
process := "image/resize,m_lfit"
|
||||||
|
if opt.Image.Width > 0 {
|
||||||
|
process += ",w_" + strconv.Itoa(opt.Image.Width)
|
||||||
|
}
|
||||||
|
if opt.Image.Height > 0 {
|
||||||
|
process += ",h_" + strconv.Itoa(opt.Image.Height)
|
||||||
|
}
|
||||||
|
process += ",format," + format
|
||||||
|
opts = append(opts, oss.Process(process))
|
||||||
|
}
|
||||||
|
if !publicRead {
|
||||||
|
if opt.ContentType != "" {
|
||||||
|
opts = append(opts, oss.ResponseContentType(opt.ContentType))
|
||||||
|
}
|
||||||
|
if opt.Filename != "" {
|
||||||
|
opts = append(opts, oss.ResponseContentDisposition(`attachment; filename=`+strconv.Quote(opt.Filename)))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if expire <= 0 {
|
if expire <= 0 {
|
||||||
expire = time.Hour * 24 * 365 * 99 // 99 years
|
expire = time.Hour * 24 * 365 * 99 // 99 years
|
||||||
@@ -297,44 +325,5 @@ func (o *OSS) AccessURL(ctx context.Context, name string, expire time.Duration,
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
params := getURLParams(*o.bucket.Client.Conn, rawParams)
|
params := getURLParams(*o.bucket.Client.Conn, rawParams)
|
||||||
|
|
||||||
return getURL(o.um, o.bucket.BucketName, name, params).String(), nil
|
return getURL(o.um, o.bucket.BucketName, name, params).String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func optsForAccessURL(opt *s3.AccessURLOption, opts []oss.Option, publicRead bool) []oss.Option {
|
|
||||||
if opt.Image != nil {
|
|
||||||
// 文档地址: https://help.aliyun.com/zh/oss/user-guide/resize-images-4?spm=a2c4g.11186623.0.0.4b3b1e4fWW6yji
|
|
||||||
var format string
|
|
||||||
switch opt.Image.Format {
|
|
||||||
case
|
|
||||||
imagePng,
|
|
||||||
imageJpg,
|
|
||||||
imageJpeg,
|
|
||||||
imageGif,
|
|
||||||
imageWebp:
|
|
||||||
format = opt.Image.Format
|
|
||||||
default:
|
|
||||||
opt.Image.Format = imageJpg
|
|
||||||
}
|
|
||||||
// https://oss-console-img-demo-cn-hangzhou.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,h_100,m_lfit
|
|
||||||
process := "image/resize,m_lfit"
|
|
||||||
if opt.Image.Width > 0 {
|
|
||||||
process += ",w_" + strconv.Itoa(opt.Image.Width)
|
|
||||||
}
|
|
||||||
if opt.Image.Height > 0 {
|
|
||||||
process += ",h_" + strconv.Itoa(opt.Image.Height)
|
|
||||||
}
|
|
||||||
process += ",format," + format
|
|
||||||
opts = append(opts, oss.Process(process))
|
|
||||||
}
|
|
||||||
if !publicRead {
|
|
||||||
if opt.ContentType != "" {
|
|
||||||
opts = append(opts, oss.ResponseContentType(opt.ContentType))
|
|
||||||
}
|
|
||||||
if opt.Filename != "" {
|
|
||||||
opts = append(opts, oss.ResponseContentDisposition(`attachment; filename=`+strconv.Quote(opt.Filename)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return opts
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ type GroupModel struct {
|
|||||||
Introduction string `gorm:"column:introduction;size:255" json:"introduction"`
|
Introduction string `gorm:"column:introduction;size:255" json:"introduction"`
|
||||||
FaceURL string `gorm:"column:face_url;size:255" json:"faceURL"`
|
FaceURL string `gorm:"column:face_url;size:255" json:"faceURL"`
|
||||||
CreateTime time.Time `gorm:"column:create_time;index:create_time;autoCreateTime"`
|
CreateTime time.Time `gorm:"column:create_time;index:create_time;autoCreateTime"`
|
||||||
Ex string `gorm:"column:ex;size:1024" json:"ex"`
|
Ex string `gorm:"column:ex" json:"ex;size:1024"`
|
||||||
Status int32 `gorm:"column:status"`
|
Status int32 `gorm:"column:status"`
|
||||||
CreatorUserID string `gorm:"column:creator_user_id;size:64"`
|
CreatorUserID string `gorm:"column:creator_user_id;size:64"`
|
||||||
GroupType int32 `gorm:"column:group_type"`
|
GroupType int32 `gorm:"column:group_type"`
|
||||||
|
|||||||
@@ -15,8 +15,6 @@
|
|||||||
package relation
|
package relation
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"github.com/OpenIMSDK/tools/utils"
|
"github.com/OpenIMSDK/tools/utils"
|
||||||
@@ -34,5 +32,5 @@ type GroupSimpleUserID struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func IsNotFound(err error) bool {
|
func IsNotFound(err error) bool {
|
||||||
return errors.Is(utils.Unwrap(err), gorm.ErrRecordNotFound)
|
return utils.Unwrap(err) == gorm.ErrRecordNotFound
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -150,7 +150,6 @@ func (m *MsgDocModel) IsFull() bool {
|
|||||||
|
|
||||||
func (m MsgDocModel) GetDocID(conversationID string, seq int64) string {
|
func (m MsgDocModel) GetDocID(conversationID string, seq int64) string {
|
||||||
seqSuffix := (seq - 1) / singleGocMsgNum
|
seqSuffix := (seq - 1) / singleGocMsgNum
|
||||||
|
|
||||||
return m.indexGen(conversationID, seqSuffix)
|
return m.indexGen(conversationID, seqSuffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,7 +164,6 @@ func (m MsgDocModel) GetDocIDSeqsMap(conversationID string, seqs []int64) map[st
|
|||||||
t[docID] = append(value, seqs[i])
|
t[docID] = append(value, seqs[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,6 +181,5 @@ func (MsgDocModel) GenExceptionMessageBySeqs(seqs []int64) (exceptionMsg []*sdkw
|
|||||||
msgModel.Seq = v
|
msgModel.Seq = v
|
||||||
exceptionMsg = append(exceptionMsg, msgModel)
|
exceptionMsg = append(exceptionMsg, msgModel)
|
||||||
}
|
}
|
||||||
|
|
||||||
return exceptionMsg
|
return exceptionMsg
|
||||||
}
|
}
|
||||||
|
|||||||
54
pkg/common/db/unrelation/mongo.go
Executable file → Normal file
54
pkg/common/db/unrelation/mongo.go
Executable file → Normal file
@@ -16,7 +16,6 @@ package unrelation
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -45,12 +44,27 @@ type Mongo struct {
|
|||||||
// NewMongo Initialize MongoDB connection.
|
// NewMongo Initialize MongoDB connection.
|
||||||
func NewMongo() (*Mongo, error) {
|
func NewMongo() (*Mongo, error) {
|
||||||
specialerror.AddReplace(mongo.ErrNoDocuments, errs.ErrRecordNotFound)
|
specialerror.AddReplace(mongo.ErrNoDocuments, errs.ErrRecordNotFound)
|
||||||
// uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority"
|
uri := "mongodb://sample.host:27017/?maxPoolSize=20&w=majority"
|
||||||
var uri string
|
|
||||||
if config.Config.Mongo.Uri != "" {
|
if config.Config.Mongo.Uri != "" {
|
||||||
uri = config.Config.Mongo.Uri
|
uri = config.Config.Mongo.Uri
|
||||||
} else {
|
} else {
|
||||||
uri = defaultMongoUriForNewMongo()
|
mongodbHosts := ""
|
||||||
|
for i, v := range config.Config.Mongo.Address {
|
||||||
|
if i == len(config.Config.Mongo.Address)-1 {
|
||||||
|
mongodbHosts += v
|
||||||
|
} else {
|
||||||
|
mongodbHosts += v + ","
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if config.Config.Mongo.Password != "" && config.Config.Mongo.Username != "" {
|
||||||
|
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin",
|
||||||
|
config.Config.Mongo.Username, config.Config.Mongo.Password, mongodbHosts,
|
||||||
|
config.Config.Mongo.Database, config.Config.Mongo.MaxPoolSize)
|
||||||
|
} else {
|
||||||
|
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin",
|
||||||
|
mongodbHosts, config.Config.Mongo.Database,
|
||||||
|
config.Config.Mongo.MaxPoolSize)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fmt.Println("mongo:", uri)
|
fmt.Println("mongo:", uri)
|
||||||
var mongoClient *mongo.Client
|
var mongoClient *mongo.Client
|
||||||
@@ -62,41 +76,17 @@ func NewMongo() (*Mongo, error) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
return &Mongo{db: mongoClient}, nil
|
return &Mongo{db: mongoClient}, nil
|
||||||
}
|
}
|
||||||
var cmdErr mongo.CommandError
|
if cmdErr, ok := err.(mongo.CommandError); ok {
|
||||||
if errors.As(err, &cmdErr) {
|
|
||||||
if cmdErr.Code == 13 || cmdErr.Code == 18 {
|
if cmdErr.Code == 13 || cmdErr.Code == 18 {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Failed to connect to MongoDB: %s\n", err)
|
||||||
}
|
}
|
||||||
fmt.Printf("Failed to connect to MongoDB: %s\n", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func defaultMongoUriForNewMongo() string {
|
|
||||||
var uri string
|
|
||||||
mongodbHosts := ""
|
|
||||||
for i, v := range config.Config.Mongo.Address {
|
|
||||||
if i == len(config.Config.Mongo.Address)-1 {
|
|
||||||
mongodbHosts += v
|
|
||||||
} else {
|
|
||||||
mongodbHosts += v + ","
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if config.Config.Mongo.Password != "" && config.Config.Mongo.Username != "" {
|
|
||||||
uri = fmt.Sprintf("mongodb://%s:%s@%s/%s?maxPoolSize=%d&authSource=admin",
|
|
||||||
config.Config.Mongo.Username, config.Config.Mongo.Password, mongodbHosts,
|
|
||||||
config.Config.Mongo.Database, config.Config.Mongo.MaxPoolSize)
|
|
||||||
} else {
|
|
||||||
uri = fmt.Sprintf("mongodb://%s/%s/?maxPoolSize=%d&authSource=admin",
|
|
||||||
mongodbHosts, config.Config.Mongo.Database,
|
|
||||||
config.Config.Mongo.MaxPoolSize)
|
|
||||||
}
|
|
||||||
|
|
||||||
return uri
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *Mongo) GetClient() *mongo.Client {
|
func (m *Mongo) GetClient() *mongo.Client {
|
||||||
return m.db
|
return m.db
|
||||||
}
|
}
|
||||||
@@ -116,7 +106,6 @@ func (m *Mongo) CreateSuperGroupIndex() error {
|
|||||||
if err := m.createMongoIndex(unrelation.CUserToSuperGroup, true, "user_id"); err != nil {
|
if err := m.createMongoIndex(unrelation.CUserToSuperGroup, true, "user_id"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -150,6 +139,5 @@ func (m *Mongo) createMongoIndex(collection string, isUnique bool, keys ...strin
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return utils.Wrap(err, result)
|
return utils.Wrap(err, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
134
pkg/common/db/unrelation/msg.go
Executable file → Normal file
134
pkg/common/db/unrelation/msg.go
Executable file → Normal file
@@ -49,7 +49,6 @@ type MsgMongoDriver struct {
|
|||||||
|
|
||||||
func NewMsgMongoDriver(database *mongo.Database) table.MsgDocModelInterface {
|
func NewMsgMongoDriver(database *mongo.Database) table.MsgDocModelInterface {
|
||||||
collection := database.Collection(table.MsgDocModel{}.TableName())
|
collection := database.Collection(table.MsgDocModel{}.TableName())
|
||||||
|
|
||||||
return &MsgMongoDriver{MsgCollection: collection}
|
return &MsgMongoDriver{MsgCollection: collection}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +59,6 @@ func (m *MsgMongoDriver) PushMsgsToDoc(ctx context.Context, docID string, msgsTo
|
|||||||
|
|
||||||
func (m *MsgMongoDriver) Create(ctx context.Context, model *table.MsgDocModel) error {
|
func (m *MsgMongoDriver) Create(ctx context.Context, model *table.MsgDocModel) error {
|
||||||
_, err := m.MsgCollection.InsertOne(ctx, model)
|
_, err := m.MsgCollection.InsertOne(ctx, model)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +81,6 @@ func (m *MsgMongoDriver) UpdateMsg(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, utils.Wrap(err, "")
|
return nil, utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +108,6 @@ func (m *MsgMongoDriver) PushUnique(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, utils.Wrap(err, "")
|
return nil, utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,7 +120,6 @@ func (m *MsgMongoDriver) UpdateMsgContent(ctx context.Context, docID string, ind
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return utils.Wrap(err, "")
|
return utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -148,14 +143,12 @@ func (m *MsgMongoDriver) UpdateMsgStatusByIndexInOneDoc(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return utils.Wrap(err, "")
|
return utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgMongoDriver) FindOneByDocID(ctx context.Context, docID string) (*table.MsgDocModel, error) {
|
func (m *MsgMongoDriver) FindOneByDocID(ctx context.Context, docID string) (*table.MsgDocModel, error) {
|
||||||
doc := &table.MsgDocModel{}
|
doc := &table.MsgDocModel{}
|
||||||
err := m.MsgCollection.FindOne(ctx, bson.M{"doc_id": docID}).Decode(doc)
|
err := m.MsgCollection.FindOne(ctx, bson.M{"doc_id": docID}).Decode(doc)
|
||||||
|
|
||||||
return doc, err
|
return doc, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,7 +177,6 @@ func (m *MsgMongoDriver) GetMsgDocModelByIndex(
|
|||||||
if len(msgs) > 0 {
|
if len(msgs) > 0 {
|
||||||
return &msgs[0], nil
|
return &msgs[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, ErrMsgListNotExist
|
return nil, ErrMsgListNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,7 +225,6 @@ func (m *MsgMongoDriver) DeleteMsgsInOneDocByIndex(ctx context.Context, docID st
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return utils.Wrap(err, "")
|
return utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,7 +233,6 @@ func (m *MsgMongoDriver) DeleteDocs(ctx context.Context, docIDs []string) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
_, err := m.MsgCollection.DeleteMany(ctx, bson.M{"doc_id": bson.M{"$in": docIDs}})
|
_, err := m.MsgCollection.DeleteMany(ctx, bson.M{"doc_id": bson.M{"$in": docIDs}})
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -256,7 +246,6 @@ func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc(
|
|||||||
for _, seq := range seqs {
|
for _, seq := range seqs {
|
||||||
indexs = append(indexs, m.model.GetMsgIndex(seq))
|
indexs = append(indexs, m.model.GetMsgIndex(seq))
|
||||||
}
|
}
|
||||||
//nolint:govet //This is already the officially recommended standard practice.
|
|
||||||
pipeline := mongo.Pipeline{
|
pipeline := mongo.Pipeline{
|
||||||
{
|
{
|
||||||
{"$match", bson.D{
|
{"$match", bson.D{
|
||||||
@@ -347,7 +336,6 @@ func (m *MsgMongoDriver) GetMsgBySeqIndexIn1Doc(
|
|||||||
}
|
}
|
||||||
msgs = append(msgs, msg)
|
msgs = append(msgs, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
return msgs, nil
|
return msgs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -356,7 +344,6 @@ func (m *MsgMongoDriver) IsExistDocID(ctx context.Context, docID string) (bool,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return false, errs.Wrap(err)
|
return false, errs.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return count > 0, nil
|
return count > 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -385,7 +372,6 @@ func (m *MsgMongoDriver) MarkSingleChatMsgsAsRead(
|
|||||||
updates = append(updates, updateModel)
|
updates = append(updates, updateModel)
|
||||||
}
|
}
|
||||||
_, err := m.MsgCollection.BulkWrite(ctx, updates)
|
_, err := m.MsgCollection.BulkWrite(ctx, updates)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -625,39 +611,7 @@ func (m *MsgMongoDriver) RangeUserSendCount(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
pipeline := buildPiplineForRangeUserSendCount(or, start, end, sort, pageNumber, showNumber)
|
pipeline := bson.A{
|
||||||
cur, err := m.MsgCollection.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(true))
|
|
||||||
if err != nil {
|
|
||||||
return 0, 0, nil, nil, errs.Wrap(err)
|
|
||||||
}
|
|
||||||
defer cur.Close(ctx)
|
|
||||||
var result []Result
|
|
||||||
if err = cur.All(ctx, &result); err != nil {
|
|
||||||
return 0, 0, nil, nil, errs.Wrap(err)
|
|
||||||
}
|
|
||||||
if len(result) == 0 {
|
|
||||||
return 0, 0, nil, nil, errs.Wrap(err)
|
|
||||||
}
|
|
||||||
users = make([]*table.UserCount, len(result[0].Users))
|
|
||||||
for i, r := range result[0].Users {
|
|
||||||
users[i] = &table.UserCount{
|
|
||||||
UserID: r.UserID,
|
|
||||||
Count: r.Count,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dateCount = make(map[string]int64)
|
|
||||||
for _, r := range result[0].Dates {
|
|
||||||
dateCount[r.Date] = r.Count
|
|
||||||
}
|
|
||||||
|
|
||||||
return result[0].MsgCount, result[0].UserCount, users, dateCount, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//nolint:funlen // it need to be such long
|
|
||||||
func buildPiplineForRangeUserSendCount(or bson.A, start time.Time,
|
|
||||||
end time.Time, sort int, pageNumber, showNumber int32,
|
|
||||||
) bson.A {
|
|
||||||
return bson.A{
|
|
||||||
bson.M{
|
bson.M{
|
||||||
"$match": bson.M{
|
"$match": bson.M{
|
||||||
"$and": bson.A{
|
"$and": bson.A{
|
||||||
@@ -841,6 +795,30 @@ func buildPiplineForRangeUserSendCount(or bson.A, start time.Time,
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cur, err := m.MsgCollection.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(true))
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, nil, nil, errs.Wrap(err)
|
||||||
|
}
|
||||||
|
defer cur.Close(ctx)
|
||||||
|
var result []Result
|
||||||
|
if err := cur.All(ctx, &result); err != nil {
|
||||||
|
return 0, 0, nil, nil, errs.Wrap(err)
|
||||||
|
}
|
||||||
|
if len(result) == 0 {
|
||||||
|
return 0, 0, nil, nil, errs.Wrap(err)
|
||||||
|
}
|
||||||
|
users = make([]*table.UserCount, len(result[0].Users))
|
||||||
|
for i, r := range result[0].Users {
|
||||||
|
users[i] = &table.UserCount{
|
||||||
|
UserID: r.UserID,
|
||||||
|
Count: r.Count,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dateCount = make(map[string]int64)
|
||||||
|
for _, r := range result[0].Dates {
|
||||||
|
dateCount[r.Date] = r.Count
|
||||||
|
}
|
||||||
|
return result[0].MsgCount, result[0].UserCount, users, dateCount, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgMongoDriver) RangeGroupSendCount(
|
func (m *MsgMongoDriver) RangeGroupSendCount(
|
||||||
@@ -869,39 +847,7 @@ func (m *MsgMongoDriver) RangeGroupSendCount(
|
|||||||
Count int64 `bson:"count"`
|
Count int64 `bson:"count"`
|
||||||
} `bson:"dates"`
|
} `bson:"dates"`
|
||||||
}
|
}
|
||||||
pipeline := buildPiplineForRangeGroupSendCount(start, end, sort, pageNumber, showNumber)
|
pipeline := bson.A{
|
||||||
cur, err := m.MsgCollection.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(true))
|
|
||||||
if err != nil {
|
|
||||||
return 0, 0, nil, nil, errs.Wrap(err)
|
|
||||||
}
|
|
||||||
defer cur.Close(ctx)
|
|
||||||
var result []Result
|
|
||||||
if err = cur.All(ctx, &result); err != nil {
|
|
||||||
return 0, 0, nil, nil, errs.Wrap(err)
|
|
||||||
}
|
|
||||||
if len(result) == 0 {
|
|
||||||
return 0, 0, nil, nil, errs.Wrap(err)
|
|
||||||
}
|
|
||||||
groups = make([]*table.GroupCount, len(result[0].Groups))
|
|
||||||
for i, r := range result[0].Groups {
|
|
||||||
groups[i] = &table.GroupCount{
|
|
||||||
GroupID: r.GroupID,
|
|
||||||
Count: r.Count,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dateCount = make(map[string]int64)
|
|
||||||
for _, r := range result[0].Dates {
|
|
||||||
dateCount[r.Date] = r.Count
|
|
||||||
}
|
|
||||||
|
|
||||||
return result[0].MsgCount, result[0].UserCount, groups, dateCount, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//nolint:funlen //it need to has such length
|
|
||||||
func buildPiplineForRangeGroupSendCount(start time.Time,
|
|
||||||
end time.Time, sort int, pageNumber, showNumber int32,
|
|
||||||
) bson.A {
|
|
||||||
return bson.A{
|
|
||||||
bson.M{
|
bson.M{
|
||||||
"$match": bson.M{
|
"$match": bson.M{
|
||||||
"$and": bson.A{
|
"$and": bson.A{
|
||||||
@@ -1098,6 +1044,30 @@ func buildPiplineForRangeGroupSendCount(start time.Time,
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cur, err := m.MsgCollection.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(true))
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, nil, nil, errs.Wrap(err)
|
||||||
|
}
|
||||||
|
defer cur.Close(ctx)
|
||||||
|
var result []Result
|
||||||
|
if err := cur.All(ctx, &result); err != nil {
|
||||||
|
return 0, 0, nil, nil, errs.Wrap(err)
|
||||||
|
}
|
||||||
|
if len(result) == 0 {
|
||||||
|
return 0, 0, nil, nil, errs.Wrap(err)
|
||||||
|
}
|
||||||
|
groups = make([]*table.GroupCount, len(result[0].Groups))
|
||||||
|
for i, r := range result[0].Groups {
|
||||||
|
groups[i] = &table.GroupCount{
|
||||||
|
GroupID: r.GroupID,
|
||||||
|
Count: r.Count,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dateCount = make(map[string]int64)
|
||||||
|
for _, r := range result[0].Dates {
|
||||||
|
dateCount[r.Date] = r.Count
|
||||||
|
}
|
||||||
|
return result[0].MsgCount, result[0].UserCount, groups, dateCount, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MsgMongoDriver) SearchMessage(ctx context.Context, req *msg.SearchMessageReq) (int32, []*table.MsgInfoModel, error) {
|
func (m *MsgMongoDriver) SearchMessage(ctx context.Context, req *msg.SearchMessageReq) (int32, []*table.MsgInfoModel, error) {
|
||||||
@@ -1105,7 +1075,6 @@ func (m *MsgMongoDriver) SearchMessage(ctx context.Context, req *msg.SearchMessa
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil, err
|
return 0, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return total, msgs, nil
|
return total, msgs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1150,7 +1119,7 @@ func (m *MsgMongoDriver) searchMessage(ctx context.Context, req *msg.SearchMessa
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
//nolint:govet //this is already standard
|
|
||||||
pipe = mongo.Pipeline{
|
pipe = mongo.Pipeline{
|
||||||
{
|
{
|
||||||
{"$match", bson.D{
|
{"$match", bson.D{
|
||||||
@@ -1245,6 +1214,5 @@ func (m *MsgMongoDriver) searchMessage(ctx context.Context, req *msg.SearchMessa
|
|||||||
} else {
|
} else {
|
||||||
msgs = msgs[start:]
|
msgs = msgs[start:]
|
||||||
}
|
}
|
||||||
|
|
||||||
return n, msgs, nil
|
return n, msgs, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,14 +31,12 @@ func (m *MsgMongoDriver) ConvertMsgsDocLen(ctx context.Context, conversationIDs
|
|||||||
cursor, err := m.MsgCollection.Find(ctx, bson.M{"doc_id": regex})
|
cursor, err := m.MsgCollection.Find(ctx, bson.M{"doc_id": regex})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ZError(ctx, "convertAll find msg doc failed", err, "conversationID", conversationID)
|
log.ZError(ctx, "convertAll find msg doc failed", err, "conversationID", conversationID)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var msgDocs []table.MsgDocModel
|
var msgDocs []table.MsgDocModel
|
||||||
err = cursor.All(ctx, &msgDocs)
|
err = cursor.All(ctx, &msgDocs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.ZError(ctx, "convertAll cursor all failed", err, "conversationID", conversationID)
|
log.ZError(ctx, "convertAll cursor all failed", err, "conversationID", conversationID)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if len(msgDocs) < 1 {
|
if len(msgDocs) < 1 {
|
||||||
@@ -46,45 +44,39 @@ func (m *MsgMongoDriver) ConvertMsgsDocLen(ctx context.Context, conversationIDs
|
|||||||
}
|
}
|
||||||
log.ZInfo(ctx, "msg doc convert", "conversationID", conversationID, "len(msgDocs)", len(msgDocs))
|
log.ZInfo(ctx, "msg doc convert", "conversationID", conversationID, "len(msgDocs)", len(msgDocs))
|
||||||
if len(msgDocs[0].Msg) == int(m.model.GetSingleGocMsgNum5000()) {
|
if len(msgDocs[0].Msg) == int(m.model.GetSingleGocMsgNum5000()) {
|
||||||
convertMsgDocs(m, ctx, msgDocs, conversationID, regex)
|
if _, err := m.MsgCollection.DeleteMany(ctx, bson.M{"doc_id": regex}); err != nil {
|
||||||
}
|
log.ZError(ctx, "convertAll delete many failed", err, "conversationID", conversationID)
|
||||||
}
|
continue
|
||||||
}
|
}
|
||||||
|
var newMsgDocs []interface{}
|
||||||
func convertMsgDocs(m *MsgMongoDriver, ctx context.Context, msgDocs []table.MsgDocModel, conversationID string, regex primitive.Regex) {
|
for _, msgDoc := range msgDocs {
|
||||||
var err error
|
if int64(len(msgDoc.Msg)) == m.model.GetSingleGocMsgNum() {
|
||||||
if _, err = m.MsgCollection.DeleteMany(ctx, bson.M{"doc_id": regex}); err != nil {
|
continue
|
||||||
log.ZError(ctx, "convertAll delete many failed", err, "conversationID", conversationID)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var newMsgDocs []interface{}
|
|
||||||
for _, msgDoc := range msgDocs {
|
|
||||||
if int64(len(msgDoc.Msg)) == m.model.GetSingleGocMsgNum() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
var index int64
|
|
||||||
for index < int64(len(msgDoc.Msg)) {
|
|
||||||
msg := msgDoc.Msg[index]
|
|
||||||
if msg != nil && msg.Msg != nil {
|
|
||||||
msgDocModel := table.MsgDocModel{DocID: m.model.GetDocID(conversationID, msg.Msg.Seq)}
|
|
||||||
end := index + m.model.GetSingleGocMsgNum()
|
|
||||||
if int(end) >= len(msgDoc.Msg) {
|
|
||||||
msgDocModel.Msg = msgDoc.Msg[index:]
|
|
||||||
} else {
|
|
||||||
msgDocModel.Msg = msgDoc.Msg[index:end]
|
|
||||||
}
|
}
|
||||||
newMsgDocs = append(newMsgDocs, msgDocModel)
|
var index int64
|
||||||
index = end
|
for index < int64(len(msgDoc.Msg)) {
|
||||||
|
msg := msgDoc.Msg[index]
|
||||||
|
if msg != nil && msg.Msg != nil {
|
||||||
|
msgDocModel := table.MsgDocModel{DocID: m.model.GetDocID(conversationID, msg.Msg.Seq)}
|
||||||
|
end := index + m.model.GetSingleGocMsgNum()
|
||||||
|
if int(end) >= len(msgDoc.Msg) {
|
||||||
|
msgDocModel.Msg = msgDoc.Msg[index:]
|
||||||
|
} else {
|
||||||
|
msgDocModel.Msg = msgDoc.Msg[index:end]
|
||||||
|
}
|
||||||
|
newMsgDocs = append(newMsgDocs, msgDocModel)
|
||||||
|
index = end
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_, err = m.MsgCollection.InsertMany(ctx, newMsgDocs)
|
||||||
|
if err != nil {
|
||||||
|
log.ZError(ctx, "convertAll insert many failed", err, "conversationID", conversationID, "len(newMsgDocs)", len(newMsgDocs))
|
||||||
} else {
|
} else {
|
||||||
break
|
log.ZInfo(ctx, "msg doc convert", "conversationID", conversationID, "len(newMsgDocs)", len(newMsgDocs))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_, err = m.MsgCollection.InsertMany(ctx, newMsgDocs)
|
|
||||||
if err != nil {
|
|
||||||
log.ZError(ctx, "convertAll insert many failed", err, "conversationID", conversationID, "len(newMsgDocs)", len(newMsgDocs))
|
|
||||||
} else {
|
|
||||||
log.ZInfo(ctx, "msg doc convert", "conversationID", conversationID, "len(newMsgDocs)", len(newMsgDocs))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,7 +59,6 @@ func (s *SuperGroupMongoDriver) CreateSuperGroup(ctx context.Context, groupID st
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,7 +69,6 @@ func (s *SuperGroupMongoDriver) TakeSuperGroup(
|
|||||||
if err := s.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&group); err != nil {
|
if err := s.superGroupCollection.FindOne(ctx, bson.M{"group_id": groupID}).Decode(&group); err != nil {
|
||||||
return nil, utils.Wrap(err, "")
|
return nil, utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return group, nil
|
return group, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +86,6 @@ func (s *SuperGroupMongoDriver) FindSuperGroup(
|
|||||||
if err := cursor.All(ctx, &groups); err != nil {
|
if err := cursor.All(ctx, &groups); err != nil {
|
||||||
return nil, utils.Wrap(err, "")
|
return nil, utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return groups, nil
|
return groups, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,7 +113,6 @@ func (s *SuperGroupMongoDriver) AddUserToSuperGroup(ctx context.Context, groupID
|
|||||||
return utils.Wrap(err, "transaction failed")
|
return utils.Wrap(err, "transaction failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +129,6 @@ func (s *SuperGroupMongoDriver) RemoverUserFromSuperGroup(ctx context.Context, g
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,7 +138,6 @@ func (s *SuperGroupMongoDriver) GetSuperGroupByUserID(
|
|||||||
) (*unrelation.UserToSuperGroupModel, error) {
|
) (*unrelation.UserToSuperGroupModel, error) {
|
||||||
var user unrelation.UserToSuperGroupModel
|
var user unrelation.UserToSuperGroupModel
|
||||||
err := s.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
|
err := s.userToSuperGroupCollection.FindOne(ctx, bson.M{"user_id": userID}).Decode(&user)
|
||||||
|
|
||||||
return &user, utils.Wrap(err, "")
|
return &user, utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,7 +149,6 @@ func (s *SuperGroupMongoDriver) DeleteSuperGroup(ctx context.Context, groupID st
|
|||||||
if _, err := s.superGroupCollection.DeleteOne(ctx, bson.M{"group_id": groupID}); err != nil {
|
if _, err := s.superGroupCollection.DeleteOne(ctx, bson.M{"group_id": groupID}); err != nil {
|
||||||
return utils.Wrap(err, "")
|
return utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
return s.RemoveGroupFromUser(ctx, groupID, group.MemberIDs)
|
return s.RemoveGroupFromUser(ctx, groupID, group.MemberIDs)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,6 +158,5 @@ func (s *SuperGroupMongoDriver) RemoveGroupFromUser(ctx context.Context, groupID
|
|||||||
bson.M{"user_id": bson.M{"$in": userIDs}},
|
bson.M{"user_id": bson.M{"$in": userIDs}},
|
||||||
bson.M{"$pull": bson.M{"group_id_list": groupID}},
|
bson.M{"$pull": bson.M{"group_id_list": groupID}},
|
||||||
)
|
)
|
||||||
|
|
||||||
return utils.Wrap(err, "")
|
return utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|||||||
21
pkg/common/db/unrelation/user.go
Executable file → Normal file
21
pkg/common/db/unrelation/user.go
Executable file → Normal file
@@ -16,7 +16,6 @@ package unrelation
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
|
|
||||||
"github.com/OpenIMSDK/tools/errs"
|
"github.com/OpenIMSDK/tools/errs"
|
||||||
"github.com/OpenIMSDK/tools/utils"
|
"github.com/OpenIMSDK/tools/utils"
|
||||||
@@ -51,7 +50,6 @@ type UserMongoDriver struct {
|
|||||||
// AddSubscriptionList Subscriber's handling of thresholds.
|
// AddSubscriptionList Subscriber's handling of thresholds.
|
||||||
func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string, userIDList []string) error {
|
func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string, userIDList []string) error {
|
||||||
// Check the number of lists in the key.
|
// Check the number of lists in the key.
|
||||||
//nolint:govet //this has already been the standard format for mongo.Pipeline
|
|
||||||
pipeline := mongo.Pipeline{
|
pipeline := mongo.Pipeline{
|
||||||
{{"$match", bson.D{{"user_id", SubscriptionPrefix + userID}}}},
|
{{"$match", bson.D{{"user_id", SubscriptionPrefix + userID}}}},
|
||||||
{{"$project", bson.D{{"count", bson.D{{"$size", "$user_id_list"}}}}}},
|
{{"$project", bson.D{{"count", bson.D{{"$size", "$user_id_list"}}}}}},
|
||||||
@@ -67,7 +65,7 @@ func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string
|
|||||||
}
|
}
|
||||||
// iterate over aggregated results
|
// iterate over aggregated results
|
||||||
for cursor.Next(ctx) {
|
for cursor.Next(ctx) {
|
||||||
err = cursor.Decode(&cnt)
|
err := cursor.Decode(&cnt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
@@ -124,7 +122,6 @@ func (u *UserMongoDriver) AddSubscriptionList(ctx context.Context, userID string
|
|||||||
return utils.Wrap(err, "transaction failed")
|
return utils.Wrap(err, "transaction failed")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +139,6 @@ func (u *UserMongoDriver) UnsubscriptionList(ctx context.Context, userID string,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +152,6 @@ func (u *UserMongoDriver) RemoveSubscribedListFromUser(ctx context.Context, user
|
|||||||
bson.M{"$pull": bson.M{"user_id_list": userID}},
|
bson.M{"$pull": bson.M{"user_id_list": userID}},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
return errs.Wrap(err)
|
return errs.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,13 +163,12 @@ func (u *UserMongoDriver) GetAllSubscribeList(ctx context.Context, userID string
|
|||||||
bson.M{"user_id": SubscriptionPrefix + userID})
|
bson.M{"user_id": SubscriptionPrefix + userID})
|
||||||
err = cursor.Decode(&user)
|
err = cursor.Decode(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, mongo.ErrNoDocuments) {
|
if err == mongo.ErrNoDocuments {
|
||||||
return []string{}, nil
|
return []string{}, nil
|
||||||
|
} else {
|
||||||
|
return nil, errs.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, errs.Wrap(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return user.UserIDList, nil
|
return user.UserIDList, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,12 +180,11 @@ func (u *UserMongoDriver) GetSubscribedList(ctx context.Context, userID string)
|
|||||||
bson.M{"user_id": SubscribedPrefix + userID})
|
bson.M{"user_id": SubscribedPrefix + userID})
|
||||||
err = cursor.Decode(&user)
|
err = cursor.Decode(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, mongo.ErrNoDocuments) {
|
if err == mongo.ErrNoDocuments {
|
||||||
return []string{}, nil
|
return []string{}, nil
|
||||||
|
} else {
|
||||||
|
return nil, errs.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, errs.Wrap(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return user.UserIDList, nil
|
return user.UserIDList, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,14 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/OpenIMSDK/tools/discoveryregistry"
|
"github.com/OpenIMSDK/tools/discoveryregistry"
|
||||||
openkeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper"
|
openkeeper "github.com/OpenIMSDK/tools/discoveryregistry/zookeeper"
|
||||||
"github.com/OpenIMSDK/tools/log"
|
"github.com/OpenIMSDK/tools/log"
|
||||||
"google.golang.org/grpc"
|
|
||||||
|
|
||||||
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
"github.com/openimsdk/open-im-server/v3/pkg/common/config"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewDiscoveryRegister(envType string) (discoveryregistry.SvcDiscoveryRegistry, error) {
|
func NewDiscoveryRegister(envType string) (discoveryregistry.SvcDiscoveryRegistry, error) {
|
||||||
@@ -30,7 +28,6 @@ func NewDiscoveryRegister(envType string) (discoveryregistry.SvcDiscoveryRegistr
|
|||||||
client = nil
|
client = nil
|
||||||
err = errors.New("envType not correct")
|
err = errors.New("envType not correct")
|
||||||
}
|
}
|
||||||
|
|
||||||
return client, err
|
return client, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,51 +42,47 @@ func NewK8sDiscoveryRegister() (discoveryregistry.SvcDiscoveryRegistry, error) {
|
|||||||
|
|
||||||
func (cli *K8sDR) Register(serviceName, host string, port int, opts ...grpc.DialOption) error {
|
func (cli *K8sDR) Register(serviceName, host string, port int, opts ...grpc.DialOption) error {
|
||||||
cli.rpcRegisterAddr = serviceName
|
cli.rpcRegisterAddr = serviceName
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *K8sDR) UnRegister() error {
|
func (cli *K8sDR) UnRegister() error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *K8sDR) CreateRpcRootNodes(serviceNames []string) error {
|
func (cli *K8sDR) CreateRpcRootNodes(serviceNames []string) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *K8sDR) RegisterConf2Registry(key string, conf []byte) error {
|
func (cli *K8sDR) RegisterConf2Registry(key string, conf []byte) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *K8sDR) GetConfFromRegistry(key string) ([]byte, error) {
|
func (cli *K8sDR) GetConfFromRegistry(key string) ([]byte, error) {
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *K8sDR) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) {
|
func (cli *K8sDR) GetConns(ctx context.Context, serviceName string, opts ...grpc.DialOption) ([]*grpc.ClientConn, error) {
|
||||||
conn, err := grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...)
|
|
||||||
|
|
||||||
|
conn, err := grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...)
|
||||||
return []*grpc.ClientConn{conn}, err
|
return []*grpc.ClientConn{conn}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *K8sDR) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
func (cli *K8sDR) GetConn(ctx context.Context, serviceName string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
|
||||||
|
|
||||||
return grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...)
|
return grpc.DialContext(ctx, serviceName, append(cli.options, opts...)...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *K8sDR) GetSelfConnTarget() string {
|
func (cli *K8sDR) GetSelfConnTarget() string {
|
||||||
|
|
||||||
return cli.rpcRegisterAddr
|
return cli.rpcRegisterAddr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *K8sDR) AddOption(opts ...grpc.DialOption) {
|
func (cli *K8sDR) AddOption(opts ...grpc.DialOption) {
|
||||||
cli.options = append(cli.options, opts...)
|
cli.options = append(cli.options, opts...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *K8sDR) CloseConn(conn *grpc.ClientConn) {
|
func (cli *K8sDR) CloseConn(conn *grpc.ClientConn) {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// do not use this method for call rpc.
|
// do not use this method for call rpc
|
||||||
func (cli *K8sDR) GetClientLocalConns() map[string][]*grpc.ClientConn {
|
func (cli *K8sDR) GetClientLocalConns() map[string][]*grpc.ClientConn {
|
||||||
fmt.Println("should not call this function!!!!!!!!!!!!!!!!!!!!!!!!!")
|
fmt.Println("should not call this function!!!!!!!!!!!!!!!!!!!!!!!!!")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,21 +34,16 @@ import (
|
|||||||
var client http.Client
|
var client http.Client
|
||||||
|
|
||||||
func Get(url string) (response []byte, err error) {
|
func Get(url string) (response []byte, err error) {
|
||||||
clientGet := http.Client{Timeout: 5 * time.Second}
|
client := http.Client{Timeout: 5 * time.Second}
|
||||||
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
|
resp, err := client.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
resp, err2 := clientGet.Do(req)
|
|
||||||
if err2 != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, err3 := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err3 != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return body, nil
|
return body, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +83,6 @@ func Post(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +98,6 @@ func PostReturn(
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(b, output)
|
err = json.Unmarshal(b, output)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,22 +116,17 @@ func callBackPostReturn(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
||||||
log.ZWarn(ctx, "callback failed but continue", err, "url", url)
|
log.ZWarn(ctx, "callback failed but continue", err, "url", url)
|
||||||
|
|
||||||
return errs.ErrCallbackContinue
|
return errs.ErrCallbackContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
return errs.ErrNetwork.Wrap(err.Error())
|
return errs.ErrNetwork.Wrap(err.Error())
|
||||||
}
|
}
|
||||||
if err = json.Unmarshal(b, output); err != nil {
|
if err = json.Unmarshal(b, output); err != nil {
|
||||||
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
if callbackConfig.CallbackFailedContinue != nil && *callbackConfig.CallbackFailedContinue {
|
||||||
log.ZWarn(ctx, "callback failed but continue", err, "url", url)
|
log.ZWarn(ctx, "callback failed but continue", err, "url", url)
|
||||||
|
|
||||||
return errs.ErrCallbackContinue
|
return errs.ErrCallbackContinue
|
||||||
}
|
}
|
||||||
|
|
||||||
return errs.ErrData.Wrap(err.Error())
|
return errs.ErrData.Wrap(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return output.Parse()
|
return output.Parse()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ func NewMConsumerGroup(consumerConfig *MConsumerGroupConfig, topics, addrs []str
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return &MConsumerGroup{
|
return &MConsumerGroup{
|
||||||
consumerGroup,
|
consumerGroup,
|
||||||
groupID,
|
groupID,
|
||||||
|
|||||||
@@ -67,7 +67,6 @@ func NewKafkaProducer(addr []string, topic string) *Producer {
|
|||||||
producer, err = sarama.NewSyncProducer(p.addr, p.config) // Initialize the client
|
producer, err = sarama.NewSyncProducer(p.addr, p.config) // Initialize the client
|
||||||
if err == nil {
|
if err == nil {
|
||||||
p.producer = producer
|
p.producer = producer
|
||||||
|
|
||||||
return &p
|
return &p
|
||||||
}
|
}
|
||||||
//TODO If the password is wrong, exit directly
|
//TODO If the password is wrong, exit directly
|
||||||
@@ -84,7 +83,6 @@ func NewKafkaProducer(addr []string, topic string) *Producer {
|
|||||||
panic(err.Error())
|
panic(err.Error())
|
||||||
}
|
}
|
||||||
p.producer = producer
|
p.producer = producer
|
||||||
|
|
||||||
return &p
|
return &p
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +91,6 @@ func GetMQHeaderWithContext(ctx context.Context) ([]sarama.RecordHeader, error)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return []sarama.RecordHeader{
|
return []sarama.RecordHeader{
|
||||||
{Key: []byte(constant.OperationID), Value: []byte(operationID)},
|
{Key: []byte(constant.OperationID), Value: []byte(operationID)},
|
||||||
{Key: []byte(constant.OpUserID), Value: []byte(opUserID)},
|
{Key: []byte(constant.OpUserID), Value: []byte(opUserID)},
|
||||||
@@ -103,11 +100,10 @@ func GetMQHeaderWithContext(ctx context.Context) ([]sarama.RecordHeader, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetContextWithMQHeader(header []*sarama.RecordHeader) context.Context {
|
func GetContextWithMQHeader(header []*sarama.RecordHeader) context.Context {
|
||||||
values := make([]string, 0, len(header))
|
var values []string
|
||||||
for _, recordHeader := range header {
|
for _, recordHeader := range header {
|
||||||
values = append(values, string(recordHeader.Value))
|
values = append(values, string(recordHeader.Value))
|
||||||
}
|
}
|
||||||
|
|
||||||
return mcontext.WithMustInfoCtx(values) // TODO
|
return mcontext.WithMustInfoCtx(values) // TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,6 +134,5 @@ func (p *Producer) SendMessage(ctx context.Context, key string, msg proto.Messag
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
prome.Inc(prome.SendMsgCounter)
|
prome.Inc(prome.SendMsgCounter)
|
||||||
}
|
}
|
||||||
|
|
||||||
return partition, offset, utils.Wrap(err, "")
|
return partition, offset, utils.Wrap(err, "")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,13 +42,11 @@ func (l *LockerMessage) LockMessageTypeKey(ctx context.Context, clientMsgID, typ
|
|||||||
err = l.cache.LockMessageTypeKey(ctx, clientMsgID, typeKey)
|
err = l.cache.LockMessageTypeKey(ctx, clientMsgID, typeKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
time.Sleep(time.Millisecond * 100)
|
time.Sleep(time.Millisecond * 100)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,13 +55,11 @@ func (l *LockerMessage) LockGlobalMessage(ctx context.Context, clientMsgID strin
|
|||||||
err = l.cache.LockMessageTypeKey(ctx, clientMsgID, GlOBALLOCK)
|
err = l.cache.LockMessageTypeKey(ctx, clientMsgID, GlOBALLOCK)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
time.Sleep(time.Millisecond * 100)
|
time.Sleep(time.Millisecond * 100)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -79,7 +79,6 @@ var (
|
|||||||
ConversationCreateFailedCounter prometheus.Counter
|
ConversationCreateFailedCounter prometheus.Counter
|
||||||
)
|
)
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewUserLoginCounter() {
|
func NewUserLoginCounter() {
|
||||||
if UserLoginCounter != nil {
|
if UserLoginCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -90,7 +89,6 @@ func NewUserLoginCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewUserRegisterCounter() {
|
func NewUserRegisterCounter() {
|
||||||
if UserRegisterCounter != nil {
|
if UserRegisterCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -101,7 +99,6 @@ func NewUserRegisterCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewSeqGetSuccessCounter() {
|
func NewSeqGetSuccessCounter() {
|
||||||
if SeqGetSuccessCounter != nil {
|
if SeqGetSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -112,7 +109,6 @@ func NewSeqGetSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewSeqGetFailedCounter() {
|
func NewSeqGetFailedCounter() {
|
||||||
if SeqGetFailedCounter != nil {
|
if SeqGetFailedCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -123,7 +119,6 @@ func NewSeqGetFailedCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewSeqSetSuccessCounter() {
|
func NewSeqSetSuccessCounter() {
|
||||||
if SeqSetSuccessCounter != nil {
|
if SeqSetSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -134,7 +129,6 @@ func NewSeqSetSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewSeqSetFailedCounter() {
|
func NewSeqSetFailedCounter() {
|
||||||
if SeqSetFailedCounter != nil {
|
if SeqSetFailedCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -145,7 +139,6 @@ func NewSeqSetFailedCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewApiRequestCounter() {
|
func NewApiRequestCounter() {
|
||||||
if ApiRequestCounter != nil {
|
if ApiRequestCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -156,7 +149,6 @@ func NewApiRequestCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewApiRequestSuccessCounter() {
|
func NewApiRequestSuccessCounter() {
|
||||||
if ApiRequestSuccessCounter != nil {
|
if ApiRequestSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -167,7 +159,6 @@ func NewApiRequestSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewApiRequestFailedCounter() {
|
func NewApiRequestFailedCounter() {
|
||||||
if ApiRequestFailedCounter != nil {
|
if ApiRequestFailedCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -178,7 +169,6 @@ func NewApiRequestFailedCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewGrpcRequestCounter() {
|
func NewGrpcRequestCounter() {
|
||||||
if GrpcRequestCounter != nil {
|
if GrpcRequestCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -189,7 +179,6 @@ func NewGrpcRequestCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewGrpcRequestSuccessCounter() {
|
func NewGrpcRequestSuccessCounter() {
|
||||||
if GrpcRequestSuccessCounter != nil {
|
if GrpcRequestSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -200,7 +189,6 @@ func NewGrpcRequestSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewGrpcRequestFailedCounter() {
|
func NewGrpcRequestFailedCounter() {
|
||||||
if GrpcRequestFailedCounter != nil {
|
if GrpcRequestFailedCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -211,7 +199,6 @@ func NewGrpcRequestFailedCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewSendMsgCount() {
|
func NewSendMsgCount() {
|
||||||
if SendMsgCounter != nil {
|
if SendMsgCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -222,7 +209,6 @@ func NewSendMsgCount() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewMsgInsertRedisSuccessCounter() {
|
func NewMsgInsertRedisSuccessCounter() {
|
||||||
if MsgInsertRedisSuccessCounter != nil {
|
if MsgInsertRedisSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -233,7 +219,6 @@ func NewMsgInsertRedisSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix its
|
|
||||||
func NewMsgInsertRedisFailedCounter() {
|
func NewMsgInsertRedisFailedCounter() {
|
||||||
if MsgInsertRedisFailedCounter != nil {
|
if MsgInsertRedisFailedCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -244,7 +229,6 @@ func NewMsgInsertRedisFailedCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewMsgInsertMongoSuccessCounter() {
|
func NewMsgInsertMongoSuccessCounter() {
|
||||||
if MsgInsertMongoSuccessCounter != nil {
|
if MsgInsertMongoSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -255,7 +239,6 @@ func NewMsgInsertMongoSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewMsgInsertMongoFailedCounter() {
|
func NewMsgInsertMongoFailedCounter() {
|
||||||
if MsgInsertMongoFailedCounter != nil {
|
if MsgInsertMongoFailedCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -266,7 +249,6 @@ func NewMsgInsertMongoFailedCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewMsgPullFromRedisSuccessCounter() {
|
func NewMsgPullFromRedisSuccessCounter() {
|
||||||
if MsgPullFromRedisSuccessCounter != nil {
|
if MsgPullFromRedisSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -277,7 +259,6 @@ func NewMsgPullFromRedisSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewMsgPullFromRedisFailedCounter() {
|
func NewMsgPullFromRedisFailedCounter() {
|
||||||
if MsgPullFromRedisFailedCounter != nil {
|
if MsgPullFromRedisFailedCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -288,7 +269,6 @@ func NewMsgPullFromRedisFailedCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewMsgPullFromMongoSuccessCounter() {
|
func NewMsgPullFromMongoSuccessCounter() {
|
||||||
if MsgPullFromMongoSuccessCounter != nil {
|
if MsgPullFromMongoSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -299,7 +279,6 @@ func NewMsgPullFromMongoSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewMsgPullFromMongoFailedCounter() {
|
func NewMsgPullFromMongoFailedCounter() {
|
||||||
if MsgPullFromMongoFailedCounter != nil {
|
if MsgPullFromMongoFailedCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -340,7 +319,6 @@ func NewPullMsgBySeqListTotalCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewSingleChatMsgRecvSuccessCounter() {
|
func NewSingleChatMsgRecvSuccessCounter() {
|
||||||
if SingleChatMsgRecvSuccessCounter != nil {
|
if SingleChatMsgRecvSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -351,7 +329,6 @@ func NewSingleChatMsgRecvSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewGroupChatMsgRecvSuccessCounter() {
|
func NewGroupChatMsgRecvSuccessCounter() {
|
||||||
if GroupChatMsgRecvSuccessCounter != nil {
|
if GroupChatMsgRecvSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -362,7 +339,6 @@ func NewGroupChatMsgRecvSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewWorkSuperGroupChatMsgRecvSuccessCounter() {
|
func NewWorkSuperGroupChatMsgRecvSuccessCounter() {
|
||||||
if WorkSuperGroupChatMsgRecvSuccessCounter != nil {
|
if WorkSuperGroupChatMsgRecvSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -383,7 +359,6 @@ func NewOnlineUserGauges() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewSingleChatMsgProcessSuccessCounter() {
|
func NewSingleChatMsgProcessSuccessCounter() {
|
||||||
if SingleChatMsgProcessSuccessCounter != nil {
|
if SingleChatMsgProcessSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -394,7 +369,6 @@ func NewSingleChatMsgProcessSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewSingleChatMsgProcessFailedCounter() {
|
func NewSingleChatMsgProcessFailedCounter() {
|
||||||
if SingleChatMsgProcessFailedCounter != nil {
|
if SingleChatMsgProcessFailedCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -405,7 +379,6 @@ func NewSingleChatMsgProcessFailedCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewGroupChatMsgProcessSuccessCounter() {
|
func NewGroupChatMsgProcessSuccessCounter() {
|
||||||
if GroupChatMsgProcessSuccessCounter != nil {
|
if GroupChatMsgProcessSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -416,7 +389,6 @@ func NewGroupChatMsgProcessSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewGroupChatMsgProcessFailedCounter() {
|
func NewGroupChatMsgProcessFailedCounter() {
|
||||||
if GroupChatMsgProcessFailedCounter != nil {
|
if GroupChatMsgProcessFailedCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -427,7 +399,6 @@ func NewGroupChatMsgProcessFailedCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewWorkSuperGroupChatMsgProcessSuccessCounter() {
|
func NewWorkSuperGroupChatMsgProcessSuccessCounter() {
|
||||||
if WorkSuperGroupChatMsgProcessSuccessCounter != nil {
|
if WorkSuperGroupChatMsgProcessSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -438,7 +409,6 @@ func NewWorkSuperGroupChatMsgProcessSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewWorkSuperGroupChatMsgProcessFailedCounter() {
|
func NewWorkSuperGroupChatMsgProcessFailedCounter() {
|
||||||
if WorkSuperGroupChatMsgProcessFailedCounter != nil {
|
if WorkSuperGroupChatMsgProcessFailedCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -449,7 +419,6 @@ func NewWorkSuperGroupChatMsgProcessFailedCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewMsgOnlinePushSuccessCounter() {
|
func NewMsgOnlinePushSuccessCounter() {
|
||||||
if MsgOnlinePushSuccessCounter != nil {
|
if MsgOnlinePushSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -460,7 +429,6 @@ func NewMsgOnlinePushSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewMsgOfflinePushSuccessCounter() {
|
func NewMsgOfflinePushSuccessCounter() {
|
||||||
if MsgOfflinePushSuccessCounter != nil {
|
if MsgOfflinePushSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -471,7 +439,6 @@ func NewMsgOfflinePushSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewMsgOfflinePushFailedCounter() {
|
func NewMsgOfflinePushFailedCounter() {
|
||||||
if MsgOfflinePushFailedCounter != nil {
|
if MsgOfflinePushFailedCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -482,7 +449,6 @@ func NewMsgOfflinePushFailedCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewConversationCreateSuccessCounter() {
|
func NewConversationCreateSuccessCounter() {
|
||||||
if ConversationCreateSuccessCounter != nil {
|
if ConversationCreateSuccessCounter != nil {
|
||||||
return
|
return
|
||||||
@@ -493,7 +459,6 @@ func NewConversationCreateSuccessCounter() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:promlinter //no idea to fix it
|
|
||||||
func NewConversationCreateFailedCounter() {
|
func NewConversationCreateFailedCounter() {
|
||||||
if ConversationCreateFailedCounter != nil {
|
if ConversationCreateFailedCounter != nil {
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -30,16 +30,13 @@ func StartPrometheusSrv(prometheusPort int) error {
|
|||||||
if config.Config.Prometheus.Enable {
|
if config.Config.Prometheus.Enable {
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
err := http.ListenAndServe(":"+strconv.Itoa(prometheusPort), nil)
|
err := http.ListenAndServe(":"+strconv.Itoa(prometheusPort), nil)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrometheusHandler() gin.HandlerFunc {
|
func PrometheusHandler() gin.HandlerFunc {
|
||||||
h := promhttp.Handler()
|
h := promhttp.Handler()
|
||||||
|
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
h.ServeHTTP(c.Writer, c.Request)
|
h.ServeHTTP(c.Writer, c.Request)
|
||||||
}
|
}
|
||||||
@@ -52,7 +49,6 @@ type responseBodyWriter struct {
|
|||||||
|
|
||||||
func (r responseBodyWriter) Write(b []byte) (int, error) {
|
func (r responseBodyWriter) Write(b []byte) (int, error) {
|
||||||
r.body.Write(b)
|
r.body.Write(b)
|
||||||
|
|
||||||
return r.ResponseWriter.Write(b)
|
return r.ResponseWriter.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,5 +12,4 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
// Package startrpc start rpc server.
|
package startrpc // import "github.com/openimsdk/open-im-server/v3/pkg/common/startrpc"
|
||||||
package startrpc
|
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user