Skip to content
Closed
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ DEFAULT_ENABLE_SILENT=false
DEFAULT_ENABLE_NSFW=false
DEFAULT_MEDIA_ALBUM_LIMIT=10
DEFAULT_LANGUAGE=en
DEFAULT_HIGH_QUALITY_VIDEOS=false

# other
REPO_URL=https://github.com/govdbot/govd
Expand All @@ -46,4 +47,4 @@ AUTOMATIC_LANGUAGE_DETECTION=true
# dev
PGWEB_PORT=8081
PGWEB_USER=admin
PGWEB_PASSWORD=password
PGWEB_PASSWORD=password
15 changes: 15 additions & 0 deletions internal/bot/handlers/settings/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,21 @@ var botSettings = []BotSettings{
return res.DeleteLinks
},
},
{
ID: "high_quality_videos",
ButtonKey: localization.HighQualityVideosButton.ID,
DescriptionKey: localization.HighQualityVideosSettingsMessage.ID,

Type: SettingsTypeToggle,
Scope: SettingsScopeGroup,

ToggleFunc: func(ctx context.Context, chatID int64) error {
return database.Q().ToggleChatHighQualityVideos(ctx, chatID)
},
GetCurrentValueFunc: func(res *database.GetOrCreateChatRow) any {
return res.HighQualityVideos
},
},
{
ID: "disabled_extractors",
ButtonKey: localization.ExtractorsButton.ID,
Expand Down
19 changes: 13 additions & 6 deletions internal/config/env.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"os"
"time"

"github.com/PaulSonOfLars/gotgbot/v2"
Expand Down Expand Up @@ -40,6 +41,11 @@ func loadFromEnv() {
parseEnvInt32Range("DEFAULT_MEDIA_ALBUM_LIMIT", &Env.DefaultMediaAlbumLimit, 1, 20, false)
parseEnvLanguage("DEFAULT_LANGUAGE", &Env.DefaultLanguage, false)
parseEnvBool("DEFAULT_DELETE_LINKS", &Env.DefaultDeleteLinks, false)
if _, ok := os.LookupEnv("DEFAULT_HIGH_QUALITY_VIDEOS"); ok {
parseEnvBool("DEFAULT_HIGH_QUALITY_VIDEOS", &Env.DefaultHighQualityVideos, false)
} else {
parseEnvBool("DEFAULT_ENABLE_HIGH_QUALITY_VIDEOS", &Env.DefaultHighQualityVideos, false) // backward compatibility
}
parseEnvBool("AUTOMATIC_LANGUAGE_DETECTION", &Env.AutomaticLanguageDetection, false)
}

Expand All @@ -64,12 +70,13 @@ func GetDefaultConfig() *EnvConfig {
CaptionsHeader: "<a href='{{url}}'>source</a> - @{{username}}",
CaptionsDescription: "<blockquote expandable>{{text}}</blockquote>",

DefaultCaptions: true,
DefaultSilent: false,
DefaultNSFW: false,
DefaultMediaAlbumLimit: 10,
DefaultLanguage: "en",
DefaultDeleteLinks: false,
DefaultCaptions: true,
DefaultSilent: false,
DefaultNSFW: false,
DefaultMediaAlbumLimit: 10,
DefaultLanguage: "en",
DefaultDeleteLinks: false,
DefaultHighQualityVideos: false,

AutomaticLanguageDetection: true,
}
Expand Down
13 changes: 7 additions & 6 deletions internal/config/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ type EnvConfig struct {
CaptionsHeader string
CaptionsDescription string

DefaultCaptions bool
DefaultSilent bool
DefaultNSFW bool
DefaultMediaAlbumLimit int32
DefaultLanguage string
DefaultDeleteLinks bool
DefaultCaptions bool
DefaultSilent bool
DefaultNSFW bool
DefaultMediaAlbumLimit int32
DefaultLanguage string
DefaultDeleteLinks bool
DefaultHighQualityVideos bool

AutomaticLanguageDetection bool
}
Expand Down
32 changes: 32 additions & 0 deletions internal/core/high_quality.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package core

import (
"github.com/govdbot/govd/internal/database"
"github.com/govdbot/govd/internal/models"
)

func shouldUseHighQualityVideoUpload(chat *database.GetOrCreateChatRow, extractorID string) bool {
if chat == nil || !chat.HighQualityVideos {
return false
}
switch extractorID {
case "instagram", "tiktok":
return true
default:
return false
}
}

func shouldUploadAsHighQualityVideo(
chat *database.GetOrCreateChatRow,
media *models.Media,
format *models.MediaFormat,
) bool {
if media == nil || format == nil {
return false
}
if format.Type != database.MediaTypeVideo {
return false
}
return shouldUseHighQualityVideoUpload(chat, media.ExtractorID)
}
12 changes: 12 additions & 0 deletions internal/core/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ func SendFormats(
if i == 0 {
caption = options.Caption
}
highQualityVideo := shouldUploadAsHighQualityVideo(
extractorCtx.Chat,
media,
f.Format,
)
inputMedia, err := f.Format.GetInputMedia(
f.FilePath, f.ThumbnailFilePath,
caption, options.IsSpoiler,
highQualityVideo,
)
if err != nil {
return nil, fmt.Errorf("failed to get input media: %w", err)
Expand Down Expand Up @@ -149,9 +155,15 @@ func SendInlineFormats(
fileID := util.GetMessageFileID(&msg)
format.Format.FileID = fileID

highQualityVideo := shouldUploadAsHighQualityVideo(
extractorCtx.Chat,
media,
format.Format,
)
inputMedia, err := format.Format.GetInputMedia(
format.FilePath, format.ThumbnailFilePath,
options.Caption, options.IsSpoiler,
highQualityVideo,
)
if err != nil {
return err
Expand Down
31 changes: 18 additions & 13 deletions internal/database/chat.sql_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE settings ADD COLUMN high_quality_videos BOOLEAN NOT NULL DEFAULT FALSE;
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
ALTER TABLE settings DROP COLUMN IF EXISTS high_quality_videos;
-- +goose StatementEnd
1 change: 1 addition & 0 deletions internal/database/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions internal/database/queries/chat.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ WITH upsert_chat AS (
RETURNING *
),
upsert_settings AS (
INSERT INTO settings (chat_id, language, captions, silent, nsfw, media_album_limit, delete_links)
VALUES (@chat_id, @language, @captions, @silent, @nsfw, @media_album_limit, @delete_links)
INSERT INTO settings (chat_id, language, captions, silent, nsfw, media_album_limit, delete_links, high_quality_videos)
VALUES (@chat_id, @language, @captions, @silent, @nsfw, @media_album_limit, @delete_links, @high_quality_videos)
ON CONFLICT (chat_id) DO UPDATE SET
language = CASE
WHEN settings.language = 'XX' THEN EXCLUDED.language
Expand All @@ -32,6 +32,7 @@ SELECT
s.silent,
s.language,
s.disabled_extractors,
s.delete_links
s.delete_links,
s.high_quality_videos
FROM final_chat c
JOIN final_settings s ON s.chat_id = c.chat_id;
JOIN final_settings s ON s.chat_id = c.chat_id;
7 changes: 6 additions & 1 deletion internal/database/queries/settings.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ WHERE chat_id = @chat_id;
-- name: ToggleChatDeleteLinks :exec
UPDATE settings
SET delete_links = NOT delete_links, updated_at = CURRENT_TIMESTAMP
WHERE chat_id = @chat_id;
WHERE chat_id = @chat_id;

-- name: ToggleChatHighQualityVideos :exec
UPDATE settings
SET high_quality_videos = NOT high_quality_videos, updated_at = CURRENT_TIMESTAMP
WHERE chat_id = @chat_id;
11 changes: 11 additions & 0 deletions internal/database/settings.sql_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/localization/locales/active.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CaptionsSettingsMessage = "when enabled, adds original description to downloaded
CloseButton = "close"
DeleteProcessedButton = "links"
DeleteProcessedSettingsMessage = "when enabled, deletes the user's original message after successfully processing the link"
HighQualityVideosButton = "high-quality video"
HighQualityVideosSettingsMessage = "when enabled, TikTok and Instagram videos are sent as media with high-quality preference"
DisabledButton = "disabled"
DisabledExtractorsSettingsMessage = "select which extractors should be disabled. links from disabled extractors will be ignored by the bot"
EnabledButton = "enabled"
Expand Down
8 changes: 8 additions & 0 deletions internal/localization/locales/active.it.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ other = "formato immagine non supportato"
hash = "sha1-90f775f104687d04dc6c10f0a6b929d9f9060a9b"
other = "estrattori"

[HighQualityVideosButton]
hash = "sha1-7f4f46c8e472f4e4354de65fa9ac9f4f95dfac28"
other = "video di alta qualità"

[HighQualityVideosSettingsMessage]
hash = "sha1-59d702bff8d797a87e5e1f98db8c3f3d34e9f71d"
other = "quando abilitato, i video TikTok e Instagram vengono inviati come media con preferenza di alta qualità"

[GroupSettingsMessage]
hash = "sha1-dbc9223b568195bdd978bf68acd969c46f28b6b3"
other = "utilizza i pulsanti sottostanti per modificare le impostazioni del bot per questo gruppo"
Expand Down
8 changes: 8 additions & 0 deletions internal/localization/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ var (
ID: "DeleteProcessedSettingsMessage",
Other: "when enabled, deletes the user's original message after successfully processing the link",
}
HighQualityVideosButton = &i18n.Message{
ID: "HighQualityVideosButton",
Other: "high-quality video",
}
HighQualityVideosSettingsMessage = &i18n.Message{
ID: "HighQualityVideosSettingsMessage",
Other: "when enabled, TikTok and Instagram videos are sent as media with high-quality preference",
}
SupportedExtractorsMessage = &i18n.Message{
ID: "SupportedExtractorsMessage",
Other: "list of supported extractors by the bot",
Expand Down
Loading