From 90331c7403c55458fe9e1a40343a3b2d16b6aef4 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 | 10 +++++----- deltachat-jsonrpc/typescript/src/client.ts | 16 ++++++++-------- deltachat-jsonrpc/typescript/test/online.ts | 8 ++++---- .../examples/echobot_advanced.py | 4 ++-- .../src/deltachat_rpc_client/client.py | 4 ++-- .../src/deltachat_rpc_client/events.py | 2 +- .../src/deltachat_rpc_client/pytestplugin.py | 4 ++-- deltachat-rpc-client/tests/test_something.py | 10 +++++----- deltachat-rpc-client/tests/test_webxdc.py | 2 +- 15 files changed, 36 insertions(+), 35 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..e03347b86d 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!}

`); @@ -81,7 +81,7 @@ async function run() { messageIds ); for (const [_messageId, message] of Object.entries(messages)) { - if (message.variant === "message") + if (message.kind === "message") write($main, `

${message.text}

`); else write($main, `

loading error: ${message.error}

`); } @@ -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..83cc2f7e71 100644 --- a/deltachat-jsonrpc/typescript/src/client.ts +++ b/deltachat-jsonrpc/typescript/src/client.ts @@ -6,22 +6,22 @@ import { WebsocketTransport, BaseTransport, Request } from "yerpc"; import { TinyEmitter } from "@deltachat/tiny-emitter"; type Events = { ALL: (accountId: number, event: EventType) => void } & { - [Property in EventType["type"]]: ( + [Property in EventType["kind"]]: ( accountId: number, - event: Extract + event: Extract ) => void; }; type ContextEvents = { ALL: (event: EventType) => void } & { - [Property in EventType["type"]]: ( - event: Extract + [Property in EventType["kind"]]: ( + 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/examples/echobot_advanced.py b/deltachat-rpc-client/examples/echobot_advanced.py index 3030941c85..4fcbc52f26 100644 --- a/deltachat-rpc-client/examples/echobot_advanced.py +++ b/deltachat-rpc-client/examples/echobot_advanced.py @@ -14,9 +14,9 @@ @hooks.on(events.RawEvent) async def log_event(event): - if event.type == EventType.INFO: + if event.kind == EventType.INFO: logging.info(event.msg) - elif event.type == EventType.WARNING: + elif event.kind == EventType.WARNING: logging.warning(event.msg) diff --git a/deltachat-rpc-client/src/deltachat_rpc_client/client.py b/deltachat-rpc-client/src/deltachat_rpc_client/client.py index 5205a1ed98..c270fb0f7b 100644 --- a/deltachat-rpc-client/src/deltachat_rpc_client/client.py +++ b/deltachat-rpc-client/src/deltachat_rpc_client/client.py @@ -106,10 +106,10 @@ async def run_until(self, func: Callable[[AttrDict], Union[bool, Coroutine]]) -> await self._process_messages() # Process old messages. while True: event = await self.account.wait_for_event() - event["type"] = EventType(event.type) + event["kind"] = EventType(event.kind) event["account"] = self.account await self._on_event(event) - if event.type == EventType.INCOMING_MSG: + if event.kind == EventType.INCOMING_MSG: await self._process_messages() stop = func(event) diff --git a/deltachat-rpc-client/src/deltachat_rpc_client/events.py b/deltachat-rpc-client/src/deltachat_rpc_client/events.py index 4896527b9a..2c1f71369d 100644 --- a/deltachat-rpc-client/src/deltachat_rpc_client/events.py +++ b/deltachat-rpc-client/src/deltachat_rpc_client/events.py @@ -83,7 +83,7 @@ def __eq__(self, other) -> bool: return False async def filter(self, event: "AttrDict") -> bool: - if self.types and event.type not in self.types: + if self.types and event.kind not in self.types: return False return await self._call_func(event) diff --git a/deltachat-rpc-client/src/deltachat_rpc_client/pytestplugin.py b/deltachat-rpc-client/src/deltachat_rpc_client/pytestplugin.py index 9b207ecfa9..8087327a65 100644 --- a/deltachat-rpc-client/src/deltachat_rpc_client/pytestplugin.py +++ b/deltachat-rpc-client/src/deltachat_rpc_client/pytestplugin.py @@ -57,7 +57,7 @@ async def get_online_account(self) -> Account: while True: event = await account.wait_for_event() print(event) - if event.type == EventType.IMAP_INBOX_IDLE: + if event.kind == EventType.IMAP_INBOX_IDLE: break return account @@ -98,7 +98,7 @@ async def process_message( group=group, ) - return await to_client.run_until(lambda e: e.type == EventType.INCOMING_MSG) + return await to_client.run_until(lambda e: e.kind == EventType.INCOMING_MSG) @pytest_asyncio.fixture 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