diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index a3c033a43..ca1b8ba90 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -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; diff --git a/app/Actions/CreateCommit.php b/app/Actions/CreateCommit.php index eda2c2f22..1757682ac 100644 --- a/app/Actions/CreateCommit.php +++ b/app/Actions/CreateCommit.php @@ -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, diff --git a/app/Http/Controllers/Api/CommitPreviewController.php b/app/Http/Controllers/Api/CommitPreviewController.php index 20e1a07ee..6848925aa 100644 --- a/app/Http/Controllers/Api/CommitPreviewController.php +++ b/app/Http/Controllers/Api/CommitPreviewController.php @@ -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()), ]]); } diff --git a/app/Http/Requests/ShowApiOrganizationRequest.php b/app/Http/Requests/ShowApiOrganizationRequest.php index 670e9ed24..b776a1a27 100644 --- a/app/Http/Requests/ShowApiOrganizationRequest.php +++ b/app/Http/Requests/ShowApiOrganizationRequest.php @@ -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)); diff --git a/app/Http/Resources/CommitResource.php b/app/Http/Resources/CommitResource.php index b20286609..f531915c8 100644 --- a/app/Http/Resources/CommitResource.php +++ b/app/Http/Resources/CommitResource.php @@ -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 ?? []), 'status' => $this->status, 'is_pinned' => $this->author?->user?->pinned_commit_id === $this->id, 'starred' => $this->whenHas('starred'), diff --git a/app/Http/Resources/MessageResource.php b/app/Http/Resources/MessageResource.php index 37bb893d1..28fac1ab8 100644 --- a/app/Http/Resources/MessageResource.php +++ b/app/Http/Resources/MessageResource.php @@ -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, @@ -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 => [ diff --git a/app/Services/Mentions.php b/app/Services/Mentions.php index 07dbba4e8..4a2d39c74 100644 --- a/app/Services/Mentions.php +++ b/app/Services/Mentions.php @@ -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 $organizations - * @return list + * @param array $users + * @return list */ - public function positions(string $text, array $organizations = []): array + public function positions(string $text, array $organizations = [], array $users = []): array { $positions = []; $offset = 0; @@ -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, ]; } } diff --git a/routes/api.php b/routes/api.php index 9a0422ab6..552a98959 100644 --- a/routes/api.php +++ b/routes/api.php @@ -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... diff --git a/tests/Feature/Controllers/Api/CommitControllerTest.php b/tests/Feature/Controllers/Api/CommitControllerTest.php index 8863ac514..2d0348dda 100644 --- a/tests/Feature/Controllers/Api/CommitControllerTest.php +++ b/tests/Feature/Controllers/Api/CommitControllerTest.php @@ -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, diff --git a/tests/Feature/Controllers/Api/ConversationMessageControllerTest.php b/tests/Feature/Controllers/Api/ConversationMessageControllerTest.php index 55e4fdfef..27841234a 100644 --- a/tests/Feature/Controllers/Api/ConversationMessageControllerTest.php +++ b/tests/Feature/Controllers/Api/ConversationMessageControllerTest.php @@ -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) diff --git a/tests/Feature/Controllers/Api/LogControllerTest.php b/tests/Feature/Controllers/Api/LogControllerTest.php index 682edb311..a4093538e 100644 --- a/tests/Feature/Controllers/Api/LogControllerTest.php +++ b/tests/Feature/Controllers/Api/LogControllerTest.php @@ -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, diff --git a/tests/Feature/OrganizationMentionsTest.php b/tests/Feature/OrganizationMentionsTest.php index c3f580d43..293a5b854 100644 --- a/tests/Feature/OrganizationMentionsTest.php +++ b/tests/Feature/OrganizationMentionsTest.php @@ -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]]); }); diff --git a/tests/Feature/UserMentionsTest.php b/tests/Feature/UserMentionsTest.php new file mode 100644 index 000000000..5cf02414b --- /dev/null +++ b/tests/Feature/UserMentionsTest.php @@ -0,0 +1,101 @@ +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(); +}); diff --git a/tests/Unit/Services/MentionsTest.php b/tests/Unit/Services/MentionsTest.php index 9ef2e274b..afb084913 100644 --- a/tests/Unit/Services/MentionsTest.php +++ b/tests/Unit/Services/MentionsTest.php @@ -58,9 +58,9 @@ $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], ]); }); @@ -68,7 +68,7 @@ $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], ]); }); @@ -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], ]); });