Comments 0.2.0: Attachments and voice notes in comments
Comments 0.2.0 lets people attach images, video, audio and other files to a comment, next to its text or a Giphy GIF, and record a voice note straight from the comment form.
Attachments
Files go through PhoenixKit’s storage, so they get the same bucket redundancy and image variants as other media on the site. A comment can now be just text, just a GIF, just attachments, or any mix of them. The files are linked to the comment in a new table, phoenix_kit_comment_media, in the order they were added.
Attachments are off until an admin turns them on. The comments settings page at /admin/settings/comments has a new Attachments section with three settings:
- Enable Comment Attachments, the master switch, off by default.
- Max attachments per comment, from 1 to 10, 4 by default.
- Max file size (MB), from 1 to 500, and never above the site’s global upload limit.
Voice notes
With attachments on, the comment form has a Record button. It records from the microphone in the browser and sends the recording through the same upload queue as dropped files, so a voice note is simply an audio attachment.
For developers
create_comment/4 takes :attachment_file_uuids, a list of storage file uuids to attach in display order; the comment and its attachments are saved in one transaction. New functions manage attachments directly: attach_media/3, detach_media/2, detach_media_by_uuid/1 and list_comment_media/2, plus attachments_enabled?/0, get_max_attachments/0 and get_max_attachment_size_mb/0 to read the settings.
Upgrading
The new table comes from PhoenixKit core’s migration V113. Update phoenix_kit to a version that includes it, then run mix ecto.migrate. Nothing changes on a live site until an admin enables attachments.
Comments 0.2.0. Install or update: {:phoenix_kit_comments, "~> 0.2"}, then mix deps.get && mix phoenix_kit.update. Full history: CHANGELOG.