Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for downloading messages for day/month as plaintext.

  • Loading branch information...
commit c2c7095503d91788390ccb9a291aad8a8aeef604 1 parent 2eb8384
whitequark authored
Showing with 46 additions and 11 deletions.
  1. +20 −0 lib/irclogger/message.rb
  2. +26 −11 lib/irclogger/viewer.rb
20 lib/irclogger/message.rb
View
@@ -27,6 +27,17 @@ def info?
!opcode.nil?
end
+ def to_s
+ Time.at(timestamp).gmtime.strftime("%Y-%m-%d %H:%M") +
+ if talk?
+ " <#{nick}> #{line}"
+ elsif me_tell?
+ " * #{nick} #{line}"
+ else
+ " #{line}"
+ end
+ end
+
def self.nicks(messages)
messages.
filter('nick is not null').
@@ -92,6 +103,15 @@ def self.check_by_channel_and_date(channel, date)
count > 0
end
+ def self.find_by_channel_and_month(channel, date)
+ from = Time.utc(date.year, date.month, 1)
+ to = Time.utc((date >> 1).year, (date >> 1).month, 1) - 1
+
+ filter('timestamp > ? and timestamp < ?', from.to_i, to.to_i).
+ filter(:channel => channel).
+ order(:timestamp)
+ end
+
def self.check_by_channel_and_month(channel, date)
from = Time.utc(date.year, date.month, 1)
to = Time.utc((date >> 1).year, (date >> 1).month, 1) - 1
37 lib/irclogger/viewer.rb
View
@@ -96,21 +96,36 @@ class Viewer < Sinatra::Base
end
end
- get '/:channel/:date?' do
- @channel = channel_unescape(params[:channel])
- @date = Date.parse(params[:date]) rescue nil
+ get '/:channel/:interval?.?:format?' do |channel, interval, format|
+ @channel = channel_unescape(channel)
+
+ begin
+ if interval =~ /^\d+-\d+-\d+$/
+ @date = Date.parse(interval)
+ messages = Message.find_by_channel_and_date(@channel, @date)
+ elsif interval =~ /^\d+-\d+$/ && %w(txt).include?(format)
+ @date = Date.parse(interval + "-01")
+ messages = Message.find_by_channel_and_month(@channel, @date)
+ else
+ raise ArgumentError, "invalid interval"
+ end
+
+ rescue ArgumentError # invalid date or interval
+ redirect channel_url(@channel, Time.now.gmtime.to_date)
+ end
- if @date
- @is_today = (@date == Time.now.gmtime.to_date)
+ case format
+ when 'txt'
+ response['Content-Type'] = 'text/plain'
- dataset = Message.find_by_channel_and_date(@channel, @date)
- @nicks = Message.nicks(dataset)
- @messages = Message.track_chains(dataset, @nicks)
- @topic = Message.most_recent_topic_for(@channel, @date)
+ messages.map(&:to_s).join("\n")
+ else
+ @is_today = (@date == Time.now.gmtime.to_date)
+ @nicks = Message.nicks(messages)
+ @messages = Message.track_chains(messages, @nicks)
+ @topic = Message.most_recent_topic_for(@channel, @date)
haml :channel
- else
- redirect channel_url(@channel, Time.now.gmtime.to_date)
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.