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
1 change: 1 addition & 0 deletions KNOWLEDGE_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ A signed request sees what the web shows you, such as your own pending commits a

- read your account, logs, replies, notifications (including grouped activity), and starred commits;
- read the organizations you are a member of, your role in each, and whether posting as an organization is switched on;
- open the person or organization a mention in a commit or message captured, by the id each mention carries, as the web's mention links do, so it still leads to them after they rename and someone else takes the handle;
- commit, reply, and quote, with images, and delete your commits, with the same undo window as the web;
- preview commit drafts without publishing them;
- request a feature or report a bug as `/feature` and `/bug` do, with the same limit of 10 opened issues a day;
Expand Down
4 changes: 3 additions & 1 deletion app/Actions/CreateCommit.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ public function handle(User $user, string $body, ?Commit $parent = null, ?Commit
'author_id' => $organization->id ?? $user->id,
'guild_id' => $guild?->id,
'body' => $this->emojiShortcodes->replace($body),
'mentions' => [...$replyRecipients, ...$mentions],
// Keyed by handle, which PHP makes an integer for a handle of digits, like "123", so a spread would
// renumber it and link the mention to someone else...
'mentions' => array_replace($replyRecipients, $mentions),
'parent_id' => $parent?->id,
'root_id' => $parent?->threadRootId(),
'status' => $user->isAdmin() || $hasGoodReputation ? CommitStatus::Approved : CommitStatus::Pending,
Expand Down
3 changes: 2 additions & 1 deletion app/Http/Controllers/Api/CommitPreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
public function store(CreateApiCommitPreviewRequest $request, EmojiShortcodes $emojiShortcodes, CommitBodies $commitBodies, Mentions $mentions, MentionedUsersQuery $mentionedUsers, MentionedOrganizationsQuery $mentionedOrganizations): JsonResponse
{
$body = $emojiShortcodes->replace($request->string('body')->value());
$users = $mentionedUsers->get($body);

return response()->json(['data' => [
'body' => $body,
...$commitBodies->segments($body),
'mentions' => $mentions->positions($body, $mentionedOrganizations->get($body, $mentionedUsers->get($body))),
'mentions' => $mentions->positions($body, $mentionedOrganizations->get($body, $users), $users),
'guild_mentions' => ShownGuildMentions::get([$body], $request->user()),
]]);
}
Expand Down
9 changes: 9 additions & 0 deletions app/Http/Requests/ShowApiOrganizationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,18 @@ public function rules(): array

/**
* Only organizations that already have a page, so looking one up never asks GitHub.
*
* In a scenario where an organization was renamed on github and devnull, a mention opens
* the organization based on the captured id, not based on the old username.
*/
public function organization(): ?Organization
{
$id = $this->route('organization');

if (is_string($id)) {
return $this->organization ??= Organization::query()->find($id);
}

$handle = $this->route('handle');
assert(is_string($handle));

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Resources/CommitResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function toArray(Request $request): array
$shown = [
...$commit,
...resolve(CommitBodies::class)->segments($this->body),
'mentions' => resolve(Mentions::class)->positions($this->body, $this->organization_mentions ?? []),
'mentions' => resolve(Mentions::class)->positions($this->body, $this->organization_mentions ?? [], $this->mentions ?? []),
Comment thread
shanesmithdev marked this conversation as resolved.
Resolved
'status' => $this->status,
'is_pinned' => $this->author?->user?->pinned_commit_id === $this->id,
'starred' => $this->whenHas('starred'),
Expand Down
5 changes: 2 additions & 3 deletions app/Http/Resources/MessageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;

/**
* A message as the web's thread shows it: its body, also split into prose, inline code and highlighted code blocks,
Expand Down Expand Up @@ -47,8 +46,8 @@ public function toArray(Request $request): array
'client_id' => $this->client_id,
'body' => $this->body,
'body_segments' => resolve(CodeSpans::class)->highlight($this->body),
'mentions' => collect(resolve(Mentions::class)->positions($this->body))
->filter(fn (array $mention): bool => array_key_exists(Str::lower($mention['handle']), $this->mentions ?? []))
'mentions' => collect(resolve(Mentions::class)->positions($this->body, users: $this->mentions ?? []))
->filter(fn (array $mention): bool => $mention['id'] !== null)
->values(),
'reply_to_id' => $this->when(! in_array('reply_to_id', $this->getHidden(), true), $this->reply_to_id),
'reply_to' => $this->whenLoaded('replyTo', fn (Message $replyTo): array => [
Expand Down
15 changes: 12 additions & 3 deletions app/Services/Mentions.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,15 @@ public function handles(string $text): array
* the whole text and include the "@". The kind says whether it mentions one of the given organizations, by
* lowercase handle, or anyone else.
*
* The id is the organization's or user's id which the mention captured, by lowercase handle, as the web links it,
* so it still leads to them once they rename and someone else takes the handle. It is null for a handle that was
* nobody's on /dev/null.
*
* @param array<string, string> $organizations
* @return list<array{handle: string, offset: int, length: int, kind: 'user'|'organization'}>
* @param array<string, string> $users
* @return list<array{handle: string, offset: int, length: int, kind: 'user'|'organization', id: string|null}>
*/
public function positions(string $text, array $organizations = []): array
public function positions(string $text, array $organizations = [], array $users = []): array
{
$positions = [];
$offset = 0;
Expand All @@ -52,11 +57,15 @@ public function positions(string $text, array $organizations = []): array
preg_match_all(self::Pattern, $part, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE);

foreach ($matches as $match) {
$handle = mb_strtolower($match[1][0]);
$isOrganization = array_key_exists($handle, $organizations);

$positions[] = [
'handle' => $match[1][0],
'offset' => $offset + mb_strlen(mb_strcut($part, 0, $match[0][1])),
'length' => mb_strlen($match[0][0]),
'kind' => array_key_exists(mb_strtolower($match[1][0]), $organizations) ? 'organization' : 'user',
'kind' => $isOrganization ? 'organization' : 'user',
'id' => $isOrganization ? $organizations[$handle] : $users[$handle] ?? null,
];
}
}
Expand Down
9 changes: 9 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@
->name('api-organization-commit.index');
Route::get('organizations/{handle}/mentions', [OrganizationMentionController::class, 'index'])
->name('api-organization-mention.index');

// Mentioned profiles: the user or organization a commit's or message's mention captured, by the id it carries,
// as the web's mention links open them, so they still lead there once they rename...
Route::get('mentions/{user}', [UserController::class, 'show'])
->whereUuid('user')
->name('api-mentioned-user.show');
Route::get('mentions/organizations/{organization}', [OrganizationController::class, 'show'])
->whereUuid('organization')
->name('api-mentioned-organization.show');
});

// Signing in: the app redeems the code it got on the web, signed with a key that isn't approved yet...
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Controllers/Api/CommitControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@
['type' => 'code', 'code' => '@Override'],
],
'preview_segments' => null,
'mentions' => [['handle' => 'nunomaduro', 'offset' => 4, 'length' => 11, 'kind' => 'user']],
'mentions' => [['handle' => 'nunomaduro', 'offset' => 4, 'length' => 11, 'kind' => 'user', 'id' => null]],
'status' => 'approved',
'is_pinned' => false,
'starred' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
['type' => 'code', 'code' => 'the PR'],
['type' => 'text', 'text' => ', @ghost? https://laravel.com'],
])
->assertJsonPath('data.messages.0.mentions', [['handle' => 'octocat', 'offset' => 4, 'length' => 8, 'kind' => 'user']])
->assertJsonPath('data.messages.0.mentions', [['handle' => 'octocat', 'offset' => 4, 'length' => 8, 'kind' => 'user', 'id' => $this->user->id]])
->assertJsonPath('data.messages.0.link_preview.url', 'https://laravel.com')
->assertJsonPath('data.messages.0.link_preview.is_pending', true)
->assertJsonPath('data.messages.0.reply_to', null)
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Controllers/Api/LogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
->assertJsonPath('data.0.stars_count', 2)
->assertJsonPath('data.0.starred', true)
->assertJsonPath('data.0.is_pinned', true)
->assertJsonPath('data.0.mentions', [['handle' => 'NunoMaduro', 'offset' => 4, 'length' => 11, 'kind' => 'user']])
->assertJsonPath('data.0.mentions', [['handle' => 'NunoMaduro', 'offset' => 4, 'length' => 11, 'kind' => 'user', 'id' => null]])
->assertJsonPath('data.0.link_preview', [
'url' => 'https://laravel.com',
'title' => $linkPreview->title,
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/OrganizationMentionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@
signedApiRequest($keyPair, 'GET', route('api-log.show', 'latest'))
->assertOk()
->assertJsonPath('data.0.mentions', [
['handle' => 'laravel', 'offset' => 0, 'length' => 8, 'kind' => 'organization'],
['handle' => 'tom', 'offset' => 13, 'length' => 4, 'kind' => 'user'],
['handle' => 'laravel', 'offset' => 0, 'length' => 8, 'kind' => 'organization', 'id' => $organization->id],
['handle' => 'tom', 'offset' => 13, 'length' => 4, 'kind' => 'user', 'id' => null],
]);

signedApiRequest($keyPair, 'POST', route('api-commit-preview.store'), ['body' => 'Hi @Laravel'])
->assertOk()
->assertJsonPath('data.mentions', [['handle' => 'Laravel', 'offset' => 3, 'length' => 8, 'kind' => 'organization']]);
->assertJsonPath('data.mentions', [['handle' => 'Laravel', 'offset' => 3, 'length' => 8, 'kind' => 'organization', 'id' => $organization->id]]);
});
101 changes: 101 additions & 0 deletions tests/Feature/UserMentionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

