FEATURE: Permalinks for users (#25552)

This commit is contained in:
Gerhard Schlager 2024-02-05 17:31:31 +01:00 committed by GitHub
parent 4b2be8c6b3
commit dd5ca6cc4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 70 additions and 2 deletions

View File

@ -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") },
];
}

View File

@ -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

View File

@ -5,6 +5,7 @@ class Permalink < ActiveRecord::Base
belongs_to :post
belongs_to :category
belongs_to :tag
belongs_to :user
before_validation :normalize_url
@ -75,12 +76,13 @@ class Permalink < ActiveRecord::Base
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)
@ -101,6 +103,7 @@ end
# updated_at :datetime not null
# external_url :string(1000)
# tag_id :integer
# user_id :integer
#
# Indexes
#

View File

@ -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
@ -54,4 +57,12 @@ class PermalinkSerializer < ApplicationSerializer
def tag_url
object&.tag&.full_url
end
def user_url
object&.user&.full_url
end
def username
object&.user&.username
end
end

View File

@ -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?

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddUserToPermalink < ActiveRecord::Migration[7.0]
def change
add_column :permalinks, :user_id, :integer
end
end

View File

@ -31,6 +31,7 @@ RSpec.describe Permalink do
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
@ -96,5 +97,15 @@ RSpec.describe Permalink do
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

View File

@ -92,6 +92,8 @@ RSpec.describe Admin::PermalinksController do
post_id: nil,
category_id: nil,
tag_id: nil,
external_url: nil,
user_id: nil,
)
end
@ -112,6 +114,8 @@ RSpec.describe Admin::PermalinksController do
post_id: some_post.id,
category_id: nil,
tag_id: nil,
external_url: nil,
user_id: nil,
)
end
@ -132,6 +136,8 @@ RSpec.describe Admin::PermalinksController do
post_id: nil,
category_id: category.id,
tag_id: nil,
external_url: nil,
user_id: nil,
)
end
@ -152,6 +158,30 @@ RSpec.describe Admin::PermalinksController do
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