How do I... Simulate a discussion board

Table of contents
No headers

The following code simulates a simple forum inside a wiki.  Use the "Add Comment" (at the bottom of the page) to reply.

Note: an updated, template-based version may be found here.  Once we figure out a way to properly present templates on this wiki, we'll pull it back here.

<p>{{wiki.create("Create New Topic",page.path,_,true,"Put Your Title Here")}}</p>
<table cellspacing="0" cellpadding="4" border="1" block="var topics = [];
    foreach (var p in page.subpages) {
    var originator = wiki.getuser(web.list(p.api .. '/revisions?revision=1', '/page/user.author/username')[0]);
    var originator=(originator.id!=0)?originator:p.author;
    var lastAuthor = originator;
    var lastDate = date.format(p.date, 's');
    if (#p.comments != 0) {
    var lastComment = list.reverse(p.comments)[0];
    if (date.isafter(lastComment.date,p.date)){
    let lastAuthor = lastComment.author;
    let lastDate =date.format(lastComment.date, 's');
    }
    }
    let topics ..= [ { page:p, originator:originator, lastAuthor:lastAuthor, lastDate:lastDate } ];
    }
    let topics = list.sort(topics, 'lastDate', true);" class="table">
    <tbody>
        <tr>
            <th valign="top" style="width: 70%;">Topic/Starter</th>
            <th valign="top" style="width: 15%;">Last Post</th>
            <th valign="top" style="width: 7%;">#Views</th>
            <th valign="top" style="width: 7%;">#Replies</th>
        </tr>
        <tr foreach="var t in topics" class="{{__count % 2 == 0 ? 'bg1' : 'bg2'}}">
            <td valign="top"><font style="font-size: 17px;"><strong>{{web.link(t.page.uri, t.page.title)}}</strong></font> <br  />
            {{web.link(t.originator.uri, t.originator.name); }}</td>
            <td valign="top">{{date.format(t.lastDate,'yyyy-MM-d hh:mm');}} <br  />
            by {{web.link(t.lastAuthor.uri, t.lastAuthor.name)}}</td>
            <td valign="top">{{t.page.viewcount}}</td>
            <td valign="top">{{#t.page.comments}}</td>
        </tr>
    </tbody>
</table>

Topic/Starter Last Post #Views #Replies
Salut Thanh-Trung
michelbuffa
2009-02-5 09:42
by michelbuffa
1072 2
Salut Christian
michelbuffa
2009-02-5 07:54
by michelbuffa
486 0
Put Your Title Here (1)
michelbuffa
2009-01-20 02:29
by michelbuffa
289 0
Salut Frederic
michelbuffa
2008-11-18 08:49
by michelbuffa
546 0
Quote of the day
jaykul
2008-11-18 03:14
by jaykul
531 0
Hello la maige
michelbuffa
2008-10-24 08:48
by michelbuffa
699 1
Oui, mais non en fait...
michelbuffa
2008-10-21 07:46
by michelbuffa
744 0
Heelo ceci est un test
michelbuffa
2008-10-20 08:08
by michelbuffa
656 0
Put Your Title Here
peytz
2008-09-16 07:58
by solo-id
825 1
First Post!
craigsivils
2008-08-29 04:51
by jadus
1239 2

    

Tag page
Viewing 5 of 5 comments: view all
Special thanks to neilw for vast improvements on my original code.
Posted 14:26, 30 Aug 2008
The originator code was returning 0 on my wiki (perhaps because it is a private wiki). I put code in to trap this situation since it caused an error in the table output.
Posted 14:50, 30 Aug 2008
I neglected to mention the caveat that the originator code is vulnerable to security restrictions.

I have an alternate solution, though, that I will possibly roll into this code when I get a chance. That is, insert the originator and original date into the topic itself using a template. Then, rather than do the API call, the originator can be found by a wiki.page() call. Another benefit is that this will fix the fact that the originator is currently not knowable on the topic page.

I'll get to this soon! :-) edited 00:18, 1 Sep 2008
Posted 00:17, 1 Sep 2008
You should be able to get the originator by requesting the page metadata while providing ?revision=1.
Posted 01:33, 1 Sep 2008
That's what it's doing now, but I think it doesn't work if the page is restricted.
Posted 10:49, 2 Sep 2008
Viewing 5 of 5 comments: view all
You must login to post a comment.