From d6ed2eb512f09600d7cd8150bb9b547442a9d68b Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 22 Mar 2017 19:26:22 +0100 Subject: [PATCH] Prettier account and stream entry URLs --- app/controllers/statuses_controller.rb | 39 +++++++++++++++++++ app/lib/tag_manager.rb | 4 +- app/services/process_interaction_service.rb | 2 +- app/views/accounts/_header.html.haml | 4 +- app/views/stream_entries/_status.html.haml | 4 +- app/views/stream_entries/show.html.haml | 2 +- config/routes.rb | 8 ++-- .../controllers/api/salmon_controller_spec.rb | 1 - 8 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 app/controllers/statuses_controller.rb diff --git a/app/controllers/statuses_controller.rb b/app/controllers/statuses_controller.rb new file mode 100644 index 00000000..696bb4f5 --- /dev/null +++ b/app/controllers/statuses_controller.rb @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +class StatusesController < ApplicationController + layout 'public' + + before_action :set_account + before_action :set_status + before_action :set_link_headers + before_action :check_account_suspension + + def show + @ancestors = @status.reply? ? cache_collection(@status.ancestors(current_account), Status) : [] + @descendants = cache_collection(@status.descendants(current_account), Status) + + render 'stream_entries/show' + end + + private + + def set_account + @account = Account.find_local!(params[:account_username]) + end + + def set_link_headers + response.headers['Link'] = LinkHeader.new([[account_stream_entry_url(@account, @status.stream_entry, format: 'atom'), [%w(rel alternate), %w(type application/atom+xml)]]]) + end + + def set_status + @status = @account.statuses.find(params[:id]) + @stream_entry = @status.stream_entry + @type = @stream_entry.activity_type.downcase + + raise ActiveRecord::RecordNotFound unless @status.permitted?(current_account) + end + + def check_account_suspension + gone if @account.suspended? + end +end diff --git a/app/lib/tag_manager.rb b/app/lib/tag_manager.rb index 9fef70fd..34c3edc4 100644 --- a/app/lib/tag_manager.rb +++ b/app/lib/tag_manager.rb @@ -82,7 +82,9 @@ class TagManager case target.object_type when :person - account_url(target) + short_account_url(target) + when :note, :comment, :activity + short_account_status_url(target.account, target) else account_stream_entry_url(target.account, target.stream_entry) end diff --git a/app/services/process_interaction_service.rb b/app/services/process_interaction_service.rb index c74ff9e2..d5f7b4b3 100644 --- a/app/services/process_interaction_service.rb +++ b/app/services/process_interaction_service.rb @@ -64,7 +64,7 @@ class ProcessInteractionService < BaseService end def mentions_account?(xml, account) - xml.xpath('/xmlns:entry/xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each { |mention_link| return true if mention_link.attribute('href').value == TagManager.instance.url_for(account) } + xml.xpath('/xmlns:entry/xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each { |mention_link| return true if [TagManager.instance.uri_for(account), TagManager.instance.url_for(account)].include?(mention_link.attribute('href').value) } false end diff --git a/app/views/accounts/_header.html.haml b/app/views/accounts/_header.html.haml index f575e855..e35b0831 100644 --- a/app/views/accounts/_header.html.haml +++ b/app/views/accounts/_header.html.haml @@ -20,8 +20,8 @@ .account__header__content.p-note.emojify= Formatter.instance.simplified_format(@account) .details-counters - .counter{ class: active_nav_class(account_url(@account)) } - = link_to account_url(@account), class: 'u-url u-uid' do + .counter{ class: active_nav_class(short_account_url(@account)) } + = link_to short_account_url(@account), class: 'u-url u-uid' do %span.counter-label= t('accounts.posts') %span.counter-number= number_with_delimiter @account.statuses.count .counter{ class: active_nav_class(following_account_url(@account)) } diff --git a/app/views/stream_entries/_status.html.haml b/app/views/stream_entries/_status.html.haml index f70e2c89..cdd0dde3 100644 --- a/app/views/stream_entries/_status.html.haml +++ b/app/views/stream_entries/_status.html.haml @@ -4,7 +4,7 @@ - centered ||= include_threads && !is_predecessor && !is_successor - if status.reply? && include_threads - = render partial: 'status', collection: @ancestors, as: :status, locals: { is_predecessor: true } + = render partial: 'stream_entries/status', collection: @ancestors, as: :status, locals: { is_predecessor: true } .entry{ class: entry_classes(status, is_predecessor, is_successor, include_threads) } - if status.reblog? @@ -19,4 +19,4 @@ = render partial: centered ? 'stream_entries/detailed_status' : 'stream_entries/simple_status', locals: { status: proper_status(status) } - if include_threads - = render partial: 'status', collection: @descendants, as: :status, locals: { is_successor: true } + = render partial: 'stream_entries/status', collection: @descendants, as: :status, locals: { is_successor: true } diff --git a/app/views/stream_entries/show.html.haml b/app/views/stream_entries/show.html.haml index 7a1afdd9..c109ff4b 100644 --- a/app/views/stream_entries/show.html.haml +++ b/app/views/stream_entries/show.html.haml @@ -24,4 +24,4 @@ = render partial: 'shared/landing_strip', locals: { account: @stream_entry.account } .activity-stream.activity-stream-headless - = render partial: @type, locals: { @type.to_sym => @stream_entry.activity, include_threads: true } + = render partial: "stream_entries/#{@type}", locals: { @type.to_sym => @stream_entry.activity, include_threads: true } diff --git a/config/routes.rb b/config/routes.rb index b3f623c0..cf836496 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -24,6 +24,8 @@ Rails.application.routes.draw do confirmations: 'auth/confirmations', } + get '/users/:username', to: redirect('/@%{username}'), constraints: { format: :html } + resources :accounts, path: 'users', only: [:show], param: :username do resources :stream_entries, path: 'updates', only: [:show] do member do @@ -43,6 +45,9 @@ Rails.application.routes.draw do end end + get '/@:username', to: 'accounts#show', as: :short_account + get '/@:account_username/:id', to: 'statuses#show', as: :short_account_status + namespace :settings do resource :profile, only: [:show, :update] resource :preferences, only: [:show, :update] @@ -189,8 +194,5 @@ Rails.application.routes.draw do root 'home#index' - get '/:username', to: redirect('/users/%{username}') - get '/:username/:id', to: redirect('/users/%{username}/updates/%{id}') - match '*unmatched_route', via: :all, to: 'application#raise_not_found' end diff --git a/spec/controllers/api/salmon_controller_spec.rb b/spec/controllers/api/salmon_controller_spec.rb index 3d3a973d..6897caee 100644 --- a/spec/controllers/api/salmon_controller_spec.rb +++ b/spec/controllers/api/salmon_controller_spec.rb @@ -6,7 +6,6 @@ RSpec.describe Api::SalmonController, type: :controller do let(:account) { Fabricate(:user, account: Fabricate(:account, username: 'catsrgr8')).account } before do - stub_request(:post, "https://pubsubhubbub.superfeedr.com/").to_return(:status => 200, :body => "", :headers => {}) stub_request(:get, "https://quitter.no/.well-known/host-meta").to_return(request_fixture('.host-meta.txt')) stub_request(:get, "https://quitter.no/.well-known/webfinger?resource=acct:gargron@quitter.no").to_return(request_fixture('webfinger.txt')) stub_request(:get, "https://quitter.no/api/statuses/user_timeline/7477.atom").to_return(request_fixture('feed.txt'))