openvibers/openvibe.mediahub Commits

A picture alone is a comment on a video (#426)

OP OpenVibers committed 19 h ago · f7afa268e6 · 2 files, +24 −2

The video page's comment form refused a picture with no words; words are now optional when a file is attached (the owner, 11-12). Community takes it as of OpenVibe.Community #412.

Changes

ddiff --git a/server/comments/form.js b/server/comments/form.js
iindex 85d5f7a..305132d 100644
---- a/server/comments/form.js
++++ b/server/comments/form.js
@@@ -12,10 +12,11 @@ async function readCommentForm(req) {
const request = new Request('http://localhost/comment', { method: 'POST', headers: { 'content-type': req.get('content-type') }, body: req.body });
const form = await request.formData();
const message = String(form.get('message') || '').trim();
-- if (!message || message.length > 2000) throw new Error('A comment is 1 to 2,000 characters');
++ const files = form.getAll('files').filter((f) => f && typeof f.arrayBuffer === 'function' && f.size);
++ // A picture alone is a comment, as on any social app (the owner, 11-12): words are optional with a file attached.
++ if ((!message && !files.length) || message.length > 2000) throw new Error('Write something or attach a picture (up to 2,000 characters)');
const parent = String(form.get('parent_id') || '');
if (parent && !ID.test(parent)) throw new Error('That reply target is not valid');
-- const files = form.getAll('files').filter((f) => f && typeof f.arrayBuffer === 'function' && f.size);
if (files.length > 4) throw new Error('Attach up to four files');
for (const file of files) {
if (file.size > MAX_FILE) throw new Error('A file can be up to 16 MB');
ddiff --git a/test/comment-form.test.js b/test/comment-form.test.js
nnew file mode 100644
iindex 0000000..5ea2430
---- /dev/null
++++ b/test/comment-form.test.js
@@@ -0,0 +1,21 @@
++'use strict';
++// The video page's comment form: a picture alone is a comment (the owner, 11-12), words alone are, nothing is not.
++const assert = require('assert');
++const { readCommentForm } = require('../server/comments/form');
++async function reqOf(fields) {
++ const fd = new FormData();
++ for (const [k, v] of fields) fd.append(k, v);
++ const r = new Request('http://x/', { method: 'POST', body: fd });
++ const body = Buffer.from(await r.arrayBuffer());
++ return { body, get: (h) => (h.toLowerCase() === 'content-type' ? r.headers.get('content-type') : null) };
++}
++(async () => {
++ const pic = new Blob([Buffer.from('png bytes')], { type: 'image/png' });
++ const onlyPic = await readCommentForm(await reqOf([['message', ''], ['files', new File([pic], 'a.png', { type: 'image/png' })]]));
++ assert.deepStrictEqual([onlyPic.message, onlyPic.files.length], ['', 1], 'a picture with no caption');
++ const words = await readCommentForm(await reqOf([['message', ' nice ']]));
++ assert.deepStrictEqual([words.message, words.files.length], ['nice', 0]);
++ await assert.rejects(readCommentForm(await reqOf([['message', ' ']])), /Write something or attach a picture/);
++ await assert.rejects(readCommentForm(await reqOf([['message', 'x'.repeat(2001)]])), /up to 2,000 characters/);
++ console.log('comment form: a picture alone, words alone, never nothing');
++})().catch((e) => { console.error(e); process.exit(1); });