Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show number of attachments on the message list #522

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions assets/javascripts/mailcatcher.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class MailCatcher
.append($("<td/>").text(message.sender or "No sender").toggleClass("blank", !message.sender))
.append($("<td/>").text((message.recipients || []).join(", ") or "No receipients").toggleClass("blank", !message.recipients.length))
.append($("<td/>").text(message.subject or "No subject").toggleClass("blank", !message.subject))
.append($("""<td class="attachments-count"/>""").text(message.attachments_count || ""))
.append($("<td/>").text(@formatDate(message.created_at)))
.prependTo($("#messages tbody"))
@updateMessagesCount()
Expand Down
7 changes: 7 additions & 0 deletions assets/stylesheets/mailcatcher.css.sass
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ body > header
font-weight: bold
color: #666
+text-shadow(0 1px 0 white)
&.attachments-header
width: 3em
text-align: right
padding-right: 1em
tbody tr
cursor: pointer
+transition(0.1s ease)
Expand All @@ -139,6 +143,9 @@ body > header
&.blank
color: #666
font-style: italic
&.attachments-count
text-align: right
padding-right: 1em
#resizer
padding-bottom: 5px
cursor: ns-resize
Expand Down
6 changes: 4 additions & 2 deletions lib/mail_catcher/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ def add_message(message)
add_message_part(message_id, cid, part.mime_type || "text/plain", part.attachment? ? 1 : 0, part.filename, part.charset, body, body.length)
end

attachments_count = parts.count(&:attachment?)

EventMachine.next_tick do
message = MailCatcher::Mail.message message_id
MailCatcher::Bus.push(type: "add", message: message)
MailCatcher::Bus.push(type: "add", message: message.merge("attachments_count" => attachments_count))
end
end

Expand All @@ -73,7 +75,7 @@ def latest_created_at
end

def messages
@messages_query ||= db.prepare "SELECT id, sender, recipients, subject, size, created_at FROM message ORDER BY created_at, id ASC"
@messages_query ||= db.prepare "SELECT id, sender, recipients, subject, size, created_at, (SELECT COUNT(*) FROM message_part WHERE message_id = message.id AND is_attachment = 1) AS attachments_count FROM message ORDER BY created_at, id ASC"
@messages_query.execute.map do |row|
Hash[row.fields.zip(row)].tap do |message|
message["recipients"] &&= JSON.parse(message["recipients"])
Expand Down
13 changes: 12 additions & 1 deletion spec/delivery_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ def message_subject_element
message_row_element.find(:xpath, ".//td[3]")
end

def message_received_element
def message_attachments_element
message_row_element.find(:xpath, ".//td[4]")
end

def message_received_element
message_row_element.find(:xpath, ".//td[5]")
end

def html_tab_element
page.find("#message header .format.html a")
end
Expand Down Expand Up @@ -65,6 +69,7 @@ def body_element
expect(message_from_element).to have_text(DEFAULT_FROM)
expect(message_to_element).to have_text(DEFAULT_TO)
expect(message_subject_element).to have_text("Plain mail")
expect(message_attachments_element.text).to eq("")
expect(Time.parse(message_received_element.text)).to be <= Time.now + 5

message_row_element.click
Expand Down Expand Up @@ -227,6 +232,7 @@ def body_element
expect(message_from_element).to have_text(DEFAULT_FROM)
expect(message_to_element).to have_text(DEFAULT_TO)
expect(message_subject_element).to have_text("Test Attachment Mail")
expect(message_attachments_element.text).to eq("1")
expect(Time.parse(message_received_element.text)).to be <= Time.now + 5

message_row_element.click
Expand Down Expand Up @@ -256,6 +262,11 @@ def body_element
expect(page).to have_text "Content-Disposition: attachment"
# Too hard to add expectations on the transfer encoded attachment contents
end

# Refresh the page
refresh

expect(message_attachments_element.text).to eq("1")
end

it "doesn't choke on messages containing dots" do
Expand Down
1 change: 1 addition & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<th>From</th>
<th>To</th>
<th>Subject</th>
<th class="attachments-header">📎</th>
<th>Received</th>
</tr>
</thead>
Expand Down