Class: Dubhe::Sources::Mail
- Inherits:
-
Object
- Object
- Dubhe::Sources::Mail
- Includes:
- Dubhe::Source
- Defined in:
- lib/dubhe/sources/mail.rb
Instance Method Summary collapse
- #entries(filter: nil) ⇒ Object
- #fetch_body(entry) ⇒ Object
- #icon ⇒ Object
- #id ⇒ Object
-
#initialize(path:, store: Store.new, policy: Policy.new(profile: :mail)) ⇒ Mail
constructor
A new instance of Mail.
- #refresh ⇒ Object
- #supports?(capability) ⇒ Boolean
- #title ⇒ Object
Constructor Details
#initialize(path:, store: Store.new, policy: Policy.new(profile: :mail)) ⇒ Mail
Returns a new instance of Mail.
12 13 14 15 16 17 |
# File 'lib/dubhe/sources/mail.rb', line 12 def initialize(path:, store: Store.new, policy: Policy.new(profile: :mail)) @path = File.(path.to_s) @store = store @policy = policy @messages = {} end |
Instance Method Details
#entries(filter: nil) ⇒ Object
43 44 45 46 47 |
# File 'lib/dubhe/sources/mail.rb', line 43 def entries(filter: nil) result = @store.entries(source_id: id) result = refresh if result.empty? filter ? result.select { |entry| filter.call(entry) } : result end |
#fetch_body(entry) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/dubhe/sources/mail.rb', line 49 def fetch_body(entry) = @messages[entry.id] return @store.body(entry.id) unless if (part = .html_part) html = .decoded(part) .inline_parts.each do |content_id, inline_part| next if content_id.start_with?("<") mime = inline_part.content_type.to_s data = Base64.strict_encode64(inline_part.body.to_s) html = html.gsub("cid:#{content_id}", "data:#{mime};base64,#{data}") end result = @policy.apply(html) Body.new(kind: :html, content: result.html, base_url: nil, attachments: ..map(&:filename).compact, headers: {"blocked_count" => result.blocked_count}) elsif (part = .text_part) Body.new(kind: :text, content: .decoded(part), base_url: nil, attachments: ..map(&:filename).compact, headers: {}) else Body.new(kind: :text, content: "", base_url: nil, attachments: [], headers: {}) end end |
#icon ⇒ Object
21 |
# File 'lib/dubhe/sources/mail.rb', line 21 def icon = "mail" |
#id ⇒ Object
19 |
# File 'lib/dubhe/sources/mail.rb', line 19 def id = "mail:#{Digest::SHA256.hexdigest(@path)}" |
#refresh ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/dubhe/sources/mail.rb', line 23 def refresh = if File.extname(@path).downcase == ".mbox" Ukdah::Mbox.each(File.binread(@path)).to_a else [Ukdah::Message.parse(File.binread(@path))] end entries = .map do || = . || Digest::SHA256.hexdigest(.raw.to_s) entry = Entry.new(id: "#{id}:#{}", source_id: id, title: .subject.to_s, author: .from.map { |address| address.respond_to?(:email) ? address.email : address.to_s }.join(", "), published_at: .date, updated_at: .date, url: nil, summary: .header("x-snippet"), tags: [], state: :unread, parent_id: nil) @messages[entry.id] = @store.save_entry(entry) @store.save_body(entry.id, fetch_body(entry)) entry end entries end |
#supports?(capability) ⇒ Boolean
73 74 75 |
# File 'lib/dubhe/sources/mail.rb', line 73 def supports?(capability) %i[refresh search unread].include?(capability) end |
#title ⇒ Object
20 |
# File 'lib/dubhe/sources/mail.rb', line 20 def title = File.basename(@path) |