Thanks for all the people who send me PM today and allowed me to get the plugin to version 1.0
The code is obscure and safe and no warranty is provided.
Features
- direct access to the last ~15 pm
- shows if new or how many unread messages in a conversation
- quick preview of the last replies inline
- participants avatars in conversation, hover to get name tooltip if needed.
Known stuff & miscellaneous
* preview messages does not show date and time - what a shame
* The plugin does not auto-refresh, if you want to update you have to open the envelope.
the other notification will show you if you have new message anyway.
* I started the idea of adding an inline reply in the preview section to provide a chat like interface but this feature is not there yet.
-
run in console and should work if add to an extension(greasemonkey)
-
security: The preview functionality almost got me with xss. I was trying to show a text version only due to visual issues,The conversion allowed html tag to be rendered. I moved back to just rendering the "cooked" version
and instead added some styling to fix the visual issues. I consider it pretty safe on that aspect, but testing is welcomed.
* there is a small bug, it may show 1 even if there are no messages need attention blah
- 1/20/2014: click on @mention in the preview will go to the user page as a new page which will reset the widget
###Inline preview of the last replies
###code (Version 1.4.5)
(function(f){f.Styler||(f.Styler=new function(){var c={};this.loadStyle=function(a,h){c.hasOwnProperty(a)?0==c[a].added&&(c[a].content=h,this.appendStyle(a)):(c[a]={content:h,added:!1},this.appendStyle(a))};this.listLoadedStyles=function(){};this.removeStyle=function(a){c.hasOwnProperty(a)&&(c[a].added=!1,f("#Styler_"+a).remove())};this.appendStyle=function(a){c.hasOwnProperty(a)&&(c[a].added=!0,a=f("<style id='Styler_"+a+"'>"+c[a].content+"</style>"),f("body").append(a))}})})(jQuery);
window.APIHelper=new function(){this.get_last_page=function(f,c){window.Messages_plugin.getCache();var a=window.Messages_plugin.get_topic_data(f),h=a.preview_posts.last_post_number||1,n=void 0===a.preview_posts.last_post_number,k=Math.ceil(h/20);$.getJSON("http://what.thedailywtf.com/t/"+f+".json?page="+k,function(a){var g=Math.ceil(a.posts_count/20),q=20-(20-h%20);g>k?0==q||n?$.getJSON("http://what.thedailywtf.com/t/"+f+".json?page="+g,c):c(a):c(a)})}};
window.Messages_plugin=function(){function f(a){if(null===g.json_result||null===g.last_fetch_time||1E4<Date.now()-g.last_fetch_time){var b=Discourse.User.current();b&&(b="http://what.thedailywtf.com/topics/private-messages/"+b.username+".json?_="+Date.now(),$.getJSON(b).then(function(b){g.json_result=b;a(b)}),g.last_fetch_time=Date.now())}else a(g.json_result)}function c(a,b){var p=$("<div></div>");_.forEach(a.posters,function(d){var e=_.find(b.users,{id:d.user_id});d=e.username;e=$("<img src='"+
e.avatar_template.replace("{size}","20")+"'></img>");e.attr("title",d);p.append(e)});return p}function a(a){var b=a.topic_list.topics;n.find("div.loading").hide();g.unseen=0;var p=0<n.find("li").length;_.first(b,15).forEach(function(d){if(l.hasOwnProperty(d.id))return!0;var e="<span style='float:right;margin-top:-2px;padding:3px;border-radius: 3px;'>"+Discourse.Formatter.relativeAge(new Date(d.last_posted_at),{format:"tiny",wrapInSpan:!1})+"</span>",e=$("<li><a>"+Handlebars.Utils.escapeExpression(d.title)+
"<span>"+e+"</span></a></li>");e.data("topic",d.id);e.data("preview-posts",{preview_rendered:!1});e.attr("data-topic-id",d.id);e.data("preview-posts");var b=$(e.find("a")),f="",m=d.posts_count;d.unseen&&(e.addClass("unread"),e.css("background","#D6F5FF"),b.append($("<span class='badge-notification new-posts'>new</span>")));null!==d.last_read_post_number&&(f="/"+d.last_read_post_number,m-=d.last_read_post_number);0<m&&(b.append($('<span class="badge-notification">'+m+"</span>")),g.unseen+=1);b.append(c(d,
a));var m=$("<div class='last-message'><div class='messages'></div></div>"),k=m.find("div.messages");k.css({"overflow-y":"auto","max-height":"209px"});var q=$("<div class='quick_preview'><span style='background:#fff;color:#979797;border-radius: 3px;cursor:pointer' class='qr_btn badge-notification'>preview</span></div>");e.data("preview_rendered",!1);q.click(function(){l[d.id].preview_posts.preview_rendered&&$(l[d.id].last_message_preview).toggle();"none"!=$(l[d.id].last_message_preview).css("display")&&
h.update_topic_preview(d.id)});e.append(q);e.data("last-message",m);e.append(m);b.attr("href","/t/"+d.slug+"/"+d.id+f);e.css({padding:"5px 5px 5px 5px","margin-bottom":"2px"});p?n.prepend(e):n.append(e);l.hasOwnProperty[d.id]||(l[d.id]={model:d,element:e,preview_rendered:!1,last_message_preview:m,last_message_preview_messages:k,preview_posts:{},active:!1})});0<g.unseen?(_pm_notification.html(g.unseen),_pm_notification.css("display","block"),$("#user-messages").css("color","#48AAEF")):(_pm_notification.html("0"),
_pm_notification.css("display","none"),$("#user-messages").css("color","#999"))}var h=this,n=null,k=null,l={},g={unseen:0,last_fetch_time:null,json_result:null,last_notifications_history_unread:{},chunk_size:null};this.debug=function(){return{self:h,_list_outlet:n,_dropdown:k,_max_topics:15,_max_preview_messages:3,_list_items:l,_cache:g}};var q=function(a,b){var p=a.preview_posts,d=a.last_message_preview_messages;_.forEach(b,function(a){if(p.hasOwnProperty(a.post_number))return!0;p[a.post_number]=
!0;p.last_post_number=a.post_number;var b=$("<div></div>");b.addClass("preview-stream-msg");b.attr("data-post-number",a.post_number);b.css({background:"#E6E6E6",margin:"3px","border-radius":"6px",padding:"5px"});var c=$("<div>"+a.username+"</div>");c.css({display:"inline-block","font-weight":"bold"});var f=$("<span>"+Discourse.Formatter.relativeAge(new Date(a.updated_at),{format:"tiny",wrapInSpan:!1})+"</span>");f.addClass("relative-date");f.attr({"data-time":new Date(a.updated_at),"data-format":"tiny"});
f.css({color:"#9AAFFF","margin-left":"5px"});var g=$("<img src='"+a.avatar_template.replace("{size}","20")+"'></img>");g.css({"margin-right":"5px"});c.prepend(g);a=$("<div></div>").html(a.cooked);b.append(c).append(f).append(a);d.append(b)})},r=function(a){var b=h.get_topic_data(a),c=b.preview_posts,d=b.element;if(1!=c.preview_rendered){c.preview_rendered=!0;d.addClass("active_preview");b.active=!0;var c=$("<div class='quick_reply_container'><div class='qr_input_container' ><textarea></textarea></div><span class='quick-reply-button badge-notification'><i class='fa fa-reply'></i> reply</span></div>"),
e=c.find("span.quick-reply-button"),f=c.find("textarea");f.keydown(function(a){a.ctrlKey&&13==a.keyCode&&e.click()});f.focus(function(){d[0].scrollIntoView();$("#messages-dropdown").css({overflow:"hidden"})});f.blur(function(){$("#messages-dropdown").css({overflow:"auto"})});e.click(function(){var b=$(this).find("i");b.addClass("fa-spin");$.post("/posts",{raw:f.val(),category:"",topic_id:a,is_warning:"false",archetype:"regular"}).done(function(){f.val("");b.removeClass("fa-spin");h.update_topic_preview(a)})});
f.css({background:"#E2E2E2",width:"97%",height:"45px","margin-bottom":"0px","margin-top":"3px","padding-bottom":"0px"});e.css({margin:"5px 0px 5px 0px",border:"1px solid #8D8EE3",background:"#3D7FFF","border-radius":"4px",cursor:"pointer"});b.last_message_preview.append(c)}};this.get_topic_data=function(a){return l.hasOwnProperty(a)?l[a]:!1};this.update_topic_preview=function(a){var b=h.get_topic_data(a);APIHelper.get_last_page(a,function(c){b.element.css("background","#BDD8FE");var d=b.last_message_preview_messages;
d.css({width:"100%",background:"#F5F5F5","max-height":"250px",margin:"-2px 0px 5px 0px",overflow:"auto"});c=_.last(c.post_stream.posts,3);var e=d[0].scrollHeight-d[0].scrollTop==d[0].clientHeight;q(b,c);e&&d.animate({scrollTop:d[0].scrollHeight},"slow");r(a)})};this.getMessages=function(){f(a);this.showDropdown()};this.showDropdown=function(){k.css("display","block");$("#messages_menu").addClass("active")};this.hideDropdown=function(){k.css("display","none");$("#messages_menu").removeClass("active")};
this.prepare=function(){f(function(c){a(c)});h.hideDropdown()};this.getCache=function(){return g};(function(){var a=$("ul[role=navigation] li.notifications"),b=$("<li id='messages_menu' class='messages'><a class='icon' href='#' id='user-messages'><i class='fa fa-envelope-o'></i><span class='sr-only'>notifications of @name mentions, replies to your posts and topics, private messages, etc</span></a></li>"),b=$(b);b.insertBefore(a);_pm_notification=$('<a id="pm-notification" href="#" style="background:#48AAEF;margin-left:-14px;" class="badge-notification"></a>');
_pm_notification.css("display","none");$("#user-messages").append(_pm_notification);$("#messages_menu").click(function(a){a.preventDefault();"block"==k.css("display")?h.hideDropdown():h.getMessages()})})();(function(){var a=$("div.panel"),b=$("<section class='d-dropdown' id='messages-dropdown'> <div id='messages_container'><ul><div class='loading'>Loading ... </div></ul></div> </section>");b.css("width","480px");k=b;n=b.find("ul");a.append(k)})()};
function hook_to_user_notifications(){window.MessageBus.subscribe("/notification/"+Discourse.User.current().id,function(f){window.last_notification_call&&300>Date.now()-window.last_notification_call?window.last_notification_call=Date.now():Discourse.ajax("/notifications/history.json").then(function(c){c=_.where(c,{read:!1,notification_type:6});_.forEach(c,function(a){window.Messages_plugin.get_topic_data(a.topic_id).active&&window.Messages_plugin.update_topic_preview(a.topic_id);window.last_notification_call=
Date.now()});window.Messages_plugin.getCache().last_notifications_history_unread=c})})}$("document").ready(function(){Em.run.next(function(){hook_to_user_notifications();window.Messages_plugin=new window.Messages_plugin;window.Messages_plugin.prepare()})});$.Styler.loadStyle("custom-pm-styles","#messages-dropdown li:hover .last-message a{display:inline-block}#messages-dropdown li .last-message a{display:inline-block!important}#messages-dropdown li .last-message a.mention,#messages-dropdown li:hover .last-message a.mention{background:#cfcfcf!important}#messages-dropdown .last-message a:not(.mention){background:0 0;display:inline-block}#messages-dropdown .title{float:none;display:block;height:auto};");
$.Styler.loadStyle("custom-pm-styles2","div.last-message::-webkit-scrollbar{width:8px;height:16px}div.last-message::-webkit-scrollbar-track {-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); }div.last-message::-webkit-scrollbar-thumb {-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5);background: rgba(162, 162, 162, 0.49); }#messages_container .last-message .preview-stream-msg {background:#fff !important;border:1px solid #cecece;}.qr_btn{margin-top:3px;}#messages_container li:hover a:not(.badge-notification) {background: rgba(138, 138, 138, 0.1)!important;}#messages_container li{ margin-top:5px;}.quick-reply-button:hover{background:#519FFF!important;}");