-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathatmosphere.module
More file actions
268 lines (228 loc) · 6.99 KB
/
atmosphere.module
File metadata and controls
268 lines (228 loc) · 6.99 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/**
* @file
* ATmosphere module: Publish Drupal content to AT Protocol.
*/
declare(strict_types=1);
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
/**
* Implements hook_entity_base_field_info().
*
* Adds hidden base fields to node entities for AT Protocol metadata.
*/
function atmosphere_entity_base_field_info(EntityTypeInterface $entity_type): array {
$fields = [];
if ($entity_type->id() === 'node') {
$field_names = [
'atmosphere_bsky_tid' => 'Bluesky TID',
'atmosphere_bsky_uri' => 'Bluesky AT-URI',
'atmosphere_bsky_cid' => 'Bluesky CID',
'atmosphere_doc_tid' => 'Document TID',
'atmosphere_doc_uri' => 'Document AT-URI',
'atmosphere_doc_cid' => 'Document CID',
];
foreach ($field_names as $field_name => $label) {
$fields[$field_name] = BaseFieldDefinition::create('string')
->setLabel(t('ATmosphere: @label', ['@label' => $label]))
->setDescription(t('AT Protocol metadata managed by the ATmosphere module.'))
->setRevisionable(false)
->setTranslatable(false)
->setDisplayConfigurable('form', false)
->setDisplayConfigurable('view', false);
}
}
return $fields;
}
/**
* Implements hook_entity_insert().
*/
function atmosphere_entity_insert(EntityInterface $entity): void {
if (!$entity instanceof NodeInterface) {
return;
}
if (\Drupal\atmosphere\Service\Publisher::$isSaving) {
return;
}
$config = \Drupal::config('atmosphere.settings');
$connection_manager = \Drupal::service('atmosphere.connection_manager');
if (!$connection_manager->isConnected()) {
return;
}
if (!$config->get('auto_publish')) {
return;
}
$syncable_types = $config->get('syncable_node_types') ?? [];
if (!in_array($entity->bundle(), $syncable_types, true)) {
return;
}
if (!$entity->isPublished()) {
return;
}
\Drupal::queue('atmosphere_publish')->createItem([
'nid' => $entity->id(),
]);
}
/**
* Implements hook_entity_update().
*/
function atmosphere_entity_update(EntityInterface $entity): void {
if (!$entity instanceof NodeInterface) {
return;
}
if (\Drupal\atmosphere\Service\Publisher::$isSaving) {
return;
}
$config = \Drupal::config('atmosphere.settings');
$connection_manager = \Drupal::service('atmosphere.connection_manager');
if (!$connection_manager->isConnected()) {
return;
}
$syncable_types = $config->get('syncable_node_types') ?? [];
if (!in_array($entity->bundle(), $syncable_types, true)) {
return;
}
$was_published = isset($entity->original) && $entity->original->isPublished();
$is_published = $entity->isPublished();
if ($is_published && !$was_published) {
// Newly published.
\Drupal::queue('atmosphere_publish')->createItem([
'nid' => $entity->id(),
]);
}
elseif ($is_published && $was_published) {
// Updated while published.
\Drupal::queue('atmosphere_update')->createItem([
'nid' => $entity->id(),
]);
}
elseif (!$is_published && $was_published) {
// Unpublished — delete from AT Protocol.
$bsky_tid = $entity->get('atmosphere_bsky_tid')->value;
$doc_tid = $entity->get('atmosphere_doc_tid')->value;
if ($bsky_tid || $doc_tid) {
\Drupal::queue('atmosphere_delete')->createItem([
'nid' => $entity->id(),
'bsky_tid' => $bsky_tid,
'doc_tid' => $doc_tid,
]);
}
}
}
/**
* Implements hook_entity_predelete().
*/
function atmosphere_entity_predelete(EntityInterface $entity): void {
if (!$entity instanceof NodeInterface) {
return;
}
$connection_manager = \Drupal::service('atmosphere.connection_manager');
if (!$connection_manager->isConnected()) {
return;
}
$bsky_tid = $entity->get('atmosphere_bsky_tid')->value;
$doc_tid = $entity->get('atmosphere_doc_tid')->value;
if ($bsky_tid || $doc_tid) {
\Drupal::queue('atmosphere_delete')->createItem([
'bsky_tid' => $bsky_tid,
'doc_tid' => $doc_tid,
]);
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for node forms.
*/
function atmosphere_form_node_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
$node = $form_state->getFormObject()->getEntity();
$config = \Drupal::config('atmosphere.settings');
$syncable_types = $config->get('syncable_node_types') ?? [];
if (!in_array($node->bundle(), $syncable_types, true)) {
return;
}
$connection_manager = \Drupal::service('atmosphere.connection_manager');
if (!$connection_manager->isConnected()) {
return;
}
$doc_uri = $node->get('atmosphere_doc_uri')->value ?? '';
$bsky_uri = $node->get('atmosphere_bsky_uri')->value ?? '';
$form['atmosphere'] = [
'#type' => 'details',
'#title' => t('ATmosphere'),
'#group' => 'advanced',
'#weight' => 100,
'#open' => false,
];
if ($doc_uri) {
$form['atmosphere']['status'] = [
'#markup' => '<p>' . t('Published to AT Protocol.') . '</p>',
];
$form['atmosphere']['doc_uri'] = [
'#markup' => '<p><strong>' . t('Document:') . '</strong> <code>' . $doc_uri . '</code></p>',
];
if ($bsky_uri) {
$form['atmosphere']['bsky_uri'] = [
'#markup' => '<p><strong>' . t('Bluesky:') . '</strong> <code>' . $bsky_uri . '</code></p>',
];
}
}
else {
$auto = $config->get('auto_publish') ? t('Yes') : t('No');
$form['atmosphere']['status'] = [
'#markup' => '<p>' . t('Not yet published to AT Protocol.') . '</p>'
. '<p>' . t('Auto-publish enabled: @auto', ['@auto' => $auto]) . '</p>',
];
}
}
/**
* Implements hook_page_attachments().
*/
function atmosphere_page_attachments(array &$attachments): void {
$route_match = \Drupal::routeMatch();
if ($route_match->getRouteName() !== 'entity.node.canonical') {
return;
}
$node = $route_match->getParameter('node');
if (!$node instanceof NodeInterface) {
return;
}
$doc_uri = $node->get('atmosphere_doc_uri')->value ?? '';
if (empty($doc_uri)) {
return;
}
$attachments['#attached']['html_head'][] = [
[
'#tag' => 'link',
'#attributes' => [
'rel' => 'site.standard.document',
'href' => $doc_uri,
],
],
'atmosphere_document_link',
];
}
/**
* Implements hook_cron().
*
* Proactively refreshes the AT Protocol access token if expiring soon.
*/
function atmosphere_cron(): void {
$connection_manager = \Drupal::service('atmosphere.connection_manager');
if (!$connection_manager->isConnected()) {
return;
}
// Refresh if expiring within 6 hours.
$expires_at = $connection_manager->getExpiresAt();
if ($expires_at > 0 && $expires_at < (\Drupal::time()->getRequestTime() + 21600)) {
try {
\Drupal::service('atmosphere.oauth_client')->refresh();
}
catch (\Exception $e) {
\Drupal::logger('atmosphere')->error('Token refresh failed during cron: @message', [
'@message' => $e->getMessage(),
]);
}
}
}