Skip to content

Commit

Permalink
Primarily rely on date instead of id when determining message freshness
Browse files Browse the repository at this point in the history
Workaround for vysheng/tg#1077, variation of PR #15
  • Loading branch information
tvdstaaij committed Apr 14, 2016
1 parent 40fede6 commit c1fcf92
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
17 changes: 13 additions & 4 deletions dumpers/lib/dumper_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
# * Implement one or more of the functions listed below (at least dump_msg)

# Note:
# Dumpers are a low-level construct and as of v2.0.0 dumpers they are no
# longer used for custom output formats. Instead, custom formatters have been
# introduced for this purpose.
# Dumpers are a low-level construct and as of v2.0.0 they are no longer used
# for implementing custom output formats. Instead, custom formatters have been
# introduced for this purpose (see /formatters/lib/formatter_base.rb).

class DumperInterface

Expand All @@ -30,7 +30,16 @@ def start_dialog(dialog, progress)
# This default makes sense in simple cases, override for advanced custom logic
def msg_fresh?(msg, progress)
# msg: Hash, progress: DumpProgress
!progress.last_id || msg['id'] > progress.last_id

return true unless progress.newest_date

if msg['date'] && msg['date'] > progress.newest_date
return true
elsif msg['date'] == progress.newest_date && progress.newest_id
return true if msg['id'] && msg['id'] > progress.newest_id
end

false
end

# Will be called for each message to dump (from newest to oldest)
Expand Down
31 changes: 17 additions & 14 deletions lib/dump_progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@

class DumpProgress

attr_reader :last_id
attr_reader :last_date
attr_reader :newest_id
attr_reader :newest_date
attr_reader :dumper_state
attr_writer :dumper_state

def initialize(last_id = nil, last_date = nil, dumper_state = {})
@last_id = last_id
@last_date = last_date
def initialize(newest_id = nil, newest_date = nil, dumper_state = {})
@newest_id = newest_id
@newest_date = newest_date
@dumper_state = dumper_state
end

def self.from_hash(hash)
self.new(hash['last_id'], hash['last_date'], hash['dumper_state'])
self.new(
hash['newest_id'],
hash['newest_date'] || hash['last_date'], # last_date is v2.0.x compat
hash['dumper_state']
)
end

def to_hash
{
:last_id => @last_id,
:last_date => @last_date,
:newest_id => @newest_id,
:newest_date => @newest_date,
:dumper_state => @dumper_state
}
end
Expand All @@ -29,12 +33,11 @@ def to_json(*a)
to_hash.to_json(*a)
end

def bump_id(id)
@last_id = id if !@last_id || id > @last_id
end

def bump_date(date)
@last_date = date if !@last_date || date > @last_date
def update(msg)
if !@newest_date || (msg['date'] && msg['date'] >= @newest_date)
@newest_date = msg['date'] || @newest_date
@newest_id = msg['id'] || @newest_id
end
end

end
15 changes: 7 additions & 8 deletions telegram-history-dump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,22 @@ def dump_dialog(dialog)
raise 'Expected array' unless msg_chunk.is_a?(Array)
msg_chunk.reverse_each do |msg|
dump_msg = true
if msg['id']
cur_progress.bump_id(msg['id'])
else
unless msg['id']
$log.warn('Dropping message without id: %s' % msg)
dump_msg = false
end
if msg['date']
cur_progress.bump_date(msg['date'])
else
$log.warn('Message without date: %s' % msg)
unless msg['date']
$log.warn('Dropping message without date: %s' % msg)
dump_msg = false
end

cur_progress.update(msg)

if msg['text'] && filter_regex && filter_regex =~ msg['text']
dump_msg = false
end

unless $dumper.msg_fresh?(msg, old_progress)
unless msg['date'] && $dumper.msg_fresh?(msg, old_progress)
if keep_dumping
$log.info('Reached end of new messages since last backup')
end
Expand Down

0 comments on commit c1fcf92

Please sign in to comment.