change now returns event names as id

directly, no conversion method or number ids anymore

also longer timeout for requesting test accounts from mailadm
This commit is contained in:
Simon Laux
2022-06-01 19:01:13 +02:00
parent 8357b3a98c
commit 564d283852
8 changed files with 117 additions and 80 deletions

View File

@@ -1,5 +1,7 @@
use deltachat::{Event, EventType};
use serde::Serialize;
use serde_json::{json, Value};
use typescript_type_def::TypeDef;
pub fn event_to_json_rpc_notification(event: Event) -> Value {
let (field1, field2): (Value, Value) = match &event.typ {
@@ -61,9 +63,92 @@ pub fn event_to_json_rpc_notification(event: Event) -> Value {
};
json!({
"id": event.typ.as_id(),
"id": event_type_to_string(event.typ),
"contextId": event.id,
"field1": field1,
"field2": field2
})
}
#[derive(Serialize, TypeDef)]
pub enum EventTypeName {
Info,
SmtpConnected,
ImapConnected,
SmtpMessageSent,
ImapMessageDeleted,
ImapMessageMoved,
NewBlobFile,
DeletedBlobFile,
Warning,
Error,
ErrorSelfNotInGroup,
MsgsChanged,
IncomingMsg,
MsgsNoticed,
MsgDelivered,
MsgFailed,
MsgRead,
ChatModified,
ChatEphemeralTimerModified,
ContactsChanged,
LocationChanged,
ConfigureProgress,
ImexProgress,
ImexFileWritten,
SecurejoinInviterProgress,
SecurejoinJoinerProgress,
ConnectivityChanged,
SelfavatarChanged,
WebxdcStatusUpdate,
}
fn event_type_to_string(event: EventType) -> EventTypeName {
use EventTypeName::*;
match event {
EventType::Info(_) => Info,
EventType::SmtpConnected(_) => SmtpConnected,
EventType::ImapConnected(_) => ImapConnected,
EventType::SmtpMessageSent(_) => SmtpMessageSent,
EventType::ImapMessageDeleted(_) => ImapMessageDeleted,
EventType::ImapMessageMoved(_) => ImapMessageMoved,
EventType::NewBlobFile(_) => NewBlobFile,
EventType::DeletedBlobFile(_) => DeletedBlobFile,
EventType::Warning(_) => Warning,
EventType::Error(_) => Error,
EventType::ErrorSelfNotInGroup(_) => ErrorSelfNotInGroup,
EventType::MsgsChanged { .. } => MsgsChanged,
EventType::IncomingMsg { .. } => IncomingMsg,
EventType::MsgsNoticed(_) => MsgsNoticed,
EventType::MsgDelivered { .. } => MsgDelivered,
EventType::MsgFailed { .. } => MsgFailed,
EventType::MsgRead { .. } => MsgRead,
EventType::ChatModified(_) => ChatModified,
EventType::ChatEphemeralTimerModified { .. } => ChatEphemeralTimerModified,
EventType::ContactsChanged(_) => ContactsChanged,
EventType::LocationChanged(_) => LocationChanged,
EventType::ConfigureProgress { .. } => ConfigureProgress,
EventType::ImexProgress(_) => ImexProgress,
EventType::ImexFileWritten(_) => ImexFileWritten,
EventType::SecurejoinInviterProgress { .. } => SecurejoinInviterProgress,
EventType::SecurejoinJoinerProgress { .. } => SecurejoinJoinerProgress,
EventType::ConnectivityChanged => ConnectivityChanged,
EventType::SelfavatarChanged => SelfavatarChanged,
EventType::WebxdcStatusUpdate { .. } => WebxdcStatusUpdate,
}
}
#[cfg(test)]
#[test]
fn generate_events_ts_types_definition() {
let events = {
let mut buf = Vec::new();
let options = typescript_type_def::DefinitionFileOptions {
root_namespace: None,
..typescript_type_def::DefinitionFileOptions::default()
};
typescript_type_def::write_definition_file::<_, EventTypeName>(&mut buf, options).unwrap();
String::from_utf8(buf).unwrap()
};
std::fs::write("typescript/generated/events.ts", events).unwrap();
}

View File

@@ -319,7 +319,7 @@ impl CommandApi {
flags: u32,
) -> Result<Vec<u32>> {
let ctx = self.get_context(account_id).await?;
let msg = get_chat_msgs(&ctx, ChatId::new(chat_id), flags, None).await?;
let msg = get_chat_msgs(&ctx, ChatId::new(chat_id), flags).await?;
Ok(msg
.iter()
.filter_map(|chat_item| match chat_item {

View File

@@ -1,8 +1,7 @@
import { RawClient, RPC } from "./src/lib";
import { eventIdToName } from "./src/events";
import { WebsocketTransport, Request } from "yerpc";
type DeltaEvent = { id: number; contextId: number; field1: any; field2: any };
type DeltaEvent = { id: string; contextId: number; field1: any; field2: any };
var selectedAccount = 0;
window.addEventListener("DOMContentLoaded", (_event) => {
(window as any).selectDeltaAccount = (id: string) => {
@@ -26,8 +25,7 @@ async function run() {
const method = request.method;
if (method === "event") {
const params = request.params! as DeltaEvent;
const name = eventIdToName(params.id);
onIncomingEvent(params, name);
onIncomingEvent(params, params.id);
}
});

View File

@@ -0,0 +1,3 @@
// AUTO-GENERATED by typescript-type-def
export type EventTypeName=("Info"|"SmtpConnected"|"ImapConnected"|"SmtpMessageSent"|"ImapMessageDeleted"|"ImapMessageMoved"|"NewBlobFile"|"DeletedBlobFile"|"Warning"|"Error"|"ErrorSelfNotInGroup"|"MsgsChanged"|"IncomingMsg"|"MsgsNoticed"|"MsgDelivered"|"MsgFailed"|"MsgRead"|"ChatModified"|"ChatEphemeralTimerModified"|"ContactsChanged"|"LocationChanged"|"ConfigureProgress"|"ImexProgress"|"ImexFileWritten"|"SecurejoinInviterProgress"|"SecurejoinJoinerProgress"|"ConnectivityChanged"|"SelfavatarChanged"|"WebxdcStatusUpdate");

View File

@@ -1,19 +1,20 @@
import * as T from "../generated/types.js";
import * as RPC from "../generated/jsonrpc.js";
import { RawClient } from "../generated/client.js";
import { EventTypeName } from "../generated/events.js";
import { WebsocketTransport, BaseTransport, Request } from "yerpc";
import { eventIdToName } from "./events.js";
import { TinyEmitter } from "tiny-emitter";
export type EventNames = ReturnType<typeof eventIdToName> | "ALL";
export type WireEvent = {
id: number;
export type DeltachatEvent = {
id: EventTypeName;
contextId: number;
field1: any;
field2: any;
};
export type DeltachatEvent = WireEvent & { name: EventNames };
export type Events = Record<EventNames, (event: DeltachatEvent) => void>;
export type Events = Record<
EventTypeName | "ALL",
(event: DeltachatEvent) => void
>;
export class BaseDeltachat<
Transport extends BaseTransport
@@ -26,15 +27,13 @@ export class BaseDeltachat<
this.transport.on("request", (request: Request) => {
const method = request.method;
if (method === "event") {
const params = request.params! as WireEvent;
const name = eventIdToName(params.id);
const event = { name, ...params };
this.emit(name, event);
const event = request.params! as DeltachatEvent;
this.emit(event.id, event);
this.emit("ALL", event);
if (this.contextEmitters[params.contextId]) {
this.contextEmitters[params.contextId].emit(name, event);
this.contextEmitters[params.contextId].emit("ALL", event);
if (this.contextEmitters[event.contextId]) {
this.contextEmitters[event.contextId].emit(event.id, event);
this.contextEmitters[event.contextId].emit("ALL", event);
}
}
});

View File

@@ -1,44 +0,0 @@
// Manual update might be required from time to time as this is NOT generated
export enum Event_TypeID {
INFO = 100,
SMTP_CONNECTED = 101,
IMAP_CONNECTED = 102,
SMTP_MESSAGE_SENT = 103,
IMAP_MESSAGE_DELETED = 104,
IMAP_MESSAGE_MOVED = 105,
NEW_BLOB_FILE = 150,
DELETED_BLOB_FILE = 151,
WARNING = 300,
ERROR = 400,
ERROR_NETWORK = 401,
ERROR_SELF_NOT_IN_GROUP = 410,
MSGS_CHANGED = 2000,
INCOMING_MSG = 2005,
MSGS_NOTICED = 2008,
MSG_DELIVERED = 2010,
MSG_FAILED = 2012,
MSG_READ = 2015,
CHAT_MODIFIED = 2020,
CHAT_EPHEMERAL_TIMER_MODIFIED = 2021,
CONTACTS_CHANGED = 2030,
LOCATION_CHANGED = 2035,
CONFIGURE_PROGRESS = 2041,
IMEX_PROGRESS = 2051,
IMEX_FILE_WRITTEN = 2052,
SECUREJOIN_INVITER_PROGRESS = 2060,
SECUREJOIN_JOINER_PROGRESS = 2061,
CONNECTIVITY_CHANGED = 2100,
}
export function eventIdToName(
event_id: number
): keyof typeof Event_TypeID | "UNKNOWN_EVENT" {
const name = Event_TypeID[event_id];
if (name) {
return name as keyof typeof Event_TypeID;
} else {
console.error("Unknown Event id:", event_id);
return "UNKNOWN_EVENT";
}
}

View File

@@ -1,6 +1,6 @@
export * as RPC from "../generated/jsonrpc.js";
export * as T from "../generated/types.js";
export * from "../generated/events.js";
export { RawClient } from "../generated/client.js";
export * from "./client.js";
export * as yerpc from "yerpc";
export * from "./events.js";

View File

@@ -1,10 +1,5 @@
import { assert, expect } from "chai";
import {
Deltachat,
DeltachatEvent,
eventIdToName,
Event_TypeID,
} from "../dist/deltachat.js";
import { Deltachat, DeltachatEvent, EventTypeName } from "../dist/deltachat.js";
import {
CMD_API_Server_Handle,
CMD_API_SERVER_PORT,
@@ -20,6 +15,7 @@ describe("online tests", function () {
let acc1: number, acc2: number;
before(async function () {
this.timeout(12000)
if (!process.env.DCC_NEW_TMP_EMAIL) {
if (process.env.COVERAGE && !process.env.COVERAGE_OFFLINE) {
console.error(
@@ -38,8 +34,8 @@ describe("online tests", function () {
url: "ws://localhost:" + CMD_API_SERVER_PORT + "/ws",
});
dc.on("ALL", ({ name, contextId }) => {
if (name !== "INFO") console.log(contextId, name);
dc.on("ALL", ({ id, contextId }) => {
if (id !== "Info") console.log(contextId, id);
});
account = await createTempUser(process.env.DCC_NEW_TMP_EMAIL);
@@ -97,8 +93,8 @@ describe("online tests", function () {
);
const chatId = await dc.rpc.contactsCreateChatByContactId(acc1, contactId);
const eventPromise = Promise.race([
waitForEvent(dc, "MSGS_CHANGED", acc2),
waitForEvent(dc, "INCOMING_MSG", acc2),
waitForEvent(dc, "MsgsChanged", acc2),
waitForEvent(dc, "IncomingMsg", acc2),
]);
dc.rpc.miscSendTextMessage(acc1, "Hello", chatId);
@@ -129,8 +125,8 @@ describe("online tests", function () {
);
const chatId = await dc.rpc.contactsCreateChatByContactId(acc1, contactId);
const eventPromise = Promise.race([
waitForEvent(dc, "MSGS_CHANGED", acc2),
waitForEvent(dc, "INCOMING_MSG", acc2),
waitForEvent(dc, "MsgsChanged", acc2),
waitForEvent(dc, "IncomingMsg", acc2),
]);
dc.rpc.miscSendTextMessage(acc1, "Hello2", chatId);
// wait for message from A
@@ -152,8 +148,8 @@ describe("online tests", function () {
expect(message.text).equal("Hello2");
// Send message back from B to A
const eventPromise2 = Promise.race([
waitForEvent(dc, "MSGS_CHANGED", acc1),
waitForEvent(dc, "INCOMING_MSG", acc1),
waitForEvent(dc, "MsgsChanged", acc1),
waitForEvent(dc, "IncomingMsg", acc1),
]);
dc.rpc.miscSendTextMessage(acc2, "super secret message", chatId);
// Check if answer arives at A and if it is encrypted
@@ -187,12 +183,12 @@ describe("online tests", function () {
type event_data = {
contextId: number;
id: Event_TypeID;
id: EventTypeName;
[key: string]: any;
};
async function waitForEvent(
dc: Deltachat,
event: ReturnType<typeof eventIdToName>,
event: EventTypeName,
accountId: number
): Promise<event_data> {
return new Promise((res, rej) => {