Store the thread tree (id/refs) in memory using hashes instead of full strings
[citadel.git] / webcit / blogview_renderer.c
1 /* 
2  * Blog view renderer module for WebCit
3  *
4  * Copyright (c) 1996-2010 by the citadel.org team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "webcit.h"
22 #include "webserver.h"
23 #include "groupdav.h"
24
25 /*
26  * Data which gets passed around between the various functions in this module
27  *
28  */
29
30 struct blogpost {
31         long msgnum;
32         int id;
33         int refs;
34         int comment_count;
35 };
36
37 struct blogview {
38         struct blogpost *msgs;  /* Array of msgnums for messages we are displaying */
39         int num_msgs;           /* Number of msgnums stored in 'msgs' */
40         int alloc_msgs;         /* Currently allocated size of array */
41 };
42
43
44 /*
45  * Entry point for message read operations.
46  */
47 int blogview_GetParamsGetServerCall(SharedMessageStatus *Stat, 
48                                    void **ViewSpecific, 
49                                    long oper, 
50                                    char *cmd, 
51                                    long len)
52 {
53         struct blogview *BLOG = malloc(sizeof(struct blogview));
54         memset(BLOG, 0, sizeof(struct blogview));
55         *ViewSpecific = BLOG;
56
57         Stat->startmsg = (-1);                                  /* not used here */
58         Stat->sortit = 1;                                       /* not used here */
59         Stat->num_displayed = DEFAULT_MAXMSGS;                  /* not used here */
60         if (Stat->maxmsgs == 0) Stat->maxmsgs = DEFAULT_MAXMSGS;
61         
62         /* perform a "read all" call to fetch the message list -- we'll cut it down later */
63         rlid[2].cmd(cmd, len);
64         
65         return 200;
66 }
67
68
69 /*
70  * This function is called for every message in the list.
71  */
72 int blogview_LoadMsgFromServer(SharedMessageStatus *Stat, 
73                               void **ViewSpecific, 
74                               message_summary* Msg, 
75                               int is_new, 
76                               int i)
77 {
78         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
79
80         if (BLOG->alloc_msgs == 0) {
81                 BLOG->alloc_msgs = 1000;
82                 BLOG->msgs = malloc(BLOG->alloc_msgs * sizeof(struct blogpost));
83                 memset(BLOG->msgs, 0, (BLOG->alloc_msgs * sizeof(struct blogpost)) );
84         }
85
86         /* Check our buffer size */
87         if (BLOG->num_msgs >= BLOG->alloc_msgs) {
88                 BLOG->alloc_msgs *= 2;
89                 BLOG->msgs = realloc(BLOG->msgs, (BLOG->alloc_msgs * sizeof(long)));
90                 memset(&BLOG->msgs[BLOG->num_msgs], 0, ((BLOG->alloc_msgs - BLOG->num_msgs) * sizeof(long)) );
91         }
92
93         BLOG->msgs[BLOG->num_msgs++].msgnum = Msg->msgnum;
94         BLOG->msgs[BLOG->num_msgs].id = 0;
95         BLOG->msgs[BLOG->num_msgs].refs = 0;
96         BLOG->msgs[BLOG->num_msgs].comment_count = 0;
97
98         return 200;
99 }
100
101
102
103 /*
104  * Sort a list of 'struct blogpost' objects by newest-to-oldest msgnum.
105  */
106 int blogview_sortfunc(const void *s1, const void *s2) {
107         struct blogpost *l1 = (struct blogpost *)(s1);
108         struct blogpost *l2 = (struct blogpost *)(s2);
109
110         if (l1->msgnum > l2->msgnum) return(-1);
111         if (l1->msgnum < l2->msgnum) return(+1);
112         return(0);
113 }
114
115
116
117 /*
118  * Given a 'struct blogpost' containing a msgnum, populate the id
119  * and refs fields by fetching them from the Citadel server
120  */
121 void blogview_learn_thread_references(struct blogpost *bp)
122 {
123         StrBuf *Buf;
124         Buf = NewStrBuf();
125         serv_printf("MSG0 %ld|1", bp->msgnum);          /* top level citadel headers only */
126         StrBuf_ServGetln(Buf);
127         if (GetServerStatus(Buf, NULL) == 1) {
128                 while (StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
129                         if (!strncasecmp(ChrPtr(Buf), "msgn=", 5)) {
130                                 StrBufCutLeft(Buf, 5);
131                                 bp->id = HashLittle(ChrPtr(Buf), StrLength(Buf));
132                         }
133                         else if (!strncasecmp(ChrPtr(Buf), "wefw=", 5)) {
134                                 StrBufCutLeft(Buf, 5);          /* trim the field name */
135                                 StrBufCutAt(Buf, 0, "|");       /* trim all but the first thread ref */
136                                 bp->refs = HashLittle(ChrPtr(Buf), StrLength(Buf));
137                         }
138                 }
139         }
140         FreeStrBuf(&Buf);
141 }
142
143
144
145
146 int blogview_render(SharedMessageStatus *Stat, void **ViewSpecific, long oper)
147 {
148         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
149         int i;
150
151         /* Pass #1 - sort */
152         if (Stat->nummsgs > 0) {
153                 lprintf(9, "sorting %d messages\n", BLOG->num_msgs);
154                 qsort(BLOG->msgs, (size_t)(BLOG->num_msgs), sizeof(struct blogpost), blogview_sortfunc);
155         }
156
157         /* Pass #2 - learn thread references */
158         lprintf(9, "learning thread references\n");
159         for (i=0; (i<BLOG->num_msgs); ++i) {
160                 if (BLOG->msgs[i].msgnum > 0L) {
161                         blogview_learn_thread_references(&BLOG->msgs[i]);
162                 }
163         }
164
165         /* Pass #3 - turn it into a thread tree */
166         /* FIXME implement this */
167
168         /* Pass #4 - render
169          * This will require several different modes:
170          * * Top level
171          * * Single story permalink
172          * * Comments
173          * * etc
174          */
175         for (i=0; (i<BLOG->num_msgs); ++i) {
176                 if (BLOG->msgs[i].msgnum > 0L) {
177                         if (BLOG->msgs[i].refs == 0) {
178                                 wc_printf("<b>");
179                         }
180                         wc_printf("Message %d, #%ld, id %d, refs %d",
181                                 i,
182                                 BLOG->msgs[i].msgnum,
183                                 BLOG->msgs[i].id,
184                                 BLOG->msgs[i].refs
185                         );
186                         if (BLOG->msgs[i].refs == 0) {
187                                 wc_printf("</b>");
188                         }
189                         wc_printf("<br>\n");
190                 }
191         }
192
193         return(0);
194 }
195
196
197 int blogview_Cleanup(void **ViewSpecific)
198 {
199         struct blogview *BLOG = (struct blogview *) *ViewSpecific;
200
201         if (BLOG->alloc_msgs != 0) {
202                 free(BLOG->msgs);
203         }
204         free(BLOG);
205
206         wDumpContent(1);
207         return 0;
208 }
209
210
211 void 
212 InitModule_BLOGVIEWRENDERERS
213 (void)
214 {
215         RegisterReadLoopHandlerset(
216                 VIEW_BLOG,
217                 blogview_GetParamsGetServerCall,
218                 NULL,
219                 NULL, 
220                 blogview_LoadMsgFromServer,
221                 blogview_render,
222                 blogview_Cleanup
223         );
224 }