Sha256: c6adbd4dc15c271f86e97db17a154d46332872f5528b4d919b30c32379306fa2

Contents?: true

Size: 1.62 KB

Versions: 9

Compression:

Stored size: 1.62 KB

Contents

module Thredded
  class EmailProcessor
    attr_accessor :email, :messageboard, :user

    def initialize(email)
      @email = email
      @user = find_user
      @messageboard = find_messageboard
    end

    def self.process(email)
      processor = self.new(email)
      processor.create_or_update_topic
    end

    def create_or_update_topic
      if can_post_to_topic?
        topic = find_or_build_topic
        post = topic.posts.build(
          user: user,
          content: email.body,
          source: 'email',
          messageboard: messageboard,
          attachments_attributes: attachment_params,
        )
        post.user_email = user.email

        topic.save
      else
        return false
      end
    end

    private

    def attachment_params
      @attachment_params = {}

      email.attachments.each_with_index do |attachment, i|
        @attachment_params[i.to_s] = { 'attachment' => attachment }
      end

      @attachment_params
    end

    def can_post_to_topic?
      user && messageboard &&
        Thredded::Ability.new(user).can?(:create, messageboard.topics.new)
    end

    def find_or_build_topic
      topic = find_topic

      if topic.nil?
        topic = messageboard.topics.build(title: email.subject)
        topic.user = user
        topic.state = 'pending'
      end

      topic.last_user = user
      topic
    end

    def find_topic
      Thredded::Topic.where(hash_id: email.to).first
    end

    def find_messageboard
      Thredded::Messageboard.where(name: email.to).first || find_topic.try(:messageboard)
    end

    def find_user
      User.where(email: email.from).first
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
thredded-0.0.12 lib/thredded/email_processor.rb
thredded-0.0.10 lib/thredded/email_processor.rb
thredded-0.0.9 lib/thredded/email_processor.rb
thredded-0.0.8 lib/thredded/email_processor.rb
thredded-0.0.7 lib/thredded/email_processor.rb
thredded-0.0.6 lib/thredded/email_processor.rb
thredded-0.0.5 lib/thredded/email_processor.rb
thredded-0.0.4 lib/thredded/email_processor.rb
thredded-0.0.3 lib/thredded/email_processor.rb