Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
BLADE_BUCKET_NAME = 'blade.ruby-lang.org'

class Message < ApplicationRecord
# Not really sure we will utlize this configuration,
# but I don't want to make this column.
# https://blade.ruby-lang.org/ruby-talk/1 is JST.
# https://blade.ruby-lang.org/ruby-talk/410000 is not.
self.skip_time_zone_conversion_for_attributes = [:published_at]

def self.from_s3(list_name, list_seq)
client = Aws::S3::Client.new(region: BLADE_BUCKET_REGION)
obj = client.get_object(bucket: BLADE_BUCKET_NAME, key: "#{list_name}/#{list_seq}")
Expand All @@ -18,6 +24,13 @@ def self.from_string(str)
line.split(/:\s+/, 2)
}.to_h

self.new(body: body, subject: headers['Subject'], from: headers['From'])
published_at = DateTime.strptime(headers['Date'], '%Y-%m-%dT%H:%M:%S%:z')

self.new(
body: body,
subject: headers['Subject'],
from: headers['From'],
published_at: published_at,
)
end
end
5 changes: 5 additions & 0 deletions db/migrate/20241010055923_add_published_at_to_messages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPublishedAtToMessages < ActiveRecord::Migration[7.1]
def change
add_column :messages, :published_at, :timestamp
end
end
6 changes: 6 additions & 0 deletions db/migrate/20241010062050_published_at_time_zone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class PublishedAtTimeZone < ActiveRecord::Migration[7.1]
# https://github.com/rails/rails/pull/41084
def change
change_column :messages, :published_at, :timestamptz
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion test/models/message_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ class MessageTest < ActiveSupport::TestCase
m = Message.from_string(<<END_OF_BODY)
Subject: [ruby-list:1] Hello
From: alice@...
Date: Mon, 01 Jan 2022 12:34:56 +0900
Date: 2005-12-15T19:32:40+09:00

Hello, world!
END_OF_BODY
assert_equal "Hello, world!\n", m.body
assert_nil m.id

assert_equal DateTime.parse('2005-12-15T19:32:40+09:00'), m.published_at
end
end