Skip to content

Commit

Permalink
feat: define multiple source fields for external posting
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricerenck committed Aug 17, 2024
1 parent 3e73eb9 commit 0f9f04c
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 95 deletions.
4 changes: 2 additions & 2 deletions docs/mastodon.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ You can enable automatically posting to Mastodon and/or Bluesky when a new page

## General setup

In order to post to Mastodon or Bluesky, you have to setup some fields in your `config.php`. The **textfield** is the field that will be used as the text for the post. And if you want to also post an image, you have to define an **imagefield**. Use the same name as in your blueprint:
In order to post to Mastodon or Bluesky, you have to setup some fields in your `config.php`. The **textfields** is a list of fields that will be used as the text for the post, the first one found and not empty will be used. And if you want to also post an image, you have to define an **imagefield**. Use the same name as in your blueprint:

```php
'mauricerenck.indieConnector.post.textfield' => 'description',
'mauricerenck.indieConnector.post.textfields' => ['description'],
'mauricerenck.indieConnector.post.imagefield' => 'postImage',
```

Expand Down
12 changes: 6 additions & 6 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@

## Posting to external services

| Option | Default | Description |
| ----------------------- | --------------- | ----------------------------------------------------------- |
| `post.textfield` | `description` | Text source field for posting elsewhere |
| `post.imagefield` | `''` | Image source field for posting elsewhere, must be one image |
| `post.allowedTemplates` | `[]` | Set templates allowed to send webmentions |
| `post.blockedTemplates` | `[]` | Block templates from sending webmentions |
| Option | Default | Description |
| ----------------------- | ------------------ | ----------------------------------------------------------- |
| `post.textfields` | `['description']` | Text source fields for posting elsewhere |
| `post.imagefield` | `''` | Image source field for posting elsewhere, must be one image |
| `post.allowedTemplates` | `[]` | Set templates allowed to send webmentions |
| `post.blockedTemplates` | `[]` | Block templates from sending webmentions |


### Mastodon ([details](mastodon.md))
Expand Down
41 changes: 5 additions & 36 deletions lib/BlueskySender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,27 @@

namespace mauricerenck\IndieConnector;

use Kirby\Toolkit\Str;
use Exception;
use Kirby\Filesystem\F;
use cjrasmussen\BlueskyApi\BlueskyApi;

