Merge pull request #1305 from anandpdoshi/messages-realtime

Realtime fixes for communication in timeline and messages page
This commit is contained in:
Anand Doshi 2015-09-14 16:34:01 +05:30
commit 01334c7a8a
8 changed files with 60 additions and 18 deletions

View file

@ -145,6 +145,7 @@ def emit_via_redis(event, message, room):
try:
r.publish('events', frappe.as_json({'event': event, 'message': message, 'room': room}))
except redis.exceptions.ConnectionError:
# print frappe.get_traceback()
pass
def put_log(line_no, line, task_id=None):

View file

@ -174,6 +174,10 @@ def files_dirty():
return False
def compile_less():
from distutils.spawn import find_executable
if not find_executable("lessc"):
return
for path in app_paths:
less_path = os.path.join(path, "public", "less")
if os.path.exists(less_path):
@ -189,4 +193,4 @@ def compile_less():
print "compiling {0}".format(fpath)
css_path = os.path.join(path, "public", "css", fname.rsplit(".", 1)[0] + ".css")
os.system("which lessc && lessc {0} > {1}".format(fpath, css_path))
os.system("lessc {0} > {1}".format(fpath, css_path))

View file

@ -42,7 +42,8 @@ class Comment(Document):
message['broadcast'] = True
frappe.publish_realtime('new_message', message)
else:
frappe.publish_realtime('new_message', self.as_dict(), user=frappe.session.user)
# comment_docname contains the user who is addressed in the messages' page comment
frappe.publish_realtime('new_message', self.as_dict(), user=self.comment_docname)
else:
frappe.publish_realtime('new_comment', self.as_dict(), doctype= self.comment_doctype,
docname = self.comment_docname)

View file

@ -33,6 +33,16 @@ class Communication(Document):
else:
self.status = "Open"
def after_insert(self):
# send new comment to listening clients
comment = self.as_dict()
comment["comment"] = comment["content"]
comment["comment_by"] = comment["sender"]
comment["comment_type"] = comment["communication_medium"]
frappe.publish_realtime('new_comment', comment, doctype = self.reference_doctype,
docname = self.reference_name)
def on_update(self):
"""Update parent status as `Open` or `Replied`."""
self.update_parent()
@ -49,7 +59,7 @@ class Communication(Document):
to_status = "Open" if self.sent_or_received=="Received" else "Replied"
if to_status in status_field.options.splitlines():
frappe.db.set_value(parent.doctype, parent.name, "status", to_status)
parent.db_set("status", to_status)
parent.notify_update()

View file

@ -41,6 +41,7 @@ frappe.desk.pages.Messages = Class.extend({
},
setup_realtime: function() {
var me = this;
frappe.realtime.on('new_message', function(comment) {
if(comment.modified_by !== user) {
frappe.utils.notify(__("Message from {0}", [comment.comment_by_fullname]), comment.comment);
@ -48,16 +49,20 @@ frappe.desk.pages.Messages = Class.extend({
if (frappe.get_route()[0] === 'messages') {
var current_contact = $(cur_page.page).find('[data-contact]').data('contact');
var on_broadcast_page = current_contact === user;
if (current_contact == comment.owner || (on_broadcast_page && comment.broadcast)) {
var $row = $('<div class="list-row"/>');
frappe.desk.pages.messages.list.data.unshift(comment);
frappe.desk.pages.messages.list.render_row($row, comment);
frappe.desk.pages.messages.list.parent.prepend($row);
if ((current_contact == comment.owner) || (on_broadcast_page && comment.broadcast)) {
me.prepend_comment(comment);
}
}
});
},
prepend_comment: function(comment) {
var $row = $('<div class="list-row"/>');
frappe.desk.pages.messages.list.data.unshift(comment);
frappe.desk.pages.messages.list.render_row($row, comment);
frappe.desk.pages.messages.list.$w.prepend($row);
},
make_sidebar: function() {
var me = this;
return frappe.call({
@ -124,7 +129,9 @@ frappe.desk.pages.Messages = Class.extend({
},
callback:function(r,rt) {
textarea.val('');
me.list.run();
if (!r.exc) {
me.prepend_comment(r.message);
}
},
btn: this
});

View file

@ -89,6 +89,8 @@ def post(txt, contact, parenttype=None, notify=False, subject=None):
else:
_notify(contact, txt, subject)
return d
@frappe.whitelist()
def delete(arg=None):
frappe.get_doc("Comment", frappe.form_dict['name']).delete()

View file

@ -154,10 +154,11 @@ $.extend(frappe.model, {
},
new_comment: function(comment) {
if (frappe.model.docinfo[comment.comment_doctype]
&& frappe.model.docinfo[comment.comment_doctype][comment.comment_docname]) {
var comments = frappe.model.docinfo[comment.comment_doctype][comment.comment_docname].comments;
var reference_doctype = comment.comment_doctype || comment.reference_doctype;
var reference_name = comment.comment_docname || comment.reference_name;
if (frappe.model.docinfo[reference_doctype] && frappe.model.docinfo[reference_doctype][reference_name]) {
var comments = frappe.model.docinfo[reference_doctype][reference_name].comments;
var comment_exists = false;
for (var i=0, l=comments.length; i<l; i++) {
if (comments[i].name==comment.name) {
@ -165,12 +166,13 @@ $.extend(frappe.model, {
break;
}
}
if (!comment_exists) {
frappe.model.docinfo[comment.comment_doctype][comment.comment_docname].comments = comments.concat([comment]);
frappe.model.docinfo[reference_doctype][reference_name].comments = comments.concat([comment]);
}
}
if (cur_frm.doctype === comment.comment_doctype && cur_frm.docname === comment.comment_docname) {
cur_frm.comments.refresh();
if (cur_frm.doctype === reference_doctype && cur_frm.docname === reference_name) {
cur_frm.comments.refresh();
}
},

View file

@ -25,10 +25,13 @@ io.on('connection', function(socket){
if (get_hostname(socket.request.headers.host) != get_hostname(socket.request.headers.origin)) {
return;
}
// console.log("connection!");
var sid = cookie.parse(socket.request.headers.cookie).sid
if(!sid) {
return;
}
// console.log("firing get_user_info");
request.post(get_url(socket, '/api/method/frappe.async.get_user_info'))
.type('form')
.send({
@ -45,16 +48,19 @@ io.on('connection', function(socket){
socket.join(room);
socket.join(get_site_room(socket));
}
})
});
socket.on('task_subscribe', function(task_id) {
var room = 'task:' + task_id;
socket.join(room);
})
});
socket.on('progress_subscribe', function(task_id) {
var room = 'task_progress:' + task_id;
socket.join(room);
send_existing_lines(task_id, socket);
})
});
socket.on('doc_subscribe', function(doctype, docname) {
// console.log('trying to subscribe', doctype, docname)
request.post(get_url(socket, '/api/method/frappe.async.can_subscribe_doc'))
@ -66,6 +72,10 @@ io.on('connection', function(socket){
})
.end(function(err, res) {
if(err) console.log(err);
if(!res) {
console.log("No response for doc_subscribe");
return;
}
if(res.status == 200) {
var room = get_doc_room(socket, doctype, docname);
// console.log('joining', room)
@ -73,10 +83,15 @@ io.on('connection', function(socket){
}
})
});
socket.on('doc_unsubscribe', function(doctype, docname) {
var room = get_doc_room(socket, doctype, docname);
socket.leave(room);
});
// socket.on('disconnect', function (arguments) {
// console.log("user disconnected", arguments);
// });
});
function send_existing_lines(task_id, socket) {