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"
20 typedef struct __BLOG {
30 * Generate a permalink for a post
31 * (Call with NULL arguments to make this function wcprintf() the permalink
32 * instead of writing it to the template)
34 void tmplput_blog_permalink(StrBuf *Target, WCTemplputParams *TP) {
37 strcpy(perma, "/readfwd?go=");
38 urlesc(&perma[strlen(perma)], sizeof(perma)-strlen(perma), (char *)ChrPtr(WC->CurRoom.name));
39 snprintf(&perma[strlen(perma)], sizeof(perma)-strlen(perma), "?p=%d", WC->bptlid);
41 wc_printf("%s", perma);
44 StrBufAppendPrintf(Target, "%s", perma);
50 * Render a single blog post and (optionally) its comments
52 void blogpost_render(struct blogpost *bp, int with_comments)
57 WC->bptlid = bp->top_level_id; /* This is used in templates; do not remove it */
59 /* Always show the top level post, unless we somehow ended up with an empty list */
60 if (bp->num_msgs > 0) {
61 read_message(WC->WBuf, HKEY("view_blog_post"), bp->msgs[0], NULL, &Mime);
65 /* Show any existing comments, then offer the comment box */
66 wc_printf("<a class=\"blog_show_comments_link\" name=\"comments\"></a>\n");
67 wc_printf(_("%d comments"), bp->num_msgs - 1);
68 wc_printf(" | <a class=\"blog_permalink_link\" href=\"");
69 tmplput_blog_permalink(NULL, NULL);
70 wc_printf("\">%s</a>", _("permalink"));
71 wc_printf("</div>\n");
72 for (i=1; i<bp->num_msgs; ++i) {
73 read_message(WC->WBuf, HKEY("view_blog_comment"), bp->msgs[i], NULL, &Mime);
75 do_template("view_blog_comment_box");
79 /* Show only the number of comments */
80 wc_printf("<a class=\"blog_show_comments_link\" href=\"readfwd?p=%d?go=", bp->top_level_id);
81 urlescputs(ChrPtr(WC->CurRoom.name));
82 wc_printf("#comments\">");
83 wc_printf(_("%d comments"), bp->num_msgs - 1);
84 wc_printf(" %d %s", bp->unread_oments, _("new"));
85 wc_printf("</a> | <a class=\"blog_permalink_link\" href=\"");
86 tmplput_blog_permalink(NULL, NULL);
87 wc_printf("\">%s</a>", _("permalink"));
88 wc_printf("<hr>\n</div>\n");
94 * Destructor for "struct blogpost"
96 void blogpost_destroy(struct blogpost *bp) {
97 if (bp->alloc_msgs > 0) {
105 * Entry point for message read operations.
107 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat,
115 BLOG *BL = (BLOG*) malloc(sizeof(BLOG));
116 BL->BLOG = NewHash(1, lFlathash);
118 /* are we looking for a specific post? */
120 BL->gotonext = havebstr("gotonext");
121 BL->Charset = NewStrBuf();
122 BL->Buf = NewStrBuf();
125 Stat->startmsg = (-1); /* not used here */
126 Stat->sortit = 1; /* not used here */
127 Stat->num_displayed = DEFAULT_MAXMSGS; /* not used here */
128 if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
130 /* perform a "read all" call to fetch the message list -- we'll cut it down later */
131 rlid[2].cmd(cmd, len);
139 * This function is called for every message in the list.
141 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat,
143 message_summary* Msg,
147 BLOG *BL = (BLOG*) *ViewSpecific;
148 struct blogpost *bp = NULL;
150 ReadOneMessageSummary(Msg, BL->Charset, BL->Buf);
152 /* Stop processing if the viewer is only interested in a single post and
153 * that message ID is neither the id nor the refs.
156 (BL->p != Msg->reply_inreplyto_hash) &&
157 (BL->p != Msg->reply_references_hash)) {
162 * Add our little bundle of blogworthy wonderfulness to the hash table
164 if (Msg->reply_references_hash == 0) {
165 bp = malloc(sizeof(struct blogpost));
166 if (!bp) return(200);
167 memset(bp, 0, sizeof (struct blogpost));
168 bp->top_level_id = Msg->reply_inreplyto_hash;
170 (const char *)&Msg->reply_inreplyto_hash,
171 sizeof(Msg->reply_inreplyto_hash),
173 (DeleteHashDataFunc)blogpost_destroy);
177 (const char *)&Msg->reply_references_hash,
178 sizeof(Msg->reply_references_hash),
183 * Now we have a 'struct blogpost' to which we can add a message. It's either the
184 * blog post itself or a comment attached to it; either way, the code is the same from
188 if (bp->alloc_msgs == 0) {
189 bp->alloc_msgs = 1000;
190 bp->msgs = malloc(bp->alloc_msgs * sizeof(long));
191 memset(bp->msgs, 0, (bp->alloc_msgs * sizeof(long)) );
193 if (bp->num_msgs >= bp->alloc_msgs) {
195 bp->msgs = realloc(bp->msgs, (bp->alloc_msgs * sizeof(long)));
196 memset(&bp->msgs[bp->num_msgs], 0,
197 ((bp->alloc_msgs - bp->num_msgs) * sizeof(long)) );
199 bp->msgs[bp->num_msgs++] = Msg->msgnum;
200 if ((Msg->Flags & MSGFLAG_READ) != 0) {
205 syslog(LOG_DEBUG, "** comment %ld is unparented", Msg->msgnum);
213 * Sort a list of 'struct blogpost' pointers by newest-to-oldest msgnum.
214 * With big thanks to whoever wrote http://www.c.happycodings.com/Sorting_Searching/code14.html
216 static int blogview_sortfunc(const void *a, const void *b) {
217 struct blogpost * const *one = a;
218 struct blogpost * const *two = b;
220 if ( (*one)->msgs[0] > (*two)->msgs[0] ) return(-1);
221 if ( (*one)->msgs[0] < (*two)->msgs[0] ) return(+1);
227 * All blogpost entries are now in the hash list.
228 * Sort them, select the desired range, and render what we want to see.
230 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
232 BLOG *BL = (BLOG*) *ViewSpecific;
238 struct blogpost **blogposts = NULL;
239 int num_blogposts = 0;
240 int num_blogposts_alloc = 0;
241 int with_comments = 0;
245 /* Comments are shown if we are only viewing a single blog post */
246 with_comments = (BL->p != 0);
248 firstp = ibstr("firstp"); /* start reading at... */
249 maxp = ibstr("maxp"); /* max posts to show... */
250 if (maxp < 1) maxp = 5; /* default; move somewhere else? */
252 it = GetNewHashPos(BL->BLOG, 0);
254 if ((BL->gotonext) && (BL->p == 0)) {
255 /* did we come here via gotonext? lets find out whether
256 * this blog has just one blogpost with new comments just display
259 struct blogpost *unread_bp = NULL;
260 int unread_count = 0;
261 while (GetNextHashPos(BL->BLOG, it, &len, &Key, &Data)) {
262 struct blogpost *one_bp = (struct blogpost *) Data;
263 if (one_bp->unread_oments > 0) {
268 if (unread_count == 1) {
269 blogpost_render(unread_bp, 1);
275 RewindHashPos(BL->BLOG, it, 0);
278 /* Iterate through the hash list and copy the data pointers into an array */
279 while (GetNextHashPos(BL->BLOG, it, &len, &Key, &Data)) {
280 if (num_blogposts >= num_blogposts_alloc) {
281 if (num_blogposts_alloc == 0) {
282 num_blogposts_alloc = 100;
285 num_blogposts_alloc *= 2;
287 blogposts = realloc(blogposts, (num_blogposts_alloc * sizeof (struct blogpost *)));
289 blogposts[num_blogposts++] = (struct blogpost *) Data;
293 /* Now we have our array. It is ONLY an array of pointers. The objects to
294 * which they point are still owned by the hash list.
296 if (num_blogposts > 0) {
298 /* Sort newest-to-oldest */
299 qsort(blogposts, num_blogposts, sizeof(void *), blogview_sortfunc);
301 /* allow the user to select a starting point in the list */
302 for (i=0; i<num_blogposts; ++i) {
303 if (blogposts[i]->top_level_id == firstp) {
308 /* FIXME -- allow the user (or a default setting) to select a maximum number of posts to display */
310 /* Now go through the list and render what we've got */
311 for (i=start_here; i<num_blogposts; ++i) {
312 if ((i > 0) && (i == start_here)) {
315 wc_printf("<div class=\"newer_blog_posts\"><a href=\"readfwd?go=");
316 urlescputs(ChrPtr(WC->CurRoom.name));
317 wc_printf("?firstp=%d?maxp=%d\">", blogposts[j]->top_level_id, maxp);
318 wc_printf("%s →</a></div>\n", _("Newer posts"));
320 if (i < (start_here + maxp)) {
321 blogpost_render(blogposts[i], with_comments);
323 else if (i == (start_here + maxp)) {
324 wc_printf("<div class=\"older_blog_posts\"><a href=\"readfwd?go=");
325 urlescputs(ChrPtr(WC->CurRoom.name));
326 wc_printf("?firstp=%d?maxp=%d\">", blogposts[i]->top_level_id, maxp);
327 wc_printf("← %s</a></div>\n", _("Older posts"));
331 /* Done. We are only freeing the array of pointers; the data itself
332 * will be freed along with the hash list.
341 int blogview_Cleanup(void **ViewSpecific)
343 BLOG *BL = (BLOG*) *ViewSpecific;
345 FreeStrBuf(&BL->Buf);
346 FreeStrBuf(&BL->Charset);
347 DeleteHash(&BL->BLOG);
355 InitModule_BLOGVIEWRENDERERS
358 RegisterReadLoopHandlerset(
360 blogview_GetParamsGetServerCall,
364 blogview_LoadMsgFromServer,
368 RegisterNamespace("BLOG:PERMALINK", 0, 0, tmplput_blog_permalink, NULL, CTX_NONE);