Skip to content

Commit

Permalink
users have tried to use usernames directly
Browse files Browse the repository at this point in the history
  • Loading branch information
pushcx committed Feb 2, 2024
1 parent ea26128 commit 535981c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/models/mastodon_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ def self.find_or_register(instance_name)
app
end

# user may input hostname (foo.social), url (https://foo.social/@user), or user (@user@foo.social)
# extract hostname from possible URL
def self.sanitized_instance_name(instance_name)
instance_name.to_s.delete_prefix("https://").split("/").first
instance_name
.to_s
.delete_prefix("https://")
.split("/").first
.split("@").last
end
end
18 changes: 18 additions & 0 deletions spec/models/mastodon_app_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
# typed: false

require "rails_helper"

RSpec.describe MastodonApp, type: :model do
describe "#sanitized_instance_name" do
it "accepts an instance name" do
expect(MastodonApp.sanitized_instance_name("example.com")).to eq("example.com")
end

it "accepts urls" do
expect(MastodonApp.sanitized_instance_name("https://example.com")).to eq("example.com")
expect(MastodonApp.sanitized_instance_name("https://example.com/")).to eq("example.com")
expect(MastodonApp.sanitized_instance_name("https://example.com/@user")).to eq("example.com")
end

it "accepts a user id" do
expect(MastodonApp.sanitized_instance_name("user@example.com")).to eq("example.com")
expect(MastodonApp.sanitized_instance_name("@user@example.com")).to eq("example.com")
end
end
end

0 comments on commit 535981c

Please sign in to comment.