Skip to content

Commit

Permalink
Allow creating an inbox from spam emails on dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
tsubery committed Jun 5, 2024
1 parent 29fbc26 commit 44936de
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 8 deletions.
40 changes: 39 additions & 1 deletion app/admin/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
th :title
th :feed
end
MediaItem.includes(:feed).where.not(mime_type: "video/mp4").order(created_at: :desc).first(params[:count]&.to_i || 10).each do |media_item|
MediaItem.includes(:feed).where.not(mime_type: "video/mp4").where(sent_to: '').order(created_at: :desc).first(params[:count]&.to_i || 10).each do |media_item|
tr do
td do
a media_item.id, href: admin_media_item_path(media_item.id)
Expand All @@ -58,6 +58,44 @@
end
end
end

div class: "blank_slate_container", id: "latest_articles" do
span class: "blank_slate" do
h2 "Latest Emails"
table do
thead do
th :id
th :created_at
th :title
th :sent_to
th :feed
end
MediaItem.includes(:feed).where.not(sent_to: '').order(created_at: :desc).first(params[:count]&.to_i || 10).each do |media_item|
tr do
td do
a media_item.id, href: admin_media_item_path(media_item.id)
end
td time_ago_in_words(media_item.created_at)
td do
a media_item.title, href: media_item.url
end
td media_item.sent_to
td do
if media_item.feed&.spam?
form_with(model: [:admin, Feed.new]) do |f|
f.hidden_field(:url, value: media_item.sent_to) +
f.hidden_field('library_ids', multiple: true, value: PocketLibrary.pluck(:id)) +
f.submit
end
else
a media_item.feed.title, href: admin_feed_path(media_item.feed_id)
end
end
end
end
end
end
end
div class: "blank_slate_container", id: "latest_feed_errors" do
span class: "blank_slate" do
h2 "Latest Errors"
Expand Down
3 changes: 2 additions & 1 deletion app/mailboxes/incoming_mailbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ def process
description: mail.html_part&.decoded || mail.body.to_s,
url: MediaItem.temporary_url,
guid: Digest::MD5.hexdigest(Time.zone.now.to_s + mail.body.to_s),
mime_type: "text/html"
mime_type: "text/html",
sent_to: mail.to.first
)
feed.touch(:last_sync)
feed.libraries.each { |l| l.add_media_item(new_media_item) }
Expand Down
7 changes: 7 additions & 0 deletions app/models/feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ class Feed < ApplicationRecord
before_validation :set_type
before_validation :normalize_url
before_validation :fill_missing_details
after_create :associate_previous_media_items

USER_AGENT = "FeedBurner/1.0 (http://www.FeedBurner.com)".freeze # Cloudflare protection sometimes blocks default user agent

def associate_previous_media_items
if self.class.name != type
becomes(type.constantize).associate_previous_media_items
end
end

def fill_missing_details
if self.class.name != type
becomes(type.constantize).fill_missing_details
Expand Down
17 changes: 17 additions & 0 deletions app/models/incoming_email_feed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,21 @@ def recent_media_items(*)
def fill_missing_details
self.title = url
end

def email
url
end

def spam?
email == SPAM_EMAIL
end

def associate_previous_media_items
self.media_items << MediaItem.where(sent_to: email)
media_items.each do |media_item|
libraries.each do |library|
library.add_media_item(media_item)
end
end
end
end
4 changes: 3 additions & 1 deletion app/models/pocket_library.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class PocketLibrary < Library
def add_media_item(new_media_item)
PocketClient.add(new_media_item.url)
unless ENV['DISABLE_POCKET']
PocketClient.add(new_media_item.url)
end
media_items << new_media_item
end
end
11 changes: 11 additions & 0 deletions db/migrate/20240605220823_add_sent_to_email_to_media_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddSentToEmailToMediaItems < ActiveRecord::Migration[7.2]
def change
add_column :media_items, :sent_to, :string, null: false, default: ''
add_index :media_items, :sent_to, where: "sent_to != ''"
begin
IncomingEmailFeed.all.each {|f| f.media_items.update_all(sent_to: f.url)}
rescue => e
p e
end
end
end
10 changes: 5 additions & 5 deletions db/schema.rb

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

2 changes: 2 additions & 0 deletions test/mailboxes/incoming_mailbox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class IncomingMailboxTest < ActionMailbox::TestCase
assert_equal "http://#{ENV.fetch('HOSTNAME')}/media_items/#{media_item.id}/article", media_item.url
assert_equal "text/html", media_item.mime_type
assert_equal "else@example.com", media_item.author
assert_equal "someone@example.com", media_item.sent_to
end

test "receive mail to a monitored inbox" do
Expand All @@ -33,5 +34,6 @@ class IncomingMailboxTest < ActionMailbox::TestCase
assert_equal "text/html", media_item.mime_type
assert_equal "else@example.com", media_item.author
assert_equal [media_item], library.media_items.to_a
assert_equal "newsletter@example.com", media_item.sent_to
end
end

0 comments on commit 44936de

Please sign in to comment.