From b4f24f0ae0eb325ee2febaf790fc331fd9d967e9 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 25 Apr 2024 21:00:45 +0800 Subject: [PATCH] :sparkles: Able to get the creator details of channel --- .idea/Messaging.iml | 9 +++++++++ .idea/dataSources.xml | 19 +++++++++++++++++++ .idea/modules.xml | 8 ++++++++ .idea/sqldialects.xml | 6 ++++++ .idea/vcs.xml | 6 ++++++ pkg/server/channels_api.go | 6 ++---- pkg/server/{unified_ws.go => messages_ws.go} | 2 +- pkg/server/startup.go | 9 +++++---- pkg/services/channels.go | 7 ++++--- 9 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 .idea/Messaging.iml create mode 100644 .idea/dataSources.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/sqldialects.xml create mode 100644 .idea/vcs.xml rename pkg/server/{unified_ws.go => messages_ws.go} (96%) diff --git a/.idea/Messaging.iml b/.idea/Messaging.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/Messaging.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..56fcb14 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,19 @@ + + + + + postgresql + true + org.postgresql.Driver + jdbc:postgresql://localhost:5432/hy_messaging + $ProjectFileDir$ + + + postgresql + true + org.postgresql.Driver + jdbc:postgresql://im.solsynth.dev:5432/messaging + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..a3ae566 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sqldialects.xml b/.idea/sqldialects.xml new file mode 100644 index 0000000..6df4889 --- /dev/null +++ b/.idea/sqldialects.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pkg/server/channels_api.go b/pkg/server/channels_api.go index 30adfa9..54c96dc 100644 --- a/pkg/server/channels_api.go +++ b/pkg/server/channels_api.go @@ -10,10 +10,8 @@ import ( func getChannel(c *fiber.Ctx) error { alias := c.Params("channel") - var channel models.Channel - if err := database.C.Where(&models.Channel{ - Alias: alias, - }).First(&channel).Error; err != nil { + channel, err := services.GetChannelWithAlias(alias) + if err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) } diff --git a/pkg/server/unified_ws.go b/pkg/server/messages_ws.go similarity index 96% rename from pkg/server/unified_ws.go rename to pkg/server/messages_ws.go index 086ab99..a51475b 100644 --- a/pkg/server/unified_ws.go +++ b/pkg/server/messages_ws.go @@ -8,7 +8,7 @@ import ( "github.com/samber/lo" ) -func unifiedGateway(c *websocket.Conn) { +func messageGateway(c *websocket.Conn) { user := c.Locals("principal").(models.Account) // Push connection diff --git a/pkg/server/startup.go b/pkg/server/startup.go index bc3458c..f7b1274 100644 --- a/pkg/server/startup.go +++ b/pkg/server/startup.go @@ -1,13 +1,14 @@ package server import ( - "git.solsynth.dev/hydrogen/messaging/pkg" - "github.com/gofiber/contrib/websocket" - "github.com/gofiber/fiber/v2/middleware/favicon" "net/http" "strings" "time" + "git.solsynth.dev/hydrogen/messaging/pkg" + "github.com/gofiber/contrib/websocket" + "github.com/gofiber/fiber/v2/middleware/favicon" + "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cache" "github.com/gofiber/fiber/v2/middleware/cors" @@ -103,7 +104,7 @@ func NewServer() { channels.Post("/:channel/calls/ongoing/token", authMiddleware, exchangeCallToken) } - api.Get("/unified", authMiddleware, websocket.New(unifiedGateway)) + api.Get("/ws", authMiddleware, websocket.New(messageGateway)) } A.Use(favicon.New(favicon.Config{ diff --git a/pkg/services/channels.go b/pkg/services/channels.go index adb151a..5761f5a 100644 --- a/pkg/services/channels.go +++ b/pkg/services/channels.go @@ -2,6 +2,7 @@ package services import ( "fmt" + "git.solsynth.dev/hydrogen/messaging/pkg/database" "git.solsynth.dev/hydrogen/messaging/pkg/models" "github.com/samber/lo" @@ -11,7 +12,7 @@ func GetChannel(id uint) (models.Channel, error) { var channel models.Channel if err := database.C.Where(models.Channel{ BaseModel: models.BaseModel{ID: id}, - }).First(&channel).Error; err != nil { + }).Preload("Account").First(&channel).Error; err != nil { return channel, err } @@ -22,7 +23,7 @@ func GetChannelWithAlias(alias string) (models.Channel, error) { var channel models.Channel if err := database.C.Where(models.Channel{ Alias: alias, - }).First(&channel).Error; err != nil { + }).Preload("Account").First(&channel).Error; err != nil { return channel, err } @@ -67,7 +68,7 @@ func GetAvailableChannel(id uint, user models.Account) (models.Channel, models.C func ListChannel() ([]models.Channel, error) { var channels []models.Channel - if err := database.C.Find(&channels).Error; err != nil { + if err := database.C.Preload("Account").Find(&channels).Error; err != nil { return channels, err }