English
Checkpoint
What must work
powershell
dotnet buildpowershell
dotnet format Chatex.slnx --verify-no-changespowershell
dotnet test --solution Chatex.slnxThe business layer is complete. Both projects must now be identical to the reference:
powershell
git diff --no-index --stat Source/Chatex.Core D:\Work\Chatex-Reference\Source\Chatex.Corepowershell
git diff --no-index --stat Tests/Chatex.Core.Tests D:\Work\Chatex-Reference\Tests\Chatex.Core.TestsOnly the folders bin and obj may appear in the output.
Commit:
powershell
git add .powershell
git commit -m "Add messages and attachments; complete the business layer"Module 3 is complete: the business layer implements every rule of the messenger and is covered by 41 tests.
Common mistakes
Table 11.1. Common mistakes in the message and attachment services
| Symptom | Cause | Fix |
|---|---|---|
| the chat does not move to the top after a message | TouchAsync is not called, or is called with the wrong time | call it inside the transaction with message.CreatedAt |
InvalidOperationException about user-initiated transactions | a transaction is begun outside ExecuteInTransactionAsync | use the Unit of Work |
| attachments stay unlinked after a retried send | the attachments are loaded only outside the transaction lambda | load them again inside the lambda, as the reference does |
MessageCreated is raised for a message that failed | the notifier is called inside the transaction | call it after the transaction returned |
| the edit test fails by a few hundred nanoseconds | the time is compared exactly | compare with a tolerance of one millisecond |
| files remain in storage after a refused upload | the empty or too-large file is not deleted | delete it in each failure branch |
| a non-member can download a sent file | the download check uses membership of the uploader, not visibility of the chat | check GetVisibleAsync for the message's chat |
Self-check questions
- Why are attachments uploaded before the message is sent? What problems does this solve, and what new problem does it create?
- Why is the list of allowed media types an allow-list rather than a block-list? Which types are missing on purpose?
- Which size does Chatex store for an attachment, and why not the length the client sent?
- Who may download an attachment, and why is everybody else told "not found"?
- What happens in the transaction of
SendAsync? Why must the attachments be loaded again inside the lambda? - Why is the notifier called only after the transaction?
- Why may only the sender edit a message, while admins may delete it?
- What remains of a deleted message, and why?
- Why is deleting a deleted message a success?
- Why does
GetHistoryAsyncuse visibility instead of membership?
Exercises
- Reply previews. Clients would like to show a quote of the replied message.
MessageDtohas onlyReplyToMessageId. Design the change: which DTO gets which new properties, which repository method must load what, and what is shown when the original was deleted? - Edit window. Telegram allows editing for 48 hours. Add the rule to
EditAsyncwith a new erroredit_window_closed, and write the test with the fake clock. (Do it on a branch: the reference has no such rule.) - The cleanup age.
CleanupStaleAsynctakes the age as a parameter. Write a test that proves an upload of 23 hours survives a cleanup with 24 hours.