Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Add mastodon handles to profile #3321

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ def twitter_username(user)
def twitter_url(user)
"https://twitter.com/#{user.twitter_username}"
end

def mastodon_handle(user)
"@#{user.mastodon_handle}" if user.mastodon_handle.present?
end

def mastodon_url(user)
"@#{user.mastodon_handle}"
end
end
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class User < ApplicationRecord
password
website
twitter_username
mastodon_handle
].freeze

before_save :_generate_confirmation_token_no_reset_unconfirmed_email, if: :will_save_change_to_unconfirmed_email?
Expand Down Expand Up @@ -51,7 +52,13 @@ class User < ApplicationRecord
message: "can only contain letters, numbers, and underscores"
}, allow_nil: true

# https://rubular.com/r/Zjjh8UbLfAFBCm
validates :mastodon_handle, format: {
with: /\A[a-zA-Z0-9_]+@[a-zA-Z0-9_\.]+\z/
}, allow_nil: true

validates :twitter_username, length: { within: 0..20 }, allow_nil: true
validates :mastodon_handle, length: { within: 0..20 }, allow_nil: true
validates :password,
length: { within: 10..200 },
unpwn: true,
Expand Down
19 changes: 19 additions & 0 deletions app/views/profiles/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@
<%= form.text_field :handle, :class => 'form__input' %>
</div>

<div class="text_field">
<%= form.label :mastodon_handle, class: 'form__label form__label__icon-container' do %>
<%=
image_tag("/images/mastodon_icon.png", alt: 'Mastodon icon', class: 'form__label__icon')
%>

<span class='form__label__text'>Mastodon handle</span>
<% end %>

<p class='form__field__instructions'>
<%= t('.optional_mastodon_handle') %>
</p>

<div class="form__input__addon-container form__input__addon-left">
<span class="form__input__addon">@</span>
<%= form.text_field(:mastodon_handle, class: 'form__input') %>
</div>
</div>

<div class="text_field">
<%= form.label :twitter_username, class: 'form__label form__label__icon-container' do %>
<%=
Expand Down
12 changes: 12 additions & 0 deletions app/views/profiles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@
</p>
<% end %>

<% if @user.mastodon_handle.present? %>
<%=
image_tag(
"/images/mastodon_icon.png",
alt: "Mastodon icon",
class: "profile__header__icon"
)
%>

<%= mastodon_handle(user) %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it possible to generate link using mastodon handle? 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simi I wasn't able to find a way to do this, it's also weird if we just let anyone put any link they want on the profile? We probably have to sanitize it, etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't enough to just split on last @ and reverse join with / (and prepending https protocol)?

<% end %>

<% if @user.twitter_username.present? %>
<%=
image_tag(
Expand Down
1 change: 1 addition & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ de:
email_awaiting_confirmation:
enter_password:
hide_email: Verberge E-Mails in öffentlichem Profil
optional_mastodon_handle:
optional_twitter_username:
title: Bearbeite Profil
delete:
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ en:
email_awaiting_confirmation: Please confirm your new email address %{unconfirmed_email}
enter_password: Please enter your account's password
hide_email: Hide email in public profile
optional_mastodon_handle: Optional Mastodon handle. Will be displayed publicly
optional_twitter_username: Optional Twitter username. Will be displayed publicly
title: Edit profile
delete:
Expand Down
1 change: 1 addition & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ es:
%{unconfirmed_email}
enter_password: Por favor introduce tu contraseña
hide_email: Ocultar correo electrónico en perfil público
optional_mastodon_handle:
optional_twitter_username: Usuario de Twitter opcional. Será mostrado en tu
perfil público
title: Editar perfil
Expand Down
1 change: 1 addition & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ fr:
%{unconfirmed_email}
enter_password: Veuillez entrer le mot de passe de votre compte
hide_email: Ne pas afficher l'email dans le profil public
optional_mastodon_handle:
optional_twitter_username: Nom d'utilisateur Twitter optionnel. Sera affiché
publiquement
title: Modification de profil
Expand Down
1 change: 1 addition & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ ja:
email_awaiting_confirmation: あなたの新しいメールアドレス%{unconfirmed_email}を確認してください。
enter_password: パスワードを入力してください。
hide_email: メールアドレスを公開しない
optional_mastodon_handle: Mastodonのユーザー名(任意,公開)
optional_twitter_username: Twitterのユーザー名(任意,公開)
title: プロフィールを編集する
delete:
Expand Down
1 change: 1 addition & 0 deletions config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ nl:
email_awaiting_confirmation:
enter_password:
hide_email:
optional_mastodon_handle:
optional_twitter_username:
title: Wijzig profiel
delete:
Expand Down
1 change: 1 addition & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ pt-BR:
email_awaiting_confirmation:
enter_password:
hide_email: Não mostrar meu email
optional_mastodon_handle:
optional_twitter_username:
title: Editar Perfil
delete:
Expand Down
1 change: 1 addition & 0 deletions config/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ zh-CN:
email_awaiting_confirmation: 请验证你的新邮箱地址 %{unconfirmed_email}
enter_password: 输入密码
hide_email: 在公开的个人资料里面隐藏我的 Email
optional_mastodon_handle:
optional_twitter_username: Twitter 账号(可选)
title: 修改个人资料
delete:
Expand Down
1 change: 1 addition & 0 deletions config/locales/zh-TW.yml
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ zh-TW:
email_awaiting_confirmation: 請驗證你的新 Email %{unconfirmed_email}
enter_password: 輸入密碼
hide_email: 在公開的個人頁面中隱藏 email
optional_mastodon_handle:
optional_twitter_username: Twitter 帳號(可選)
title: 編輯個人檔案
delete:
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20230103103445_add_mastodon_handle_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddMastodonHandleToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :mastodon_handle, :string
end
end
Binary file added public/images/mastodon_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions test/integration/profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,21 @@ def sign_in
refute page.has_content?("Email Me")
end

test "adding Mastodon username" do
sign_in
visit profile_path("nick1")

click_link "Edit Profile"
fill_in "Mastodon handle", with: "[email protected]"
fill_in "Password", with: PasswordHelpers::SECURE_TEST_PASSWORD
click_button "Update"

click_link "Sign out"
visit profile_path("nick1")

assert page.has_content?("@[email protected]")
end

test "adding Twitter username" do
sign_in
visit profile_path("nick1")
Expand Down
10 changes: 10 additions & 0 deletions test/unit/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ class UserTest < ActiveSupport::TestCase
end
end

context "mastodon_handle" do
should validate_length_of(:mastodon_handle)
should allow_value("user123_32@mastodon").for(:mastodon_handle)
should_not allow_value("@user").for(:mastodon_handle)
should_not allow_value("user 1").for(:mastodon_handle)
should_not allow_value("user-1").for(:mastodon_handle)
should allow_value("01234567890123456789@01234567890123456789").for(:mastodon_handle)
should_not allow_value("012345678901234567890").for(:mastodon_handle)
end

context "twitter_username" do
should validate_length_of(:twitter_username)
should allow_value("user123_32").for(:twitter_username)
Expand Down