Skip to content

Commit

Permalink
Added EVENT_USER_CONNECTED and EVENT_USER_DISCONNECTED events
Browse files Browse the repository at this point in the history
  • Loading branch information
bymayo committed Sep 17, 2019
1 parent 046931c commit 91b159f
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 7 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
44 changes: 40 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions src/events/UserConnectedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace bymayo\stravasync\events;

use yii\base\Event;

class UserConnectedEvent extends Event
{
// Properties
// =========================================================================

public $user;

}
13 changes: 13 additions & 0 deletions src/events/UserDisconnectedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
namespace bymayo\stravasync\events;

use yii\base\Event;

class UserDisconnectedEvent extends Event
{
// Properties
// =========================================================================

public $user;

}
44 changes: 43 additions & 1 deletion src/services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use bymayo\stravasync\StravaSync;
use bymayo\stravasync\records\UsersRecord as UsersRecord;
use bymayo\stravasync\events\UserConnectedEvent;
use bymayo\stravasync\events\UserDisconnectedEvent;

use Craft;
use craft\base\Component;
Expand All @@ -16,6 +18,15 @@
class UserService extends Component
{

// Constants
// =========================================================================

const EVENT_USER_CONNECTED = 'userConnected';
const EVENT_USER_DISCONNECTED = 'userDisconnected';

// Private Vars
// =========================================================================

private $_requestClient;
private $_athlete;
private $_tokens;
Expand Down Expand Up @@ -100,6 +111,8 @@ public function postAuthenticateRedirect($tokens)
public function linkUserToStrava($userId)
{

$user = Craft::$app->users->getUserById($userId);

$record = new UsersRecord();
$record->userId = $userId;
$record->athleteId = $this->_athlete['id'];
Expand All @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -245,8 +286,9 @@ public function disconnect()
$user = Craft::$app->getUser()->getIdentity();

if ($user) {

$this->unlinkUserFromStrava($user);
// return true;

}

}
Expand Down

0 comments on commit 91b159f

Please sign in to comment.