This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7eb749
commit a2d284e
Showing
6 changed files
with
265 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,65 @@ | ||
# tratschtante | ||
A Kirby Webmention Plugin your Plugins can subscribe to | ||
# Kirby Tratschtante | ||
|
||
## A Kirby Webmention Plugin your Plugins can subscribe to | ||
|
||
   | ||
|
||
This plugin currently works only with webmention.io. | ||
|
||
Add the Tratschtante Endpoint to your webmention.io Webhooks and enter the callback secret you set in your kirby config. | ||
|
||
## Installation | ||
|
||
- `composer require mauricerenck/tratschtante` | ||
- unzip [master.zip](https://github.com/mauricerenck/tratschtante/releases/latest) to `site/plugins/tratschtante` | ||
- `git submodule add https://github.com/mauricerenck/tratschtante.git site/plugins/tratschtante` | ||
|
||
## Config | ||
|
||
You have to set a callback secret in your config.php | ||
|
||
``` | ||
[ | ||
'mauricerenck.tratschtante.secret' => 'my-secret, | ||
] | ||
``` | ||
|
||
## Usage | ||
|
||
Whenever a webmention ins received, Tratschtante will trigger a Kirby-Hook your plugin can subscribe to: | ||
|
||
``` | ||
'hooks' => [ | ||
'tratschtante.webhook.received' => function ($webmention, $targetPage) { | ||
// $webmention: webmention data, see below | ||
// $targetPage: a kirby page object | ||
// YOUR CODE | ||
} | ||
], | ||
``` | ||
|
||
## Data | ||
|
||
Tratschtante will handle an array with some data to you: | ||
|
||
``` | ||
[ | ||
'type' => STRING // one of the webmention.io types, see https://webmention.io/settings/webhooks, | ||
'target' => 'target url', | ||
'source' => 'source url', | ||
'published' => 'publication date', | ||
'author' => [ | ||
'type' => 'card' or null, | ||
'name' => 'name' or null, | ||
'avatar' => 'avatar-url' or null, | ||
'url' => 'author url' or null, | ||
], | ||
'content' => 'comment text or empty string' | ||
] | ||
``` | ||
|
||
## Future Plans | ||
|
||
- Sending webmentions (already implemented elsewhere, just have to move it here) | ||
- Support for "native" webmentions, not only webmention.io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "mauricerenck/tratschtante", | ||
"version": "1.0.0", | ||
"description": "Kirby Webmentions your plugings can subscribe to", | ||
"type": "kirby-plugin", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Maurice Renck", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"files": [] | ||
}, | ||
"require": { | ||
"getkirby/composer-installer": "^1.1" | ||
}, | ||
"config": { | ||
"optimize-autoloader": true | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
return [ | ||
'secret' => null, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Plugin\Traschtante; | ||
|
||
use Kirby; | ||
|
||
@include_once __DIR__ . '/vendor/autoload.php'; | ||
|
||
load([ | ||
'Plugin\Traschtante\WebmentionReceiver' => 'utils/receiver.php' | ||
], __DIR__); | ||
|
||
Kirby::plugin('mauricerenck/tratschtante', [ | ||
'options' => require_once(__DIR__ . '/config/options.php'), | ||
'routes' => [ | ||
[ | ||
'pattern' => 'tratschtante/webhook/webmentionio', | ||
'method' => 'POST', | ||
'action' => function () { | ||
$response = json_decode(file_get_contents('php://input')); | ||
$receiver = new WebmentionReceiver(); | ||
$webmention = []; | ||
|
||
if ($response->secret !== option('mauricerenck.tratschtante.secret')) { | ||
return 'PAGE NOT FOUND'; | ||
} | ||
|
||
$targetPage = $receiver->getPageFromUrl($response->post->{'wm-target'}); | ||
if (is_null($targetPage)) { | ||
return 'PAGE NOT FOUND'; | ||
} | ||
|
||
$webmention['type'] = $receiver->getWebmentionType($response->post->{'wm-property'}); | ||
$webmention['target'] = $response->post->{'wm-target'}; | ||
$webmention['source'] = $response->post->{'wm-source'}; | ||
$webmention['published'] = $response->post->published; | ||
$webmention['author'] = $receiver->getAuthor($response); | ||
$webmention['content'] = (isset($response->post->content) && isset($response->post->content->text)) ? $response->post->content->text : ''; | ||
|
||
kirby()->trigger('tratschtante.webhook.received', $webmention, $targetPage); | ||
|
||
return 'THANKS'; | ||
} | ||
], | ||
] | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Plugin\Traschtante; | ||
|
||
use Kirby\Http\Url; | ||
use Kirby\Toolkit\V; | ||
use Kirby\Toolkit\Str; | ||
use json_decode; | ||
use json_encode; | ||
use is_null; | ||
use preg_split; | ||
use str_replace; | ||
use date; | ||
|
||
class WebmentionReceiver | ||
{ | ||
public function getPageFromUrl(string $url) | ||
{ | ||
if (V::url($url)) { | ||
$path = Url::path($url); | ||
$languages = kirby()->languages(); | ||
|
||
if ($languages->count() > 0) { | ||
foreach ($languages as $language) { | ||
$path = str_replace($language . '/', '', $path); | ||
} | ||
} | ||
|
||
$targetPage = page($path); | ||
|
||
if (is_null($targetPage)) { | ||
return null; | ||
} | ||
|
||
return $targetPage; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function getWebmentionType(string $wmProperty) | ||
{ | ||
/* | ||
in-reply-to | ||
like-of | ||
repost-of | ||
bookmark-of | ||
mention-of | ||
rsvp | ||
*/ | ||
switch ($wmProperty) { | ||
case 'like-of': return 'LIKE'; | ||
case 'in-reply-to': return 'REPLY'; | ||
case 'repost-of': return 'REPOST'; // retweet z.b. | ||
default: return 'REPLY'; | ||
} | ||
} | ||
|
||
public function getAuthor($webmention) | ||
{ | ||
$authorInfo = $webmention->post->author; | ||
|
||
return [ | ||
'type' => (isset($authorInfo->type)) ? $authorInfo->type : null, | ||
'name' => (isset($authorInfo->name)) ? $authorInfo->name : null, | ||
'avatar' => (isset($authorInfo->photo)) ? $authorInfo->photo : null, | ||
'url' => (isset($authorInfo->url)) ? $authorInfo->url : null, | ||
]; | ||
} | ||
} |