Skip to content

Commit b4bdfd3

Browse files
committed
Keep track of last seen magnetlink
This is to prevent processing magnetlinks if they haven't changed
1 parent e7d827f commit b4bdfd3

File tree

6 files changed

+130
-53
lines changed

6 files changed

+130
-53
lines changed

appdatabase/migrations/bindata.go

Lines changed: 64 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE communities_archive_info ADD COLUMN last_magnetlink_uri TEXT DEFAULT "";
2+

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ require (
6868
go.uber.org/zap v1.23.0
6969
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e
7070
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb
71-
google.golang.org/protobuf v1.28.1
71+
google.golang.org/protobuf v1.28.1 // indirect
7272
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
7373
gopkg.in/go-playground/validator.v9 v9.31.0
7474
gopkg.in/natefinch/lumberjack.v2 v2.0.0

protocol/communities/manager.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,14 @@ func (m *Manager) UpdateMagnetlinkMessageClock(communityID types.HexBytes, clock
11531153
return m.persistence.UpdateMagnetlinkMessageClock(communityID, clock)
11541154
}
11551155

1156+
func (m *Manager) UpdateLastSeenMagnetlink(communityID types.HexBytes, magnetlinkURI string) error {
1157+
return m.persistence.UpdateLastSeenMagnetlink(communityID, magnetlinkURI)
1158+
}
1159+
1160+
func (m *Manager) GetLastSeenMagnetlink(communityID types.HexBytes) (string, error) {
1161+
return m.persistence.GetLastSeenMagnetlink(communityID)
1162+
}
1163+
11561164
func (m *Manager) LeaveCommunity(id types.HexBytes) (*Community, error) {
11571165
community, err := m.GetByID(id)
11581166
if err != nil {
@@ -2121,6 +2129,10 @@ func (m *Manager) GetHistoryArchiveDownloadTask(communityID string) *HistoryArch
21212129
return m.historyArchiveDownloadTasks[communityID]
21222130
}
21232131

2132+
func (m *Manager) DeleteHistoryArchiveDownloadTask(communityID string) {
2133+
delete(m.historyArchiveDownloadTasks, communityID)
2134+
}
2135+
21242136
func (m *Manager) AddHistoryArchiveDownloadTask(communityID string, task *HistoryArchiveDownloadTask) {
21252137
m.historyArchiveDownloadTasks[communityID] = task
21262138
}
@@ -2146,13 +2158,23 @@ func (m *Manager) DownloadHistoryArchivesByMagnetlink(communityID types.HexBytes
21462158
return nil, err
21472159
}
21482160

2161+
downloadTaskInfo := &HistoryArchiveDownloadTaskInfo{
2162+
TotalDownloadedArchivesCount: 0,
2163+
TotalArchivesCount: 0,
2164+
Cancelled: false,
2165+
}
2166+
21492167
m.torrentTasks[id] = ml.InfoHash
21502168
timeout := time.After(20 * time.Second)
21512169

21522170
m.LogStdout("fetching torrent info", zap.String("magnetlink", magnetlink))
21532171
select {
21542172
case <-timeout:
21552173
return nil, ErrTorrentTimedout
2174+
case <-cancelTask:
2175+
m.LogStdout("cancelled fetching torrent info")
2176+
downloadTaskInfo.Cancelled = true
2177+
return downloadTaskInfo, nil
21562178
case <-torrent.GotInfo():
21572179

21582180
files := torrent.Files()
@@ -2170,12 +2192,6 @@ func (m *Manager) DownloadHistoryArchivesByMagnetlink(communityID types.HexBytes
21702192
ticker := time.NewTicker(100 * time.Millisecond)
21712193
defer ticker.Stop()
21722194

2173-
downloadTaskInfo := &HistoryArchiveDownloadTaskInfo{
2174-
TotalDownloadedArchivesCount: 0,
2175-
TotalArchivesCount: 0,
2176-
Cancelled: false,
2177-
}
2178-
21792195
for {
21802196
select {
21812197
case <-cancelTask:

protocol/communities/persistence.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,15 @@ func (p *Persistence) HasCommunityArchiveInfo(communityID types.HexBytes) (exist
597597
return exists, err
598598
}
599599

600+
func (p *Persistence) GetLastSeenMagnetlink(communityID types.HexBytes) (string, error) {
601+
var magnetlinkURI string
602+
err := p.db.QueryRow(`SELECT last_magnetlink_uri FROM communities_archive_info WHERE community_id = ?`, communityID.String()).Scan(&magnetlinkURI)
603+
if err == sql.ErrNoRows {
604+
return "", nil
605+
}
606+
return magnetlinkURI, err
607+
}
608+
600609
func (p *Persistence) GetMagnetlinkMessageClock(communityID types.HexBytes) (uint64, error) {
601610
var magnetlinkClock uint64
602611
err := p.db.QueryRow(`SELECT magnetlink_clock FROM communities_archive_info WHERE community_id = ?`, communityID.String()).Scan(&magnetlinkClock)
@@ -623,6 +632,15 @@ func (p *Persistence) UpdateMagnetlinkMessageClock(communityID types.HexBytes, c
623632
return err
624633
}
625634

635+
func (p *Persistence) UpdateLastSeenMagnetlink(communityID types.HexBytes, magnetlinkURI string) error {
636+
_, err := p.db.Exec(`UPDATE communities_archive_info SET
637+
last_magnetlink_uri = ?
638+
WHERE community_id = ?`,
639+
magnetlinkURI,
640+
communityID.String())
641+
return err
642+
}
643+
626644
func (p *Persistence) SaveLastMessageArchiveEndDate(communityID types.HexBytes, endDate uint64) error {
627645
_, err := p.db.Exec(`INSERT INTO communities_archive_info (last_message_archive_end_date, community_id) VALUES (?, ?)`,
628646
endDate,

0 commit comments

Comments
 (0)