Skip to content

Commit

Permalink
Plaintext formatter: handle user joins (#49), include name of user be…
Browse files Browse the repository at this point in the history
…ing replied to
  • Loading branch information
tvdstaaij committed Nov 12, 2016
1 parent 0b4d006 commit b4133fa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
11 changes: 10 additions & 1 deletion formatters/lib/daily_file_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ def format_dialog(dialog, messages)
@dialog_dir = File.join(output_dir, safe_name)
FileUtils.mkdir_p(@dialog_dir)

@messages = messages
start_dialog(dialog)
messages.reverse_each do |message|
(0...messages.length).reverse_each do |i|
message = messages[@msg_index = i]
date = message['date']
next unless date
date = Time.at(date).to_date
Expand Down Expand Up @@ -54,4 +56,11 @@ def get_filename_for_date(dialog, date)
raise 'get_filename_for_date must be implemented'
end

def find_earlier_message(id)
(@msg_index...@messages.length).each do |i|
return @messages[i] if @messages[i]['id'] == id
end
nil
end

end
20 changes: 17 additions & 3 deletions formatters/plaintext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ class PlaintextFormatter < DailyFileFormatter
def format_message(dialog, message, output_stream)
date_str = Time.at(message['date']).strftime('[%s] ' % @options['date_format'])
from_name = get_full_name(message['from'])
from_name = '(Unknown)' if from_name.empty?

line = case message['event'].downcase
when 'message'
fwd_from_name = get_full_name(message['fwd_from'])
if !fwd_from_name.empty?
from_name += ' (forwarded from %s)' % fwd_from_name
elsif message['reply_id']
from_name += ' (reply)'
reply_target = find_earlier_message(message['reply_id'])
if reply_target
reply_name = get_full_name(reply_target['from'])
from_name += ' (in reply to %s)' %
[reply_name.to_s.empty? ? 'someone' : reply_name]
else
from_name += ' (reply)'
end
# Possible impovement: find reply text
end

Expand Down Expand Up @@ -42,10 +50,16 @@ def format_message(dialog, message, output_stream)
end

when 'service'
user_name = get_full_name(message['action']['user'])
user = message['action']['user']
user_name = get_full_name(user)
case message['action']['type'].downcase
when 'chat_add_user'
"#{from_name} added #{user_name}"
if message['from']['peer_id'] == user['peer_id'] ||
!message['from']['peer_id']
"#{user_name} joined"
else
"#{from_name} added #{user_name}"
end
when 'chat_del_user'
"#{from_name} removed #{user_name}"
when 'chat_rename'
Expand Down

0 comments on commit b4133fa

Please sign in to comment.