Skip to content

Commit

Permalink
Rebase fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
andymandias committed Oct 30, 2024
1 parent d98a7c1 commit 3de0295
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 92 deletions.
20 changes: 18 additions & 2 deletions data/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use std::{fmt, io};
use tokio::fs;

use crate::history::ReadMarker;
use crate::isupport::{ChatHistorySubcommand, MessageReference};
use crate::isupport::{ChatHistoryState, ChatHistorySubcommand, MessageReference};
use crate::message::{message_id, server_time, source};
use crate::time::Posix;
use crate::user::{Nick, NickRef};
use crate::{
buffer, compression, config, ctcp, dcc, environment, isupport, message, mode, Buffer, Server, User,
buffer, compression, config, ctcp, dcc, environment, isupport, message, mode, Server, User,
};
use crate::{file_transfer, server};

Expand Down Expand Up @@ -2090,6 +2090,22 @@ impl Map {
.unwrap_or_default()
}

pub fn get_chathistory_state(&self, server: &Server, target: &str) -> Option<ChatHistoryState> {
self.client(server).and_then(|client| {
if client.supports_chathistory {
if client.chathistory_request(target).is_some() {
Some(ChatHistoryState::PendingRequest)
} else if client.chathistory_exhausted(target) {
Some(ChatHistoryState::Exhausted)
} else {
Some(ChatHistoryState::Ready)
}
} else {
None
}
})
}

pub fn get_server_handle(&self, server: &Server) -> Option<&server::Handle> {
self.client(server).map(|client| &client.handle)
}
Expand Down
42 changes: 30 additions & 12 deletions data/src/history/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,25 +244,18 @@ impl Manager {
self.data.load_metadata(server, channel)
}

pub fn first_can_reference(&self, server: &Server, target: &str) -> Option<&crate::Message> {
self.data
.map
.get(server)
.and_then(|map| map.get(&history::Kind::from(target)))
.map(|history| history.first_can_reference())?
pub fn first_can_reference(&self, server: Server, target: String) -> Option<&crate::Message> {
self.data.first_can_reference(server, target)
}

pub fn last_can_reference_before(
&self,
server: &Server,
target: &str,
server: Server,
target: String,
server_time: DateTime<Utc>,
) -> Option<MessageReferences> {
self.data
.map
.get(server)
.and_then(|map| map.get(&history::Kind::from(target)))
.map(|history| history.last_can_reference_before(server_time))?
.last_can_reference_before(server, target, server_time)
}

pub fn get_messages(
Expand Down Expand Up @@ -762,6 +755,31 @@ impl Data {
}
}

fn first_can_reference(
&self,
server: server::Server,
target: String,
) -> Option<&crate::Message> {
let kind = history::Kind::from_target(server, target);

self.map
.get(&kind)
.and_then(|history| history.first_can_reference())
}

fn last_can_reference_before(
&self,
server: Server,
target: String,
server_time: DateTime<Utc>,
) -> Option<MessageReferences> {
let kind = history::Kind::from_target(server, target);

self.map
.get(&kind)
.and_then(|history| history.last_can_reference_before(server_time))
}

fn untrack(
&mut self,
kind: &history::Kind,
Expand Down
3 changes: 2 additions & 1 deletion data/src/history/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use serde::{Deserialize, Serialize};
use tokio::fs;

use crate::history::{dir_path, Error, Kind};
use crate::{isupport, message, server, Message};
use crate::message::source;
use crate::{isupport, Message};

#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct Metadata {
Expand Down
7 changes: 7 additions & 0 deletions data/src/isupport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,13 @@ impl ChatHistorySubcommand {
}
}

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ChatHistoryState {
Exhausted,
PendingRequest,
Ready,
}

#[derive(Clone, Debug)]
pub enum ClientOnlyTags {
Allowed(String),
Expand Down
8 changes: 7 additions & 1 deletion data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ fn parse_user_and_channel_fragments(text: &str, channel_users: &[User]) -> Vec<F
})
}

#[derive(Debug, Clone, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, Serialize, Deserialize)]
pub enum Content {
Plain(String),
Fragments(Vec<Fragment>),
Expand All @@ -602,6 +602,12 @@ impl PartialEq for Content {
}
}

