change advertise timing

peertube
Namekuji 2023-01-20 10:39:51 -05:00
rodzic 7ceb656bdf
commit 3469d62210
3 zmienionych plików z 63 dodań i 54 usunięć

1
.gitignore vendored
Wyświetl plik

@ -188,3 +188,4 @@ config/*
# Ignore data directories # Ignore data directories
mongo/ mongo/
redis/ redis/
cache/

48
room.go
Wyświetl plik

@ -4,17 +4,13 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"net/http" "net/http"
"strings"
"time" "time"
"github.com/jaevor/go-nanoid" "github.com/jaevor/go-nanoid"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/livekit/protocol/auth" "github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit" "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/bson"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
) )
@ -342,46 +338,6 @@ func joinRoomHandler(c echo.Context) (err error) {
c.Logger().Error(err) c.Logger().Error(err)
return echo.NewHTTPError(http.StatusConflict) 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) 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) { func findRoomByID(ctx context.Context, roomID string) (*Room, error) {
if err := mainValidator.Var(&roomID, "required,printascii"); err != nil {
return nil, err
}
var room Room var room Room
collRoom := mainDB.Collection(COLLECTION_ROOM) collRoom := mainDB.Collection(COLLECTION_ROOM)
if err := collRoom.FindOne(ctx, bson.D{{Key: "room_id", Value: roomID}}).Decode(&room); err != nil { if err := collRoom.FindOne(ctx, bson.D{{Key: "room_id", Value: roomID}}).Decode(&room); err != nil {

Wyświetl plik

@ -1,11 +1,15 @@
package main package main
import ( import (
"fmt"
"net/http" "net/http"
"strings"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/livekit/protocol/auth" "github.com/livekit/protocol/auth"
"github.com/livekit/protocol/webhook" "github.com/livekit/protocol/webhook"
mastodon "github.com/mattn/go-mastodon"
"github.com/nicksnyder/go-i18n/v2/i18n"
) )
func livekitWebhookHandler(c echo.Context) error { func livekitWebhookHandler(c echo.Context) error {
@ -17,19 +21,63 @@ func livekitWebhookHandler(c echo.Context) error {
} }
if event.GetEvent() == webhook.EventRoomFinished { if event.GetEvent() == webhook.EventRoomFinished {
roomID := event.GetRoom().GetName() room, err := findRoomByID(c.Request().Context(), event.GetRoom().GetName())
if err := mainValidator.Var(&roomID, "required,printascii"); err == nil { if err != nil {
room, err := findRoomByID(c.Request().Context(), roomID) c.Logger().Error(err)
if err == nil && room.EndedAt.IsZero() { return echo.NewHTTPError(http.StatusNotFound)
if err := endRoom(c.Request().Context(), room); err != nil { }
c.Logger().Error(err) if room.EndedAt.IsZero() {
return echo.NewHTTPError(http.StatusInternalServerError) 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)
} }