CREATE VIEW uniform_resource_imap AS
SELECT
ur.uniform_resource_id,
graph.name,
iac.ur_ingest_session_imap_account_id,
iac.email,
iac.host,
iacm.subject,
iacm."from",
iacm.message,
iacm.date,
iaf.ur_ingest_session_imap_acct_folder_id,
iaf.ingest_account_id,
iaf.folder_name,
ur.size_bytes,
ur.nature,
ur.content
FROM uniform_resource ur
INNER JOIN uniform_resource_edge edge ON edge.uniform_resource_id=ur.uniform_resource_id
INNER JOIN uniform_resource_graph graph ON graph.name=edge.graph_name
INNER JOIN ur_ingest_session_imap_acct_folder_message iacm ON iacm.ur_ingest_session_imap_acct_folder_message_id = edge.node_id
INNER JOIN ur_ingest_session_imap_acct_folder iaf ON iacm.ingest_imap_acct_folder_id = iaf.ur_ingest_session_imap_acct_folder_id
LEFT JOIN ur_ingest_session_imap_account iac ON iac.ur_ingest_session_imap_account_id = iaf.ingest_account_id
WHERE ur.nature = 'text' AND graph.name='imap' AND ur.ingest_session_imap_acct_folder_message IS NOT NULL