use App\Actions\CreateCommitTopics;
use App\Models\Commit;
use App\Models\Organization;
use App\Models\User;

beforeEach(function (): void {
$this->viewer = User::factory()->create();
$this->keyPair = apiKeyPairFor($this->viewer);
});

it('keeps an API mention on the user it captured after they rename and someone else takes the handle', function (): void {
$original = User::factory()->create(['handle' => 'oldname']);

signedApiRequest($this->keyPair, 'POST', route('api-commit.store'), ['body' => 'Thanks @OldName'])->assertCreated();

$original->profile->update(['handle' => 'newname']);
$newcomer = User::factory()->create(['handle' => 'oldname']);

signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'latest'))
->assertOk()
->assertJsonPath('data.0.mentions', [['handle' => 'OldName', 'offset' => 7, 'length' => 8, 'kind' => 'user', 'id' => $original->id]]);

signedApiRequest($this->keyPair, 'GET', route('api-mentioned-user.show', $original->id))
->assertOk()
->assertJsonPath('data.id', $original->id)
->assertJsonPath('data.handle', 'newname');

expect($newcomer->id)->not->toBe($original->id);
});

it('keeps each mention of a handle made of digits on the user it captured', function (): void {
$hundred = User::factory()->create(['handle' => '123']);
$zero = User::factory()->create(['handle' => '0']);

$id = signedApiRequest($this->keyPair, 'POST', route('api-commit.store'), ['body' => '@123 @0'])->assertCreated()->json('data.id');

signedApiRequest($this->keyPair, 'GET', route('api-commit.show', $id))
->assertOk()
->assertJsonPath('data.mentions.*.id', [$hundred->id, $zero->id]);
});

