return $params;
}
+ /**
+ * Finds all used smilies (like :heart: or :p) in the provided text.
+ *
+ * @param string $text that might contain smilie usages (denoted by a starting colon)
+ * @param bool $extract_url whether to further extract image urls
+ * @return array with smilie codes (colon included) as the keys, the smilie images as values
+ */
+ public static function extractUsedSmilies(string $text, bool $extract_url = false): array
+ {
+ $emojis = [];
+
+ $smilies = self::getList();
+ $icons = $smilies['icons'];
+ foreach ($smilies['texts'] as $i => $name) {
+ if (strstr($text, $name)) {
+ $image = $icons[$i];
+ if ($extract_url) {
+ if (preg_match('/src="(.+?)"/', $image, $match)) {
+ $image = $match[1];
+ } else {
+ continue;
+ }
+ }
+ $emojis[$name] = $image;
+ }
+ }
+ return $emojis;
+ }
/**
* Copied from http://php.net/manual/en/function.str-replace.php#88569
}
/**
+ * Creates an emoji collection from shortcode => image mappings.
+ *
* @param array $smilies
*
* @return Emojis
*/
- public function createCollectionFromSmilies(array $smilies): Emojis
+ public function createCollectionFromArray(array $smilies): Emojis
{
$prototype = null;
$emojis = [];
- foreach ($smilies['texts'] as $key => $shortcode) {
- if (preg_match('/src="(.+?)"/', $smilies['icons'][$key], $matches)) {
+ foreach ($smilies as $shortcode => $icon) {
+ if (preg_match('/src="(.+?)"/', $icon, $matches)) {
$url = $matches[1];
+ $shortcode = trim($shortcode, ':');
if ($prototype === null) {
$prototype = $this->create($shortcode, $url);
} else {
$emojis[] = \Friendica\Object\Api\Mastodon\Emoji::createFromPrototype($prototype, $shortcode, $url);
}
- };
+ }
}
return new Emojis($emojis);
}
+
+ /**
+ * @param array $smilies
+ *
+ * @return Emojis
+ */
+ public function createCollectionFromSmilies(array $smilies): Emojis
+ {
+ return self::createCollectionFromArray(array_combine($smilies['texts'], $smilies['icons']));
+ }
}
use Friendica\BaseFactory;
use Friendica\Content\ContactSelector;
use Friendica\Content\Item as ContentItem;
+use Friendica\Content\Smilies;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Logger;
use Friendica\Database\Database;
private $mstdnCardFactory;
/** @var Attachment */
private $mstdnAttachmentFactory;
+ /** @var Emoji */
+ private $mstdnEmojiFactory;
/** @var Error */
private $mstdnErrorFactory;
/** @var Poll */
Tag $mstdnTagFactory,
Card $mstdnCardFactory,
Attachment $mstdnAttachmentFactory,
+ Emoji $mstdnEmojiFactory,
Error $mstdnErrorFactory,
Poll $mstdnPollFactory,
ContentItem $contentItem,
$this->mstdnTagFactory = $mstdnTagFactory;
$this->mstdnCardFactory = $mstdnCardFactory;
$this->mstdnAttachmentFactory = $mstdnAttachmentFactory;
+ $this->mstdnEmojiFactory = $mstdnEmojiFactory;
$this->mstdnErrorFactory = $mstdnErrorFactory;
$this->mstdnPollFactory = $mstdnPollFactory;
$this->contentItem = $contentItem;
}
}
+ $used_smilies = Smilies::extractUsedSmilies($item['body'] ?: $item['raw-body']);
+ $emojis = $this->mstdnEmojiFactory->createCollectionFromArray($used_smilies)->getArrayCopy(true);
+
if ($is_reshare) {
try {
$reshare = $this->createFromUriId($uriId, $uid, $display_quote, false, false)->toArray();
$visibility_data = $uid != $item['uid'] ? null : new FriendicaVisibility($this->aclFormatter->expand($item['allow_cid']), $this->aclFormatter->expand($item['deny_cid']), $this->aclFormatter->expand($item['allow_gid']), $this->aclFormatter->expand($item['deny_gid']));
$friendica = new FriendicaExtension($item['title'] ?? '', $item['changed'], $item['commented'], $item['received'], $counts->dislikes, $origin_dislike, $delivery_data, $visibility_data);
- return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $in_reply, $reshare, $friendica, $quote, $poll);
+ return new \Friendica\Object\Api\Mastodon\Status($item, $account, $counts, $userAttributes, $sensitive, $application, $mentions, $tags, $card, $attachments, $in_reply, $reshare, $friendica, $quote, $poll, $emojis);
}
/**
* @param array $item
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
- public function __construct(array $item, Account $account, Counts $counts, UserAttributes $userAttributes, bool $sensitive, Application $application, array $mentions, array $tags, Card $card, array $attachments, array $in_reply, array $reblog, FriendicaExtension $friendica, array $quote = null, array $poll = null)
+ public function __construct(array $item, Account $account, Counts $counts, UserAttributes $userAttributes, bool $sensitive, Application $application, array $mentions, array $tags, Card $card, array $attachments, array $in_reply, array $reblog, FriendicaExtension $friendica, array $quote = null, array $poll = null, array $emojis = null)
{
$reblogged = !empty($reblog);
$this->id = (string)$item['uri-id'];
$this->media_attachments = $reblogged ? [] : $attachments;
$this->mentions = $reblogged ? [] : $mentions;
$this->tags = $reblogged ? [] : $tags;
- $this->emojis = $reblogged ? [] : [];
+ $this->emojis = $reblogged ? [] : ($emojis ?: []);
$this->card = $reblogged ? null : ($card->toArray() ?: null);
$this->poll = $reblogged ? null : $poll;
$this->friendica = $reblogged ? null : $friendica;
use Friendica\App;
use Friendica\Content\Feature;
+use Friendica\Content\Smilies;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Cache\Enum\Duration;
use Friendica\Core\Logger;
return $location;
}
+ /**
+ * Appends emoji tags to a tag array according to the tags used.
+ *
+ * @param array $tags Tag array
+ * @param string $text Text containing tags like :tag:
+ */
+ private static function addEmojiTags(array &$tags, string $text)
+ {
+ foreach (Smilies::extractUsedSmilies($text, true) as $name => $url) {
+ $tags[] = [
+ 'type' => 'Emoji',
+ 'name' => $name,
+ 'icon' => [
+ 'type' => 'Image',
+ 'url' => $url,
+ ],
+ ];
+ }
+ }
+
/**
* Returns a tag array for a given item array
*
}
}
+ self::addEmojiTags($tags, $item['body']);
+
$announce = self::getAnnounceArray($item);
// Mention the original author upon commented reshares
if (!empty($announce['comment'])) {