Sha256: f9cf88f04cae6ce2d9716bd592df93bd1598103a497bf06797a59adb8b2fab2c

Contents?: true

Size: 1.72 KB

Versions: 19

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module Decidim
  # The controller to show the newsletter on the website.
  class NewslettersController < Decidim::ApplicationController
    layout "decidim/mailer", only: [:show]
    helper Decidim::SanitizeHelper
    include Decidim::NewslettersHelper

    helper_method :newsletter

    def show
      @user = current_user
      @organization = current_organization

      raise ActionController::RoutingError, "Not Found" unless newsletter.sent?

      @encrypted_token = Decidim::NewsletterEncryptor.sent_at_encrypted(@user.id, newsletter.sent_at) if @user.present?
      @body = parse_interpolations(newsletter.body[I18n.locale.to_s], @user, newsletter.id)
    end

    def unsubscribe
      encryptor = Decidim::NewsletterEncryptor

      decrypted_string = encryptor.sent_at_decrypted(params[:u])
      user = User.find_by(decidim_organization_id: current_organization.id, id: decrypted_string.split("-").first)
      sent_at_time = Time.zone.at(decrypted_string.split("-").second.to_i)

      if sent_at_time > (Time.current - 15.days)
        UnsubscribeSettings.call(user) do
          on(:ok) do
            flash.now[:notice] = t("newsletters.unsubscribe.success", scope: "decidim")
          end

          on(:invalid) do
            flash.now[:alert] = t("newsletters.unsubscribe.error", scope: "decidim")
            render action: :unsubscribe
          end
        end
      else
        flash.now[:alert] = t("newsletters.unsubscribe.token_error", scope: "decidim")
        render action: :unsubscribe
      end
    end

    def newsletter
      @newsletter ||= collection.find(params[:id])
    end

    private

    def collection
      Newsletter.where(organization: current_organization)
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
decidim-core-0.21.0 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.20.1 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.20.0 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.19.1 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.18.1 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.19.0 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.17.2 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.18.0 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.17.1 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.16.1 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.17.0 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.16.0 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.15.2 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.15.1 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.15.0 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.14.4 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.14.3 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.14.2 app/controllers/decidim/newsletters_controller.rb
decidim-core-0.14.1 app/controllers/decidim/newsletters_controller.rb