Repository navigation
Expand file tree
/
Copy pathPushPreference.php
More file actions
63 lines (54 loc) · 1.63 KB
/
Copy pathPushPreference.php
File metadata and controls
63 lines (54 loc) · 1.63 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
<?php
declare(strict_types=1);
namespace App\Enums;
use App\Models\User;
/**
* What a user can choose to have pushed to their phones, each a switch of its own.
*/
enum PushPreference: string
{
case Follows = 'follows';
case Replies = 'replies';
case Mentions = 'mentions';
case DirectRemotes = 'direct_remotes';
case OrgRemotes = 'org_remotes';
/**
* The switch for a notification, or null when it is one that isn't pushed.
*/
public static function forNotification(NotificationType $type): ?self
{
return match ($type) {
NotificationType::Followed => self::Follows,
NotificationType::Replied => self::Replies,
NotificationType::Mentioned => self::Mentions,
default => null,
};
}
/**
* The switch for messages and pings in a conversation of this kind.
*/
public static function forConversation(ConversationKind $kind): self
{
return match ($kind) {
ConversationKind::Direct => self::DirectRemotes,
ConversationKind::Org => self::OrgRemotes,
};
}
/**
* Every switch and whether the user has it on.
*
* @return array<value-of<self>, bool>
*/
public static function allFor(User $user): array
{
return collect(self::cases())->mapWithKeys(fn (self $preference): array => [$preference->value => $preference->isOnFor($user)])->all();
}
public function column(): string
{
return "push_{$this->value}";
}
public function isOnFor(User $user): bool
{
return $user->getAttribute($this->column()) === true;
}
}