Skip to content

Commit

Permalink
Added Webhook Sync Event
Browse files Browse the repository at this point in the history
  • Loading branch information
bymayo committed Sep 16, 2019
1 parent c5958ea commit 046931c
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Strava Sync Changelog

## 1.0.10 - 2019-09-16
### Added
- Ability to get data from Strava webhooks, and sync with custom plugins via new `webhookSync` event

## 1.0.9 - 2019-08-28
### Fixed
- Issue where webhook pulled in the userId, not the user element.
Expand Down
36 changes: 34 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ Strava Sync is a Craft CMS plugin that lets you connect Strava with Craft CMS. A

https://plugins.craftcms.com/strava-sync

- [Features](#features)
- [Requirements](#requirements)
- [Install](#install)
- [Configuration](#configuration)
- [Options](#options)
- [Webhooks](#webhooks)
- [Support](#support)

## Features

- Login via oAuth
Expand Down Expand Up @@ -177,15 +185,39 @@ Depending on your scope type when you authorised the account, the supported requ

## Webhooks

@TODO Documentation on this feature. Open issue if any questions.
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.

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.

Next, you need to create a _Webhook Subscription_ by doing a POST request to the Strava Sync webhook controller (`http://website.com/strava-sync/webhook/sync`) with a Bearer Token and `client_id`, `client_secret`, `verify_token`, `callback_url` parameters (The `callback_url` should be the same as the POST request URL)

This will then return a callback validation. If this is successful you will get back an `id` (It's worth making note of this to view/delete the subscription during your project developement)

Once the subscription has been created, you can now use the `webhookSync` event. So whenever an activity/athlete is created, edited or deleted on Strava you can get data back from it for your own plugin/module:

use bymayo\stravasync\events\WebhookSyncEvent;
use bymayo\stravasync\services\WebhookService;
use yii\base\Event;

Event::on(
WebhookService::class,
WebhookService::EVENT_WEBHOOK_SYNC,
function(WebhookSyncEvent $event) {
// Do something
}
);

The `$event` returns an `$event->athlete` and `$event->request` property.

The `$event->athlete` property contains the `userId`, `athleteId` and `accessToken` of the validated Strava user.
The `$event->request` property contains all `Event` data from the Strava Webhook e.g. `object_type` which is either athlete, or activity aswell as `aspect_type` which returns whether it's new, updated etc (See `Event Data` on https://developers.strava.com/docs/webhooks/)

## Support

If you have any issues (Surely not!) then I'll aim to reply to these as soon as possible. If it's a site-breaking-oh-no-what-has-happened moment, then hit me up on the Craft CMS Slack / Discord - @bymayo

## Roadmap

* Webhooks.
* Ability to set an admin user Strava credentials in the CP.
* Convertors (Distance to mi / km)

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.9",
"version": "1.0.10",
"keywords": [
"craft",
"cms",
Expand Down
19 changes: 1 addition & 18 deletions src/controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use bymayo\stravasync\StravaSync;
use bymayo\stravasync\services\OauthService;
use modules\eventsmodule\EventsModule as EventsModule;

use Craft;
use craft\web\Controller;
Expand Down Expand Up @@ -47,23 +46,7 @@ public function actionSync()
}
else {

$requestBody = Json::decode($request->getRawBody());

$user = StravaSync::getInstance()->userService->getUserFromAthleteId($requestBody['owner_id']);

if ($user){

if ($requestBody['aspect_type'] == 'create' && $requestBody['object_type'] == 'activity')
{
// Sync to Events Module
EventsModule::getInstance()->events->sync($user, $requestBody);
return true;

}

}

return false;
StravaSync::getInstance()->webhookService->sync($request);

}

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

use yii\base\Event;

class WebhookSyncEvent extends Event
{
// Properties
// =========================================================================

public $athlete;
public $request;

}
39 changes: 39 additions & 0 deletions src/services/WebhookService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,55 @@

use bymayo\stravasync\StravaSync;
use bymayo\stravasync\records\UsersRecord as UsersRecord;
use bymayo\stravasync\events\WebhookSyncEvent;

use Craft;
use craft\base\Component;

use yii\helpers\Json;

class WebhookService extends Component
{

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

const EVENT_WEBHOOK_SYNC = 'webhookSync';

// Public Methods
// =========================================================================

public function sync($request)
{

$requestAsJson = Json::decode($request->getRawBody());

$athlete = StravaSync::getInstance()->userService->getUserFromAthleteId($requestAsJson['owner_id']);

if ($athlete){

if ($this->hasEventHandlers(self::EVENT_WEBHOOK_SYNC)) {

$this->trigger(
self::EVENT_WEBHOOK_SYNC,
new WebhookSyncEvent(
[
'athlete' => $athlete,
'request' => $requestAsJson
]
)
);

}

return true;

}

return false;

}

public function splitString($string)
{
$explode = explode("&", $string);
Expand Down

0 comments on commit 046931c

Please sign in to comment.