X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FRealtime%2Frealtimeupdate.js;h=e044f2f916c0fbe1e8e3bb9b1c37581d97093941;hb=ae3eaee6cd5f67b0260abf30c3517072e372e270;hp=e615895cab9e3989ec9667d71be50041403d39b6;hpb=55b4303ea4da2a74723f7df5dbd1d83ae3922d27;p=quix0rs-gnu-social.git diff --git a/plugins/Realtime/realtimeupdate.js b/plugins/Realtime/realtimeupdate.js index e615895cab..e044f2f916 100644 --- a/plugins/Realtime/realtimeupdate.js +++ b/plugins/Realtime/realtimeupdate.js @@ -1,6 +1,6 @@ /* * StatusNet - a distributed open-source microblogging tool - * Copyright (C) 2008, StatusNet, Inc. + * Copyright (C) 2009-2011, StatusNet, Inc. * * Add a notice encoded as JSON into the current timeline * @@ -21,7 +21,7 @@ * @package StatusNet * @author Evan Prodromou * @author Sarven Capadisli - * @copyright 2009 StatusNet, Inc. + * @copyright 2009-2011 StatusNet, Inc. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://status.net/ */ @@ -44,10 +44,9 @@ */ RealtimeUpdate = { _userid: 0, - _replyurl: '', - _favorurl: '', - _repeaturl: '', - _deleteurl: '', + _showurl: '', + _keepaliveurl: '', + _closeurl: '', _updatecounter: 0, _maxnotices: 50, _windowhasfocus: true, @@ -66,21 +65,15 @@ RealtimeUpdate = { * feed data into the RealtimeUpdate object! * * @param {int} userid: local profile ID of the currently logged-in user - * @param {String} replyurl: URL for newnotice action, used when generating reply buttons - * @param {String} favorurl: URL for favor action, used when generating fave buttons - * @param {String} repeaturl: URL for repeat action, used when generating repeat buttons - * @param {String} deleteurl: URL template for deletenotice action, used when generating delete buttons. + * @param {String} showurl: URL for shownotice action, used when fetching formatting notices. * This URL contains a stub value of 0000000000 which will be replaced with the notice ID. * * @access public */ - init: function(userid, replyurl, favorurl, repeaturl, deleteurl) + init: function(userid, showurl) { RealtimeUpdate._userid = userid; - RealtimeUpdate._replyurl = replyurl; - RealtimeUpdate._favorurl = favorurl; - RealtimeUpdate._repeaturl = repeaturl; - RealtimeUpdate._deleteurl = deleteurl; + RealtimeUpdate._showurl = showurl; RealtimeUpdate._documenttitle = document.title; @@ -163,50 +156,55 @@ RealtimeUpdate = { return; } - var noticeItem = RealtimeUpdate.makeNoticeItem(data); - var noticeItemID = $(noticeItem).attr('id'); - - var list = $("#notices_primary .notices:first") - var prepend = true; - - var threaded = list.hasClass('threaded-notices'); - if (threaded && data.in_reply_to_status_id) { - // aho! - var parent = $('#notice-' + data.in_reply_to_status_id); - if (parent.length == 0) { - // @todo fetch the original, insert it, and finish the rest - } else { - // Check the parent notice to make sure it's not a reply itself. - // If so, use it's parent as the parent. - var parentList = parent.closest('.notices'); - if (parentList.hasClass('threaded-replies')) { - parent = parentList.closest('.notice'); - } - list = parent.find('.threaded-replies'); - if (list.length == 0) { - list = $(''); - parent.append(list); + RealtimeUpdate.makeNoticeItem(data, function(noticeItem) { + // Check again in case it got shown while we were waiting for data... + if (RealtimeUpdate.isNoticeVisible(data.id)) { + return; + } + var noticeItemID = $(noticeItem).attr('id'); + + var list = $("#notices_primary .notices:first") + var prepend = true; + + var threaded = list.hasClass('threaded-notices'); + if (threaded && data.in_reply_to_status_id) { + // aho! + var parent = $('#notice-' + data.in_reply_to_status_id); + if (parent.length == 0) { + // @todo fetch the original, insert it, and finish the rest + } else { + // Check the parent notice to make sure it's not a reply itself. + // If so, use it's parent as the parent. + var parentList = parent.closest('.notices'); + if (parentList.hasClass('threaded-replies')) { + parent = parentList.closest('.notice'); + } + list = parent.find('.threaded-replies'); + if (list.length == 0) { + list = $(''); + parent.append(list); + SN.U.NoticeInlineReplyPlaceholder(parent); + } + prepend = false; } - prepend = false; } - } - var newNotice = $(noticeItem); - if (prepend) { - list.prepend(newNotice); - } else { - var placeholder = list.find('li.notice-reply-placeholder') - if (placeholder.length > 0) { - newNotice.insertBefore(placeholder) + var newNotice = $(noticeItem); + if (prepend) { + list.prepend(newNotice); } else { - newNotice.appendTo(list); - SN.U.NoticeInlineReplyPlaceholder(parent); + var placeholder = list.find('li.notice-reply-placeholder') + if (placeholder.length > 0) { + newNotice.insertBefore(placeholder) + } else { + newNotice.appendTo(list); + } } - } - newNotice.css({display:"none"}).fadeIn(1000); + newNotice.css({display:"none"}).fadeIn(1000); - SN.U.NoticeReplyTo($('#'+noticeItemID)); - SN.U.NoticeWithAttachment($('#'+noticeItemID)); + SN.U.NoticeReplyTo($('#'+noticeItemID)); + SN.U.NoticeWithAttachment($('#'+noticeItemID)); + }); }, /** @@ -263,86 +261,24 @@ RealtimeUpdate = { }, /** - * Builds a notice HTML block from JSON API-style data. + * Builds a notice HTML block from JSON API-style data; + * loads data from server, so runs async. * * @param {Object} data: extended JSON API-formatted notice - * @return {String} HTML fragment - * - * @fixme this replicates core StatusNet code, making maintenance harder - * @fixme sloppy HTML building (raw concat without escaping) - * @fixme no i18n support - * @fixme local variables pollute global namespace + * @param {function} callback: function(DOMNode) to receive new code * * @access private */ - makeNoticeItem: function(data) + makeNoticeItem: function(data, callback) { - if (data.hasOwnProperty('retweeted_status')) { - original = data['retweeted_status']; - repeat = data; - data = original; - unique = repeat['id']; - responsible = repeat['user']; - } else { - original = null; - repeat = null; - unique = data['id']; - responsible = data['user']; - } - - user = data['user']; - html = data['html'].replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/&/g,'&'); - source = data['source'].replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/&/g,'&'); - - ni = "
  • "+ - "
    "+ - ""+ - ""+ - "\""+user['screen_name']+"\"/"+ - ""+user['screen_name']+""+ - ""+ - ""+ - "

    "+html+"

    "+ - "
    "+ - "
    "+ - ""+ - "a few seconds ago"+ - " "+ - ""+ - "from "+ - ""+source+""+ // may have a link - ""; - if (data['conversation_url']) { - ni = ni+" in context"; - } - - if (repeat) { - ru = repeat['user']; - ni = ni + "Repeated by " + - "" + - ""+ ru['screen_name'] + ""; - } - - ni = ni+"
    "; - - ni = ni + "
    "; - - if (RealtimeUpdate._userid != 0) { - var input = $("form#form_notice fieldset input#token"); - var session_key = input.val(); - ni = ni+RealtimeUpdate.makeFavoriteForm(data['id'], session_key); - ni = ni+RealtimeUpdate.makeReplyLink(data['id'], data['user']['screen_name']); - if (RealtimeUpdate._userid == responsible['id']) { - ni = ni+RealtimeUpdate.makeDeleteLink(data['id']); - } else if (RealtimeUpdate._userid != user['id']) { - ni = ni+RealtimeUpdate.makeRepeatForm(data['id'], session_key); - } - } - - ni = ni+"
    "; - - ni = ni+"
  • "; - return ni; + var url = RealtimeUpdate._showurl.replace('0000000000', data.id); + $.get(url, {ajax: 1}, function(data, textStatus, xhr) { + var notice = $('li.notice:first', data); + if (notice.length) { + var node = document._importNode(notice[0], true); + callback(node); + } + }); }, /** @@ -456,11 +392,28 @@ RealtimeUpdate = { * * @access private */ - initActions: function(url, timeline, path) + initActions: function(url, timeline, path, keepaliveurl, closeurl) { $('#notices_primary').prepend(''); RealtimeUpdate._pluginPath = path; + RealtimeUpdate._keepaliveurl = keepaliveurl; + RealtimeUpdate._closeurl = closeurl; + + + // On unload, let the server know we're no longer listening + $(window).unload(function() { + $.ajax({ + type: 'POST', + url: RealtimeUpdate._closeurl}); + }); + + setInterval(function() { + $.ajax({ + type: 'POST', + url: RealtimeUpdate._keepaliveurl}); + + }, 15 * 60 * 1000 ); // every 15 min; timeout in 30 min RealtimeUpdate.initPlayPause(); RealtimeUpdate.initAddPopup(url, timeline, RealtimeUpdate._pluginPath);