| const EventEmitter = require( 'events' ).EventEmitter; |
| const url = require( 'url' ); |
| const WPNotificationsAPI = require( '../../../lib/notifications/api' ); |
|
|
| |
| |
| class WPNotificationsViewModel extends EventEmitter { |
| constructor() { |
| super(); |
|
|
| WPNotificationsAPI.on( 'note', ( note ) => { |
| const notification = parseNote( note ); |
| this.emit( 'notification', notification ); |
| } ); |
| } |
|
|
| didClickNotification( noteId, callback ) { |
| WPNotificationsAPI.markNoteAsRead( noteId, callback ); |
| } |
| } |
|
|
| function parseNote( note ) { |
| const id = note.id; |
| const title = getSiteTitle( note ); |
| const subtitle = note.subject.length > 1 ? note.subject[ 0 ].text : ''; |
| const body = note.subject.length > 1 ? note.subject[ 1 ].text : note.subject[ 0 ].text; |
|
|
| const type = note.type; |
| const siteId = note.meta.ids.site; |
| const postId = note.meta.ids.post; |
| const commentId = note.meta.ids.comment; |
| const fallbackUrl = note.url ? note.url : null; |
|
|
| const isApproved = getApprovedStatus( note ); |
|
|
| let navigate = null; |
|
|
| switch ( type ) { |
| case 'automattcher': |
| case 'post': |
| navigate = `/reader/blogs/${ siteId }/posts/${ postId }`; |
| break; |
| case 'comment': |
| { |
| |
| if ( isApproved ) { |
| navigate = `/reader/blogs/${ siteId }/posts/${ postId }#comment-${ commentId }`; |
| } |
| } |
| break; |
| case 'comment_like': |
| navigate = `/reader/blogs/${ siteId }/posts/${ postId }#comment-${ commentId }`; |
| break; |
| case 'site': |
| navigate = `/reader/blogs/${ siteId }`; |
| break; |
| default: |
| navigate = null; |
| } |
|
|
| if ( ! navigate ) { |
| navigate = fallbackUrl; |
| } |
|
|
| return { |
| id, |
| body, |
| type, |
| title, |
| subtitle, |
| navigate, |
| }; |
| } |
|
|
| function getSiteTitle( note ) { |
| |
| |
| return '' || ( note.url ? url.parse( note.url ).host : note.title ); |
| } |
|
|
| function getApprovedStatus( note ) { |
| if ( ! note.body || ! Array.isArray( note.body ) ) { |
| return false; |
| } |
|
|
| if ( note.body.length < 1 ) { |
| return false; |
| } |
|
|
| |
| |
| |
| |
| |
| let actions = null; |
| for ( let i = 0; i < note.body.length; i++ ) { |
| actions = note.body[ i ].actions; |
| if ( actions ) { |
| break; |
| } |
| } |
|
|
| if ( ! actions ) { |
| return false; |
| } |
|
|
| const approveComment = actions[ 'approve-comment' ]; |
| if ( approveComment === undefined ) { |
| return false; |
| } |
|
|
| return approveComment === true; |
| } |
|
|
| module.exports = new WPNotificationsViewModel(); |
|
|