Skip to content

Commit

Permalink
Add url & opengraph card previews
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed Jan 22, 2025
1 parent 4bf0c5d commit 467e247
Show file tree
Hide file tree
Showing 25 changed files with 1,324 additions and 254 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ itertools = "0.13.0"
rodio = "0.19.0"
strum = { version = "0.26.3", features = ["derive"] }
tokio-stream = { version = "0.1.16", features = ["fs"] }
url = "2.5.0"

# change to 1.2.0 when it is released https://github.com/frewsxcv/rust-dark-light/issues/38
dark-light = { git = "https://github.com/frewsxcv/rust-dark-light", rev = "3eb3e93dd0fa30733c3e93082dd9517fb580ae95" }
Expand Down
1 change: 1 addition & 0 deletions data/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const_format = "0.2.32"
strum = { version = "0.26.3", features = ["derive"] }
derive_more = { version = "1.0.0", features = ["full"] }
anyhow = "1.0.91"
image = "0.24.9"

[dependencies.irc]
path = "../irc"
Expand Down
6 changes: 6 additions & 0 deletions data/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub fn data_dir() -> PathBuf {
})
}

pub fn cache_dir() -> PathBuf {
dirs_next::cache_dir()
.expect("expected valid cache dir")
.join("halloy")
}

/// Checks if a config file exists in the same directory as the executable.
/// If so, it'll use that directory for both config & data dirs.
fn portable_dir() -> Option<PathBuf> {
Expand Down
2 changes: 2 additions & 0 deletions data/src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ fn has_matching_content(message: &Message, other: &Message) -> bool {
#[derive(Debug)]
pub struct View<'a> {
pub total: usize,
pub has_more_older_messages: bool,
pub has_more_newer_messages: bool,
pub old_messages: Vec<&'a Message>,
pub new_messages: Vec<&'a Message>,
pub max_nick_chars: Option<usize>,
Expand Down
21 changes: 21 additions & 0 deletions data/src/history/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,14 @@ impl Data {
})
.unwrap_or_default();

let first_without_limit = filtered.first().copied();
let last_without_limit = filtered.last().copied();

let limited = with_limit(limit, filtered.into_iter());

let first_with_limit = limited.first();
let last_with_limit = limited.last();

let split_at = read_marker.map_or(0, |read_marker| {
limited
.iter()
Expand All @@ -674,8 +680,23 @@ impl Data {

let (old, new) = limited.split_at(split_at);

let has_more_older_messages =
first_without_limit
.zip(first_with_limit)
.is_some_and(|(without_limit, with_limit)| {
without_limit.server_time < with_limit.server_time
});
let has_more_newer_messages =
last_without_limit
.zip(last_with_limit)
.is_some_and(|(without_limit, with_limit)| {
without_limit.server_time > with_limit.server_time
});

Some(history::View {
total,
has_more_older_messages,
has_more_newer_messages,
old_messages: old.to_vec(),
new_messages: new.to_vec(),
max_nick_chars,
Expand Down
2 changes: 2 additions & 0 deletions data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use self::input::Input;
pub use self::message::Message;
pub use self::mode::Mode;
pub use self::pane::Pane;
pub use self::preview::Preview;
pub use self::server::Server;
pub use self::shortcut::Shortcut;
pub use self::url::Url;
Expand Down Expand Up @@ -36,6 +37,7 @@ pub mod log;
pub mod message;
pub mod mode;
pub mod pane;
pub mod preview;
pub mod server;
pub mod shortcut;
pub mod stream;
Expand Down
18 changes: 17 additions & 1 deletion data/src/message.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::collections::HashSet;
use std::hash::{DefaultHasher, Hash as _, Hasher};
use std::iter;

Expand Down Expand Up @@ -167,6 +168,7 @@ pub struct Message {
pub content: Content,
pub id: Option<String>,
pub hash: Hash,
pub hidden_urls: HashSet<Url>,
}

impl Message {
Expand Down Expand Up @@ -255,6 +257,7 @@ impl Message {
content,
id,
hash,
hidden_urls: HashSet::default(),
})
}

Expand All @@ -270,6 +273,7 @@ impl Message {
content,
id: None,
hash,
hidden_urls: HashSet::default(),
}
}

Expand All @@ -293,6 +297,7 @@ impl Message {
content,
id: None,
hash,
hidden_urls: HashSet::default(),
}
}

Expand All @@ -312,6 +317,7 @@ impl Message {
content,
id: None,
hash,
hidden_urls: HashSet::default(),
}
}

Expand Down Expand Up @@ -341,6 +347,7 @@ impl Message {
content,
id: None,
hash,
hidden_urls: HashSet::default(),
}
}

Expand Down Expand Up @@ -449,11 +456,12 @@ impl<'de> Deserialize<'de> for Message {
content,
id,
hash,
hidden_urls: HashSet::default(),
})
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Hash(u64);

impl Hash {
Expand Down Expand Up @@ -652,6 +660,14 @@ pub enum Fragment {
}

impl Fragment {
pub fn url(&self) -> Option<&Url> {
if let Self::Url(url) = self {
Some(url)
} else {
None
}
}

pub fn as_str(&self) -> &str {
match self {
Fragment::Text(s) => s,
Expand Down
3 changes: 3 additions & 0 deletions data/src/message/broadcast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Generate messages that can be broadcast into every buffer
use std::collections::HashSet;

use chrono::{DateTime, Utc};

use super::{parse_fragments, plain, source, Content, Direction, Message, Source, Target};
Expand Down Expand Up @@ -32,6 +34,7 @@ fn expand(
content,
id: None,
hash,
hidden_urls: HashSet::default(),
}
};

Expand Down
Loading

0 comments on commit 467e247

Please sign in to comment.