]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/notification.js
Update strings
[friendica-addons.git] / jappixmini / jappix / js / notification.js
1 /*
2
3 Jappix - An open social platform
4 These are the notification JS scripts for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 27/08/11
11
12 */
13
14 // Resets the notifications alert if no one remaining
15 function closeEmptyNotifications() {
16         if(!$('.one-notification').size())
17                 closeBubbles();
18 }
19
20 // Checks if there are pending notifications
21 function checkNotifications() {
22         // Define the selectors
23         var notif = '#top-content .notifications';
24         var nothing = '.notifications-content .nothing';
25         var empty = '.notifications-content .empty';
26         
27         // Get the notifications number
28         var number = $('.one-notification').size();
29         
30         // Remove the red notify bubble
31         $(notif + ' .notify').remove();
32         
33         // Any notification?
34         if(number) {
35                 $(notif).prepend('<div class="notify one-counter" data-counter="' + number + '">' + number + '</div>');
36                 $(nothing).hide();
37                 $(empty).show();
38         }
39         
40         // No notification!
41         else {
42                 $(empty).hide();
43                 $(nothing).show();
44                 
45                 // Purge the social inbox node
46                 purgeNotifications();
47         }
48         
49         // Update the page title
50         updateTitle();
51 }
52
53 // Creates a new notification
54 function newNotification(type, from, data, body, id, inverse) {
55         if(!type || !from)
56                 return;
57         
58         // Generate an ID hash
59         if(!id)
60                 var id = hex_md5(type + from);
61         
62         // Generate the text to be displayed
63         var text, action, code;
64         var yes_path = 'href="#"';
65         
66         // User things
67         from = bareXID(from);
68         var hash = hex_md5(from);
69         
70         switch(type) {
71                 case 'subscribe':
72                         // Get the name to display
73                         var display_name = data[1];
74                         
75                         if(!display_name)
76                                 display_name = data[0];
77                         
78                         text = '<b>' + display_name.htmlEnc() + '</b> ' + _e("would like to add you as a friend.") + ' ' + _e("Do you accept?");
79                         
80                         break;
81                 
82                 case 'invite_room':
83                         text = '<b>' + getBuddyName(from).htmlEnc() + '</b> ' + _e("would like you to join this chatroom:") + ' <em>' + data[0].htmlEnc() + '</em> ' + _e("Do you accept?");
84                         
85                         break;
86                 
87                 case 'request':
88                         text = '<b>' + from.htmlEnc() + '</b> ' + _e("would like to get authorization.") + ' ' + _e("Do you accept?");
89                         
90                         break;
91                 
92                 case 'send':
93                         yes_path = 'href="' + encodeQuotes(data[1]) + '" target="_blank"';
94                         
95                         text = '<b>' + getBuddyName(from).htmlEnc() + '</b> ' + printf(_e("would like to send you a file: “%s”.").htmlEnc(), '<em>' + truncate(body, 25).htmlEnc() + '</em>') + ' ' + _e("Do you accept?");
96                         
97                         break;
98                 
99                 case 'send_pending':
100                         text = '<b>' + getBuddyName(from).htmlEnc() + '</b> ' + printf(_e("has received a file exchange request: “%s”.").htmlEnc(), '<em>' + truncate(body, 25).htmlEnc() + '</em>');
101                         
102                         break;
103                 
104                 case 'send_accept':
105                         text = '<b>' + getBuddyName(from).htmlEnc() + '</b> ' + printf(_e("has accepted to received your file: “%s”.").htmlEnc(), '<em>' + truncate(body, 25).htmlEnc() + '</em>');
106                         
107                         break;
108                 
109                 case 'send_reject':
110                         text = '<b>' + getBuddyName(from).htmlEnc() + '</b> ' + printf(_e("has rejected to receive your file: “%s”.").htmlEnc(), '<em>' + truncate(body, 25).htmlEnc() + '</em>');
111                         
112                         break;
113                 
114                 case 'send_fail':
115                         text = '<b>' + getBuddyName(from).htmlEnc() + '</b> ' + printf(_e("could not receive your file: “%s”.").htmlEnc(), '<em>' + truncate(body, 25).htmlEnc() + '</em>');
116                         
117                         break;
118                 
119                 case 'rosterx':
120                         text = printf(_e("Do you want to see the friends %s suggests you?").htmlEnc(), '<b>' + getBuddyName(from).htmlEnc() + '</b>');
121                         
122                         break;
123                 
124                 case 'comment':
125                         text = '<b>' + data[0].htmlEnc() + '</b> ' + printf(_e("commented an item you follow: “%s”.").htmlEnc(), '<em>' + truncate(body, 25).htmlEnc() + '</em>');
126                         
127                         break;
128                 
129                 case 'like':
130                         text = '<b>' + data[0].htmlEnc() + '</b> ' + printf(_e("liked your post: “%s”.").htmlEnc(), '<em>' + truncate(body, 25).htmlEnc() + '</em>');
131                         
132                         break;
133                 
134                 case 'quote':
135                         text = '<b>' + data[0].htmlEnc() + '</b> ' + printf(_e("quoted you somewhere: “%s”.").htmlEnc(), '<em>' + truncate(body, 25).htmlEnc() + '</em>');
136                         
137                         break;
138                 
139                 case 'wall':
140                         text = '<b>' + data[0].htmlEnc() + '</b> ' + printf(_e("published on your wall: “%s”.").htmlEnc(), '<em>' + truncate(body, 25).htmlEnc() + '</em>');
141                         
142                         break;
143                 
144                 case 'photo':
145                         text = '<b>' + data[0].htmlEnc() + '</b> ' + printf(_e("tagged you in a photo (%s).").htmlEnc(), '<em>' + truncate(body, 25).htmlEnc() + '</em>');
146                         
147                         break;
148                 
149                 case 'video':
150                         text = '<b>' + data[0].htmlEnc() + '</b> ' + printf(_e("tagged you in a video (%s).").htmlEnc(), '<em>' + truncate(body, 25).htmlEnc() + '</em>');
151                         
152                         break;
153                 
154                 default:
155                         break;
156         }
157         
158         // No text?
159         if(!text)
160                 return;
161         
162         // Action links?
163         if((type == 'send_pending') || (type == 'send_accept') || (type == 'send_reject') || (type == 'send_fail') || (type == 'comment') || (type == 'like') || (type == 'quote') || (type == 'wall') || (type == 'photo') || (type == 'video')) {
164                 action = '<a href="#" class="no">' + _e("Hide") + '</a>';
165                 
166                 // Any parent link?
167                 if((type == 'comment') && data[2])
168                         action = '<a href="#" class="yes">' + _e("Show") + '</a>' + action;
169         }
170         
171         else    
172                 action = '<a ' + yes_path + ' class="yes">' + _e("Yes") + '</a><a href="#" class="no">' + _e("No") + '</a>';
173         
174         if(text) {
175                 // We display the notification
176                 if(!exists('.notifications-content .' + id)) {
177                         // We create the html markup depending of the notification type
178                         code = '<div class="one-notification ' + id + ' ' + hash + '" title="' + encodeQuotes(body) + '" data-type="' + encodeQuotes(type) + '">' + 
179                                         '<div class="avatar-container">' + 
180                                                 '<img class="avatar" src="' + './img/others/default-avatar.png' + '" alt="" />' + 
181                                         '</div>' + 
182                                         
183                                         '<p class="notification-text">' + text + '</p>' + 
184                                         '<p class="notification-actions">' + 
185                                                 '<span class="talk-images" />' + 
186                                                 action + 
187                                         '</p>' + 
188                                '</div>';
189                         
190                         // Add the HTML code
191                         if(inverse)
192                                 $('.notifications-content .nothing').before(code);
193                         else
194                                 $('.notifications-content .empty').after(code);
195                         
196                         // Play a sound to alert the user
197                         soundPlay(2);
198                         
199                         // The yes click function
200                         $('.' + id + ' a.yes').click(function() {
201                                 actionNotification(type, data, 'yes', id);
202                                 
203                                 if(($(this).attr('href') == '#') && ($(this).attr('target') != '_blank'))
204                                         return false;
205                         });
206                         
207                         // The no click function
208                         $('.' + id + ' a.no').click(function() {
209                                 return actionNotification(type, data, 'no', id);
210                         });
211                         
212                         // Get the user avatar
213                         getAvatar(from, 'cache', 'true', 'forget');
214                 }
215         }
216         
217         // We tell the user he has a new pending notification
218         checkNotifications();
219         
220         logThis('New notification: ' + from, 3);
221 }
222
223 // Performs an action on a given notification
224 function actionNotification(type, data, value, id) {
225         // We launch a function depending of the type
226         if((type == 'subscribe') && (value == 'yes'))
227                 acceptSubscribe(data[0], data[1]);
228         
229         else if((type == 'subscribe') && (value == 'no'))
230                 sendSubscribe(data[0], 'unsubscribed');
231         
232         else if((type == 'invite_room') && (value == 'yes'))
233                 checkChatCreate(data[0], 'groupchat');
234         
235         else if(type == 'request')
236                 requestReply(value, data[0]);
237         
238         if((type == 'send') && (value == 'yes'))
239                 replyOOB(data[0], data[3], 'accept', data[2], data[4]);
240         
241         else if((type == 'send') && (value == 'no'))
242                 replyOOB(data[0], data[3], 'reject', data[2], data[4]);
243         
244         else if((type == 'rosterx') && (value == 'yes'))
245                 openRosterX(data[0]);
246         
247         else if((type == 'comment') || (type == 'like') || (type == 'quote') || (type == 'wall') || (type == 'photo') || (type == 'video')) {
248                 if(value == 'yes') {
249                         // Get the microblog item
250                         fromInfosMicroblog(data[2]);
251                         
252                         // Append the marker
253                         $('#channel .top.individual').append('<input type="hidden" name="comments" value="' + encodeQuotes(data[1]) + '" />');
254                 }
255                 
256                 removeNotification(data[3]);
257         }
258         
259         // We remove the notification
260         $('.notifications-content .' + id).remove();
261         
262         // We check if there's any other pending notification
263         closeEmptyNotifications();
264         checkNotifications();
265         
266         return false;
267 }
268
269 // Clear the social notifications
270 function clearNotifications() {
271         // Remove notifications
272         $('.one-notification').remove();
273         
274         // Refresh
275         closeEmptyNotifications();
276         checkNotifications();
277         
278         return false;
279 }
280
281 // Gets the pending social notifications
282 function getNotifications() {
283         var iq = new JSJaCIQ();
284         iq.setType('get');
285         
286         var pubsub = iq.appendNode('pubsub', {'xmlns': NS_PUBSUB});
287         pubsub.appendChild(iq.buildNode('items', {'node': NS_URN_INBOX, 'xmlns': NS_PUBSUB}));
288         
289         con.send(iq, handleNotifications);
290         
291         logThis('Getting social notifications...');
292 }
293
294 // Handles the social notifications
295 function handleNotifications(iq) {
296         // Any error?
297         if((iq.getType() == 'error') && $(iq.getNode()).find('item-not-found').size()) {
298                 // The node may not exist, create it!
299                 setupMicroblog('', NS_URN_INBOX, '1', '1000000', 'whitelist', 'open', true);
300                 
301                 logThis('Error while getting social notifications, trying to reconfigure the Pubsub node!', 2);
302         }
303         
304         // Selector
305         var items = $(iq.getNode()).find('item');
306         
307         // Should we inverse?
308         var inverse = true;
309         
310         if(items.size() == 1)
311                 inverse = false;
312         
313         // Parse notifications
314         items.each(function() {
315                 // Parse the current item
316                 var current_item = $(this).attr('id');
317                 var current_type = $(this).find('link[rel=via]:first').attr('title');
318                 var current_href = $(this).find('link[rel=via]:first').attr('href');
319                 var current_parent_href = $(this).find('link[rel=related]:first').attr('href');
320                 var current_xid = explodeThis(':', $(this).find('source author uri').text(), 1);
321                 var current_name = $(this).find('source author name').text();
322                 var current_text = $(this).find('content[type=text]:first').text();
323                 var current_bname = getBuddyName(current_xid);
324                 var current_id = hex_md5(current_type + current_xid + current_href + current_text);
325                 
326                 // Choose the good name!
327                 if(!current_name || (current_bname != getXIDNick(current_xid)))
328                         current_name = current_bname;
329                 
330                 // Create it!
331                 newNotification(current_type, current_xid, [current_name, current_href, current_parent_href, current_item], current_text, current_id, inverse);
332         });
333         
334         logThis(items.size() + ' social notification(s) got!', 3);
335 }
336
337 // Sends a social notification
338 function sendNotification(xid, type, href, text, parent) {
339         // Notification ID
340         var id = hex_md5(xid + text + getTimeStamp());
341         
342         // IQ
343         var iq = new JSJaCIQ();
344         iq.setType('set');
345         iq.setTo(xid);
346         
347         // ATOM content
348         var pubsub = iq.appendNode('pubsub', {'xmlns': NS_PUBSUB});
349         var publish = pubsub.appendChild(iq.buildNode('publish', {'node': NS_URN_INBOX, 'xmlns': NS_PUBSUB}));
350         var item = publish.appendChild(iq.buildNode('item', {'id': id, 'xmlns': NS_PUBSUB}));
351         var entry = item.appendChild(iq.buildNode('entry', {'xmlns': NS_ATOM}));
352         
353         // Notification author (us)
354         var Source = entry.appendChild(iq.buildNode('source', {'xmlns': NS_ATOM}));
355         var author = Source.appendChild(iq.buildNode('author', {'xmlns': NS_ATOM}));
356         author.appendChild(iq.buildNode('name', {'xmlns': NS_ATOM}, getName()));
357         author.appendChild(iq.buildNode('uri', {'xmlns': NS_ATOM}, 'xmpp:' + getXID()));
358         
359         // Notification content
360         entry.appendChild(iq.buildNode('published', {'xmlns': NS_ATOM}, getXMPPTime('utc')));
361         entry.appendChild(iq.buildNode('content', {'type': 'text', 'xmlns': NS_ATOM}, text));
362         entry.appendChild(iq.buildNode('link', {'rel': 'via', 'title': type, 'href': href, 'xmlns': NS_ATOM}));
363         
364         // Any parent item?
365         if(parent && parent[0] && parent[1] && parent[2]) {
366                 // Generate the parent XMPP URI
367                 var parent_href = 'xmpp:' + parent[0] + '?;node=' + encodeURIComponent(parent[1]) + ';item=' + encodeURIComponent(parent[2]);
368                 
369                 entry.appendChild(iq.buildNode('link', {'rel': 'related', 'href': parent_href, 'xmlns': NS_ATOM}));
370         }
371         
372         con.send(iq);
373         
374         logThis('Sending a social notification to ' + xid + ' (type: ' + type + ')...');
375 }
376
377 // Removes a social notification
378 function removeNotification(id) {
379         var iq = new JSJaCIQ();
380         iq.setType('set');
381         
382         var pubsub = iq.appendNode('pubsub', {'xmlns': NS_PUBSUB});
383         var retract = pubsub.appendChild(iq.buildNode('retract', {'node': NS_URN_INBOX, 'xmlns': NS_PUBSUB}));
384         retract.appendChild(iq.buildNode('item', {'id': id, 'xmlns': NS_PUBSUB}));
385         
386         con.send(iq);
387 }
388
389 // Purge the social notifications
390 function purgeNotifications() {
391         var iq = new JSJaCIQ();
392         iq.setType('set');
393         
394         var pubsub = iq.appendNode('pubsub', {'xmlns': NS_PUBSUB_OWNER});
395         pubsub.appendChild(iq.buildNode('purge', {'node': NS_URN_INBOX, 'xmlns': NS_PUBSUB_OWNER}));
396         
397         con.send(iq);
398         
399         return false;
400 }
401
402 // Adapt the notifications bubble max-height
403 function adaptNotifications() {
404         // Process the new height
405         var max_height = $('#right-content').height() - 22;
406         
407         // New height too small
408         if(max_height < 250)
409                 max_height = 250;
410         
411         // Apply the new height
412         $('.notifications-content .tools-content-subitem').css('max-height', max_height);
413 }
414
415 // Addon launcher
416 function launchNotifications() {
417         // Adapt the notifications height
418         adaptNotifications();
419 }
420
421 // Window resize event handler
422 $(window).resize(adaptNotifications);