From bff0aef2edab9ae5e63ef037b5ebd6758d02451b Mon Sep 17 00:00:00 2001 From: link2xt Date: Thu, 3 Aug 2023 19:37:26 +0000 Subject: [PATCH] api!(deltachat-jsonrpc): use `kind` as a tag for all union types --- deltachat-jsonrpc/src/api/types/account.rs | 2 +- deltachat-jsonrpc/src/api/types/chat.rs | 1 + deltachat-jsonrpc/src/api/types/chat_list.rs | 2 +- deltachat-jsonrpc/src/api/types/events.rs | 2 +- deltachat-jsonrpc/src/api/types/message.rs | 2 +- deltachat-jsonrpc/src/api/types/qr.rs | 2 +- deltachat-jsonrpc/typescript/example/example.ts | 8 ++++---- deltachat-jsonrpc/typescript/src/client.ts | 12 ++++++------ deltachat-jsonrpc/typescript/test/online.ts | 8 ++++---- deltachat-rpc-client/tests/test_something.py | 10 +++++----- deltachat-rpc-client/tests/test_webxdc.py | 2 +- 11 files changed, 26 insertions(+), 25 deletions(-) diff --git a/deltachat-jsonrpc/src/api/types/account.rs b/deltachat-jsonrpc/src/api/types/account.rs index d27a537238..b2909109db 100644 --- a/deltachat-jsonrpc/src/api/types/account.rs +++ b/deltachat-jsonrpc/src/api/types/account.rs @@ -7,7 +7,7 @@ use typescript_type_def::TypeDef; use super::color_int_to_hex_string; #[derive(Serialize, TypeDef, schemars::JsonSchema)] -#[serde(tag = "type")] +#[serde(tag = "kind")] pub enum Account { #[serde(rename_all = "camelCase")] Configured { diff --git a/deltachat-jsonrpc/src/api/types/chat.rs b/deltachat-jsonrpc/src/api/types/chat.rs index c8d7d867f4..67bd815947 100644 --- a/deltachat-jsonrpc/src/api/types/chat.rs +++ b/deltachat-jsonrpc/src/api/types/chat.rs @@ -167,6 +167,7 @@ impl BasicChat { } #[derive(Clone, Serialize, Deserialize, TypeDef, schemars::JsonSchema)] +#[serde(tag = "kind")] pub enum MuteDuration { NotMuted, Forever, diff --git a/deltachat-jsonrpc/src/api/types/chat_list.rs b/deltachat-jsonrpc/src/api/types/chat_list.rs index 2c15165d9d..510c20e207 100644 --- a/deltachat-jsonrpc/src/api/types/chat_list.rs +++ b/deltachat-jsonrpc/src/api/types/chat_list.rs @@ -18,7 +18,7 @@ use super::message::MessageViewtype; pub struct ChatListEntry(pub u32, pub u32); #[derive(Serialize, TypeDef, schemars::JsonSchema)] -#[serde(tag = "type")] +#[serde(tag = "kind")] pub enum ChatListItemFetchResult { #[serde(rename_all = "camelCase")] ChatListItem { diff --git a/deltachat-jsonrpc/src/api/types/events.rs b/deltachat-jsonrpc/src/api/types/events.rs index 0cfb02a84a..dd03358bcc 100644 --- a/deltachat-jsonrpc/src/api/types/events.rs +++ b/deltachat-jsonrpc/src/api/types/events.rs @@ -22,7 +22,7 @@ impl From for Event { } #[derive(Serialize, TypeDef, schemars::JsonSchema)] -#[serde(tag = "type")] +#[serde(tag = "kind")] pub enum EventType { /// The library-user may write an informational string to the log. /// diff --git a/deltachat-jsonrpc/src/api/types/message.rs b/deltachat-jsonrpc/src/api/types/message.rs index 9e50d90eb4..2d4acec7e9 100644 --- a/deltachat-jsonrpc/src/api/types/message.rs +++ b/deltachat-jsonrpc/src/api/types/message.rs @@ -19,7 +19,7 @@ use super::reactions::JSONRPCReactions; use super::webxdc::WebxdcMessageInfo; #[derive(Serialize, TypeDef, schemars::JsonSchema)] -#[serde(rename_all = "camelCase", tag = "variant")] +#[serde(rename_all = "camelCase", tag = "kind")] pub enum MessageLoadResult { Message(MessageObject), LoadingError { error: String }, diff --git a/deltachat-jsonrpc/src/api/types/qr.rs b/deltachat-jsonrpc/src/api/types/qr.rs index d53ebeab5f..0f6d79c8c3 100644 --- a/deltachat-jsonrpc/src/api/types/qr.rs +++ b/deltachat-jsonrpc/src/api/types/qr.rs @@ -4,7 +4,7 @@ use typescript_type_def::TypeDef; #[derive(Serialize, TypeDef, schemars::JsonSchema)] #[serde(rename = "Qr", rename_all = "camelCase")] -#[serde(tag = "type")] +#[serde(tag = "kind")] pub enum QrObject { AskVerifyContact { contact_id: u32, diff --git a/deltachat-jsonrpc/typescript/example/example.ts b/deltachat-jsonrpc/typescript/example/example.ts index d7b038e0c8..d4bbb67732 100644 --- a/deltachat-jsonrpc/typescript/example/example.ts +++ b/deltachat-jsonrpc/typescript/example/example.ts @@ -35,7 +35,7 @@ async function run() { const accounts = await client.rpc.getAllAccounts(); console.log("accounts loaded", accounts); for (const account of accounts) { - if (account.type === "Configured") { + if (account.kind === "Configured") { write( $head, ` @@ -57,7 +57,7 @@ async function run() { clear($main); const selectedAccount = SELECTED_ACCOUNT; const info = await client.rpc.getAccountInfo(selectedAccount); - if (info.type !== "Configured") { + if (info.kind !== "Configured") { return write($main, "Account is not configured"); } write($main, `

