Skip to content

Commit 4785ba8

Browse files
committed
Add core/original-format tag
This allows plugins to determine what the best format would be for handling text directly. The original use case was for the seabird-url-plugin to be able to determine if the url blocks would be accurate enough to rely on, or if it should be parsing the text directly.
1 parent 3941ba6 commit 4785ba8

4 files changed

Lines changed: 63 additions & 25 deletions

File tree

proto

Submodule proto updated 1 file

src/server/grpc.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ impl Seabird for Arc<super::Server> {
9292
.map_err(|_| Status::invalid_argument("failed to parse channel_id"))?
9393
.into_inner();
9494

95-
let (text, root_block) = normalize_block(req.text, req.root_block)?;
95+
let (text, root_block, additional_tags) = normalize_block(req.text, req.root_block)?;
96+
97+
let mut tags = req.tags;
98+
tags.extend(additional_tags);
9699

97100
self.broadcast(
98101
EventInner::SendMessage(proto::SendMessageEvent {
@@ -101,7 +104,7 @@ impl Seabird for Arc<super::Server> {
101104
text: text.clone(),
102105
root_block: Some(root_block.clone()),
103106
}),
104-
req.tags.clone(),
107+
tags.clone(),
105108
);
106109

107110
let resp = self
@@ -111,7 +114,7 @@ impl Seabird for Arc<super::Server> {
111114
channel_id,
112115
text,
113116
root_block: Some(root_block),
114-
tags: req.tags,
117+
tags,
115118
}),
116119
)
117120
.await?;
@@ -135,7 +138,10 @@ impl Seabird for Arc<super::Server> {
135138
.map_err(|_| Status::invalid_argument("failed to parse user_id"))?
136139
.into_inner();
137140

138-
let (text, root_block) = normalize_block(req.text, req.root_block)?;
141+
let (text, root_block, additional_tags) = normalize_block(req.text, req.root_block)?;
142+
143+
let mut tags = req.tags;
144+
tags.extend(additional_tags);
139145

140146
self.broadcast(
141147
EventInner::SendPrivateMessage(proto::SendPrivateMessageEvent {
@@ -144,7 +150,7 @@ impl Seabird for Arc<super::Server> {
144150
text: text.clone(),
145151
root_block: Some(root_block.clone()),
146152
}),
147-
req.tags.clone(),
153+
tags.clone(),
148154
);
149155

150156
let resp = self
@@ -154,7 +160,7 @@ impl Seabird for Arc<super::Server> {
154160
user_id,
155161
text,
156162
root_block: Some(root_block),
157-
tags: req.tags,
163+
tags,
158164
}),
159165
)
160166
.await?;
@@ -178,7 +184,10 @@ impl Seabird for Arc<super::Server> {
178184
.map_err(|_| Status::invalid_argument("failed to parse channel_id"))?
179185
.into_inner();
180186

181-
let (text, root_block) = normalize_block(req.text, req.root_block)?;
187+
let (text, root_block, additional_tags) = normalize_block(req.text, req.root_block)?;
188+
189+
let mut tags = req.tags;
190+
tags.extend(additional_tags);
182191

183192
self.broadcast(
184193
EventInner::PerformAction(proto::PerformActionEvent {
@@ -187,7 +196,7 @@ impl Seabird for Arc<super::Server> {
187196
text: text.clone(),
188197
root_block: Some(root_block.clone()),
189198
}),
190-
req.tags.clone(),
199+
tags.clone(),
191200
);
192201

193202
let resp = self
@@ -197,7 +206,7 @@ impl Seabird for Arc<super::Server> {
197206
channel_id,
198207
text,
199208
root_block: Some(root_block),
200-
tags: req.tags,
209+
tags,
201210
}),
202211
)
203212
.await?;
@@ -221,7 +230,10 @@ impl Seabird for Arc<super::Server> {
221230
.map_err(|_| Status::invalid_argument("failed to parse user_id"))?
222231
.into_inner();
223232

224-
let (text, root_block) = normalize_block(req.text, req.root_block)?;
233+
let (text, root_block, additional_tags) = normalize_block(req.text, req.root_block)?;
234+
235+
let mut tags = req.tags;
236+
tags.extend(additional_tags);
225237

226238
self.broadcast(
227239
EventInner::PerformPrivateAction(proto::PerformPrivateActionEvent {
@@ -230,7 +242,7 @@ impl Seabird for Arc<super::Server> {
230242
text: text.clone(),
231243
root_block: Some(root_block.clone()),
232244
}),
233-
req.tags.clone(),
245+
tags.clone(),
234246
);
235247

236248
let resp = self
@@ -241,7 +253,7 @@ impl Seabird for Arc<super::Server> {
241253
user_id,
242254
text,
243255
root_block: Some(root_block),
244-
tags: req.tags,
256+
tags,
245257
},
246258
),
247259
)

