3 Jappix - An open social platform
4 These are the archives functions for Jappix
6 -------------------------------------------------
10 Last revision: 03/03/11
14 // Opens the archive tools
15 function openArchives() {
18 '<div class="top">' + _e("Message archives") + '</div>' +
20 '<div class="content">' +
21 '<div class="filter">' +
22 '<select class="friend" multiple=""></select>' +
24 '<div class="date"></div>' +
27 '<div class="current">' +
28 '<span class="name"></span>' +
29 '<span class="time">' + _e("Please select a friend to view the chat history.") + '</span>' +
32 '<div class="logs" id="chat-content-archives"></div>' +
35 '<div class="bottom">' +
36 '<div class="wait wait-medium"></div>' +
38 '<a href="#" class="finish">' + _e("Close") + '</a>' +
42 createPopup('archives', html);
44 // Associate the events
47 // Get all the buddies in our roster
48 var buddies = getAllBuddies();
52 var current = buddies[i];
54 // Add the current buddy
55 options += '<option value="' + encodeQuotes(current) + '">' + getBuddyName(current).htmlEnc() + '</option>';
58 // Can append the buddy HTML code?
60 $('#archives .filter .friend').append(options);
65 // Closes the archive tools
66 function closeArchives() {
68 destroyPopup('archives');
73 // Gets the archives list for a buddy
74 function getListArchives(xid) {
75 // Reset the archives viewer
76 $('#archives .logs').empty();
78 // Show the waiting icon
79 $('#archives .wait').show();
83 $('#archives').attr('data-session', id);
86 var iq = new JSJaCIQ();
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'));
94 con.send(iq, handleListArchives);
96 logThis('Getting archives list for: ' + xid + '...');
99 // Handles the archives list for a buddy
100 function handleListArchives(iq) {
101 // Hide the waiting icon
102 $('#archives .wait').hide();
105 if(handleErrorReply(iq) || !exists('#archives[data-session=' + iq.getID() + ']'))
108 // Get the last archive date
109 var last = $(iq.getNode()).find('list set changed').text();
114 var date = Date.jab2date(last);
116 // Change the datepicker value
117 $('#archives .filter .date').DatePickerSetDate(date, true);
119 // Retrieve the archives
120 checkChangeArchives();
123 logThis('Got archives list.', 2);
126 // Gets the archives for a day
127 function getDayArchives(xid, date) {
128 // Reset the archives viewer
129 $('#archives .logs').empty();
131 // Show the waiting icon
132 $('#archives .wait').show();
136 $('#archives').attr('data-session', id);
139 var iq = new JSJaCIQ();
143 iq.appendNode('list', {'xmlns': NS_URN_ARCHIVE, 'with': xid, 'start': date + 'T00:00:00Z', 'end': date + 'T23:59:59Z'});
145 con.send(iq, handleDayArchives);
147 logThis('Getting day archives (' + date + ') for: ' + xid + '...');
150 // Handles the archives for a day
151 function handleDayArchives(iq) {
152 // Hide the waiting icon
153 $('#archives .wait').hide();
156 if(handleErrorReply(iq) || !exists('#archives[data-session=' + iq.getID() + ']'))
159 // Get each archive thread
160 $(iq.getNode()).find('chat').each(function() {
162 var xid = $(this).attr('with');
163 var start = $(this).attr('start');
166 $('#archives .logs').append('<input class="archives-pending" type="hidden" data-with="' + encodeQuotes(xid) + '" data-start="' + encodeQuotes(start) + '" />');
170 var date = parseDay($('#archives .filter .date').DatePickerGetDate(true) + 'T00:00:00Z' + getDateTZO());
172 // Try to get the first thread
173 var pending = '#archives input.archives-pending:first';
176 date = printf(_e("Nothing found for: %s"), date);
179 retrieveArchives($(pending).attr('data-with'), $(pending).attr('data-start'));
183 $('#archives .current .time').text(date);
185 logThis('Got day archives.', 2);
188 // Retrieves a specified archive collection
189 function retrieveArchives(xid, start) {
190 // Show the waiting icon
191 $('#archives .wait').show();
195 $('#archives').attr('data-session', id);
198 var iq = new JSJaCIQ();
202 var list = iq.appendNode('retrieve', {'xmlns': NS_URN_ARCHIVE, 'with': xid, 'start': start});
204 con.send(iq, handleRetrieveArchives);
206 logThis('Retrieving archives (start: ' + start + ') for: ' + xid + '...');
209 // Handles a specified archive collection
210 function handleRetrieveArchives(iq) {
211 // Hide the waiting icon
212 $('#archives .wait').hide();
215 if(handleErrorReply(iq) || !exists('#archives[data-session=' + iq.getID() + ']'))
219 var chat = $(iq.getNode()).find('chat:first');
222 var xid = bareXID(chat.attr('with'));
224 // Get the start date & stamp
225 var start_date = Date.jab2date(chat.attr('start'));
226 var start_stamp = extractStamp(start_date);
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();
236 if((node == 'to') && body)
237 displayMessage('chat', getXID(), 'archives', getBuddyName(getXID()).htmlEnc(), body, date, start_stamp, 'user-message', true, '', 'me');
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');
244 // Not the latest thread?
245 var pending = '#archives input.archives-pending:first';
247 if(exists(pending)) {
248 retrieveArchives($(pending).attr('data-with'), $(pending).attr('data-start'));
252 // Everything has been retrieved, get the avatars
254 getAvatar(getXID(), 'cache', 'true', 'forget');
255 getAvatar(xid, 'cache', 'true', 'forget');
258 logThis('Got archives.', 2);
261 // Gets the archiving configuration
262 function getConfigArchives() {
263 // Lock the archiving options
264 $('#archiving').attr('checked', false).attr('disabled', true);
266 // Get the archiving configuration
267 var iq = new JSJaCIQ();
270 iq.appendNode('pref', {'xmlns': NS_URN_ARCHIVE});
272 con.send(iq, handleGetConfigArchives);
275 // Handles the archiving configuration
276 function handleGetConfigArchives(iq) {
277 // Reset the options stuffs
278 waitOptions('archives');
280 // Unlock the archiving options
281 $('#archiving').removeAttr('disabled');
283 // End if not a result
284 if(!iq || (iq.getType() != 'result'))
287 // Extract the preferences from the IQ
288 var enabled = $(iq.getNode()).find('pref auto').attr('save');
290 // Define the input enabling/disabling vars
293 if(enabled != 'true')
297 $('#archiving').attr('checked', checked);
300 // Configures the archiving on the server
301 function configArchives(enabled) {
302 // Configure the auto element
303 var iq = new JSJaCIQ();
306 iq.appendNode('auto', {'xmlns': NS_URN_ARCHIVE, 'save': enabled});
308 con.send(iq, handleConfigArchives);
310 // Configure the default element
311 var iq = new JSJaCIQ();
314 var pref = iq.appendNode('pref', {'xmlns': NS_URN_ARCHIVE});
315 pref.appendChild(iq.appendNode('default', {'xmlns': NS_URN_ARCHIVE, 'otr': 'concede', 'save': 'body'}));
319 // Configure the method element
320 var iq = new JSJaCIQ();
323 var mType = new Array('auto', 'local', 'manual');
324 var mUse = new Array('prefer', 'concede', 'concede');
326 var pref = iq.appendNode('pref', {'xmlns': NS_URN_ARCHIVE});
329 pref.appendChild(iq.appendNode('method', {'xmlns': NS_URN_ARCHIVE, 'type': mType[i], 'use': mUse[i]}));
334 logThis('Configuring archives...', 3);
337 // Handles the archives configuration
338 function handleConfigArchives(iq) {
339 if(!iq || (iq.getType() != 'result'))
340 logThis('Archives not configured.', 2);
342 logThis('Archives configured.', 3);
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);
351 if(!xid || !xid.length)
356 $('#archives .filter .friend').val(xid[0]);
365 getDayArchives(xid, date);
368 // Update the archives with the selected XID
369 function updateArchives() {
371 var xid = $('#archives .filter .friend').val();
372 var date = $('#archives .filter .date').DatePickerGetDate(true);
375 if(!xid || !xid.length)
380 $('#archives .filter .friend').val(xid[0]);
388 // Apply the current marker
389 $('#archives .current .name').text(getBuddyName(xid));
390 $('#archives .current .time').text(parseDay(date + 'T00:00:00Z' + getDateTZO()));
393 getListArchives(xid, date);
397 function launchArchives() {
399 var current_date = explodeThis('T', getXMPPTime(), 0);
402 $('#archives .filter .date').DatePicker({
405 current: current_date,
408 onChange: checkChangeArchives
412 $('#archives .bottom .finish').click(function() {
413 return closeArchives();
417 $('#archives .filter .friend').change(updateArchives);