-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshaarli2bluesky.php
165 lines (137 loc) · 5.14 KB
/
shaarli2bluesky.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* shaarli2bluesky
*
* Automatically publishes your new Shaarli links to Bluesky.
* Get Shaarli at https://github.com/shaarli/shaarli
*
* See README.md for instructions.
*
* @author kalvn <https://mastodon.xyz/@kalvn>
*/
use Shaarli\Config\ConfigManager;
use Shaarli\Plugin\PluginManager;
use Shaarli\Render\TemplatePage;
require __DIR__ . '/vendor/autoload.php';
/**
* The default message format if none is specified.
*/
const SHAARLI2BLUESKY_MESSAGE_DEFAULT_FORMAT = '#Shaarli : ${title} ${url} ${tags}';
const SHAARLI2BLUESKY_POST_PARAM_MESSAGE = 'shaarli2bluesky-message';
const SHAARLI2BLUESKY_POST_PARAM_MESSAGE_FORMAT = 'shaarli2bluesky-format';
const SHAARLI2BLUESKY_MESSAGE_MAX_LENGTH = 300;
const SHAARLI2BLUESKY_DIRECTORY_PATH = __DIR__;
/**
* Init function: check settings, and set default format.
*
* @param ConfigManager $conf instance.
*
* @return array|void Error if config is not valid.
*/
function shaarli2bluesky_init ($conf) {
$format = $conf->get('plugins.BLUESKY_MESSAGE_FORMAT');
if (empty($format)) {
$conf->set('plugins.BLUESKY_MESSAGE_FORMAT', SHAARLI2BLUESKY_MESSAGE_DEFAULT_FORMAT);
}
if (!BlueskyUtils::isConfigValid($conf)) {
return ['Please set up your Bluesky parameters in plugin administration page.'];
}
}
/**
* Inject CSS when editing a link.
*
* @param array $data New link values.
*
* @return array $data with the CSS file.
*/
function hook_shaarli2bluesky_render_includes ($data) {
if (in_array($data['_PAGE_'], [TemplatePage::EDIT_LINK, TemplatePage::EDIT_LINK_BATCH])) {
$data['css_files'][] = PluginManager::$PLUGINS_PATH . '/shaarli2bluesky/shaarli2bluesky.css';
}
return $data;
}
/**
* Inject CSS when editing a link.
*
* @param array $data New link values.
* @param ConfigManager $conf instance.
*
* @return array $data with the JS file.
*/
function hook_shaarli2bluesky_render_footer ($data, $conf) {
if (in_array($data['_PAGE_'], [TemplatePage::EDIT_LINK, TemplatePage::EDIT_LINK_BATCH])) {
$data['js_files'][] = PluginManager::$PLUGINS_PATH . '/shaarli2bluesky/shaarli2bluesky.js';
}
return $data;
}
/**
* Hook save link: will automatically publish a message when a new public link is created.
*
* @param array $data New link values.
* @param ConfigManager $conf instance.
*
* @return array $data not altered.
*/
function hook_shaarli2bluesky_save_link ($data, $conf) {
// No message without config, for private links, or on edit.
if (!BlueskyUtils::isConfigValid($conf)
|| $data['private']
|| !isset($_POST[SHAARLI2BLUESKY_POST_PARAM_MESSAGE])
) {
return $data;
}
// We make sure not to alter data
$link = array_merge([], $data);
$tagsSeparator = $conf->get('general.tags_separator', ' ');
$blueskyUsername = $conf->get('plugins.BLUESKY_USERNAME');
$blueskyPassword = $conf->get('plugins.BLUESKY_PASSWORD');
$blueskyMessageFormat = isset($_POST[SHAARLI2BLUESKY_POST_PARAM_MESSAGE_FORMAT]) ? $_POST[SHAARLI2BLUESKY_POST_PARAM_MESSAGE_FORMAT] : $conf->get('plugins.BLUESKY_MESSAGE_FORMAT', SHAARLI2BLUESKY_MESSAGE_DEFAULT_FORMAT);
$blueskyReplaceUrlByPermalinkWhenTruncating = $conf->get('plugins.BLUESKY_REPLACE_URL_BY_PERMALINK_WHEN_TRUNCATING', 'false') === 'true';
$data['permalink'] = index_url($_SERVER) . 'shaare/' . $data['shorturl'];
// If the link is a note, we use the permalink as the url.
if(BlueskyUtils::isLinkNote($data)){
$data['url'] = $data['permalink'];
}
$message = new BlueskyMessage($data, $blueskyMessageFormat, $tagsSeparator, SHAARLI2BLUESKY_MESSAGE_MAX_LENGTH, $blueskyReplaceUrlByPermalinkWhenTruncating);
$client = new BlueskyClient($blueskyUsername, $blueskyPassword);
try {
$client->postMessage($message->generateText());
} catch (Throwable $e) {
BlueskyUtils::log('error', 'Something went wrong when publishing the link on Bluesky: [' . $e->getMessage() . '].');
}
BlueskyUtils::log('success', 'One more post for your Bluesky account!');
return $link;
}
/**
* Hook render_editlink: add a checkbox to publish the new link version or not.
*
* @param array $data New link values.
* @param ConfigManager $conf instance.
*
* @return array $data with `edit_link_plugin` placeholder filled.
*/
function hook_shaarli2bluesky_render_editlink ($data, $conf) {
if (!BlueskyUtils::isConfigValid($conf)) {
return $data;
}
$private = $conf->get('privacy.default_private_links', false);
$checked = $data['link_is_new'] && !$private;
$html = file_get_contents(SHAARLI2BLUESKY_DIRECTORY_PATH . '/edit_link.html');
$html = str_replace([
'##checked##',
'##shaarli2bluesky-format##',
'##id##',
'##max-length##',
'##tags-separator##',
'##is-note##',
], [
$checked ? 'checked="checked"' : '',
$conf->get('plugins.BLUESKY_MESSAGE_FORMAT', SHAARLI2BLUESKY_MESSAGE_DEFAULT_FORMAT),
uniqid(),
SHAARLI2BLUESKY_MESSAGE_MAX_LENGTH,
$conf->get('general.tags_separator', ' '),
BlueskyUtils::isLinkNote($data['link']) ? 'true' : 'false',
], $html);
$data['edit_link_plugin'][] = $html;
return $data;
}