Skip to content

Commit 8d6b72b

Browse files
authored
Merge pull request #212 from cronxco/plugin-admin
♻️ Better Blocks - enforce uniqueness
2 parents 6f5f88a + 930ea20 commit 8d6b72b

29 files changed

+1310
-104
lines changed

app/Integrations/Base/WebhookPlugin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,9 @@ protected function createEventsFromWebhook(array $convertedData, Integration $in
197197

198198
// Create blocks if any
199199
foreach ($eventData['blocks'] ?? [] as $blockData) {
200-
$event->blocks()->create([
200+
$event->createBlock([
201201
'time' => $blockData['time'] ?? now(),
202+
'block_type' => $blockData['block_type'] ?? '',
202203
'title' => $blockData['title'],
203204
'metadata' => $blockData['metadata'] ?? (isset($blockData['content']) ? ['text' => (string) $blockData['content']] : []),
204205
'url' => $blockData['url'] ?? null,

app/Integrations/GitHub/GitHubPlugin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,9 +1015,9 @@ protected function createEventFromData(array $convertedData, Integration $integr
10151015
$blocks = [];
10161016
}
10171017
foreach ($blocks as $blockData) {
1018-
$event->blocks()->create([
1018+
$event->createBlock([
10191019
'time' => $blockData['time'] ?? now(),
1020-
'integration_id' => $integration->id,
1020+
'block_type' => $blockData['block_type'] ?? '',
10211021
'title' => $blockData['title'],
10221022
'metadata' => $blockData['metadata'] ?? (isset($blockData['content']) ? ['text' => (string) $blockData['content']] : []),
10231023
'url' => $blockData['url'] ?? null,

app/Integrations/Hevy/HevyPlugin.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ public static function getBlockTypes(): array
141141
'value_unit' => null,
142142
'hidden' => false,
143143
],
144+
'exercise_summary' => [
145+
'icon' => 'o-chart-bar',
146+
'display_name' => 'Exercise Summary',
147+
'description' => 'Summary statistics for an exercise in a workout',
148+
'display_with_object' => true,
149+
'value_unit' => 'kg',
150+
'hidden' => false,
151+
],
144152
];
145153
}
146154

@@ -398,8 +406,8 @@ public function createWorkoutEvent(Integration $integration, array $workout): vo
398406
$content .= "\n**Rest:** {$rest} s";
399407
}
400408

401-
$event->blocks()->create(['block_type' => 'exercise',
402-
409+
$event->createBlock([
410+
'block_type' => 'exercise',
403411
'time' => $startIso,
404412
'title' => $exerciseName . ' - Set ' . $setNum,
405413
'metadata' => ['text' => $content],
@@ -413,8 +421,8 @@ public function createWorkoutEvent(Integration $integration, array $workout): vo
413421

414422
if ($includeExerciseSummary && $exerciseName !== '') {
415423
[$encExVol, $exVolMult] = $this->encodeNumericValue($exerciseVolume);
416-
$event->blocks()->create(['block_type' => 'exercise',
417-
424+
$event->createBlock([
425+
'block_type' => 'exercise_summary',
418426
'time' => $startIso,
419427
'title' => $exerciseName . ' - Total Volume',
420428
'metadata' => ['text' => 'Total volume (weight x reps) for this exercise'],

app/Integrations/Monzo/MonzoPlugin.php

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,22 @@ public static function getBlockTypes(): array
374374
'value_unit' => null,
375375
'hidden' => false,
376376
],
377+
'merchant' => [
378+
'icon' => 'o-building-storefront',
379+
'display_name' => 'Merchant',
380+
'description' => 'Merchant information for transaction',
381+
'display_with_object' => true,
382+
'value_unit' => null,
383+
'hidden' => false,
384+
],
385+
'pot_transfer' => [
386+
'icon' => 'o-arrow-path-rounded-square',
387+
'display_name' => 'Pot Transfer',
388+
'description' => 'Money transfer to or from a Monzo pot',
389+
'display_with_object' => true,
390+
'value_unit' => 'GBP',
391+
'hidden' => false,
392+
],
377393
];
378394
}
379395

@@ -527,10 +543,9 @@ public function fetchData(Integration $integration): void
527543
public function addBalanceBlocks(Event $event, Integration $integration, array $account, string $date, int $balance, int $spendToday): void
528544
{
529545
// Spend Today block
530-
$event->blocks()->create([
546+
$event->createBlock([
531547
'time' => $event->time,
532548
'block_type' => 'spend_today',
533-
'integration_id' => $event->integration_id,
534549
'title' => 'Spend Today',
535550
'metadata' => [],
536551
'media_url' => null,
@@ -553,11 +568,9 @@ public function addBalanceBlocks(Event $event, Integration $integration, array $
553568
$currentVal = (int) abs($balance);
554569
$delta = $currentVal - $prevVal; // cents
555570
if ($delta !== 0) {
556-
$event->blocks()->create(['block_type' => 'balance',
557-
571+
$event->createBlock([
558572
'time' => $event->time,
559573
'block_type' => 'balance_change',
560-
'integration_id' => $event->integration_id,
561574
'title' => 'Balance Change',
562575
'metadata' => ['text' => $delta > 0 ? 'Up' : 'Down'],
563576
'media_url' => null,
@@ -1396,8 +1409,9 @@ private function maybeAddTransactionBlocks(Event $event, array $tx): void
13961409
$parts['address'] = $addrLine;
13971410
}
13981411
}
1399-
$event->blocks()->create([
1412+
$event->createBlock([
14001413
'time' => $event->time,
1414+
'block_type' => 'merchant',
14011415
'title' => 'Merchant',
14021416
'metadata' => $parts,
14031417
'media_url' => $m['logo'] ?? null,
@@ -1422,7 +1436,7 @@ private function maybeAddTransactionBlocks(Event $event, array $tx): void
14221436
if ($rate !== null) {
14231437
$metadata['rate'] = $rate;
14241438
}
1425-
$event->blocks()->create([
1439+
$event->createBlock([
14261440
'time' => $event->time,
14271441
'block_type' => 'foreign_exchange',
14281442
'title' => 'FX',
@@ -1437,7 +1451,7 @@ private function maybeAddTransactionBlocks(Event $event, array $tx): void
14371451
// Example block for virtual cards
14381452
if (! empty($tx['virtual_card'])) {
14391453
$vc = (array) $tx['virtual_card'];
1440-
$event->blocks()->create([
1454+
$event->createBlock([
14411455
'time' => $event->time,
14421456
'block_type' => 'virtual_card',
14431457
'title' => 'Virtual Card',
@@ -1465,9 +1479,9 @@ private function maybeAddTransactionBlocks(Event $event, array $tx): void
14651479
} catch (Throwable $e) {
14661480
// ignore
14671481
}
1468-
$event->blocks()->create(['block_type' => 'pot',
1469-
1482+
$event->createBlock([
14701483
'time' => $event->time,
1484+
'block_type' => 'pot_transfer',
14711485
'title' => 'Pot Transfer',
14721486
'metadata' => ['direction' => $direction, 'pot_name' => $potName ?? 'Pot'],
14731487
'media_url' => null,
@@ -1488,10 +1502,9 @@ private function maybeAddTransactionBlocks(Event $event, array $tx): void
14881502
$details['sort_code'] = $cp['sort_code'];
14891503
$details['account_number'] = $cp['account_number'];
14901504
}
1491-
$event->blocks()->create([
1505+
$event->createBlock([
14921506
'block_type' => 'bank_transfer',
14931507
'time' => $event->time,
1494-
'integration_id' => $event->integration_id,
14951508
'title' => 'Bank Transfer',
14961509
'metadata' => $details,
14971510
'media_url' => null,

app/Integrations/Oura/OuraPlugin.php

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,10 +1155,9 @@ public function createDailyRecordEvent(Integration $integration, string $kind, a
11551155
$contributors = $contributorsField ? Arr::get($item, $contributorsField, []) : [];
11561156
foreach ($contributors as $name => $value) {
11571157
[$encodedContrib, $contribMultiplier] = $this->encodeNumericValue(is_numeric($value) ? (float) $value : null);
1158-
$event->blocks()->create([
1158+
$event->createBlock([
11591159
'block_type' => 'contributors',
11601160
'time' => $event->time,
1161-
'integration_id' => $integration->id,
11621161
'title' => Str::title(str_replace('_', ' ', (string) $name)),
11631162
'metadata' => ['type' => 'contributor', 'field' => $name],
11641163
'value' => $encodedContrib,
@@ -1183,10 +1182,9 @@ public function createDailyRecordEvent(Integration $integration, string $kind, a
11831182
$label = Str::title(str_replace('_', ' ', $field));
11841183
$value = $item[$field];
11851184
[$encodedDetail, $detailMultiplier] = $this->encodeNumericValue(is_numeric($value) ? (float) $value : null);
1186-
$event->blocks()->create([
1185+
$event->createBlock([
11871186
'block_type' => 'activity_metrics',
11881187
'time' => $event->time,
1189-
'integration_id' => $integration->id,
11901188
'title' => $label,
11911189
'metadata' => ['type' => 'detail', 'field' => $field],
11921190
'value' => $encodedDetail,
@@ -1241,10 +1239,9 @@ public function createWorkoutEvent(Integration $integration, array $item): void
12411239
]);
12421240

12431241
[$encodedCalories, $calMultiplier] = $this->encodeNumericValue($calories);
1244-
$event->blocks()->create([
1242+
$event->createBlock([
12451243
'block_type' => 'workout_metrics',
12461244
'time' => $event->time,
1247-
'integration_id' => $integration->id,
12481245
'title' => 'Calories',
12491246
'metadata' => ['type' => 'calorie_burn', 'estimated' => true],
12501247
'value' => $encodedCalories,
@@ -1255,10 +1252,9 @@ public function createWorkoutEvent(Integration $integration, array $item): void
12551252
$avgHr = Arr::get($item, 'average_heart_rate');
12561253
if ($avgHr !== null) {
12571254
[$encodedAvgHr, $avgHrMultiplier] = $this->encodeNumericValue($avgHr);
1258-
$event->blocks()->create([
1255+
$event->createBlock([
12591256
'block_type' => 'heart_rate',
12601257
'time' => $event->time,
1261-
'integration_id' => $integration->id,
12621258
'title' => 'Average Heart Rate',
12631259
'metadata' => ['type' => 'average', 'context' => 'workout'],
12641260
'value' => $encodedAvgHr,
@@ -1341,10 +1337,10 @@ public function createEventsSafely(Integration $integration, array $eventData):
13411337
]
13421338
);
13431339

1344-
// Create blocks if any
1340+
// Create blocks if any using the new unique creation method
13451341
if (isset($data['blocks'])) {
13461342
foreach ($data['blocks'] as $blockData) {
1347-
$event->blocks()->create([
1343+
$event->createBlock([
13481344
'time' => $blockData['time'] ?? $event->time,
13491345
'block_type' => $blockData['block_type'] ?? '',
13501346
'title' => $blockData['title'],
@@ -2062,7 +2058,7 @@ protected function fetchSleepRecords(Integration $integration, string $startDate
20622058
if ($seconds === null) {
20632059
continue;
20642060
}
2065-
$event->blocks()->create([
2061+
$event->createBlock([
20662062
'block_type' => 'sleep_stages',
20672063
'time' => $event->time,
20682064
'integration_id' => $integration->id,
@@ -2077,7 +2073,7 @@ protected function fetchSleepRecords(Integration $integration, string $startDate
20772073
$hrAvg = Arr::get($item, 'average_heart_rate');
20782074
if ($hrAvg !== null) {
20792075
[$encodedHrAvg, $hrAvgMultiplier] = $this->encodeNumericValue($hrAvg);
2080-
$event->blocks()->create([
2076+
$event->createBlock([
20812077
'block_type' => 'heart_rate',
20822078
'time' => $event->time,
20832079
'integration_id' => $integration->id,
@@ -2314,10 +2310,9 @@ protected function fetchHeartRateSeries(Integration $integration, string $startI
23142310

23152311
// Replace summary with separate min/max blocks
23162312
[$encMin, $minMult] = $this->encodeNumericValue($min);
2317-
$event->blocks()->create([
2313+
$event->createBlock([
23182314
'block_type' => 'heart_rate',
23192315
'time' => $event->time,
2320-
'integration_id' => $integration->id,
23212316
'title' => 'Min Heart Rate',
23222317
'metadata' => ['type' => 'minimum', 'context' => 'daily_series'],
23232318
'value' => $encMin,
@@ -2326,21 +2321,19 @@ protected function fetchHeartRateSeries(Integration $integration, string $startI
23262321
]);
23272322

23282323
[$encMax, $maxMult] = $this->encodeNumericValue($max);
2329-
$event->blocks()->create([
2324+
$event->createBlock([
23302325
'block_type' => 'heart_rate',
23312326
'time' => $event->time,
2332-
'integration_id' => $integration->id,
23332327
'title' => 'Max Heart Rate',
23342328
'metadata' => ['type' => 'maximum', 'context' => 'daily_series'],
23352329
'value' => $encMax,
23362330
'value_multiplier' => $maxMult,
23372331
'value_unit' => 'bpm',
23382332
]);
23392333

2340-
$event->blocks()->create([
2334+
$event->createBlock([
23412335
'block_type' => 'heart_rate',
23422336
'time' => $event->time,
2343-
'integration_id' => $integration->id,
23442337
'title' => 'Data Points',
23452338
'metadata' => ['type' => 'count', 'context' => 'daily_series'],
23462339
'value' => (int) $points->count(),
@@ -2389,10 +2382,9 @@ protected function createSessionEvent(Integration $integration, array $item): vo
23892382

23902383
$state = Arr::get($item, 'mood', Arr::get($item, 'state'));
23912384
if ($state) {
2392-
$event->blocks()->create([
2385+
$event->createBlock([
23932386
'block_type' => 'biometrics',
23942387
'time' => $event->time,
2395-
'integration_id' => $integration->id,
23962388
'title' => 'State',
23972389
'metadata' => ['type' => 'mood_state', 'value' => (string) $state],
23982390
'content' => (string) $state,
@@ -2445,10 +2437,9 @@ protected function createTagEvent(Integration $integration, array $item): void
24452437
'target_id' => $tagTarget->id,
24462438
]);
24472439

2448-
$event->blocks()->create([
2440+
$event->createBlock([
24492441
'block_type' => 'tag_info',
24502442
'time' => $event->time,
2451-
'integration_id' => $integration->id,
24522443
'title' => 'Tag',
24532444
'metadata' => ['type' => 'user_tag', 'label' => (string) $label],
24542445
'content' => (string) $label,
@@ -2750,7 +2741,7 @@ protected function createEnhancedTagEvent(Integration $integration, array $item)
27502741

27512742
// Add tag details as blocks
27522743
if ($tagType) {
2753-
$event->blocks()->create([
2744+
$event->createBlock([
27542745
'block_type' => 'tag_info',
27552746
'time' => $event->time,
27562747
'integration_id' => $integration->id,
@@ -2761,7 +2752,7 @@ protected function createEnhancedTagEvent(Integration $integration, array $item)
27612752
}
27622753

27632754
if ($item['comment'] ?? null) {
2764-
$event->blocks()->create([
2755+
$event->createBlock([
27652756
'block_type' => 'tag_info',
27662757
'time' => $event->time,
27672758
'integration_id' => $integration->id,
@@ -2822,7 +2813,7 @@ protected function createSleepTimeEvent(Integration $integration, array $item):
28222813

28232814
// Add recommendation blocks
28242815
if ($recommendation) {
2825-
$event->blocks()->create([
2816+
$event->createBlock([
28262817
'block_type' => 'recommendation',
28272818
'time' => $event->time,
28282819
'integration_id' => $integration->id,
@@ -2887,7 +2878,7 @@ protected function createRestModePeriodEvent(Integration $integration, array $it
28872878

28882879
// Add episode blocks
28892880
if (is_array($episodes) && count($episodes) > 0) {
2890-
$event->blocks()->create([
2881+
$event->createBlock([
28912882
'block_type' => 'biometrics',
28922883
'time' => $event->time,
28932884
'integration_id' => $integration->id,
@@ -3019,7 +3010,7 @@ private function createSleepRecordFromItem(Integration $integration, array $item
30193010
if ($seconds === null) {
30203011
continue;
30213012
}
3022-
$event->blocks()->create([
3013+
$event->createBlock([
30233014
'block_type' => 'sleep_stages',
30243015
'time' => $event->time,
30253016
'integration_id' => $integration->id,
@@ -3034,7 +3025,7 @@ private function createSleepRecordFromItem(Integration $integration, array $item
30343025
$hrAvg = Arr::get($item, 'average_heart_rate');
30353026
if ($hrAvg !== null) {
30363027
[$encodedHrAvg, $hrAvgMultiplier] = $this->encodeNumericValue($hrAvg);
3037-
$event->blocks()->create([
3028+
$event->createBlock([
30383029
'block_type' => 'heart_rate',
30393030
'time' => $event->time,
30403031
'integration_id' => $integration->id,

0 commit comments

Comments
 (0)