diff --git a/.gitignore b/.gitignore index 7429352..c60b633 100644 --- a/.gitignore +++ b/.gitignore @@ -188,3 +188,4 @@ config/* # Ignore data directories mongo/ redis/ +cache/ diff --git a/room.go b/room.go index aca6f6c..50f87c2 100644 --- a/room.go +++ b/room.go @@ -4,17 +4,13 @@ import ( "context" "encoding/json" "errors" - "fmt" "net/http" - "strings" "time" "github.com/jaevor/go-nanoid" "github.com/labstack/echo/v4" "github.com/livekit/protocol/auth" "github.com/livekit/protocol/livekit" - mastodon "github.com/mattn/go-mastodon" - "github.com/nicksnyder/go-i18n/v2/i18n" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" ) @@ -342,46 +338,6 @@ func joinRoomHandler(c echo.Context) (err error) { c.Logger().Error(err) return echo.NewHTTPError(http.StatusConflict) } - - // Have the bot advertise the room - if mainConfig.Bot.Enable && room.Advertise != "" && room.Restriction == EVERYONE { - botClient := mastodon.NewClient(&mastodon.Config{ - Server: mainConfig.Bot.Server.String(), - ClientID: mainConfig.Bot.ClientID, - ClientSecret: mainConfig.Bot.ClientSecret, - AccessToken: mainConfig.Bot.AccessToken, - }) - botClient.UserAgent = USER_AGENT - - localizer := i18n.NewLocalizer(localeBundle, room.Advertise) - header := localizer.MustLocalize(&i18n.LocalizeConfig{ - DefaultMessage: &i18n.Message{ - ID: "Advertise", - Other: "@{{.Host}} is streaming now!", - }, - TemplateData: map[string]string{ - "Host": room.Host.Webfinger, - }, - }) - - messages := []string{ - header, - fmt.Sprintf(":audon: %s\n🎙️ https://%s/r/%s", room.Title, mainConfig.LocalDomain, room.RoomID), - } - if room.Description != "" { - messages = append(messages, room.Description) - } - messages = append(messages, "#Audon") - message := strings.Join(messages, "\n\n") - - if _, err := botClient.PostStatus(c.Request().Context(), &mastodon.Toot{ - Status: message, - Language: room.Advertise, - Visibility: "public", - }); err != nil { - c.Logger().Error(err) - } - } } return c.JSON(http.StatusOK, resp) @@ -525,6 +481,10 @@ func getRoomInLivekit(ctx context.Context, roomID string) (*livekit.Room, bool) } func findRoomByID(ctx context.Context, roomID string) (*Room, error) { + if err := mainValidator.Var(&roomID, "required,printascii"); err != nil { + return nil, err + } + var room Room collRoom := mainDB.Collection(COLLECTION_ROOM) if err := collRoom.FindOne(ctx, bson.D{{Key: "room_id", Value: roomID}}).Decode(&room); err != nil { diff --git a/webhooks.go b/webhooks.go index db51484..fe99c44 100644 --- a/webhooks.go +++ b/webhooks.go @@ -1,11 +1,15 @@ package main import ( + "fmt" "net/http" + "strings" "github.com/labstack/echo/v4" "github.com/livekit/protocol/auth" "github.com/livekit/protocol/webhook" + mastodon "github.com/mattn/go-mastodon" + "github.com/nicksnyder/go-i18n/v2/i18n" ) func livekitWebhookHandler(c echo.Context) error { @@ -17,19 +21,63 @@ func livekitWebhookHandler(c echo.Context) error { } if event.GetEvent() == webhook.EventRoomFinished { - roomID := event.GetRoom().GetName() - if err := mainValidator.Var(&roomID, "required,printascii"); err == nil { - room, err := findRoomByID(c.Request().Context(), roomID) - if err == nil && room.EndedAt.IsZero() { - if err := endRoom(c.Request().Context(), room); err != nil { - c.Logger().Error(err) - return echo.NewHTTPError(http.StatusInternalServerError) - } + room, err := findRoomByID(c.Request().Context(), event.GetRoom().GetName()) + if err != nil { + c.Logger().Error(err) + return echo.NewHTTPError(http.StatusNotFound) + } + if room.EndedAt.IsZero() { + if err := endRoom(c.Request().Context(), room); err != nil { + c.Logger().Error(err) + return echo.NewHTTPError(http.StatusInternalServerError) } } + } else if event.GetEvent() == webhook.EventRoomStarted { + // Have the bot advertise the room + room, err := findRoomByID(c.Request().Context(), event.GetRoom().GetName()) + if err != nil { + c.Logger().Error(err) + return echo.NewHTTPError(http.StatusNotFound) + } + if err == nil && mainConfig.Bot.Enable && room.Advertise != "" && room.Restriction == EVERYONE { + botClient := mastodon.NewClient(&mastodon.Config{ + Server: mainConfig.Bot.Server.String(), + ClientID: mainConfig.Bot.ClientID, + ClientSecret: mainConfig.Bot.ClientSecret, + AccessToken: mainConfig.Bot.AccessToken, + }) + botClient.UserAgent = USER_AGENT - return c.NoContent(http.StatusOK) + localizer := i18n.NewLocalizer(localeBundle, room.Advertise) + header := localizer.MustLocalize(&i18n.LocalizeConfig{ + DefaultMessage: &i18n.Message{ + ID: "Advertise", + Other: "@{{.Host}} is streaming now!", + }, + TemplateData: map[string]string{ + "Host": room.Host.Webfinger, + }, + }) + + messages := []string{ + header, + fmt.Sprintf(":audon: %s\n🎙️ https://%s/r/%s", room.Title, mainConfig.LocalDomain, room.RoomID), + } + if room.Description != "" { + messages = append(messages, room.Description) + } + messages = append(messages, "#Audon") + message := strings.Join(messages, "\n\n") + + if _, err := botClient.PostStatus(c.Request().Context(), &mastodon.Toot{ + Status: message, + Language: room.Advertise, + Visibility: "public", + }); err != nil { + c.Logger().Error(err) + } + } } - return echo.NewHTTPError(http.StatusNotFound) + return c.NoContent(http.StatusOK) }