${info.addr!}

`); @@ -93,9 +93,9 @@ async function run() { $side, `

- [${event.type} on account ${accountId}]
+ [${event.kind} on account ${accountId}]
f1: ${JSON.stringify( - Object.assign({}, event, { type: undefined }) + Object.assign({}, event, { kind: undefined }) )}

` ); diff --git a/deltachat-jsonrpc/typescript/src/client.ts b/deltachat-jsonrpc/typescript/src/client.ts index 1a51b5e046..1241fc3a8f 100644 --- a/deltachat-jsonrpc/typescript/src/client.ts +++ b/deltachat-jsonrpc/typescript/src/client.ts @@ -8,20 +8,20 @@ import { TinyEmitter } from "@deltachat/tiny-emitter"; type Events = { ALL: (accountId: number, event: EventType) => void } & { [Property in EventType["type"]]: ( accountId: number, - event: Extract + event: Extract ) => void; }; type ContextEvents = { ALL: (event: EventType) => void } & { [Property in EventType["type"]]: ( - event: Extract + event: Extract ) => void; }; export type DcEvent = EventType; -export type DcEventType = Extract< +export type DcEventType = Extract< EventType, - { type: T } + { kind: T } >; export class BaseDeltaChat< @@ -46,12 +46,12 @@ export class BaseDeltaChat< while (true) { const event = await this.rpc.getNextEvent(); //@ts-ignore - this.emit(event.event.type, event.contextId, event.event); + this.emit(event.event.kind, event.contextId, event.event); this.emit("ALL", event.contextId, event.event); if (this.contextEmitters[event.contextId]) { this.contextEmitters[event.contextId].emit( - event.event.type, + event.event.kind, //@ts-ignore event.event as any ); diff --git a/deltachat-jsonrpc/typescript/test/online.ts b/deltachat-jsonrpc/typescript/test/online.ts index 3ab6cb820a..889b1e22e7 100644 --- a/deltachat-jsonrpc/typescript/test/online.ts +++ b/deltachat-jsonrpc/typescript/test/online.ts @@ -29,8 +29,8 @@ describe("online tests", function () { serverHandle = await startServer(); dc = new DeltaChat(serverHandle.stdin, serverHandle.stdout, true); - dc.on("ALL", (contextId, { type }) => { - if (type !== "Info") console.log(contextId, type); + dc.on("ALL", (contextId, { kind }) => { + if (kind !== "Info") console.log(contextId, kind); }); account1 = await createTempUser(process.env.DCC_NEW_TMP_EMAIL); @@ -177,12 +177,12 @@ describe("online tests", function () { }); }); -async function waitForEvent( +async function waitForEvent( dc: DeltaChat, eventType: T, accountId: number, timeout: number = EVENT_TIMEOUT -): Promise> { +): Promise> { return new Promise((resolve, reject) => { const rejectTimeout = setTimeout( () => reject(new Error("Timeout reached before event came in")), diff --git a/deltachat-rpc-client/tests/test_something.py b/deltachat-rpc-client/tests/test_something.py index 77fdb0d3b4..b28d2822e7 100644 --- a/deltachat-rpc-client/tests/test_something.py +++ b/deltachat-rpc-client/tests/test_something.py @@ -45,7 +45,7 @@ async def test_acfactory(acfactory) -> None: account = await acfactory.new_configured_account() while True: event = await account.wait_for_event() - if event.type == EventType.CONFIGURE_PROGRESS: + if event.kind == EventType.CONFIGURE_PROGRESS: assert event.progress != 0 # Progress 0 indicates error. if event.progress == 1000: # Success break @@ -76,7 +76,7 @@ async def test_account(acfactory) -> None: while True: event = await bob.wait_for_event() - if event.type == EventType.INCOMING_MSG: + if event.kind == EventType.INCOMING_MSG: chat_id = event.chat_id msg_id = event.msg_id break @@ -146,7 +146,7 @@ async def test_chat(acfactory) -> None: while True: event = await bob.wait_for_event() - if event.type == EventType.INCOMING_MSG: + if event.kind == EventType.INCOMING_MSG: chat_id = event.chat_id msg_id = event.msg_id break @@ -232,7 +232,7 @@ async def test_message(acfactory) -> None: while True: event = await bob.wait_for_event() - if event.type == EventType.INCOMING_MSG: + if event.kind == EventType.INCOMING_MSG: chat_id = event.chat_id msg_id = event.msg_id break @@ -272,7 +272,7 @@ async def test_is_bot(acfactory) -> None: while True: event = await bob.wait_for_event() - if event.type == EventType.INCOMING_MSG: + if event.kind == EventType.INCOMING_MSG: msg_id = event.msg_id message = bob.get_message_by_id(msg_id) snapshot = await message.get_snapshot() diff --git a/deltachat-rpc-client/tests/test_webxdc.py b/deltachat-rpc-client/tests/test_webxdc.py index 8a0584d035..4111ca39e6 100644 --- a/deltachat-rpc-client/tests/test_webxdc.py +++ b/deltachat-rpc-client/tests/test_webxdc.py @@ -13,7 +13,7 @@ async def test_webxdc(acfactory) -> None: while True: event = await bob.wait_for_event() - if event.type == EventType.INCOMING_MSG: + if event.kind == EventType.INCOMING_MSG: bob_chat_alice = bob.get_chat_by_id(event.chat_id) message = bob.get_message_by_id(event.msg_id) break