Started working on contact resolution
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
-- Revert participants table to remove contact_id column
|
||||
-- SQLite does not support DROP COLUMN directly, so we recreate the table without contact_id
|
||||
PRAGMA foreign_keys=off;
|
||||
CREATE TABLE participants_backup (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
display_name TEXT,
|
||||
is_me BOOL NOT NULL
|
||||
);
|
||||
INSERT INTO participants_backup (id, display_name, is_me)
|
||||
SELECT id, display_name, is_me
|
||||
FROM participants;
|
||||
DROP TABLE participants;
|
||||
ALTER TABLE participants_backup RENAME TO participants;
|
||||
PRAGMA foreign_keys=on;
|
||||
@@ -0,0 +1,2 @@
|
||||
-- Add contact_id column to participants to store an external contact identifier (e.g., Folks ID)
|
||||
ALTER TABLE participants ADD COLUMN contact_id TEXT;
|
||||
@@ -8,6 +8,7 @@ pub struct Record {
|
||||
pub id: i32,
|
||||
pub display_name: Option<String>,
|
||||
pub is_me: bool,
|
||||
pub contact_id: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
@@ -15,19 +16,22 @@ pub struct Record {
|
||||
pub struct InsertableRecord {
|
||||
pub display_name: Option<String>,
|
||||
pub is_me: bool,
|
||||
pub contact_id: Option<String>,
|
||||
}
|
||||
|
||||
impl From<Participant> 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,
|
||||
},
|
||||
Participant::Me => InsertableRecord {
|
||||
display_name: None,
|
||||
is_me: true,
|
||||
contact_id: None,
|
||||
},
|
||||
Participant::Remote { display_name, contact_id, .. } => InsertableRecord {
|
||||
display_name: Some(display_name),
|
||||
is_me: false,
|
||||
contact_id,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,6 +54,7 @@ impl From<Record> for Participant {
|
||||
Participant::Remote {
|
||||
id: Some(record.id),
|
||||
display_name: record.display_name.unwrap_or_default(),
|
||||
contact_id: record.contact_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,16 +63,18 @@ impl From<Record> for Participant {
|
||||
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,
|
||||
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,
|
||||
},
|
||||
Participant::Me => Record {
|
||||
id: 0, // This will be set by the database
|
||||
display_name: None,
|
||||
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),
|
||||
is_me: false,
|
||||
contact_id,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ impl From<kordophone::model::Message> for Message {
|
||||
Some(sender) => Participant::Remote {
|
||||
id: None,
|
||||
display_name: sender,
|
||||
contact_id: None,
|
||||
},
|
||||
None => Participant::Me,
|
||||
},
|
||||
|
||||
@@ -4,6 +4,7 @@ pub enum Participant {
|
||||
Remote {
|
||||
id: Option<i32>,
|
||||
display_name: String,
|
||||
contact_id: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -12,6 +13,7 @@ impl From<String> for Participant {
|
||||
Participant::Remote {
|
||||
id: None,
|
||||
display_name,
|
||||
contact_id: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,6 +23,7 @@ impl From<&str> for Participant {
|
||||
Participant::Remote {
|
||||
id: None,
|
||||
display_name: display_name.to_string(),
|
||||
contact_id: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,7 @@ use crate::{
|
||||
},
|
||||
Conversation, Message, Participant,
|
||||
},
|
||||
schema,
|
||||
target,
|
||||
schema, target,
|
||||
};
|
||||
|
||||
pub struct Repository<'a> {
|
||||
@@ -195,6 +194,7 @@ impl<'a> Repository<'a> {
|
||||
let new_participant = InsertableParticipantRecord {
|
||||
display_name: Some(display_name.clone()),
|
||||
is_me: false,
|
||||
contact_id: None,
|
||||
};
|
||||
|
||||
diesel::insert_into(participants_dsl::participants)
|
||||
@@ -371,11 +371,27 @@ impl<'a> Repository<'a> {
|
||||
)
|
||||
}
|
||||
|
||||
/// Update the contact_id for an existing participant record.
|
||||
pub fn update_participant_contact(
|
||||
&mut self,
|
||||
participant_db_id: i32,
|
||||
new_contact_id: &str,
|
||||
) -> Result<()> {
|
||||
use crate::schema::participants::dsl::*;
|
||||
|
||||
log::debug!(target: target::REPOSITORY, "Updating participant contact id {} => {}", participant_db_id, new_contact_id);
|
||||
diesel::update(participants.filter(id.eq(participant_db_id)))
|
||||
.set(contact_id.eq(Some(new_contact_id.to_string())))
|
||||
.execute(self.connection)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_or_create_participant(&mut self, participant: &Participant) -> Option<i32> {
|
||||
match participant {
|
||||
Participant::Me => None,
|
||||
Participant::Remote {
|
||||
display_name: p_name,
|
||||
contact_id: c_id,
|
||||
..
|
||||
} => {
|
||||
use crate::schema::participants::dsl::*;
|
||||
@@ -393,6 +409,7 @@ impl<'a> Repository<'a> {
|
||||
let participant_record = InsertableParticipantRecord {
|
||||
display_name: Some(participant.display_name()),
|
||||
is_me: false,
|
||||
contact_id: c_id.clone(),
|
||||
};
|
||||
|
||||
diesel::insert_into(participants)
|
||||
|
||||
@@ -16,6 +16,7 @@ diesel::table! {
|
||||
id -> Integer,
|
||||
display_name -> Nullable<Text>,
|
||||
is_me -> Bool,
|
||||
contact_id -> Nullable<Text>,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user