Skip to content
Merged
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
70 changes: 70 additions & 0 deletions app/Devnull/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Devnull\Data\Connection;
use App\Devnull\Data\CurrentUser;
use App\Devnull\Data\DraftImage;
use App\Devnull\Data\IncomingCall;
use App\Devnull\Data\MessageVoice;
use App\Devnull\Data\Notification;
use App\Devnull\Data\Page;
Expand Down Expand Up @@ -141,6 +142,14 @@ public static function threadPath(string $username = '', ?string $orgId = null):
return $orgId === null ? 'remotes/@'.rawurlencode($username) : 'remotes/orgs/'.rawurlencode($orgId);
}

/**
* Where a call with someone rings, is answered and ends, beside your thread with them.
*/
public static function callPath(string $username): string
{
return self::threadPath($username).'/call';
}

/**
* Where a message's reactions are left and taken back.
*/
Expand Down Expand Up @@ -600,6 +609,67 @@ public function unblock(string $username): void
$this->cache->blockChanged($this->json($this->send('DELETE', 'users/'.rawurlencode($username).'/block')));
}

/**
* The call ringing for you, as the web checks for one every few seconds, or null.
*/
public function incomingCall(): ?IncomingCall
{
$call = $this->json($this->send('GET', 'remotes/call'))['call'] ?? null;

return is_array($call) ? IncomingCall::fromArray($call) : null;
}

/**
* Fresh relay credentials for one call with them, which devnull only hands out for someone you can call.
*
* @return array{ice_servers: list<array{urls: list<string>, username: string, credential: string}>, is_relay_only: bool}
*/
public function callIceServers(string $username): array
{
$json = $this->json($this->send('GET', self::callPath($username).'/ice-servers'));
/** @var list<array{urls: list<string>, username: string, credential: string}> $servers */
$servers = array_values(array_filter((array) ($json['ice_servers'] ?? []), is_array(...)));

return ['ice_servers' => $servers, 'is_relay_only' => ($json['is_relay_only'] ?? true) !== false];
}

/**
* Rings them with this phone's offer, for 45 seconds.
*/
public function ring(string $username, string $offer): void
{
$this->send('POST', self::callPath($username), payload: ['sdp' => $offer]);
}

/**
* Your call to them while it rings: null once it's gone, with their answer once they pick up.
*
* @return array{answer: string|null}|null
*/
public function outgoingCall(string $username): ?array
{
$call = $this->json($this->send('GET', self::callPath($username)))['call'] ?? null;

return is_array($call) ? ['answer' => is_string($call['answer'] ?? null) ? $call['answer'] : null] : null;
}

/**
* Picks up their call with this phone's answer.
*/
public function answerCall(string $username, string $answer): void
{
$this->send('PUT', self::callPath($username), payload: ['sdp' => $answer]);
}

/**
* Ends the call between you, whoever rang. Not picking up one that rang logs it declined, or failed when this
* phone tried to answer and couldn't.
*/
public function endCall(string $username, bool $failed = false): void
{
$this->send('DELETE', self::callPath($username), payload: ['failed' => $failed]);
}

/**
* The people you mute, last muted first.
*
Expand Down
28 changes: 28 additions & 0 deletions app/Devnull/Data/IncomingCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Devnull\Data;

/**
* A call ringing for you: who's calling, the offer to answer it with, and whether their thread is muted, which
* rings it silently.
*/
final readonly class IncomingCall
{
public function __construct(
public User $partner,
public string $offer,
public bool $isMuted,
) {}

/**
* @param array<string, mixed> $data
*/
public static function fromArray(array $data): self
{
return new self(
partner: User::fromArray(is_array($data['partner'] ?? null) ? $data['partner'] : []),
offer: is_string($data['sdp'] ?? null) ? $data['sdp'] : '',
isMuted: ($data['is_muted'] ?? false) === true,
);
}
}
17 changes: 12 additions & 5 deletions app/Devnull/Data/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ public function hasReacted(?string $viewerId, string $emoji): bool
* name in an org for someone else's, "sent a voice note" or "sent an image" for a message with no text, and the
* event for an org's log line.
*/
public function preview(?string $viewerId, bool $inOrg): string
public function preview(?string $viewerId, bool $inOrg, ?User $partner = null): string
{
if ($this->isEvent()) {
return $this->eventText($viewerId);
return $this->eventText($viewerId, partner: $partner);
}

$text = trim((string) preg_replace('/\s+/', ' ', $this->body));
Expand All @@ -246,11 +246,12 @@ public function preview(?string $viewerId, bool $inOrg): string

/**
* An event, as the web logs it between messages: who did it, and to whom. In a thread with one person devnull
* leaves the author off a ping, since it can only be them.
* leaves the author off a ping or a call, since it can only be them or the partner.
*/
public function eventText(?string $viewerId, ?string $orgName = null): string
public function eventText(?string $viewerId, ?string $orgName = null, ?User $partner = null): string
{
$actor = $this->userId === $viewerId ? 'you' : ($this->user === null ? 'someone' : $this->user->handle());
$author = $this->user ?? $partner;
$actor = $this->userId === $viewerId ? 'you' : ($author === null ? 'someone' : $author->handle());
$subject = $this->subjectId === $viewerId ? 'you' : ($this->subject === null ? 'someone' : $this->subject->handle());

return match ($this->event) {
Expand All @@ -262,6 +263,12 @@ public function eventText(?string $viewerId, ?string $orgName = null): string
'promoted' => "{$actor} made {$subject} a maintainer",
'demoted' => "{$actor} made {$subject} a contributor",
'inherited' => "{$actor} inherited maintainer",
'call_started' => "{$actor} started a call",
'call_answered' => "{$actor} answered the call",
'call_cancelled' => "{$actor} cancelled the call",
'call_declined' => "{$actor} declined the call",
'call_failed' => "{$actor} couldn't answer the call",
'call_missed' => $this->userId === $viewerId ? "your call wasn't answered" : "missed call from {$actor}",
self::Ping => match (true) {
$this->userId === $viewerId => 'you sent a ping',
$this->user !== null => "{$actor} sent a ping",
Expand Down
6 changes: 6 additions & 0 deletions app/Icons/Lucide.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ enum Lucide
case RotateCw;
case Square;
case Copy;
case Phone;
case PhoneOff;
case MicOff;

/**
* The icon the web's inbox draws for a kind of notification.
Expand Down Expand Up @@ -227,6 +230,9 @@ public function custom(bool $filled = false): ?string
self::RotateCw => 'rotate-cw',
self::Square => 'square-filled',
self::Copy => 'copy',
self::Phone => 'phone',
self::PhoneOff => 'phone-off',
self::MicOff => 'mic-off',
default => null,
};

Expand Down
Loading
Loading