File size: 2,889 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const EventEmitter = require( 'events' ).EventEmitter;
const url = require( 'url' );
const WPNotificationsAPI = require( '../../../lib/notifications/api' );

// Parses raw note data from the API into a notification for display,
// and exposes handlers for actions performed by the user.
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 the note is approved, construct the URL to navigate to.
				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 ) {
	// TODO: Ideally we should augment the note data from the API with
	// the site's human-readable name. Using the note's URL for now.
	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;
	}

	// Unfortunately it appears that the exact location within the note body array
	// containing action attributes may not be consistent between note types (and
	// has in fact changed since this code was originally written).
	//
	// Inspect all elements in the body array to be safe.
	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();