class BlueskySender extends Sender
class BlueskySender extends ExternalPostSender
{
private $maxPostLength = 300;

public function __construct(
private ?string $textfield = null,
private ?string $imagefield = null,
private ?string $handle = null,
private ?string $password = null,
private ?bool $enabled = null,
private ?UrlChecks $urlChecks = null,
private ?PageChecks $pageChecks = null
) {
parent::__construct();

$this->textfield = $textfield ?? option('mauricerenck.indieConnector.post.textfield', 'description');
$this->imagefield = $imagefield ?? option('mauricerenck.indieConnector.post.imagefield', false);

$this->enabled = $enabled ?? option('mauricerenck.indieConnector.bluesky.enabled', false);
$this->handle = $password ?? option('mauricerenck.indieConnector.bluesky.handle', false);
$this->password = $password ?? option('mauricerenck.indieConnector.bluesky.password', false);
$this->enabled = $enabled ?? option('mauricerenck.indieConnector.bluesky.enabled', false);

$this->urlChecks = $urlChecks ?? new UrlChecks();
$this->pageChecks = $pageChecks ?? new PageChecks();

// backwards compatibility
if (!$textfield && option('mauricerenck.indieConnector.mastodon-text-field', false)) {
$this->textfield = option('mauricerenck.indieConnector.mastodon-text-field');
}
}

public function sendPost($page)
{

if (!$this->enabled) {
return false;
}
Expand Down Expand Up @@ -69,12 +52,9 @@ public function sendPost($page)
try {
$pageUrl = $page->url();
$trimTextPosition = $this->calculatePostTextLength($page->url());
$textfield = $this->textfield;
$language = 'en';

$message = $page->$textfield()->isNotEmpty()
? $page->$textfield()->value()
: Str::short($page->title(), $trimTextPosition);
$message = $this->getTextFieldContent($page, $trimTextPosition);
$message .= "\n" . $pageUrl;

if ($defaultLanguage = kirby()->defaultLanguage()) {
Expand Down Expand Up @@ -121,7 +101,7 @@ public function sendPost($page)

$response = $bluesky->request('POST', 'com.atproto.repo.createRecord', $args);

$this->updatePosts($response->uri, 200, $page);
$this->updatePosts($response->uri, 200, $page, 'bluesky');
return true;
} catch (Exception $e) {
throw new Exception($e->getMessage());
Expand Down Expand Up @@ -183,15 +163,4 @@ public function getMediaAttachment($page)
return false;
}
}

public function calculatePostTextLength(string $url)
{
$urlLength = Str::length($url);
return $this->maxPostLength - $urlLength - 2;
}

public function updatePosts($url, $statusCode, $page)
{
return $this->updateExternalPosts($url, $statusCode, 'bluesky', $page);
}
}
67 changes: 67 additions & 0 deletions lib/ExternalPostSender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace mauricerenck\IndieConnector;

use Kirby\Toolkit\Str;

class ExternalPostSender extends Sender
{
public function __construct(
public ?array $textfields = null,
public ?string $imagefield = null,
private ?int $maxPostLength = null,
public ?UrlChecks $urlChecks = null,
public ?PageChecks $pageChecks = null
) {
parent::__construct();

$this->textfields = $textfields ?? option('mauricerenck.indieConnector.post.textfields', ['description']);
$this->imagefield = $imagefield ?? option('mauricerenck.indieConnector.post.imagefield', false);
$this->maxPostLength = $maxPostLength ?? 300;

$this->urlChecks = $urlChecks ?? new UrlChecks();
$this->pageChecks = $pageChecks ?? new PageChecks();

// backwards compatibility
$singleTextfield = option('mauricerenck.indieConnector.post.textfield', false);
if (!$textfields && $singleTextfield) {
$this->textfields = [$singleTextfield];
}

if (!$textfields && $singleTextfield) {
$this->textfields = [$singleTextfield];
}

if (!$maxPostLength && option('mauricerenck.indieConnector.mastodon-text-length', false)) {
$this->maxPostLength = option('mauricerenck.indieConnector.mastodon-text-length');
}
}

public function getTextFieldContent($page, $trimTextPosition) {
if(is_array($this->textfields)) {
foreach($this->textfields as $field) {
if($page->$field()->exists() && $page->$field()->isNotEmpty()) {
return $page->$field()->value();
}
}
}

$field = $this->textfields;
if(!is_array($this->textfields) && $page->$field()->isNotEmpty()) {
return $page->$field()->value();
}

return Str::short($page->title(), $trimTextPosition);
}

public function calculatePostTextLength(string $url)
{
$urlLength = Str::length($url);
return $this->maxPostLength - $urlLength - 2;
}

public function updatePosts($url, $statusCode, $page, $target)
{
return $this->updateExternalPosts($url, $statusCode, $target, $page);
}
}
40 changes: 3 additions & 37 deletions lib/MastodonSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,24 @@

namespace mauricerenck\IndieConnector;

use Kirby\Toolkit\Str;
use Exception;
use Kirby\Http\Remote;
use Kirby\Filesystem\F;

class MastodonSender extends Sender
class MastodonSender extends ExternalPostSender
{
public function __construct(
private ?int $tootMaxLength = null,
private ?string $textfield = null,
private ?string $imagefield = null,
private ?string $instanceUrl = null,
private ?string $token = null,
private ?bool $enabled = null,
private ?UrlChecks $urlChecks = null,
private ?PageChecks $pageChecks = null
) {
parent::__construct();

$this->textfield = $textfield ?? option('mauricerenck.indieConnector.post.textfield', 'description');
$this->imagefield = $imagefield ?? option('mauricerenck.indieConnector.post.imagefield', false);

$this->enabled = $enabled ?? option('mauricerenck.indieConnector.mastodon.enabled', false);
$this->instanceUrl = $instanceUrl ?? option('mauricerenck.indieConnector.mastodon.instance-url', false);
$this->token = $token ?? option('mauricerenck.indieConnector.mastodon.bearer', false);
$this->tootMaxLength = $tootMaxLength ?? option('mauricerenck.indieConnector.mastodon.text-length', 500);

$this->urlChecks = $urlChecks ?? new UrlChecks();
$this->pageChecks = $pageChecks ?? new PageChecks();

// backwards compatibility
if (!$textfield && option('mauricerenck.indieConnector.mastodon-text-field', false)) {
$this->textfield = option('mauricerenck.indieConnector.mastodon-text-field');
}

if (!$tootMaxLength && option('mauricerenck.indieConnector.mastodon-text-length', false)) {
$this->tootMaxLength = option('mauricerenck.indieConnector.mastodon-text-length');
}

if (!$instanceUrl && option('mauricerenck.indieConnector.mastodon-instance-url', false)) {
$this->instanceUrl = option('mauricerenck.indieConnector.mastodon-instance-url');
}
Expand Down Expand Up @@ -90,11 +69,8 @@ public function sendPost($page)
try {
$pageUrl = $page->url();
$trimTextPosition = $this->calculatePostTextLength($page->url());
$textfield = $this->textfield;

$message = $page->$textfield()->isNotEmpty()
? $page->$textfield()->value()
: Str::short($page->title(), $trimTextPosition);
$message = $this->getTextFieldContent($page, $trimTextPosition);
$message .= "\n" . $pageUrl;

$headers = ['Authorization: Bearer ' . $this->token, 'Content-Type: application/json'];
Expand All @@ -121,7 +97,7 @@ public function sendPost($page)
$result = $response->json();

$url = $result['url'] ?? null;
$this->updatePosts($url, $response->code(), $page);
$this->updatePosts($url, $response->code(), $page, 'mastodon');

return true;
} catch (Exception $e) {
Expand Down Expand Up @@ -191,14 +167,4 @@ public function uploadImage($page)
}
}

public function calculatePostTextLength(string $url)
{
$urlLength = Str::length($url);
return $this->tootMaxLength - $urlLength - 2;
}

public function updatePosts($url, $statusCode, $page)
{
return $this->updateExternalPosts($url, $statusCode, 'mastodon', $page);
}
}
2 changes: 1 addition & 1 deletion site/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
'mauricerenck.indieConnector.sqlitePath' => '.sqlite/',

'mauricerenck.indieConnector.post' => [
'textfield' => 'description',
'textfields' => ['description'],
'imagefield' => 'mastodonimage',
'allowedTemplates' => ['phpunit', 'default'],
'blockedTemplates' => ['blocked-template'],
Expand Down
18 changes: 9 additions & 9 deletions tests/lib/MastodonSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function testShouldSendPost()
$this->urlCheckMock->shouldReceive('isLocalUrl')->andReturn(false);

$sender = new MastodonSender(
500,
'description',
null,
'https://example.com',
'1234567890',
true,
500,
['description'],
null,
$this->urlCheckMock
);

Expand All @@ -51,12 +51,12 @@ public function testShouldHandleDisabled()
$this->urlCheckMock->shouldReceive('isLocalUrl')->andReturn(false);

$sender = new MastodonSender(
500,
'description',
null,
'https://example.com',
'1234567890',
true,
500,
['description'],
null,
$this->urlCheckMock
);

Expand All @@ -78,12 +78,12 @@ public function testShouldHandleDraft()
$this->urlCheckMock->shouldReceive('isLocalUrl')->andReturn(false);

$sender = new MastodonSender(
500,
'description',
null,
'https://example.com',
'1234567890',
true,
500,
['description'],
null,
$this->urlCheckMock
);

Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'Kirby\\ComposerInstaller\\PluginInstaller' => $vendorDir . '/getkirby/composer-installer/src/ComposerInstaller/PluginInstaller.php',
'cjrasmussen\\BlueskyApi\\BlueskyApi' => $vendorDir . '/cjrasmussen/bluesky-api/src/BlueskyApi.php',
'mauricerenck\\IndieConnector\\BlueskySender' => $baseDir . '/lib/BlueskySender.php',
'mauricerenck\\IndieConnector\\ExternalPostSender' => $baseDir . '/lib/ExternalPostSender.php',
'mauricerenck\\IndieConnector\\IndieConnectorDatabase' => $baseDir . '/lib/Database.php',
'mauricerenck\\IndieConnector\\MastodonSender' => $baseDir . '/lib/MastodonSender.php',
'mauricerenck\\IndieConnector\\Microformats' => $baseDir . '/lib/Microformats.php',
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class ComposerStaticInit84944e4200b9284446d234824c8e9dcc
'Kirby\\ComposerInstaller\\PluginInstaller' => __DIR__ . '/..' . '/getkirby/composer-installer/src/ComposerInstaller/PluginInstaller.php',
'cjrasmussen\\BlueskyApi\\BlueskyApi' => __DIR__ . '/..' . '/cjrasmussen/bluesky-api/src/BlueskyApi.php',
'mauricerenck\\IndieConnector\\BlueskySender' => __DIR__ . '/../..' . '/lib/BlueskySender.php',
'mauricerenck\\IndieConnector\\ExternalPostSender' => __DIR__ . '/../..' . '/lib/ExternalPostSender.php',
'mauricerenck\\IndieConnector\\IndieConnectorDatabase' => __DIR__ . '/../..' . '/lib/Database.php',
'mauricerenck\\IndieConnector\\MastodonSender' => __DIR__ . '/../..' . '/lib/MastodonSender.php',
'mauricerenck\\IndieConnector\\Microformats' => __DIR__ . '/../..' . '/lib/Microformats.php',
Expand Down
8 changes: 4 additions & 4 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php return array(
'root' => array(
'name' => 'mauricerenck/indieconnector',
'pretty_version' => '2.0.0',
'version' => '2.0.0.0',
'pretty_version' => '2.0.1',
'version' => '2.0.1.0',
'reference' => NULL,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
Expand Down Expand Up @@ -38,8 +38,8 @@
'dev_requirement' => false,
),
'mauricerenck/indieconnector' => array(
'pretty_version' => '2.0.0',
'version' => '2.0.0.0',
'pretty_version' => '2.0.1',
'version' => '2.0.1.0',
'reference' => NULL,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
Expand Down

0 comments on commit 0f9f04c

Please sign in to comment.