Don't overwrite already resolved participants, better naming of keys
This commit is contained in:
@@ -75,7 +75,7 @@ impl From<kordophone::model::Conversation> for Conversation {
|
||||
participants: value
|
||||
.participant_display_names
|
||||
.into_iter()
|
||||
.map(|p| p.into())
|
||||
.map(|p| Participant::Remote { handle: p, contact_id: None }) // todo: this is wrong
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ use diesel::prelude::*;
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct Record {
|
||||
pub id: String,
|
||||
pub sender_participant_id: Option<i32>,
|
||||
pub sender_participant_handle: Option<String>,
|
||||
pub text: String,
|
||||
pub date: NaiveDateTime,
|
||||
pub file_transfer_guids: Option<String>, // JSON array
|
||||
@@ -28,9 +28,9 @@ impl From<Message> for Record {
|
||||
|
||||
Self {
|
||||
id: message.id,
|
||||
sender_participant_id: match message.sender {
|
||||
sender_participant_handle: match message.sender {
|
||||
Participant::Me => None,
|
||||
Participant::Remote { id, .. } => id,
|
||||
Participant::Remote { handle, .. } => Some(handle),
|
||||
},
|
||||
text: message.text,
|
||||
date: message.date,
|
||||
@@ -51,10 +51,13 @@ impl From<Record> for Message {
|
||||
.attachment_metadata
|
||||
.and_then(|json| serde_json::from_str(&json).ok());
|
||||
|
||||
let message_sender = match record.sender_participant_handle {
|
||||
Some(handle) => Participant::Remote { handle, contact_id: None },
|
||||
None => Participant::Me,
|
||||
};
|
||||
Self {
|
||||
id: record.id,
|
||||
// We'll set the proper sender later when loading participant info
|
||||
sender: Participant::Me,
|
||||
sender: message_sender,
|
||||
text: record.text,
|
||||
date: record.date,
|
||||
file_transfer_guids,
|
||||
|
||||
@@ -4,9 +4,9 @@ use diesel::prelude::*;
|
||||
|
||||
#[derive(Queryable, Selectable, AsChangeset, Identifiable)]
|
||||
#[diesel(table_name = crate::schema::participants)]
|
||||
#[diesel(primary_key(handle))]
|
||||
pub struct Record {
|
||||
pub id: i32,
|
||||
pub display_name: Option<String>,
|
||||
pub handle: String,
|
||||
pub is_me: bool,
|
||||
pub contact_id: Option<String>,
|
||||
}
|
||||
@@ -14,7 +14,7 @@ pub struct Record {
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = crate::schema::participants)]
|
||||
pub struct InsertableRecord {
|
||||
pub display_name: Option<String>,
|
||||
pub handle: String,
|
||||
pub is_me: bool,
|
||||
pub contact_id: Option<String>,
|
||||
}
|
||||
@@ -23,12 +23,12 @@ impl From<Participant> for InsertableRecord {
|
||||
fn from(participant: Participant) -> Self {
|
||||
match participant {
|
||||
Participant::Me => InsertableRecord {
|
||||
display_name: None,
|
||||
handle: "me".to_string(),
|
||||
is_me: true,
|
||||
contact_id: None,
|
||||
},
|
||||
Participant::Remote { display_name, contact_id, .. } => InsertableRecord {
|
||||
display_name: Some(display_name),
|
||||
Participant::Remote { handle, contact_id, .. } => InsertableRecord {
|
||||
handle,
|
||||
is_me: false,
|
||||
contact_id,
|
||||
},
|
||||
@@ -38,12 +38,12 @@ impl From<Participant> for InsertableRecord {
|
||||
|
||||
#[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(belongs_to(Record, foreign_key = participant_handle))]
|
||||
#[diesel(table_name = conversation_participants)]
|
||||
#[diesel(primary_key(conversation_id, participant_id))]
|
||||
#[diesel(primary_key(conversation_id, participant_handle))]
|
||||
pub struct ConversationParticipant {
|
||||
pub conversation_id: String,
|
||||
pub participant_id: i32,
|
||||
pub participant_handle: String,
|
||||
}
|
||||
|
||||
impl From<Record> for Participant {
|
||||
@@ -52,8 +52,7 @@ impl From<Record> for Participant {
|
||||
Participant::Me
|
||||
} else {
|
||||
Participant::Remote {
|
||||
id: Some(record.id),
|
||||
display_name: record.display_name.unwrap_or_default(),
|
||||
handle: record.handle.clone(),
|
||||
contact_id: record.contact_id,
|
||||
}
|
||||
}
|
||||
@@ -64,14 +63,12 @@ impl From<Participant> for Record {
|
||||
fn from(participant: Participant) -> Self {
|
||||
match participant {
|
||||
Participant::Me => Record {
|
||||
id: 0, // This will be set by the database
|
||||
display_name: None,
|
||||
handle: "me".to_string(),
|
||||
is_me: true,
|
||||
contact_id: None,
|
||||
},
|
||||
Participant::Remote { display_name, contact_id, .. } => Record {
|
||||
id: 0, // This will be set by the database
|
||||
display_name: Some(display_name),
|
||||
Participant::Remote { handle, contact_id, .. } => Record {
|
||||
handle,
|
||||
is_me: false,
|
||||
contact_id,
|
||||
},
|
||||
|
||||
@@ -27,8 +27,7 @@ impl From<kordophone::model::Message> for Message {
|
||||
id: value.guid,
|
||||
sender: match value.sender {
|
||||
Some(sender) => Participant::Remote {
|
||||
id: None,
|
||||
display_name: sender,
|
||||
handle: sender,
|
||||
contact_id: None,
|
||||
},
|
||||
None => Participant::Me,
|
||||
|
||||
@@ -5,4 +5,4 @@ pub mod participant;
|
||||
|
||||
pub use conversation::Conversation;
|
||||
pub use message::Message;
|
||||
pub use participant::Participant;
|
||||
pub use participant::Participant;
|
||||
@@ -2,37 +2,21 @@
|
||||
pub enum Participant {
|
||||
Me,
|
||||
Remote {
|
||||
id: Option<i32>,
|
||||
display_name: String,
|
||||
handle: String,
|
||||
contact_id: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
impl From<String> for Participant {
|
||||
fn from(display_name: String) -> Self {
|
||||
Participant::Remote {
|
||||
id: None,
|
||||
display_name,
|
||||
contact_id: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&str> for Participant {
|
||||
fn from(display_name: &str) -> Self {
|
||||
Participant::Remote {
|
||||
id: None,
|
||||
display_name: display_name.to_string(),
|
||||
contact_id: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Participant {
|
||||
pub fn display_name(&self) -> String {
|
||||
pub fn handle(&self) -> String {
|
||||
match self {
|
||||
Participant::Me => "(Me)".to_string(),
|
||||
Participant::Remote { display_name, .. } => display_name.clone(),
|
||||
Participant::Remote { handle, .. } => handle.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
// Temporary alias for backward compatibility
|
||||
pub fn display_name(&self) -> String {
|
||||
self.handle()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user