Skip to content

Commit a3328ea

Browse files
authored
api!(jsonrpc): chat_type now contains a variant of a string enum/union. Affected places: FullChat.chat_type, BasicChat.chat_type, ChatListItemFetchResult::ChatListItem.chat_type, Event:: SecurejoinInviterProgress.chat_type and MessageSearchResult.chat_type (#7285)
Actually it will be not as breaking if you used the constants, because this pr also changes the constants. closes #7029 Note that I had to change the constants from enum to namespace, this has the side effect, that you can no longer also use the constants as types, you need to instead prefix them with `typeof ` now.
1 parent ee75094 commit a3328ea

File tree

6 files changed

+73
-24
lines changed

6 files changed

+73
-24
lines changed

deltachat-jsonrpc/src/api/types/chat.rs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use deltachat::chat::{Chat, ChatId};
66
use deltachat::constants::Chattype;
77
use deltachat::contact::{Contact, ContactId};
88
use deltachat::context::Context;
9-
use num_traits::cast::ToPrimitive;
109
use serde::{Deserialize, Serialize};
1110
use typescript_type_def::TypeDef;
1211

@@ -46,7 +45,7 @@ pub struct FullChat {
4645
archived: bool,
4746
pinned: bool,
4847
// subtitle - will be moved to frontend because it uses translation functions
49-
chat_type: u32,
48+
chat_type: JsonrpcChatType,
5049
is_unpromoted: bool,
5150
is_self_talk: bool,
5251
contacts: Vec<ContactObject>,
@@ -130,7 +129,7 @@ impl FullChat {
130129
profile_image, //BLOBS ?
131130
archived: chat.get_visibility() == chat::ChatVisibility::Archived,
132131
pinned: chat.get_visibility() == chat::ChatVisibility::Pinned,
133-
chat_type: chat.get_type().to_u32().context("unknown chat type id")?,
132+
chat_type: chat.get_type().into(),
134133
is_unpromoted: chat.is_unpromoted(),
135134
is_self_talk: chat.is_self_talk(),
136135
contacts,
@@ -192,7 +191,7 @@ pub struct BasicChat {
192191
profile_image: Option<String>, //BLOBS ?
193192
archived: bool,
194193
pinned: bool,
195-
chat_type: u32,
194+
chat_type: JsonrpcChatType,
196195
is_unpromoted: bool,
197196
is_self_talk: bool,
198197
color: String,
@@ -220,7 +219,7 @@ impl BasicChat {
220219
profile_image, //BLOBS ?
221220
archived: chat.get_visibility() == chat::ChatVisibility::Archived,
222221
pinned: chat.get_visibility() == chat::ChatVisibility::Pinned,
223-
chat_type: chat.get_type().to_u32().context("unknown chat type id")?,
222+
chat_type: chat.get_type().into(),
224223
is_unpromoted: chat.is_unpromoted(),
225224
is_self_talk: chat.is_self_talk(),
226225
color,
@@ -274,3 +273,37 @@ impl JsonrpcChatVisibility {
274273
}
275274
}
276275
}
276+
277+
#[derive(Clone, Serialize, Deserialize, PartialEq, TypeDef, schemars::JsonSchema)]
278+
#[serde(rename = "ChatType")]
279+
pub enum JsonrpcChatType {
280+
Single,
281+
Group,
282+
Mailinglist,
283+
OutBroadcast,
284+
InBroadcast,
285+
}
286+
287+
impl From<Chattype> for JsonrpcChatType {
288+
fn from(chattype: Chattype) -> Self {
289+
match chattype {
290+
Chattype::Single => JsonrpcChatType::Single,
291+
Chattype::Group => JsonrpcChatType::Group,
292+
Chattype::Mailinglist => JsonrpcChatType::Mailinglist,
293+
Chattype::OutBroadcast => JsonrpcChatType::OutBroadcast,
294+
Chattype::InBroadcast => JsonrpcChatType::InBroadcast,
295+
}
296+
}
297+
}
298+
299+
impl From<JsonrpcChatType> for Chattype {
300+
fn from(chattype: JsonrpcChatType) -> Self {
301+
match chattype {
302+
JsonrpcChatType::Single => Chattype::Single,
303+
JsonrpcChatType::Group => Chattype::Group,
304+
JsonrpcChatType::Mailinglist => Chattype::Mailinglist,
305+
JsonrpcChatType::OutBroadcast => Chattype::OutBroadcast,
306+
JsonrpcChatType::InBroadcast => Chattype::InBroadcast,
307+
}
308+
}
309+
}

deltachat-jsonrpc/src/api/types/chat_list.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use num_traits::cast::ToPrimitive;
1111
use serde::Serialize;
1212
use typescript_type_def::TypeDef;
1313

14+
use super::chat::JsonrpcChatType;
1415
use super::color_int_to_hex_string;
1516
use super::message::MessageViewtype;
1617

@@ -23,7 +24,7 @@ pub enum ChatListItemFetchResult {
2324
name: String,
2425
avatar_path: Option<String>,
2526
color: String,
26-
chat_type: u32,
27+
chat_type: JsonrpcChatType,
2728
last_updated: Option<i64>,
2829
summary_text1: String,
2930
summary_text2: String,
@@ -151,7 +152,7 @@ pub(crate) async fn get_chat_list_item_by_id(
151152
name: chat.get_name().to_owned(),
152153
avatar_path,
153154
color,
154-
chat_type: chat.get_type().to_u32().context("unknown chat type id")?,
155+
chat_type: chat.get_type().into(),
155156
last_updated,
156157
summary_text1,
157158
summary_text2,

deltachat-jsonrpc/src/api/types/events.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use deltachat::{Event as CoreEvent, EventType as CoreEventType};
2-
use num_traits::ToPrimitive;
32
use serde::Serialize;
43
use typescript_type_def::TypeDef;
54

5+
use super::chat::JsonrpcChatType;
6+
67
#[derive(Serialize, TypeDef, schemars::JsonSchema)]
78
#[serde(rename_all = "camelCase")]
89
pub struct Event {
@@ -307,7 +308,7 @@ pub enum EventType {
307308
/// The type of the joined chat.
308309
/// This can take the same values
309310
/// as `BasicChat.chatType` ([`crate::api::types::chat::BasicChat::chat_type`]).
310-
chat_type: u32,
311+
chat_type: JsonrpcChatType,
311312
/// ID of the chat in case of success.
312313
chat_id: u32,
313314

@@ -570,7 +571,7 @@ impl From<CoreEventType> for EventType {
570571
progress,
571572
} => SecurejoinInviterProgress {
572573
contact_id: contact_id.to_u32(),
573-
chat_type: chat_type.to_u32().unwrap_or(0),
574+
chat_type: chat_type.into(),
574575
chat_id: chat_id.to_u32(),
575576
progress,
576577
},

deltachat-jsonrpc/src/api/types/message.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use num_traits::cast::ToPrimitive;
1616
use serde::{Deserialize, Serialize};
1717
use typescript_type_def::TypeDef;
1818

19+
use super::chat::JsonrpcChatType;
1920
use super::color_int_to_hex_string;
2021
use super::contact::ContactObject;
2122
use super::reactions::JsonrpcReactions;
@@ -531,7 +532,7 @@ pub struct MessageSearchResult {
531532
chat_profile_image: Option<String>,
532533
chat_color: String,
533534
chat_name: String,
534-
chat_type: u32,
535+
chat_type: JsonrpcChatType,
535536
is_chat_contact_request: bool,
536537
is_chat_archived: bool,
537538
message: String,
@@ -569,7 +570,7 @@ impl MessageSearchResult {
569570
chat_id: chat.id.to_u32(),
570571
chat_name: chat.get_name().to_owned(),
571572
chat_color,
572-
chat_type: chat.get_type().to_u32().context("unknown chat type id")?,
573+
chat_type: chat.get_type().into(),
573574
chat_profile_image,
574575
is_chat_contact_request: chat.is_contact_request(),
575576
is_chat_archived: chat.get_visibility() == ChatVisibility::Archived,

deltachat-jsonrpc/typescript/scripts/generate-constants.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,30 @@ const constants = data
4545
key.startsWith("DC_SOCKET_") ||
4646
key.startsWith("DC_LP_AUTH_") ||
4747
key.startsWith("DC_PUSH_") ||
48-
key.startsWith("DC_TEXT1_")
48+
key.startsWith("DC_TEXT1_") ||
49+
key.startsWith("DC_CHAT_TYPE")
4950
);
5051
})
5152
.map((row) => {
52-
return ` ${row.key}: ${row.value}`;
53+
return ` export const ${row.key} = ${row.value};`;
5354
})
54-
.join(",\n");
55+
.join("\n");
5556

5657
writeFileSync(
5758
resolve(__dirname, "../generated/constants.ts"),
58-
`// Generated!\n\nexport enum C {\n${constants.replace(/:/g, " =")},\n}\n`,
59+
`// Generated!
60+
61+
export namespace C {
62+
${constants}
63+
/** @deprecated 10-8-2025 compare string directly with \`== "Group"\` */
64+
export const DC_CHAT_TYPE_GROUP = "Group";
65+
/** @deprecated 10-8-2025 compare string directly with \`== "InBroadcast"\`*/
66+
export const DC_CHAT_TYPE_IN_BROADCAST = "InBroadcast";
67+
/** @deprecated 10-8-2025 compare string directly with \`== "Mailinglist"\` */
68+
export const DC_CHAT_TYPE_MAILINGLIST = "Mailinglist";
69+
/** @deprecated 10-8-2025 compare string directly with \`== "OutBroadcast"\` */
70+
export const DC_CHAT_TYPE_OUT_BROADCAST = "OutBroadcast";
71+
/** @deprecated 10-8-2025 compare string directly with \`== "Single"\` */
72+
export const DC_CHAT_TYPE_SINGLE = "Single";
73+
}\n`,
5974
);

deltachat-rpc-client/src/deltachat_rpc_client/const.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,17 @@ class ChatId(IntEnum):
9191
LAST_SPECIAL = 9
9292

9393

94-
class ChatType(IntEnum):
94+
class ChatType(str, Enum):
9595
"""Chat type."""
9696

97-
UNDEFINED = 0
98-
99-
SINGLE = 100
97+
SINGLE = "Single"
10098
"""1:1 chat, i.e. a direct chat with a single contact"""
10199

102-
GROUP = 120
100+
GROUP = "Group"
103101

104-
MAILINGLIST = 140
102+
MAILINGLIST = "Mailinglist"
105103

106-
OUT_BROADCAST = 160
104+
OUT_BROADCAST = "OutBroadcast"
107105
"""Outgoing broadcast channel, called "Channel" in the UI.
108106
109107
The user can send into this channel,
@@ -115,7 +113,7 @@ class ChatType(IntEnum):
115113
which would make it hard to grep for it.
116114
"""
117115

118-
IN_BROADCAST = 165
116+
IN_BROADCAST = "InBroadcast"
119117
"""Incoming broadcast channel, called "Channel" in the UI.
120118
121119
This channel is read-only,

0 commit comments

Comments
 (0)