5 Examples of notifications:
7 ------------------------------------------
10 Andrew: Yo how are you...
11 Andrew uploaded account.xls
12 ------------------------------------------
15 ------------------------------------------
17 Andrew uploaded account.xls
18 Andrew uploaded account2.xls
19 ------------------------------------------
21 Mike: hey how are you?
26 var MAX_NOTIFICATION_TEXT = 1024;
27 var LAST_LINE_MIN_LENGTH = 30;
29 var ent = require('ent');
31 function truncate(line, maxLineLength) {
32 if (line.length > maxLineLength) {
33 line = line.substring(0, maxLineLength - 1).trim() + '…';
38 function getHeaderLine(troupe) {
39 return troupe.name || troupe.uri;
42 function getText(username, chat) {
43 if (!chat.text) return;
44 var encodedText = ent.decode(chat.text); // Do we need to do this?
46 return username + ': ' + encodedText;
52 function getShortFromUserName(user) {
55 var displayName = user.displayName;
56 if (!displayName) return '';
58 // this is dodgy, btw. See #373
59 //return displayName && displayName.split(/\s/,1)[0];
60 return displayName.trim();
63 // eslint-disable-next-line complexity
64 function summarizeChatsInRoom(troupe, chats, options) {
65 var appendText = options && options.appendText;
66 var maxMessageLength = (options && options.maxMessageLength) || MAX_NOTIFICATION_TEXT;
67 var maxLength = maxMessageLength - (appendText ? appendText.length : 0);
70 // Generate notification text
72 var notificationText = getHeaderLine(troupe) || '';
73 var lastUsername = null;
76 i < chats.length && notificationText.length <= maxLength - LAST_LINE_MIN_LENGTH;
81 // Only group chats prefix the username
82 var username = troupe.oneToOne ? null : getShortFromUserName(chat.fromUser);
83 if (username === lastUsername) {
84 username = null; // Don't prefix the username if it's the same as the last message
86 lastUsername = username;
89 var text = getText(username, chat);
92 if (notificationText.length + text.length > maxLength) {
93 text = truncate(text, maxLength - notificationText.length - 3 /* newline etc */);
94 // We add extra spaces so that when they're removed on an iphone the notificationText still makes sense
97 notificationText = notificationText ? notificationText + ' \n' + text : text;
101 notificationText += appendText;
104 return notificationText;
107 module.exports = summarizeChatsInRoom;