Initial URLify of vid desc and profile

This commit is contained in:
Pablo Ferreiro 2023-04-02 19:55:04 +02:00
parent 9364a2e212
commit 4188fcf064
No known key found for this signature in database
GPG key ID: 41FBCE65B779FA24
5 changed files with 37 additions and 5 deletions

View file

@ -0,0 +1,7 @@
<?php
namespace App\Constants;
abstract class TextExtras {
const USER = 0;
const HASHTAG = 1;
}

View file

@ -5,10 +5,9 @@ use App\Cache\ApcuCache;
use App\Cache\JSONCache;
use App\Cache\RedisCache;
use App\Constants\CacheMethods;
use App\Constants\TextExtras;
use App\Models\BaseTemplate;
use TikScraper\Constants\UserAgents as TikScraperUserAgents;
class Wrappers {
/**
* Setup of Latte template engine
@ -92,6 +91,30 @@ class Wrappers {
return UrlBuilder::download($url, $username, $id, $watermark);
});
// Add URLs to video descriptions
// TODO: Make it work with unicode characters such as emojis
$latte->addFunction('render_desc', function (string $desc, array $textExtras = []): string {
$sanitizedDesc = htmlspecialchars($desc);
$out = $sanitizedDesc;
foreach ($textExtras as $extra) {
$url = '';
$text = mb_substr($desc, $extra->start, $extra->end - $extra->start, 'UTF-8');
switch ($extra->type) {
// User URL
case TextExtras::USER:
$url = UrlBuilder::user(htmlspecialchars($extra->userUniqueId));
break;
// Hashtag URL
case TextExtras::HASHTAG:
$url = UrlBuilder::tag(htmlspecialchars($extra->hashtagName));
break;
}
$out = str_replace($text, "<a href=\"$url\">$text</a>", $out);
}
return $out;
});
$latte->render(Misc::getView($template), $base);
}

View file

@ -17,7 +17,7 @@
{if !empty($item->challenges)}
<p>{include './common/tags.latte', challenges: $item->challenges}</p>
{/if}
<p n:ifcontent>{$item->desc}</p>
<p n:ifcontent>{render_desc($item->desc, $item->textExtra)|noescape}</p>
<p>Song: {$item->music->title}</p>
<audio controls preload="none" src="{url_stream($item->music->playUrl)}"></audio>
{include './common/stats.latte', playCount: $item->stats->playCount, diggCount: $item->stats->diggCount, commentCount: $item->stats->commentCount, shareCount: $item->stats->shareCount}

View file

@ -15,8 +15,10 @@
{if $info->detail->privateAccount}
<p><strong>Private account</strong></p>
{/if}
<p></p>
<p>{$info->detail->signature}</p>
{if isset($info->detail->bioLink)}
<p>Link: <a href="{$info->detail->bioLink->link}">{$info->detail->bioLink->link}</a></p>
{/if}
<p>Following: {number($info->stats->followingCount)} / Followers: {number($info->stats->followerCount)}</p>
<p>Hearts: {number($info->stats->heartCount)} / Videos: {$info->stats->videoCount}</p>
{/block}

View file

@ -33,7 +33,7 @@
<p>{include '../components/themes/common/tags.latte', challenges: $item->challenges}</p>
{/if}
<div class="content">
<p n:ifcontent>{$item->desc}</p>
<p n:ifcontent>{render_desc($item->desc, $item->textExtra)|noescape}</p>
<p>Song: {$item->music->title}</p>
<audio controls preload="none" src="{url_stream($item->music->playUrl)}"></audio>
{include '../components/themes/common/stats.latte', playCount: $item->stats->playCount, diggCount: $item->stats->diggCount, commentCount: $item->stats->commentCount, shareCount: $item->stats->shareCount}