From daa9f497a5a76c0d46d1563c2f1998c4bc2db37e Mon Sep 17 00:00:00 2001 From: Ryan Irelan Date: Fri, 13 Nov 2020 08:42:16 -0600 Subject: [PATCH 1/4] initial work for allowing live not live status toggling via webhook --- src/config.php | 1 + src/controllers/StreamController.php | 56 ++++++++++++++++++++++++++++ src/models/Settings.php | 1 + src/services/Stream.php | 1 + src/templates/settings.twig | 7 ++++ 5 files changed, 66 insertions(+) create mode 100644 src/controllers/StreamController.php create mode 100644 src/services/Stream.php diff --git a/src/config.php b/src/config.php index 4c01999..bca38c8 100644 --- a/src/config.php +++ b/src/config.php @@ -26,4 +26,5 @@ // YouTube Channel ID "youtubeChannelId" => '', "isLive" => false, + "connectionToken" => '', ]; diff --git a/src/controllers/StreamController.php b/src/controllers/StreamController.php new file mode 100644 index 0000000..0a077b7 --- /dev/null +++ b/src/controllers/StreamController.php @@ -0,0 +1,56 @@ +embed->isLive(); + + if ($streamIsLive) { + // turn it off + YoutubeLiveEmbed::$plugin->setStreamStatus(false); + } + elseif (!$streamIsLive) { + // turn it on + YoutubeLiveEmbed::$plugin->setStreamStatus(true); + } + + + } +} diff --git a/src/models/Settings.php b/src/models/Settings.php index 49e9f0a..a16a107 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -44,6 +44,7 @@ public function rules() ['youtubeChannelId', 'string'], ['youtubeChannelId', 'default', 'value' => ''], ['isLive', 'boolean'], + ['connectionToken', 'string'], ]; } } diff --git a/src/services/Stream.php b/src/services/Stream.php new file mode 100644 index 0000000..b3d9bbc --- /dev/null +++ b/src/services/Stream.php @@ -0,0 +1 @@ + Date: Fri, 13 Nov 2020 18:05:37 -0600 Subject: [PATCH 2/4] action controller to turn stream status on and off --- src/YoutubeLiveEmbed.php | 2 ++ src/controllers/StreamController.php | 20 ++++++------- src/models/Settings.php | 5 ++++ src/services/Embed.php | 5 ++++ src/services/Stream.php | 43 ++++++++++++++++++++++++++++ src/templates/settings.twig | 4 +-- 6 files changed, 67 insertions(+), 12 deletions(-) diff --git a/src/YoutubeLiveEmbed.php b/src/YoutubeLiveEmbed.php index 1f2c7d0..012106a 100644 --- a/src/YoutubeLiveEmbed.php +++ b/src/YoutubeLiveEmbed.php @@ -11,6 +11,7 @@ namespace nystudio107\youtubeliveembed; use nystudio107\youtubeliveembed\services\Embed as EmbedService; +use nystudio107\youtubeliveembed\services\Stream as StreamService; use nystudio107\youtubeliveembed\variables\YoutubeLiveEmbedVariable; use nystudio107\youtubeliveembed\models\Settings; @@ -30,6 +31,7 @@ * @since 1.0.0 * * @property EmbedService $embed + * * @property StreamService $stream */ class YoutubeLiveEmbed extends Plugin { diff --git a/src/controllers/StreamController.php b/src/controllers/StreamController.php index 0a077b7..905e56c 100644 --- a/src/controllers/StreamController.php +++ b/src/controllers/StreamController.php @@ -26,7 +26,7 @@ class StreamController extends Controller // ========================================================================= protected $allowAnonymous = [ - 'is-live', + 'update-stream-status', ]; // Public Methods @@ -38,19 +38,19 @@ class StreamController extends Controller * * */ - public function actionUpdateStreamStatus() + public function actionUpdateStreamStatus($yttoken = '') { $streamIsLive = YoutubeLiveEmbed::$plugin->embed->isLive(); + $myPlugin = Craft::$app->plugins->getPlugin( 'youtubeliveembed' ); - if ($streamIsLive) { - // turn it off - YoutubeLiveEmbed::$plugin->setStreamStatus(false); + if (YoutubeLiveEmbed::$plugin->embed->isLive()) { + $settings = array('isLive' => false); } - elseif (!$streamIsLive) { - // turn it on - YoutubeLiveEmbed::$plugin->setStreamStatus(true); + else { + $settings = array('isLive' => true); + } + if ( YoutubeLiveEmbed::$plugin->embed->getToken() == $yttoken) { + return Craft::$app->plugins->savePluginSettings( $myPlugin, $settings ); } - - } } diff --git a/src/models/Settings.php b/src/models/Settings.php index a16a107..d5abb0d 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -32,6 +32,11 @@ class Settings extends Model */ public $isLive = false; + /** + * @var string + */ + public $connectionToken = ''; + // Public Methods // ========================================================================= diff --git a/src/services/Embed.php b/src/services/Embed.php index 63bad67..b812202 100644 --- a/src/services/Embed.php +++ b/src/services/Embed.php @@ -141,6 +141,11 @@ public function isLive(): bool return YoutubeLiveEmbed::getInstance()->settings->isLive; } + public function getToken(): string + { + return YoutubeLiveEmbed::getInstance()->settings->connectionToken; + } + // Protected Methods // ========================================================================= diff --git a/src/services/Stream.php b/src/services/Stream.php index b3d9bbc..fa43a29 100644 --- a/src/services/Stream.php +++ b/src/services/Stream.php @@ -1 +1,44 @@ validateToken($token)) { + return YoutubeLiveEmbed::$plugin->savePluginSettings(YoutubeLiveEmbed::$plugin, array('isLive' => $status)); + } + + } + + private function validateToken($token) + { + if ($plugin->getSettings('connectionToken') == $token) { + return true; + } + } +} diff --git a/src/templates/settings.twig b/src/templates/settings.twig index c00aa54..5b985a2 100644 --- a/src/templates/settings.twig +++ b/src/templates/settings.twig @@ -23,8 +23,8 @@ value: settings['youtubeChannelId'] }) }} {{ forms.textField({ - label: 'Connection Token'|t('youtubeliveembed'), - instructions: 'A connection token you can use to toggle the stream status via cURL.'|t('youtubeliveembed'), + label: 'Connection Token', + instructions: 'A connection token you can use to toggle the stream status via cURL.', id: 'connectionToken', name: 'connectionToken', value: settings['connectionToken'] From f9b769dfd0acc6d57c748dc2e43a8c91e65523b5 Mon Sep 17 00:00:00 2001 From: Ryan Irelan Date: Sat, 14 Nov 2020 16:10:33 -0600 Subject: [PATCH 3/4] refactored to use service --- composer.json | 3 ++- src/YoutubeLiveEmbed.php | 7 +++++-- src/controllers/StreamController.php | 21 ++++++--------------- src/services/Embed.php | 5 ----- src/services/Stream.php | 16 ++++++++-------- 5 files changed, 21 insertions(+), 31 deletions(-) diff --git a/composer.json b/composer.json index 80a2728..4a354fa 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,8 @@ "hasCpSection": false, "changelogUrl": "https://raw.githubusercontent.com/nystudio107/craft-youtubeliveembed/master/CHANGELOG.md", "components": { - "embed": "nystudio107\\youtubeliveembed\\services\\Embed" + "embed": "nystudio107\\youtubeliveembed\\services\\Embed", + "stream": "nystudio107\\youtubelivembed\\services\\Stream" }, "class": "nystudio107\\youtubeliveembed\\YoutubeLiveEmbed" } diff --git a/src/YoutubeLiveEmbed.php b/src/YoutubeLiveEmbed.php index 012106a..32e064e 100644 --- a/src/YoutubeLiveEmbed.php +++ b/src/YoutubeLiveEmbed.php @@ -31,7 +31,7 @@ * @since 1.0.0 * * @property EmbedService $embed - * * @property StreamService $stream + * @property StreamService $stream */ class YoutubeLiveEmbed extends Plugin { @@ -54,7 +54,7 @@ class YoutubeLiveEmbed extends Plugin /** * @var string */ - public $schemaVersion = '1.0.0'; + public $schemaVersion = '1.0.9'; // Public Methods // ========================================================================= @@ -65,6 +65,9 @@ class YoutubeLiveEmbed extends Plugin public function init() { parent::init(); + $this->setComponents([ + 'stream' => services\Stream::class, + ]); self::$plugin = $this; self::$youtubeChannelId = $this->getSettings()->youtubeChannelId; diff --git a/src/controllers/StreamController.php b/src/controllers/StreamController.php index 905e56c..583f9c2 100644 --- a/src/controllers/StreamController.php +++ b/src/controllers/StreamController.php @@ -23,7 +23,7 @@ class StreamController extends Controller { // Protected Properties - // ========================================================================= + // =========== ============================================================== protected $allowAnonymous = [ 'update-stream-status', @@ -36,21 +36,12 @@ class StreamController extends Controller /** * Toggles the stream to live or offline in plugin * - * + * @param string $yttoken + * @return */ + public function actionUpdateStreamStatus($yttoken = '') { - $streamIsLive = YoutubeLiveEmbed::$plugin->embed->isLive(); - $myPlugin = Craft::$app->plugins->getPlugin( 'youtubeliveembed' ); - - if (YoutubeLiveEmbed::$plugin->embed->isLive()) { - $settings = array('isLive' => false); - } - else { - $settings = array('isLive' => true); - } - if ( YoutubeLiveEmbed::$plugin->embed->getToken() == $yttoken) { - return Craft::$app->plugins->savePluginSettings( $myPlugin, $settings ); - } + return YoutubeLiveEmbed::$plugin->stream->setStreamStatus($yttoken); } -} +} \ No newline at end of file diff --git a/src/services/Embed.php b/src/services/Embed.php index b812202..63bad67 100644 --- a/src/services/Embed.php +++ b/src/services/Embed.php @@ -141,11 +141,6 @@ public function isLive(): bool return YoutubeLiveEmbed::getInstance()->settings->isLive; } - public function getToken(): string - { - return YoutubeLiveEmbed::getInstance()->settings->connectionToken; - } - // Protected Methods // ========================================================================= diff --git a/src/services/Stream.php b/src/services/Stream.php index fa43a29..2c627d4 100644 --- a/src/services/Stream.php +++ b/src/services/Stream.php @@ -27,18 +27,18 @@ class Stream extends Component { - function setStreamStatus($status, $token) + public function setStreamStatus($yttoken) { - if ($this->validateToken($token)) { - return YoutubeLiveEmbed::$plugin->savePluginSettings(YoutubeLiveEmbed::$plugin, array('isLive' => $status)); - } + $myPlugin = Craft::$app->plugins->getPlugin('youtubeliveembed'); + $isLive = (YoutubeLiveEmbed::$plugin->embed->isLive() ? false : true); + if ($this->getToken() == $yttoken) { + return Craft::$app->plugins->savePluginSettings($myPlugin, array('isLive' => $isLive)); + } } - private function validateToken($token) + protected function getToken(): string { - if ($plugin->getSettings('connectionToken') == $token) { - return true; - } + return YoutubeLiveEmbed::getInstance()->settings->connectionToken; } } From c2d7913c13c3ab2c2ece7446710b237532fb1898 Mon Sep 17 00:00:00 2001 From: Ryan Irelan Date: Sat, 14 Nov 2020 16:14:37 -0600 Subject: [PATCH 4/4] updated changelog and composer version --- CHANGELOG.md | 12 ++++++++++++ composer.json | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec473fd..82d475e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # YouTube Live Embed Changelog +### 1.0.8 - 2020-11-14 +### Added +- Support for webhook to toggle livestream status on and off. + +## 1.0.7 - 2020-11-12 +### Fixed +- Fixed additional issue with regex of livestream page to get videoId + +## 1.0.6 - 2020-11-12 +### Fixed +- Fixed issue with regex of livestream page to get videoId so the chat would load properly + ## 1.0.5 2020-07-12 ### Fixed diff --git a/composer.json b/composer.json index 4a354fa..babeb2b 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nystudio107/craft-youtubeliveembed", "description": "This plugin allows you to embed a YouTube live stream and/or live chat on your webpage", "type": "craft-plugin", - "version": "1.0.7", + "version": "1.0.8", "keywords": [ "craft", "cms",