use crate::models::Participant; use crate::schema::conversation_participants; use diesel::prelude::*; #[derive(Queryable, Selectable, AsChangeset, Identifiable)] #[diesel(table_name = crate::schema::participants)] pub struct Record { pub id: i32, pub display_name: Option, pub is_me: bool, } #[derive(Insertable)] #[diesel(table_name = crate::schema::participants)] pub struct InsertableRecord { pub display_name: Option, pub is_me: bool, } impl From for InsertableRecord { fn from(participant: Participant) -> Self { match participant { Participant::Me => InsertableRecord { display_name: None, is_me: true, }, Participant::Remote { display_name, .. } => InsertableRecord { display_name: Some(display_name), is_me: false, }, } } } #[derive(Identifiable, Selectable, Queryable, Associations, Debug)] #[diesel(belongs_to(super::conversation::Record, foreign_key = conversation_id))] #[diesel(belongs_to(Record, foreign_key = participant_id))] #[diesel(table_name = conversation_participants)] #[diesel(primary_key(conversation_id, participant_id))] pub struct ConversationParticipant { pub conversation_id: String, pub participant_id: i32, } impl From for Participant { fn from(record: Record) -> Self { if record.is_me { Participant::Me } else { Participant::Remote { id: Some(record.id), display_name: record.display_name.unwrap_or_default(), } } } } impl From for Record { fn from(participant: Participant) -> Self { match participant { Participant::Me => Record { id: 0, // This will be set by the database display_name: None, is_me: true, }, Participant::Remote { display_name, .. } => Record { id: 0, // This will be set by the database display_name: Some(display_name), is_me: false, }, } } }