1
0
Fork 0

New function to convert BBCode for a given ID

This commit is contained in:
Michael 2021-07-08 13:47:46 +00:00
commit f29bd23ea8
15 changed files with 145 additions and 38 deletions

View file

@ -1263,6 +1263,37 @@ class BBCode
return $bbcode;
}
/**
* Converts a BBCode message for a given ID to a HTML message
*
* BBcode 2 HTML was written by WAY2WEB.net
* extended to work with Mistpark/Friendica - Mike Macgirvin
*
* Simple HTML values meaning:
* - 0: Friendica display
* - 1: Unused
* - 2: Used for Windows Phone push, Friendica API
* - 3: Used before converting to Markdown in bb2diaspora.php
* - 4: Used for WordPress, Libertree (before Markdown), pump.io and tumblr
* - 5: Unused
* - 6: Unused
* - 7: Used for dfrn, OStatus
* - 8: Used for Twitter, WP backlink text setting
* - 9: ActivityPub
*
* @param int $uriid
* @param string $text
* @param int $simple_html
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function convertForItem(int $uriid, string $text, int $simple_html = self::INTERNAL)
{
$try_oembed = ($simple_html == self::INTERNAL);
return self::convert($text, $try_oembed, $simple_html, false, $uriid);
}
/**
* Converts a BBCode message to HTML message
*

View file

@ -1886,7 +1886,7 @@ class Contact
{
if (Strings::normaliseLink($new_url) != Strings::normaliseLink($old_url)) {
Logger::notice('New URL differs from old URL', ['old' => $old_url, 'new' => $new_url]);
// @todo It is to decide what to do when the URL is changed
return;
}
if (!DBA::update('contact', $fields, ['id' => $id])) {
@ -2073,6 +2073,14 @@ class Contact
return false;
}
if (Strings::normaliseLink($ret['url']) != Strings::normaliseLink($contact['url'])) {
$cid = self::getIdForURL($ret['url']);
if (!empty($cid) && ($cid != $id)) {
Logger::notice('URL of contact changed.', ['id' => $id, 'new_id' => $cid, 'old' => $contact['url'], 'new' => $ret['url']]);
return self::updateFromProbeArray($cid, $ret);
}
}
if (isset($ret['hide']) && is_bool($ret['hide'])) {
$ret['unsearchable'] = $ret['hide'];
}

View file

@ -273,6 +273,7 @@ class Event
$event['cid'] = intval($arr['cid'] ?? 0);
$event['guid'] = ($arr['guid'] ?? '') ?: System::createUUID();
$event['uri'] = ($arr['uri'] ?? '') ?: Item::newURI($event['uid'], $event['guid']);
$event['uri-id'] = ItemURI::insert(['uri' => $event['uri'], 'guid' => $event['guid']]);
$event['type'] = ($arr['type'] ?? '') ?: 'event';
$event['summary'] = $arr['summary'] ?? '';
$event['desc'] = $arr['desc'] ?? '';
@ -937,7 +938,7 @@ class Event
$tpl = Renderer::getMarkupTemplate('event_stream_item.tpl');
$return = Renderer::replaceMacros($tpl, [
'$id' => $item['event-id'],
'$title' => BBCode::convert($item['event-summary']),
'$title' => BBCode::convertForItem($item['uri-id'], $item['event-summary']),
'$dtstart_label' => DI::l10n()->t('Starts:'),
'$dtstart_title' => $dtstart_title,
'$dtstart_dt' => $dtstart_dt,
@ -955,7 +956,7 @@ class Event
'$author_name' => $item['author-name'],
'$author_link' => $profile_link,
'$author_avatar' => $item['author-avatar'],
'$description' => BBCode::convert($item['event-desc']),
'$description' => BBCode::convertForItem($item['uri-id'], $item['event-desc']),
'$location_label' => DI::l10n()->t('Location:'),
'$show_map_label' => DI::l10n()->t('Show map'),
'$hide_map_label' => DI::l10n()->t('Hide map'),

View file

@ -2640,7 +2640,7 @@ class Item
) {
self::addRedirToImageTags($item);
$item['rendered-html'] = BBCode::convert($item['body'], true, BBCode::INTERNAL, false, $item['uri-id']);
$item['rendered-html'] = BBCode::convertForItem($item['uri-id'], $item['body']);
$item['rendered-hash'] = hash('md5', BBCode::VERSION . '::' . $body);
$hook_data = ['item' => $item, 'rendered-html' => $item['rendered-html'], 'rendered-hash' => $item['rendered-hash']];

View file

@ -140,6 +140,8 @@ class Statuses extends BaseApi
$item['gravity'] = GRAVITY_COMMENT;
$item['object-type'] = Activity\ObjectType::COMMENT;
} else {
self::checkThrottleLimit();
$item['gravity'] = GRAVITY_PARENT;
$item['object-type'] = Activity\ObjectType::NOTE;
}

View file

@ -25,9 +25,11 @@ use Friendica\BaseModule;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\DI;
use Friendica\Model\Post;
use Friendica\Network\HTTPException;
use Friendica\Security\BasicAuth;
use Friendica\Security\OAuth;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\HTTPInputData;
require_once __DIR__ . '/../../include/api.php';
@ -282,6 +284,60 @@ class BaseApi extends BaseModule
}
}
public static function checkThrottleLimit()
{
$uid = self::getCurrentUserID();
// Check for throttling (maximum posts per day, week and month)
$throttle_day = DI::config()->get('system', 'throttle_limit_day');
if ($throttle_day > 0) {
$datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60);
$condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, $uid, $datefrom];
$posts_day = Post::count($condition);
if ($posts_day > $throttle_day) {
Logger::info('Daily posting limit reached', ['uid' => $uid, 'posts' => $posts_day, 'limit' => $throttle_day]);
$error = DI::l10n()->t('Too Many Requests');
$error_description = DI::l10n()->tt("Daily posting limit of %d post reached. The post was rejected.", "Daily posting limit of %d posts reached. The post was rejected.", $throttle_day);
$errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
System::jsonError(429, $errorobj->toArray());
}
}
$throttle_week = DI::config()->get('system', 'throttle_limit_week');
if ($throttle_week > 0) {
$datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*7);
$condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, $uid, $datefrom];
$posts_week = Post::count($condition);
if ($posts_week > $throttle_week) {
Logger::info('Weekly posting limit reached', ['uid' => $uid, 'posts' => $posts_week, 'limit' => $throttle_week]);
$error = DI::l10n()->t('Too Many Requests');
$error_description = DI::l10n()->tt("Weekly posting limit of %d post reached. The post was rejected.", "Weekly posting limit of %d posts reached. The post was rejected.", $throttle_week);
$errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
System::jsonError(429, $errorobj->toArray());
}
}
$throttle_month = DI::config()->get('system', 'throttle_limit_month');
if ($throttle_month > 0) {
$datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*30);
$condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, $uid, $datefrom];
$posts_month = Post::count($condition);
if ($posts_month > $throttle_month) {
Logger::info('Monthly posting limit reached', ['uid' => $uid, 'posts' => $posts_month, 'limit' => $throttle_month]);
$error = DI::l10n()->t('Too Many Requests');
$error_description = DI::l10n()->t("Monthly posting limit of %d post reached. The post was rejected.", "Monthly posting limit of %d posts reached. The post was rejected.", $throttle_month);
$errorobj = new \Friendica\Object\Api\Mastodon\Error($error, $error_description);
System::jsonError(429, $errorobj->toArray());
}
}
}
/**
* Get user info array.
*

View file

@ -131,7 +131,7 @@ class Status extends BaseDataTransferObject
$this->muted = $userAttributes->muted;
$this->bookmarked = $userAttributes->bookmarked;
$this->pinned = $userAttributes->pinned;
$this->content = BBCode::convert($item['raw-body'] ?? $item['body'], false, BBCode::API, false, $item['uri-id']);
$this->content = BBCode::convertForItem($item['uri-id'], ($item['raw-body'] ?? $item['body']), BBCode::API);
$this->reblog = $reblog;
$this->application = $application->toArray();
$this->account = $account->toArray();

View file

@ -1464,7 +1464,7 @@ class Transmitter
{
$event = [];
$event['name'] = $item['event-summary'];
$event['content'] = BBCode::convert($item['event-desc'], false, BBCode::ACTIVITYPUB, false, $item['uri-id']);
$event['content'] = BBCode::convertForItem($item['uri-id'], $item['event-desc'], BBCode::ACTIVITYPUB);
$event['startTime'] = DateTimeFormat::utc($item['event-start'] . '+00:00', DateTimeFormat::ATOM);
if (!$item['event-nofinish']) {
@ -1571,7 +1571,7 @@ class Transmitter
$regexp = "/[@!]\[url\=([^\[\]]*)\].*?\[\/url\]/ism";
$body = preg_replace_callback($regexp, ['self', 'mentionCallback'], $body);
$data['content'] = BBCode::convert($body, false, BBCode::ACTIVITYPUB, false, $item['uri-id']);
$data['content'] = BBCode::convertForItem($item['uri-id'], $body, BBCode::ACTIVITYPUB);
}
// The regular "content" field does contain a minimized HTML. This is done since systems like
@ -1583,7 +1583,7 @@ class Transmitter
$richbody = preg_replace_callback($regexp, ['self', 'mentionCallback'], $item['body']);
$richbody = BBCode::removeAttachment($richbody);
$data['contentMap'][$language] = BBCode::convert($richbody, false, BBCode::EXTERNAL, false, $item['uri-id']);
$data['contentMap'][$language] = BBCode::convertForItem($item['uri-id'], $richbody, BBCode::EXTERNAL);
}
$data['source'] = ['content' => $item['body'], 'mediaType' => "text/bbcode"];

View file

@ -918,7 +918,7 @@ class DFRN
$htmlbody = "[b]" . $item['title'] . "[/b]\n\n" . $htmlbody;
}
$htmlbody = BBCode::convert($htmlbody, false, BBCode::OSTATUS, false, $item['uri-id']);
$htmlbody = BBCode::convert($item['uri-id'], $htmlbody, BBCode::OSTATUS);
}
$author = self::addEntryAuthor($doc, "author", $item["author-link"], $item);

View file

@ -1109,7 +1109,7 @@ class Feed
$body = OStatus::formatPicturePost($item['body'], $item['uri-id']);
$body = BBCode::convert($body, false, BBCode::OSTATUS, false, $item['uri-id']);
$body = BBCode::convertForItem($item['uri-id'], $body, BBCode::OSTATUS, false);
XML::addElement($doc, $entry, "content", $body, ["type" => "html"]);
@ -1186,7 +1186,7 @@ class Feed
private static function getTitle(array $item)
{
if ($item['title'] != '') {
return BBCode::convert($item['title'], false, BBCode::OSTATUS, false, $item['uri-id']);
return BBCode::convertForItem($item['uri-id'], $item['title'], BBCode::OSTATUS);
}
// Fetch information about the post

View file

@ -1803,7 +1803,7 @@ class OStatus
if (!$toplevel) {
if (!empty($item['title'])) {
$title = BBCode::convert($item['title'], false, BBCode::OSTATUS, false, $item['uri-id']);
$title = BBCode::convertForItem($item['uri-id'], $item['title'], BBCode::OSTATUS);
} else {
$title = sprintf("New note by %s", $owner["nick"]);
}
@ -1892,7 +1892,7 @@ class OStatus
$body = "[b]".$item['title']."[/b]\n\n".$body;
}
$body = BBCode::convert($body, false, BBCode::OSTATUS, false, $item['uri-id']);
$body = BBCode::convertForItem($item['uri-id'], $body, BBCode::OSTATUS);
XML::addElement($doc, $entry, "content", $body, ["type" => "html"]);

View file

@ -183,6 +183,7 @@ class ExpirePosts
AND NOT EXISTS(SELECT `thr-parent-id` FROM `post-user` WHERE `thr-parent-id` = `item-uri`.`id`)
AND NOT EXISTS(SELECT `external-id` FROM `post-user` WHERE `external-id` = `item-uri`.`id`)
AND NOT EXISTS(SELECT `uri-id` FROM `mail` WHERE `uri-id` = `item-uri`.`id`)
AND NOT EXISTS(SELECT `uri-id` FROM `event` WHERE `uri-id` = `item-uri`.`id`)
AND NOT EXISTS(SELECT `parent-uri-id` FROM `mail` WHERE `parent-uri-id` = `item-uri`.`id`)
AND NOT EXISTS(SELECT `thr-parent-id` FROM `mail` WHERE `thr-parent-id` = `item-uri`.`id`)", $item['uri-id']]);