* Enhance the older/newer logic. It's still not quite right.
authorArt Cancro <ajc@citadel.org>
Wed, 30 Dec 2009 22:38:05 +0000 (22:38 +0000)
committerArt Cancro <ajc@citadel.org>
Wed, 30 Dec 2009 22:38:05 +0000 (22:38 +0000)
webcit/bbsview_renderer.c
webcit/messages.c
webcit/messages.h
webcit/msg_renderers.c

index 4bcb1935df9cc362c671b833fc60a6635e05bf4b..78fc554c225769e007120333df4803c0819891de 100644 (file)
@@ -375,21 +375,19 @@ int bbsview_LoadMsgFromServer(SharedMessageStatus *Stat,
 
        lprintf(9, "bbsview_LoadMsgFromServer() has been called.\n");
 
-       if (BBS->num_msgs < Stat->maxmsgs) {
+       if (BBS->alloc_msgs == 0) {
+               BBS->alloc_msgs = Stat->maxmsgs;
+               BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
+       }
 
-               if (BBS->alloc_msgs == 0) {
-                       BBS->alloc_msgs = Stat->maxmsgs;
-                       BBS->msgs = malloc(BBS->alloc_msgs * sizeof(long));
-               }
-       
-               /* Theoretically this never happens because the initial allocation == maxmsgs */
-               if (BBS->num_msgs >= BBS->alloc_msgs) {
-                       BBS->alloc_msgs *= 2;
-                       BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
-               }
-       
-               BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
+       /* Theoretically this never happens because the initial allocation == maxmsgs */
+       if (BBS->num_msgs >= BBS->alloc_msgs) {
+               BBS->alloc_msgs *= 2;
+               BBS->msgs = realloc(BBS->msgs, (BBS->alloc_msgs * sizeof(long)));
        }
+
+       BBS->msgs[BBS->num_msgs++] = Msg->msgnum;
+
        return 200;
 }
 
@@ -426,10 +424,78 @@ int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat,
        struct bbsview *BBS = (struct bbsview *) *ViewSpecific;
        int i;
        const StrBuf *Mime;
+       char olderdiv[64];
        char newerdiv[64];
+       int doing_older_messages = 0;
+       int doing_newer_messages = 0;
+
+       snprintf(olderdiv, sizeof olderdiv, "olderdiv%08lx%08x", time(NULL), rand());
+       snprintf(newerdiv, sizeof newerdiv, "newerdiv%08lx%08x", time(NULL), rand());
 
        lprintf(9, "starting bbsview_RenderView_or_Tail() - there are %d messages.\n", BBS->num_msgs);
 
+       /* If this is the initial page load (and not an update), supply the required JavaScript code */
+       if (!WC->is_ajax) {
+          StrBufAppendPrintf(WC->trailing_javascript,
+               "       function moremsgs(target_div, gt_or_lt, gt_or_lt_value, maxmsgs, sortorder) {   \n"
+               "               $(target_div).innerHTML = '<div class=\"moreprompt\">%s ... <img src=\"static/throbber.gif\"><br><br><br></div>';       \n"
+               "               p = gt_or_lt + '=' + gt_or_lt_value + '&maxmsgs=' + maxmsgs             \n"
+               "                       + '&is_summary=0&SortOrder=' + sortorder + '&is_ajax=1'         \n"
+               "                       + '&gt_or_lt=' + gt_or_lt                                       \n"
+               "                       + '&r=' + CtdlRandomString();                                   \n"
+               "               new Ajax.Updater(target_div, 'read' + gt_or_lt,                                 \n"
+               "                       { method: 'get', parameters: p, evalScripts: true } );          \n"
+               "       }                                                                               \n"
+               "",
+               _("Loading")
+          );
+       }
+
+
+       /* Determine whether we are in the middle of a 'click for older messages' or 'click for
+        * newer messages' operation.  If neither, then we are in the initial page load.
+        */
+       if (!strcasecmp(bstr("gt_or_lt"), "lt")) {
+               doing_older_messages = 1;
+               doing_newer_messages = 0;
+               lprintf(9, "\033[31m ** OLDER MESSAGES ** \033[0m\n");
+       }
+       else if (!strcasecmp(bstr("gt_or_lt"), "gt")) {
+               doing_older_messages = 0;
+               doing_newer_messages = 1;
+               lprintf(9, "\033[32m ** NEWER MESSAGES ** \033[0m\n");
+       }
+       else {
+               doing_older_messages = 0;
+               doing_newer_messages = 0;
+               lprintf(9, "\033[33m ** INITIAL PAGE LOAD ** \033[0m\n");
+       }
+
+
+       /* Supply the link to prepend the previous 20 messages */
+
+       if (doing_newer_messages == 0) {
+               wc_printf("<div id=\"%s\">", olderdiv);
+               /* if (Stat->nummsgs > 0) { */
+               if (Stat->nummsgs > 0) {
+                       wc_printf("<a href=\"javascript:moremsgs('%s', 'lt', %ld, %ld, %d );\">",
+                               olderdiv,
+                               BBS->msgs[0],
+                               Stat->maxmsgs,
+                               (Stat->reverse ? 2 : 1)
+                       );
+               
+                       wc_printf("<div class=\"moreprompt\">"
+                               "&uarr; &uarr; &uarr; %s &uarr; &uarr; &uarr;"
+                               "</div>", _("click here for older messages")
+                       );
+                       wc_printf("</a>");
+               }
+               wc_printf("<br></div>");
+       }
+
+
+
        /* Handle the empty message set gracefully... */
        if (Stat->nummsgs == 0) {
                if (!WC->is_ajax) {
@@ -443,6 +509,25 @@ int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat,
        else {
                lprintf(9, "sorting %d messages\n", BBS->num_msgs);
                qsort(BBS->msgs, (size_t)(BBS->num_msgs), sizeof(long), (Stat->reverse ? bbsview_sortfunc_reverse : bbsview_sortfunc_forward));
+
+               /* Cut it down to 20 messages (or whatever value Stat->maxmsgs is set to) */
+
+               if (BBS->num_msgs > Stat->maxmsgs) {
+
+                       if (doing_older_messages) {
+                               /* LT ... cut it down to the LAST 20 messages received */
+                               memcpy(&BBS->msgs[0], &BBS->msgs[BBS->num_msgs - Stat->maxmsgs],
+                                       (Stat->maxmsgs * sizeof(long))
+                               );
+                               BBS->num_msgs = Stat->maxmsgs;
+                       }
+                       else {
+                               /* GT ... cut it down to the FIRST 20 messages received */
+                               BBS->num_msgs = Stat->maxmsgs;
+                       }
+               }
+
+               /* Now render them */
        
                for (i=0; i<BBS->num_msgs; ++i) {
                        read_message(WC->WBuf, HKEY("view_message"), BBS->msgs[i], NULL, &Mime);
@@ -450,59 +535,48 @@ int bbsview_RenderView_or_Tail(SharedMessageStatus *Stat,
 
        }
 
-       snprintf(newerdiv, sizeof newerdiv, "newerdiv%08lx%08x", time(NULL), rand());
 
-       if (!WC->is_ajax) {     /* only supply the script during the initial page load */
-          StrBufAppendPrintf(WC->trailing_javascript,
-               "       function moremsgs(target_div, subcmd_name, subcmd_value, maxmsgs, sortorder) {  \n"
-               "               $(target_div).innerHTML = '<div class=\"moreprompt\">%s ... <img src=\"static/throbber.gif\"><br><br><br></div>';       \n"
-               "               p = subcmd_name + '=' + subcmd_value + '&maxmsgs=' + maxmsgs            \n"
-               "                       + '&is_summary=0&SortOrder=' + sortorder + '&is_ajax=1'         \n"
-               "                       + '&r=' + CtdlRandomString();                                   \n"
-               "               new Ajax.Updater(target_div, 'readgt',                                  \n"
-               "                       { method: 'get', parameters: p, evalScripts: true } );          \n"
-               "       }                                                                               \n"
-               "",
-               _("Loading")
-          );
-       }
+       /* Supply the link to append the next 20 messages */
 
-       wc_printf("<div id=\"%s\">", newerdiv);
-       /* if (Stat->nummsgs > 0) { */
-       if (Stat->nummsgs >= Stat->maxmsgs) {
-               wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld, %d );\">",
-                       newerdiv,
-                       BBS->msgs[BBS->num_msgs-1],
-                       Stat->maxmsgs,
-                       (Stat->reverse ? 2 : 1)
-               );
-       
-               wc_printf("<div class=\"moreprompt\">"
-                       "&darr; &darr; &darr; %s &darr; &darr; &darr;"
-                       "</div>", _("newer messages")
-               );
-               wc_printf("</a>");
-       }
-       else {
-               long gt = 0;    /* if new messages appear later, where will they begin? */
-               if (Stat->nummsgs > 0) {
-                       gt = BBS->msgs[BBS->num_msgs-1];
+       if (doing_older_messages == 0) {
+               wc_printf("<div id=\"%s\">", newerdiv);
+               /* if (Stat->nummsgs > 0) { */
+               if (Stat->nummsgs >= Stat->maxmsgs) {
+                       wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld, %d );\">",
+                               newerdiv,
+                               BBS->msgs[BBS->num_msgs-1],
+                               Stat->maxmsgs,
+                               (Stat->reverse ? 2 : 1)
+                       );
+               
+                       wc_printf("<div class=\"moreprompt\">"
+                               "&darr; &darr; &darr; %s &darr; &darr; &darr;"
+                               "</div>", _("click here for newer messages")
+                       );
+                       wc_printf("</a>");
                }
                else {
-                       gt = atol(bstr("gt"));
+                       long gt = 0;    /* if new messages appear later, where will they begin? */
+                       if (Stat->nummsgs > 0) {
+                               gt = BBS->msgs[BBS->num_msgs-1];
+                       }
+                       else {
+                               gt = atol(bstr("gt"));
+                       }
+                       wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld, %d );\">",
+                               newerdiv,
+                               gt,
+                               Stat->maxmsgs,
+                               (Stat->reverse ? 2 : 1)
+                       );
+                       wc_printf("<div class=\"moreprompt\">");
+                       wc_printf("no more ... gt would have been %ld ... FIXME", gt);
+                       wc_printf("</div>");
+                       wc_printf("</a>");
                }
-               wc_printf("<a href=\"javascript:moremsgs('%s', 'gt', %ld, %ld, %d );\">",
-                       newerdiv,
-                       gt,
-                       Stat->maxmsgs,
-                       (Stat->reverse ? 2 : 1)
-               );
-               wc_printf("<div class=\"moreprompt\">");
-               wc_printf("no more ... gt would have been %ld ... FIXME", gt);
-               wc_printf("</div>");
-               wc_printf("</a>");
+               wc_printf("<br><br><br><br></div>");
        }
-       wc_printf("<br><br><br><br></div>");
+
 
        return(0);
 }
