Page Forum
A very early attempt at making a mini-forum in Confluence...
You can see this script in action on the XEN Wiki.
Requirements
This script makes use of Community Bubbles plugin to add in profile pictures and microformats (hCard). Without that plugin you'll get errors in the Author and Last Reply columns.
The plugin also uses the Builder Theme plugin's menuicon macro to display the
icon. Without this plugin you'll see an error where the icon should appear.
Script: forum
Name: forum
Author(s): Guy Fraser
Scriptix Version: 1.1.1 or above
Scripting Engine: Mozilla Rhino
Components: subRenderer
Vectors: Wiki Macro
View source...
output = '{div:class=forum}\n';
output += '||New||Topic||Author||Date||Replies||Last Reply||Date||\n';
var forum = scriptix.pageManager.getPage('XEN','Forum');
var topics = forum.descendents;
var attachments = function(topic) { // show attachment icon if attachments present
var x = topic.attachments.size();
if (x>0) {
var s = (x>1) ? 's' : '';
return ' {menulink:pageattachments|page='+topic.title
+'|tooltip='+x+' attachment'+s+'}{menuicon:paperclip}{menulink}';
} else {
return '';
}
}
var author = function(topic) { // format author for display
return '{my-picture:'+topic.lastModifierName+'|showControls=false|size=16}';
}
var formatDate = function(date) { // format a date for output
// want to eventually update this to do friendly dates, eg. "today", etc
// if i can access generalUtil.getRelativeTime that will be easy ;)
return scriptix.formatDate(date, 'dd MMM yyyy');
}
var isNew = function(topic) { // are there new posts since user last logged in?
if (!user) {
return false;
} else {
return topic.isRecentlyUpdatedFor(user);
}
}
var newIndicator = function(topic,reply) {
return (isNew(topic) || isNew(reply)) ? '(on)' : '(off)';
}
var getReply = function(comments) { // get most recent comment object
var x = comments.size();
var last = (x>0) ? comments.get(x-1) : null;
var current;
while (-1<--x) {
current = comments.get(x);
if (current.creationDate.after(last.creationDate)) last = current;
}
return last;
}
var lastReplyDate = function(reply) {
return (reply != null) ? formatDate(reply.creationDate) : '-';
}
var lastReplyAuthor = function(reply) {
return (reply != null) ? '{my-picture:'+reply.creatorName+'|showControls=false|size=16}' : '-';
}
var x = topics.size();
var topic;
var reply;
while (-1<--x) {
topic = topics.get(x); // get the page object
reply = getReply(topic.comments); // get the last reply
output += '| '+newIndicator(topic,reply)+' | '
+'['+topic.title+']'+attachments(topic)+' | '
+author(topic)+' | '
+formatDate(topic.lastModificationDate)+' | '
+topic.comments.size()+' | '
+lastReplyAuthor(reply)+' | '
+lastReplyDate(reply)+' |\n';
}
output += '*Key:* (on) New Posts (off) No New Posts {menuicon:paperclip} Topic Has Attachments\n';
output += '{div}';
output = scriptix.subRenderer.render(output, macro.renderContext);
Known issues
- New posts indicator relies on Confluence's .isRecentlyUpdatedFor() method which uses a basic check against your last login and any new edits or comments on the page - as such, you have to log-out and then log-in again to get it to update
- New comments don't make topics go to the top of the list - this should be fairly simple to fix by creating an array of topics then sorting the array
To-Do
It would be nice to do the following:
- Use a servlet to spit out XML so that the display could be AJAXified
- Have a servlet spit out RSS feed
- Get the "new posts" indicator working properly - this would likely involve storing data on the pages to say when each user last visited them, or find some way to hook in to Confluence's Activity Tracking plugin (ie. the Lucene search index it uses)