Update lexer utils

pull/3006/head
Daniel Supernault 2021-11-15 19:35:10 -07:00
rodzic 639e9859ed
commit ed62a09f9a
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 0DEF1C662C9033F7
2 zmienionych plików z 67 dodań i 4 usunięć

Wyświetl plik

@ -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;

Wyświetl plik

@ -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],