Skip to content

Commit c4f05cd

Browse files
committed
Track updates and fix parent preview fallback image calculation
1 parent 16399a4 commit c4f05cd

File tree

6 files changed

+47
-32
lines changed

6 files changed

+47
-32
lines changed

src/main/php/de/thekid/dialog/api/Entries.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,22 @@ public function update(#[Value] $user, string $id, #[Entity] Entry $source) {
106106

107107
// Join places, autocomplete seems to only work for strings
108108
$suggest= '';
109-
foreach ($source->locations as $location) {
109+
foreach ($source->locations ?? $entry['locations'] as $location) {
110110
$suggest.= ' '.$location['name'];
111111
}
112-
$changes= $source->attributes() + [
112+
$content= $source->content ?? $entry['content'];
113+
$is= $source->is ?? $entry['is'];
114+
115+
$r= $this->repository->modify($id, ['$set' => $source->attributes() + [
113116
'preview' => $preview,
114117
'modified' => time(),
115118
'_searchable' => [
116-
'boost' => isset($source->is['journey']) ? 2.0 : 1.0,
119+
'boost' => isset($is['journey']) ? 2.0 : 1.0,
117120
'suggest' => trim($suggest),
118-
'content' => strip_tags(strtr($source->content, ['<br>' => "\n", '</p><p>' => "\n"]))
121+
'content' => strip_tags(strtr($content, ['<br>' => "\n", '</p><p>' => "\n"])),
119122
],
120-
];
121-
$n= $this->repository->modify($id, ['$set' => $changes])->modified();
122-
return ['updated' => $n];
123+
]]);
124+
return ['updated' => $r->modified()];
123125
}
124126

125127
#[Delete('/{id:.+(/.+)?}')]

src/main/php/de/thekid/dialog/api/Entry.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ class Entry implements Value {
2222
set { $this->attributes['title']= $value; }
2323
}
2424

25-
public string $content {
26-
get => $this->attributes['content'];
25+
public ?string $content {
26+
get => $this->attributes['content'] ?? null;
2727
set { $this->attributes['content']= $value; }
2828
}
2929

30-
public array<string, mixed> $is {
31-
get => $this->attributes['is'];
30+
public ?array<string, mixed> $is {
31+
get => $this->attributes['is'] ?? null;
3232
set { $this->attributes['is']= $value; }
3333
}
3434

@@ -37,18 +37,18 @@ class Entry implements Value {
3737
set { $this->attributes['parent']= $value; }
3838
}
3939

40-
public array<string> $keywords {
41-
get => $this->attributes['keywords'] ?? [];
40+
public ?array<string> $keywords {
41+
get => $this->attributes['keywords'] ?? null;
4242
set { $this->attributes['keywords']= $value; }
4343
}
4444

45-
public array<array<mixed>> $locations {
46-
get => $this->attributes['locations'] ?? [];
45+
public ?array<array<mixed>> $locations {
46+
get => $this->attributes['locations'] ?? null;
4747
set { $this->attributes['locations']= $value; }
4848
}
4949

50-
public array<string, mixed> $weather {
51-
get => $this->attributes['weather'];
50+
public ?array<string, mixed> $weather {
51+
get => $this->attributes['weather'] ?? null;
5252
set { $this->attributes['weather']= $value; }
5353
}
5454

src/main/php/de/thekid/dialog/import/Content.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public function entryFrom(Description $description): array<string, mixed> {
2121
}
2222

2323
public function contentsIn(Files $files): iterable {
24-
yield from $this->mediaIn($files);
24+
return yield from $this->mediaIn($files);
2525
}
2626
}

src/main/php/de/thekid/dialog/import/Journey.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Journey extends Source {
99

1010
/** Subfolders of a journey form its child contents */
1111
private function childrenIn(Files $files): iterable {
12+
$updated= false;
1213
$children= [];
1314
foreach ($this->entry['$children'] ?? [] as $child) {
1415
$children[$child['slug']]= $child;
@@ -19,7 +20,7 @@ private function childrenIn(Files $files): iterable {
1920
$folder= $path->asFolder();
2021
$slug= $this->entry['slug'].'/'.$folder->dirname;
2122

22-
yield from new Content($folder, new File($folder, 'content.md'), $children[$slug] ?? null)
23+
$updated|= yield from new Content($folder, new File($folder, 'content.md'), $children[$slug] ?? null)
2324
->nestedIn($this->entry['slug'])
2425
->synchronize($files)
2526
;
@@ -29,7 +30,9 @@ private function childrenIn(Files $files): iterable {
2930

3031
foreach ($children as $rest) {
3132
yield new DeleteEntry($rest['slug']);
33+
$updated= true;
3234
}
35+
return $updated;
3336
}
3437

3538
public function entryFrom(Description $description): array<string, mixed> {
@@ -50,7 +53,9 @@ public function entryFrom(Description $description): array<string, mixed> {
5053
}
5154

5255
public function contentsIn(Files $files): iterable {
53-
yield from $this->mediaIn($files);
54-
yield from $this->childrenIn($files);
56+
$updated= false;
57+
$updated|= yield from $this->mediaIn($files);
58+
$updated|= yield from $this->childrenIn($files);
59+
return $updated;
5560
}
5661
}

src/main/php/de/thekid/dialog/import/LookupWeather.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
class LookupWeather extends Task {
1010
private $weather= new OpenMeteo('https://open-meteo.com/v1');
1111

12-
public function __construct(private array<string, mixed> $entry, private array<mixed> $images) { }
12+
public function __construct(private array<string, mixed> $entry) { }
1313

1414
public function execute(Endpoint $api) {
15-
if (empty($this->images)) {
15+
if (empty($this->entry['images'])) {
1616
yield 'skip' => 'no images';
1717
return null;
1818
}
@@ -24,7 +24,7 @@ public function execute(Endpoint $api) {
2424

2525
// Infer date range from first and last images
2626
$dates= [];
27-
foreach ($this->images as $image) {
27+
foreach ($this->entry['images'] as $image) {
2828
$dates[]= new Date($image['meta']['dateTime'], $tz);
2929
}
3030
usort($dates, fn($a, $b) => $b->compareTo($a));

src/main/php/de/thekid/dialog/import/Source.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function origin(): Folder { return $this->origin; }
5454

5555
/** Yields all the media files in this source */
5656
protected function mediaIn(Files $files): iterable {
57+
$updated= false;
5758
$images= [];
5859
foreach ($this->entry['images'] ?? [] as $image) {
5960
$images[$image['name']]= $image;
@@ -63,13 +64,16 @@ protected function mediaIn(Files $files): iterable {
6364
$name= $file->filename;
6465
if (!isset($images[$name]) || $file->lastModified() > $images[$name]['modified']) {
6566
$this->entry['images'][$name]= yield new UploadMedia($this->entry['slug'], $file, $processing);
67+
$updated= true;
6668
}
6769
unset($images[$name]);
6870
}
6971

7072
foreach ($images as $rest) {
7173
yield new DeleteMedia($this->entry['slug'], $rest['name']);
74+
$updated= true;
7275
}
76+
return $updated;
7377
}
7478

7579
/** Returns an entry from the given description */
@@ -79,23 +83,27 @@ public abstract function entryFrom(Description $description): array<string, mixe
7983
public abstract function contentsIn(Files $files): iterable;
8084

8185
/** Yields tasks to synchronize this source */
82-
public function synchronize(Files $files) {
86+
public function synchronize(Files $files): iterable {
8387
$this->entry??= yield new FetchEntry($this->name());
8488
if (!isset($this->entry['modified']) || $this->file->lastModified() > $this->entry['modified']) {
8589
$changes= $this->entryFrom(self::$descriptions->parse($this->file));
86-
$updated= time();
90+
$this->entry= $changes + ($this->entry ?? []);
91+
$updated= true;
92+
} else {
93+
$changes= [];
94+
$updated= false;
8795
}
8896

8997
// Although the description file may not have changed, nested contents
9098
// may have, so process them unconditionally.
91-
yield from $this->contentsIn($files);
99+
$updated|= yield from $this->contentsIn($files);
92100

93-
if (isset($updated)) {
94-
$changes['locations']= yield new LookupLocationInfos($changes);
95-
$changes['weather']= yield new LookupWeather($changes, $this->entry['images'] ?? []);
96-
$changes['published']= time();
97-
yield new PublishEntry($this->entry['slug'], $changes);
101+
if ($updated) {
102+
$changes['locations']= $this->entry['locations']= yield new LookupLocationInfos($this->entry);
103+
$changes['weather']= $this->entry['weather']= yield new LookupWeather($this->entry);
104+
yield new PublishEntry($this->entry['slug'], $changes + ['published' => time()]);
98105
}
106+
return $updated;
99107
}
100108

101109
/** @return string */

0 commit comments

Comments
 (0)