From 3731230c6d25f248afa8a17b62b3db70fdfe1e03 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 3 Nov 2016 16:57:44 +0100 Subject: [PATCH] Allow @username@domain/@username in follow form, prevent duplicate accounts created via remote look-up when domains differ but point to the same resource --- .../javascripts/components/reducers/accounts.jsx | 16 ++++++++-------- .../components/reducers/relationships.jsx | 6 +++--- .../javascripts/components/reducers/statuses.jsx | 16 ++++++++-------- app/controllers/api/v1/follows_controller.rb | 8 +++++++- app/services/follow_remote_account_service.rb | 13 +++++++++++-- 5 files changed, 37 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/components/reducers/accounts.jsx b/app/assets/javascripts/components/reducers/accounts.jsx index 10d637e57c..bc21447523 100644 --- a/app/assets/javascripts/components/reducers/accounts.jsx +++ b/app/assets/javascripts/components/reducers/accounts.jsx @@ -25,7 +25,7 @@ import { } from '../actions/statuses'; import Immutable from 'immutable'; -const normalizeAccount = (state, account) => state.set(account.get('id'), account); +const normalizeAccount = (state, account) => state.set(account.id, Immutable.fromJS(account)); const normalizeAccounts = (state, accounts) => { accounts.forEach(account => { @@ -36,10 +36,10 @@ const normalizeAccounts = (state, accounts) => { }; const normalizeAccountFromStatus = (state, status) => { - state = normalizeAccount(state, status.get('account')); + state = normalizeAccount(state, status.account); - if (status.getIn(['reblog', 'account'])) { - state = normalizeAccount(state, status.getIn(['reblog', 'account'])); + if (status.reblog && status.reblog.account) { + state = normalizeAccount(state, status.reblog.account); } return state; @@ -60,24 +60,24 @@ export default function accounts(state = initialState, action) { case ACCOUNT_SET_SELF: case ACCOUNT_FETCH_SUCCESS: case FOLLOW_SUBMIT_SUCCESS: - return normalizeAccount(state, Immutable.fromJS(action.account)); + return normalizeAccount(state, action.account); case SUGGESTIONS_FETCH_SUCCESS: case FOLLOWERS_FETCH_SUCCESS: case FOLLOWING_FETCH_SUCCESS: - return normalizeAccounts(state, Immutable.fromJS(action.accounts)); + return normalizeAccounts(state, action.accounts); case TIMELINE_REFRESH_SUCCESS: case TIMELINE_EXPAND_SUCCESS: case ACCOUNT_TIMELINE_FETCH_SUCCESS: case ACCOUNT_TIMELINE_EXPAND_SUCCESS: case CONTEXT_FETCH_SUCCESS: - return normalizeAccountsFromStatuses(state, Immutable.fromJS(action.statuses)); + return normalizeAccountsFromStatuses(state, action.statuses); case TIMELINE_UPDATE: case REBLOG_SUCCESS: case FAVOURITE_SUCCESS: case UNREBLOG_SUCCESS: case UNFAVOURITE_SUCCESS: case STATUS_FETCH_SUCCESS: - return normalizeAccountFromStatus(state, Immutable.fromJS(action.status)); + return normalizeAccountFromStatus(state, action.status); default: return state; } diff --git a/app/assets/javascripts/components/reducers/relationships.jsx b/app/assets/javascripts/components/reducers/relationships.jsx index 165b1f3ffb..e4af1f0282 100644 --- a/app/assets/javascripts/components/reducers/relationships.jsx +++ b/app/assets/javascripts/components/reducers/relationships.jsx @@ -7,7 +7,7 @@ import { } from '../actions/accounts'; import Immutable from 'immutable'; -const normalizeRelationship = (state, relationship) => state.set(relationship.get('id'), relationship); +const normalizeRelationship = (state, relationship) => state.set(relationship.id, Immutable.fromJS(relationship)); const normalizeRelationships = (state, relationships) => { relationships.forEach(relationship => { @@ -25,9 +25,9 @@ export default function relationships(state = initialState, action) { case ACCOUNT_UNFOLLOW_SUCCESS: case ACCOUNT_BLOCK_SUCCESS: case ACCOUNT_UNBLOCK_SUCCESS: - return normalizeRelationship(state, Immutable.fromJS(action.relationship)); + return normalizeRelationship(state, action.relationship); case RELATIONSHIPS_FETCH_SUCCESS: - return normalizeRelationships(state, Immutable.fromJS(action.relationships)); + return normalizeRelationships(state, action.relationships); default: return state; } diff --git a/app/assets/javascripts/components/reducers/statuses.jsx b/app/assets/javascripts/components/reducers/statuses.jsx index a1f1f07f18..69c0e61937 100644 --- a/app/assets/javascripts/components/reducers/statuses.jsx +++ b/app/assets/javascripts/components/reducers/statuses.jsx @@ -21,14 +21,14 @@ import { import Immutable from 'immutable'; const normalizeStatus = (state, status) => { - status = status.set('account', status.getIn(['account', 'id'])); + status.account = status.account.id; - if (status.getIn(['reblog', 'id'])) { - state = normalizeStatus(state, status.get('reblog')); - status = status.set('reblog', status.getIn(['reblog', 'id'])); + if (status.reblog && status.reblog.id) { + state = normalizeStatus(state, status.reblog); + status.reblog = status.reblog.id; } - return state.set(status.get('id'), status); + return state.set(status.id, Immutable.fromJS(status)); }; const normalizeStatuses = (state, statuses) => { @@ -53,18 +53,18 @@ export default function statuses(state = initialState, action) { switch(action.type) { case TIMELINE_UPDATE: case STATUS_FETCH_SUCCESS: - return normalizeStatus(state, Immutable.fromJS(action.status)); + return normalizeStatus(state, action.status); case REBLOG_SUCCESS: case UNREBLOG_SUCCESS: case FAVOURITE_SUCCESS: case UNFAVOURITE_SUCCESS: - return normalizeStatus(state, Immutable.fromJS(action.response)); + return normalizeStatus(state, action.response); case TIMELINE_REFRESH_SUCCESS: case TIMELINE_EXPAND_SUCCESS: case ACCOUNT_TIMELINE_FETCH_SUCCESS: case ACCOUNT_TIMELINE_EXPAND_SUCCESS: case CONTEXT_FETCH_SUCCESS: - return normalizeStatuses(state, Immutable.fromJS(action.statuses)); + return normalizeStatuses(state, action.statuses); case TIMELINE_DELETE: return deleteStatus(state, action.id, action.references); default: diff --git a/app/controllers/api/v1/follows_controller.rb b/app/controllers/api/v1/follows_controller.rb index 9181cd0774..526316531a 100644 --- a/app/controllers/api/v1/follows_controller.rb +++ b/app/controllers/api/v1/follows_controller.rb @@ -5,7 +5,13 @@ class Api::V1::FollowsController < ApiController def create raise ActiveRecord::RecordNotFound if params[:uri].blank? - @account = FollowService.new.call(current_user.account, params[:uri].strip).try(:target_account) + @account = FollowService.new.call(current_user.account, target_uri).try(:target_account) render action: :show end + + private + + def target_uri + params[:uri].strip.gsub(/\A@/, '') + end end diff --git a/app/services/follow_remote_account_service.rb b/app/services/follow_remote_account_service.rb index 0a95badb21..b309425a83 100644 --- a/app/services/follow_remote_account_service.rb +++ b/app/services/follow_remote_account_service.rb @@ -15,16 +15,25 @@ class FollowRemoteAccountService < BaseService return nil if DomainBlock.blocked?(domain) account = Account.find_remote(username, domain) - return account unless account.nil? - Rails.logger.debug "Creating new remote account for #{uri}" + Rails.logger.debug "Looking up webfinger for #{uri}" + account = Account.new(username: username, domain: domain) data = Goldfinger.finger("acct:#{uri}") raise Goldfinger::Error, 'Missing resource links' if data.link('http://schemas.google.com/g/2010#updates-from').nil? || data.link('salmon').nil? || data.link('http://webfinger.net/rel/profile-page').nil? || data.link('magic-public-key').nil? + confirmed_username, confirmed_domain = data.subject.gsub(/\Aacct:/, '').split('@') + + return Account.find_local(confirmed_username) if TagManager.instance.local_domain?(confirmed_domain) + + confirmed_account = Account.find_remote(confirmed_username, confirmed_domain) + return confirmed_account unless confirmed_account.nil? + + Rails.logger.debug "Creating new remote account for #{uri}" + account.remote_url = data.link('http://schemas.google.com/g/2010#updates-from').href account.salmon_url = data.link('salmon').href account.url = data.link('http://webfinger.net/rel/profile-page').href