Repository navigation
Expand file tree
/
Copy pathCommitTranslations.php
More file actions
149 lines (122 loc) · 4.97 KB
/
Copy pathCommitTranslations.php
File metadata and controls
149 lines (122 loc) · 4.97 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
<?php
declare(strict_types=1);
namespace App\Services;
use App\Enums\CommitStatus;
use App\Enums\Feature;
use App\Exceptions\CommitProcessingFailed;
use App\Jobs\DetectCommitLanguage;
use App\Models\Commit;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Cache;
use Locale;
use ResourceBundle;
use Throwable;
/**
* @phpstan-type CachedTranslations array{source_language: string, translations: array<string, string>, expires_at: int, translated_at?: int}
*/
final readonly class CommitTranslations
{
public const int TTL_SECONDS = 604800;
public function __construct(private Features $features, private EncodedText $encodedText)
{
//
}
public function enabled(): bool
{
return $this->features->enabled(Feature::Translations);
}
public function available(): bool
{
return $this->features->available(Feature::Translations, request()->user());
}
public function key(Commit $commit): string
{
// Previously detected as indeterminate: encoded posts need fresh detection, without evicting other translations.
$encoding = $this->encodedText->decode($commit->body) === null ? '' : 'encoded:';
return 'commit-translations:v1:'.$encoding.hash('sha256', $commit->body);
}
/** @return list<string> */
public function languages(): array
{
return once(function (): array {
/** @var list<string> $locales */
$locales = ResourceBundle::getLocales('');
return array_values(collect($locales)->map(fn (string $locale): string => Locale::getPrimaryLanguage($locale) ?? 'und')->unique()->all());
});
}
public function language(): string
{
$language = Locale::getPrimaryLanguage(request()->user()->translation_language ?? request()->getPreferredLanguage() ?? 'en');
return in_array($language, $this->languages(), true) ? $language : 'en';
}
/** @return CachedTranslations|null */
public function cached(Commit $commit): ?array
{
/** @var CachedTranslations|null $cached */
$cached = Cache::get($this->key($commit));
return $cached;
}
/** @param Collection<int, Commit> $commits */
public function attach(Collection $commits, bool $includePending = false): void
{
if (! $this->available()) {
return;
}
Cache::memo()->many($commits->map($this->key(...))->all());
$viewer = request()->user()?->id;
foreach ($commits as $commit) {
$translation = $this->display($commit, includePending: $includePending);
$commit->setAttribute('translation', $viewer !== null && $commit->user_id === $viewer ? null : $translation);
}
}
/**
* The commits whose text was translated, or had its language found, since the given time: what a page that may
* have missed being told reads again.
*
* @param Collection<int, Commit> $commits
* @return list<string>
*/
public function translatedSince(Collection $commits, CarbonImmutable $since): array
{
if (! $this->available()) {
return [];
}
/** @var array<string, CachedTranslations|null> $cached */
$cached = Cache::many($commits->map($this->key(...))->unique()->all());
return array_values($commits
->filter(fn (Commit $commit): bool => ($cached[$this->key($commit)]['translated_at'] ?? 0) > $since->getTimestampMs())
->modelKeys());
}
/** @return array<string, mixed>|null */
public function display(Commit $commit, ?string $language = null, bool $includePending = false): ?array
{
if ($commit->trashed() || ! in_array($commit->status, $includePending ? [CommitStatus::Approved, CommitStatus::Pending] : [CommitStatus::Approved], true) || $commit->body === '' || ! $this->available()) {
return null;
}
/** @var CachedTranslations|null $cached */
$cached = Cache::memo()->get($this->key($commit));
if ($cached === null) {
try {
dispatch(new DetectCommitLanguage($commit->id))->afterCommit();
} catch (Throwable $exception) {
report(new CommitProcessingFailed('Language detection dispatch', $commit->id, $exception));
}
}
$language ??= $this->language();
$source = $cached['source_language'] ?? null;
$body = $cached['translations'][$language] ?? null;
return [
'language' => $language,
'source_language' => $source,
'source_name' => match ($source) {
null => null,
'binary' => 'Binary',
'morse' => 'Morse code',
default => Locale::getDisplayLanguage($source, 'en'),
},
'available' => $source !== $language && $source !== 'und',
'translated' => $body === null ? null : resolve(CommitBodies::class)->segments($body),
];
}
}