9caccc9ca30523cbef17f39f005c46a9a16d7950
[citadel.git] / webcit / blogview_renderer.c
1 /* 
2  * Blog view renderer module for WebCit
3  *
4  * Copyright (c) 1996-2012 by the citadel.org team
5  *
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.
8  *
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.
13  */
14
15 #include "webcit.h"
16 #include "webserver.h"
17 #include "dav.h"
18
19
20 typedef struct __BLOG {
21         HashList *BLOG;
22         long p;
23         int gotonext;
24         StrBuf *Charset;
25         StrBuf *Buf;
26 } BLOG;
27
28
29 /*
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)
33  */
34 void tmplput_blog_permalink(StrBuf *Target, WCTemplputParams *TP) {
35         char perma[SIZ];
36         
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);
40         if (!Target) {
41                 wc_printf("%s", perma);
42         }
43         else {
44                 StrBufAppendPrintf(Target, "%s", perma);
45         }
46 }
47
48
49 /*
50  * Render a single blog post and (optionally) its comments
51  */
52 void blogpost_render(struct blogpost *bp, int with_comments)
53 {
54         const StrBuf *Mime;
55         int i;
56
57         WC->bptlid = bp->top_level_id;  /* This is used in templates; do not remove it */
58
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);
62         }
63
64         if (with_comments) {
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);
74                 }
75                 do_template("view_blog_comment_box");
76         }
77
78         else {
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");
89         }
90 }
91
92
93 /*
94  * Destructor for "struct blogpost"
95  */
96 void blogpost_destroy(struct blogpost *bp) {
97         if (bp->alloc_msgs > 0) {
98                 free(bp->msgs);
99         }
100         free(bp);
101 }
102
103
104 /*
105  * Entry point for message read operations.
106  */
107 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
108                                    void **ViewSpecific, 
109                                    long oper, 
110                                    char *cmd, 
111                                     long len,
112                                     char *filter,
113                                     long flen)
114 {
115         BLOG *BL = (BLOG*) malloc(sizeof(BLOG)); 
116         BL->BLOG = NewHash(1, lFlathash);
117         
118         /* are we looking for a specific post? */
119         BL->p = lbstr("p");
120         BL->gotonext = havebstr("gotonext");
121         BL->Charset = NewStrBuf();
122         BL->Buf = NewStrBuf();
123         *ViewSpecific = BL;
124
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;
129         
130         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
131         rlid[2].cmd(cmd, len);
132         if (BL->gotonext)
133                 Stat->load_seen = 1;
134         return 200;
135 }
136
137
138 /*
139  * This function is called for every message in the list.
140  */
141 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat, 
142                               void **ViewSpecific, 
143                               message_summary* Msg, 
144                               int is_new, 
145                               int i)
146 {
147         BLOG *BL = (BLOG*) *ViewSpecific;
148         struct blogpost *bp = NULL;
149
150         ReadOneMessageSummary(Msg, BL->Charset, BL->Buf);
151
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.
154          */
155         if ((BL->p != 0) &&
156             (BL->p != Msg->reply_inreplyto_hash) &&
157             (BL->p != Msg->reply_references_hash)) {
158                 return 200;
159         }
160
161         /*
162          * Add our little bundle of blogworthy wonderfulness to the hash table
163          */
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;
169                 Put(BL->BLOG,
170                     (const char *)&Msg->reply_inreplyto_hash,
171                     sizeof(Msg->reply_inreplyto_hash),
172                     bp,
173                     (DeleteHashDataFunc)blogpost_destroy);
174         }
175         else {
176                 GetHash(BL->BLOG,
177                         (const char *)&Msg->reply_references_hash,
178                         sizeof(Msg->reply_references_hash),
179                         (void *)&bp);
180         }
181
182         /*
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
185          * this point onward.
186          */
187         if (bp != NULL) {
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)) );
192                 }
193                 if (bp->num_msgs >= bp->alloc_msgs) {
194                         bp->alloc_msgs *= 2;
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)) );
198                 }
199                 bp->msgs[bp->num_msgs++] = Msg->msgnum;
200                 if ((Msg->Flags & MSGFLAG_READ) != 0) {
201                         bp->unread_oments++;
202                 }
203         }
204         else {
205                 syslog(LOG_DEBUG, "** comment %ld is unparented", Msg->msgnum);
206         }
207
208         return 200;
209 }
210
211
212 /*
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
215  */
216 static int blogview_sortfunc(const void *a, const void *b) { 
217         struct blogpost * const *one = a;
218         struct blogpost * const *two = b;
219
220         if ( (*one)->msgs[0] > (*two)->msgs[0] ) return(-1);
221         if ( (*one)->msgs[0] < (*two)->msgs[0] ) return(+1);
222         return(0);
223 }
224
225
226 /*
227  * All blogpost entries are now in the hash list.
228  * Sort them, select the desired range, and render what we want to see.
229  */
230 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
231 {
232         BLOG *BL = (BLOG*) *ViewSpecific;
233         HashPos *it;
234         const char *Key;
235         void *Data;
236         long len;
237         int i;
238         struct blogpost **blogposts = NULL;
239         int num_blogposts = 0;
240         int num_blogposts_alloc = 0;
241         int with_comments = 0;
242         int firstp = 0;
243         int maxp = 0;
244
245         /* Comments are shown if we are only viewing a single blog post */
246         with_comments = (BL->p != 0);
247
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? */
251
252         it = GetNewHashPos(BL->BLOG, 0);
253
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 
257                  * this one.
258                  */
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) {
264                                 unread_bp = one_bp;
265                                 unread_count++;
266                         }
267                 }
268                 if (unread_count == 1) {
269                         blogpost_render(unread_bp, 1);
270
271                         DeleteHashPos(&it);
272                         return 0;
273                 }
274
275                 RewindHashPos(BL->BLOG, it, 0);
276         }
277
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;
283                         }
284                         else {
285                                 num_blogposts_alloc *= 2;
286                         }
287                         blogposts = realloc(blogposts, (num_blogposts_alloc * sizeof (struct blogpost *)));
288                 }
289                 blogposts[num_blogposts++] = (struct blogpost *) Data;
290         }
291         DeleteHashPos(&it);
292
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.
295          */
296         if (num_blogposts > 0) {
297                 int start_here = 0;
298                 /* Sort newest-to-oldest */
299                 qsort(blogposts, num_blogposts, sizeof(void *), blogview_sortfunc);
300
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) {
304                                 start_here = i;
305                         }
306                 }
307
308                 /* FIXME -- allow the user (or a default setting) to select a maximum number of posts to display */
309
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)) {
313                                 int j = i - maxp;
314                                 if (j < 0) j = 0;
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"));
319                         }
320                         if (i < (start_here + maxp)) {
321                                 blogpost_render(blogposts[i], with_comments);
322                         }
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"));
328                         }
329                 }
330
331                 /* Done.  We are only freeing the array of pointers; the data itself
332                  * will be freed along with the hash list.
333                  */
334                 free(blogposts);
335         }
336
337         return(0);
338 }
339
340
341 int blogview_Cleanup(void **ViewSpecific)
342 {
343         BLOG *BL = (BLOG*) *ViewSpecific;
344
345         FreeStrBuf(&BL->Buf);
346         FreeStrBuf(&BL->Charset);
347         DeleteHash(&BL->BLOG);
348         free(BL);
349         wDumpContent(1);
350         return 0;
351 }
352
353
354 void 
355 InitModule_BLOGVIEWRENDERERS
356 (void)
357 {
358         RegisterReadLoopHandlerset(
359                 VIEW_BLOG,
360                 blogview_GetParamsGetServerCall,
361                 NULL,
362                 NULL,
363                 NULL, 
364                 blogview_LoadMsgFromServer,
365                 blogview_render,
366                 blogview_Cleanup
367         );
368         RegisterNamespace("BLOG:PERMALINK", 0, 0, tmplput_blog_permalink, NULL, CTX_NONE);
369 }