Skip to content

Commit 9c0c7aa

Browse files
committed
feat: send webmentions via triggered hook
1 parent 6903b20 commit 9c0c7aa

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

docs/options.md

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Options
22

3-
43
## General settings
54

65
| Option | Default | Description |
@@ -14,6 +13,7 @@
1413
| Option | Default | Description |
1514
| ------------------------- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
1615
| `send.enabled` | `true` | Enable sending webmentions |
16+
| `send.hook.enabled` | `false` | Allow sending webmentions from Kirby hook triggered by other plugins |
1717
| `send.maxRetries` | `3` | How often should indieconnector try to send a mention if it failes |
1818
| `send.markDeleted` | `false` | When you delete a page, mark it as gone, so webmention targets can get informed about that - **Needs a database!** |
1919
| `send.skipSameHost` | `true` | Skip sending webmentions to yourself |
@@ -28,7 +28,6 @@
2828
| `blockedTemplates` | `[]` | **DEPRECATED** see `send.blockedTemplates` |
2929
| `send-mention-url-fields` | `['textfield:text','layouteditor:layout','blockeditor:block']` | **DEPRECATED** see `send.url-fields` |
3030

31-
3231
## Settings for receiving webmentions ([details](receiving.md))
3332

3433
| Option | Default | Description |
@@ -37,14 +36,13 @@
3736
| `receive.useHtmlContent` | `false` | Set to true if you want to show html content from the sender (not recommended) |
3837
| `receive.blockedSources` | `[]` | An array of source URLs to block, remove the path to block the entire host |
3938

40-
4139
## Settings for the queue ([details](receiving.md))
40+
4241
| Option | Default | Description |
4342
| --------------- | ------- | -------------------------------------------------------------- |
4443
| `queue.enabled` | `false` | Queue all incoming webmentions before processing them |
4544
| `queue.retries` | `5` | Retry `n` times to process the webmention if there is an error |
4645

47-
4846
## Settings for statistics in the panel
4947

5048
| Option | Default | Description |
@@ -59,18 +57,16 @@
5957
| `sqlitePath` | `'content/.sqlite/'` | Relative path to where the sqlite file should be stored (directory must exist) |
6058
| `disableMigrations` | `false` | Disable automatic migrations (may lead to errors) |
6159

62-
6360
## Posting to external services
6461

65-
| Option | Default | Description |
66-
| ----------------------- | ------------------ | -------------------------------------------------------------------- |
67-
| `post.prefereLanguage` | `-` | Use another language than your default language to use the text from |
68-
| `post.usePermalinkUrl` | `false` | Use the permalink url instead of the page url |
69-
| `post.textfields` | `['description']` | Text source fields for posting elsewhere |
70-
| `post.imagefield` | `''` | Image source field for posting elsewhere, must be one image |
71-
| `post.allowedTemplates` | `[]` | Set templates allowed to send webmentions |
72-
| `post.blockedTemplates` | `[]` | Block templates from sending webmentions |
73-
62+
| Option | Default | Description |
63+
| ----------------------- | ----------------- | -------------------------------------------------------------------- |
64+
| `post.prefereLanguage` | `-` | Use another language than your default language to use the text from |
65+
| `post.usePermalinkUrl` | `false` | Use the permalink url instead of the page url |
66+
| `post.textfields` | `['description']` | Text source fields for posting elsewhere |
67+
| `post.imagefield` | `''` | Image source field for posting elsewhere, must be one image |
68+
| `post.allowedTemplates` | `[]` | Set templates allowed to send webmentions |
69+
| `post.blockedTemplates` | `[]` | Block templates from sending webmentions |
7470

7571
### Mastodon ([details](mastodon.md))
7672

@@ -94,7 +90,6 @@
9490
| `bluesky.handle` | `‘‘` | Your user handle |
9591
| `bluesky.password` | `‘‘` | Your bluesky app password |
9692

97-
9893
## ActivityPub beta ([details](activitiypub.md))
9994

10095
| Option | Default | Description |

lib/WebmentionSender.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,38 @@ public function sendWebmentions($page)
8383
return $mergedUrls;
8484
}
8585

86+
public function sendWebmentionFromHook($page, $targetUrl, $sourceUrl)
87+
{
88+
// global config
89+
if (!$this->activeWebmentions) {
90+
return false;
91+
}
92+
93+
$processedUrls = [];
94+
95+
$sent = $this->send($targetUrl, $sourceUrl);
96+
97+
$status = $sent ? 'success' : 'error';
98+
$processedUrls[] = [
99+
'url' => $targetUrl,
100+
'date' => date('Y-m-d H:i:s'),
101+
'status' => $status,
102+
'retries' => 0,
103+
];
104+
105+
$mergedUrls = $this->mergeUrlsWithOutbox($processedUrls, $page);
106+
107+
$this->updateWebmentions($mergedUrls, $page);
108+
109+
if (option('mauricerenck.indieConnector.stats.enabled', false)) {
110+
$stats = new WebmentionStats();
111+
$stats->trackOutgoingWebmentions($mergedUrls, $page);
112+
}
113+
114+
return $mergedUrls;
115+
}
116+
117+
86118
public function send(string $targetUrl, string $sourceUrl)
87119
{
88120
$endpoint = $this->mentionClient->discoverWebmentionEndpoint($targetUrl);

plugin/hooks.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,11 @@
8282
);
8383
}
8484
},
85+
86+
'indieConnector.webmention.send' => function ($page, $targetUrl, $sourceUrl) {
87+
if (option('mauricerenck.indieConnector.send.hook.enabled', false)) {
88+
$webmentions = new WebmentionSender();
89+
$webmentions->sendWebmentionFromHook($page, $targetUrl, $sourceUrl);
90+
}
91+
},
8592
];

0 commit comments

Comments
 (0)