Skip to content

Commit

Permalink
feat(downloader): allow download of grouped message.
Browse files Browse the repository at this point in the history
  • Loading branch information
F-TD5X committed Nov 9, 2024
1 parent 0deec3d commit b2a17ee
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions app/dl/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/sha256"
"encoding/binary"
"fmt"
"github.com/gotd/td/tg"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -53,7 +54,7 @@ type iter struct {
fingerprint string
preSum []int
i, j int
elem downloader.Elem
elem chan downloader.Elem
err error
}

Expand Down Expand Up @@ -96,7 +97,7 @@ func newIter(pool dcpool.Pool, manager *peers.Manager, dialog [][]*tmessage.Dial
preSum: preSum(dialogs),
i: 0,
j: 0,
elem: nil,
elem: make(chan downloader.Elem, 11),
err: nil,
}, nil
}
Expand All @@ -113,6 +114,9 @@ func (i *iter) Next(ctx context.Context) bool {
if i.delay > 0 && (i.i+i.j) > 0 { // skip first delay
time.Sleep(i.delay)
}
if len(i.elem) > 0 {
return true
}

for {
ok, skip := i.process(ctx)
Expand Down Expand Up @@ -152,13 +156,18 @@ func (i *iter) process(ctx context.Context) (ret bool, skip bool) {
i.err = errors.Wrap(err, "resolve from input peer")
return false, false
}

message, err := tutil.GetSingleMessage(ctx, i.pool.Default(ctx), peer, msg)
if err != nil {
i.err = errors.Wrap(err, "resolve message")
return false, false
}
if _, ok := message.GetGroupedID(); ok {
return i.processGrouped(ctx, message, from)
}
return i.processSingle(message, from)
}

func (i *iter) processSingle(message *tg.Message, from peers.Peer) (bool, bool) {
item, ok := tmedia.GetMedia(message)
if !ok {
i.err = errors.Errorf("can not get media from %d/%d message", from.ID(), message.ID)
Expand All @@ -175,7 +184,7 @@ func (i *iter) process(ctx context.Context) (ret bool, skip bool) {
}

toName := bytes.Buffer{}
err = i.tpl.Execute(&toName, &fileTemplate{
err := i.tpl.Execute(&toName, &fileTemplate{
DialogID: from.ID(),
MessageID: message.ID,
MessageDate: int64(message.Date),
Expand Down Expand Up @@ -213,8 +222,8 @@ func (i *iter) process(ctx context.Context) (ret bool, skip bool) {
return false, false
}

i.elem = &iterElem{
id: i.ij2n(i.i, i.j),
i.elem <- &iterElem{
id: message.ID,

from: from,
fromMsg: message,
Expand All @@ -228,8 +237,23 @@ func (i *iter) process(ctx context.Context) (ret bool, skip bool) {
return true, false
}

func (i *iter) processGrouped(ctx context.Context, message *tg.Message, from peers.Peer) (ret1 bool, ret2 bool) {
ret1 = true
ret2 = false
grouped, err := tutil.GetGroupedMessages(ctx, i.pool.Default(ctx), from.InputPeer(), message)
if err != nil {
return false, false
}

for _, msg := range grouped {
a, _ := i.processSingle(msg, from)
ret1 = ret1 && a
}
return

Check failure on line 252 in app/dl/iter.go

View workflow job for this annotation

GitHub Actions / lint (.)

naked return in func `processGrouped` with 13 lines of code (nakedret)
}

func (i *iter) Value() downloader.Elem {
return i.elem
return <-i.elem
}

func (i *iter) Err() error {
Expand Down

0 comments on commit b2a17ee

Please sign in to comment.