it('gives a preview the ids its mentions would capture', function (): void {
$nuno = User::factory()->create(['handle' => 'nunomaduro']);
$laravel = Organization::factory()->create(['handle' => 'laravel']);

signedApiRequest($this->keyPair, 'POST', route('api-commit-preview.store'), ['body' => '@NunoMaduro @laravel @ghost'])
->assertOk()
->assertJsonPath('data.mentions.*.id', [$nuno->id, $laravel->id, null]);
});

it('opens a mentioned user by id as their profile does by handle, signed or not', function (): void {
$user = User::factory()->create(['handle' => 'nunomaduro']);

$byHandle = signedApiRequest($this->keyPair, 'GET', route('api-user.show', 'nunomaduro'))->assertOk()->json();

signedApiRequest($this->keyPair, 'GET', route('api-mentioned-user.show', $user->id))
->assertOk()
->assertExactJson($byHandle);

$this->getJson(route('api-mentioned-user.show', $user->id))
->assertOk()
->assertExactJson($this->getJson(route('api-user.show', 'nunomaduro'))->json());
});

it('answers a mentioned user across a block as their profile does', function (): void {
$user = User::factory()->create(['handle' => 'nunomaduro']);
$this->viewer->blocking()->attach($user);

$byHandle = signedApiRequest($this->keyPair, 'GET', route('api-user.show', 'nunomaduro'));

signedApiRequest($this->keyPair, 'GET', route('api-mentioned-user.show', $user->id))
->assertStatus($byHandle->status())
->assertExactJson($byHandle->json());
});

