|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const { PulseMessage } = require('bfx-api-node-models') |
| 4 | +const runExample = require('../util/run_example') |
| 5 | + |
| 6 | +module.exports = runExample({ |
| 7 | + name: 'rest-pulse', |
| 8 | + rest: { env: true, transform: true } |
| 9 | +}, async ({ debug, debugTable, rest }) => { |
| 10 | + debug('gettting pulse history..') |
| 11 | + const pulseHistRes = await rest.pulseHistory() |
| 12 | + |
| 13 | + debug('pulse history response') |
| 14 | + debugTable({ |
| 15 | + headers: [ |
| 16 | + 'PID', 'MTS', 'PUID', 'TITLE', 'CONTENT', 'COMMENTS' |
| 17 | + ], |
| 18 | + rows: pulseHistRes.map(({ id, mts, userID, title, content, comments }) => [ |
| 19 | + id, |
| 20 | + new Date(mts).toLocaleString(), |
| 21 | + userID, |
| 22 | + (title && title.substring(0, 15)) || '-', |
| 23 | + content.substring(0, 15), |
| 24 | + comments // number of comments |
| 25 | + ]) |
| 26 | + }) |
| 27 | + const pulseMsg = new PulseMessage({ |
| 28 | + title: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.', |
| 29 | + content: 'Contrary to popular belief, Lorem Ipsum is not simply random text.', |
| 30 | + isPublic: 0, |
| 31 | + isPin: 1 |
| 32 | + }) |
| 33 | + |
| 34 | + debug('submitting pulse message: %s', pulseMsg.toString()) |
| 35 | + |
| 36 | + let pulse |
| 37 | + try { |
| 38 | + pulse = await rest.addPulse(pulseMsg) |
| 39 | + } catch (e) { |
| 40 | + return debug('pulse message submittion failed: %s', e.message) |
| 41 | + } |
| 42 | + |
| 43 | + debug('pulse message submission response') |
| 44 | + debugTable({ |
| 45 | + headers: [ |
| 46 | + 'PID', 'MTS', 'PUID', 'TITLE', 'CONTENT' |
| 47 | + ], |
| 48 | + rows: [[ |
| 49 | + pulse.id, |
| 50 | + new Date(pulse.mts).toLocaleString(), |
| 51 | + pulse.userID, |
| 52 | + (pulse.title && pulse.title.substring(0, 15)) || '-', |
| 53 | + pulse.content.substring(0, 15) |
| 54 | + ]] |
| 55 | + }) |
| 56 | + |
| 57 | + const pulseComment = new PulseMessage({ |
| 58 | + parent: pulse.id, |
| 59 | + content: 'No more seven warlords of the sea', |
| 60 | + isPublic: 0, |
| 61 | + isPin: 1 |
| 62 | + }) |
| 63 | + |
| 64 | + debug('submitting pulse comment: %s', pulseComment.toString()) |
| 65 | + let comment |
| 66 | + try { |
| 67 | + comment = await rest.addPulseComment(pulseComment) |
| 68 | + } catch (e) { |
| 69 | + return debug('pulse comment submittion failed: %s', e.message) |
| 70 | + } |
| 71 | + |
| 72 | + debug('pulse comment submission response') |
| 73 | + debugTable({ |
| 74 | + headers: [ |
| 75 | + 'PID', 'MTS', 'PARENT', 'PUID', 'COMMENT' |
| 76 | + ], |
| 77 | + rows: [[ |
| 78 | + comment.id, |
| 79 | + new Date(comment.mts).toLocaleString(), |
| 80 | + comment.parent, |
| 81 | + comment.userID, |
| 82 | + comment.content.substring(0, 15) |
| 83 | + ]] |
| 84 | + }) |
| 85 | + |
| 86 | + debug('gettting pulse comments..') |
| 87 | + const pulseComments = await rest.fetchPulseComments({ |
| 88 | + parent: pulse.id, |
| 89 | + isPublic: 0, // 0 for comments made by you; 1 for all comments of the pulse |
| 90 | + limit: 3, // fetch given number of comments for this pulse |
| 91 | + end: 0 // fetch comments from a given starttime in milliseconds |
| 92 | + }) |
| 93 | + |
| 94 | + debug('pulse comments response') |
| 95 | + debugTable({ |
| 96 | + headers: [ |
| 97 | + 'PID', 'MTS', 'PUID', 'COMMENT' |
| 98 | + ], |
| 99 | + rows: pulseComments.map(({ id, mts, userID, content }) => [ |
| 100 | + id, |
| 101 | + new Date(mts).toLocaleString(), |
| 102 | + userID, |
| 103 | + content.substring(0, 15) |
| 104 | + ]) |
| 105 | + }) |
| 106 | +}) |
0 commit comments