index 6ef159c46ca09b70b553e4f01d83aff7535be047..b793f308c291554efe48c8ab65c66d9f485c751e 100644 (file)
@@ -751,7 +751,7 @@ typedef struct _RoomRenderer{
 /*
  * command loop for reading messages
  *
- * Set oper to "readnew" or "readold" or "readfwd" or "headers" or "readgt" or "do_search"
+ * Set oper to "readnew" or "readold" or "readfwd" or "headers" or "readgt" or "readlt" or "do_search"
  */
 void readloop(long oper)
 {
@@ -1673,6 +1673,7 @@ void h_readfwd(void) { readloop(readfwd);}
 void h_headers(void) { readloop(headers);}
 void h_do_search(void) { readloop(do_search);}
 void h_readgt(void) { readloop(readgt);}
+void h_readlt(void) { readloop(readlt);}
 
 void jsonMessageListHdr(void) 
 {
@@ -1748,6 +1749,7 @@ InitModule_MSG
        WebcitAddUrlHandler(HKEY("readfwd"), "", 0, h_readfwd, NEED_URL);
        WebcitAddUrlHandler(HKEY("headers"), "", 0, h_headers, NEED_URL);
        WebcitAddUrlHandler(HKEY("readgt"), "", 0, h_readgt, NEED_URL);
+       WebcitAddUrlHandler(HKEY("readlt"), "", 0, h_readlt, NEED_URL);
        WebcitAddUrlHandler(HKEY("do_search"), "", 0, h_do_search, 0);
        WebcitAddUrlHandler(HKEY("display_enter"), "", 0, display_enter, 0);
        WebcitAddUrlHandler(HKEY("post"), "", 0, post_message, 0);
index 321ef1f89c9a9353653c15ac1ceb1cffe74d2132..e543ff0322d88e9b39099ce48099d79c3f2386d9 100644 (file)
@@ -71,7 +71,8 @@ enum {
        readfwd,
        readnew,
        readold,
-       readgt
+       readgt,
+       readlt
 };
 
 typedef void (*readloop_servcmd)(char *buf, long bufsize);
index c7cb3670ede8325451db6bfc355f022c0c7e42a6..1766f9ed2ecff226af7f69feb0b53d597ad7cb64 100644 (file)
@@ -1129,6 +1129,11 @@ void servcmd_readgt(char *buf, long bufsize)
        snprintf(buf, bufsize, "MSGS GT|%s", bstr("gt"));
 }
 
+void servcmd_readlt(char *buf, long bufsize)
+{
+       snprintf(buf, bufsize, "MSGS LT|%s", bstr("lt"));
+}
+
 void servcmd_readnew(char *buf, long bufsize)
 {
        snprintf(buf, bufsize, "MSGS NEW");
@@ -1141,12 +1146,13 @@ void servcmd_readold(char *buf, long bufsize)
 
 
 readloop_struct rlid[] = {
-       { {HKEY("do_search")}, servcmd_do_search},
-       { {HKEY("headers")},   servcmd_headers},
-       { {HKEY("readfwd")},   servcmd_readfwd},
-       { {HKEY("readnew")},   servcmd_readnew},
-       { {HKEY("readold")},   servcmd_readold},
-       { {HKEY("readgt")},    servcmd_readgt}
+       { {HKEY("do_search")},  servcmd_do_search       },
+       { {HKEY("headers")},    servcmd_headers         },
+       { {HKEY("readfwd")},    servcmd_readfwd         },
+       { {HKEY("readnew")},    servcmd_readnew         },
+       { {HKEY("readold")},    servcmd_readold         },
+       { {HKEY("readgt")},     servcmd_readgt          },
+       { {HKEY("readlt")},     servcmd_readlt          }
 };
 
 /* Spit out the new summary view. This is basically a static page, so clients can cache the layout, all the dirty work is javascript :) */