mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 14:31:26 +00:00
Update lexer utils
This commit is contained in:
parent
639e9859ed
commit
ed62a09f9a
2 changed files with 67 additions and 4 deletions
|
@ -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;
|
||||
|
|
|
@ -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],
|
||||
|
|
Loading…
Reference in a new issue