]> git.mxchange.org Git - friendica.git/commitdiff
We now directly support the custom emojis from Mastodon
authorMichael <heluecht@pirati.ca>
Wed, 7 Nov 2018 20:34:03 +0000 (20:34 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 7 Nov 2018 20:34:03 +0000 (20:34 +0000)
src/Protocol/ActivityPub/Processor.php
src/Protocol/ActivityPub/Receiver.php
src/Util/JsonLD.php

index 1e5001010855ae68b8b8beaf25b75b6ed4903398..383ad35d9c7e5f4ff73ddfa055bcb0e5012bedce 100644 (file)
@@ -39,6 +39,22 @@ class Processor
                return $body;
        }
 
+       /**
+        * Replaces emojis in the body
+        *
+        * @param array $emojis
+        * @param string $body
+        *
+        * @return string with replaced emojis
+        */
+       public static function replaceEmojis($emojis, $body)
+       {
+               foreach ($emojis as $emoji) {
+                       $body = str_replace($emoji['name'], '[img=16x16]' . $emoji['href'] . '[/img]', $body);
+               }
+               return $body;
+       }
+
        /**
         * Constructs a string with tags for a given tag array
         *
@@ -115,7 +131,8 @@ class Processor
                $item['edited'] = $activity['updated'];
                $item['title'] = HTML::toBBCode($activity['name']);
                $item['content-warning'] = HTML::toBBCode($activity['summary']);
-               $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
+               $content = self::replaceEmojis($activity['emojis'], HTML::toBBCode($activity['content']));
+               $item['body'] = self::convertMentions($content);
                $item['tag'] = self::constructTagList($activity['tags'], $activity['sensitive']);
 
                Item::update($item, ['uri' => $activity['id']]);
@@ -250,7 +267,8 @@ class Processor
                $item['guid'] = $activity['diaspora:guid'];
                $item['title'] = HTML::toBBCode($activity['name']);
                $item['content-warning'] = HTML::toBBCode($activity['summary']);
-               $item['body'] = self::convertMentions(HTML::toBBCode($activity['content']));
+               $content = self::replaceEmojis($activity['emojis'], HTML::toBBCode($activity['content']));
+               $item['body'] = self::convertMentions($content);
 
                if (($activity['object_type'] == 'as:Video') && !empty($activity['alternate-url'])) {
                        $item['body'] .= "\n[video]" . $activity['alternate-url'] . '[/video]';
index 2cc165b233d7e230a44f7479b59a5f11633e8fc2..c958b9d6c3c46de6bf99ee0f3069cd1eee6c2c74 100644 (file)
@@ -727,13 +727,48 @@ class Receiver
                                continue;
                        }
 
-                       $taglist[] = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')),
+                       $element = ['type' => str_replace('as:', '', JsonLD::fetchElement($tag, '@type')),
                                'href' => JsonLD::fetchElement($tag, 'as:href'),
                                'name' => JsonLD::fetchElement($tag, 'as:name')];
+
+                       if (empty($element['type'])) {
+                               continue;
+                       }
+
+                       $taglist[] = $element;
                }
                return $taglist;
        }
 
+       /**
+        * Convert emojis from JSON-LD format into a simplified format
+        *
+        * @param array $tags Tags in JSON-LD format
+        *
+        * @return array with emojis in a simplified format
+        */
+       private static function processEmojis($emojis)
+       {
+               $emojilist = [];
+
+               if (empty($emojis)) {
+                       return [];
+               }
+
+               foreach ($emojis as $emoji) {
+                       if (empty($emoji) || (JsonLD::fetchElement($emoji, '@type') != 'toot:Emoji') || empty($emoji['as:icon'])) {
+                               continue;
+                       }
+
+                       $url = JsonLD::fetchElement($emoji['as:icon'], 'as:url');
+                       $element = ['name' => JsonLD::fetchElement($emoji, 'as:name'),
+                               'href' => $url];
+
+                       $emojilist[] = $element;
+               }
+               return $emojilist;
+       }
+
        /**
         * Convert attachments from JSON-LD format into a simplified format
         *
@@ -821,6 +856,7 @@ class Receiver
                $object_data['longitude'] = JsonLD::fetchElement($object_data, 'longitude', '@value');
                $object_data['attachments'] = self::processAttachments(JsonLD::fetchElementArray($object, 'as:attachment'));
                $object_data['tags'] = self::processTags(JsonLD::fetchElementArray($object, 'as:tag'));
+               $object_data['emojis'] = self::processEmojis(JsonLD::fetchElementArray($object, 'as:tag', 'toot:Emoji'));
                $object_data['generator'] = JsonLD::fetchElement($object, 'as:generator', 'as:name', '@type', 'as:Application');
                $object_data['alternate-url'] = JsonLD::fetchElement($object, 'as:url');
 
index bed7a67d60a01eeccf58bc6a312f6f363541f4b6..78d81816e25b1747c0f749e9488470bdcdfb6727 100644 (file)
@@ -90,7 +90,8 @@ class JsonLD
                        'dfrn' => (object)['@id' => 'http://purl.org/macgirvin/dfrn/1.0/', '@type' => '@id'],
                        'diaspora' => (object)['@id' => 'https://diasporafoundation.org/ns/', '@type' => '@id'],
                        'ostatus' => (object)['@id' => 'http://ostatus.org#', '@type' => '@id'],
-                       'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id']];
+                       'dc' => (object)['@id' => 'http://purl.org/dc/terms/', '@type' => '@id'],
+                       'toot' => (object)['@id' => 'http://joinmastodon.org/ns#', '@type' => '@id']];
 
                $jsonobj = json_decode(json_encode($json, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));