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.
19 CtxType CTX_BLOGPOST = CTX_NONE;
21 typedef struct __BLOG {
29 const StrBuf *FilterTag;
33 * Array type for a blog post. The first message is the post; the rest are comments
35 typedef struct _blogpost {
37 long *msgs; /* Array of msgnums for messages we are displaying */
38 int num_msgs; /* Number of msgnums stored in 'msgs' */
39 int alloc_msgs; /* Currently allocated size of array */
45 * XML sitemap generator -- go through the message list for a Blog room
47 void sitemap_do_blog(void) {
52 SharedMessageStatus Stat;
53 message_summary *Msg = NULL;
54 StrBuf *Buf = NewStrBuf();
55 StrBuf *FoundCharset = NewStrBuf();
56 WCTemplputParams SubTP;
58 memset(&Stat, 0, sizeof Stat);
59 memset(&oneBP, 0, sizeof(blogpost));
60 memset(&SubTP, 0, sizeof(WCTemplputParams));
61 StackContext(NULL, &SubTP, &oneBP, CTX_BLOGPOST, 0, NULL);
63 Stat.maxload = INT_MAX;
64 Stat.lowest_found = (-1);
65 Stat.highest_found = (-1);
66 num_msgs = load_msg_ptrs("MSGS ALL", NULL, NULL, &Stat, NULL, NULL, NULL, NULL, 0);
67 if (num_msgs < 1) return;
69 for (i=0; i<num_msgs; ++i) {
70 Msg = GetMessagePtrAt(i, WCC->summ);
72 ReadOneMessageSummary(Msg, FoundCharset, Buf);
73 /* Show only top level posts, not comments */
74 if ((Msg->reply_inreplyto_hash != 0) && (Msg->reply_references_hash == 0)) {
75 oneBP.top_level_id = Msg->reply_inreplyto_hash;
76 DoTemplate(HKEY("view_blog_sitemap"), WCC->WBuf, &SubTP);
80 UnStackContext(&SubTP);
82 FreeStrBuf(&FoundCharset);
88 * Generate a permalink for a post
89 * (Call with NULL arguments to make this function wcprintf() the permalink
90 * instead of writing it to the template)
92 void tmplput_blog_toplevel_id(StrBuf *Target, WCTemplputParams *TP) {
93 blogpost *bp = (blogpost*) CTX(CTX_BLOGPOST);
95 snprintf(buf, SIZ, "%d", bp->top_level_id);
96 StrBufAppendTemplateStr(Target, TP, buf, 0);
99 void tmplput_blog_comment_count(StrBuf *Target, WCTemplputParams *TP) {
100 blogpost *bp = (blogpost*) CTX(CTX_BLOGPOST);
102 snprintf(buf, SIZ, "%d", bp->num_msgs -1);
103 StrBufAppendTemplateStr(Target, TP, buf, 0);
105 void tmplput_blog_comment_unread_count(StrBuf *Target, WCTemplputParams *TP) {
106 blogpost *bp = (blogpost*) CTX(CTX_BLOGPOST);
108 snprintf(buf, SIZ, "%d", bp->unread_oments);
109 StrBufAppendTemplateStr(Target, TP, buf, 0);
115 * Render a single blog post and (optionally) its comments
117 void blogpost_render(blogpost *bp, int with_comments, WCTemplputParams *TP)
120 WCTemplputParams SubTP;
124 memset(&SubTP, 0, sizeof(WCTemplputParams));
125 StackContext(TP, &SubTP, bp, CTX_BLOGPOST, 0, NULL);
127 /* Always show the top level post, unless we somehow ended up with an empty list */
128 if (bp->num_msgs > 0) {
129 read_message(WC->WBuf, HKEY("view_blog_post"), bp->msgs[0], NULL, &Mime, TP);
133 /* Show any existing comments, then offer the comment box */
134 DoTemplate(HKEY("view_blog_show_commentlink"), WCC->WBuf, &SubTP);
136 for (i=1; i<bp->num_msgs; ++i) {
137 read_message(WC->WBuf, HKEY("view_blog_comment"), bp->msgs[i], NULL, &Mime, &SubTP);
139 DoTemplate(HKEY("view_blog_comment_box"), WCC->WBuf, &SubTP);
143 /* Show only the number of comments */
144 DoTemplate(HKEY("view_blog_show_no_comments"), WCC->WBuf, &SubTP);
146 UnStackContext(&SubTP);
151 * Destructor for "blogpost"
153 void blogpost_destroy(blogpost *bp) {
154 if (bp->alloc_msgs > 0) {
162 * Entry point for message read operations.
164 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat,
172 BLOG *BL = (BLOG*) malloc(sizeof(BLOG));
173 BL->BLOGPOSTS = NewHash(1, lFlathash);
175 /* are we looking for a specific post? */
177 BL->gotonext = havebstr("gotonext");
178 BL->Charset = NewStrBuf();
179 BL->Buf = NewStrBuf();
180 BL->FilterTag = sbstr("FilterTag");
181 BL->firstp = lbstr("firstp"); /* start reading at... */
182 BL->maxp = lbstr("maxp"); /* max posts to show... */
183 if (BL->maxp < 1) BL->maxp = 5; /* default; move somewhere else? */
184 putlbstr("maxp", BL->maxp);
187 Stat->startmsg = (-1); /* not used here */
188 Stat->sortit = 1; /* not used here */
189 Stat->num_displayed = DEFAULT_MAXMSGS; /* not used here */
190 if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
192 /* perform a "read all" call to fetch the message list -- we'll cut it down later */
193 snprintf(cmd, len, "MSGS ALL||2|8\n");
201 int blogview_IdentifyBlogposts (StrBuf *Line,
203 message_summary *Msg,
204 StrBuf *ConversionBuffer,
207 BLOG *BL = (BLOG*) *ViewSpecific;
210 /* Stop processing if the viewer is only interested in a single post and
211 * that message ID is neither the id nor the refs.
214 (BL->p != Msg->reply_inreplyto_hash) &&
215 (BL->p != Msg->reply_references_hash)) {
219 if ((Msg->reply_references_hash == 0) &&
220 (BL->FilterTag != NULL) &&
221 (strstr(ChrPtr(Msg->EnvTo) , ChrPtr(BL->FilterTag)) == NULL)) {
222 /* filtering for tags, blogpost doesn't fit. */
227 * build up a hashtable of the blogposts.
229 if (Msg->reply_references_hash == 0) {
230 bp = malloc(sizeof(blogpost));
232 if (bp == NULL) return 0;
234 memset(bp, 0, sizeof (blogpost));
236 bp->top_level_id = Msg->reply_inreplyto_hash;
237 bp->alloc_msgs = 1000;
238 bp->msgs = malloc(bp->alloc_msgs * sizeof(long));
239 memset(bp->msgs, 0, (bp->alloc_msgs * sizeof(long)) );
241 /* the first one is the blogpost itself, all subequent are comments. */
242 bp->msgs[0] = Msg->msgnum;
246 LKEY(Msg->reply_inreplyto_hash),
248 (DeleteHashDataFunc)blogpost_destroy);
251 * Comments will be handled on the next iteration.
259 * This function is called for every message in the list.
261 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat,
263 message_summary* Msg,
268 BLOG *BL = (BLOG*) *ViewSpecific;
270 if (Msg->reply_references_hash != 0) {
272 * this is a comment. try to assign it to a blogpost.
274 GetHash(BL->BLOGPOSTS,
275 LKEY(Msg->reply_references_hash),
279 * Now we have a 'blogpost' to which we can add the comment. It's either the
280 * blog post itself or a comment attached to it; either way, the code is the same from
284 if (bp->num_msgs >= bp->alloc_msgs) {
286 bp->msgs = realloc(bp->msgs, (bp->alloc_msgs * sizeof(long)));
287 memset(&bp->msgs[bp->num_msgs], 0,
288 ((bp->alloc_msgs - bp->num_msgs) * sizeof(long)) );
290 bp->msgs[bp->num_msgs++] = Msg->msgnum;
291 if ((Msg->Flags & MSGFLAG_READ) != 0) {
297 * Ok, this comment probably belongs to one of the blogposts
298 * we ruled out by filters. don't load it.
308 * Sort a list of 'struct blogpost' pointers by newest-to-oldest msgnum.
309 * With big thanks to whoever wrote http://www.c.happycodings.com/Sorting_Searching/code14.html
311 static int blogview_sortfunc(const void *a, const void *b) {
312 blogpost const *one = GetSearchPayload(a);
313 blogpost const *two = GetSearchPayload(b);
315 if ( one->msgs[0] > two->msgs[0] ) return(-1);
316 if ( one->msgs[0] < two->msgs[0] ) return(+1);
322 * All blogpost entries are now in the hash list.
323 * Sort them, select the desired range, and render what we want to see.
325 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
328 BLOG *BL = (BLOG*) *ViewSpecific;
331 blogpost *thisBlogpost;
334 int num_blogposts = 0;
335 int with_comments = 0;
336 WCTemplputParams SubTP;
337 WCTemplputParams StopSubTP;
339 long firstPOffset = 0;
342 StrBuf *PrevNext = NULL;
344 num_blogposts = GetCount(BL->BLOGPOSTS);
345 if (num_blogposts == 0) {
346 /* Nothing to do... */
349 memset(&SubTP, 0, sizeof(WCTemplputParams));
350 memset(&StopSubTP, 0, sizeof(WCTemplputParams));
351 memset(&oneBP, 0, sizeof(blogpost));
353 /* Comments are shown if we are only viewing a single blog post */
354 with_comments = (BL->p != 0);
356 it = GetNewHashPos(BL->BLOGPOSTS, 0);
358 if ((BL->gotonext) && (BL->p == 0)) {
359 /* did we come here via gotonext? lets find out whether
360 * this blog has just one blogpost with new comments just display
363 blogpost *unread_bp = NULL;
364 int unread_count = 0;
365 while (GetNextHashPos(BL->BLOGPOSTS, it, &len, &Key, &Data)) {
366 blogpost *one_bp = (blogpost *) Data;
367 if (one_bp->unread_oments > 0) {
372 if (unread_count == 1) {
373 blogpost_render(unread_bp, 1, NULL);/// TODO other than null?
380 /* Now we have our array. It is ONLY an array of pointers. The objects to
381 * which they point are still owned by the hash list.
384 /* get it into the right sort of order - blogposts may have been edited */
385 SortByPayload(BL->BLOGPOSTS, blogview_sortfunc);
387 /* Find the start post to display: */
388 if (BL->firstp != 0) {
389 /* Translate the firstpost id into an offset */
390 RewindHashPos(BL->BLOGPOSTS, it, 0);
391 while (GetNextHashPos(BL->BLOGPOSTS, it, &len, &Key, &Data)) {
392 thisBlogpost = (blogpost *) Data;
393 if (thisBlogpost->top_level_id == BL->firstp) {
394 firstPOffset = count;
401 if ((num_blogposts > BL->maxp) || (firstPOffset != 0)){
402 PrevNext = NewStrBuf();
403 if (firstPOffset > 0) {
407 /* we now need to go up to maxp items back */
408 if (firstPOffset > BL->maxp) {
409 posPrev = firstPOffset - BL->maxp;
411 GetHashAt(BL->BLOGPOSTS, posPrev, &len, &k, &Data);
412 thisBlogpost = (blogpost *) Data;
413 StackContext(NULL, &SubTP, thisBlogpost, CTX_BLOGPOST, 0, NULL);
414 DoTemplate(HKEY("view_blog_newer_posts"), PrevNext, &SubTP);
416 if (firstPOffset + BL->maxp <= num_blogposts) {
419 long posNext = firstPOffset + BL->maxp;
420 GetHashAt(BL->BLOGPOSTS, posNext, &len, &k, &Data);
421 thisBlogpost = (blogpost *) Data;
422 StackContext(NULL, &SubTP, thisBlogpost, CTX_BLOGPOST, 0, NULL);
423 DoTemplate(HKEY("view_blog_older_posts"), PrevNext, &SubTP);
427 StrBufAppendBuf(WCC->WBuf, PrevNext, 0);
428 count = totalCount = 0;
429 RewindHashPos(BL->BLOGPOSTS, it, 0);
431 /* FIXME -- allow the user (or a default setting) to select a maximum number of posts to display */
432 while (GetNextHashPos(BL->BLOGPOSTS, it, &len, &Key, &Data)) {
433 thisBlogpost = (blogpost *) Data;
435 /* allow the user to select a starting point in the list */
436 if (totalCount < firstPOffset) {
437 /* skip all till we found the first valid: */
441 if (count >= BL->maxp) {
442 /* enough is enough. */
445 StackContext(NULL, &SubTP, thisBlogpost, CTX_BLOGPOST, 0, NULL);
446 blogpost_render(thisBlogpost, with_comments, &SubTP);
447 UnStackContext(&SubTP);
451 StrBufAppendBuf(WCC->WBuf, PrevNext, 0);
452 FreeStrBuf(&PrevNext);
459 int blogview_Cleanup(void **ViewSpecific)
461 BLOG *BL = (BLOG*) *ViewSpecific;
463 FreeStrBuf(&BL->Buf);
464 FreeStrBuf(&BL->Charset);
465 DeleteHash(&BL->BLOGPOSTS);
473 InitModule_BLOGVIEWRENDERERS
476 const char* browseListFields[] = {
482 RegisterCTX(CTX_BLOGPOST);
484 RegisterReadLoopHandlerset(
486 blogview_GetParamsGetServerCall,
489 blogview_IdentifyBlogposts,
490 blogview_LoadMsgFromServer,
496 RegisterNamespace("BLOG:TOPLEVEL:MSGID", 0, 0, tmplput_blog_toplevel_id, NULL, CTX_BLOGPOST);
497 RegisterNamespace("BLOG:COMMENTS:COUNT", 0, 0, tmplput_blog_comment_count, NULL, CTX_BLOGPOST);
498 RegisterNamespace("BLOG:COMMENTS:UNREAD:COUNT", 0, 0, tmplput_blog_comment_unread_count, NULL, CTX_BLOGPOST);