| | 43 | bool markread::action(boost::shared_ptr<request> req) |
| | 44 | { |
| | 45 | enum { QUERY, MARK, UNMARK } action; |
| | 46 | action = req->get_method() == "POST" |
| | 47 | ? (req->get_form_field("undo") != "yes" |
| | 48 | ? MARK |
| | 49 | : UNMARK) |
| | 50 | : QUERY; |
| | 51 | |
| | 52 | db::user::ptr u = req->get_user(); |
| | 53 | if (!u) throw noauth(); |
| | 54 | |
| | 55 | std::string msgid = req->get_form_field("msgid"); |
| | 56 | |
| | 57 | cb.dbase().lookup_msgid(msgid); |
| | 58 | |
| | 59 | switch (action) |
| | 60 | { |
| | 61 | case QUERY: |
| | 62 | break; |
| | 63 | case MARK: |
| | 64 | u->mark_read(msgid); |
| | 65 | break; |
| | 66 | case UNMARK: |
| | 67 | u->unmark_read(msgid); |
| | 68 | break; |
| | 69 | } |
| | 70 | return u->has_read(msgid); |
| | 71 | } |
| | 72 | |
| 47 | | boost::shared_ptr<resource> r |
| 48 | | (new reauthn_resource(cb)); |
| 49 | | throw resource_exception(r); |
| 50 | | } |
| 51 | | |
| 52 | | std::string ajax = req->get_form_field("ajax"); |
| 53 | | bool value = req->get_form_field("undo") != "yes"; |
| 54 | | std::string msgid = req->get_form_field("msgid"); |
| 55 | | ::uri redir = req->get_form_field("redir"); |
| 56 | | redir.replace_query_param("markread","no"); |
| 57 | | |
| 58 | | if (req->get_method() == "POST") |
| 59 | | { |
| 60 | | if (value) |
| 61 | | u->mark_read(msgid); |
| 62 | | else |
| 63 | | u->unmark_read(msgid); |
| 64 | | } |
| 65 | | if (ajax.empty() || ajax == "no") |
| 66 | | { |
| | 78 | try |
| | 79 | { |
| | 80 | action(req); |
| | 81 | } |
| | 82 | catch (noauth) |
| | 83 | { |
| | 84 | boost::shared_ptr<resource> r |
| | 85 | (new reauthn_resource(cb)); |
| | 86 | throw resource_exception(r); |
| | 87 | } |
| | 88 | catch (db::no_such_article) |
| | 89 | { |
| | 90 | ; |
| | 91 | } |
| | 92 | ::uri redir = req->get_form_field("redir"); |
| | 93 | redir.replace_query_param("markread","no"); |
| 71 | | } |
| 72 | | boost::shared_ptr<response> rv = rf("200 Ok"); |
| 73 | | std::ostream &os = rv->body("text/plain"); |
| 74 | | os << "AJAX " |
| 75 | | << (u->has_read(msgid) ? "YES" : "NO") |
| 76 | | << "\r\n"; |
| 77 | | return rv; |
| | 98 | } |
| | 99 | if (req->get_path() == "/markread/ajax") |
| | 100 | { |
| | 101 | std::string res; |
| | 102 | try |
| | 103 | { |
| | 104 | res = action(req) ? "YES" : "NO"; |
| | 105 | } |
| | 106 | catch (noauth) |
| | 107 | { |
| | 108 | res = "ERR NOAUTH"; |
| | 109 | } |
| | 110 | catch (db::no_such_article) |
| | 111 | { |
| | 112 | res = "ERR NOMSG"; |
| | 113 | } |
| | 114 | boost::shared_ptr<response> rv = rf("200 Ok"); |
| | 115 | std::ostream &os = rv->body("text/plain"); |
| | 116 | os << "AJAX " << res << "\r\n"; |
| | 117 | return rv; |
| | 118 | } |
| | 119 | else |
| | 120 | { |
| | 121 | boost::shared_ptr<error_resource> r |
| | 122 | (new error_resource(cb, "404 Not found")); |
| | 123 | throw resource_exception(r); |
| | 124 | } |