Skip to content

Commit 0940258

Browse files
committed
fix(watch): enhance group media handling
1 parent 602fc25 commit 0940258

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

client/bot/handlers/watch.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,16 @@ func listenMediaMessageEvent(ch chan userclient.MediaMessageEvent) {
252252
logger.Errorf("Failed to get storage by user ID %d and name %s: %v", user.ChatID, user.DefaultStorage, err)
253253
continue
254254
}
255+
// Resolve the default directory path from user.DefaultDir
256+
var defaultDirPath string
257+
if user.DefaultDir != 0 {
258+
dir, err := database.GetDirByID(ctx, user.DefaultDir)
259+
if err != nil {
260+
logger.Warnf("Failed to get default dir for user %d: %v, using root", user.ChatID, err)
261+
} else {
262+
defaultDirPath = dir.Path
263+
}
264+
}
255265
switch user.FilenameStrategy {
256266
case fnamest.Message.String():
257267
file.SetName(tgutil.GenFileNameFromMessage(*file.Message()))
@@ -286,14 +296,14 @@ func listenMediaMessageEvent(ch chan userclient.MediaMessageEvent) {
286296

287297
if needAlbumHandling {
288298
// For media groups with NEW-FOR-ALBUM rule, collect all files of the same group
289-
watchMediaGroupMgr.addFile(event.ChatID, user.ID, file, time.Duration(config.C().Telegram.MediaGroupTimeout)*time.Second, func(files []tfile.TGFileMessage) {
290-
processWatchMediaGroup(ctx, user, stor, "", files)
299+
watchMediaGroupMgr.addFile(event.ChatID, user.ID, file, time.Duration(max(config.C().Telegram.MediaGroupTimeout, 1))*time.Second, func(files []tfile.TGFileMessage) {
300+
processWatchMediaGroup(ctx, user, stor, defaultDirPath, files)
291301
})
292302
continue
293303
}
294304

295305
// Process single file or media group without album folder creation
296-
var dirPath string
306+
dirPath := defaultDirPath
297307
if user.ApplyRule && user.Rules != nil {
298308
matched, matchedStorageName, matchedDirPath := ruleutil.ApplyRule(ctx, user.Rules, ruleutil.NewInput(file))
299309
if !matched {
@@ -352,6 +362,7 @@ func processWatchMediaGroup(ctx *ext.Context, user *database.User, stor storage.
352362
type albumFile struct {
353363
file tfile.TGFileMessage
354364
storage storage.Storage
365+
dirPath string
355366
}
356367
albumFiles := make(map[int64][]albumFile)
357368

@@ -374,9 +385,11 @@ func processWatchMediaGroup(ctx *ext.Context, user *database.User, stor storage.
374385
continue
375386
}
376387

377-
if !ruleDirPath.NeedNewForAlbum() {
378-
logger.Warnf("File %s does not need album folder, skipping", file.Name())
379-
continue
388+
// Use the effective dirPath: if rule returns NEW-FOR-ALBUM sentinel, fall back to the
389+
// base dirPath passed in (which is defaultDirPath from the caller).
390+
effectiveDirPath := string(ruleDirPath)
391+
if ruleDirPath.NeedNewForAlbum() {
392+
effectiveDirPath = dirPath
380393
}
381394

382395
if _, ok := albumFiles[groupId]; !ok {
@@ -385,6 +398,7 @@ func processWatchMediaGroup(ctx *ext.Context, user *database.User, stor storage.
385398
albumFiles[groupId] = append(albumFiles[groupId], albumFile{
386399
file: file,
387400
storage: fileStor,
401+
dirPath: effectiveDirPath,
388402
})
389403
}
390404

@@ -403,7 +417,7 @@ func processWatchMediaGroup(ctx *ext.Context, user *database.User, stor storage.
403417
logger.Infof("Creating album folder for group %d: %s with %d files", groupID, albumDir, len(afiles))
404418

405419
for _, af := range afiles {
406-
afstorPath := path.Join(dirPath, albumDir, af.file.Name())
420+
afstorPath := path.Join(af.dirPath, albumDir, af.file.Name())
407421
taskid := xid.New().String()
408422
task, err := coretfile.NewTGFileTask(taskid, injectCtx, af.file, albumStor, afstorPath, nil)
409423
if err != nil {

0 commit comments

Comments
 (0)