Skip to content

Commit

Permalink
FEATURE: Permalinks for users (discourse#25552)
Browse files Browse the repository at this point in the history
  • Loading branch information
gschlager authored Feb 5, 2024
1 parent 4b2be8c commit dd5ca6c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default class PermalinkForm extends Component {
{ id: "category_id", name: I18n.t("admin.permalink.category_id") },
{ id: "tag_name", name: I18n.t("admin.permalink.tag_name") },
{ id: "external_url", name: I18n.t("admin.permalink.external_url") },
{ id: "user_id", name: I18n.t("admin.permalink.user_id") },
];
}

Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/admin/addon/templates/permalinks.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@
{{/if}}
<a href={{pl.external_url}}>{{pl.external_url}}</a>
{{/if}}
{{#if pl.user_id}}
<a href={{pl.user_url}}>{{pl.username}}</a>
{{/if}}
</td>
<td class="col action" style="text-align: right;">
<DButton
Expand Down
5 changes: 4 additions & 1 deletion app/models/permalink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Permalink < ActiveRecord::Base
belongs_to :post
belongs_to :category
belongs_to :tag
belongs_to :user

before_validation :normalize_url

Expand Down Expand Up @@ -75,12 +76,13 @@ def target_url
return topic.relative_url if topic
return category.url if category
return tag.full_url if tag
return user.full_url if user
nil
end

def self.filter_by(url = nil)
permalinks =
Permalink.includes(:topic, :post, :category, :tag).order("permalinks.created_at desc")
Permalink.includes(:topic, :post, :category, :tag, :user).order("permalinks.created_at desc")

permalinks.where!("url ILIKE :url OR external_url ILIKE :url", url: "%#{url}%") if url.present?
permalinks.limit!(100)
Expand All @@ -101,6 +103,7 @@ def self.filter_by(url = nil)
# updated_at :datetime not null
# external_url :string(1000)
# tag_id :integer
# user_id :integer
#
# Indexes
#
Expand Down
13 changes: 12 additions & 1 deletion app/serializers/permalink_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class PermalinkSerializer < ApplicationSerializer
:external_url,
:tag_id,
:tag_name,
:tag_url
:tag_url,
:user_id,
:user_url,
:username

def topic_title
object&.topic&.title
Expand Down Expand Up @@ -54,4 +57,12 @@ def tag_name
def tag_url
object&.tag&.full_url
end

def user_url
object&.user&.full_url
end

def username
object&.user&.username
end
end
2 changes: 2 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6575,6 +6575,8 @@ en:
category_title: "Category"
tag_name: "Tag name"
external_url: "External or Relative URL"
user_id: "User ID"
username: "Username"
destination: "Destination"
copy_to_clipboard: "Copy Permalink to Clipboard"
delete_confirm: Are you sure you want to delete this permalink?
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20240204204532_add_user_to_permalink.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddUserToPermalink < ActiveRecord::Migration[7.0]
def change
add_column :permalinks, :user_id, :integer
end
end
11 changes: 11 additions & 0 deletions spec/models/permalink_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
let(:post) { Fabricate(:post, topic: topic) }
let(:category) { Fabricate(:category) }
let(:tag) { Fabricate(:tag) }
let(:user) { Fabricate(:user) }

it "returns a topic url when topic_id is set" do
permalink.topic_id = topic.id
Expand Down Expand Up @@ -96,5 +97,15 @@
it "returns nil when nothing is set" do
expect(target_url).to eq(nil)
end

it "returns a user url when user_id is set" do
permalink.user_id = user.id
expect(target_url).to eq(user.full_url)
end

it "returns nil when user_id is set but user is not found" do
permalink.user_id = 99_999
expect(target_url).to eq(nil)
end
end
end
30 changes: 30 additions & 0 deletions spec/requests/admin/permalinks_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
post_id: nil,
category_id: nil,
tag_id: nil,
external_url: nil,
user_id: nil,
)
end

Expand All @@ -112,6 +114,8 @@
post_id: some_post.id,
category_id: nil,
tag_id: nil,
external_url: nil,
user_id: nil,
)
end

Expand All @@ -132,6 +136,8 @@
post_id: nil,
category_id: category.id,
tag_id: nil,
external_url: nil,
user_id: nil,
)
end

Expand All @@ -152,6 +158,30 @@
post_id: nil,
category_id: nil,
tag_id: tag.id,
external_url: nil,
user_id: nil,
)
end

it "works for users" do
user = Fabricate(:user)

post "/admin/permalinks.json",
params: {
url: "/people/42",
permalink_type: "user_id",
permalink_type_value: user.id,
}

expect(response.status).to eq(200)
expect(Permalink.last).to have_attributes(
url: "people/42",
topic_id: nil,
post_id: nil,
category_id: nil,
tag_id: nil,
external_url: nil,
user_id: user.id,
)
end
end
Expand Down

0 comments on commit dd5ca6c

Please sign in to comment.