-
Notifications
You must be signed in to change notification settings - Fork 785
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Mastodon connector to settings / profile (#1173)
- Loading branch information
Showing
11 changed files
with
218 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# typed: false | ||
|
||
# https://docs.joinmastodon.org/methods/apps/ | ||
class MastodonApp < ApplicationRecord | ||
validates :name, :client_id, :client_secret, presence: true | ||
|
||
# https://docs.joinmastodon.org/methods/oauth/ | ||
def oauth_auth_url | ||
"https://#{name}/oauth/authorize?response_type=code&client_id=#{client_id}&scope=read:accounts&redirect_uri=" + | ||
CGI.escape(redirect_uri) | ||
end | ||
|
||
def redirect_uri | ||
"https://#{Rails.application.domain}/settings/mastodon_callback?instance=#{name}" | ||
end | ||
|
||
def register_app | ||
raise "already registered, delete and recreate" if client_id.present? | ||
|
||
s = Sponge.new | ||
url = "https://#{name}/api/v1/apps" | ||
res = s.fetch( | ||
url, | ||
:post, | ||
client_name: Rails.application.domain, | ||
redirect_uris: [ | ||
"https://#{Rails.application.domain}/settings", | ||
redirect_uri | ||
].join("\n"), | ||
scopes: "read:accounts", | ||
website: "https://#{Rails.application.domain}" | ||
) | ||
js = JSON.parse(res.body) | ||
if js && js["client_id"].present? && js["client_secret"].present? | ||
self.client_id = js["client_id"] | ||
self.client_secret = js["client_secret"] | ||
return save! | ||
end | ||
raise "registration failed, response was #{res.body}" | ||
end | ||
|
||
def token_and_user_from_code(code) | ||
s = Sponge.new | ||
res = s.fetch( | ||
"https://#{name}/oauth/token", | ||
:post, | ||
client_id: client_id, | ||
client_secret: client_secret, | ||
redirect_uri: redirect_uri, | ||
grant_type: "authorization_code", | ||
code: code, | ||
scope: "read:account" | ||
) | ||
ps = JSON.parse(res.body) | ||
tok = ps["access_token"] | ||
|
||
if tok.present? | ||
headers = {"Authorization" => "Bearer #{tok}"} | ||
res = s.fetch( | ||
"https://#{name}/api/v1/accounts/verify_credentials", | ||
:get, | ||
nil, | ||
nil, | ||
headers | ||
).body | ||
js = JSON.parse(res) | ||
if js && js["username"].present? | ||
return [tok, js["username"]] | ||
end | ||
end | ||
|
||
[nil, nil] | ||
end | ||
|
||
def self.find_or_register(instance_name) | ||
name = sanitized_instance_name(instance_name) | ||
existing = find_by name: name | ||
return existing if existing.present? | ||
|
||
app = new name: name | ||
app.register_app | ||
app.save! | ||
app | ||
end | ||
|
||
# extract hostname from possible URL | ||
def self.sanitized_instance_name(instance_name) | ||
instance_name.delete_prefix "https://" | ||
instance_name.split("/").first | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<% content_for :subnav do %> | ||
<a href="/settings">Back to Settings</a> | ||
<% end %> | ||
|
||
<div class="box wide"> | ||
<%= form_with url: "/settings/mastodon_auth", method: :get do |f| %> | ||
<p> | ||
Enter the name or URL of your Mastodon instance (for example, mastodon.social): | ||
</p> | ||
|
||
<div class="boxline"> | ||
<%= f.label :mastodon_instance_name, "Name or URL:", :class => "required" %> | ||
<%= f.text_field :mastodon_instance_name, :autofocus => true %> | ||
</div> | ||
|
||
<p> | ||
<%= f.submit "Continue" %> | ||
<% end %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CreateMastodonApps < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :mastodon_apps do |t| | ||
t.string :name, null: false | ||
t.string :client_id, null: false | ||
t.string :client_secret, null: false | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :mastodon_apps, :name, unique: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# typed: false | ||
|
||
class Mastodon | ||
def self.enabled? | ||
Rails.env.production? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# typed: false | ||
|
||
RSpec.describe MastodonApp, type: :model do | ||
end |