Repository navigation
Expand file tree
/
Copy pathCommit.php
More file actions
400 lines (349 loc) · 12.6 KB
/
Copy pathCommit.php
File metadata and controls
400 lines (349 loc) · 12.6 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
declare(strict_types=1);
namespace App\Models;
use App\Enums\CommitStatus;
use App\Queries\CommitBuilder;
use App\Services\CommitBodies;
use App\Services\Mentions;
use Carbon\CarbonInterface;
use Database\Factories\CommitFactory;
use Illuminate\Database\Eloquent\Attributes\Hidden;
use Illuminate\Database\Eloquent\Attributes\UseEloquentBuilder;
use Illuminate\Database\Eloquent\Casts\AsVector;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* @property-read string $id
* @property-read string|null $user_id
* @property-read string|null $author_id
* @property-read string|null $guild_id
* @property-read Guild|null $guild
* @property-read string|null $parent_id
* @property-read string|null $root_id
* @property-read float $hot_score
* @property-read string $body
* @property-read array<string, string>|null $mentions
* @property-read array<string, string>|null $organization_mentions
* @property-read CommitStatus $status
* @property-read string|null $link_preview_id
* @property-read string|null $quoted_commit_id
* @property-read CommitStatus|null $ai_decision
* @property-read list<string>|null $ai_categories
* @property-read list<float>|null $embedding
* @property-read CarbonInterface|null $headlines_classified_at
* @property-read CarbonInterface $created_at
* @property-read CarbonInterface $updated_at
* @property-read CarbonInterface|null $deleted_at
* @property-read int $stars_count
* @property-read int $pasteis_count
* @property-read bool $starred
* @property-read User|null $user
* @property-read Profile|null $author
* @property-read Collection<int, User> $stars
* @property-read Commit|null $parent
* @property-read Collection<int, Commit> $replies
* @property-read int $replies_count
* @property-read int|null $repliers_count
* @property-read string|null $continuation_id
* @property-read LinkPreview|null $linkPreview
* @property-read Poll|null $poll
* @property-read Commit|null $quotedCommit
* @property-read Collection<int, Commit> $quotes
* @property-read Collection<int, CommitImage> $images
* @property-read Collection<int, Headline> $headlines
*/
#[Hidden([
'user',
'user_id',
'author_id',
'guild_id',
'guild',
'ai_decision',
'ai_categories',
'embedding',
'headlines_classified_at',
])]
#[UseEloquentBuilder(CommitBuilder::class)]
final class Commit extends Model
{
/** @use HasFactory<CommitFactory> */
use HasFactory;
use HasUuids;
use SoftDeletes;
public const int RESTORABLE_SECONDS = 30;
/**
* Moderation retries a failed check twice, the last time ten minutes later, so a pending commit older than this has
* been through every attempt.
*/
public const int CHECKING_MINUTES = 15;
public const int MIN_BODY_LENGTH = 1;
public const int MAX_BODY_LENGTH = 4000;
/**
* Lists show a longer body folded to its first characters and lines, the rest one click away.
*/
public const int PREVIEW_LENGTH = 280;
public const int PREVIEW_LINES = 8;
/**
* @var array<string, mixed>
*/
protected $attributes = [
'pasteis_count' => 0,
];
/**
* @var array{body_segments: list<array<string, string|null>>, preview_segments: list<array<string, string|null>>|null}|null
*/
private ?array $segments = null;
/** @return BelongsTo<Guild, $this> */
public function guild(): BelongsTo
{
return $this->belongsTo(Guild::class);
}
/**
* @return array<string, string>
*/
public function casts(): array
{
return [
'id' => 'string',
'user_id' => 'string',
'author_id' => 'string',
'parent_id' => 'string',
'root_id' => 'string',
'hot_score' => 'float',
'pasteis_count' => 'integer',
'body' => 'string',
'mentions' => 'array',
'organization_mentions' => 'array',
'status' => CommitStatus::class,
'link_preview_id' => 'string',
'quoted_commit_id' => 'string',
'ai_decision' => CommitStatus::class,
'ai_categories' => 'array',
'embedding' => AsVector::class,
'headlines_classified_at' => 'datetime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
];
}
/**
* A deleted commit only ever leaves a tombstone behind: never its author or body. Otherwise, the body also comes split
* into segments, for rendering, and whether it is still being checked, so its author knows how long it might stay
* pending.
*
* @return array<mixed>
*/
public function toArray(): array
{
if ($this->trashed()) {
$keys = ['id', 'parent_id', 'deleted_at', 'replies_count'];
return array_intersect_key($this->attributesToArray(), array_flip($keys));
}
if ($this->segments === null || $this->isDirty('body')) {
$this->segments = resolve(CommitBodies::class)->segments($this->body);
}
return [...parent::toArray(), ...$this->segments, 'is_checking' => $this->isBeingChecked()];
}
/**
* The user who posted the commit, whom moderation holds to account.
*
* @return BelongsTo<User, $this>
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* The profile the commit is from, which everyone sees, with the user or the organization it is.
*
* @return BelongsTo<Profile, $this>
*/
public function author(): BelongsTo
{
return $this->belongsTo(Profile::class)->with(['user', 'organization']);
}
/**
* @return BelongsToMany<User, $this>
*/
public function stars(): BelongsToMany
{
return $this->belongsToMany(User::class, 'stars')->withTimestamps();
}
/**
* @return BelongsToMany<Headline, $this>
*/
public function headlines(): BelongsToMany
{
return $this->belongsToMany(Headline::class)->withTimestamps(updatedAt: false);
}
/**
* The handles it mentions that aren't a user's, which may be GitHub organizations'.
*
* @return list<string>
*/
public function mentionedNonUsers(): array
{
return array_values(array_diff(resolve(Mentions::class)->handles($this->body), array_keys($this->mentions ?? [])));
}
public function isAddressedTo(User $user): bool
{
$explicitMentions = array_intersect_key($this->mentions ?? [], array_flip(resolve(Mentions::class)->handles($this->body)));
return in_array($user->id, $explicitMentions, true) || $this->parent?->author_id === $user->id;
}
/**
* A commit is its author's own: a person's, or each member's of the organization it is from.
*/
public function isOwnedBy(User $user): bool
{
return $this->author_id !== null && ($this->author_id === $user->id || $user->organizations()->whereKey($this->author_id)->exists());
}
/**
* Shows who posted an organization's commit, which only moderators see, so they can time out or ban that account. Null
* once it is deleted. A person's own commit has no poster apart from its author.
*/
public function withPoster(): self
{
return $this->author_id === $this->user_id ? $this : $this->setRelation('poster', $this->user);
}
/**
* Approved commits are visible to everyone but people across a block with their author, which an organization never
* is. Whoever posted a commit can also see it, and the members of the organization it is from.
*/
public function isVisibleTo(?User $user): bool
{
if ($user instanceof User && $this->user_id === $user->id) {
return true;
}
if ($this->status !== CommitStatus::Approved) {
return $user instanceof User && $this->isOwnedBy($user);
}
$author = $this->author?->user;
return ! $user instanceof User || ! $author instanceof User || ! $user->hasBlockWith($author);
}
/**
* A pending commit is being checked until its moderation job leaves an AI decision behind. After that, or once every
* attempt has had its chance, it simply waits for a moderator.
*/
public function isBeingChecked(): bool
{
return $this->status === CommitStatus::Pending
&& $this->ai_decision === null
&& $this->created_at->greaterThan(now()->subMinutes(self::CHECKING_MINUTES));
}
/**
* A deleted commit comes back within seconds, unless its author has gone since: a deleted account takes its own
* commits' authors with it, and an organization GitHub no longer has its own. Whoever posted an organization's
* commit may have gone, while its members still can bring it back.
*/
public function isRestorable(): bool
{
return $this->author_id !== null && $this->deleted_at !== null && now()->lessThan($this->deleted_at->copy()->addSeconds(self::RESTORABLE_SECONDS));
}
/**
* @return BelongsTo<Commit, $this>
*/
public function parent(): BelongsTo
{
return $this->belongsTo(self::class, 'parent_id');
}
/**
* The top-level commit that started the thread the commit belongs to, which is the commit itself when it is top-level.
*/
public function threadRootId(): string
{
return $this->root_id ?? $this->id;
}
/**
* @return HasMany<Commit, $this>
*/
public function replies(): HasMany
{
return $this->hasMany(self::class, 'parent_id');
}
/**
* @return BelongsTo<LinkPreview, $this>
*/
public function linkPreview(): BelongsTo
{
return $this->belongsTo(LinkPreview::class);
}
/**
* @return HasOne<Poll, $this>
*/
public function poll(): HasOne
{
return $this->hasOne(Poll::class);
}
/**
* @return BelongsTo<Commit, $this>
*/
public function quotedCommit(): BelongsTo
{
return $this->belongsTo(self::class, 'quoted_commit_id');
}
public function headlineBody(): string
{
$quoted = $this->quotedCommit;
if ($quoted === null || $quoted->trashed() || $quoted->status !== CommitStatus::Approved || $quoted->body === '') {
return $this->body;
}
return json_encode(['post' => $this->body, 'quoted_post' => $quoted->body], JSON_THROW_ON_ERROR);
}
/**
* @return HasMany<Commit, $this>
*/
public function quotes(): HasMany
{
return $this->hasMany(self::class, 'quoted_commit_id');
}
/**
* @return HasMany<CommitImage, $this>
*/
public function images(): HasMany
{
return $this->hasMany(CommitImage::class)->orderBy('position');
}
/**
* Images follow the commit's visibility, and moderators see them to review the commit.
*/
public function imagesAreVisibleTo(?User $user): bool
{
if ($this->trashed()) {
return false;
}
if ($this->status === CommitStatus::Approved) {
return true;
}
return $user instanceof User && ($this->user_id === $user->id || $user->isAdmin() || $this->isOwnedBy($user));
}
/**
* Loads what anyone may see around the commit: its author, its approved replies, and its parent only when approved.
*/
public function loadPublicPreview(): self
{
$this->forceFill([
'replies_count' => self::query()->withTrashed()->withVisibleReplyCount(null)
->whereKey($this->getAttributeFromArray($this->getKeyName()))->first()->replies_count ?? 0,
])->syncOriginalAttribute('replies_count');
return $this->load(['author', 'parent' => fn (BelongsTo $query): BelongsTo => $query->where('status', CommitStatus::Approved)->with('author')]);
}
/**
* Changes whenever anything drawn on the commit's link preview image may have changed.
*/
public function openGraphImageVersion(): string
{
return hash('xxh3', (string) json_encode([
$this->body,
$this->replies_count,
[$this->author?->name, $this->author?->handle, $this->author?->avatar_url, $this->author?->owner()?->lane()],
$this->parent?->author?->handle,
]));
}
}