Skip to content

Group chat and DMs: S3 fixes, reactions, cleaner push text, no client SENDs to /topic (combines #293, #295-#299, #302) - #314

Open
svaka2000 wants to merge 43 commits into
Open-Coding-Society:masterfrom
svaka2000:combined/group-chat
Open

svaka2000 wants to merge 43 commits into
Open-Coding-Society:masterfrom
svaka2000:combined/group-chat

Conversation

@svaka2000

@svaka2000 svaka2000 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

This puts my seven open Spring chat PRs (#293, #295-#299 and #302) into one, plus four group chat fixes I hadn't opened yet. Mostly it stops group chat from losing or wiping messages when S3 has a problem, and it adds reactions. I merged each branch as it was, so all the original commits are here, plus two small fixes for #302's deletes on top. I'm closing the seven with a link here.

Merge order and deploy:

What students see:

  • When S3 is unavailable (throttling, down, too slow, or its RequestTimeout), a send, edit, delete or file share answers 503 with Retry-After: 5, and nothing goes out to the group. Before, a send could wipe the chat, and a change S3 never took still answered 200.
  • Deleting a message that isn't in the group's file answers 404 instead of taking it off everyone's screen.
  • With no S3 settings, like a local server, sends still work live, and edits and deletes answer 404 since nothing is saved.

"Strict" below means the ...OrThrow S3 calls, which throw when S3 fails instead of returning null or false.

#296 Group chat: a lock per group so two sends at once can't lose a message

  • Two sends, edits or deletes at once both started from the same copy of the group's messages file, so the second upload erased the first one's change. GroupChatService now holds a lock per group, so they take turns. It only covers one Spring server.

#297 Group chat: a send returns 503 instead of wiping the history when S3 can't read it

  • The send read with decodeFile, which returns null for any failure, so it uploaded just the new message over the real file. Now it reads with the strict decodeFileOrThrow, and only S3's 404 means there are no messages yet.

#298 Group chat: a restart no longer wipes a chat when S3 fails during the start-up check

  • When Spring starts, StartupService writes an empty messages file for any group without one, and a failed check counted as missing. Now only a 404 makes it write one, and any other failure is logged and that group is checked again on the next start.

#299 Group chat files: 503 instead of 500 when S3 can't store the file

  • Shared files upload with the strict uploadFileOrThrow. S3's RequestTimeout (a 400 for a request that was too slow) now counts as unavailable too, here and in a send's read. SharedFileCheckFailedException from Group chat files: 503 instead of 500 when S3 can't check a path #281 is now SharedFileStorageUnavailableException, since the upload throws it too.

Four group chat fixes I hadn't opened yet

  • Saves used uploadFile, which returns null on failure, so a send, edit or delete that S3 never took still answered 200. Now saves are strict and edits and deletes read strictly too, so S3 being unavailable is a 503. S3 refusing the save, or a bug, gets the route's usual 400.
  • Trade-off: S3 can store the file and still time out answering. That's a 503 now too, so sending again shows the message twice. Before, a save that really failed still answered 200 and the message was gone after a reload, so a duplicate you can see and delete seemed better.
  • A send or edit answers with what it saved instead of reading the file again, which answered an empty list when that read failed.
  • Deleting a message that isn't in the group's file answers 404 and goes out to no one. Before, it skipped the sender check and still told every open chat to remove it, so anyone signed in could hide a message until a reload: any message on a server with no S3 settings, and on the live site when S3's read failed. Nothing was deleted from S3.
  • A save keeps lines that aren't valid message JSON instead of dropping them, and a message that can't be turned into JSON fails with the usual 400 instead of being saved as {}.

#302 Chat reactions: react to a DM or group message with an emoji

  • Six reactions: like, love, haha, wow, sad and celebrate. PUT and DELETE on .../messages/{messageId}/reactions/{reaction} add and take back yours, and GET .../reactions lists a chat's, under /api/dm/conversations/{id} (only the DM's two people, 403 otherwise) and /api/groups/chat/{groupId} (any signed-in account, like posting). All six requests are in permissions.json.
  • Changes go out live on the chat's socket topic, with no phone notification and nothing made unread.
  • A delete removes the message's reactions only after the delete really happened, so a delete that answers 404 changes nothing. If the database can't remove them (the table is missing, say), the delete still goes through: a group's is still broadcast, and a DM's no longer rolls back, since its reactions are now removed after it commits. The error is logged.

#293 Phone notifications: show a DM's words, not its markers or HTML

  • It used to show the raw text, with the [[…]] markers the Messages page uses for replies, code and pins, and the rich text box's HTML. Now it shows just the words. Group messages don't send phone notifications.

#295 Chat socket: drop client SENDs to every /topic destination

  • The broker passed anything a client sent to /topic/... straight to every subscriber, skipping our REST checks. Chat socket: only a DM's participants can subscribe to its topic #286 blocked that for DM topics, and this blocks every /topic. Only the server sends there now, which is how messages and reactions go out live. A dropped SEND gets no error, just a warning in the log, and no page sends to /topic itself.

Other notes:

How to try it:

  1. Run ./mvnw test -Dtest='GroupChat*Test,StartupServiceTest,S3FileHandler*Test,*Reaction*Test,PushNotification*Test,DirectMessage*Test,PermissionsPolicyTest'.
  2. Start Spring with S3 settings and run the requests in api/groups.http in order with VS Code's REST Client, with baseUrl, adminUid and adminPassword set in .vscode/settings.json. Request 9 sends a message, 13g adds a reaction to a sample message id, and 13h lists the group's reactions. The S3 failures need S3 to fail, so the tests in step 1 cover those.

A send reads the group's whole messages file, adds the message and writes
the file back. decodeFile answers null for any failure, not only a missing
file, so when S3 timed out, was throttled or answered a 5xx, the send took
the chat for empty and wrote the one new message over the whole history. It
still answered 200 and broadcast the message.

The send now reads with a strict decodeFileOrThrow, like fileExistsOrThrow:
only S3's 404 means no messages yet. A timeout, no connection or a 5xx
throws MessagesReadFailedException, nothing is saved or broadcast, and
POST /api/groups/chat/{id}/messages answers 503 with Retry-After, the same
as a shared-file check that fails. S3 refusing the read (a 4xx) or a bug
here also writes nothing and gets the route's usual 400, since waiting
won't fix it. S3 failures to read are logged with their cause. Without S3
settings a send still broadcasts live, as before. decodeFile, edits and
deletes are unchanged.

Tests send through the controller to a real S3FileHandler over a stand-in
S3 client: a 503 and a timeout keep the stored file byte for byte and
answer 503, a 403 and a bug write nothing and get no retry, a missing file
starts the history, and a server without S3 still broadcasts. The two older
tests that stub S3 now stub the strict read a send makes.
Sending, editing and deleting a message each read the group's whole messages
file from S3, change it and write the file back, with no lock. Two at once
both read the same old copy, so the later write dropped the other's change:
two people sending at the same moment lost a message, and an edit or delete
during a send lost the edit or the new message. They now take one lock per
group, the same pattern as the shared-files metadata lock. Reads don't wait,
and other groups don't wait on each other.

The lock only works within one server. Several servers writing the same file
would need S3's conditional writes instead.

Tests run two sends, an edit and a send, and a delete and a send at once
against an S3 stub that holds each read until the other one has started.
The send route and the storage notes now say a send reads the messages
file with decodeFileOrThrow: S3 being unavailable answers 503 with
Retry-After and nothing is saved or broadcast, S3 refusing the read or a
bug gets the route's usual 400, and edits and deletes still read with
decodeFile. The testing list gets a bullet
for GroupChatSendTest and S3FileHandlerDecodeTest, with the command that
runs the S3 handler tests.
The storage notes said every send rewrites the file with no lock, and Known
issues listed two sends at once losing one. The note now says sending,
editing and deleting take one lock per group, within one server only. The
testing list names GroupChatServiceTest, and the known issue is gone.
…HTML

The push text was the stored body with its whitespace collapsed, so a phone
showed what the page reads: [[reply:…]], [[files:…]], [[code:…]] and
[[pin:…]] markers, and the rich composer's HTML (<a href=…>, &nbsp;). Now
the markers are left out, the HTML becomes the text a reader sees, and a
message that was only a marker says what it did ("pinned a message",
"sent code", "shared files"). A marker's payload can't hold a bracket, so a
body full of unclosed "[[" is still matched in linear time.

notifyPerson runs inside the message's transaction and never throws, so if
the text can't be made from a body, the notification goes out without text
and the DM is still sent.
PermissionsPolicyTest checks permissions.json against every request the
controllers answer. The reaction routes for direct messages and group
chats go with the other /api/dm and /api/groups/chat requests: any
signed-in admin, student, teacher or user, as the filter chain answers
today.
DirectMessageTopicGuard already dropped a client's SEND to a DM topic.
It now drops a client's SEND to any destination the simple broker
handles (WebSocketBrokerConfig.BROKER_PREFIX, "/topic"), so group chat
topics get the same rule. Only the server publishes to topics: pages
send group messages over REST, and Spring's group page sends to
/app/groups.chat.

The tests cover SENDs to DM, group and other topic destinations being
dropped, and the server's own group broadcast still reaching its
subscribers.
…se group ids

A remove read the row and then deleted it, so a second remove at the same
moment could fail. Removing one reaction and clearing a deleted message's
reactions are now single delete statements. The names of the people who
reacted come from one query for id, uid and name, instead of whole Person
rows that also loaded each person's eager links. A group message id has to
be a lowercase UUID, like the ids Spring saves, so an uppercase copy can't
match some lookups and miss others on MySQL.
…in then

The frame was sent inside the change's transaction with the reactions read
there, so two people reacting at the same moment could each send a list
missing the other's reaction, and everyone in the chat would keep one of
them. It is now sent once the transaction commits, with the reactions read
in a transaction of their own. A group's reactions were already read after
their change had committed.
Spring still starts, as it does when the calls or calendar tables can't
be made, but the log now says reactions won't work instead of a warning
that is easy to miss.
The group chat fixes (Open-Coding-Society#296 to Open-Coding-Society#299) each add a line after the chat
regression tests, so the reactions line goes above it and the two
change different places.
…or's types

GroupChatSendTest built GroupChatApiController, GroupChatRealtimeService and
GroupChatReadService by naming their constructors. Open PRs add constructor
dependencies to the controller and the realtime service (Open-Coding-Society#302 a
MessageReactionService, Open-Coding-Society#303 a MentorAccess), so once one of them and this
test were both on master, the tests would stop compiling, with no merge
conflict to show it.

TestBeans builds a class through its biggest constructor: each parameter gets
the first object the test passes that fits it, and a Mockito mock otherwise.
The test then keeps compiling and running whatever dependencies those classes
gain.
After the path check, the upload wrote the file with
S3FileHandler.uploadFile, which catches every failure and answers null. So
when S3 throttled the write, timed out or couldn't be reached, the upload got
a 500 "Upload failed", which reads as a bug in this server. Shared files now
write with uploadFileOrThrow, a strict version beside it, the way
fileExistsOrThrow sits beside fileExists. uploadFile is unchanged for the
other S3 features.

A timeout, no connection or a 5xx on the write now answers 503 with a
try-again message and Retry-After, like a failed check, and the file isn't
shared: no "shared by" record and no socket frame. S3 refusing the write (a
4xx such as no permission) or a bug here is still a 500 "Upload failed", and
every failed write is still logged.

S3's RequestTimeout counts as a timeout too, for the check and the write:
S3 sends it as a 400, but it means the connection was too slow, and the AWS
SDK retries it like a 5xx.

SharedFileCheckFailedException is now SharedFileStorageUnavailableException,
since a failed write throws it too, so the 503 is still answered in one
place. One check (isUnavailable) decides what counts as S3 being unavailable
for both the check and the write. The architecture doc says the same.

Tests cover a throttle, a timeout and a RequestTimeout on the write (503), an
S3 403, an S3 400 and a bug (500), sharing again once S3 is back, and
uploadFileOrThrow itself.
At every start, StartupService checks each group's messages file and
creates an empty one for a group that has none. The check used
fileExists, which reads any failure (a throttle, a 5xx, a timeout) as a
missing file, so a check that failed at boot wrote an empty file over
that group's whole history.

ensureGroupStorageExists now checks with fileExistsOrThrow: only S3's
404 creates the file, and any other failure throws before anything is
written. StartupService logs that group with the cause and goes on to
the next one, so the server still starts. The check holds the group's
message lock, so a message sent while the server is starting isn't
written over, and a server without S3 settings checks nothing
(S3FileHandler.isConfigured). The shared-files/ placeholder keeps the
lenient check: it is empty, so writing it again loses nothing.
S3's RequestTimeout comes as a 400, but it means the connection was too
slow, and the AWS SDK retries it like a 5xx. The shared-file check and write
already count it as S3 being unavailable (isUnavailable), but the send's
strict read had its own rule that only counted a 5xx, so a timed-out read got
the route's 400 instead of a 503 with Retry-After. The read now uses
isUnavailable too, so one check decides it for the whole group chat. The
history was kept either way: only the status was wrong.

The send test adds a RequestTimeout to the failures that answer 503.
The storage notes now say StartupService creates a group's empty
messages file and shared-files/ placeholder only when S3 answers 404
(fileExistsOrThrow). Any other failure to check is logged and leaves
that group as it is, the other groups are still checked, the check
holds the group's message lock, and a server without S3 settings
checks nothing. The testing list gets a bullet
for StartupServiceTest, with its command.
S3 can store a shared file and then fail to answer, and the AWS SDK's own
retries can fail too. The upload then answers 503 without a "shared by"
record or a sendFileServer frame, and sharing it again finds the path taken
(409), which the page handles like any other taken name. Nothing is lost,
and the old 500 ended the same way. Recognizing "this stored file is mine"
on a retry would need a way to match the stored copy to the upload, so for now
the doc and a comment at the write say what happens.
Sending, editing and deleting a message each read the group's messages file
and save it back. The save used S3FileHandler.uploadFile, which catches every
failure and answers null, so when S3 throttled, timed out or couldn't be
reached on the upload, the route still answered 200: a send showed live and
was gone after a reload, and an edit or a delete came back after one. Edits
and deletes also read with the lenient decodeFile, so a failed read answered
an edit with 404 and a delete with 200 (and told the group it was deleted).

All three now read with readMessagesOrThrow and save with a strict
writeMessagesOrThrow (uploadFileOrThrow). S3 being unavailable on either
(isUnavailable) throws MessagesStorageUnavailableException, the renamed
MessagesReadFailedException, and the send, edit and delete routes answer 503
with Retry-After. Nothing is broadcast. S3 refusing the request or a bug here
still gets the route's usual 400, and the strict read now logs every failure,
a bug included, the way the save does. Without S3 settings nothing is saved,
and the chat still works live.

A save that timed out may still be stored, so a send tried again can show up
twice. That's easier to see than a message lost quietly.

Tests: GroupChatSendTest adds an upload that fails after a good read, and
GroupChatEditDeleteTest covers edits and deletes whose read or upload fails,
that both still save when S3 is up, and that every failed read or upload is
logged. The S3 failure lists move to
S3Failures. GroupChatServiceTest, GroupChatMessageJsonTest and
GroupChatRegressionTest now stand in for a configured S3 through the strict
read and write; the regression test saves each upload back into its list, so
its checks on what's stored can still fail.
The send, edit and delete routes now list their 503 with Retry-After, and
that a save which timed out may still be stored. The
storage note says all three read and save the messages file strictly
(MessagesStorageUnavailableException), a new note says a save that timed out
may still be stored, so a send tried again can show up twice, and the testing
list names GroupChatEditDeleteTest.
After a send or an edit worked, the route read the group's messages file
again with the lenient getMessages and answered with that. If S3 failed on
that second read, decodeFile answered null and the route answered 200 with
an empty list. The Messages page uses that list to show the sender's own
message when its socket missed the frame, so they could see nothing and send
again. Every send and edit also read the same S3 file twice.

publishMessage now returns the list addMessage saved, and editMessage returns
the saved list, or nothing when no message has that id (still a 404). The
routes answer with that list and don't read the file again.

Tests: a send and an edit whose save works, with every S3 read after it
failing, answer with exactly what they saved.
…lls no one

Deleting a message that wasn't in the group's messages file returned quietly,
and the route still broadcast deleteMessageServer to the whole group and
answered 200. The ownership check only runs on a saved message, so any
signed-in account could take any message off everyone's screen by sending
its id. On a server without S3 settings, where nothing is saved, that worked
for every live message.

deleteMessage now returns whether a message with that id was removed. The
realtime service only broadcasts when it was, and the route answers 404
otherwise, like an edit of an unknown id.

Tests: a delete of an id that isn't saved, and any delete on a server
without S3 settings, answer 404 and broadcast nothing.
…le is saved

parseMessages skipped a line of the messages file that didn't read as a
message, and the next send, edit or delete wrote the file back without it,
so one bad line was gone for good. toJson answered "{}" for a message it
couldn't write, which replaced that message in the file with nothing.

A send, an edit and a delete now read the file as MessagesLine entries: a
message, or a line that didn't read as one, kept as it was. The save writes
each message as JSON and each unreadable line back in its place, and loading
the chat still leaves unreadable lines out. A message that can't be written
now throws, so the change is refused and the file stays as it was; the log
line names the message's id instead of printing its text.

Tests: GroupChatMessagesFileTest keeps an unreadable line in place through a
send, an edit and a delete, leaves it out when the chat loads, and refuses a
message that can't be written without touching the file.
The send and edit routes now say they return the history as saved, the edit route lists its 404, and the testing list names the new checks.
The delete route lists its 404, the note on servers without S3 settings says edits and deletes answer 404 there, the note on saves that time out says a retried delete answers 404, and the testing list names the new checks.
A new note under Persistence says a send, edit or delete writes an unreadable line back as it was, loading leaves it out, and a message that can't be written refuses the change instead of being saved as {}. The testing list names GroupChatMessagesFileTest.
… of wiping the history when S3 can't read it
…a chat when S3 fails during the start-up check
… message with an emoji

Where it meets the group chat fixes merged above:
- GroupChatRealtimeService: a delete removes the message's reactions only after the chat
  says it deleted the message, so deleting an id that isn't saved (404) changes nothing.
- GroupChatApiController: keeps both the 503 helper and the reaction routes.
- docs: keeps the send, edit and delete rows with their 503 and 404 notes, plus the three
  reaction rows.
- GroupChatReactionTest: the delete test now stubs the chat's delete, which reads S3 itself
  instead of calling getMessages, and a new test checks that a 404 delete keeps the reactions.
…y /topic destination

docs: Open-Coding-Society#295's rewrite of the WebSocket testing bullet, with Open-Coding-Society#302's reactions bullet kept
after it.
…e removed

By the time the reactions are removed, the message is already gone from S3. A database error
there answered an error and told no one, and a retry found nothing to delete (404). Now the
error is logged, the group still gets deleteMessageServer, and the leftover reactions are never
shown.
…be removed

DirectMessageService.deleteMessage removed the message's reactions inside its own transaction,
so any error from the reactions table, such as a missing message_reaction table under
ddl-auto=none, rolled back the delete and sent no "deleted" frame. The reactions are now removed
after the delete commits, and a failure there is logged. removeAllFor runs in a transaction of
its own (REQUIRES_NEW), since Spring runs a transactional call made from afterCommit inside the
finished transaction, where it would never commit. MessageReactionTableInitializer's comment now
says what breaks without the table.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant