From a202578af77b8ecf66cb58a7f535481818db91e5 Mon Sep 17 00:00:00 2001 From: Evgeny Fadeev Date: Fri, 21 Sep 2012 01:45:45 -0400 Subject: made possibility to display threads as new/seen --- askbot/media/js/group_messaging.js | 59 +++++++-- askbot/media/js/utils.js | 35 +++++- askbot/templates/group_messaging/threads_list.html | 21 ++-- askbot/templates/user_inbox/messages.html | 2 +- ...astvisittime__add_field_message_senders_info.py | 134 +++++++++++++++++++++ group_messaging/models.py | 91 +++++++++++--- group_messaging/tests.py | 20 ++- group_messaging/views.py | 72 +++++++++-- 8 files changed, 370 insertions(+), 64 deletions(-) create mode 100644 group_messaging/migrations/0002_auto__add_lastvisittime__add_field_message_senders_info.py diff --git a/askbot/media/js/group_messaging.js b/askbot/media/js/group_messaging.js index 08dd305a..d9d3f0df 100644 --- a/askbot/media/js/group_messaging.js +++ b/askbot/media/js/group_messaging.js @@ -48,11 +48,27 @@ MessageComposer.prototype.onAfterCancel = function(handler) { } }; -MessageComposer.prototype.onAfterSend = function(handler) { - if (handler) { - this._onAfterSend = handler; - } else { - return this._onAfterSend(); +/** override these two + * @param {object} data - the response data + * these functions will run after .send() receives + * the response + */ +MessageComposer.prototype.onSendSuccessInternal = function(data) {}; +MessageComposer.prototype.onSendErrorInternal = function(data) {}; + +MessageComposer.prototype.onSendSuccess = function(callback) { + if (callback) { + this._onSendSuccess = callback; + } else if (this._onSendSuccess) { + this._onSendSuccess(); + } +}; + +MessageComposer.prototype.onSendError = function(callback) { + if (callback) { + this._onSendError = callback; + } else if (this._onSendError) { + this._onSendError(); } }; @@ -105,7 +121,15 @@ MessageComposer.prototype.send = function() { url: url, data: data, cache: false, - success: function() { me.onAfterSend(); } + success: function(data) { + if (data['success']) { + me.onSendSuccessInternal(data); + me.onSendSuccess(); + } else { + me.onSendErrorInternal(data); + me.onSendError(); + } + } }); }; @@ -130,7 +154,6 @@ MessageComposer.prototype.createDom = function() { var sendBtn = this.makeButton( gettext('send'), function() { - debugger; if (me.dataIsValid()){ me.send(); } @@ -167,21 +190,33 @@ NewThreadComposer.prototype.onAfterShow = function() { this._toInput.focus(); }; +NewThreadComposer.prototype.onSendErrorInternal = function(data) { + var missingUsers = data['missing_users'] + if (missingUsers) { + var errorTpl = ngettext( + 'user {{str}} does not exist', + 'users {{str}} do not exist', + missingUsers.length + ) + error = errorTpl.replace('{{str}}', joinAsPhrase(missingUsers)); + this._toInputError.html(error); + } +}; + NewThreadComposer.prototype.getInputData = function() { var data = NewThreadComposer.superClass_.getInputData.call(this); - data['to_username'] = $.trim(this._toInput.val()); + data['to_usernames'] = $.trim(this._toInput.val()); return data; }; NewThreadComposer.prototype.dataIsValid = function() { var superIsValid = NewThreadComposer.superClass_.dataIsValid.call(this); var to = $.trim(this._toInput.val()); - var meIsValid = true; if (to === '') { - meIsValid = false; this._toInputError.html(gettext('required')); + return false; } - return superIsValid && meIsValid; + return superIsValid; }; NewThreadComposer.prototype.createDom = function() { @@ -283,7 +318,7 @@ MessageCenter.prototype.decorate = function(element) { this._secondCol.append(editor.getElement()); editor.setSendUrl(element.data('createThreadUrl')); editor.onAfterCancel(function() { me.setState('show-list') }); - editor.onAfterSend(function() { + editor.onSendSuccess(function() { me.setState('show-list'); notify.show(gettext('message sent'), true); }); diff --git a/askbot/media/js/utils.js b/askbot/media/js/utils.js index 42f19d5b..f34ee876 100644 --- a/askbot/media/js/utils.js +++ b/askbot/media/js/utils.js @@ -29,17 +29,40 @@ var animateHashes = function(){ } }; -var getUniqueWords = function(value){ - var words = $.trim(value).split(/\s+/); +var getUniqueValues = function(values) { var uniques = new Object(); var out = new Array(); - $.each(words, function(idx, item){ - if (!(item in uniques)){ - uniques[item] = 1; - out.push(item); + $.each(values, function(idx, value){ + if (!(value in uniques)){ + uniques[value] = 1; + out.push(value); }; }); return out; +} + +var getUniqueWords = function(value){ + var words = $.trim(value).split(/\s+/); + return getUniqueValues(words); +}; + +/** + * comma-joins items and uses "and' + * between the last and penultimate items + * @param {Array} values + * @return {string} + */ +var joinAsPhrase = function(values) { + var count = values.length; + if (count === 0) { + return ''; + } else if (count === 1) { + return values[0]; + } else { + var last = values.pop(); + var prev = values.pop(); + return values.join(', ') + prev + gettext('and') + last; + } }; var showMessage = function(element, msg, where) { diff --git a/askbot/templates/group_messaging/threads_list.html b/askbot/templates/group_messaging/threads_list.html index 164867a1..80df18b9 100644 --- a/askbot/templates/group_messaging/threads_list.html +++ b/askbot/templates/group_messaging/threads_list.html @@ -1,13 +1,18 @@ -