From 91b159f5344c9461fda775a017da653e1665fccc Mon Sep 17 00:00:00 2001 From: Jason Mayo Date: Tue, 17 Sep 2019 10:46:18 +0100 Subject: [PATCH] Added `EVENT_USER_CONNECTED` and `EVENT_USER_DISCONNECTED` events --- CHANGELOG.md | 6 +++- README.md | 44 +++++++++++++++++++++++++--- composer.json | 2 +- src/events/UserConnectedEvent.php | 13 ++++++++ src/events/UserDisconnectedEvent.php | 13 ++++++++ src/services/UserService.php | 44 +++++++++++++++++++++++++++- 6 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 src/events/UserConnectedEvent.php create mode 100644 src/events/UserDisconnectedEvent.php diff --git a/CHANGELOG.md b/CHANGELOG.md index c548499..be13aa4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,12 @@ # Strava Sync Changelog +## 1.0.11 - 2019-09-17 +### Added +- Added `EVENT_USER_CONNECTED` and `EVENT_USER_DISCONNECTED` events to check if a user has connected/disconnected Strava from Craft CMS. + ## 1.0.10 - 2019-09-16 ### Added -- Ability to get data from Strava webhooks, and sync with custom plugins via new `webhookSync` event +- Ability to get data from Strava webhooks, and sync with custom plugins via new `EVENT_WEBHOOK_SYNC` event. ## 1.0.9 - 2019-08-28 ### Fixed diff --git a/README.md b/README.md index ef7e473..cc7a9fd 100644 --- a/README.md +++ b/README.md @@ -4,14 +4,16 @@ Strava Sync is a Craft CMS plugin that lets you connect Strava with Craft CMS. Allowing users to login with Strava oAuth, get data from the Strava API (Athletes, activities, segments, routes etc) -https://plugins.craftcms.com/strava-sync +🛒 Buy on the Plugin Store - https://plugins.craftcms.com/strava-sync + +## The Plugin - [Features](#features) - [Requirements](#requirements) - [Install](#install) - [Configuration](#configuration) - [Options](#options) -- [Webhooks](#webhooks) +- [Events](#events) - [Support](#support) ## Features @@ -183,9 +185,43 @@ Depending on your scope type when you authorised the account, the supported requ - getStreamsSegment - getStreamsRoute -## Webhooks +## Events + +### Connected Event + +To recieve data when a user connects their Strava account to Craft, use the `EVENT_USER_CONNECTED` event. This returns `$event->user`: + + use bymayo\stravasync\events\UserConnectedEvent; + use bymayo\stravasync\services\UserService; + use yii\base\Event; + + Event::on( + UserService::class, + UserService::EVENT_USER_CONNECTED, + function(UserConnectedEvent $event) { + // Do something + } + ); + +### Disconnected Event + +To recieve data when a user disconnects their Strava account to Craft, use the `EVENT_USER_DISCONNECTED` event. This returns `$event->user`: + + use bymayo\stravasync\events\userDisconnectedEvent; + use bymayo\stravasync\services\UserService; + use yii\base\Event; + + Event::on( + UserService::class, + UserService::EVENT_USER_DISCONNECTED, + function(userDisconnectedEvent $event) { + // Do something + } + ); + +### Webhook Event -If you want to receive data from the Strava Webhook Events API (https://developers.strava.com/docs/webhooks/) when an activity/athlete is created or updated for example, you can use the plugins `webhookSync` event. +If you want to receive data from the Strava Webhook Events API (https://developers.strava.com/docs/webhooks/) when an activity/athlete is created or updated for example, you can use the plugins `EVENT_WEBHOOK_SYNC` event. To initally set this up, you need to request Webhook access from Strava (See _Webhooks Overview_ on https://developers.strava.com/docs/webhooks). Strava will then enable your account to access the Webhooks feature. diff --git a/composer.json b/composer.json index c08f924..f30116d 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "bymayo/strava-sync", "description": "Connect to Strava with oAuth and sync activities etc to Craft CMS ", "type": "craft-plugin", - "version": "1.0.10", + "version": "1.0.11", "keywords": [ "craft", "cms", diff --git a/src/events/UserConnectedEvent.php b/src/events/UserConnectedEvent.php new file mode 100644 index 0000000..977e1df --- /dev/null +++ b/src/events/UserConnectedEvent.php @@ -0,0 +1,13 @@ +users->getUserById($userId); + $record = new UsersRecord(); $record->userId = $userId; $record->athleteId = $this->_athlete['id']; @@ -108,6 +121,19 @@ public function linkUserToStrava($userId) $record->expires = $this->_tokens['expires']; $record->save(true); + if ($this->hasEventHandlers(self::EVENT_USER_CONNECTED)) { + + $this->trigger( + self::EVENT_USER_CONNECTED, + new UserConnectedEvent( + [ + 'user' => $user + ] + ) + ); + + } + } public function unlinkUserFromStrava($user) @@ -116,7 +142,22 @@ public function unlinkUserFromStrava($user) $athleteRecord = UsersRecord::findOne(['userId' => $user->id]); if ($athleteRecord) { + $athleteRecord->delete(); + + if ($this->hasEventHandlers(self::EVENT_USER_DISCONNECTED)) { + + $this->trigger( + self::EVENT_USER_DISCONNECTED, + new UserDisconnectedEvent( + [ + 'user' => $user + ] + ) + ); + + } + } return false; @@ -245,8 +286,9 @@ public function disconnect() $user = Craft::$app->getUser()->getIdentity(); if ($user) { + $this->unlinkUserFromStrava($user); - // return true; + } }