From 0367450c4b6e1f4069baf537ffef08c714c5037b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Fri, 29 Mar 2024 09:08:15 +0400 Subject: [PATCH] Replace hyphen with underscore in Mastodon address Unfortunately, Mastodon only allows underscores for usernames, and reversely, akkounts only allows hyphens and no underscores. --- app/models/user.rb | 2 +- spec/models/user_spec.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index 2169402..f0609a6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -131,7 +131,7 @@ class User < ApplicationRecord def mastodon_address return nil unless Setting.mastodon_enabled? - "#{self.cn}@#{Setting.mastodon_address_domain}" + "#{self.cn.gsub("-", "_")}@#{Setting.mastodon_address_domain}" end def valid_attribute?(attribute_name) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 6e7300d..b0d077e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -41,6 +41,14 @@ RSpec.describe User, type: :model do expect(user.mastodon_address).to eq("jimmy@kosmos.social") end end + + describe "username contains hyphen/dash" do + let(:jammy) { build :user, cn: "jammy-jellyfish", ou: "kosmos.org" } + + it "returns the user address" do + expect(jammy.mastodon_address).to eq("jammy_jellyfish@kosmos.org") + end + end end end