Skip to content

Commit 7abf1c9

Browse files
committed
ui: user can upload images
Signed-off-by: kernelkind <[email protected]>
1 parent 1091bd0 commit 7abf1c9

File tree

2 files changed

+307
-35
lines changed

2 files changed

+307
-35
lines changed

crates/notedeck_columns/src/post.rs

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ use enostr::FullKeypair;
22
use nostrdb::{Note, NoteBuilder, NoteReply};
33
use std::collections::HashSet;
44

5+
use crate::media_upload::Nip94Event;
6+
57
pub struct NewPost {
68
pub content: String,
79
pub account: FullKeypair,
10+
pub media: Vec<Nip94Event>,
811
}
912

1013
fn add_client_tag(builder: NoteBuilder<'_>) -> NoteBuilder<'_> {
@@ -15,26 +18,36 @@ fn add_client_tag(builder: NoteBuilder<'_>) -> NoteBuilder<'_> {
1518
}
1619

1720
impl NewPost {
18-
pub fn new(content: String, account: FullKeypair) -> Self {
19-
NewPost { content, account }
21+
pub fn new(content: String, account: FullKeypair, media: Vec<Nip94Event>) -> Self {
22+
NewPost {
23+
content,
24+
account,
25+
media,
26+
}
2027
}
2128

2229
pub fn to_note(&self, seckey: &[u8; 32]) -> Note {
23-
let mut builder = add_client_tag(NoteBuilder::new())
24-
.kind(1)
25-
.content(&self.content);
30+
let mut content = self.content.clone();
31+
append_urls(&mut content, &self.media);
32+
33+
let mut builder = add_client_tag(NoteBuilder::new()).kind(1).content(&content);
2634

2735
for hashtag in Self::extract_hashtags(&self.content) {
2836
builder = builder.start_tag().tag_str("t").tag_str(&hashtag);
2937
}
3038

39+
if !self.media.is_empty() {
40+
builder = add_imeta_tags(builder, &self.media);
41+
}
42+
3143
builder.sign(seckey).build().expect("note should be ok")
3244
}
3345

3446
pub fn to_reply(&self, seckey: &[u8; 32], replying_to: &Note) -> Note {
35-
let builder = add_client_tag(NoteBuilder::new())
36-
.kind(1)
37-
.content(&self.content);
47+
let mut content = self.content.clone();
48+
append_urls(&mut content, &self.media);
49+
50+
let builder = add_client_tag(NoteBuilder::new()).kind(1).content(&content);
3851

3952
let nip10 = NoteReply::new(replying_to.tags());
4053

@@ -96,25 +109,35 @@ impl NewPost {
96109
builder = builder.start_tag().tag_str("p").tag_str(&hex::encode(id));
97110
}
98111

112+
if !self.media.is_empty() {
113+
builder = add_imeta_tags(builder, &self.media);
114+
}
115+
99116
builder
100117
.sign(seckey)
101118
.build()
102119
.expect("expected build to work")
103120
}
104121

105122
pub fn to_quote(&self, seckey: &[u8; 32], quoting: &Note) -> Note {
106-
let new_content = format!(
123+
let mut new_content = format!(
107124
"{}\nnostr:{}",
108125
self.content,
109126
enostr::NoteId::new(*quoting.id()).to_bech().unwrap()
110127
);
111128

129+
append_urls(&mut new_content, &self.media);
130+
112131
let mut builder = NoteBuilder::new().kind(1).content(&new_content);
113132

114133
for hashtag in Self::extract_hashtags(&self.content) {
115134
builder = builder.start_tag().tag_str("t").tag_str(&hashtag);
116135
}
117136

137+
if !self.media.is_empty() {
138+
builder = add_imeta_tags(builder, &self.media);
139+
}
140+
118141
builder
119142
.start_tag()
120143
.tag_str("q")
@@ -143,6 +166,43 @@ impl NewPost {
143166
}
144167
}
145168

169+
fn append_urls(content: &mut String, media: &Vec<Nip94Event>) {
170+
for ev in media {
171+
content.push(' ');
172+
content.push_str(&ev.url);
173+
}
174+
}
175+
176+
fn add_imeta_tags<'a>(builder: NoteBuilder<'a>, media: &Vec<Nip94Event>) -> NoteBuilder<'a> {
177+
let mut builder = builder;
178+
for item in media {
179+
builder = builder
180+
.start_tag()
181+
.tag_str("imeta")
182+
.tag_str(&format!("url {}", item.url));
183+
184+
if let Some(ox) = &item.ox {
185+
builder = builder.tag_str(&format!("ox {ox}"));
186+
};
187+
if let Some(x) = &item.x {
188+
builder = builder.tag_str(&format!("x {x}"));
189+
}
190+
if let Some(media_type) = &item.media_type {
191+
builder = builder.tag_str(&format!("m {media_type}"));
192+
}
193+
if let Some(dims) = &item.dimensions {
194+
builder = builder.tag_str(&format!("dim {}x{}", dims.0, dims.1));
195+
}
196+
if let Some(bh) = &item.blurhash {
197+
builder = builder.tag_str(&format!("blurhash {bh}"));
198+
}
199+
if let Some(thumb) = &item.thumb {
200+
builder = builder.tag_str(&format!("thumb {thumb}"));
201+
}
202+
}
203+
builder
204+
}
205+
146206
#[cfg(test)]
147207
mod tests {
148208
use super::*;

0 commit comments

Comments
 (0)