]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/archives.js
Update strings
[friendica-addons.git] / jappixmini / jappix / js / archives.js
1 /*
2
3 Jappix - An open social platform
4 These are the archives functions for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 03/03/11
11
12 */
13
14 // Opens the archive tools
15 function openArchives() {
16         // Popup HTML content
17         var html = 
18         '<div class="top">' + _e("Message archives") + '</div>' + 
19         
20         '<div class="content">' + 
21                 '<div class="filter">' + 
22                         '<select class="friend" multiple=""></select>' + 
23                         
24                         '<div class="date"></div>' + 
25                 '</div>' + 
26                 
27                 '<div class="current">' + 
28                         '<span class="name"></span>' + 
29                         '<span class="time">' + _e("Please select a friend to view the chat history.") + '</span>' + 
30                 '</div>' + 
31                 
32                 '<div class="logs" id="chat-content-archives"></div>' + 
33         '</div>' + 
34         
35         '<div class="bottom">' + 
36                 '<div class="wait wait-medium"></div>' + 
37                 
38                 '<a href="#" class="finish">' + _e("Close") + '</a>' + 
39         '</div>';
40         
41         // Create the popup
42         createPopup('archives', html);
43         
44         // Associate the events
45         launchArchives();
46         
47         // Get all the buddies in our roster
48         var buddies = getAllBuddies();
49         var options = '';
50         
51         for(i in buddies) {
52                 var current = buddies[i];
53                 
54                 // Add the current buddy
55                 options += '<option value="' + encodeQuotes(current) + '">' + getBuddyName(current).htmlEnc() + '</option>';
56         }
57         
58         // Can append the buddy HTML code?
59         if(options)
60                 $('#archives .filter .friend').append(options);
61         
62         return false;
63 }
64
65 // Closes the archive tools
66 function closeArchives() {
67         // Destroy the popup
68         destroyPopup('archives');
69         
70         return false;
71 }
72
73 // Gets the archives list for a buddy
74 function getListArchives(xid) {
75         // Reset the archives viewer
76         $('#archives .logs').empty();
77         
78         // Show the waiting icon
79         $('#archives .wait').show();
80         
81         // Apply the ID
82         var id = genID();
83         $('#archives').attr('data-session', id);
84         
85         // New IQ
86         var iq = new JSJaCIQ();
87         iq.setType('get');
88         iq.setID(id);
89         
90         var list = iq.appendNode('list', {'xmlns': NS_URN_ARCHIVE, 'with': xid});
91         var set = list.appendChild(iq.buildNode('set', {'xmlns': NS_RSM}));
92         set.appendChild(iq.buildNode('max', {'xmlns': NS_RSM}, '0'));
93         
94         con.send(iq, handleListArchives);
95         
96         logThis('Getting archives list for: ' + xid + '...');
97 }
98
99 // Handles the archives list for a buddy
100 function handleListArchives(iq) {
101         // Hide the waiting icon
102         $('#archives .wait').hide();
103         
104         // Any error?
105         if(handleErrorReply(iq) || !exists('#archives[data-session=' + iq.getID() + ']'))
106                 return;
107         
108         // Get the last archive date
109         var last = $(iq.getNode()).find('list set changed').text();
110         
111         // Any last archive?
112         if(last) {
113                 // Read the date
114                 var date = Date.jab2date(last);
115                 
116                 // Change the datepicker value
117                 $('#archives .filter .date').DatePickerSetDate(date, true);
118                 
119                 // Retrieve the archives
120                 checkChangeArchives();
121         }
122         
123         logThis('Got archives list.', 2);
124 }
125
126 // Gets the archives for a day
127 function getDayArchives(xid, date) {
128         // Reset the archives viewer
129         $('#archives .logs').empty();
130         
131         // Show the waiting icon
132         $('#archives .wait').show();
133         
134         // Apply the ID
135         var id = genID();
136         $('#archives').attr('data-session', id);
137         
138         // New IQ
139         var iq = new JSJaCIQ();
140         iq.setType('get');
141         iq.setID(id);
142         
143         iq.appendNode('list', {'xmlns': NS_URN_ARCHIVE, 'with': xid, 'start': date + 'T00:00:00Z', 'end': date + 'T23:59:59Z'});
144         
145         con.send(iq, handleDayArchives);
146         
147         logThis('Getting day archives (' + date + ') for: ' + xid + '...');
148 }
149
150 // Handles the archives for a day
151 function handleDayArchives(iq) {
152         // Hide the waiting icon
153         $('#archives .wait').hide();
154         
155         // Any error?
156         if(handleErrorReply(iq) || !exists('#archives[data-session=' + iq.getID() + ']'))
157                 return;
158         
159         // Get each archive thread
160         $(iq.getNode()).find('chat').each(function() {
161                 // Current values
162                 var xid = $(this).attr('with');
163                 var start = $(this).attr('start');
164                 
165                 if(xid && start)
166                         $('#archives .logs').append('<input class="archives-pending" type="hidden" data-with="' + encodeQuotes(xid) + '" data-start="' + encodeQuotes(start) + '" />');
167         });
168         
169         // Display the day
170         var date = parseDay($('#archives .filter .date').DatePickerGetDate(true) + 'T00:00:00Z' + getDateTZO());
171         
172         // Try to get the first thread
173         var pending = '#archives input.archives-pending:first';
174         
175         if(!exists(pending))
176                 date = printf(_e("Nothing found for: %s"), date);
177         
178         else {
179                 retrieveArchives($(pending).attr('data-with'), $(pending).attr('data-start'));
180                 $(pending).remove();
181         }
182         
183         $('#archives .current .time').text(date);
184         
185         logThis('Got day archives.', 2);
186 }
187
188 // Retrieves a specified archive collection
189 function retrieveArchives(xid, start) {
190         // Show the waiting icon
191         $('#archives .wait').show();
192         
193         // Apply the ID
194         var id = genID();
195         $('#archives').attr('data-session', id);
196         
197         // New IQ
198         var iq = new JSJaCIQ();
199         iq.setType('get');
200         iq.setID(id);
201         
202         var list = iq.appendNode('retrieve', {'xmlns': NS_URN_ARCHIVE, 'with': xid, 'start': start});
203         
204         con.send(iq, handleRetrieveArchives);
205         
206         logThis('Retrieving archives (start: ' + start + ') for: ' + xid + '...');
207 }
208
209 // Handles a specified archive collection
210 function handleRetrieveArchives(iq) {
211         // Hide the waiting icon
212         $('#archives .wait').hide();
213         
214         // Any error?
215         if(handleErrorReply(iq) || !exists('#archives[data-session=' + iq.getID() + ']'))
216                 return;
217         
218         // Get the node
219         var chat = $(iq.getNode()).find('chat:first');
220         
221         // Get the buddy XID
222         var xid = bareXID(chat.attr('with'));
223         
224         // Get the start date & stamp
225         var start_date = Date.jab2date(chat.attr('start'));
226         var start_stamp = extractStamp(start_date);
227         
228         // Parse the result chat
229         chat.find('to, from').each(function() {
230                 var node = (this).nodeName;
231                 var stamp = start_stamp + parseInt($(this).attr('secs'));
232                 var date = extractTime(new Date(stamp * 1000));
233                 var body = $(this).find('body').text();
234                 
235                 // Is it my message?
236                 if((node == 'to') && body)
237                         displayMessage('chat', getXID(), 'archives', getBuddyName(getXID()).htmlEnc(), body, date, start_stamp, 'user-message', true, '', 'me');
238                 
239                 // Is it a buddy message?
240                 else if((node == 'from') && body)
241                         displayMessage('chat', xid, 'archives', getBuddyName(xid).htmlEnc(), body, date, start_stamp, 'user-message', true, '', 'him');
242         });
243         
244         // Not the latest thread?
245         var pending = '#archives input.archives-pending:first';
246         
247         if(exists(pending)) {
248                 retrieveArchives($(pending).attr('data-with'), $(pending).attr('data-start'));
249                 $(pending).remove();
250         }
251         
252         // Everything has been retrieved, get the avatars
253         else {
254                 getAvatar(getXID(), 'cache', 'true', 'forget');
255                 getAvatar(xid, 'cache', 'true', 'forget');
256         }
257         
258         logThis('Got archives.', 2);
259 }
260
261 // Gets the archiving configuration
262 function getConfigArchives() {
263         // Lock the archiving options
264         $('#archiving').attr('checked', false).attr('disabled', true);
265         
266         // Get the archiving configuration
267         var iq = new JSJaCIQ();
268         iq.setType('get');
269         
270         iq.appendNode('pref', {'xmlns': NS_URN_ARCHIVE});
271         
272         con.send(iq, handleGetConfigArchives);
273 }
274
275 // Handles the archiving configuration
276 function handleGetConfigArchives(iq) {
277         // Reset the options stuffs
278         waitOptions('archives');
279         
280         // Unlock the archiving options
281         $('#archiving').removeAttr('disabled');
282         
283         // End if not a result
284         if(!iq || (iq.getType() != 'result'))
285                 return;
286         
287         // Extract the preferences from the IQ
288         var enabled = $(iq.getNode()).find('pref auto').attr('save');
289         
290         // Define the input enabling/disabling vars
291         var checked = true;
292         
293         if(enabled != 'true')
294                 checked = false;
295         
296         // Apply the values
297         $('#archiving').attr('checked', checked);
298 }
299
300 // Configures the archiving on the server
301 function configArchives(enabled) {
302         // Configure the auto element
303         var iq = new JSJaCIQ();
304         iq.setType('set');
305         
306         iq.appendNode('auto', {'xmlns': NS_URN_ARCHIVE, 'save': enabled});
307         
308         con.send(iq, handleConfigArchives);
309         
310         // Configure the default element
311         var iq = new JSJaCIQ();
312         iq.setType('set');
313         
314         var pref = iq.appendNode('pref', {'xmlns': NS_URN_ARCHIVE});
315         pref.appendChild(iq.appendNode('default', {'xmlns': NS_URN_ARCHIVE, 'otr': 'concede', 'save': 'body'}));
316         
317         con.send(iq);
318         
319         // Configure the method element
320         var iq = new JSJaCIQ();
321         iq.setType('set');
322         
323         var mType = new Array('auto', 'local', 'manual');
324         var mUse = new Array('prefer', 'concede', 'concede');
325         
326         var pref = iq.appendNode('pref', {'xmlns': NS_URN_ARCHIVE});
327         
328         for(i in mType)
329                 pref.appendChild(iq.appendNode('method', {'xmlns': NS_URN_ARCHIVE, 'type': mType[i], 'use': mUse[i]}));
330         
331         con.send(iq);
332         
333         // Logger
334         logThis('Configuring archives...', 3);
335 }
336
337 // Handles the archives configuration
338 function handleConfigArchives(iq) {
339         if(!iq || (iq.getType() != 'result'))
340                 logThis('Archives not configured.', 2);
341         else
342                 logThis('Archives configured.', 3);
343 }
344
345 // Checks if the datepicker has changed
346 function checkChangeArchives() {
347         var xid = $('#archives .filter .friend').val();
348         var date = $('#archives .filter .date').DatePickerGetDate(true);
349         
350         // No XID?
351         if(!xid || !xid.length)
352                 return;
353         
354         // Too many value?
355         if(xid.length > 1) {
356                 $('#archives .filter .friend').val(xid[0]);
357                 
358                 return;
359         }
360         
361         // Get the first XID
362         xid = xid[0];
363         
364         // Get the archives
365         getDayArchives(xid, date);
366 }
367
368 // Update the archives with the selected XID
369 function updateArchives() {
370         // Read the values
371         var xid = $('#archives .filter .friend').val();
372         var date = $('#archives .filter .date').DatePickerGetDate(true);
373         
374         // No XID?
375         if(!xid || !xid.length)
376                 return;
377         
378         // Too many value?
379         if(xid.length > 1) {
380                 $('#archives .filter .friend').val(xid[0]);
381                 
382                 return;
383         }
384         
385         // Get the first XID
386         xid = xid[0];
387         
388         // Apply the current marker
389         $('#archives .current .name').text(getBuddyName(xid));
390         $('#archives .current .time').text(parseDay(date + 'T00:00:00Z' + getDateTZO()));
391         
392         // Get the archives
393         getListArchives(xid, date);
394 }
395
396 // Addon launcher
397 function launchArchives() {
398         // Current date
399         var current_date = explodeThis('T', getXMPPTime(), 0);
400         
401         // Datepicker
402         $('#archives .filter .date').DatePicker({
403                 flat: true,
404                 date: current_date,
405                 current: current_date,
406                 calendars: 1,
407                 starts: 1,
408                 onChange: checkChangeArchives
409         });
410         
411         // Click events
412         $('#archives .bottom .finish').click(function() {
413                 return closeArchives();
414         });
415         
416         // Change event
417         $('#archives .filter .friend').change(updateArchives);
418 }