You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1 lines
16 KiB

define(["jquery","core/auto_rows","core/backoff_timer","core/custom_interaction_events","core/notification","core/pubsub","core/str","core_message/message_repository","core_message/message_drawer_events","core_message/message_drawer_view_conversation_constants","core_message/message_drawer_view_conversation_patcher","core_message/message_drawer_view_conversation_renderer","core_message/message_drawer_view_conversation_state_manager","core_message/message_drawer_router","core_message/message_drawer_routes"],function(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o){var p={},q=null,r=!1,s=0,t=null,u=!0,v=!1,w=null,x=[],y=j.NEWEST_MESSAGES_FIRST,z=j.LOAD_MESSAGE_LIMIT,A=j.INITIAL_NEW_MESSAGE_POLL_TIMEOUT,B=j.SELECTORS,C=j.CONVERSATION_TYPES,D=function(){if(!q||q.type==C.PUBLIC)return null;var a=q.loggedInUserId;if(q.type==C.SELF)return a;var b=Object.keys(q.members).filter(function(b){return a!=b});return b.length?b[0]:null},E=function(a){return Object.keys(p).reduce(function(b,c){if(!b){var d=p[c].state;d.type!=C.PUBLIC&&a in d.members&&(b=d.id)}return b},null)},F=function(a){return{id:parseInt(a.attr("data-user-id"),10),fullname:null,profileimageurl:null,profileimageurlsmall:null,isonline:null,showonlinestatus:null,isblocked:null,iscontact:null,isdeleted:null,canmessage:null,requirescontact:null,contactrequests:[]}},G=function(){return s},H=function(a){s=a,p[q.id].messagesOffset=a},I=function(){return r},J=function(a){r=a,p[q.id].loadedAllMessages=a},K=function(a){return a.find(B.MESSAGES_CONTAINER)},L=function(b){return{id:b.id,name:b.name,subname:b.subname,imageUrl:b.imageUrl,isFavourite:b.isFavourite,isMuted:b.isMuted,type:b.type,totalMemberCount:b.totalMemberCount,loggedInUserId:b.loggedInUserId,messages:b.messages.map(function(b){return a.extend({},b)}),members:Object.keys(b.members).map(function(c){var d=a.extend({},b.members[c]);return d.contactrequests=b.members[c].contactrequests.map(function(b){return a.extend({},b)}),d})}},M=function(a,b){var c=a.id,d=c==b?C.SELF:C.PRIVATE,f=m.setLoadingMembers(q,!0);return f=m.setLoadingMessages(f,!0),w(f).then(function(){return h.getMemberInfo(c,[b],!0,!0)}).then(function(a){if(a.length)return a[0];throw new Error("Unable to load other user profile")}).then(function(b){var c=d==C.SELF?[b]:[b,a],e=m.addMembers(q,c);return e=m.setLoadingMembers(e,!1),e=m.setLoadingMessages(e,!1),e=m.setName(e,b.fullname),e=m.setType(e,d),e=m.setImageUrl(e,b.profileimageurl),e=m.setTotalMemberCount(e,c.length),w(e).then(function(){return b})})["catch"](function(a){var b=m.setLoadingMembers(q,!1);w(b),e.exception(a)})},N=function(a,b){var c=null;if(a.type==C.PRIVATE){var d=a.members.filter(function(a){return a.id!=b});c=d.length?d[0]:null}else a.type==C.SELF&&(c=a.members[0]);var e=a.name,f=a.imageurl;a.type!=C.PUBLIC&&(e=e||c?c.fullname:"",f=f||c?c.profileimageurl:"");var g=m.addMembers(q,a.members);return g=m.setName(g,e),g=m.setSubname(g,a.subname),g=m.setType(g,a.type),g=m.setImageUrl(g,f),g=m.setTotalMemberCount(g,a.membercount),g=m.setIsFavourite(g,a.isfavourite),g=m.setIsMuted(g,a.ismuted),g=m.addMessages(g,a.messages),g=m.setCanDeleteMessagesForAllUsers(g,a.candeletemessagesforallusers)},O=function(a,b,c,d,f){var g=b.id,i=m.setLoadingMembers(q,!0);return i=m.setLoadingMessages(i,!0),w(i).then(function(){return h.getConversation(g,a,!0,!0,0,0,c+1,d,f)}).then(function(a){return a.messages.length>c?a.messages=a.messages.slice(1):J(!0),H(d+c),a}).then(function(a){var c=a.members.filter(function(a){return a.id==b.id});c.length<1&&(a.members=a.members.concat([b]));var d=N(a,b.id);return d=m.setLoadingMembers(d,!1),d=m.setLoadingMessages(d,!1),w(d).then(function(){return a})}).then(function(){return S(a)})["catch"](function(a){var b=m.setLoadingMembers(q,!1);b=m.setLoadingMessages(b,!1),w(b),e.exception(a)})},P=function(a,b,c,d){var f=a.members.filter(function(a){return a.id==b.id});f.length<1&&(a.members=a.members.concat([b]));var g=N(a,b.id);g=m.setLoadingMembers(g,!1),g=m.setLoadingMessages(g,!0);var h=a.messages.length;return w(g).then(function(){if(h<c)return Q(a.id,c,h,d,[]).then(function(a){return a.messages});var b=m.setLoadingMessages(q,!1);return w(b).then(function(){return a.messages})}).then(function(a){return H(a.length),a}).then(function(){return S(a.id)})["catch"](e.exception)},Q=function(a,b,c,d,e,f){return h.getMessages(q.loggedInUserId,a,b?b+1:b,c,d,f).then(function(a){return a.messages.length&&e.length&&(a.messages=a.messages.filter(function(a){return e.indexOf(parseInt(a.id,10))<0})),a}).then(function(a){return b?(a.messages.length>b?a.messages=a.messages.slice(0,-1):J(!0),a):a}).then(function(a){var b=a.members.filter(function(a){return!(a.id in q.members)}),c=m.addMembers(q,b);return c=m.addMessages(c,a.messages),c=m.setLoadingMessages(c,!1),w(c).then(function(){return a})})["catch"](function(a){var b=m.setLoadingMessages(q,!1);throw w(b),a})},R=function(b,c){return function(){var d=q.messages,e=d.length?d[d.length-1]:null;if(e&&!u&&!v){for(var g=[],h=d.length-1;h>=0;h--){var j=d[h];if(j.timeCreated!==e.timeCreated)break;g.push(j.id)}return Q(b,0,0,c,g,e.timeCreated).then(function(a){if(a.messages.length){t.restart();var c=L(q);return f.publish(i.CONVERSATION_NEW_LAST_MESSAGE,c),S(b)}return a})}return a.Deferred().resolve().promise()}},S=function(a){var b=q.loggedInUserId;return h.markAllConversationMessagesAsRead(b,a).then(function(){var b=m.markMessagesAsRead(q,q.messages);return f.publish(i.CONVERSATION_READ,a),w(b)})},T=function(a){return ha(a).then(function(){var b=m.addPendingBlockUsersById(q,[a]);return w(b)})},U=function(a){var b=m.setLoadingConfirmAction(q,!0);return w(b).then(function(){return h.blockUser(q.loggedInUserId,a)}).then(function(b){var c=m.addMembers(q,[b]);return c=m.removePendingBlockUsersById(c,[a]),c=m.setLoadingConfirmAction(c,!1),f.publish(i.CONTACT_BLOCKED,a),w(c)})},V=function(a){return ha(a).then(function(){var b=m.addPendingUnblockUsersById(q,[a]);return w(b)})},W=function(a){var b=m.setLoadingConfirmAction(q,!0);return w(b).then(function(){return h.unblockUser(q.loggedInUserId,a)}).then(function(b){var c=m.addMembers(q,[b]);return c=m.removePendingUnblockUsersById(c,[a]),c=m.setLoadingConfirmAction(c,!1),f.publish(i.CONTACT_UNBLOCKED,a),w(c)})},X=function(a){return ha(a).then(function(){var b=m.addPendingRemoveContactsById(q,[a]);return w(b)})},Y=function(a){var b=m.setLoadingConfirmAction(q,!0);return w(b).then(function(){return h.deleteContacts(q.loggedInUserId,[a])}).then(function(b){var c=m.addMembers(q,b);return c=m.removePendingRemoveContactsById(c,[a]),c=m.setLoadingConfirmAction(c,!1),f.publish(i.CONTACT_REMOVED,a),w(c)})},Z=function(a){return ha(a).then(function(){var b=m.addPendingAddContactsById(q,[a]);return w(b)})},$=function(a){var b=m.setLoadingConfirmAction(q,!0);return w(b).then(function(){return h.createContactRequest(q.loggedInUserId,a)}).then(function(a){if(!a.request)throw new Error(a.warnings[0].message);return a.request}).then(function(b){var c=m.removePendingAddContactsById(q,[a]);return c=m.addContactRequests(c,[b]),c=m.setLoadingConfirmAction(c,!1),w(c)})},_=function(){var a=q.loggedInUserId,b=q.id;return h.setFavouriteConversations(a,[b]).then(function(){var a=m.setIsFavourite(q,!0);return w(a)}).then(function(){return f.publish(i.CONVERSATION_SET_FAVOURITE,L(q))})},aa=function(){var a=q.loggedInUserId,b=q.id;return h.unsetFavouriteConversations(a,[b]).then(function(){var a=m.setIsFavourite(q,!1);return w(a)}).then(function(){return f.publish(i.CONVERSATION_UNSET_FAVOURITE,L(q))})},ba=function(){var a=q.loggedInUserId,b=q.id;return h.setMutedConversations(a,[b]).then(function(){var a=m.setIsMuted(q,!0);return w(a)}).then(function(){return f.publish(i.CONVERSATION_SET_MUTED,L(q))})},ca=function(){var a=q.loggedInUserId,b=q.id;return h.unsetMutedConversations(a,[b]).then(function(){var a=m.setIsMuted(q,!1);return w(a)}).then(function(){return f.publish(i.CONVERSATION_UNSET_MUTED,L(q))})},da=function(a){var b=q.selectedMessageIds;return ha(a).then(function(){var a=m.addPendingDeleteMessagesById(q,b);return w(a)})},ea=function(){var a=q.pendingDeleteMessageIds,b=m.setLoadingConfirmAction(q,!0);return w(b).then(function(){return b.deleteMessagesForAllUsers?h.deleteMessagesForAllUsers(q.loggedInUserId,a):h.deleteMessages(q.loggedInUserId,a)}).then(function(){var b=m.removeMessagesById(q,a);b=m.removePendingDeleteMessagesById(b,a),b=m.removeSelectedMessagesById(b,a),b=m.setLoadingConfirmAction(b,!1),b=m.setDeleteMessagesForAllUsers(b,!1);var c=q.messages[q.messages.length-1],d=b.messages.length?b.messages[b.messages.length-1]:null;if(d&&d.id!=c.id){var e=L(b);f.publish(i.CONVERSATION_NEW_LAST_MESSAGE,e)}else b.messages.length||f.publish(i.CONVERSATION_DELETED,b.id);return w(b)})},fa=function(a){return ha(a).then(function(){var a=m.setPendingDeleteConversation(q,!0);return w(a)})},ga=function(){var a=m.setLoadingConfirmAction(q,!0);return w(a).then(function(){return h.deleteConversation(q.loggedInUserId,q.id)}).then(function(){var a=m.removeMessages(q,q.messages);return a=m.removeSelectedMessagesById(a,q.selectedMessageIds),a=m.setPendingDeleteConversation(a,!1),a=m.setLoadingConfirmAction(a,!1),f.publish(i.CONVERSATION_DELETED,a.id),w(a)})},ha=function(a){var b=q.pendingDeleteMessageIds,c=m.removePendingAddContactsById(q,[a]);return c=m.removePendingRemoveContactsById(c,[a]),c=m.removePendingUnblockUsersById(c,[a]),c=m.removePendingBlockUsersById(c,[a]),c=m.removePendingDeleteMessagesById(c,b),c=m.setPendingDeleteConversation(c,!1),c=m.setDeleteMessagesForAllUsers(c,!1),w(c)},ia=function(a){var b=q.loggedInUserId,c=q.members[a].contactrequests.filter(function(a){return a.requesteduserid==b}),d=c[0],e=m.setLoadingConfirmAction(q,!0);return w(e).then(function(){return h.acceptContactRequest(a,b)}).then(function(a){var b=m.removeContactRequests(q,[d]);return b=m.addMembers(q,[a]),b=m.setLoadingConfirmAction(b,!1),w(b)}).then(function(){f.publish(i.CONTACT_ADDED,q.members[a]),f.publish(i.CONTACT_REQUEST_ACCEPTED,d)})},ja=function(a){var b=q.loggedInUserId,c=q.members[a].contactrequests.filter(function(a){return a.requesteduserid==b}),d=c[0],e=m.setLoadingConfirmAction(q,!0);return w(e).then(function(){return h.declineContactRequest(a,b)}).then(function(a){var b=m.removeContactRequests(q,[d]);return b=m.addMembers(q,[a]),b=m.setLoadingConfirmAction(b,!1),w(b)}).then(function(){f.publish(i.CONTACT_REQUEST_DECLINED,d)})},ka=function(a,b){v=!0;var c=m.setSendingMessage(q,!0),d=null,g=!1;return w(c).then(function(){if(a||q.type==C.PUBLIC)return h.sendMessageToConversation(a,b);var c=D();return h.sendMessageToUser(c,b).then(function(a){return d=parseInt(a.conversationid,10),g=a.candeletemessagesforallusers,a})}).then(function(a){var b=m.addMessages(q,[a]);b=m.setSendingMessage(b,!1);var c=L(b);return b.id||(b=m.setId(b,d),c.id=d,Aa(d),f.publish(i.CONVERSATION_CREATED,c),b=m.setCanDeleteMessagesForAllUsers(b,g)),w(b).then(function(){v=!1,f.publish(i.CONVERSATION_NEW_LAST_MESSAGE,c)})})["catch"](function(a){v=!1;var b=m.setSendingMessage(q,!1);w(b),e.exception(a)})},la=function(a){var b=q;return b=q.selectedMessageIds.indexOf(a)>-1?m.removeSelectedMessagesById(q,[a]):m.addSelectedMessagesById(q,[a]),w(b)},ma=function(){return ha(D()).then(function(){var a=m.removeSelectedMessagesById(q,q.selectedMessageIds);return w(a)})},na=function(b,c,d,e){var f=function(a){return l.render(b,c,d,a)};if(!e){var g=m.buildInitialState(q.midnight,q.loggedInUserId,q.id),h=k.buildPatch(g,q);f(h)}return x.push(f),function(b){var c=k.buildPatch(q,b),d=x.map(function(a){return a(c)});return a.when.apply(null,d).then(function(){q=b,b.id&&(p[b.id]={state:b,messagesOffset:G(),loadedAllMessages:I()})})}},oa=function(a){return function(b,c){q.loadingConfirmAction||a(D())["catch"](function(a){var b=m.setLoadingConfirmAction(q,!1);w(b),e.exception(a)}),c.originalEvent.preventDefault()}},pa=function(b,c){var d=a(b.target),e=d.closest(B.FOOTER_CONTAINER),f=e.find(B.MESSAGE_TEXT_AREA),g=f.val().trim();""!==g&&ka(q.id,g),c.originalEvent.preventDefault()},qa=function(b,c){var d=window.getSelection(),f=a(b.target);if(""==d.toString()&&!f.is("a")){var g=f.closest(B.MESSAGE),h=parseInt(g.attr("data-message-id"),10);la(h)["catch"](e.exception),c.originalEvent.preventDefault()}},ra=function(a,b){ma()["catch"](e.exception),b.originalEvent.preventDefault()},sa=function(a){return function(b,c){var d=D(),e=q.members[d];n.go(a,o.VIEW_CONTACT,e),c.originalEvent.preventDefault()}},ta=function(a,b){_()["catch"](e.exception),b.originalEvent.preventDefault()},ua=function(a,b){aa()["catch"](e.exception),b.originalEvent.preventDefault()},va=function(a,b){ba()["catch"](e.exception),b.originalEvent.preventDefault()},wa=function(a,b){ca()["catch"](e.exception),b.originalEvent.preventDefault()},xa=function(b){var c=a(b.target).prop("checked"),d=m.setDeleteMessagesForAllUsers(q,c);w(d)},ya=function(a){return function(b,c){n.go(a,o.VIEW_GROUP_INFO,{id:q.id,name:q.name,subname:q.subname,imageUrl:q.imageUrl,totalMemberCount:q.totalMemberCount},q.loggedInUserId),c.originalEvent.preventDefault()}},za=function(a,c,g,h){var j=!1,k=K(g),l=[[B.ACTION_REQUEST_BLOCK,oa(T)],[B.ACTION_REQUEST_UNBLOCK,oa(V)],[B.ACTION_REQUEST_ADD_CONTACT,oa(Z)],[B.ACTION_REQUEST_REMOVE_CONTACT,oa(X)],[B.ACTION_REQUEST_DELETE_CONVERSATION,oa(fa)],[B.ACTION_CANCEL_EDIT_MODE,ra],[B.ACTION_VIEW_CONTACT,sa(a)],[B.ACTION_VIEW_GROUP_INFO,ya(a)],[B.ACTION_CONFIRM_FAVOURITE,ta],[B.ACTION_CONFIRM_MUTE,va],[B.ACTION_CONFIRM_UNFAVOURITE,ua],[B.ACTION_CONFIRM_UNMUTE,wa]],n=[[B.ACTION_CANCEL_CONFIRM,oa(ha)],[B.ACTION_CONFIRM_BLOCK,oa(U)],[B.ACTION_CONFIRM_UNBLOCK,oa(W)],[B.ACTION_CONFIRM_ADD_CONTACT,oa($)],[B.ACTION_CONFIRM_REMOVE_CONTACT,oa(Y)],[B.ACTION_CONFIRM_DELETE_SELECTED_MESSAGES,oa(ea)],[B.ACTION_CONFIRM_DELETE_CONVERSATION,oa(ga)],[B.ACTION_REQUEST_ADD_CONTACT,oa(Z)],[B.ACTION_ACCEPT_CONTACT_REQUEST,oa(ia)],[B.ACTION_DECLINE_CONTACT_REQUEST,oa(ja)],[B.MESSAGE,qa],[B.DELETE_MESSAGES_FOR_ALL_USERS_TOGGLE,xa]],p=[[B.SEND_MESSAGE_BUTTON,pa],[B.ACTION_REQUEST_DELETE_SELECTED_MESSAGES,oa(da)],[B.ACTION_REQUEST_ADD_CONTACT,oa(Z)],[B.ACTION_REQUEST_UNBLOCK,oa(V)]];b.init(h),d.define(c,[d.events.activate]),d.define(g,[d.events.activate]),d.define(h,[d.events.activate,d.events.enter]),d.define(k,[d.events.scrollTop,d.events.scrollLock]),k.on(d.events.scrollTop,function(a,b){var c=Object.keys(q.members).length>1;if(!u&&!j&&!I()&&c){j=!0;var d=m.setLoadingMessages(q,!0);w(d).then(function(){return Q(q.id,z,G(),y,[])}).then(function(){j=!1,H(G()+z)})["catch"](function(a){j=!1,e.exception(a)})}b.originalEvent.preventDefault()}),l.forEach(function(a){var b=a[0],e=a[1];c.on(d.events.activate,b,e)}),n.forEach(function(a){var b=a[0],c=a[1];g.on(d.events.activate,b,c)}),p.forEach(function(a){var b=a[0],c=a[1];h.on(d.events.activate,b,c)}),h.on(d.events.enter,B.MESSAGE_TEXT_AREA,function(a,b){var c=h.attr("data-enter-to-send");c&&"false"!=c&&"0"!=c&&pa(a,b)}),f.subscribe(i.ROUTE_CHANGED,function(a){t&&a.route!=o.VIEW_CONVERSATION&&t.stop()})},Aa=function(a){t&&t.stop(),t=new c(R(a,y),function(a){return a?2*a:A}),t.start()},Ba=function(a,b,c){var d=c.id,e=parseInt(a.attr("data-midnight"),10),f=m.buildInitialState(e,d,b);return q||(q=f),t&&t.stop(),w(f)},Ca=function(a,b,c){return Ba(a,null,b).then(function(){return b.id!=c?h.getConversationBetweenUsers(b.id,c,!0,!0,0,0,z,0,y):h.getSelfConversation(b.id,z,0,y)}).then(function(c){return Ea(a,c,b)})["catch"](function(){return M(b,c)})},Da=function(a,b,c){var d=null;return b in p&&(d=p[b]),Ba(a,b,c).then(function(){if(d){var a=d.state;return a=m.setLoadingMessages(a,!1),a=m.setLoadingMembers(a,!1),H(d.messagesOffset),J(d.loadedAllMessages),w(a)}return O(b,c,z,0,y)}).then(function(){return Aa(b)})},Ea=function(a,b,c){var d=null;return b.id in p&&(d=p[b.id]),Ba(a,b.id,c).then(function(){if(d){var a=d.state;return a=m.setLoadingMessages(a,!1),a=m.setLoadingMembers(a,!1),H(d.messagesOffset),J(d.loadedAllMessages),w(a)}return P(b,c,z,y)}).then(function(){return Aa(b.id)})},Fa=function(b,c,d,f,g,h,i){var k=null,l=null;g&&null!==g&&"object"==typeof g?(k=g,l=parseInt(k.id,10)):(k=null,l=parseInt(g,10),l=isNaN(l)?null:l),!l&&h&&i&&(l=E(i));var m=!q||q.id!=l||i&&i!=D();if(d.attr("data-init")||(w=na(c,d,f,m),za(b,c,d,f),d.attr("data-init",!0)),m){u=!0;var n=null,o=F(d);return n=k?Ea(d,k,o,i):l?Da(d,l,o,i):Ca(d,o,i),n.then(function(){u=!1,c.find(j.SELECTORS.CAN_RECEIVE_FOCUS).first().focus()})["catch"](function(a){u=!1,e.exception(a)})}if(Aa(l),q.type==C.PRIVATE&&h){var p=D();switch(h){case"block":return T(p);case"unblock":return V(p);case"add-contact":return Z(p);case"remove-contact":return X(p)}}return a.Deferred().resolve().promise()},Ga=function(){return g.get_string("messagedrawerviewconversation","core_message",q.name)};return{show:Fa,description:Ga}});