Change routing paths to use usernames in web UI (#16171)
parent
9c92907681
commit
52e5c07948
@ -0,0 +1,15 @@ |
|||||||
|
import { ACCOUNT_IMPORT, ACCOUNTS_IMPORT } from '../actions/importer'; |
||||||
|
import { Map as ImmutableMap } from 'immutable'; |
||||||
|
|
||||||
|
const initialState = ImmutableMap(); |
||||||
|
|
||||||
|
export default function accountsMap(state = initialState, action) { |
||||||
|
switch(action.type) { |
||||||
|
case ACCOUNT_IMPORT: |
||||||
|
return state.set(action.account.acct, action.account.id); |
||||||
|
case ACCOUNTS_IMPORT: |
||||||
|
return state.withMutations(map => action.accounts.forEach(account => map.set(account.acct, account.id))); |
||||||
|
default: |
||||||
|
return state; |
||||||
|
} |
||||||
|
}; |
@ -0,0 +1,63 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
|
||||||
|
class PermalinkRedirector |
||||||
|
include RoutingHelper |
||||||
|
|
||||||
|
def initialize(path) |
||||||
|
@path = path |
||||||
|
end |
||||||
|
|
||||||
|
def redirect_path |
||||||
|
if path_segments[0] == 'web' |
||||||
|
if path_segments[1].present? && path_segments[1].start_with?('@') && path_segments[2] =~ /\d/ |
||||||
|
find_status_url_by_id(path_segments[2]) |
||||||
|
elsif path_segments[1].present? && path_segments[1].start_with?('@') |
||||||
|
find_account_url_by_name(path_segments[1]) |
||||||
|
elsif path_segments[1] == 'statuses' && path_segments[2] =~ /\d/ |
||||||
|
find_status_url_by_id(path_segments[2]) |
||||||
|
elsif path_segments[1] == 'accounts' && path_segments[2] =~ /\d/ |
||||||
|
find_account_url_by_id(path_segments[2]) |
||||||
|
elsif path_segments[1] == 'timelines' && path_segments[2] == 'tag' && path_segments[3].present? |
||||||
|
find_tag_url_by_name(path_segments[3]) |
||||||
|
elsif path_segments[1] == 'tags' && path_segments[2].present? |
||||||
|
find_tag_url_by_name(path_segments[2]) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def path_segments |
||||||
|
@path_segments ||= @path.gsub(/\A\//, '').split('/') |
||||||
|
end |
||||||
|
|
||||||
|
def find_status_url_by_id(id) |
||||||
|
status = Status.find_by(id: id) |
||||||
|
|
||||||
|
return unless status&.distributable? |
||||||
|
|
||||||
|
ActivityPub::TagManager.instance.url_for(status) |
||||||
|
end |
||||||
|
|
||||||
|
def find_account_url_by_id(id) |
||||||
|
account = Account.find_by(id: id) |
||||||
|
|
||||||
|
return unless account |
||||||
|
|
||||||
|
ActivityPub::TagManager.instance.url_for(account) |
||||||
|
end |
||||||
|
|
||||||
|
def find_account_url_by_name(name) |
||||||
|
username, domain = name.gsub(/\A@/, '').split('@') |
||||||
|
domain = nil if TagManager.instance.local_domain?(domain) |
||||||
|
account = Account.find_remote(username, domain) |
||||||
|
|
||||||
|
return unless account |
||||||
|
|
||||||
|
ActivityPub::TagManager.instance.url_for(account) |
||||||
|
end |
||||||
|
|
||||||
|
def find_tag_url_by_name(name) |
||||||
|
tag_path(CGI.unescape(name)) |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,42 @@ |
|||||||
|
# frozen_string_literal: true |
||||||
|
|
||||||
|
require 'rails_helper' |
||||||
|
|
||||||
|
describe PermalinkRedirector do |
||||||
|
describe '#redirect_url' do |
||||||
|
before do |
||||||
|
account = Fabricate(:account, username: 'alice', id: 1) |
||||||
|
Fabricate(:status, account: account, id: 123) |
||||||
|
end |
||||||
|
|
||||||
|
it 'returns path for legacy account links' do |
||||||
|
redirector = described_class.new('web/accounts/1') |
||||||
|
expect(redirector.redirect_path).to eq 'https://cb6e6126.ngrok.io/@alice' |
||||||
|
end |
||||||
|
|
||||||
|
it 'returns path for legacy status links' do |
||||||
|
redirector = described_class.new('web/statuses/123') |
||||||
|
expect(redirector.redirect_path).to eq 'https://cb6e6126.ngrok.io/@alice/123' |
||||||
|
end |
||||||
|
|
||||||
|
it 'returns path for legacy tag links' do |
||||||
|
redirector = described_class.new('web/timelines/tag/hoge') |
||||||
|
expect(redirector.redirect_path).to eq '/tags/hoge' |
||||||
|
end |
||||||
|
|
||||||
|
it 'returns path for pretty account links' do |
||||||
|
redirector = described_class.new('web/@alice') |
||||||
|
expect(redirector.redirect_path).to eq 'https://cb6e6126.ngrok.io/@alice' |
||||||
|
end |
||||||
|
|
||||||
|
it 'returns path for pretty status links' do |
||||||
|
redirector = described_class.new('web/@alice/123') |
||||||
|
expect(redirector.redirect_path).to eq 'https://cb6e6126.ngrok.io/@alice/123' |
||||||
|
end |
||||||
|
|
||||||
|
it 'returns path for pretty tag links' do |
||||||
|
redirector = described_class.new('web/tags/hoge') |
||||||
|
expect(redirector.redirect_path).to eq '/tags/hoge' |
||||||
|
end |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue