2022-01-27 13:12:46 -08:00
|
|
|
# frozen_string_literal: true
|
2014-12-12 12:43:09 -08:00
|
|
|
|
2022-01-27 13:12:46 -08:00
|
|
|
class ServicesController < ApplicationController
|
2014-12-12 12:43:09 -08:00
|
|
|
before_action :authenticate_user!
|
|
|
|
|
|
|
|
def index
|
|
|
|
@services = current_user.services
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2022-01-27 13:12:46 -08:00
|
|
|
service = Service.initialize_from_omniauth(omniauth_hash)
|
2022-01-01 09:58:45 -08:00
|
|
|
service.user = current_user
|
2022-07-30 06:18:56 -07:00
|
|
|
service_name = service.type.split("::").last.titleize
|
2014-12-12 12:43:09 -08:00
|
|
|
|
2022-01-01 09:58:45 -08:00
|
|
|
if service.save
|
2022-07-30 06:18:56 -07:00
|
|
|
flash[:success] = t(".success", service: service_name)
|
2014-12-12 12:43:09 -08:00
|
|
|
else
|
2022-01-27 13:12:46 -08:00
|
|
|
flash[:error] = if service.errors.details[:uid]&.any? { |err| err[:error] == :taken }
|
2022-07-30 06:18:56 -07:00
|
|
|
t(".duplicate", service: service_name, app: APP_CONFIG["site_name"])
|
2022-01-27 13:12:46 -08:00
|
|
|
else
|
2022-07-30 06:18:56 -07:00
|
|
|
t(".error", service: service_name)
|
2022-01-27 13:12:46 -08:00
|
|
|
end
|
2014-12-12 12:43:09 -08:00
|
|
|
end
|
|
|
|
|
2022-01-27 13:12:46 -08:00
|
|
|
redirect_to origin || services_path
|
2014-12-12 12:43:09 -08:00
|
|
|
end
|
|
|
|
|
2022-01-04 16:00:18 -08:00
|
|
|
def update
|
|
|
|
service = current_user.services.find(params[:id])
|
2022-01-27 13:12:46 -08:00
|
|
|
service.post_tag = params[:service][:post_tag].tr("@", "")
|
2022-01-04 16:00:18 -08:00
|
|
|
if service.save
|
2022-01-27 11:18:48 -08:00
|
|
|
flash[:success] = t(".success")
|
2022-01-04 16:00:18 -08:00
|
|
|
else
|
2022-01-27 11:18:48 -08:00
|
|
|
flash[:error] = t(".error")
|
2022-01-04 16:00:18 -08:00
|
|
|
end
|
|
|
|
redirect_to services_path
|
|
|
|
end
|
|
|
|
|
2014-12-12 12:43:09 -08:00
|
|
|
def failure
|
|
|
|
Rails.logger.info "oauth error: #{params.inspect}"
|
2022-01-27 11:18:48 -08:00
|
|
|
flash[:error] = t(".error")
|
2014-12-12 12:43:09 -08:00
|
|
|
redirect_to services_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@service = current_user.services.find(params[:id])
|
2022-07-30 06:18:56 -07:00
|
|
|
service_name = @service.type.split("::").last.titleize
|
2014-12-12 12:43:09 -08:00
|
|
|
@service.destroy
|
2022-07-30 06:18:56 -07:00
|
|
|
flash[:success] = t(".success", service: service_name)
|
2014-12-12 12:43:09 -08:00
|
|
|
redirect_to services_path
|
|
|
|
end
|
2014-12-12 13:35:23 -08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-01-27 13:12:46 -08:00
|
|
|
def origin
|
|
|
|
request.env["omniauth.origin"]
|
|
|
|
end
|
2014-12-12 13:35:23 -08:00
|
|
|
|
2022-01-27 13:12:46 -08:00
|
|
|
def omniauth_hash
|
|
|
|
request.env["omniauth.auth"]
|
|
|
|
end
|
2014-12-12 12:43:09 -08:00
|
|
|
end
|