From a3ef3d52d704721042cda6a1b58704340804f125 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 5 Oct 2024 17:46:39 +0800 Subject: [PATCH] :sparkles: Better pulling available channel api by dividing dm and non-dm --- go.mod | 6 +++--- go.sum | 9 +-------- pkg/internal/server/api/channels_api.go | 12 ++++++++++-- pkg/internal/services/channels.go | 4 ++-- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index a644273..5132079 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,9 @@ toolchain go1.22.1 require ( git.solsynth.dev/hydrogen/dealer v0.0.0-20240919131945-00c52eba6827 + github.com/dgraph-io/ristretto v0.1.1 + github.com/eko/gocache/lib/v4 v4.1.6 + github.com/eko/gocache/store/ristretto/v4 v4.2.2 github.com/go-playground/validator/v10 v10.17.0 github.com/gofiber/fiber/v2 v2.52.4 github.com/google/uuid v1.6.0 @@ -31,13 +34,10 @@ require ( github.com/bep/debounce v1.2.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect - github.com/dgraph-io/ristretto v0.1.1 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/eapache/channels v1.1.0 // indirect github.com/eapache/queue v1.1.0 // indirect - github.com/eko/gocache/lib/v4 v4.1.6 // indirect - github.com/eko/gocache/store/ristretto/v4 v4.2.2 // indirect github.com/fatih/color v1.17.0 // indirect github.com/frostbyte73/core v0.0.10 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect diff --git a/go.sum b/go.sum index 2ed8e37..1953a4e 100644 --- a/go.sum +++ b/go.sum @@ -1,9 +1,3 @@ -git.solsynth.dev/hydrogen/dealer v0.0.0-20240911145828-d734d617bfc8 h1:kWheneSdSySG5tz9TAXrtr546JdMpQZTyWDFk4jeGwg= -git.solsynth.dev/hydrogen/dealer v0.0.0-20240911145828-d734d617bfc8/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= -git.solsynth.dev/hydrogen/dealer v0.0.0-20240914133539-4a48b980f0b3 h1:ucko8l9rC/XD/Gfk9lcQftyqb9tvHjL5f+aW3ydf1OI= -git.solsynth.dev/hydrogen/dealer v0.0.0-20240914133539-4a48b980f0b3/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= -git.solsynth.dev/hydrogen/dealer v0.0.0-20240917083841-b14c0240a75f h1:3jLpcws4/zmNUA60w1RtAtGNjcQd5NZCcbW5HQcUcvw= -git.solsynth.dev/hydrogen/dealer v0.0.0-20240917083841-b14c0240a75f/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= git.solsynth.dev/hydrogen/dealer v0.0.0-20240919131945-00c52eba6827 h1:1ACMPm2ArRpVNYrND/y/R6oPiuMfKe49fP+lG3mcNug= git.solsynth.dev/hydrogen/dealer v0.0.0-20240919131945-00c52eba6827/go.mod h1:Q51JPkKnV0UoOT/IRmdBh5CyfSlp7s8BRGzgooYHqkI= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= @@ -44,6 +38,7 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1 github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8= github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA= +github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA= github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= @@ -575,8 +570,6 @@ golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= -golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/pkg/internal/server/api/channels_api.go b/pkg/internal/server/api/channels_api.go index 0003a2f..6891fec 100644 --- a/pkg/internal/server/api/channels_api.go +++ b/pkg/internal/server/api/channels_api.go @@ -98,12 +98,20 @@ func listAvailableChannel(c *fiber.Ctx) error { } user := c.Locals("user").(models.Account) + tx := database.C + isDirect := c.QueryBool("direct", false) + if isDirect { + tx = tx.Where("type = ?", models.ChannelTypeDirect) + } else { + tx = tx.Where("type = ?", models.ChannelTypeCommon) + } + var err error var channels []models.Channel if val, ok := c.Locals("realm").(models.Realm); ok { - channels, err = services.ListAvailableChannel(user, val.ID) + channels, err = services.ListAvailableChannel(tx, user, val.ID) } else { - channels, err = services.ListAvailableChannel(user) + channels, err = services.ListAvailableChannel(tx, user) } if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) diff --git a/pkg/internal/services/channels.go b/pkg/internal/services/channels.go index be944d4..3778dfc 100644 --- a/pkg/internal/services/channels.go +++ b/pkg/internal/services/channels.go @@ -140,7 +140,7 @@ func ListChannelWithUser(user models.Account, realmId ...uint) ([]models.Channel return channels, nil } -func ListAvailableChannel(user models.Account, realmId ...uint) ([]models.Channel, error) { +func ListAvailableChannel(tx *gorm.DB, user models.Account, realmId ...uint) ([]models.Channel, error) { var channels []models.Channel var members []models.ChannelMember if err := database.C.Where(&models.ChannelMember{ @@ -153,7 +153,7 @@ func ListAvailableChannel(user models.Account, realmId ...uint) ([]models.Channe return item.ChannelID }) - tx := database.C.Preload("Realm").Where("id IN ?", idx) + tx = tx.Preload("Realm").Where("id IN ?", idx) if len(realmId) > 0 { tx = tx.Where("realm_id = ?", realmId) }