Skip to content

Commit afedda2

Browse files
committed
Adds a new Panel View Button for clearing the Mastodon cache manually.
1 parent 0e290aa commit afedda2

File tree

6 files changed

+113
-3
lines changed

6 files changed

+113
-3
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ Example Config:
7676
```
7777

7878

79+
## Panel View Button
80+
81+
The plugin includes a custom [Panel View Button](https://getkirby.com/releases/5/view-buttons) for Kirby 5.x to manually clear the Mastodon cache file which can be added to the site or a page blueprint using the `buttons` [option](https://getkirby.com/docs/reference/panel/blueprints/page#view-buttons).
82+
83+
84+
```yml
85+
buttons:
86+
mastodoncache: true
87+
```
88+
89+
7990
## Compatibility
8091
8192
* Kirby 4.x

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(function(){"use strict";panel.plugin("scottboms/mastodon-feed",{viewButtons:{mastodoncache:{template:`
2+
<k-button
3+
icon="mastodon"
4+
variant="filled"
5+
theme="purple-icon"
6+
size="sm"
7+
@click="clearCache"
8+
>Clear Cache
9+
</k-button>`,data(){return{loading:!1}},methods:{async clearCache(){this.loading=!0;try{const e=await this.$api.post("mastodon/clear-cache");e.status==="ok"?this.$panel.notification.success({message:"Mastodon cache cleared",icon:"check",timeout:5e3}):e.status==="noop"&&this.$panel.notification.info({message:"No cache to clear",icon:"alert",theme:"notice",timeout:5e3})}catch(e){this.$panel.notification.error({message:e.message||"Request failed",timeout:5e3})}finally{this.loading=!1}}}}}})})();

index.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
info: [
3333
'homepage' => 'https://github.com/scottboms/kirby-mastodon-feed'
3434
],
35-
version: '1.0.1',
35+
version: '1.0.2',
3636
license: 'MIT',
3737
extends: [
3838
'options' => [
@@ -45,6 +45,9 @@
4545
'excludereplies' => true,
4646
'onlymedia' => false
4747
],
48+
'api' => [
49+
'routes' => require __DIR__ . '/lib/routes.php'
50+
],
4851

4952
'snippets' => [
5053
'mastodon_feed' => __DIR__ . '/snippets/mastodon_feed.php'

lib/routes.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Scottboms\Mastodon\Feed;
4+
5+
return [
6+
[
7+
'pattern' => 'mastodon/clear-cache',
8+
'method' => 'POST',
9+
'auth' => true, // require a panel-authenticated user
10+
'action' => function () {
11+
// restrict to admins
12+
$user = kirby()->user();
13+
if (!$user || !$user->isAdmin()) {
14+
throw new PermissionException('Not allowed.');
15+
}
16+
17+
try {
18+
$ok = Feed::clearFeedCache();
19+
return [
20+
'status' => $ok ? 'ok' : 'noop',
21+
'message' => $ok ? 'Feed cache cleared.' : 'Nothing to clear.'
22+
];
23+
} catch (Throwable $e) {
24+
// return a clean error to the panel
25+
return [
26+
'status' => 'error',
27+
'message' => 'Cache clear failed: ' . $e->getMessage()
28+
];
29+
}
30+
}
31+
]
32+
];

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
"name": "mastodon-feed",
33
"description": "Mastodon Feed plugin for Kirby",
44
"author": "Scott Boms <[email protected]>",
5-
"version": "1.0.1",
5+
"version": "1.0.2",
66
"type": "kirby-plugin",
7-
"license": "MIT"
7+
"license": "MIT",
8+
"scripts": {
9+
"dev": "kirbyup serve src/index.js",
10+
"build": "kirbyup src/index.js"
11+
},
12+
"devDependencies": {
13+
"kirbyup": "^3.1.5"
14+
}
815
}

src/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
panel.plugin("scottboms/mastodon-feed", {
2+
viewButtons: {
3+
mastodoncache: {
4+
template: `
5+
<k-button
6+
icon="mastodon"
7+
variant="filled"
8+
theme="purple-icon"
9+
size="sm"
10+
@click="clearCache"
11+
>Clear Cache
12+
</k-button>`,
13+
data() {
14+
return { loading: false };
15+
},
16+
methods: {
17+
async clearCache() {
18+
// console.log('log','Mastodon Cache button click registered');
19+
this.loading = true;
20+
try {
21+
const res = await this.$api.post('mastodon/clear-cache');
22+
if (res.status === 'ok') {
23+
this.$panel.notification.success({
24+
message: "Mastodon cache cleared",
25+
icon: "check",
26+
timeout: 5000
27+
});
28+
} else if (res.status === 'noop') {
29+
this.$panel.notification.info({
30+
message: "No cache to clear",
31+
icon: "alert",
32+
theme: "notice",
33+
timeout: 5000
34+
});
35+
}
36+
} catch (e) {
37+
this.$panel.notification.error({
38+
message: e.message || "Request failed",
39+
timeout: 5000
40+
});
41+
} finally {
42+
this.loading = false;
43+
}
44+
}
45+
},
46+
},
47+
},
48+
});

0 commit comments

Comments
 (0)