From a597cf4ecfa6807a3413177d99c816056a7e7c45 Mon Sep 17 00:00:00 2001
From: Yassine Doghri <yassine@doghri.fr>
Date: Thu, 3 Mar 2022 16:15:42 +0000
Subject: [PATCH] feat(podcasting 2.0): update podcast:social tag to adhere to
 latest spec

---
 app/Helpers/rss_helper.php                    | 86 ++++++++++++++++++-
 .../Controllers/PodcastPlatformController.php |  2 +-
 2 files changed, 84 insertions(+), 4 deletions(-)

diff --git a/app/Helpers/rss_helper.php b/app/Helpers/rss_helper.php
index 355460d04f..d9b8e17974 100644
--- a/app/Helpers/rss_helper.php
+++ b/app/Helpers/rss_helper.php
@@ -101,11 +101,73 @@ if (! function_exists('get_rss_feed')) {
             }
         }
 
+        $castopodSocialElement = $channel->addChild('social', null, $podcastNamespace);
+        $castopodSocialElement->addAttribute('priority', '1');
+        $castopodSocialElement->addAttribute('platform', 'castopod');
+        $castopodSocialElement->addAttribute('protocol', 'activitypub');
+        $castopodSocialElement->addAttribute('accountId', "@{$podcast->actor->username}@{$podcast->actor->domain}");
+        $castopodSocialElement->addAttribute('accountUrl', $podcast->link);
+
         foreach ($podcast->social_platforms as $socialPlatform) {
-            $socialPlatformElement = $channel->addChild('social', $socialPlatform->account_id, $podcastNamespace,);
-            $socialPlatformElement->addAttribute('platform', $socialPlatform->slug);
+            $socialElement = $channel->addChild('social', null, $podcastNamespace,);
+            $socialElement->addAttribute('priority', '2');
+            $socialElement->addAttribute('platform', $socialPlatform->slug);
+
+            // TODO: get activitypub info somewhere else
+            if (in_array(
+                $socialPlatform->slug,
+                ['mastodon', 'peertube', 'funkwhale', 'misskey', 'mobilizon', 'pixelfed', 'plume', 'writefreely'],
+                true
+            )) {
+                $socialElement->addAttribute('protocol', 'activitypub');
+            } else {
+                $socialElement->addAttribute('protocol', $socialPlatform->slug);
+            }
+
+            if ($socialPlatform->account_id !== null) {
+                $socialElement->addAttribute('accountId', esc($socialPlatform->account_id));
+            }
             if ($socialPlatform->link_url !== null) {
-                $socialPlatformElement->addAttribute('url', htmlspecialchars($socialPlatform->link_url));
+                $socialElement->addAttribute('accountUrl', esc($socialPlatform->link_url));
+            }
+
+            if ($socialPlatform->slug === 'mastodon') {
+                $socialSignUpelement = $socialElement->addChild('socialSignUp', null, $podcastNamespace);
+                $socialSignUpelement->addAttribute('priority', '1');
+                $socialSignUpelement->addAttribute(
+                    'homeUrl',
+                    parse_url($socialPlatform->link_url, PHP_URL_SCHEME) . '://' . parse_url(
+                        $socialPlatform->link_url,
+                        PHP_URL_HOST
+                    ) . '/public'
+                );
+                $socialSignUpelement->addAttribute(
+                    'signUpUrl',
+                    parse_url($socialPlatform->link_url, PHP_URL_SCHEME) . '://' . parse_url(
+                        $socialPlatform->link_url,
+                        PHP_URL_HOST
+                    ) . '/auth/sign_up'
+                );
+                $castopodSocialSignUpelement = $castopodSocialElement->addChild(
+                    'socialSignUp',
+                    null,
+                    $podcastNamespace
+                );
+                $castopodSocialSignUpelement->addAttribute('priority', '1');
+                $castopodSocialSignUpelement->addAttribute(
+                    'homeUrl',
+                    parse_url($socialPlatform->link_url, PHP_URL_SCHEME) . '://' . parse_url(
+                        $socialPlatform->link_url,
+                        PHP_URL_HOST
+                    ) . '/public'
+                );
+                $castopodSocialSignUpelement->addAttribute(
+                    'signUpUrl',
+                    parse_url($socialPlatform->link_url, PHP_URL_SCHEME) . '://' . parse_url(
+                        $socialPlatform->link_url,
+                        PHP_URL_HOST
+                    ) . '/auth/sign_up'
+                );
             }
         }
 
@@ -251,6 +313,24 @@ if (! function_exists('get_rss_feed')) {
             $comments->addAttribute('uri', url_to('episode-comments', $podcast->handle, $episode->slug));
             $comments->addAttribute('contentType', 'application/podcast-activity+json');
 
+            if ($episode->getPosts()) {
+                $socialInteractUrl = $episode->getPosts()[0]
+                    ->uri;
+                $socialInteractElement = $item->addChild('socialInteract', $socialInteractUrl, $podcastNamespace);
+                $socialInteractElement->addAttribute('priority', '1');
+                $socialInteractElement->addAttribute('platform', 'castopod');
+                $socialInteractElement->addAttribute('protocol', 'activitypub');
+                $socialInteractElement->addAttribute(
+                    'accountId',
+                    "@{$podcast->actor->username}@{$podcast->actor->domain}"
+                );
+                $socialInteractElement->addAttribute(
+                    'pubDate',
+                    $episode->getPosts()[0]
+                        ->published_at->format(DateTime::ISO8601)
+                );
+            }
+
             if ($episode->transcript !== null) {
                 $transcriptElement = $item->addChild('transcript', null, $podcastNamespace);
                 $transcriptElement->addAttribute('url', $episode->transcript->file_url);
diff --git a/modules/Admin/Controllers/PodcastPlatformController.php b/modules/Admin/Controllers/PodcastPlatformController.php
index 76b5346a87..0bdc4db5b5 100644
--- a/modules/Admin/Controllers/PodcastPlatformController.php
+++ b/modules/Admin/Controllers/PodcastPlatformController.php
@@ -81,7 +81,7 @@ class PodcastPlatformController extends BaseController
                 'platform_slug' => $platformSlug,
                 'podcast_id' => $this->podcast->id,
                 'link_url' => $podcastPlatformUrl,
-                'account_id' => $podcastPlatform['account_id'],
+                'account_id' => $podcastPlatform['account_id'] === '' ? null : $podcastPlatform['account_id'],
                 'is_visible' =>
                     array_key_exists('visible', $podcastPlatform) &&
                     $podcastPlatform['visible'] === 'yes',
-- 
GitLab