mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-22 06:21:27 +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;
|
namespace App\Util\Lexer;
|
||||||
|
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use App\Services\AutolinkService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Twitter Autolink Class.
|
* Twitter Autolink Class.
|
||||||
|
@ -140,6 +141,12 @@ class Autolink extends Regex
|
||||||
*/
|
*/
|
||||||
protected $extractor = null;
|
protected $extractor = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var autolinkActiveUsersOnly
|
||||||
|
*/
|
||||||
|
protected $autolinkActiveUsersOnly = false;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides fluent method chaining.
|
* Provides fluent method chaining.
|
||||||
*
|
*
|
||||||
|
@ -185,6 +192,30 @@ class Autolink extends Regex
|
||||||
$this->url_base_cash = config('app.url').'/search?q=%24';
|
$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.
|
* CSS class for auto-linked URLs.
|
||||||
*
|
*
|
||||||
|
@ -529,6 +560,14 @@ class Autolink extends Regex
|
||||||
}
|
}
|
||||||
$entities = $this->extractor->extractMentionsOrListsWithIndices($tweet);
|
$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);
|
return $this->autoLinkEntities($tweet, $entities);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -707,8 +746,14 @@ class Autolink extends Regex
|
||||||
public function linkToMentionAndList($entity)
|
public function linkToMentionAndList($entity)
|
||||||
{
|
{
|
||||||
$attributes = [];
|
$attributes = [];
|
||||||
|
|
||||||
$screen_name = $entity['screen_name'];
|
$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'])) {
|
if (!empty($entity['list_slug'])) {
|
||||||
// Replace the list and username
|
// Replace the list and username
|
||||||
$linkText = Str::startsWith($screen_name, '@') ? $screen_name : '@'.$screen_name;
|
$linkText = Str::startsWith($screen_name, '@') ? $screen_name : '@'.$screen_name;
|
||||||
|
|
|
@ -11,6 +11,7 @@ namespace App\Util\Lexer;
|
||||||
|
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use App\Status;
|
use App\Status;
|
||||||
|
use App\Services\AutolinkService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Twitter Extractor Class.
|
* Twitter Extractor Class.
|
||||||
|
@ -33,6 +34,7 @@ class Extractor extends Regex
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
protected $extractURLWithoutProtocol = true;
|
protected $extractURLWithoutProtocol = true;
|
||||||
|
protected $activeUsersOnly = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides fluent method chaining.
|
* Provides fluent method chaining.
|
||||||
|
@ -48,6 +50,12 @@ class Extractor extends Regex
|
||||||
return new self($tweet);
|
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.
|
* Reads in a tweet to be parsed and extracts elements from it.
|
||||||
*
|
*
|
||||||
|
@ -172,6 +180,12 @@ class Extractor extends Regex
|
||||||
$mentionsWithIndices = $this->extractMentionsOrListsWithIndices($tweet);
|
$mentionsWithIndices = $this->extractMentionsOrListsWithIndices($tweet);
|
||||||
|
|
||||||
foreach ($mentionsWithIndices as $mentionWithIndex) {
|
foreach ($mentionsWithIndices as $mentionWithIndex) {
|
||||||
|
if($this->activeUsersOnly == true) {
|
||||||
|
if(!AutolinkService::mentionedUsernameExists($mentionWithIndex['screen_name'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$screen_name = mb_strtolower($mentionWithIndex['screen_name']);
|
$screen_name = mb_strtolower($mentionWithIndex['screen_name']);
|
||||||
if (empty($screen_name) or in_array($screen_name, $usernamesOnly)) {
|
if (empty($screen_name) or in_array($screen_name, $usernamesOnly)) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -452,9 +466,13 @@ class Extractor extends Regex
|
||||||
$start_position = $at[1] > 0 ? StringUtils::strlen(substr($tweet, 0, $at[1])) : $at[1];
|
$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]);
|
$end_position = $start_position + StringUtils::strlen($at[0]) + StringUtils::strlen($username[0]);
|
||||||
$screenname = trim($all[0]) == '@'.$username[0] ? $username[0] : trim($all[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 = [
|
$entity = [
|
||||||
'screen_name' => $screenname,
|
'screen_name' => $screenname,
|
||||||
'list_slug' => $list_slug[0],
|
'list_slug' => $list_slug[0],
|
||||||
|
|
Loading…
Reference in a new issue