Repository navigation
feat: edit remote messages within three minutes #1552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PacketByte
wants to merge
4
commits into
dev-null-sh:production
Choose a base branch
from
PacketByte:feat/edit-remote-messages
base: production
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
56b56a3
feat: edit remote messages within three minutes
PacketByte 1d5fb17
fix: address review on message editing
PacketByte 78b85a2
fix: image-only editor and overlapping-edit mention race
PacketByte abc738e
fix: refresh the message after the edit lock
PacketByte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Actions; | ||
|
|
||
| use App\Enums\UserUpdate; | ||
| use App\Events\UsersUpdated; | ||
| use App\Jobs\UnfurlLink; | ||
| use App\Models\ConversationParticipant; | ||
| use App\Models\LinkPreview; | ||
| use App\Models\Message; | ||
| use App\Queries\MentionedUsersQuery; | ||
| use App\Services\EmojiShortcodes; | ||
| use App\Services\Urls; | ||
| use Illuminate\Support\Facades\DB; | ||
|
|
||
| final readonly class UpdateMessage | ||
| { | ||
| public function __construct( | ||
| private EmojiShortcodes $emojiShortcodes, | ||
| private MentionedUsersQuery $mentionedUsers, | ||
| private Urls $urls, | ||
| ) { | ||
| // | ||
| } | ||
|
|
||
| /** | ||
| * Rewords a message within its edit window: the body, mentions, and link card are recomputed the way pushing | ||
| * computes them, everything else stays, and the thread hears about it so the partner sees the new wording live. | ||
| */ | ||
| public function handle(Message $message, string $body): Message | ||
| { | ||
| $url = $this->urls->first($body); | ||
| $sender = $message->user; | ||
|
|
||
| DB::transaction(function () use ($message, $body, $url, $sender): void { | ||
| // Read under a lock so overlapping edits serialize: the second one diffs against the first one's | ||
| // committed mentions instead of a stale snapshot, and no badge is left behind. | ||
| $previousMentions = array_values(Message::query()->whereKey($message->id)->lockForUpdate()->value('mentions') ?? []); | ||
| $message->refresh(); | ||
|
|
||
| $linkPreview = $url === null ? null : LinkPreview::query()->firstOrCreate(['url_hash' => hash('sha256', $url)], ['url' => $url]); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| $message->update([ | ||
| 'body' => $this->emojiShortcodes->replace($body), | ||
| 'mentions' => $this->mentionedUsers->get($body), | ||
| 'link_preview_id' => $linkPreview?->id, | ||
| 'edited_at' => now(), | ||
| ]); | ||
|
|
||
| if ($linkPreview instanceof LinkPreview && $linkPreview->fetched_at === null) { | ||
| dispatch(new UnfurlLink($linkPreview))->afterCommit(); | ||
| } | ||
|
|
||
| // Mention badges live on a pivot, not in the JSON. Badges are only added for newly mentioned | ||
| // recipients and removed for deleted mentions, so a badge the recipient cleared stays cleared. | ||
| if ($message->conversation->isOrg()) { | ||
| $currentMentions = array_values($message->mentions ?? []); | ||
|
|
||
| $message->mentionedUsers()->detach(array_diff($previousMentions, $currentMentions)); | ||
|
|
||
| $added = array_diff($currentMentions, $previousMentions); | ||
|
|
||
| if ($added !== []) { | ||
| $message->mentionedUsers()->attach($message->conversation->participants() | ||
| ->whereIn('user_id', $added) | ||
| ->where('user_id', '!=', $sender->id) | ||
| ->where('visible_from_id', '<', $message->id) | ||
| ->whereNotIn('user_id', $sender->idsAcrossBlocks()) | ||
| ->whereNotIn('user_id', $sender->mutedBy()->newPivotQuery()->select('muter_id')) | ||
| ->pluck('user_id')); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| event(new UsersUpdated(ConversationParticipant::query()->where('conversation_id', $message->conversation_id)->pluck('user_id'), UserUpdate::Messages, ['conversation' => $message->conversation_id])); | ||
|
|
||
| return $message->refresh()->load(Message::SHOWN_WITH); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use App\Enums\PushAccess; | ||
| use App\Models\Conversation; | ||
| use App\Models\Message; | ||
| use App\Models\User; | ||
| use App\Queries\PushAccessQuery; | ||
| use Illuminate\Auth\Access\Response; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| final class UpdateConversationMessageRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Only the author may reword their own message, only within its edit window, and only where both halves can | ||
| * already see it. Editing pushes to the conversation, so it also takes what pushing does: no blocks, and a merged | ||
| * request. Anything else reads as missing, except an expired window or an unmerged request, which say so. | ||
| */ | ||
| public static function authorization(User $user, Message $message, User $partner, PushAccessQuery $access): Response | ||
| { | ||
| $conversation = Conversation::between($user, $partner); | ||
|
|
||
| if (! $user->remotes_enabled || $message->event !== null || ! $message->isVisibleTo($user) || $conversation === null || $message->conversation_id !== $conversation->id) { | ||
| return Response::denyAsNotFound(); | ||
| } | ||
|
|
||
| if ($message->user_id !== $user->id) { | ||
| return Response::denyAsNotFound(); | ||
| } | ||
|
|
||
| if ($message->voice !== null) { | ||
| return Response::deny('Voice notes cannot be edited.'); | ||
| } | ||
|
|
||
| if ($message->editExpired()) { | ||
| return Response::deny('Messages can only be edited for 3 minutes after pushing.'); | ||
| } | ||
|
|
||
| if (! $user->canMessage($partner)) { | ||
| return Response::denyAsNotFound(); | ||
| } | ||
|
|
||
| return $access->get($user, $partner) === PushAccess::Open | ||
| ? Response::allow() | ||
| : Response::deny("You can't edit until the request is merged."); | ||
| } | ||
|
|
||
| public function authorize(PushAccessQuery $access): Response | ||
| { | ||
| $user = $this->user(); | ||
| assert($user instanceof User); | ||
|
|
||
| $partner = $this->route('partner'); | ||
| assert($partner instanceof User); | ||
|
|
||
| $message = $this->route('message'); | ||
| assert($message instanceof Message); | ||
|
|
||
| return self::authorization($user, $message, $partner, $access); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, array<int, string>> | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| // A caption can be cleared down to nothing, but only where images remain to hold the message up. | ||
| $message = $this->route('message'); | ||
| $bodyRequired = ! ($message instanceof Message && $message->images()->exists()); | ||
|
|
||
| return [ | ||
| 'body' => [$bodyRequired ? 'required' : 'nullable', 'string', 'max:'.Message::MAX_LENGTH], | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use App\Enums\OrgRole; | ||
| use App\Models\Conversation; | ||
| use App\Models\Message; | ||
| use App\Models\User; | ||
| use Illuminate\Auth\Access\Response; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| final class UpdateOrgMessageRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Only the author's own message, only within its edit window, and only where they still belong. | ||
| */ | ||
| public static function authorization(User $user, Message $message, Conversation $org): Response | ||
| { | ||
| return match (true) { | ||
| ! $user->remotes_enabled || $message->event !== null || ! $message->isVisibleTo($user) || $message->conversation_id !== $org->id || ! ($org->roleOf($user) instanceof OrgRole) => Response::denyAsNotFound(), | ||
| $message->user_id !== $user->id => Response::denyAsNotFound(), | ||
| $message->voice !== null => Response::deny('Voice notes cannot be edited.'), | ||
| $message->editExpired() => Response::deny('Messages can only be edited for 3 minutes after pushing.'), | ||
| default => Response::allow(), | ||
| }; | ||
| } | ||
|
|
||
| public function authorize(): Response | ||
| { | ||
| $user = $this->user(); | ||
| assert($user instanceof User); | ||
|
|
||
| $org = $this->route('org'); | ||
| assert($org instanceof Conversation); | ||
|
|
||
| $message = $this->route('message'); | ||
| assert($message instanceof Message); | ||
|
|
||
| return self::authorization($user, $message, $org); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, array<int, string>> | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| // A caption can be cleared down to nothing, but only where images remain to hold the message up. | ||
| $message = $this->route('message'); | ||
| $bodyRequired = ! ($message instanceof Message && $message->images()->exists()); | ||
|
|
||
| return [ | ||
| 'body' => [$bodyRequired ? 'required' : 'nullable', 'string', 'max:'.Message::MAX_LENGTH], | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
database/migrations/2026_11_13_120000_add_edited_at_to_messages_table.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| public function up(): void | ||
| { | ||
| Schema::table('messages', function (Blueprint $table): void { | ||
| $table->timestamp('edited_at')->nullable()->after('pinned_at'); | ||
| }); | ||
| } | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.