2 * Blog view renderer module for WebCit
4 * Copyright (c) 1996-2012 by the citadel.org team
6 * This program is open source software. You can redistribute it and/or
7 * modify it under the terms of the GNU General Public License, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include "webserver.h"
19 CtxType CTX_BLOGPOST = CTX_NONE;
21 typedef struct __BLOG {
30 * Array type for a blog post. The first message is the post; the rest are comments
32 typedef struct _blogpost {
34 long *msgs; /* Array of msgnums for messages we are displaying */
35 int num_msgs; /* Number of msgnums stored in 'msgs' */
36 int alloc_msgs; /* Currently allocated size of array */
42 * XML sitemap generator -- go through the message list for a Blog room
44 void sitemap_do_blog(void) {
49 SharedMessageStatus Stat;
50 message_summary *Msg = NULL;
51 StrBuf *Buf = NewStrBuf();
52 StrBuf *FoundCharset = NewStrBuf();
53 WCTemplputParams SubTP;
55 memset(&Stat, 0, sizeof Stat);
56 memset(&oneBP, 0, sizeof(blogpost));
57 memset(&SubTP, 0, sizeof(WCTemplputParams));
58 StackContext(NULL, &SubTP, &oneBP, CTX_BLOGPOST, 0, NULL);
60 Stat.maxload = INT_MAX;
61 Stat.lowest_found = (-1);
62 Stat.highest_found = (-1);
63 num_msgs = load_msg_ptrs("MSGS ALL", NULL, &Stat, NULL);
64 if (num_msgs < 1) return;
66 for (i=0; i<num_msgs; ++i) {
67 Msg = GetMessagePtrAt(i, WCC->summ);
69 ReadOneMessageSummary(Msg, FoundCharset, Buf);
70 /* Show only top level posts, not comments */
71 if ((Msg->reply_inreplyto_hash != 0) && (Msg->reply_references_hash == 0)) {
72 oneBP.top_level_id = Msg->reply_inreplyto_hash;
73 DoTemplate(HKEY("view_blog_sitemap"), WCC->WBuf, &SubTP);
77 UnStackContext(&SubTP);
79 FreeStrBuf(&FoundCharset);
85 * Generate a permalink for a post
86 * (Call with NULL arguments to make this function wcprintf() the permalink
87 * instead of writing it to the template)
89 void tmplput_blog_toplevel_id(StrBuf *Target, WCTemplputParams *TP) {
90 blogpost *bp = (blogpost*) CTX(CTX_BLOGPOST);
92 snprintf(buf, SIZ, "%d", bp->top_level_id);
93 StrBufAppendTemplateStr(Target, TP, buf, 0);
96 void tmplput_blog_comment_count(StrBuf *Target, WCTemplputParams *TP) {
97 blogpost *bp = (blogpost*) CTX(CTX_BLOGPOST);
99 snprintf(buf, SIZ, "%d", bp->num_msgs -1);
100 StrBufAppendTemplateStr(Target, TP, buf, 0);
102 void tmplput_blog_comment_unread_count(StrBuf *Target, WCTemplputParams *TP) {
103 blogpost *bp = (blogpost*) CTX(CTX_BLOGPOST);
105 snprintf(buf, SIZ, "%d", bp->unread_oments);
106 StrBufAppendTemplateStr(Target, TP, buf, 0);
112 * Render a single blog post and (optionally) its comments
114 void blogpost_render(blogpost *bp, int with_comments, WCTemplputParams *TP)
117 WCTemplputParams SubTP;
121 memset(&SubTP, 0, sizeof(WCTemplputParams));
122 StackContext(TP, &SubTP, bp, CTX_BLOGPOST, 0, NULL);
124 /* Always show the top level post, unless we somehow ended up with an empty list */
125 if (bp->num_msgs > 0) {
126 read_message(WC->WBuf, HKEY("view_blog_post"), bp->msgs[0], NULL, &Mime, TP);
130 /* Show any existing comments, then offer the comment box */
131 DoTemplate(HKEY("view_blog_show_commentlink"), WCC->WBuf, &SubTP);
133 for (i=1; i<bp->num_msgs; ++i) {
134 read_message(WC->WBuf, HKEY("view_blog_comment"), bp->msgs[i], NULL, &Mime, &SubTP);
136 DoTemplate(HKEY("view_blog_comment_box"), WCC->WBuf, &SubTP);
140 /* Show only the number of comments */
141 DoTemplate(HKEY("view_blog_show_no_comments"), WCC->WBuf, &SubTP);
143 UnStackContext(&SubTP);
148 * Destructor for "blogpost"
150 void blogpost_destroy(blogpost *bp) {
151 if (bp->alloc_msgs > 0) {
159 * Entry point for message read operations.
161 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat,
169 BLOG *BL = (BLOG*) malloc(sizeof(BLOG));
170 BL->BLOGPOSTS = NewHash(1, lFlathash);
172 /* are we looking for a specific post? */
174 BL->gotonext = havebstr("gotonext");
175 BL->Charset = NewStrBuf();
176 BL->Buf = NewStrBuf();
179 Stat->startmsg = (-1); /* not used here */
180 Stat->sortit = 1; /* not used here */
181 Stat->num_displayed = DEFAULT_MAXMSGS; /* not used here */
182 if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
184 /* perform a "read all" call to fetch the message list -- we'll cut it down later */
185 rlid[2].cmd(cmd, len);
193 * This function is called for every message in the list.
195 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat,
197 message_summary* Msg,
201 BLOG *BL = (BLOG*) *ViewSpecific;
204 ReadOneMessageSummary(Msg, BL->Charset, BL->Buf);
206 /* Stop processing if the viewer is only interested in a single post and
207 * that message ID is neither the id nor the refs.
210 (BL->p != Msg->reply_inreplyto_hash) &&
211 (BL->p != Msg->reply_references_hash)) {
216 * Add our little bundle of blogworthy wonderfulness to the hash table
218 if (Msg->reply_references_hash == 0) {
219 bp = malloc(sizeof(blogpost));
220 if (!bp) return(200);
221 memset(bp, 0, sizeof (blogpost));
222 bp->top_level_id = Msg->reply_inreplyto_hash;
224 (const char *)&Msg->reply_inreplyto_hash,
225 sizeof(Msg->reply_inreplyto_hash),
227 (DeleteHashDataFunc)blogpost_destroy);
230 GetHash(BL->BLOGPOSTS,
231 (const char *)&Msg->reply_references_hash,
232 sizeof(Msg->reply_references_hash),
237 * Now we have a 'blogpost' to which we can add a message. It's either the
238 * blog post itself or a comment attached to it; either way, the code is the same from
242 if (bp->alloc_msgs == 0) {
243 bp->alloc_msgs = 1000;
244 bp->msgs = malloc(bp->alloc_msgs * sizeof(long));
245 memset(bp->msgs, 0, (bp->alloc_msgs * sizeof(long)) );
247 if (bp->num_msgs >= bp->alloc_msgs) {
249 bp->msgs = realloc(bp->msgs, (bp->alloc_msgs * sizeof(long)));
250 memset(&bp->msgs[bp->num_msgs], 0,
251 ((bp->alloc_msgs - bp->num_msgs) * sizeof(long)) );
253 bp->msgs[bp->num_msgs++] = Msg->msgnum;
254 if ((Msg->Flags & MSGFLAG_READ) != 0) {
259 syslog(LOG_DEBUG, "** comment %ld is unparented", Msg->msgnum);
267 * Sort a list of 'struct blogpost' pointers by newest-to-oldest msgnum.
268 * With big thanks to whoever wrote http://www.c.happycodings.com/Sorting_Searching/code14.html
270 static int blogview_sortfunc(const void *a, const void *b) {
271 blogpost * const *one = a;
272 blogpost * const *two = b;
274 if ( (*one)->msgs[0] > (*two)->msgs[0] ) return(-1);
275 if ( (*one)->msgs[0] < (*two)->msgs[0] ) return(+1);
281 * All blogpost entries are now in the hash list.
282 * Sort them, select the desired range, and render what we want to see.
284 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
287 BLOG *BL = (BLOG*) *ViewSpecific;
293 blogpost **blogposts = NULL;
294 int num_blogposts = 0;
295 int num_blogposts_alloc = 0;
296 int with_comments = 0;
299 WCTemplputParams SubTP;
300 WCTemplputParams StopSubTP;
303 memset(&SubTP, 0, sizeof(WCTemplputParams));
304 memset(&StopSubTP, 0, sizeof(WCTemplputParams));
305 memset(&oneBP, 0, sizeof(blogpost));
307 /* Comments are shown if we are only viewing a single blog post */
308 with_comments = (BL->p != 0);
310 firstp = ibstr("firstp"); /* start reading at... */
311 maxp = ibstr("maxp"); /* max posts to show... */
312 if (maxp < 1) maxp = 5; /* default; move somewhere else? */
313 putlbstr("maxp", maxp);
315 it = GetNewHashPos(BL->BLOGPOSTS, 0);
317 if ((BL->gotonext) && (BL->p == 0)) {
318 /* did we come here via gotonext? lets find out whether
319 * this blog has just one blogpost with new comments just display
322 blogpost *unread_bp = NULL;
323 int unread_count = 0;
324 while (GetNextHashPos(BL->BLOGPOSTS, it, &len, &Key, &Data)) {
325 blogpost *one_bp = (blogpost *) Data;
326 if (one_bp->unread_oments > 0) {
331 if (unread_count == 1) {
332 blogpost_render(unread_bp, 1, NULL);/// TODO other than null?
338 RewindHashPos(BL->BLOGPOSTS, it, 0);
341 /* Iterate through the hash list and copy the data pointers into an array */
342 while (GetNextHashPos(BL->BLOGPOSTS, it, &len, &Key, &Data)) {
343 if (num_blogposts >= num_blogposts_alloc) {
344 if (num_blogposts_alloc == 0) {
345 num_blogposts_alloc = 100;
348 num_blogposts_alloc *= 2;
350 blogposts = realloc(blogposts, (num_blogposts_alloc * sizeof (blogpost *)));
352 blogposts[num_blogposts++] = (blogpost *) Data;
356 /* Now we have our array. It is ONLY an array of pointers. The objects to
357 * which they point are still owned by the hash list.
359 if (num_blogposts > 0) {
361 /* Sort newest-to-oldest */
362 qsort(blogposts, num_blogposts, sizeof(void *), blogview_sortfunc);
364 /* allow the user to select a starting point in the list */
365 for (i=0; i<num_blogposts; ++i) {
366 if (blogposts[i]->top_level_id == firstp) {
371 /* FIXME -- allow the user (or a default setting) to select a maximum number of posts to display */
373 /* Now go through the list and render what we've got */
374 for (i=start_here; i<num_blogposts; ++i) {
377 StackContext(NULL, &SubTP, &blogposts[j], CTX_BLOGPOST, 0, NULL);
378 if ((i > 0) && (i == start_here)) {
379 StackContext(NULL, &SubTP, &blogposts[j], CTX_BLOGPOST, 0, NULL);
380 DoTemplate(HKEY("view_blog_post_start"), WCC->WBuf, &SubTP);
382 if (i < (start_here + maxp)) {
383 blogpost_render(blogposts[i], with_comments, &SubTP);
385 else if (i == (start_here + maxp)) {
386 StackContext(&SubTP, &StopSubTP, &blogposts[i], CTX_BLOGPOST, 0, NULL);
387 DoTemplate(HKEY("view_blog_post_stop"), WCC->WBuf, &SubTP);
388 UnStackContext(&StopSubTP);
390 UnStackContext(&SubTP);
393 /* Done. We are only freeing the array of pointers; the data itself
394 * will be freed along with the hash list.
403 int blogview_Cleanup(void **ViewSpecific)
405 BLOG *BL = (BLOG*) *ViewSpecific;
407 FreeStrBuf(&BL->Buf);
408 FreeStrBuf(&BL->Charset);
409 DeleteHash(&BL->BLOGPOSTS);
417 InitModule_BLOGVIEWRENDERERS
420 RegisterCTX(CTX_BLOGPOST);
422 RegisterReadLoopHandlerset(
424 blogview_GetParamsGetServerCall,
428 blogview_LoadMsgFromServer,
433 RegisterNamespace("BLOG:TOPLEVEL:MSGID", 0, 0, tmplput_blog_toplevel_id, NULL, CTX_BLOGPOST);
434 RegisterNamespace("BLOG:COMMENTS:COUNT", 0, 0, tmplput_blog_comment_count, NULL, CTX_BLOGPOST);
435 RegisterNamespace("BLOG:COMMENTS:UNREAD:COUNT", 0, 0, tmplput_blog_comment_unread_count, NULL, CTX_BLOGPOST);