|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const _compact = require('lodash/compact') |
| 4 | +const _flatten = require('lodash/flatten') |
| 5 | +const numberValidator = require('./validators/number') |
| 6 | +const dateValidator = require('./validators/date') |
| 7 | +const stringValidator = require('./validators/string') |
| 8 | +const Model = require('./model') |
| 9 | + |
| 10 | +const fields = { |
| 11 | + id: 0, |
| 12 | + mts: 1, |
| 13 | + // placeholder |
| 14 | + userID: 3, |
| 15 | + // placeholder |
| 16 | + title: 5, |
| 17 | + content: 6, |
| 18 | + // placeholder |
| 19 | + // placeholder |
| 20 | + isPin: 9, |
| 21 | + isPublic: 10, |
| 22 | + // placeholder |
| 23 | + tags: 12, |
| 24 | + attachments: 13, |
| 25 | + // placeholder |
| 26 | + likes: 15, |
| 27 | + userLiked: 16 |
| 28 | + // placeholder |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Private PulseMessage model |
| 33 | + */ |
| 34 | +class PulseMessage extends Model { |
| 35 | + /** |
| 36 | + * @param {object|Array} data - pulse message data |
| 37 | + * @param {string} data.id - pulse message ID |
| 38 | + * @param {number} data.mts - millisecond timestamp |
| 39 | + * @param {string} data.userID - pulse User ID |
| 40 | + * @param {string} data.title - title of the pulse message |
| 41 | + * @param {string} data.content - content of the pulse message |
| 42 | + * @param {number} data.isPin - 1 if the message is pinned, 0 if it is not pinned |
| 43 | + * @param {number} data.isPublic - 1 if the message is public, 0 if it is not public |
| 44 | + * @param {string} data.tags - tags used in the message |
| 45 | + * @param {string} data.attachments - attachments used in the message |
| 46 | + * @param {number} data.likes - number of likes |
| 47 | + * @param {number} data.userLiked - flag to show if the private user liked the pulse |
| 48 | + */ |
| 49 | + constructor (data = {}) { |
| 50 | + super({ data, fields }) |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param {object[]|object|Array[]|Array} data - data to convert to POJO |
| 55 | + * @returns {object} pojo |
| 56 | + */ |
| 57 | + static unserialize (data) { |
| 58 | + return super.unserialize({ data, fields }) |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @returns {string} str |
| 63 | + */ |
| 64 | + toString () { |
| 65 | + const { |
| 66 | + id, mts, userID, title, content, isPin, isPublic, tags, attachments, likes, userLiked |
| 67 | + } = this |
| 68 | + |
| 69 | + return _compact(_flatten([ |
| 70 | + `(${id})`, |
| 71 | + new Date(mts).toLocaleString(), |
| 72 | + userID, |
| 73 | + title, |
| 74 | + content, |
| 75 | + !isPin ? 'not pinned' : 'pinned', |
| 76 | + !isPublic ? 'not public' : 'public', |
| 77 | + tags, |
| 78 | + attachments, |
| 79 | + likes && `likes(${likes})`, |
| 80 | + userLiked |
| 81 | + ])).join(' ') |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Validates a given public pulse profile instance |
| 86 | + * |
| 87 | + * @param {object[]|object|PulseMessage[]|PulseMessage|Array} data - instance to validate |
| 88 | + * @returns {string} error - null if instance is valid |
| 89 | + */ |
| 90 | + static validate (data) { |
| 91 | + return super.validate({ |
| 92 | + data, |
| 93 | + fields, |
| 94 | + validators: { |
| 95 | + id: stringValidator, |
| 96 | + mts: dateValidator, |
| 97 | + userID: stringValidator, |
| 98 | + title: stringValidator, |
| 99 | + content: stringValidator, |
| 100 | + isPin: numberValidator, |
| 101 | + isPublic: numberValidator, |
| 102 | + tags: stringValidator, |
| 103 | + attachments: stringValidator, |
| 104 | + likes: numberValidator, |
| 105 | + userLiked: numberValidator |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +module.exports = PulseMessage |
0 commit comments