Skip to content

Commit 52b91b7

Browse files
Daniel1984JacobPlaster
authored andcommitted
enriching pulse message model with ability to display public profile
1 parent ae0ba3b commit 52b91b7

2 files changed

Lines changed: 101 additions & 9 deletions

File tree

lib/pulse_message.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
const _compact = require('lodash/compact')
44
const _flatten = require('lodash/flatten')
5+
const _isArray = require('lodash/isArray')
56
const numberValidator = require('./validators/number')
67
const dateValidator = require('./validators/date')
78
const stringValidator = require('./validators/string')
9+
const PublicPulseProfile = require('./public_pulse_profile')
810
const Model = require('./model')
911

1012
const fields = {
@@ -24,8 +26,9 @@ const fields = {
2426
attachments: 13,
2527
// placeholder
2628
likes: 15,
27-
userLiked: 16
29+
userLiked: 16,
2830
// placeholder
31+
pulseProfile: 18
2932
}
3033

3134
/**
@@ -55,7 +58,29 @@ class PulseMessage extends Model {
5558
* @returns {object} pojo
5659
*/
5760
static unserialize (data) {
58-
return super.unserialize({ data, fields })
61+
const pojo = super.unserialize({ data, fields })
62+
63+
if (pojo.isPublic && _isArray(pojo.pulseProfile)) {
64+
pojo.pulseProfile = PublicPulseProfile.unserialize(pojo.pulseProfile)
65+
}
66+
67+
return pojo
68+
}
69+
70+
/**
71+
* Return an array representation of this model
72+
*
73+
* @returns {Array} arr
74+
*/
75+
serialize () {
76+
const arr = super.serialize()
77+
78+
if (this.isPublic && this.pulseProfile) {
79+
const profile = new PublicPulseProfile(this.pulseProfile)
80+
arr[fields.pulseProfile] = profile.serialize()
81+
}
82+
83+
return arr
5984
}
6085

6186
/**

test/lib/models/pulse_message.js

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ describe('Pulse Message model', () => {
2929
null,
3030
'likes',
3131
'userLiked',
32-
null
32+
null,
33+
'pulseProfile'
3334
]
3435
})
3536

@@ -69,7 +70,19 @@ describe('Pulse Message model', () => {
6970
null,
7071
1,
7172
2,
72-
null
73+
null,
74+
[
75+
'foo',
76+
12345,
77+
null,
78+
'foo',
79+
null,
80+
'bar.jpg',
81+
'baz',
82+
null,
83+
null,
84+
'qux'
85+
]
7386
])
7487

7588
assert.strictEqual(pm.id, 'foo')
@@ -83,6 +96,18 @@ describe('Pulse Message model', () => {
8396
assert.deepEqual(pm.attachments, ['foo', 'bar'])
8497
assert.strictEqual(pm.likes, 1)
8598
assert.strictEqual(pm.userLiked, 2)
99+
assert.deepEqual(pm.pulseProfile, [
100+
'foo',
101+
12345,
102+
null,
103+
'foo',
104+
null,
105+
'bar.jpg',
106+
'baz',
107+
null,
108+
null,
109+
'qux'
110+
])
86111
})
87112

88113
it('serializes correctly', () => {
@@ -104,11 +129,25 @@ describe('Pulse Message model', () => {
104129
null,
105130
1,
106131
2,
107-
null
132+
null,
133+
[
134+
'foo',
135+
12345,
136+
null,
137+
'foo',
138+
null,
139+
'bar.jpg',
140+
'baz',
141+
null,
142+
null,
143+
'qux'
144+
]
108145
])
109146

110-
const arr = _compact(pm.serialize())
111-
assert.deepStrictEqual(arr, [
147+
const arr = pm.serialize()
148+
arr[pm._fields.pulseProfile] = _compact(arr[pm._fields.pulseProfile])
149+
150+
assert.deepStrictEqual(_compact(arr), [
112151
'foo',
113152
12345,
114153
'bar',
@@ -119,7 +158,15 @@ describe('Pulse Message model', () => {
119158
['#hash', '#tag'],
120159
['foo', 'bar'],
121160
1,
122-
2
161+
2,
162+
[
163+
'foo',
164+
12345,
165+
'foo',
166+
'bar.jpg',
167+
'baz',
168+
'qux'
169+
]
123170
])
124171
})
125172

@@ -142,7 +189,19 @@ describe('Pulse Message model', () => {
142189
null,
143190
1,
144191
2,
145-
null
192+
null,
193+
[
194+
'foo',
195+
12345,
196+
null,
197+
'foo',
198+
null,
199+
'bar.jpg',
200+
'baz',
201+
null,
202+
null,
203+
'qux'
204+
]
146205
])
147206

148207
assert.strictEqual(obj.id, 'foo')
@@ -156,6 +215,14 @@ describe('Pulse Message model', () => {
156215
assert.deepEqual(obj.attachments, ['foo', 'bar'])
157216
assert.strictEqual(obj.likes, 1)
158217
assert.strictEqual(obj.userLiked, 2)
218+
assert.deepEqual(obj.pulseProfile, {
219+
id: 'foo',
220+
mtsCreate: 12345,
221+
nickname: 'foo',
222+
picture: 'bar.jpg',
223+
text: 'baz',
224+
twitterHandle: 'qux'
225+
})
159226
})
160227

161228
describe('toString', () => {

0 commit comments

Comments
 (0)