Skip to content
This repository has been archived by the owner on Nov 17, 2021. It is now read-only.

Commit

Permalink
add: initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricerenck committed Apr 8, 2020
1 parent b7eb749 commit a2d284e
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 2 deletions.
67 changes: 65 additions & 2 deletions README.md
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

![GitHub release](https://img.shields.io/github/release/mauricerenck/tratschtante.svg?maxAge=1800) ![License](https://img.shields.io/github/license/mashape/apistatus.svg) ![Kirby Version](https://img.shields.io/badge/Kirby-3%2B-black.svg)

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
22 changes: 22 additions & 0 deletions composer.json
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
}
}
57 changes: 57 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions config/options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'secret' => null,
];
46 changes: 46 additions & 0 deletions index.php
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';
}
],
]
]);
70 changes: 70 additions & 0 deletions utils/receiver.php
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,
];
}
}

0 comments on commit a2d284e

Please sign in to comment.