@ -3,10 +3,11 @@ odoo.define('mail_private', function (require) {
var core = require ( 'web.core' ) ;
var core = require ( 'web.core' ) ;
var Chatter = require ( 'mail.Chatter' ) ;
var Chatter = require ( 'mail.Chatter' ) ;
var Mail Composer = require ( 'mail_base.base' ) . MailComposer ;
var chat_manager = require ( 'mail.chat_manager' ) ;
var Chatter Composer = require ( 'mail.ChatterComposer' ) ;
var chat_manager = require ( 'mail_base.base' ) . chat_manager ;
var session = require ( 'web.session' ) ;
var session = require ( 'web.session' ) ;
var Model = require ( 'web.Model' ) ;
var rpc = require ( 'web.rpc' ) ;
var config = require ( 'web.config' ) ;
var config = require ( 'web.config' ) ;
var utils = require ( 'mail.utils' ) ;
var utils = require ( 'mail.utils' ) ;
@ -18,97 +19,126 @@ Chatter.include({
this . events [ 'click .oe_compose_post_private' ] = 'on_open_composer_private_message' ;
this . events [ 'click .oe_compose_post_private' ] = 'on_open_composer_private_message' ;
} ,
} ,
on_post_message : function ( message ) {
on_open_com poser_priva te _message : function ( event ) {
var self = this ;
var self = this ;
if ( this . private ) {
message . subtype = false ;
}
var options = { model : this . model , res_id : this . res_id } ;
chat_manager . post_message ( message , options ) . then (
function ( ) {
self . close_composer ( ) ;
if ( message . partner_ids . length ) {
self . refresh_followers ( ) ;
}
} ) . fail ( function ( ) {
// todo: display notification
this . fetch_recipients_for_internal_message ( ) . then ( function ( data ) {
self . _openComposer ( {
is_private : true ,
suggested_partners : data
} ) ;
} ) ;
} ) ;
} ,
} ,
on_open_composer_private_message : function ( event ) {
_openComposer : function ( options ) {
var self = this ;
var self = this ;
this . private = true ;
this . get_recipients_for_internal_message ( ) . then ( function ( data ) {
self . recipients_for_internal_message = data ;
self . open_composer ( { is_private : true } ) ;
var old_composer = this . composer ;
// create the new composer
this . composer = new ChatterComposer ( this , this . record . model , options . suggested_partners || [ ] , {
commands_enabled : false ,
context : this . context ,
input_min_height : 50 ,
input_max_height : Number . MAX_VALUE ,
input_baseline : 14 ,
is_log : options && options . is_log ,
record_name : this . record_name ,
default_body : old_composer && old_composer . $input && old_composer . $input . val ( ) ,
default_mention_selections : old_composer && old_composer . mention_get_listener_selections ( ) ,
is_private : options . is_private
} ) ;
} ) ;
} ,
this . composer . on ( 'input_focused' , this , function ( ) {
this . composer . mention_set_prefetched_partners ( this . mentionSuggestions || [ ] ) ;
} ) ;
this . composer . insertAfter ( this . $ ( '.o_chatter_topbar' ) ) . then ( function ( ) {
// destroy existing composer
if ( old_composer ) {
old_composer . destroy ( ) ;
}
if ( ! config . device . touch ) {
self . composer . focus ( ) ;
}
self . composer . on ( 'post_message' , self , function ( message ) {
if ( options . is_private ) {
self . composer . options . is_log = true ;
}
self . fields . thread . postMessage ( message ) . then ( function ( ) {
on_open_composer_new_message : function ( ) {
this . _super . apply ( this , arguments ) ;
this . private = false ;
self . _closeComposer ( true ) ;
if ( self . postRefresh === 'always' || ( self . postRefresh === 'recipients' && message . partner_ids . length ) ) {
self . trigger_up ( 'reload' ) ;
}
} ) ;
} ) ;
var toggle_post_private = self . composer . options . is_private || false ;
self . composer . on ( 'need_refresh' , self , self . trigger_up . bind ( self , 'reload' ) ) ;
self . composer . on ( 'close_composer' , null , self . _closeComposer . bind ( self , true ) ) ;
self . $el . addClass ( 'o_chatter_composer_active' ) ;
self . $ ( '.o_chatter_button_new_message, .o_chatter_button_log_note, .oe_compose_post_private' ) . removeClass ( 'o_active' ) ;
self . $ ( '.o_chatter_button_new_message' ) . toggleClass ( 'o_active' , ! self . composer . options . is_log && ! self . composer . options . is_private ) ;
self . $ ( '.o_chatter_button_log_note' ) . toggleClass ( 'o_active' , ( self . composer . options . is_log && ! options . is_private ) ) ;
self . $ ( '.oe_compose_post_private' ) . toggleClass ( 'o_active' , toggle_post_private ) ;
} ) ;
} ,
} ,
open_composer : function ( options ) {
fetch_recipients_for_internal_message : function ( ) {
var self = this ;
var self = this ;
this . _super . apply ( this , arguments ) ;
if ( options && options . is_private ) {
this . composer . options . is_private = options . is_private ;
_ . each ( self . recipients_for_internal_message , function ( partner ) {
self . composer . suggested_partners . push ( {
checked : ( partner . user_ids . length > 0 ) ,
partner_id : partner . id ,
full_name : partner . name ,
name : partner . name ,
email_address : partner . email ,
reason : _ . include ( partner . user_ids , self . session . uid )
? 'Partner'
: 'Follower'
self . result = { } ;
var follower_ids_domain = [ [ 'id' , '=' , self . context . default_res_id ] ] ;
return rpc . query ( {
model : 'mail.message' ,
method : 'send_recepients_for_internal_message' ,
args : [ [ ] , self . context . default_model , follower_ids_domain ]
} ) . then ( function ( res ) {
return _ . filter ( res , function ( obj ) {
return obj . partner_id !== session . partner_id ;
} ) ;
} ) ;
} ) ;
} ) ;
}
}
} ) ;
ChatterComposer . include ( {
init : function ( parent , model , suggested_partners , options ) {
this . _super ( parent , model , suggested_partners , options ) ;
this . events [ 'click .oe_composer_uncheck' ] = 'on_uncheck_recipients' ;
} ,
} ,
get_recipients_for_internal_message : function ( ) {
preprocess _message: function ( ) {
var self = this ;
var self = this ;
self . result = { } ;
return new Model ( this . context . default_model ) . query (
[ 'message_follower_ids' , 'partner_id' ] ) . filter (
[ [ 'id' , '=' , self . context . default_res_id ] ] ) . all ( ) .
then ( function ( thread ) {
var follower_ids = thread [ 0 ] . message_follower_ids ;
self . result [ self . context . default_res_id ] = [ ] ;
self . customer = thread [ 0 ] . partner_id ;
// Fetch partner ids
return new Model ( 'mail.followers' ) . call (
'read' , [ follower_ids , [ 'partner_id' ] ] ) . then ( function ( res_partners ) {
// Filter result and push to array
var res_partners_filtered = _ . map ( res_partners , function ( partner ) {
if ( partner . partner_id [ 0 ] && partner . partner_id [ 0 ] !== session . partner_id ) {
return partner . partner_id [ 0 ] ;
}
} ) . filter ( function ( partner ) {
return typeof partner !== 'undefined' ;
var def = $ . Deferred ( ) ;
this . _super ( ) . then ( function ( message ) {
message = _ . extend ( message , {
subtype : 'mail.mt_comment' ,
message_type : 'comment' ,
content_subtype : 'html' ,
context : self . context ,
} ) ;
} ) ;
return new Model ( 'res.partner' ) . call (
'read' , [ res_partners_filtered , [ 'name' , 'email' , 'user_ids' ] ]
) . then ( function ( recipients ) {
return recipients ;
} ) ;
// Subtype
if ( self . options . is_log ) {
message . subtype = 'mail.mt_note' ;
}
if ( self . options . is_private ) {
message . is_private = true ;
message . channel_ids = self . get_checked_channel_ids ( ) ;
}
// Partner_ids
if ( ! self . options . is_log ) {
var checked_suggested_partners = self . get_checked_suggested_partners ( ) ;
self . check_suggested_partners ( checked_suggested_partners ) . done ( function ( partner_ids ) {
message . partner_ids = ( message . partner_ids || [ ] ) . concat ( partner_ids ) ;
// update context
message . context = _ . defaults ( { } , message . context , {
mail_post_autofollow : true ,
} ) ;
} ) ;
def . resolve ( message ) ;
} ) ;
} ) ;
} else {
def . resolve ( message ) ;
}
}
} ) ;
} ) ;
MailComposer . include ( {
init : function ( parent , dataset , options ) {
this . _super ( parent , dataset , options ) ;
this . events [ 'click .oe_composer_uncheck' ] = 'on_uncheck_recipients' ;
return def ;
} ,
} ,
on_uncheck_recipients : function ( ) {
on_uncheck_recipients : function ( ) {
@ -121,7 +151,6 @@ MailComposer.include({
if ( ! this . do_check_attachment_upload ( ) ) {
if ( ! this . do_check_attachment_upload ( ) ) {
return false ;
return false ;
}
}
var self = this ;
var self = this ;
var recipient_done = $ . Deferred ( ) ;
var recipient_done = $ . Deferred ( ) ;
if ( this . options . is_log ) {
if ( this . options . is_log ) {
@ -138,6 +167,7 @@ MailComposer.include({
default_partner_ids : partner_ids ,
default_partner_ids : partner_ids ,
default_is_log : self . options . is_log ,
default_is_log : self . options . is_log ,
mail_post_autofollow : true ,
mail_post_autofollow : true ,
is_private : self . options . is_private ,
} ;
} ;
if ( self . options && self . options . is_private ) {
if ( self . options && self . options . is_private ) {
@ -148,7 +178,6 @@ MailComposer.include({
context . default_model = self . context . default_model ;
context . default_model = self . context . default_model ;
context . default_res_id = self . context . default_res_id ;
context . default_res_id = self . context . default_res_id ;
}
}
self . do_action ( {
self . do_action ( {
type : 'ir.actions.act_window' ,
type : 'ir.actions.act_window' ,
res_model : 'mail.compose.message' ,
res_model : 'mail.compose.message' ,
@ -174,9 +203,27 @@ MailComposer.include({
_ . each ( checked_partners , function ( partner ) {
_ . each ( checked_partners , function ( partner ) {
partner . checked = true ;
partner . checked = true ;
} ) ;
} ) ;
checked_partners = _ . uniq ( _ . filter ( checked_partners , function ( obj ) {
return obj . reason !== 'Channel' ;
} ) ) ;
this . get_checked_channel_ids ( ) ;
return checked_partners ;
return checked_partners ;
} ,
} ,
get_checked_channel_ids : function ( ) {
var self = this ;
var checked_channels = [ ] ;
this . $ ( '.o_composer_suggested_partners input:checked' ) . each ( function ( ) {
var full_name = $ ( this ) . data ( 'fullname' ) ;
checked_channels = checked_channels . concat ( _ . filter ( self . suggested_partners , function ( item ) {
return full_name === item . full_name ;
} ) ) ;
} ) ;
checked_channels = _ . uniq ( _ . filter ( checked_channels , function ( obj ) {
return obj . reason === 'Channel' ;
} ) ) ;
return _ . pluck ( checked_channels , 'channel_id' ) ;
}
} ) ;
} ) ;
} ) ;
} ) ;