Module message

Module message 

Source
Expand description

This module represents common (but not all) columns in the message table.

§Iterating over Message Data

Generally, use Message::stream() to iterate over message rows.

§Example

use imessage_database::{
    tables::{
        messages::Message,
        table::{get_connection, Diagnostic, Table},
    },
    util::dirs::default_db_path,
};

// Your custom error type
struct ProgramError;

// Get the default database path and connect to it
let db_path = default_db_path();
let conn = get_connection(&db_path).unwrap();

Message::stream(&conn, |message_result| {
   match message_result {
       Ok(message) => println!("Message: {:#?}", message),
       Err(e) => eprintln!("Error: {:?}", e),
   }
   Ok::<(), ProgramError>(())
}).unwrap();

§Making Custom Message Queries

In addition columns from the messages table, there are several additional fields represented by Message that are not present in the database:

§Sample Queries

To provide a custom query, ensure inclusion of the foregoing columns:

SELECT
    *,
    c.chat_id,
    (SELECT COUNT(*) FROM message_attachment_join a WHERE m.ROWID = a.message_id) as num_attachments,
    d.chat_id as deleted_from,
    (SELECT COUNT(*) FROM message m2 WHERE m2.thread_originator_guid = m.guid) as num_replies
FROM
    message as m
LEFT JOIN chat_message_join as c ON m.ROWID = c.message_id
LEFT JOIN chat_recoverable_message_join as d ON m.ROWID = d.message_id
ORDER BY
    m.date;

If the source database does not include these required columns, include them as so:

SELECT
    *,
    c.chat_id,
    (SELECT COUNT(*) FROM message_attachment_join a WHERE m.ROWID = a.message_id) as num_attachments,
    NULL as deleted_from,
    0 as num_replies
FROM
    message as m
LEFT JOIN chat_message_join as c ON m.ROWID = c.message_id
ORDER BY
    m.date;

§Custom Query Example

The following will return an iterator over messages that have an associated emoji:

use imessage_database::{
    tables::{
        messages::Message,
        table::{get_connection, Diagnostic, Table},
    },
    util::dirs::default_db_path
};

let db_path = default_db_path();
let db = get_connection(&db_path).unwrap();

let mut statement = db.prepare("
SELECT
    *,
    c.chat_id,
    (SELECT COUNT(*) FROM message_attachment_join a WHERE m.ROWID = a.message_id) as num_attachments,
    d.chat_id as deleted_from,
    (SELECT COUNT(*) FROM message m2 WHERE m2.thread_originator_guid = m.guid) as num_replies
FROM
    message as m
LEFT JOIN chat_message_join as c ON m.ROWID = c.message_id
LEFT JOIN chat_recoverable_message_join as d ON m.ROWID = d.message_id
WHERE m.associated_message_emoji IS NOT NULL
ORDER BY
    m.date;
").unwrap();

let messages = statement.query_map([], |row| Ok(Message::from_row(row))).unwrap();

messages.for_each(|msg| println!("{:#?}", Message::extract(msg)));

Structs§

Message
Represents a single row in the message table.