Merge branch 'feature/user_avatars' into live
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Râu Cao 2025-05-14 15:40:47 +04:00
commit 208977177a
Signed by: raucao
GPG Key ID: 37036C356E56CC51
3 changed files with 63 additions and 2 deletions

View File

@ -33,6 +33,10 @@ class WebfingerController < WellKnownController
links: [] links: []
} }
if @user.avatar.attached?
jrd[:links] += avatar_link
end
if Setting.mastodon_enabled && @user.service_enabled?(:mastodon) if Setting.mastodon_enabled && @user.service_enabled?(:mastodon)
# https://docs.joinmastodon.org/spec/webfinger/ # https://docs.joinmastodon.org/spec/webfinger/
jrd[:aliases] += mastodon_aliases jrd[:aliases] += mastodon_aliases
@ -47,6 +51,16 @@ class WebfingerController < WellKnownController
jrd jrd
end end
def avatar_link
[
{
rel: "http://webfinger.net/rel/avatar",
type: @user.avatar.content_type,
href: helpers.image_url_for(@user.avatar)
}
]
end
def mastodon_aliases def mastodon_aliases
[ [
"#{Setting.mastodon_public_url}/@#{@user.cn}", "#{Setting.mastodon_public_url}/@#{@user.cn}",

View File

@ -16,8 +16,15 @@ module ApplicationHelper
end end
def image_url_for(attachment) def image_url_for(attachment)
if Setting.s3_enabled? return s3_image_url(attachment) if Setting.s3_enabled?
s3_image_url(attachment)
if attachment.record.is_a?(User) && attachment.name == "avatar"
hash, format = attachment.blob.filename.to_s.split(".", 2)
user_avatar_url(
username: attachment.record.cn,
hash: hash,
format: format
)
else else
Rails.application.routes.url_helpers.rails_blob_path(attachment, only_path: true) Rails.application.routes.url_helpers.rails_blob_path(attachment, only_path: true)
end end

View File

@ -18,6 +18,46 @@ RSpec.describe "WebFinger", type: :request do
}) })
end end
describe "Avatar" do
context "not available" do
it "does not include an avatar link" do
get "/.well-known/webfinger?resource=acct%3Atony%40kosmos.org"
expect(response).to have_http_status(:ok)
res = JSON.parse(response.body)
link = res["links"].find{|l| l["rel"] == "http://webfinger.net/rel/avatar"}
expect(link).to be_nil
end
end
context "available" do
let(:fixture_path) { Rails.root.join("spec/fixtures/files/taipei.jpg") }
let(:img_data) { File.read(fixture_path) }
let(:img_hash) { Digest::SHA256.hexdigest(img_data) }
before do
ActiveStorage::Blob.create_and_upload!(
io: File.open(fixture_path),
filename: "#{img_hash}.jpg",
content_type: "image/jpeg"
).tap do |blob|
user.avatar.attach(blob)
end
end
it "includes a public avatar link" do
get "/.well-known/webfinger?resource=acct%3Atony%40kosmos.org"
expect(response).to have_http_status(:ok)
res = JSON.parse(response.body)
link = res["links"].find { |l| l["rel"] == "http://webfinger.net/rel/avatar" }
expect(link).to be_present
expect(link["type"]).to eq("image/jpeg")
expect(link["href"]).to match(%r{users/tony/avatars/#{img_hash}.jpg})
end
end
end
describe "Mastodon entries" do describe "Mastodon entries" do
context "Mastodon available" do context "Mastodon available" do
it "includes the Mastodon aliases and links for the user" do it "includes the Mastodon aliases and links for the user" do