Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 59 additions & 10 deletions app/Devnull/Data/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function __construct(
public ?int $level = null,
public ?string $rank = null,
public ?string $rankDescription = null,
public ?string $organization = null,
public ?string $timeoutReason = null,
public ?string $timeoutDuration = null,
public ?CarbonImmutable $timeoutEndsAt = null,
) {}

/**
Expand All @@ -53,6 +57,8 @@ public static function fromArray(array $data): self
? (int) $data['unread_count'] > 0
: array_key_exists('read_at', $data) && $data['read_at'] === null;
$id = (string) ($data['id'] ?? '');
$organization = is_array($data['organization'] ?? null) && is_string($data['organization']['handle'] ?? null) ? $data['organization']['handle'] : null;
$timeout = is_array($data['timeout'] ?? null) ? $data['timeout'] : [];

return new self(
id: $id,
Expand All @@ -75,6 +81,10 @@ public static function fromArray(array $data): self
level: is_int($data['level'] ?? null) ? $data['level'] : null,
rank: is_string($data['rank'] ?? null) && $data['rank'] !== '' ? $data['rank'] : null,
rankDescription: is_string($data['rank_description'] ?? null) && trim($data['rank_description']) !== '' ? $data['rank_description'] : null,
organization: $organization,
timeoutReason: is_string($timeout['reason'] ?? null) ? $timeout['reason'] : null,
timeoutDuration: is_string($timeout['duration'] ?? null) ? $timeout['duration'] : null,
timeoutEndsAt: Commit::date($timeout['ends_at'] ?? null),
);
}

Expand All @@ -99,11 +109,25 @@ public function isFollow(): bool
}

/**
* A moderator approved or rejected your commit. Nobody else is named.
* A moderator approved or rejected your commit, or put you in timeout. Nobody else is named.
*/
public function isModeration(): bool
{
return in_array($this->type, ['approved', 'rejected'], true);
return in_array($this->type, ['approved', 'rejected', 'timed_out'], true);
}

public function isTimeout(): bool
{
return $this->type === 'timed_out';
}

/**
* Someone else's commit is what it's about, so the web leads with them: their avatar on the rail, their name and
* handle over what they did, then their words. A reply, a quote, a mention or someone's first commit.
*/
public function isActorsCommit(): bool
{
return in_array($this->type, ['replied', 'quoted', 'mentioned', 'first_commit'], true);
}

public function isRejection(): bool
Expand Down Expand Up @@ -140,7 +164,7 @@ public function levelLabel(): ?string
*/
public function isAboutYourCommit(): bool
{
return $this->isStar() || $this->isModeration();
return in_array($this->type, ['starred', 'approved', 'rejected'], true);
}

/**
Expand All @@ -165,11 +189,12 @@ public function isNewSince(?CarbonImmutable $readAt): bool
}

/**
* The commit a tap opens, as on the web: the notification's own commit. A follow or a level-up opens a profile instead.
* The commit a tap opens, as on the web: the notification's own commit. A follow, a timeout or a level-up opens a
* profile instead.
*/
public function commitToOpen(): ?string
{
return $this->isFollow() || $this->isLevelUp() ? null : $this->commitId;
return $this->isFollow() || $this->isLevelUp() || $this->isTimeout() ? null : $this->commitId;
}

/**
Expand Down Expand Up @@ -202,19 +227,43 @@ public function others(): string
return $others < 1 ? '' : "and {$others} ".($others === 1 ? 'other' : 'others');
}

/**
* When exactly two people followed you or starred your commit, the web names both: the other one, or null.
*/
public function secondActor(): ?User
{
if (! in_array($this->type, ['followed', 'starred'], true) || $this->actorsCount !== 2) {
return null;
}

$second = $this->actors[1] ?? null;

return $second !== null && $second->id !== $this->actor->id ? $second : null;
}

/**
* Whose commit it is, as the web words it: "your", or the organization's you heard about it for.
*/
private function whose(): string
{
return $this->organization === null ? 'your' : "@{$this->organization}'s";
}

/**
* What happened, as the web words it. For a person it follows their name and their handle or the others.
*/
public function sentence(): string
{
return match ($this->type) {
'followed' => 'followed you',
'mentioned' => 'mentioned you',
'starred' => 'starred your commit',
'quoted' => 'quoted your commit',
'approved', 'rejected' => "Your commit was {$this->type}",
'mentioned' => $this->organization === null ? 'mentioned you' : "mentioned @{$this->organization}",
'starred' => "starred {$this->whose()} commit",
'quoted' => "quoted {$this->whose()} commit",
'first_commit' => 'made their first commit',
'approved', 'rejected' => ($this->organization === null ? 'Your' : $this->whose())." commit was {$this->type}",
'timed_out' => 'You were put in timeout',
'leveled_up' => $this->levelLabel() === null ? 'You reached a new level' : "You reached level {$this->level}: {$this->rank}",
default => 'replied to your commit',
default => "replied to {$this->whose()} commit",
};
}

Expand Down
133 changes: 133 additions & 0 deletions app/Devnull/NotificationFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace App\Devnull;

use Illuminate\Support\Facades\Cache;

