mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-25 07:00:48 +09:00
refactor(core): migrate channel identity and binding across app
Align channel identity and bind flow across backend and app-facing layers, including generated swagger artifacts and package lock updates while excluding docs content changes.
This commit is contained in:
@@ -9,26 +9,26 @@ import (
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
|
||||
"github.com/memohai/memoh/internal/accounts"
|
||||
"github.com/memohai/memoh/internal/auth"
|
||||
"github.com/memohai/memoh/internal/bots"
|
||||
"github.com/memohai/memoh/internal/identity"
|
||||
"github.com/memohai/memoh/internal/settings"
|
||||
"github.com/memohai/memoh/internal/users"
|
||||
)
|
||||
|
||||
type SettingsHandler struct {
|
||||
service *settings.Service
|
||||
botService *bots.Service
|
||||
userService *users.Service
|
||||
logger *slog.Logger
|
||||
service *settings.Service
|
||||
botService *bots.Service
|
||||
accountService *accounts.Service
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func NewSettingsHandler(log *slog.Logger, service *settings.Service, botService *bots.Service, userService *users.Service) *SettingsHandler {
|
||||
func NewSettingsHandler(log *slog.Logger, service *settings.Service, botService *bots.Service, accountService *accounts.Service) *SettingsHandler {
|
||||
return &SettingsHandler{
|
||||
service: service,
|
||||
botService: botService,
|
||||
userService: userService,
|
||||
logger: log.With(slog.String("handler", "settings")),
|
||||
service: service,
|
||||
botService: botService,
|
||||
accountService: accountService,
|
||||
logger: log.With(slog.String("handler", "settings")),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func (h *SettingsHandler) Register(e *echo.Echo) {
|
||||
// @Failure 500 {object} ErrorResponse
|
||||
// @Router /bots/{bot_id}/settings [get]
|
||||
func (h *SettingsHandler) Get(c echo.Context) error {
|
||||
userID, err := h.requireUserID(c)
|
||||
channelIdentityID, err := h.requireChannelIdentityID(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -57,7 +57,7 @@ func (h *SettingsHandler) Get(c echo.Context) error {
|
||||
if botID == "" {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "bot id is required")
|
||||
}
|
||||
if _, err := h.authorizeBotAccess(c.Request().Context(), userID, botID); err != nil {
|
||||
if _, err := h.authorizeBotAccess(c.Request().Context(), channelIdentityID, botID); err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := h.service.GetBot(c.Request().Context(), botID)
|
||||
@@ -78,7 +78,7 @@ func (h *SettingsHandler) Get(c echo.Context) error {
|
||||
// @Router /bots/{bot_id}/settings [put]
|
||||
// @Router /bots/{bot_id}/settings [post]
|
||||
func (h *SettingsHandler) Upsert(c echo.Context) error {
|
||||
userID, err := h.requireUserID(c)
|
||||
channelIdentityID, err := h.requireChannelIdentityID(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -86,7 +86,7 @@ func (h *SettingsHandler) Upsert(c echo.Context) error {
|
||||
if botID == "" {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "bot id is required")
|
||||
}
|
||||
if _, err := h.authorizeBotAccess(c.Request().Context(), userID, botID); err != nil {
|
||||
if _, err := h.authorizeBotAccess(c.Request().Context(), channelIdentityID, botID); err != nil {
|
||||
return err
|
||||
}
|
||||
var req settings.UpsertRequest
|
||||
@@ -95,6 +95,9 @@ func (h *SettingsHandler) Upsert(c echo.Context) error {
|
||||
}
|
||||
resp, err := h.service.UpsertBot(c.Request().Context(), botID, req)
|
||||
if err != nil {
|
||||
if errors.Is(err, settings.ErrPersonalBotGuestAccessUnsupported) {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "personal bot does not support guest access")
|
||||
}
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return c.JSON(http.StatusOK, resp)
|
||||
@@ -109,7 +112,7 @@ func (h *SettingsHandler) Upsert(c echo.Context) error {
|
||||
// @Failure 500 {object} ErrorResponse
|
||||
// @Router /bots/{bot_id}/settings [delete]
|
||||
func (h *SettingsHandler) Delete(c echo.Context) error {
|
||||
userID, err := h.requireUserID(c)
|
||||
channelIdentityID, err := h.requireChannelIdentityID(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -117,7 +120,7 @@ func (h *SettingsHandler) Delete(c echo.Context) error {
|
||||
if botID == "" {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "bot id is required")
|
||||
}
|
||||
if _, err := h.authorizeBotAccess(c.Request().Context(), userID, botID); err != nil {
|
||||
if _, err := h.authorizeBotAccess(c.Request().Context(), channelIdentityID, botID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := h.service.Delete(c.Request().Context(), botID); err != nil {
|
||||
@@ -126,26 +129,26 @@ func (h *SettingsHandler) Delete(c echo.Context) error {
|
||||
return c.NoContent(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) requireUserID(c echo.Context) (string, error) {
|
||||
userID, err := auth.UserIDFromContext(c)
|
||||
func (h *SettingsHandler) requireChannelIdentityID(c echo.Context) (string, error) {
|
||||
channelIdentityID, err := auth.ChannelIdentityIDFromContext(c)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := identity.ValidateUserID(userID); err != nil {
|
||||
if err := identity.ValidateChannelIdentityID(channelIdentityID); err != nil {
|
||||
return "", echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
return userID, nil
|
||||
return channelIdentityID, nil
|
||||
}
|
||||
|
||||
func (h *SettingsHandler) authorizeBotAccess(ctx context.Context, actorID, botID string) (bots.Bot, error) {
|
||||
if h.botService == nil || h.userService == nil {
|
||||
func (h *SettingsHandler) authorizeBotAccess(ctx context.Context, channelIdentityID, botID string) (bots.Bot, error) {
|
||||
if h.botService == nil || h.accountService == nil {
|
||||
return bots.Bot{}, echo.NewHTTPError(http.StatusInternalServerError, "bot services not configured")
|
||||
}
|
||||
isAdmin, err := h.userService.IsAdmin(ctx, actorID)
|
||||
isAdmin, err := h.accountService.IsAdmin(ctx, channelIdentityID)
|
||||
if err != nil {
|
||||
return bots.Bot{}, echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
bot, err := h.botService.AuthorizeAccess(ctx, actorID, botID, isAdmin, bots.AccessPolicy{AllowPublicMember: false})
|
||||
bot, err := h.botService.AuthorizeAccess(ctx, channelIdentityID, botID, isAdmin, bots.AccessPolicy{AllowPublicMember: false})
|
||||
if err != nil {
|
||||
if errors.Is(err, bots.ErrBotNotFound) {
|
||||
return bots.Bot{}, echo.NewHTTPError(http.StatusNotFound, "bot not found")
|
||||
@@ -156,4 +159,4 @@ func (h *SettingsHandler) authorizeBotAccess(ctx context.Context, actorID, botID
|
||||
return bots.Bot{}, echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
return bot, nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user