mirror of
https://github.com/chatmail/core.git
synced 2026-05-16 13:26:38 +03:00
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:
@@ -1,5 +1,7 @@
|
|||||||
use deltachat::{Event, EventType};
|
use deltachat::{Event, EventType};
|
||||||
|
use serde::Serialize;
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
|
use typescript_type_def::TypeDef;
|
||||||
|
|
||||||
pub fn event_to_json_rpc_notification(event: Event) -> Value {
|
pub fn event_to_json_rpc_notification(event: Event) -> Value {
|
||||||
let (field1, field2): (Value, Value) = match &event.typ {
|
let (field1, field2): (Value, Value) = match &event.typ {
|
||||||
@@ -61,9 +63,92 @@ pub fn event_to_json_rpc_notification(event: Event) -> Value {
|
|||||||
};
|
};
|
||||||
|
|
||||||
json!({
|
json!({
|
||||||
"id": event.typ.as_id(),
|
"id": event_type_to_string(event.typ),
|
||||||
"contextId": event.id,
|
"contextId": event.id,
|
||||||
"field1": field1,
|
"field1": field1,
|
||||||
"field2": field2
|
"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();
|
||||||
|
}
|
||||||
|
|||||||
@@ -319,7 +319,7 @@ impl CommandApi {
|
|||||||
flags: u32,
|
flags: u32,
|
||||||
) -> Result<Vec<u32>> {
|
) -> Result<Vec<u32>> {
|
||||||
let ctx = self.get_context(account_id).await?;
|
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
|
Ok(msg
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|chat_item| match chat_item {
|
.filter_map(|chat_item| match chat_item {
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import { RawClient, RPC } from "./src/lib";
|
import { RawClient, RPC } from "./src/lib";
|
||||||
import { eventIdToName } from "./src/events";
|
|
||||||
import { WebsocketTransport, Request } from "yerpc";
|
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;
|
var selectedAccount = 0;
|
||||||
window.addEventListener("DOMContentLoaded", (_event) => {
|
window.addEventListener("DOMContentLoaded", (_event) => {
|
||||||
(window as any).selectDeltaAccount = (id: string) => {
|
(window as any).selectDeltaAccount = (id: string) => {
|
||||||
@@ -26,8 +25,7 @@ async function run() {
|
|||||||
const method = request.method;
|
const method = request.method;
|
||||||
if (method === "event") {
|
if (method === "event") {
|
||||||
const params = request.params! as DeltaEvent;
|
const params = request.params! as DeltaEvent;
|
||||||
const name = eventIdToName(params.id);
|
onIncomingEvent(params, params.id);
|
||||||
onIncomingEvent(params, name);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
3
deltachat-jsonrpc/typescript/generated/events.ts
Normal file
3
deltachat-jsonrpc/typescript/generated/events.ts
Normal 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");
|
||||||
@@ -1,19 +1,20 @@
|
|||||||
import * as T from "../generated/types.js";
|
import * as T from "../generated/types.js";
|
||||||
import * as RPC from "../generated/jsonrpc.js";
|
import * as RPC from "../generated/jsonrpc.js";
|
||||||
import { RawClient } from "../generated/client.js";
|
import { RawClient } from "../generated/client.js";
|
||||||
|
import { EventTypeName } from "../generated/events.js";
|
||||||
import { WebsocketTransport, BaseTransport, Request } from "yerpc";
|
import { WebsocketTransport, BaseTransport, Request } from "yerpc";
|
||||||
import { eventIdToName } from "./events.js";
|
|
||||||
import { TinyEmitter } from "tiny-emitter";
|
import { TinyEmitter } from "tiny-emitter";
|
||||||
|
|
||||||
export type EventNames = ReturnType<typeof eventIdToName> | "ALL";
|
export type DeltachatEvent = {
|
||||||
export type WireEvent = {
|
id: EventTypeName;
|
||||||
id: number;
|
|
||||||
contextId: number;
|
contextId: number;
|
||||||
field1: any;
|
field1: any;
|
||||||
field2: any;
|
field2: any;
|
||||||
};
|
};
|
||||||
export type DeltachatEvent = WireEvent & { name: EventNames };
|
export type Events = Record<
|
||||||
export type Events = Record<EventNames, (event: DeltachatEvent) => void>;
|
EventTypeName | "ALL",
|
||||||
|
(event: DeltachatEvent) => void
|
||||||
|
>;
|
||||||
|
|
||||||
export class BaseDeltachat<
|
export class BaseDeltachat<
|
||||||
Transport extends BaseTransport
|
Transport extends BaseTransport
|
||||||
@@ -26,15 +27,13 @@ export class BaseDeltachat<
|
|||||||
this.transport.on("request", (request: Request) => {
|
this.transport.on("request", (request: Request) => {
|
||||||
const method = request.method;
|
const method = request.method;
|
||||||
if (method === "event") {
|
if (method === "event") {
|
||||||
const params = request.params! as WireEvent;
|
const event = request.params! as DeltachatEvent;
|
||||||
const name = eventIdToName(params.id);
|
this.emit(event.id, event);
|
||||||
const event = { name, ...params };
|
|
||||||
this.emit(name, event);
|
|
||||||
this.emit("ALL", event);
|
this.emit("ALL", event);
|
||||||
|
|
||||||
if (this.contextEmitters[params.contextId]) {
|
if (this.contextEmitters[event.contextId]) {
|
||||||
this.contextEmitters[params.contextId].emit(name, event);
|
this.contextEmitters[event.contextId].emit(event.id, event);
|
||||||
this.contextEmitters[params.contextId].emit("ALL", event);
|
this.contextEmitters[event.contextId].emit("ALL", event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
export * as RPC from "../generated/jsonrpc.js";
|
export * as RPC from "../generated/jsonrpc.js";
|
||||||
export * as T from "../generated/types.js";
|
export * as T from "../generated/types.js";
|
||||||
|
export * from "../generated/events.js";
|
||||||
export { RawClient } from "../generated/client.js";
|
export { RawClient } from "../generated/client.js";
|
||||||
export * from "./client.js";
|
export * from "./client.js";
|
||||||
export * as yerpc from "yerpc";
|
export * as yerpc from "yerpc";
|
||||||
export * from "./events.js";
|
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
import { assert, expect } from "chai";
|
import { assert, expect } from "chai";
|
||||||
import {
|
import { Deltachat, DeltachatEvent, EventTypeName } from "../dist/deltachat.js";
|
||||||
Deltachat,
|
|
||||||
DeltachatEvent,
|
|
||||||
eventIdToName,
|
|
||||||
Event_TypeID,
|
|
||||||
} from "../dist/deltachat.js";
|
|
||||||
import {
|
import {
|
||||||
CMD_API_Server_Handle,
|
CMD_API_Server_Handle,
|
||||||
CMD_API_SERVER_PORT,
|
CMD_API_SERVER_PORT,
|
||||||
@@ -20,6 +15,7 @@ describe("online tests", function () {
|
|||||||
let acc1: number, acc2: number;
|
let acc1: number, acc2: number;
|
||||||
|
|
||||||
before(async function () {
|
before(async function () {
|
||||||
|
this.timeout(12000)
|
||||||
if (!process.env.DCC_NEW_TMP_EMAIL) {
|
if (!process.env.DCC_NEW_TMP_EMAIL) {
|
||||||
if (process.env.COVERAGE && !process.env.COVERAGE_OFFLINE) {
|
if (process.env.COVERAGE && !process.env.COVERAGE_OFFLINE) {
|
||||||
console.error(
|
console.error(
|
||||||
@@ -38,8 +34,8 @@ describe("online tests", function () {
|
|||||||
url: "ws://localhost:" + CMD_API_SERVER_PORT + "/ws",
|
url: "ws://localhost:" + CMD_API_SERVER_PORT + "/ws",
|
||||||
});
|
});
|
||||||
|
|
||||||
dc.on("ALL", ({ name, contextId }) => {
|
dc.on("ALL", ({ id, contextId }) => {
|
||||||
if (name !== "INFO") console.log(contextId, name);
|
if (id !== "Info") console.log(contextId, id);
|
||||||
});
|
});
|
||||||
|
|
||||||
account = await createTempUser(process.env.DCC_NEW_TMP_EMAIL);
|
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 chatId = await dc.rpc.contactsCreateChatByContactId(acc1, contactId);
|
||||||
const eventPromise = Promise.race([
|
const eventPromise = Promise.race([
|
||||||
waitForEvent(dc, "MSGS_CHANGED", acc2),
|
waitForEvent(dc, "MsgsChanged", acc2),
|
||||||
waitForEvent(dc, "INCOMING_MSG", acc2),
|
waitForEvent(dc, "IncomingMsg", acc2),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
dc.rpc.miscSendTextMessage(acc1, "Hello", chatId);
|
dc.rpc.miscSendTextMessage(acc1, "Hello", chatId);
|
||||||
@@ -129,8 +125,8 @@ describe("online tests", function () {
|
|||||||
);
|
);
|
||||||
const chatId = await dc.rpc.contactsCreateChatByContactId(acc1, contactId);
|
const chatId = await dc.rpc.contactsCreateChatByContactId(acc1, contactId);
|
||||||
const eventPromise = Promise.race([
|
const eventPromise = Promise.race([
|
||||||
waitForEvent(dc, "MSGS_CHANGED", acc2),
|
waitForEvent(dc, "MsgsChanged", acc2),
|
||||||
waitForEvent(dc, "INCOMING_MSG", acc2),
|
waitForEvent(dc, "IncomingMsg", acc2),
|
||||||
]);
|
]);
|
||||||
dc.rpc.miscSendTextMessage(acc1, "Hello2", chatId);
|
dc.rpc.miscSendTextMessage(acc1, "Hello2", chatId);
|
||||||
// wait for message from A
|
// wait for message from A
|
||||||
@@ -152,8 +148,8 @@ describe("online tests", function () {
|
|||||||
expect(message.text).equal("Hello2");
|
expect(message.text).equal("Hello2");
|
||||||
// Send message back from B to A
|
// Send message back from B to A
|
||||||
const eventPromise2 = Promise.race([
|
const eventPromise2 = Promise.race([
|
||||||
waitForEvent(dc, "MSGS_CHANGED", acc1),
|
waitForEvent(dc, "MsgsChanged", acc1),
|
||||||
waitForEvent(dc, "INCOMING_MSG", acc1),
|
waitForEvent(dc, "IncomingMsg", acc1),
|
||||||
]);
|
]);
|
||||||
dc.rpc.miscSendTextMessage(acc2, "super secret message", chatId);
|
dc.rpc.miscSendTextMessage(acc2, "super secret message", chatId);
|
||||||
// Check if answer arives at A and if it is encrypted
|
// Check if answer arives at A and if it is encrypted
|
||||||
@@ -187,12 +183,12 @@ describe("online tests", function () {
|
|||||||
|
|
||||||
type event_data = {
|
type event_data = {
|
||||||
contextId: number;
|
contextId: number;
|
||||||
id: Event_TypeID;
|
id: EventTypeName;
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
};
|
};
|
||||||
async function waitForEvent(
|
async function waitForEvent(
|
||||||
dc: Deltachat,
|
dc: Deltachat,
|
||||||
event: ReturnType<typeof eventIdToName>,
|
event: EventTypeName,
|
||||||
accountId: number
|
accountId: number
|
||||||
): Promise<event_data> {
|
): Promise<event_data> {
|
||||||
return new Promise((res, rej) => {
|
return new Promise((res, rej) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user