diff --git a/app/Util/Lexer/Autolink.php b/app/Util/Lexer/Autolink.php index 4aa38d7f1..c40f552f4 100755 --- a/app/Util/Lexer/Autolink.php +++ b/app/Util/Lexer/Autolink.php @@ -10,6 +10,7 @@ namespace App\Util\Lexer; use Illuminate\Support\Str; +use App\Services\AutolinkService; /** * Twitter Autolink Class. @@ -140,6 +141,12 @@ class Autolink extends Regex */ protected $extractor = null; + /** + * @var autolinkActiveUsersOnly + */ + protected $autolinkActiveUsersOnly = false; + + /** * Provides fluent method chaining. * @@ -185,6 +192,30 @@ class Autolink extends Regex $this->url_base_cash = config('app.url').'/search?q=%24'; } + public function setBaseUserPath($path = '/') + { + $this->url_base_user = config('app.url') . $path; + $this->target = null; + $this->external = null; + return $this; + } + + public function setBaseHashPath($path = '/discover/tags/') + { + $this->url_base_hash = config('app.url') . $path; + $this->target = null; + $this->external = null; + return $this; + } + + public function setAutolinkActiveUsersOnly($active) + { + $this->autolinkActiveUsersOnly = $active; + $this->target = null; + $this->external = null; + return $this; + } + /** * CSS class for auto-linked URLs. * @@ -529,6 +560,14 @@ class Autolink extends Regex } $entities = $this->extractor->extractMentionsOrListsWithIndices($tweet); + if($this->autolinkActiveUsersOnly == true) { + $entities = collect($entities) + ->filter(function($entity) { + return AutolinkService::mentionedUsernameExists($entity['screen_name']); + }) + ->toArray(); + } + return $this->autoLinkEntities($tweet, $entities); } @@ -707,8 +746,14 @@ class Autolink extends Regex public function linkToMentionAndList($entity) { $attributes = []; - $screen_name = $entity['screen_name']; + + if($this->autolinkActiveUsersOnly == true) { + if(!AutolinkService::mentionedUsernameExists($screen_name)) { + return Str::of($screen_name)->startsWith('@') ? $screen_name : "@{$screen_name}"; + } + } + if (!empty($entity['list_slug'])) { // Replace the list and username $linkText = Str::startsWith($screen_name, '@') ? $screen_name : '@'.$screen_name; diff --git a/app/Util/Lexer/Extractor.php b/app/Util/Lexer/Extractor.php index d4536a846..364a1c573 100755 --- a/app/Util/Lexer/Extractor.php +++ b/app/Util/Lexer/Extractor.php @@ -11,6 +11,7 @@ namespace App\Util\Lexer; use Illuminate\Support\Str; use App\Status; +use App\Services\AutolinkService; /** * Twitter Extractor Class. @@ -33,6 +34,7 @@ class Extractor extends Regex * @var bool */ protected $extractURLWithoutProtocol = true; + protected $activeUsersOnly = false; /** * Provides fluent method chaining. @@ -48,6 +50,12 @@ class Extractor extends Regex return new self($tweet); } + public function setActiveUsersOnly($active) + { + $this->activeUsersOnly = $active; + return $this; + } + /** * Reads in a tweet to be parsed and extracts elements from it. * @@ -172,6 +180,12 @@ class Extractor extends Regex $mentionsWithIndices = $this->extractMentionsOrListsWithIndices($tweet); foreach ($mentionsWithIndices as $mentionWithIndex) { + if($this->activeUsersOnly == true) { + if(!AutolinkService::mentionedUsernameExists($mentionWithIndex['screen_name'])) { + continue; + } + } + $screen_name = mb_strtolower($mentionWithIndex['screen_name']); if (empty($screen_name) or in_array($screen_name, $usernamesOnly)) { continue; @@ -452,9 +466,13 @@ class Extractor extends Regex $start_position = $at[1] > 0 ? StringUtils::strlen(substr($tweet, 0, $at[1])) : $at[1]; $end_position = $start_position + StringUtils::strlen($at[0]) + StringUtils::strlen($username[0]); $screenname = trim($all[0]) == '@'.$username[0] ? $username[0] : trim($all[0]); - if(config('app.env') == 'production' && \App\Profile::whereUsername($screenname)->exists() == false) { - continue; - } + + if($this->activeUsersOnly == true) { + if(!AutolinkService::mentionedUsernameExists($screenname)) { + continue; + } + } + $entity = [ 'screen_name' => $screenname, 'list_slug' => $list_slug[0],