| | @@ -597,10 +597,87 @@ |
| 597 | 597 | db_finalize(&q1); |
| 598 | 598 | blob_append(&json, "\n]}", 3); |
| 599 | 599 | cgi_set_content(&json); |
| 600 | 600 | return; |
| 601 | 601 | } |
| 602 | + |
| 603 | +/* |
| 604 | +** WEBPAGE: chat-fetch-one hidden |
| 605 | +** |
| 606 | +** /chat-fetch-one/N |
| 607 | +** |
| 608 | +** Fetches a single message with the given ID, if available. |
| 609 | +** |
| 610 | +** Options: |
| 611 | +** |
| 612 | +** raw = the xmsg field will be returned unparsed. |
| 613 | +** |
| 614 | +** Response is either a single object in the format returned by |
| 615 | +** /chat-poll (without the wrapper array) or a JSON-format error |
| 616 | +** response, as documented for ajax_route_error(). |
| 617 | +*/ |
| 618 | +void chat_fetch_one(void){ |
| 619 | + Blob json = empty_blob; /* The json to be constructed and returned */ |
| 620 | + const int fRaw = PD("raw",0)!=0; |
| 621 | + const int msgid = atoi(PD("name","0")); |
| 622 | + Stmt q; |
| 623 | + login_check_credentials(); |
| 624 | + if( !g.perm.Chat ) { |
| 625 | + chat_emit_permissions_error(0); |
| 626 | + return; |
| 627 | + } |
| 628 | + chat_create_tables(); |
| 629 | + cgi_set_content_type("application/json"); |
| 630 | + db_prepare(&q, |
| 631 | + "SELECT datetime(mtime), xfrom, xmsg, length(file)," |
| 632 | + " fname, fmime, lmtime" |
| 633 | + " FROM chat WHERE msgid=%d AND mdel IS NULL", |
| 634 | + msgid); |
| 635 | + if(SQLITE_ROW==db_step(&q)){ |
| 636 | + const char *zDate = db_column_text(&q, 0); |
| 637 | + const char *zFrom = db_column_text(&q, 1); |
| 638 | + const char *zRawMsg = db_column_text(&q, 2); |
| 639 | + const int nByte = db_column_int(&q, 3); |
| 640 | + const char *zFName = db_column_text(&q, 4); |
| 641 | + const char *zFMime = db_column_text(&q, 5); |
| 642 | + const char *zLMtime = db_column_text(&q, 7); |
| 643 | + blob_appendf(&json,"{\"msgid\": %d,", msgid); |
| 644 | + |
| 645 | + blob_appendf(&json, "\"mtime\":\"%.10sT%sZ\",", zDate, zDate+11); |
| 646 | + if( zLMtime && zLMtime[0] ){ |
| 647 | + blob_appendf(&json, "\"lmtime\":%!j,", zLMtime); |
| 648 | + } |
| 649 | + blob_append(&json, "\"xfrom\":", -1); |
| 650 | + if(zFrom){ |
| 651 | + blob_appendf(&json, "%!j,", zFrom); |
| 652 | + }else{ |
| 653 | + /* see https://fossil-scm.org/forum/forumpost/e0be0eeb4c */ |
| 654 | + blob_appendf(&json, "null,"); |
| 655 | + } |
| 656 | + blob_appendf(&json, "\"uclr\":%!j,", |
| 657 | + user_color(zFrom ? zFrom : "nobody")); |
| 658 | + blob_append(&json,"\"xmsg\":", 7); |
| 659 | + if(fRaw){ |
| 660 | + blob_appendf(&json, "%!j,", zRawMsg); |
| 661 | + }else{ |
| 662 | + char * zMsg = chat_format_to_html(zRawMsg ? zRawMsg : ""); |
| 663 | + blob_appendf(&json, "%!j,", zMsg); |
| 664 | + fossil_free(zMsg); |
| 665 | + } |
| 666 | + if( nByte==0 ){ |
| 667 | + blob_appendf(&json, "\"fsize\":0"); |
| 668 | + }else{ |
| 669 | + blob_appendf(&json, "\"fsize\":%d,\"fname\":%!j,\"fmime\":%!j", |
| 670 | + nByte, zFName, zFMime); |
| 671 | + } |
| 672 | + blob_append(&json,"}",1); |
| 673 | + cgi_set_content(&json); |
| 674 | + }else{ |
| 675 | + ajax_route_error(404,"Chat message #%d not found.", msgid); |
| 676 | + } |
| 677 | + db_finalize(&q); |
| 678 | +} |
| 602 | 679 | |
| 603 | 680 | /* |
| 604 | 681 | ** WEBPAGE: chat-download hidden |
| 605 | 682 | ** |
| 606 | 683 | ** Download the CHAT.FILE attachment associated with a single chat |
| 607 | 684 | |