53 lines
2.1 KiB
Ruby
53 lines
2.1 KiB
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
module E621Ws
|
||
|
module Status
|
||
|
class WebhooksController < ApplicationController
|
||
|
def index
|
||
|
end
|
||
|
|
||
|
def discord
|
||
|
redirect_to(oauth_url(min: params[:min] == "true"), allow_other_host: true)
|
||
|
end
|
||
|
|
||
|
def discord_callback
|
||
|
res = client.authorize(redirect_uri: Websites.config.e621_status_check_discord_redirect, code: params[:code])
|
||
|
|
||
|
return redirect_to(e621_ws_status_webhook_path, alert: "Error: #{res.error_description}") if res.is_a?(Requests::DiscordOauth::Error)
|
||
|
return redirect_to(e621_ws_status_webhook_path, alert: "Error: Failed to get webhook.") if res.webhook.nil?
|
||
|
|
||
|
uid = res.scope.include?("identify") ? res.user_id : nil
|
||
|
hook = E621Webhook.from_struct(res.webhook, uid)
|
||
|
|
||
|
if hook.is_a?(Symbol)
|
||
|
# noinspection RubyCaseWithoutElseBlockInspection
|
||
|
case hook
|
||
|
when :too_many_channel
|
||
|
return redirect_to(e621_ws_status_webhook_path, alert: "Error: You already have a status check enabled in that channel. Delete the other webhook to use a new webhook. The newly created webhook will be automatically deleted.")
|
||
|
when :too_many_guild
|
||
|
return redirect_to(e621_ws_status_webhook_path, alert: "Error: You've already enabled #{E621Webhook::MAX_PER_GUILD} status checks in that server. Please delete the other webhooks before adding a new check. The newly created webhook will be automatically deleted.")
|
||
|
end
|
||
|
end
|
||
|
redirect_to(e621_ws_root_path, notice: "Check successfully setup. Delete the webhook to disable the updates.")
|
||
|
end
|
||
|
|
||
|
private
|
||
|
|
||
|
def client
|
||
|
Requests::DiscordOauth.new(
|
||
|
client_id: Websites.config.e621_status_check_discord_id,
|
||
|
client_secret: Websites.config.e621_status_check_discord_secret,
|
||
|
)
|
||
|
end
|
||
|
|
||
|
def oauth_url(min: false)
|
||
|
DiscordLink.for(
|
||
|
client_id: Websites.config.e621_status_check_discord_id,
|
||
|
redirect_uri: Websites.config.e621_status_check_discord_redirect,
|
||
|
scope: Websites.config.e621_status_check_discord_scopes[min ? :min : :all],
|
||
|
)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|