🐛 Bug fixes on getting user

This commit is contained in:
LittleSheep 2024-06-22 18:29:41 +08:00
parent c8431281d7
commit c883a52dd2
8 changed files with 98 additions and 23 deletions

View File

@ -2,6 +2,7 @@ package api
import ( import (
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/services" "git.solsynth.dev/hydrogen/messaging/pkg/internal/services"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
@ -45,7 +46,10 @@ func getOngoingCall(c *fiber.Ctx) error {
} }
func startCall(c *fiber.Ctx) error { func startCall(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
alias := c.Params("channel") alias := c.Params("channel")
var channel models.Channel var channel models.Channel
@ -72,7 +76,10 @@ func startCall(c *fiber.Ctx) error {
} }
func endCall(c *fiber.Ctx) error { func endCall(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
alias := c.Params("channel") alias := c.Params("channel")
var channel models.Channel var channel models.Channel
@ -105,7 +112,10 @@ func endCall(c *fiber.Ctx) error {
} }
func exchangeCallToken(c *fiber.Ctx) error { func exchangeCallToken(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
alias := c.Params("channel") alias := c.Params("channel")
var channel models.Channel var channel models.Channel

View File

@ -2,6 +2,7 @@ package api
import ( import (
"fmt" "fmt"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts" "git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
@ -33,7 +34,10 @@ func listChannelMembers(c *fiber.Ctx) error {
func getMyChannelMembership(c *fiber.Ctx) error { func getMyChannelMembership(c *fiber.Ctx) error {
alias := c.Params("channel") alias := c.Params("channel")
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
var err error var err error
var channel models.Channel var channel models.Channel
@ -54,7 +58,10 @@ func getMyChannelMembership(c *fiber.Ctx) error {
} }
func addChannelMember(c *fiber.Ctx) error { func addChannelMember(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
alias := c.Params("channel") alias := c.Params("channel")
var data struct { var data struct {
@ -95,7 +102,10 @@ func addChannelMember(c *fiber.Ctx) error {
} }
func removeChannelMember(c *fiber.Ctx) error { func removeChannelMember(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
alias := c.Params("channel") alias := c.Params("channel")
var data struct { var data struct {
@ -137,7 +147,10 @@ func removeChannelMember(c *fiber.Ctx) error {
} }
func editMyChannelMembership(c *fiber.Ctx) error { func editMyChannelMembership(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
alias := c.Params("channel") alias := c.Params("channel")
var data struct { var data struct {
@ -183,7 +196,10 @@ func editMyChannelMembership(c *fiber.Ctx) error {
} }
func joinChannel(c *fiber.Ctx) error { func joinChannel(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
alias := c.Params("channel") alias := c.Params("channel")
var channel models.Channel var channel models.Channel
@ -211,7 +227,10 @@ func joinChannel(c *fiber.Ctx) error {
} }
func leaveChannel(c *fiber.Ctx) error { func leaveChannel(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
alias := c.Params("channel") alias := c.Params("channel")
var channel models.Channel var channel models.Channel

View File

@ -2,6 +2,7 @@ package api
import ( import (
"fmt" "fmt"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts" "git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
@ -28,7 +29,10 @@ func getChannel(c *fiber.Ctx) error {
} }
func getChannelIdentity(c *fiber.Ctx) error { func getChannelIdentity(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
alias := c.Params("channel") alias := c.Params("channel")
var err error var err error
@ -61,7 +65,10 @@ func listChannel(c *fiber.Ctx) error {
} }
func listOwnedChannel(c *fiber.Ctx) error { func listOwnedChannel(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
var err error var err error
var channels []models.Channel var channels []models.Channel
@ -78,7 +85,10 @@ func listOwnedChannel(c *fiber.Ctx) error {
} }
func listAvailableChannel(c *fiber.Ctx) error { func listAvailableChannel(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
var err error var err error
var channels []models.Channel var channels []models.Channel
@ -95,7 +105,10 @@ func listAvailableChannel(c *fiber.Ctx) error {
} }
func createChannel(c *fiber.Ctx) error { func createChannel(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
var data struct { var data struct {
Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"` Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"`
@ -146,7 +159,10 @@ func createChannel(c *fiber.Ctx) error {
} }
func editChannel(c *fiber.Ctx) error { func editChannel(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
id, _ := c.ParamsInt("channelId", 0) id, _ := c.ParamsInt("channelId", 0)
var data struct { var data struct {
@ -196,7 +212,10 @@ func editChannel(c *fiber.Ctx) error {
} }
func deleteChannel(c *fiber.Ctx) error { func deleteChannel(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
id, _ := c.ParamsInt("channelId", 0) id, _ := c.ParamsInt("channelId", 0)
tx := database.C.Where(&models.Channel{BaseModel: models.BaseModel{ID: uint(id)}}) tx := database.C.Where(&models.Channel{BaseModel: models.BaseModel{ID: uint(id)}})

View File

@ -2,6 +2,7 @@ package api
import ( import (
"fmt" "fmt"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts" "git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
@ -11,7 +12,10 @@ import (
) )
func createDirectChannel(c *fiber.Ctx) error { func createDirectChannel(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
var data struct { var data struct {
Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"` Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"`

View File

@ -1,6 +1,7 @@
package api package api
import ( import (
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
"github.com/gofiber/contrib/websocket" "github.com/gofiber/contrib/websocket"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -44,6 +45,11 @@ func MapAPIs(app *fiber.App) {
channels.Post("/:channel/calls/ongoing/token", exchangeCallToken) channels.Post("/:channel/calls/ongoing/token", exchangeCallToken)
} }
api.Get("/ws", websocket.New(messageGateway)) api.Use(func(c *fiber.Ctx) error {
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
return c.Next()
}).Get("/ws", websocket.New(messageGateway))
} }
} }

View File

@ -3,6 +3,7 @@ package api
import ( import (
"fmt" "fmt"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts" "git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/services" "git.solsynth.dev/hydrogen/messaging/pkg/internal/services"
@ -11,7 +12,10 @@ import (
) )
func listMessage(c *fiber.Ctx) error { func listMessage(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
take := c.QueryInt("take", 0) take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0) offset := c.QueryInt("offset", 0)
alias := c.Params("channel") alias := c.Params("channel")
@ -42,7 +46,10 @@ func listMessage(c *fiber.Ctx) error {
} }
func newMessage(c *fiber.Ctx) error { func newMessage(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
alias := c.Params("channel") alias := c.Params("channel")
var data struct { var data struct {
@ -121,7 +128,10 @@ func newMessage(c *fiber.Ctx) error {
} }
func editMessage(c *fiber.Ctx) error { func editMessage(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
alias := c.Params("channel") alias := c.Params("channel")
messageId, _ := c.ParamsInt("messageId", 0) messageId, _ := c.ParamsInt("messageId", 0)
@ -175,7 +185,10 @@ func editMessage(c *fiber.Ctx) error {
} }
func deleteMessage(c *fiber.Ctx) error { func deleteMessage(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
alias := c.Params("channel") alias := c.Params("channel")
messageId, _ := c.ParamsInt("messageId", 0) messageId, _ := c.ParamsInt("messageId", 0)

View File

@ -9,7 +9,7 @@ import (
) )
func messageGateway(c *websocket.Conn) { func messageGateway(c *websocket.Conn) {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
// Push connection // Push connection
services.ClientRegister(user, c) services.ClientRegister(user, c)

View File

@ -2,12 +2,16 @@ package api
import ( import (
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func getUserinfo(c *fiber.Ctx) error { func getUserinfo(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("user").(models.Account)
if err := gap.H.EnsureAuthenticated(c); err != nil {
return err
}
var data models.Account var data models.Account
if err := database.C. if err := database.C.