stijnhau / messaging-bundle
Private messages between users for Symfony apps: inbox, sent box, read state, per-side deletion and moderation.
Package info
github.com/stijnhau/messaging-bundle
Type:symfony-bundle
pkg:composer/stijnhau/messaging-bundle
Requires
- php: >=8.4
- doctrine/doctrine-bundle: ^3.4
- doctrine/orm: ^3.8
- psr/clock: ^1.0
- psr/event-dispatcher: ^1.0
- symfony/clock: ^8.1
- symfony/config: ^8.1
- symfony/dependency-injection: ^8.1
- symfony/framework-bundle: ^8.1
- symfony/html-sanitizer: ^8.1
- symfony/http-kernel: ^8.1
- symfony/translation-contracts: ^3.5
Requires (Dev)
- phpunit/phpunit: ^13.4
- symfony/event-dispatcher: ^8.1
- symfony/translation: ^8.1
- symfony/yaml: ^8.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Private messages between users for Symfony apps: inbox and sent box, read state, each side deleting its own copy, and moderators removing a message. Shared by CityWars, Crime-Life and F1Manager.
| Piece | What it does |
|---|---|
Model\AbstractMessage |
Doctrine mapped superclass: sender, recipient, subject, body (sanitized HTML), sent/read, deleted by sender/recipient, removed, in reply to. The app maps a concrete subclass with any class and table name, and may add columns, indexes or #[ORM\AttributeOverrides]. |
Service\Mailbox |
send(), open() (marks it read for the recipient), delete() (one side's copy; purged once both deleted), remove() (moderation). |
Query\MailboxReader |
Inbox and sent box a page at a time, unread count, findVisible(). inboxQuery()/sentQuery() return a QueryBuilder you can narrow (a search) before page(). |
Service\ContentPolicy |
The subject/body checks plus sanitizing, for system messages the app writes itself. |
Block or ignore lists, mutes and rate limits are app-specific. They plug in as send guards. There's no ready-made UI: each app keeps its own pages on top of these services.
Install
composer require stijnhau/messaging-bundle
-
The user entity implements
Stijnhau\MessagingBundle\Model\MessageParticipantInterface(getId()). -
Map a message entity:
#[ORM\Entity] #[ORM\Index(name: 'idx_message_inbox', columns: ['recipient_id', 'sent_at'])] class Message extends AbstractMessage {}
-
Configure
config/packages/stijnhau_messaging.yaml:stijnhau_messaging: user_class: App\Entity\User message_class: App\Entity\Message per_page: 20 subject_max_length: 150 # at most 150, the column's length subject_required: true body_max_length: 4000 # length of the sanitized HTML purge_when_deleted_by_both: true sanitizer: ~ # an HtmlSanitizerInterface service id; default: safe elements
-
bin/console make:migration. The bundle ships no migrations.
Time comes from the clock service (symfony/clock), so a MockClock drives it in tests.
Hooks
- Who may send: implement
Security\SendGuardInterface(block lists, mutes, rate limits). Every implementation runs before each message. Throw to refuse, with aMessagingExceptionor your own exception. - Keeping deleted messages: implement
Security\MessageRetentionInterface. A message both sides deleted stays if any implementationmustKeep()it (for example, it was reported). - Events:
MessageSentEventis dispatched inside the transaction that saves the message, so a listener's writes (a notification, a Doctrine-transport Messenger message) commit with it. There are alsoMessageReadEvent,MessageDeletedEvent(with$purged) andMessageRemovedEvent. - Errors:
MessagingException::getKey()is a key in thestijnhau_messagingtranslation domain (en/nl/fr included).$e->trans($translator)gives the text.
Development
composer install vendor/bin/phpunit
License
MIT, see LICENSE.