Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

add more contact metods #6

Merged
merged 2 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,9 @@ async fn sc_chatlist_get_general_fresh_message_counter(&self) -> Result<u32> //

// contacts ------------------------------------------------------------

async fn sc_contacts_block(&self, contact_id: u32) -> Result<()>

async fn sc_contacts_unblock(&self, contact_id: u32) -> Result<()>

async fn sc_contacts_change_nickname(&self, contact_id: u32, new_name: String) -> Result<()>


async fn sc_contacts_get_contact_ids(&self, list_flags: u32, query: String) -> Result<Vec<u32>>

// formerly called getContacts2 in desktop
async fn sc_contacts_get_contacts(&self, list_flags: u32, query: String) -> Result<Vec<Contact>>

async fn sc_contacts_get_contacts_by_ids(&self, ids: Vec<u32>) -> Result<HashMap<u32, Contact>>

// contacts.getChatIdByContactId - very similar to sc_contacts_create_chat_by_contact_id
// contacts.getDMChatId - very similar to sc_contacts_create_chat_by_contact_id

Expand Down
60 changes: 60 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,66 @@ impl CommandApi {
.map(|id| id.to_u32())
}

async fn sc_contacts_block(&self, contact_id: u32) -> Result<()> {
let sc = self.selected_context().await?;
Contact::block(&sc, contact_id).await
}

async fn sc_contacts_unblock(&self, contact_id: u32) -> Result<()> {
let sc = self.selected_context().await?;
Contact::unblock(&sc, contact_id).await
}

async fn sc_contacts_get_contact_ids(
&self,
list_flags: u32,
query: Option<String>,
) -> Result<Vec<u32>> {
let sc = self.selected_context().await?;
Contact::get_all(&sc, list_flags, query).await
}

// formerly called getContacts2 in desktop
async fn sc_contacts_get_contacts(
&self,
list_flags: u32,
query: Option<String>,
) -> Result<Vec<ContactObject>> {
let sc = self.selected_context().await?;
let contact_ids = Contact::get_all(&sc, list_flags, query).await?;
let mut contacts: Vec<ContactObject> = Vec::with_capacity(contact_ids.len());
for id in contact_ids {
contacts.push(
ContactObject::from_dc_contact(
deltachat::contact::Contact::get_by_id(&sc, id).await?,
&sc,
)
.await?,
);
}
Ok(contacts)
}

async fn sc_contacts_get_contacts_by_ids(
&self,
ids: Vec<u32>,
) -> Result<HashMap<u32, ContactObject>> {
let sc = self.selected_context().await?;

let mut contacts = HashMap::with_capacity(ids.len());
for id in ids {
contacts.insert(
id,
ContactObject::from_dc_contact(
deltachat::contact::Contact::get_by_id(&sc, id).await?,
&sc,
)
.await?,
);
}
Ok(contacts)
}

// ---------------------------------------------
// misc prototyping functions
// that might get removed later again
Expand Down
31 changes: 31 additions & 0 deletions typescript/src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,37 @@ export class RawApi {
contact_id,
});
}
public async sc_contacts_block(contact_id: number): Promise<void> {
return await this.json_transport("sc_contacts_block", { contact_id });
}
public async sc_contacts_unblock(contact_id: number): Promise<void> {
return await this.json_transport("sc_contacts_unblock", { contact_id });
}
public async sc_contacts_get_contact_ids(
list_flags: number,
query: string | null
): Promise<number[]> {
return await this.json_transport("sc_contacts_get_contact_ids", {
list_flags,
query,
});
}
public async sc_contacts_get_contacts(
list_flags: number,
query: string | null
): Promise<Contact_Type[]> {
return await this.json_transport("sc_contacts_get_contacts", {
list_flags,
query,
});
}
public async sc_contacts_get_contacts_by_ids(
ids: number[]
): Promise<{ [key: number]: Contact_Type }> {
return await this.json_transport("sc_contacts_get_contacts_by_ids", {
ids,
});
}
public async sc_misc_send_text_message(
text: string,
chat_id: number
Expand Down
21 changes: 21 additions & 0 deletions typescript/test/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,25 @@ describe("basic tests", () => {
assert((await dc.raw_api.get_all_account_ids()).length === 4);
});
});

describe("contact managment", function () {
// could this also be done in an offline test?
before(async () => {
await dc.raw_api.select_account(await dc.raw_api.add_account());
});
it("block and unblock contact", async function () {
const contactId = await dc.raw_api.sc_contacts_create_contact(
"example@delta.chat",
null
);
expect((await dc.raw_api.sc_contacts_get_contact(contactId)).is_blocked)
.to.be.false;
await dc.raw_api.sc_contacts_block(contactId);
expect((await dc.raw_api.sc_contacts_get_contact(contactId)).is_blocked)
.to.be.true;
await dc.raw_api.sc_contacts_unblock(contactId);
expect((await dc.raw_api.sc_contacts_get_contact(contactId)).is_blocked)
.to.be.false;
});
});
});