Skip to content

Commit 5054919

Browse files
authored
feat: self hosted customization (#100)
* option for restrict uploads only to admins * feat: allow to customize help text
1 parent e2cc11a commit 5054919

File tree

6 files changed

+68
-13
lines changed

6 files changed

+68
-13
lines changed

.env.example

+2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ SFB_REDIS=redis://localhost:6379
1111
SFB_REDIS_MAX_OPEN_CONNS=5
1212
SFB_REDIS_MAX_IDLE_CONNS=0
1313

14+
# SFB_IS_USERS_CAN_UPLOAD_FILES=false
15+
1416
SFB_ADDR=:8000
1517
SFB_SECRET_ID_SALT=-secret-1234-

bot/bot.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,25 @@ type Bot struct {
3535
adminSrv *service.Admin
3636
chatSrv *service.Chat
3737

38+
textHelp string
39+
3840
handler tg.Handler
3941
}
4042

4143
func (bot *Bot) Self() tgbotapi.User {
4244
return bot.client.Self
4345
}
4446

45-
func New(buildInfo pkg.BuildInfo, client *tgbotapi.BotAPI, state state.Store, authSrv *service.Auth, docSrv *service.File, adminSrv *service.Admin, chatSrv *service.Chat) (*Bot, error) {
47+
func New(
48+
buildInfo pkg.BuildInfo,
49+
client *tgbotapi.BotAPI,
50+
state state.Store,
51+
authSrv *service.Auth,
52+
docSrv *service.File,
53+
adminSrv *service.Admin,
54+
chatSrv *service.Chat,
55+
textHelp string,
56+
) (*Bot, error) {
4657

4758
bot := &Bot{
4859
buildInfo: buildInfo,
@@ -53,6 +64,8 @@ func New(buildInfo pkg.BuildInfo, client *tgbotapi.BotAPI, state state.Store, au
5364
fileSrv: docSrv,
5465
adminSrv: adminSrv,
5566
chatSrv: chatSrv,
67+
68+
textHelp: textHelp,
5669
}
5770

5871
// bot.client.Debug = true
@@ -152,7 +165,7 @@ func (bot *Bot) onUpdate(ctx context.Context, update *tgbotapi.Update) error {
152165
}
153166

154167
if msg.Text == textButtonAbout {
155-
answer := bot.newAnswerMsg(msg, textStart)
168+
answer := bot.newAnswerMsg(msg, bot.getTextStart())
156169
answer.ParseMode = mdv2
157170
return bot.send(ctx, answer)
158171
}

bot/handler_common.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const (
2121
)
2222

2323
var (
24+
textHello = "Привет\\! 👋\n"
25+
2426
textHelp = dedent.Dedent(`
2527
Я помогу тебе поделиться любым медиафайлом \(фото, видео, документы, аудио, голосовые\) с подписчиками твоего канала\.
2628
Отправь любой из перечисленных файлов, а я в ответ дам тебе ссылку\. Желательно указать подпись, чтобы человек не забыл кто ему это пошарил\.
@@ -32,11 +34,27 @@ var (
3234
Новости и обновления: @share\_file\_news
3335
`)
3436

35-
textStart = "Привет\\! 👋\n" + textHelp
37+
textStart = textHello + textHelp
3638
)
3739

40+
func (bot *Bot) getTextStart() string {
41+
if bot.textHelp != "" {
42+
return textHello + bot.textHelp
43+
}
44+
45+
return textStart
46+
}
47+
48+
func (bot *Bot) getTextHelp() string {
49+
if bot.textHelp != "" {
50+
return bot.textHelp
51+
}
52+
53+
return textHelp
54+
}
55+
3856
func (bot *Bot) onHelp(ctx context.Context, msg *tgbotapi.Message) error {
39-
answer := bot.newAnswerMsg(msg, textHelp)
57+
answer := bot.newAnswerMsg(msg, bot.getTextHelp())
4058
answer.ParseMode = mdv2
4159
return bot.send(ctx, answer)
4260
}
@@ -80,7 +98,7 @@ func (bot *Bot) onStart(ctx context.Context, msg *tgbotapi.Message) error {
8098
}
8199
}
82100

83-
answer := bot.newAnswerMsg(msg, textStart)
101+
answer := bot.newAnswerMsg(msg, bot.getTextStart())
84102
return bot.send(ctx, answer)
85103
}
86104

bot/handler_file.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,15 @@ func (bot *Bot) onFile(ctx context.Context, msg *tgbotapi.Message) error {
280280

281281
file, err := bot.fileSrv.AddFile(ctx, user, inputFile)
282282

283-
if err != nil {
283+
switch {
284+
case errors.Is(err, service.ErrUsersCantUploadFiles):
285+
_ = bot.sendText(ctx,
286+
user.ID,
287+
"✋ Загрузка файлов доступна только администраторам ботам",
288+
)
289+
290+
return nil
291+
case err != nil:
284292
_ = bot.sendText(ctx,
285293
user.ID,
286294
"⚠️ Что-то пошло не так при добавлении файла",
@@ -572,7 +580,7 @@ func (bot *Bot) onFileRestrictionsChatCheck(
572580
}
573581

574582
func (bot *Bot) onPublicFileHelp(ctx context.Context, cbq *tgbotapi.CallbackQuery) error {
575-
answer := tgbotapi.NewMessage(cbq.Message.Chat.ID, textStart)
583+
answer := tgbotapi.NewMessage(cbq.Message.Chat.ID, bot.getTextStart())
576584
answer.ParseMode = mdv2
577585
return bot.send(ctx, answer)
578586
}

main.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ type Config struct {
5454
SecretIDSalt string `required:"true" split_words:"true"`
5555

5656
DryRun bool `default:"false" split_words:"true"`
57+
58+
IsUsersCanUploadFiles bool `default:"true" split_words:"true"`
59+
TextHelp string `split_words:"true"`
5760
}
5861

5962
func (cfg Config) getEnv() string {
@@ -251,11 +254,12 @@ func run(ctx context.Context) error {
251254
}
252255

253256
fileSrv := &service.File{
254-
File: pg.File(),
255-
Chat: pg.Chat(),
256-
Download: pg.Download(),
257-
Telegram: tgClient,
258-
Redis: rdb,
257+
File: pg.File(),
258+
Chat: pg.Chat(),
259+
Download: pg.Download(),
260+
Telegram: tgClient,
261+
Redis: rdb,
262+
IsUsersCanUploadFiles: cfg.IsUsersCanUploadFiles,
259263
}
260264

261265
adminSrv := &service.Admin{
@@ -273,7 +277,7 @@ func run(ctx context.Context) error {
273277
Download: pg.Download(),
274278
}
275279

276-
tgBot, err := bot.New(buildInfo, tgClient, botState, authSrv, fileSrv, adminSrv, chatSrv)
280+
tgBot, err := bot.New(buildInfo, tgClient, botState, authSrv, fileSrv, adminSrv, chatSrv, cfg.TextHelp)
277281
if err != nil {
278282
return errors.Wrap(err, "init bot")
279283
}

service/file.go

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type File struct {
2020
Telegram *tgbotapi.BotAPI
2121
Redis redis.UniversalClient
2222
Download core.DownloadStore
23+
24+
IsUsersCanUploadFiles bool
2325
}
2426

2527
type InputFile struct {
@@ -50,11 +52,19 @@ func (srv *File) newOwnedFile(ctx context.Context, doc *core.File) (*OwnedFile,
5052
}, nil
5153
}
5254

55+
var (
56+
ErrUsersCantUploadFiles = errors.New("users can't upload files")
57+
)
58+
5359
func (srv *File) AddFile(
5460
ctx context.Context,
5561
user *core.User,
5662
in *InputFile,
5763
) (*OwnedFile, error) {
64+
if !user.IsAdmin && !srv.IsUsersCanUploadFiles {
65+
return nil, ErrUsersCantUploadFiles
66+
}
67+
5868
doc := core.NewFile(
5969
in.FileID,
6070
in.Caption,

0 commit comments

Comments
 (0)