Skip to content

Commit

Permalink
enriching pulse message model with ability to display public profile
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel1984 authored and JacobPlaster committed May 20, 2020
1 parent ae0ba3b commit 52b91b7
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 9 deletions.
29 changes: 27 additions & 2 deletions lib/pulse_message.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

const _compact = require('lodash/compact')
const _flatten = require('lodash/flatten')
const _isArray = require('lodash/isArray')
const numberValidator = require('./validators/number')
const dateValidator = require('./validators/date')
const stringValidator = require('./validators/string')
const PublicPulseProfile = require('./public_pulse_profile')
const Model = require('./model')

const fields = {
Expand All @@ -24,8 +26,9 @@ const fields = {
attachments: 13,
// placeholder
likes: 15,
userLiked: 16
userLiked: 16,
// placeholder
pulseProfile: 18
}

/**
Expand Down Expand Up @@ -55,7 +58,29 @@ class PulseMessage extends Model {
* @returns {object} pojo
*/
static unserialize (data) {
return super.unserialize({ data, fields })
const pojo = super.unserialize({ data, fields })

if (pojo.isPublic && _isArray(pojo.pulseProfile)) {
pojo.pulseProfile = PublicPulseProfile.unserialize(pojo.pulseProfile)
}

return pojo
}

/**
* Return an array representation of this model
*
* @returns {Array} arr
*/
serialize () {
const arr = super.serialize()

if (this.isPublic && this.pulseProfile) {
const profile = new PublicPulseProfile(this.pulseProfile)
arr[fields.pulseProfile] = profile.serialize()
}

return arr
}

/**
Expand Down
81 changes: 74 additions & 7 deletions test/lib/models/pulse_message.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ describe('Pulse Message model', () => {
null,
'likes',
'userLiked',
null
null,
'pulseProfile'
]
})

Expand Down Expand Up @@ -69,7 +70,19 @@ describe('Pulse Message model', () => {
null,
1,
2,
null
null,
[
'foo',
12345,
null,
'foo',
null,
'bar.jpg',
'baz',
null,
null,
'qux'
]
])

assert.strictEqual(pm.id, 'foo')
Expand All @@ -83,6 +96,18 @@ describe('Pulse Message model', () => {
assert.deepEqual(pm.attachments, ['foo', 'bar'])
assert.strictEqual(pm.likes, 1)
assert.strictEqual(pm.userLiked, 2)
assert.deepEqual(pm.pulseProfile, [
'foo',
12345,
null,
'foo',
null,
'bar.jpg',
'baz',
null,
null,
'qux'
])
})

it('serializes correctly', () => {
Expand All @@ -104,11 +129,25 @@ describe('Pulse Message model', () => {
null,
1,
2,
null
null,
[
'foo',
12345,
null,
'foo',
null,
'bar.jpg',
'baz',
null,
null,
'qux'
]
])

const arr = _compact(pm.serialize())
assert.deepStrictEqual(arr, [
const arr = pm.serialize()
arr[pm._fields.pulseProfile] = _compact(arr[pm._fields.pulseProfile])

assert.deepStrictEqual(_compact(arr), [
'foo',
12345,
'bar',
Expand All @@ -119,7 +158,15 @@ describe('Pulse Message model', () => {
['#hash', '#tag'],
['foo', 'bar'],
1,
2
2,
[
'foo',
12345,
'foo',
'bar.jpg',
'baz',
'qux'
]
])
})

Expand All @@ -142,7 +189,19 @@ describe('Pulse Message model', () => {
null,
1,
2,
null
null,
[
'foo',
12345,
null,
'foo',
null,
'bar.jpg',
'baz',
null,
null,
'qux'
]
])

assert.strictEqual(obj.id, 'foo')
Expand All @@ -156,6 +215,14 @@ describe('Pulse Message model', () => {
assert.deepEqual(obj.attachments, ['foo', 'bar'])
assert.strictEqual(obj.likes, 1)
assert.strictEqual(obj.userLiked, 2)
assert.deepEqual(obj.pulseProfile, {
id: 'foo',
mtsCreate: 12345,
nickname: 'foo',
picture: 'bar.jpg',
text: 'baz',
twitterHandle: 'qux'
})
})

describe('toString', () => {
Expand Down

0 comments on commit 52b91b7

Please sign in to comment.