impl std::hash::Hash for Content {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.text().hash(state);
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Fragment {
Text(String),
Expand Down
11 changes: 7 additions & 4 deletions src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub use data::buffer::{Internal, Settings, Upstream};
use data::isupport::ChatHistoryState;
use data::user::Nick;
use data::{buffer, file_transfer, history, message, Config};
use iced::Task;
Expand Down Expand Up @@ -329,14 +330,15 @@ impl Buffer {
message: message::Hash,
history: &history::Manager,
config: &Config,
chathistory_state: Option<ChatHistoryState>,
) -> Task<Message> {
match self {
Buffer::Empty | Buffer::FileTransfers(_) => Task::none(),
Buffer::Channel(state) => state
.scroll_view
.scroll_to_message(
message,
scroll_view::Kind::Channel(&state.server, &state.channel),
scroll_view::Kind::Channel(&state.server, &state.channel, chathistory_state),
history,
config,
)
Expand All @@ -354,7 +356,7 @@ impl Buffer {
.scroll_view
.scroll_to_message(
message,
scroll_view::Kind::Query(&state.server, &state.nick),
scroll_view::Kind::Query(&state.server, &state.nick, chathistory_state),
history,
config,
)
Expand All @@ -374,13 +376,14 @@ impl Buffer {
&mut self,
history: &history::Manager,
config: &Config,
chathistory_state: Option<ChatHistoryState>,
) -> Task<Message> {
match self {
Buffer::Empty | Buffer::FileTransfers(_) => Task::none(),
Buffer::Channel(state) => state
.scroll_view
.scroll_to_backlog(
scroll_view::Kind::Channel(&state.server, &state.channel),
scroll_view::Kind::Channel(&state.server, &state.channel, chathistory_state),
history,
config,
)
Expand All @@ -392,7 +395,7 @@ impl Buffer {
Buffer::Query(state) => state
.scroll_view
.scroll_to_backlog(
scroll_view::Kind::Query(&state.server, &state.nick),
scroll_view::Kind::Query(&state.server, &state.nick, chathistory_state),
history,
config,
)
Expand Down
17 changes: 5 additions & 12 deletions src/buffer/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,12 @@ pub fn view<'a>(

let users = clients.get_channel_users(&state.server, &state.channel);

let chathistory_before_button = if clients.get_server_supports_chathistory(&state.server) {
Some((
clients
.get_chathistory_request(&state.server, &state.channel)
.is_some(),
clients.get_chathistory_exhausted(&state.server, &state.channel),
))
} else {
None
};
let chathistory_state = clients.get_chathistory_state(server, channel);

let messages = container(
scroll_view::view(
&state.scroll_view,
scroll_view::Kind::Channel(&state.server, &state.channel, chathistory_before_button),
scroll_view::Kind::Channel(&state.server, &state.channel, chathistory_state),
history,
config,
move |message, max_nick_width, max_prefix_width| {
Expand Down Expand Up @@ -361,7 +352,9 @@ impl Channel {
scroll_view::Event::UserContext(event) => Some(Event::UserContext(event)),
scroll_view::Event::OpenChannel(channel) => Some(Event::OpenChannel(channel)),
scroll_view::Event::GoToMessage(..) => None,
scroll_view::Event::RequestOlderChatHistory => Event::RequestOlderChatHistory,
scroll_view::Event::RequestOlderChatHistory => {
Some(Event::RequestOlderChatHistory)
}
});

(command.map(Message::ScrollView), event)
Expand Down
11 changes: 6 additions & 5 deletions src/buffer/highlights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,15 @@ impl Highlights {
pub fn update(&mut self, message: Message) -> (Task<Message>, Option<Event>) {
match message {
Message::ScrollView(message) => {
let (command, event) = self.scroll_view.update(message);
let (command, event) = self.scroll_view.update(message, false);

let event = event.map(|event| match event {
scroll_view::Event::UserContext(event) => Event::UserContext(event),
scroll_view::Event::OpenChannel(channel) => Event::OpenChannel(channel),
let event = event.and_then(|event| match event {
scroll_view::Event::UserContext(event) => Some(Event::UserContext(event)),
scroll_view::Event::OpenChannel(channel) => Some(Event::OpenChannel(channel)),
scroll_view::Event::GoToMessage(server, channel, message) => {
Event::GoToMessage(server, channel, message)
Some(Event::GoToMessage(server, channel, message))
}
scroll_view::Event::RequestOlderChatHistory => None,
});

(command.map(Message::ScrollView), event)
Expand Down
17 changes: 5 additions & 12 deletions src/buffer/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,12 @@ pub fn view<'a>(
let buffer = &state.buffer;
let input = history.input(buffer);

let chathistory_before_button = if clients.get_server_supports_chathistory(&state.server) {
Some((
clients
.get_chathistory_request(&state.server, state.nick.as_ref())
.is_some(),
clients.get_chathistory_exhausted(&state.server, state.nick.as_ref()),
))
} else {
None
};
let chathistory_state = clients.get_chathistory_state(server, state.nick.as_ref());

let messages = container(
scroll_view::view(
&state.scroll_view,
scroll_view::Kind::Query(server, &state.nick, chathistory_before_button),
scroll_view::Kind::Query(server, &state.nick, chathistory_state),
history,
config,
move |message, max_nick_width, _| {
Expand Down Expand Up @@ -262,7 +253,9 @@ impl Query {
scroll_view::Event::UserContext(event) => Some(Event::UserContext(event)),
scroll_view::Event::OpenChannel(channel) => Some(Event::OpenChannel(channel)),
scroll_view::Event::GoToMessage(_, _, _) => None,
scroll_view::Event::RequestOlderChatHistory => Event::RequestOlderChatHistory,
scroll_view::Event::RequestOlderChatHistory => {
Some(Event::RequestOlderChatHistory)
}
});

(command.map(Message::ScrollView), event)
Expand Down
62 changes: 30 additions & 32 deletions src/buffer/scroll_view.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use data::isupport::ChatHistoryState;
use data::message::{self, Limit};
use data::server::Server;
use data::user::Nick;
Expand Down Expand Up @@ -38,8 +39,8 @@ pub enum Event {
#[derive(Debug, Clone, Copy)]
pub enum Kind<'a> {
Server(&'a Server),
Channel(&'a Server, &'a str, Option<(bool, bool)>),
Query(&'a Server, &'a Nick, Option<(bool, bool)>),
Channel(&'a Server, &'a str, Option<ChatHistoryState>),
Query(&'a Server, &'a Nick, Option<ChatHistoryState>),
Logs,
Highlights,
}
Expand All @@ -48,10 +49,10 @@ impl From<Kind<'_>> for history::Kind {
fn from(value: Kind<'_>) -> Self {
match value {
Kind::Server(server) => history::Kind::Server(server.clone()),
Kind::Channel(server, channel) => {
Kind::Channel(server, channel, _) => {
history::Kind::Channel(server.clone(), channel.to_string())
}
Kind::Query(server, nick) => history::Kind::Query(server.clone(), nick.clone()),
Kind::Query(server, nick, _) => history::Kind::Query(server.clone(), nick.clone()),
Kind::Logs => history::Kind::Logs,
Kind::Highlights => history::Kind::Highlights,
}
Expand All @@ -76,37 +77,34 @@ pub fn view<'a>(
return column![].into();
};

let top_row =
if let Kind::Channel(_, _, Some((chathistory_request_is_some, chathistory_exhausted)))
| Kind::Query(_, _, Some((chathistory_request_is_some, chathistory_exhausted))) = kind
{
let (content, message) = if chathistory_request_is_some {
("...", None)
} else if chathistory_exhausted {
("No Older Chat History Messages Available", None)
} else {
(
"Request Older Chat History Messages",
Some(Message::RequestOlderChatHistory),
)
};
let top_row = if let Kind::Channel(_, _, Some(chathistory_state))
| Kind::Query(_, _, Some(chathistory_state)) = kind
{
let (content, message) = match chathistory_state {
ChatHistoryState::Exhausted => ("No Older Chat History Messages Available", None),
ChatHistoryState::PendingRequest => ("...", None),
ChatHistoryState::Ready => (
"Request Older Chat History Messages",
Some(Message::RequestOlderChatHistory),
),
};

let font_size = config.font.size.map(f32::from).unwrap_or(theme::TEXT_SIZE) - 1.0;
let font_size = config.font.size.map(f32::from).unwrap_or(theme::TEXT_SIZE) - 1.0;

let top_row_button = button(text(content).size(font_size))
.padding([3, 5])
.style(|theme, status| theme::button::primary(theme, status, false))
.on_press_maybe(message);
let top_row_button = button(text(content).size(font_size))
.padding([3, 5])
.style(|theme, status| theme::button::primary(theme, status, false))
.on_press_maybe(message);

Some(
row![horizontal_space(), top_row_button, horizontal_space()]
.padding(padding::top(2).bottom(6))
.width(Length::Fill)
.align_y(iced::Alignment::Center),
)
} else {
None
};
Some(
row![horizontal_space(), top_row_button, horizontal_space()]
.padding(padding::top(2).bottom(6))
.width(Length::Fill)
.align_y(iced::Alignment::Center),
)
} else {
None
};

let count = old_messages.len() + new_messages.len();
let remaining = count < total;
Expand Down
Loading

0 comments on commit 3de0295

Please sign in to comment.