src/server/ingest_events.rs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,27 @@ impl IngestEvents {
5656
ChatEventInner::Metadata(_) => {}
5757

5858
ChatEventInner::Action(action) => {
59-
let (text, root_block) = normalize_block(action.text, action.root_block)?;
59+
let (text, root_block, additional_tags) = normalize_block(action.text, action.root_block)?;
60+
61+
let mut tags = event.tags;
62+
tags.extend(additional_tags);
6063

6164
let _ = self.backend_handle.sender.send(proto::Event {
6265
inner: Some(EventInner::Action(proto::ActionEvent {
6366
source: action.source.map(|source| source.into_relative(&self.id)),
6467
text,
6568
root_block: Some(root_block),
6669
})),
67-
tags: event.tags,
70+
tags,
6871
});
6972
}
7073
ChatEventInner::PrivateAction(private_action) => {
71-
let (text, root_block) =
74+
let (text, root_block, additional_tags) =
7275
normalize_block(private_action.text, private_action.root_block)?;
7376

77+
let mut tags = event.tags;
78+
tags.extend(additional_tags);
79+
7480
let _ = self.backend_handle.sender.send(proto::Event {
7581
inner: Some(EventInner::PrivateAction(proto::PrivateActionEvent {
7682
source: private_action
@@ -79,31 +85,37 @@ impl IngestEvents {
7985
text,
8086
root_block: Some(root_block),
8187
})),
82-
tags: event.tags,
88+
tags,
8389
});
8490
}
8591
ChatEventInner::Message(msg) => {
86-
let (text, root_block) = normalize_block(msg.text, msg.root_block)?;
92+
let (text, root_block, additional_tags) = normalize_block(msg.text, msg.root_block)?;
93+
94+
let mut tags = event.tags;
95+
tags.extend(additional_tags);
8796

8897
let _ = self.backend_handle.sender.send(proto::Event {
8998
inner: Some(EventInner::Message(proto::MessageEvent {
9099
source: msg.source.map(|source| source.into_relative(&self.id)),
91100
text,
92101
root_block: Some(root_block),
93102
})),
94-
tags: event.tags,
103+
tags,
95104
});
96105
}
97106
ChatEventInner::PrivateMessage(private_msg) => {
98-
let (text, root_block) = normalize_block(private_msg.text, private_msg.root_block)?;
107+
let (text, root_block, additional_tags) = normalize_block(private_msg.text, private_msg.root_block)?;
108+
109+
let mut tags = event.tags;
110+
tags.extend(additional_tags);
99111

100112
let _ = self.backend_handle.sender.send(proto::Event {
101113
inner: Some(EventInner::PrivateMessage(proto::PrivateMessageEvent {
102114
source: private_msg.source.map(|user| user.into_relative(&self.id)),
103115
text,
104116
root_block: Some(root_block),
105117
})),
106-
tags: event.tags,
118+
tags,
107119
});
108120
}
109121
ChatEventInner::Command(cmd_msg) => {
@@ -117,7 +129,10 @@ impl IngestEvents {
117129
});
118130
}
119131
ChatEventInner::Mention(mention_msg) => {
120-
let (text, root_block) = normalize_block(mention_msg.text, mention_msg.root_block)?;
132+
let (text, root_block, additional_tags) = normalize_block(mention_msg.text, mention_msg.root_block)?;
133+
134+
let mut tags = event.tags;
135+
tags.extend(additional_tags);
121136

122137
let _ = self.backend_handle.sender.send(proto::Event {
123138
inner: Some(EventInner::Mention(proto::MentionEvent {
@@ -127,7 +142,7 @@ impl IngestEvents {
127142
text,
128143
root_block: Some(root_block),
129144
})),
130-
tags: event.tags,
145+
tags,
131146
});
132147
}
133148

src/utils.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
use itertools::Itertools;
2+
use std::collections::HashMap;
23
use tonic::Status;
34

45
use crate::error::RpcResult;
56
use crate::proto::{Block, BlockInner, TextBlock};
67

7-
pub fn normalize_block(text: String, block: Option<Block>) -> RpcResult<(String, Block)> {
8+
pub const ORIGINAL_FORMAT_TAG: &str = "core/original-format";
9+
10+
pub fn normalize_block(text: String, block: Option<Block>) -> RpcResult<(String, Block, HashMap<String, String>)> {
811
// There should never be a case where a new client submits no blocks, so if
912
// that's the case, this is probably from a client using the non-block-based
1013
// APIs and we need to add a Text block to normalize it.
14+
15+
// Detect original format before consuming the Option
16+
let original_format = if block.is_none() { "text" } else { "blocks" };
17+
1118
let mut block = block.unwrap_or_else(|| Block {
1219
plain: text.clone(),
1320
inner: Some(BlockInner::Text(TextBlock { text: text.clone() })),
@@ -16,7 +23,11 @@ pub fn normalize_block(text: String, block: Option<Block>) -> RpcResult<(String,
1623
normalize_block_inner(&mut block)?;
1724

1825
let text: String = block.plain.clone();
19-
Ok((text, block))
26+
27+
let mut additional_tags = HashMap::new();
28+
additional_tags.insert(ORIGINAL_FORMAT_TAG.to_string(), original_format.to_string());
29+
30+
Ok((text, block, additional_tags))
2031
}
2132

2233
fn normalize_block_inner(block: &mut Block) -> RpcResult<()> {

0 commit comments

Comments
 (0)