From ffd24f791ee6ff80530b6c5ae7526c6e5e66ded6 Mon Sep 17 00:00:00 2001 From: Michael Kohl Date: Tue, 4 Dec 2018 23:54:53 +0530 Subject: [PATCH] Add Mastodon URL to profile (#1137) --- app/assets/images/mastodon-logo.svg | 1 + app/controllers/internal/users_controller.rb | 1 + app/lib/constants.rb | 51 +++++++++++++++++++ app/models/user.rb | 9 ++++ app/policies/user_policy.rb | 1 + app/views/articles/index.html.erb | 5 ++ app/views/users/_profile.html.erb | 4 ++ ...0181117145537_add_mastodon_url_to_users.rb | 5 ++ db/schema.rb | 1 + spec/models/user_spec.rb | 15 ++++++ 10 files changed, 93 insertions(+) create mode 100644 app/assets/images/mastodon-logo.svg create mode 100644 app/lib/constants.rb create mode 100644 db/migrate/20181117145537_add_mastodon_url_to_users.rb diff --git a/app/assets/images/mastodon-logo.svg b/app/assets/images/mastodon-logo.svg new file mode 100644 index 000000000..79e1d8043 --- /dev/null +++ b/app/assets/images/mastodon-logo.svg @@ -0,0 +1 @@ +Mastodon icon \ No newline at end of file diff --git a/app/controllers/internal/users_controller.rb b/app/controllers/internal/users_controller.rb index 1a18509e2..b02d79f62 100644 --- a/app/controllers/internal/users_controller.rb +++ b/app/controllers/internal/users_controller.rb @@ -128,6 +128,7 @@ class Internal::UsersController < Internal::ApplicationController user.behance_url = nil user.linkedin_url = nil user.gitlab_url = nil + user.mastodon_url = nil user.add_role :banned unless user.notes.where(reason: "banned").any? user.notes. diff --git a/app/lib/constants.rb b/app/lib/constants.rb new file mode 100644 index 000000000..183b07ff7 --- /dev/null +++ b/app/lib/constants.rb @@ -0,0 +1,51 @@ +module Constants + MASTODON_INSTANCE_WHITELIST = [ + "acg.mn", + "bitcoinhackers.org", + "cmx.im", + "framapiaf.org", + "friends.nico", + "hearthtodon.com", + "hex.bz", + "horiedon.com", + "hostux.social", + "imastodon.net", + "kirakiratter.com", + "knzk.me", + "lou.lt", + "mamot.fr", + "mao.daizhige.org", + "mastodon.art", + "mastodon.at", + "mastodon.blue", + "mastodon.cloud", + "mastodon.gamedev.place", + "mastodon.host", + "mastodon.sdf.org", + "mastodon.social", + "mastodon.technology", + "mastodon.xyz", + "mathtod.online", + "mimumedon.com", + "misskey.xyz", + "mstdn-workers.com", + "mstdn.guru", + "mstdn.io", + "mstdn.jp", + "mstdn.tokyocameraclub.com", + "mstdn18.jp", + "music.pawoo.net", + "niu.moe", + "noagendasocial.com", + "octodon.social", + "otajodon.com", + "pawoo.net", + "qiitadon.com", + "ro-mastodon.puyo.jp", + "social.targaryen.house", + "social.tchncs.de", + "switter.at", + "todon.nl", + "wikitetas.club" + ].freeze +end diff --git a/app/models/user.rb b/app/models/user.rb index 11857cf7e..5febd7215 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -112,6 +112,7 @@ class User < ApplicationRecord validates :website_url, url: { allow_blank: true, no_local: true, schemes: ["https", "http"] } validates :website_url, :employer_name, :employer_url, length: { maximum: 100 } + validates :mastodon_url, url: { allow_blank: true, no_local: true, schemes: ["https", "http"] } validates :employment_title, :education, :location, length: { maximum: 100 } validates :mostly_work_with, :currently_learning, @@ -120,6 +121,7 @@ class User < ApplicationRecord validates :mentee_description, :mentor_description, length: { maximum: 1000 } validate :conditionally_validate_summary + validate :mastodon_url_whitelist validate :validate_feed_url validate :unique_including_orgs @@ -471,6 +473,13 @@ class User < ApplicationRecord errors.add(:feed_url, "is not a valid rss feed") unless RssReader.new.valid_feed_url?(feed_url) end + def mastodon_url_whitelist + return unless mastodon_url.present? + uri = URI.parse(mastodon_url) + return if uri.host&.in?(Constants::MASTODON_INSTANCE_WHITELIST) + errors.add(:mastodon_url, "is not a whitelisted Mastodon instance") + end + def title name end diff --git a/app/policies/user_policy.rb b/app/policies/user_policy.rb index 12d9650bc..619f205b9 100644 --- a/app/policies/user_policy.rb +++ b/app/policies/user_policy.rb @@ -82,6 +82,7 @@ class UserPolicy < ApplicationPolicy location looking_for_work looking_for_work_publicly + mastodon_url medium_url mentee_description mentee_form_updated_at diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb index fb0dd8030..10cc2047d 100644 --- a/app/views/articles/index.html.erb +++ b/app/views/articles/index.html.erb @@ -134,6 +134,11 @@ <% end %> <% if @user.class.name == "User" %> + <% if @user.mastodon_url.present? %> + + <%= inline_svg("mastodon-logo.svg", class:"icon-img") %> + + <% end %> <% if @user.facebook_url.present? %> <%= inline_svg("facebook-logo.svg", class:"icon-img") %> diff --git a/app/views/users/_profile.html.erb b/app/views/users/_profile.html.erb index 3416fba78..443e1269c 100644 --- a/app/views/users/_profile.html.erb +++ b/app/views/users/_profile.html.erb @@ -142,6 +142,10 @@ <%= f.label :gitlab_url %> <%= f.url_field :gitlab_url %> +
+ <%= f.label :mastodon_url %> + <%= f.url_field :mastodon_url %> +
<%= f.hidden_field :tab, value: @tab %> diff --git a/db/migrate/20181117145537_add_mastodon_url_to_users.rb b/db/migrate/20181117145537_add_mastodon_url_to_users.rb new file mode 100644 index 000000000..b19bd55c0 --- /dev/null +++ b/db/migrate/20181117145537_add_mastodon_url_to_users.rb @@ -0,0 +1,5 @@ +class AddMastodonUrlToUsers < ActiveRecord::Migration[5.1] + def change + add_column :users, :mastodon_url, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index b45953acb..fd82cbac2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -800,6 +800,7 @@ ActiveRecord::Schema.define(version: 20181129222416) do t.string "username" t.string "website_url" t.datetime "workshop_expiration" + t.string "mastodon_url" t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["language_settings"], name: "index_users_on_language_settings", using: :gin t.index ["organization_id"], name: "index_users_on_organization_id" diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 8ec7506ce..e8706f68a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -59,6 +59,21 @@ RSpec.describe User, type: :model do expect(user).not_to be_valid end + it "accepts valid https mastodon url" do + user.mastodon_url = "https://mastodon.social/@test" + expect(user).to be_valid + end + + it "does not accept a non whitelisted mastodon instance" do + user.mastodon_url = "https://SpammyMcSpamface.com/" + expect(user).not_to be_valid + end + + it "does not accept invalid mastodon url" do + user.mastodon_url = "mastodon.social/@test" + expect(user).not_to be_valid + end + it "accepts valid http website url" do user.website_url = "http://ben.com" expect(user).to be_valid