b57d9d963c6629e4790587128c1c625d166737e4
[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("</a> | <a class=\"blog_permalink_link\" href=\"");
85                 tmplput_blog_permalink(NULL, NULL);
86                 wc_printf("\">%s</a>", _("permalink"));
87                 wc_printf("<hr>\n</div>\n");
88         }
89 }
90
91
92 /*
93  * Destructor for "struct blogpost"
94  */
95 void blogpost_destroy(struct blogpost *bp) {
96         if (bp->alloc_msgs > 0) {
97                 free(bp->msgs);
98         }
99         free(bp);
100 }
101
102
103 /*
104  * Entry point for message read operations.
105  */
106 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
107                                    void **ViewSpecific, 
108                                    long oper, 
109                                    char *cmd, 
110                                     long len,
111                                     char *filter,
112                                     long flen)
113 {
114         BLOG *BL = (BLOG*) malloc(sizeof(BLOG)); 
115         BL->BLOG = NewHash(1, lFlathash);
116         
117         /* are we looking for a specific post? */
118         BL->p = lbstr("p");
119         BL->gotonext = havebstr("gotonext");
120         BL->Charset = NewStrBuf();
121         BL->Buf = NewStrBuf();
122         *ViewSpecific = BL;
123
124         Stat->startmsg = (-1);                                  /* not used here */
125         Stat->sortit = 1;                                       /* not used here */
126         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
127         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
128         
129         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
130         rlid[2].cmd(cmd, len);
131         if (BL->gotonext)
132                 Stat->load_seen = 1;
133         return 200;
134 }
135
136
137 /*
138  * This function is called for every message in the list.
139  */
140 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat, 
141                               void **ViewSpecific, 
142                               message_summary* Msg, 
143                               int is_new, 
144                               int i)
145 {
146         BLOG *BL = (BLOG*) *ViewSpecific;
147         struct blogpost *bp = NULL;
148
149         ReadOneMessageSummary(Msg, BL->Charset, BL->Buf);
150
151         /* Stop processing if the viewer is only interested in a single post and
152          * that message ID is neither the id nor the refs.
153          */
154         if ((BL->p != 0) &&
155             (BL->p != Msg->reply_inreplyto_hash) &&
156             (BL->p != Msg->reply_references_hash)) {
157                 return 200;
158         }
159
160         /*
161          * Add our little bundle of blogworthy wonderfulness to the hash table
162          */
163         if (Msg->reply_references_hash == 0) {
164                 bp = malloc(sizeof(struct blogpost));
165                 if (!bp) return(200);
166                 memset(bp, 0, sizeof (struct blogpost));
167                 bp->top_level_id = Msg->reply_inreplyto_hash;
168                 Put(BL->BLOG,
169                     (const char *)&Msg->reply_inreplyto_hash,
170                     sizeof(Msg->reply_inreplyto_hash),
171                     bp,
172                     (DeleteHashDataFunc)blogpost_destroy);
173         }
174         else {
175                 GetHash(BL->BLOG,
176                         (const char *)&Msg->reply_references_hash,
177                         sizeof(Msg->reply_references_hash),
178                         (void *)&bp);
179         }
180
181         /*
182          * Now we have a 'struct blogpost' to which we can add a message.  It's either the
183          * blog post itself or a comment attached to it; either way, the code is the same from
184          * this point onward.
185          */
186         if (bp != NULL) {
187                 if (bp->alloc_msgs == 0) {
188                         bp->alloc_msgs = 1000;
189                         bp->msgs = malloc(bp->alloc_msgs * sizeof(long));
190                         memset(bp->msgs, 0, (bp->alloc_msgs * sizeof(long)) );
191                 }
192                 if (bp->num_msgs >= bp->alloc_msgs) {
193                         bp->alloc_msgs *= 2;
194                         bp->msgs = realloc(bp->msgs, (bp->alloc_msgs * sizeof(long)));
195                         memset(&bp->msgs[bp->num_msgs], 0,
196                                 ((bp->alloc_msgs - bp->num_msgs) * sizeof(long)) );
197                 }
198                 bp->msgs[bp->num_msgs++] = Msg->msgnum;
199                 if ((Msg->Flags & MSGFLAG_READ) != 0) {
200                         syslog(LOG_DEBUG, "****************** unread %ld", Msg->msgnum);
201                         
202                         bp->unread_oments++;
203                 }
204         }
205         else {
206                 syslog(LOG_DEBUG, "** comment %ld is unparented", Msg->msgnum);
207         }
208
209         return 200;
210 }
211
212
213 /*
214  * Sort a list of 'struct blogpost' pointers by newest-to-oldest msgnum.
215  * With big thanks to whoever wrote http://www.c.happycodings.com/Sorting_Searching/code14.html
216  */
217 static int blogview_sortfunc(const void *a, const void *b) { 
218         struct blogpost * const *one = a;
219         struct blogpost * const *two = b;
220
221         if ( (*one)->msgs[0] > (*two)->msgs[0] ) return(-1);
222         if ( (*one)->msgs[0] < (*two)->msgs[0] ) return(+1);
223         return(0);
224 }
225
226
227 /*
228  * All blogpost entries are now in the hash list.
229  * Sort them, select the desired range, and render what we want to see.
230  */
231 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
232 {
233         BLOG *BL = (BLOG*) *ViewSpecific;
234         HashPos *it;
235         const char *Key;
236         void *Data;
237         long len;
238         int i;
239         struct blogpost **blogposts = NULL;
240         int num_blogposts = 0;
241         int num_blogposts_alloc = 0;
242         int with_comments = 0;
243         int firstp = 0;
244         int maxp = 0;
245
246         /* Comments are shown if we are only viewing a single blog post */
247         with_comments = (BL->p != 0);
248
249         firstp = atoi(BSTR("firstp"));  /* start reading at... */
250         maxp = atoi(BSTR("maxp"));      /* max posts to show... */
251         if (maxp < 1) maxp = 5;         /* default; move somewhere else? */
252
253
254         //// bp->unread_oments++;
255
256         /* Iterate through the hash list and copy the data pointers into an array */
257         it = GetNewHashPos(BL->BLOG, 0);
258         while (GetNextHashPos(BL->BLOG, it, &len, &Key, &Data)) {
259                 if (num_blogposts >= num_blogposts_alloc) {
260                         if (num_blogposts_alloc == 0) {
261                                 num_blogposts_alloc = 100;
262                         }
263                         else {
264                                 num_blogposts_alloc *= 2;
265                         }
266                         blogposts = realloc(blogposts, (num_blogposts_alloc * sizeof (struct blogpost *)));
267                 }
268                 blogposts[num_blogposts++] = (struct blogpost *) Data;
269         }
270         DeleteHashPos(&it);
271
272         /* Now we have our array.  It is ONLY an array of pointers.  The objects to
273          * which they point are still owned by the hash list.
274          */
275         if (num_blogposts > 0) {
276                 int start_here = 0;
277                 /* Sort newest-to-oldest */
278                 qsort(blogposts, num_blogposts, sizeof(void *), blogview_sortfunc);
279
280                 /* allow the user to select a starting point in the list */
281                 for (i=0; i<num_blogposts; ++i) {
282                         if (blogposts[i]->top_level_id == firstp) {
283                                 start_here = i;
284                         }
285                 }
286
287                 /* FIXME -- allow the user (or a default setting) to select a maximum number of posts to display */
288
289                 /* Now go through the list and render what we've got */
290                 for (i=start_here; i<num_blogposts; ++i) {
291                         if ((i > 0) && (i == start_here)) {
292                                 int j = i - maxp;
293                                 if (j < 0) j = 0;
294                                 wc_printf("<div class=\"newer_blog_posts\"><a href=\"readfwd?go=");
295                                 urlescputs(ChrPtr(WC->CurRoom.name));
296                                 wc_printf("?firstp=%d?maxp=%d\">", blogposts[j]->top_level_id, maxp);
297                                 wc_printf("%s →</a></div>\n", _("Newer posts"));
298                         }
299                         if (i < (start_here + maxp)) {
300                                 blogpost_render(blogposts[i], with_comments);
301                         }
302                         else if (i == (start_here + maxp)) {
303                                 wc_printf("<div class=\"older_blog_posts\"><a href=\"readfwd?go=");
304                                 urlescputs(ChrPtr(WC->CurRoom.name));
305                                 wc_printf("?firstp=%d?maxp=%d\">", blogposts[i]->top_level_id, maxp);
306                                 wc_printf("← %s</a></div>\n", _("Older posts"));
307                         }
308                 }
309
310                 /* Done.  We are only freeing the array of pointers; the data itself
311                  * will be freed along with the hash list.
312                  */
313                 free(blogposts);
314         }
315
316         return(0);
317 }
318
319
320 int blogview_Cleanup(void **ViewSpecific)
321 {
322         BLOG *BL = (BLOG*) *ViewSpecific;
323
324         FreeStrBuf(&BL->Buf);
325         FreeStrBuf(&BL->Charset);
326         DeleteHash(&BL->BLOG);
327         free(BL);
328         wDumpContent(1);
329         return 0;
330 }
331
332
333 void 
334 InitModule_BLOGVIEWRENDERERS
335 (void)
336 {
337         RegisterReadLoopHandlerset(
338                 VIEW_BLOG,
339                 blogview_GetParamsGetServerCall,
340                 NULL,
341                 NULL,
342                 NULL, 
343                 blogview_LoadMsgFromServer,
344                 blogview_render,
345                 blogview_Cleanup
346         );
347         RegisterNamespace("BLOG:PERMALINK", 0, 0, tmplput_blog_permalink, NULL, CTX_NONE);
348 }