]> cat aescling's git repositories - mastodon.git/commitdiff
Twidere mention workaround (#5552)
authorThibG <thib@sitedethib.com>
Tue, 7 Nov 2017 18:08:14 +0000 (19:08 +0100)
committerEugen Rochko <eugen@zeonfederated.com>
Tue, 7 Nov 2017 18:08:14 +0000 (19:08 +0100)
* Work around Twidere and Tootdon bug

Tootdon and Twidere construct @user@domain handles from mentions in toots based
solely on the mention text and account URI's domain without performing any
webfinger call or retrieving account info from the Mastodon server.

As a result, when a remote user has WEB_DOMAIN ≠ LOCAL_DOMAIN, Twidere and
Tootdon will construct the mention as @user@WEB_DOMAIN. Now, this will usually
resolve to the correct account (since the recommended configuration is to have
WEB_DOMAIN perform webfinger redirections to LOCAL_DOMAIN) when processing
mentions, but won't do so when displaying them (as it does not go through the
whole account resolution at that time).

This change rewrites mentions to the resolved account, so that displaying the
mentions will work.

* Use lookbehind instead of non-capturing group in MENTION_RE

Indeed, substitutions with the previous regexp would erroneously eat any
preceding whitespace, which would lead to concatenated mentions in the
previous commit.

Note that users will “lose” up to one character space per mention for their
toots, as that regexp is also used to remove the domain-part of mentioned
users for character counting purposes, and it also erroneously removed the
preceding character if it was a space.

app/javascript/mastodon/features/compose/util/counter.js
app/models/account.rb
app/services/process_mentions_service.rb

index e6d2487c59c15de76419dbc61d0a21ead6d38890..700ba216354e1169d235a515737776bf60bbb8dc 100644 (file)
@@ -5,5 +5,5 @@ const urlPlaceholder = 'xxxxxxxxxxxxxxxxxxxxxxx';
 export function countableText(inputText) {
   return inputText
     .replace(urlRegex, urlPlaceholder)
-    .replace(/(?:^|[^\/\w])@(([a-z0-9_]+)@[a-z0-9\.\-]+[a-z0-9]+)/ig, '@$2');
+    .replace(/(^|[^\/\w])@(([a-z0-9_]+)@[a-z0-9\.\-]+[a-z0-9]+)/ig, '$1@$3');
 };
index 1142e7c794439f2dd047764827ce009b4b048880..7e4d29f96ee4dfe522b163ea72a532b8ebacd4a5 100644 (file)
@@ -45,7 +45,7 @@
 #
 
 class Account < ApplicationRecord
-  MENTION_RE = /(?:^|[^\/[:word:]])@(([a-z0-9_]+)(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/i
+  MENTION_RE = /(?<=^|[^\/[:word:]])@(([a-z0-9_]+)(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/i
 
   include AccountAvatar
   include AccountFinderConcern
index aa649652c45e279777ea7ae991d2216b243918a9..65e6b1361887d2f8af7cff1ea62c6dfce75a2c85 100644 (file)
@@ -10,18 +10,21 @@ class ProcessMentionsService < BaseService
   def call(status)
     return unless status.local?
 
-    status.text.scan(Account::MENTION_RE).each do |match|
+    status.text = status.text.gsub(Account::MENTION_RE) do |match|
       begin
-        mentioned_account = resolve_remote_account_service.call(match.first.to_s)
+        mentioned_account = resolve_remote_account_service.call($1)
       rescue Goldfinger::Error, HTTP::Error
         mentioned_account = nil
       end
 
-      next if mentioned_account.nil? || (mentioned_account.ostatus? && status.stream_entry.hidden?)
+      next match if mentioned_account.nil? || (mentioned_account.ostatus? && status.stream_entry.hidden?)
 
       mentioned_account.mentions.where(status: status).first_or_create(status: status)
+      "@#{mentioned_account.acct}"
     end
 
+    status.save!
+
     status.mentions.includes(:account).each do |mention|
       create_notification(status, mention)
     end