/**
* Which kinds of notification you see, as the web's filters choose them.
*
* Each kind is on or off on its own. devnull is asked for the ones that are
* on as a comma list (?type=replies,stars), for all of them as `all`, and for
* none as `none`, exactly as the web asks. The web keeps the choice in a
* cookie per browser, and devnull's API keeps nothing, so the app keeps it on
* this phone, per user.
*/
final class NotificationFilter
{
/**
* The web's kinds, in the web's order: the label, a line for the settings screen, and what the list says when
* only that kind is on and there's nothing of it.
*
* @var array<string, array{0: string, 1: string, 2: string}>
*/
public const array Kinds = [
'replies' => ['Replies', 'Someone replies to one of your commits.', 'No replies yet. When someone replies to one of your commits, it shows up here.'],
'quotes' => ['Quotes', 'Someone quotes one of your commits.', 'No quotes yet. When someone quotes one of your commits, it shows up here.'],
'stars' => ['Stars', 'Someone stars one of your commits.', 'No stars yet. When someone stars one of your commits, it shows up here.'],
'mentions' => ['Mentions', 'Someone mentions you in a commit.', 'No mentions yet. When someone mentions you in a commit, it shows up here.'],
'follows' => ['Follows', 'Someone follows you.', 'No followers yet. When someone follows you, it shows up here.'],
'moderation' => ['Moderation', 'Approved, rejected, and timeout notices.', 'No moderation updates. Approved, rejected, and timeout notices show up here.'],
];

/**
* What the list says with every kind on and nothing in it.
*/
public const string AllEmpty = 'Nothing here yet. Follows, stars, replies, quotes, mentions, level-ups, and moderation updates for your commits will show up here.';

/**
* The kinds that are on for this user, in the web's order. Everything is on until they turn something off.
*
* @return list<string>
*/
public function enabled(?string $userId): array
{
$kept = $userId === null ? null : Cache::get($this->key($userId));

return is_array($kept) ? self::only($kept) : array_keys(self::Kinds);
}

/**
* @param list<string> $kinds
*/
public function save(?string $userId, array $kinds): void
{
if ($userId !== null) {
Cache::forever($this->key($userId), self::only($kinds));
}
}

/**
* Turns one kind on or off, and says what's on now.
*
* @return list<string>
*/
public function set(?string $userId, string $kind, bool $on): array
{
$enabled = $this->enabled($userId);
$kinds = $on ? [...$enabled, $kind] : array_values(array_diff($enabled, [$kind]));

$this->save($userId, $kinds);

return $this->enabled($userId);
}

/**
* What devnull is asked for, as the web asks: `all`, `none`, or the kinds that are on, comma separated.
*
* @param list<string> $kinds
*/
public static function type(array $kinds): string
{
$kinds = self::only($kinds);

return match (true) {
count($kinds) === count(self::Kinds) => 'all',
$kinds === [] => 'none',
default => implode(',', $kinds),
};
}

/**
* The web's empty state for what's on: its own words for one kind or all of them, and plainer ones otherwise.
*
* @param list<string> $kinds
*/
public static function emptyMessage(array $kinds): string
{
return match (true) {
$kinds === [] => 'All categories are turned off. Enable a category to see notifications.',
count($kinds) === count(self::Kinds) => self::AllEmpty,
count($kinds) === 1 => self::Kinds[$kinds[0]][2],
default => 'No notifications in the selected categories yet.',
};
}

/**
* The shell line over the empty state, as the web prints it.
*
* @param list<string> $kinds
*/
public static function command(array $kinds): string
{
return count($kinds) === count(self::Kinds)
? '$ cat /dev/notifications'
: "\$ cat /dev/notifications | grep -E '".($kinds === [] ? '^$' : implode('|', $kinds))."'";
}

/**
* Known kinds only, once each, in the web's order.
*
* @param array<mixed> $kinds
* @return list<string>
*/
private static function only(array $kinds): array
{
return array_values(array_filter(array_keys(self::Kinds), fn (string $kind): bool => in_array($kind, $kinds, true)));
}

private function key(string $userId): string
{
return "devnull.notification-kinds.{$userId}";
}
}
4 changes: 4 additions & 0 deletions app/Icons/Lucide.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum Lucide
case ChevronLeft;
case ChevronDown;
case ChevronsUp;
case Hourglass;
case ArrowUpRight;
case ArrowUp;
case Pin;
Expand Down Expand Up @@ -126,6 +127,8 @@ public static function forNotification(string $type): self
'mentioned' => self::AtSign,
'approved' => self::Check,
'rejected' => self::X,
'timed_out' => self::Hourglass,
'first_commit' => self::GitCommitVertical,
default => self::MessageSquareReply,
};
}
Expand Down Expand Up @@ -178,6 +181,7 @@ public function custom(bool $filled = false): string
self::ArrowUpRight => 'arrow-up-right',
self::ArrowUp => 'arrow-up',
self::ChevronsUp => 'chevrons-up',
self::Hourglass => 'hourglass',
self::ListOrdered => 'list-ordered',
self::Building2 => 'building-2',
self::MapPin => 'map-pin',
Expand Down
50 changes: 50 additions & 0 deletions app/NativeComponents/NotificationSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\NativeComponents;

use App\Devnull\NotificationFilter;
use App\NativeComponents\Concerns\TalksToDevnull;
use Illuminate\View\View;
use Native\Mobile\Edge\NativeComponent;

/**
* Which kinds of notification the Notifications tab shows, one switch each,
* as the web's filters choose them. A switch saves as soon as it flips, as a
* filter does on the web, and the list follows when you go back to it.
*
* The web keeps the choice in a cookie in your browser and devnull's API
* keeps none, so the app keeps it on this phone, per user.
*/
class NotificationSettings extends NativeComponent
{
use TalksToDevnull;

/** @var list<string> */
public array $kinds = [];

public function mount(): void
{
$this->kinds = app(NotificationFilter::class)->enabled($this->me()?->id);
}

public function navTitle(): string
{
return 'Notification settings';
}

public function setKind(string $kind, bool $on): void
{
if (! array_key_exists($kind, NotificationFilter::Kinds)) {
return;
}

$this->kinds = app(NotificationFilter::class)->set($this->me()?->id, $kind, $on);
}

public function render(): View
{
return view('native.notification-settings', [
'options' => NotificationFilter::Kinds,
]);
}
}
Loading
Loading