1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use super::{api, backend, models};
use rocket::fairing::AdHoc;
use rocket::http::{Cookie, CookieJar, SameSite};
use rocket::request::Form;
use rocket::response::Redirect;
use rocket_contrib::json::Json;
use rocket_contrib::serve::StaticFiles;
use rocket_contrib::templates::Template;
use rocket_oauth2::{OAuth2, TokenResponse};
use serde::Serialize;

struct OAuthWikimedia;

#[database("toolsdb")]
pub struct ToolsDb(diesel::MysqlConnection);

/// root.html
#[derive(Serialize)]
struct IndexTemplate {
    title: String,
    username: Option<String>,
    bookmarklet: String,
}

/// syntactic helper to get the token out of cookies
fn extract_token(cookies: &CookieJar) -> Option<String> {
    match cookies.get_private("token") {
        Some(token) => Some(token.value().to_string()),
        None => None,
    }
}

#[get("/")]
async fn index(cookies: &CookieJar<'_>) -> Template {
    let username = if let Some(token) = extract_token(cookies) {
        api::get_username(&token).await
    } else {
        None
    };
    Template::render(
        "root",
        IndexTemplate {
            title: "Suggestor".to_string(),
            username,
            bookmarklet: "".to_string(),
        },
    )
}

/// Redirect to start OAuth2 flow
#[get("/login")]
fn oauth_login(
    oauth2: OAuth2<OAuthWikimedia>,
    cookies: &CookieJar<'_>,
) -> Redirect {
    oauth2.get_redirect(&cookies, &[]).unwrap()
}

/// Redirect target to get and save the access token
#[get("/auth")]
fn oauth_auth(
    token: TokenResponse<OAuthWikimedia>,
    cookies: &CookieJar<'_>,
) -> Redirect {
    cookies.add_private(
        Cookie::build("token", token.access_token().to_string())
            .same_site(SameSite::Lax)
            .finish(),
    );
    Redirect::to("/")
}

#[derive(FromForm)]
pub struct EditForm {
    pub wiki: String,
    pub text: String,
    pub summary: String,
    pub baserevid: u32,
    pub pageid: u32,
    pub pagename: String,
}

#[derive(Serialize)]
struct ApiResponse {
    id: u32,
    error: Option<String>,
}

/// Primary endpoint for data submission
#[post("/api", data = "<edit_form>")]
async fn api_endpoint(
    conn: ToolsDb,
    edit_form: Form<EditForm>,
) -> Json<ApiResponse> {
    let form = edit_form.into_inner();
    match backend::insert_edit(form, &conn).await {
        Ok(edit) => Json(ApiResponse {
            id: edit.id,
            error: None,
        }),
        Err(e) => Json(ApiResponse {
            id: 0,
            error: Some(e.to_string()),
        }),
    }
}

/// pending.html
#[derive(Serialize)]
struct PendingTemplate {
    title: String,
    pendings: Vec<models::Edit>,
}

#[get("/pending")]
async fn pending(conn: ToolsDb) -> Template {
    // TODO: error handling
    let pendings = backend::load_edits_with_state("pending".to_string(), &conn)
        .await
        .unwrap();
    Template::render(
        "pending",
        PendingTemplate {
            pendings,
            title: "Pending edits".to_string(),
        },
    )
}

/// diff.html
#[derive(Serialize)]
struct DiffTemplate {
    title: String,
    edit: models::Edit,
    diff: String,
}

#[get("/diff/<edit_id>")]
async fn diff(edit_id: u32, conn: ToolsDb) -> Template {
    // TODO: error template if doesn't exist
    let edit = backend::load_edit(edit_id, &conn).await.unwrap();
    // TODO: cache in redis?
    let diff = api::get_diff(&edit).await;
    Template::render(
        "diff",
        DiffTemplate {
            title: format!("Diff for proposed edit #{}", edit_id),
            edit,
            diff,
        },
    )
}

#[derive(FromForm)]
struct StatusForm {
    new_state: String,
}

/// status.html
#[derive(Serialize)]
struct StatusTemplate {
    title: String,
    error: Option<String>,
}

fn build_status(error: Option<String>) -> Template {
    Template::render(
        "status",
        StatusTemplate {
            title: "Updating status...".to_string(),
            error,
        },
    )
}

#[post("/status/<edit_id>", data = "<form>")]
async fn status(
    edit_id: u32,
    conn: ToolsDb,
    form: Form<StatusForm>,
    cookies: &CookieJar<'_>,
) -> Result<Template, Redirect> {
    // FIXME: csrf protection
    let token = match extract_token(cookies) {
        Some(token) => token,
        // Not logged in...send through flow again
        None => return Err(Redirect::to("/login")),
    };
    let edit = backend::load_edit(edit_id, &conn).await;
    if edit.is_none() {
        return Ok(build_status(Some("invalid edit id provided".to_string())));
    }
    let new_state = match form.new_state.as_str() {
        "Approve" => "published",
        "Reject" => "rejected",
        _ => {
            return Ok(build_status(Some("invalid state provided".to_string())))
        }
    };
    if new_state == "published" {
        // If we're supposed to publish it, try to make the edit
        let resp = api::make_edit(edit.unwrap(), &token).await;
        if let Err(e) = resp {
            return Ok(build_status(Some(e.to_string())));
        }
    }
    // Update the database status
    Ok(
        match backend::set_state(edit_id, new_state.to_string(), &conn).await {
            Ok(_) => build_status(None),
            Err(e) => build_status(Some(e.to_string())),
        },
    )
}

// Add `/healthz` endpoint
// Disabled because causing SIGILL on `cargo test`
// rocket_healthz::healthz!();

pub fn rocket() -> rocket::Rocket {
    rocket::ignite()
        .attach(Template::fairing())
        .attach(ToolsDb::fairing())
        .attach(AdHoc::on_attach(
            "database migrations",
            super::run_db_migrations,
        ))
        .attach(OAuth2::<OAuthWikimedia>::fairing("wikimedia"))
        .mount(
            "/",
            routes![
                index,
                oauth_login,
                oauth_auth,
                api_endpoint,
                pending,
                diff,
                status,
                // healthz
            ],
        )
        .mount(
            "/public",
            StaticFiles::from(concat!(env!("CARGO_MANIFEST_DIR"), "/public")),
        )
}