it('finds no mentioned user that is gone, or an id that is not one', function (): void {
$this->getJson(route('api-mentioned-user.show', '019a0000-0000-7000-8000-000000000000'))->assertNotFound();
$this->getJson('/api/v1/mentions/nunomaduro')->assertNotFound();
});

it('opens a mentioned organization by id after it is renamed, as its page does by handle', function (): void {
$organization = Organization::factory()->create(['handle' => 'laravel']);
$commit = Commit::factory()->create(['body' => 'Thanks @laravel']);
resolve(CreateCommitTopics::class)->handle($commit, ['laravel' => $organization->id]);
$organization->profile->update(['handle' => 'laravel-org']);

$byHandle = signedApiRequest($this->keyPair, 'GET', route('api-organization.show', 'laravel-org'))->assertOk()->json();

signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'latest'))
->assertJsonPath('data.0.mentions.0.id', $organization->id);

signedApiRequest($this->keyPair, 'GET', route('api-mentioned-organization.show', $organization->id))
->assertOk()
->assertExactJson($byHandle);

$this->getJson(route('api-mentioned-organization.show', '019a0000-0000-7000-8000-000000000000'))->assertNotFound();
});
24 changes: 17 additions & 7 deletions tests/Unit/Services/MentionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@
$mentions = resolve(Mentions::class);

expect($mentions->positions('@NunoMaduro and @taylorotwell, thanks! cc @nunomaduro'))->toBe([
['handle' => 'NunoMaduro', 'offset' => 0, 'length' => 11, 'kind' => 'user'],
['handle' => 'taylorotwell', 'offset' => 16, 'length' => 13, 'kind' => 'user'],
['handle' => 'nunomaduro', 'offset' => 42, 'length' => 11, 'kind' => 'user'],
['handle' => 'NunoMaduro', 'offset' => 0, 'length' => 11, 'kind' => 'user', 'id' => null],
['handle' => 'taylorotwell', 'offset' => 16, 'length' => 13, 'kind' => 'user', 'id' => null],
['handle' => 'nunomaduro', 'offset' => 42, 'length' => 11, 'kind' => 'user', 'id' => null],
]);
});

it('counts offsets in characters, not bytes', function (): void {
$mentions = resolve(Mentions::class);

expect($mentions->positions('Café 🎉 with @nuno.'))->toBe([
['handle' => 'nuno', 'offset' => 12, 'length' => 5, 'kind' => 'user'],
['handle' => 'nuno', 'offset' => 12, 'length' => 5, 'kind' => 'user', 'id' => null],
]);
});

Expand All @@ -82,8 +82,18 @@
$mentions = resolve(Mentions::class);

expect($mentions->positions("thanks @taylorotwell for `@Override`, @nuno\n```java\n@Deprecated\nvoid run() {}\n```\ncc @jess-archer"))->toBe([
['handle' => 'taylorotwell', 'offset' => 7, 'length' => 13, 'kind' => 'user'],
['handle' => 'nuno', 'offset' => 38, 'length' => 5, 'kind' => 'user'],
['handle' => 'jess-archer', 'offset' => 85, 'length' => 12, 'kind' => 'user'],
['handle' => 'taylorotwell', 'offset' => 7, 'length' => 13, 'kind' => 'user', 'id' => null],
['handle' => 'nuno', 'offset' => 38, 'length' => 5, 'kind' => 'user', 'id' => null],
['handle' => 'jess-archer', 'offset' => 85, 'length' => 12, 'kind' => 'user', 'id' => null],
]);
});

it('gives each mention the id of the user or organization it captured, whatever its case, and null for anyone else', function (): void {
$mentions = resolve(Mentions::class);

expect($mentions->positions('@Laravel @NunoMaduro @ghost', ['laravel' => 'organization-id'], ['nunomaduro' => 'user-id', 'replied-to' => 'other-id']))->toBe([
['handle' => 'Laravel', 'offset' => 0, 'length' => 8, 'kind' => 'organization', 'id' => 'organization-id'],
['handle' => 'NunoMaduro', 'offset' => 9, 'length' => 11, 'kind' => 'user', 'id' => 'user-id'],
['handle' => 'ghost', 'offset' => 21, 'length' => 6, 'kind' => 'user', 'id' => null],
]);
});
Loading