pub fn insert_message(
    room: Room,
    user: User,
    data: &[u8],
    signature: &[u8],
) -> Result<Response, Rejection> {
    let mut conn = storage::get_conn()?;
    let tx = storage::get_transaction(&mut conn)?;
    require_authorization(
        &tx,
        &user,
        &room,
        AuthorizationRequired { write: true, ..Default::default() },
    )?;

    // Check if the requesting user needs to be rate limited

    let now_secs = unixtime_f64() - RATE_LIMIT_INTERVAL as f64;

    let recent_posts: i64 = tx
        .prepare_cached("SELECT COUNT(*) FROM messages WHERE room = ? AND user = ? AND posted >= ?")
        .map_err(db_error)?
        .query_row(params![room.id, user.id, now_secs], |row| row.get(0))
        .map_err(db_error)?;
    if recent_posts >= RATE_LIMIT_POSTS {
        return Err(warp::reject::custom(Error::RateLimited));
    }

    // Don't store useless padding; we'll repad (since it's needed for signature verification) when
    // we retrieve.
    let size = data.len();
    let trimmed = match data.iter().rposition(|&c| c != 0u8) {
        Some(last) => &data[0..=last],
        None => &data,
    };

    // Insert the message
    let message = match tx
        .prepare_cached(
            "INSERT INTO messages (room, user, data, data_size, signature) \
             VALUES (?, ?, ?, ?, ?) \
             RETURNING *",
        )
        .map_err(db_error)?
        .query_row(params![room.id, user.id, trimmed, size, signature], OldMessage::from_row)
    {
        Ok(m) => m,
        Err(e) => {
            error!("Couldn't insert message: {}.", e);
            return Err(warp::reject::custom(Error::DatabaseFailedInternally));
        }
    };

    // Commit
    if let Err(e) = tx.commit() {
        error!("Failed to commit message: {}", e);
        return Err(warp::reject::custom(Error::DatabaseFailedInternally));
    }

    let response = json!({ "status_code": StatusCode::OK.as_u16(), "message": message });
    Ok(warp::reply::json(&response).into_response())
}
/// Inserts a message into the database.
Inserts a message into the database.
Inserts a message into the database .
pub fn insert_message(
    room: Room,
    user: User,
    data: &[u8],
    signature: &[u8],
) -> Result<Response, Rejection> {
    let mut conn = storage::get_conn()?;
    let tx = storage::get_transaction(&mut conn)?;
    require_authorization(
        &tx,
        &user,
        &room,
        AuthorizationRequired { write: true, ..Default::default() },
    )?;
    // Check if the requesting user needs to be rate limited
    let now_secs = unixtime_f64() - RATE_LIMIT_INTERVAL as f64;
    let recent_posts: i64 = tx
        .prepare_cached("SELECT COUNT(*) FROM messages WHERE room = ? AND user = ? AND posted >= ?")
        .map_err(db_error)?
        .query_row(params![room.id, user.id, now_secs], |row| row.get(0))
        .map_err(db_error)?;
    if recent_posts >= RATE_LIMIT_POSTS {
        return Err(warp::reject::custom(Error::RateLimited));
    }
    // Don't store useless padding; we'll repad (since it's needed for signature verification) when
    // we retrieve.
    let size = data.len();
    let trimmed = match data.iter().rposition(|&c| c != 0u8) {
        Some(last) => &data[0..=last],
        None => &data,
    };
    // Insert the message
    let message = match tx
        .prepare_cached(
            "INSERT INTO messages (room, user, data, data_size, signature) \
             VALUES (?, ?, ?, ?, ?) \
             RETURNING *",
        )
        .map_err(db_error)?
        .query_row(params![room.id, user.id, trimmed, size, signature], OldMessage::from_row)
    {
        Ok(m) => m,
        Err(e) => {
            error!("Couldn't insert message: {}.", e);
            return Err(warp::reject::custom(Error::DatabaseFailedInternally));
        }
    };
    // Commit
    if let Err(e) = tx.commit() {
        error!("Failed to commit message: {}", e);
        return Err(warp::reject::custom(Error::DatabaseFailedInternally));
    }
    let response = json!({ "status_code": StatusCode::OK.as_u16(), "message": message });
    Ok(warp::reply::json(&response).into_response())
}
pub fn insert_message ( room : Room , user : User , data : & [ u8 ] , signature : & [ u8 ] , ) -> Result < Response , Rejection > { let mut conn = storage :: get_conn ( ) ? ; let tx = storage :: get_transaction ( & mut conn ) ? ; require_authorization ( & tx , & user , & room , AuthorizationRequired { write : true , .. Default :: default ( ) } , ) ? ; // Check if the requesting user needs to be rate limited let now_secs = unixtime_f64 ( ) - RATE_LIMIT_INTERVAL as f64 ; let recent_posts : i64 = tx . prepare_cached ( " " ) . map_err ( db_error ) ? . query_row ( params ! [ room id user id now_secs ] , | row | row . get ( 0 ) ) . map_err ( db_error ) ? ; if recent_posts >= RATE_LIMIT_POSTS { return Err ( warp :: reject :: custom ( Error :: RateLimited ) ) ; } // Don't store useless padding; we'll repad (since it's needed for signature verification) when // we retrieve. let size = data . len ( ) ; let trimmed = match data . iter ( ) . rposition ( | & c | c != 0u8 ) { Some ( last ) => & data [ 0 ..= last ] , None => & data , } ; // Insert the message let message = match tx . prepare_cached ( " \
 \
 " , ) . map_err ( db_error ) ? . query_row ( params ! [ room id user id trimmed size signature ] , OldMessage :: from_row ) { Ok ( m ) => m , Err ( e ) => { error ! ( " " e ) ; return Err ( warp :: reject :: custom ( Error :: DatabaseFailedInternally ) ) ; } } ; // Commit if let Err ( e ) = tx . commit ( ) { error ! ( " " e ) ; return Err ( warp :: reject :: custom ( Error :: DatabaseFailedInternally ) ) ; } let response = json ! ( { " " StatusCode OK as_u16 ( ) " " message } ) ; Ok ( warp :: reply :: json ( & response ) . into_response ( ) ) }
Inserts a message into the database.
Inserts a message into the database .
room
Room
user
User
data
&[u8]
signature
&[u8]
room
Room
user
User
data
&[u8]
signature
&[u8]