Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Topic Banner #237

Merged
merged 17 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

Added:

- Configuration option to enable a topic banner in channels. This can be enabled under `buffer.channel.topic`

Fix:

- Context menus now shows buttons as expected
Expand All @@ -22,6 +26,7 @@ Fixed:
- Clipped buttons in context menu

Changed:

- Improved user experience in text input when auto-completing a nickname.
- Configuration option `server_messages` changed `exclude` from a boolean value to [`All`, `None` or `!Smart seconds`].
- `All` excludes all messages for the specific server message.
Expand Down
9 changes: 8 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,14 @@ buffer:
# - Unique: Unique user colors [default]
# - Solid: Solid user colors
color: Unique

# Topic banner settings:
topic:
# Visible by default
# - Default is false
visible: true
# Maximum visible lines of the topic banner before scrolling
# - Default is 2
max_lines: 3

# Dashboard settings
dashboard:
Expand Down
21 changes: 21 additions & 0 deletions data/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use crate::config;
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct Settings {
pub users: Users,
pub topic: Topic,
}

impl From<config::Channel> for Settings {
fn from(config: config::Channel) -> Self {
Self {
users: Users::from(config.users),
topic: Topic::from(config.topic),
}
}
}
Expand Down Expand Up @@ -46,3 +48,22 @@ impl Users {
self.visible = !self.visible
}
}

#[derive(Debug, Clone, Copy, Default, Deserialize, Serialize)]
pub struct Topic {
pub visible: bool,
}

impl From<config::channel::Topic> for Topic {
fn from(config: config::channel::Topic) -> Self {
Topic {
visible: config.visible,
}
}
}

impl Topic {
pub fn toggle_visibility(&mut self) {
self.visible = !self.visible
}
}
56 changes: 53 additions & 3 deletions data/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use chrono::{DateTime, Utc};
use futures::channel::mpsc;
use irc::proto::{self, command, Command};
use itertools::Itertools;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt;
use std::time::{Duration, Instant};

use crate::message::server_time;
use crate::time::Posix;
use crate::user::{Nick, NickRef};
use crate::{config, message, mode, Buffer, Server, User};
Expand Down Expand Up @@ -638,9 +640,39 @@ impl Client {
}
}
}
#[cfg(feature = "dev")]
// Suppress topic during development to prevent history spam
Command::Numeric(RPL_TOPIC | RPL_TOPICWHOTIME, _) => return None,
Command::TOPIC(channel, topic) => {
if let Some(channel) = self.chanmap.get_mut(channel) {
channel.topic.text = topic.to_owned();

channel.topic.who = message
.user()
.map(|user| user.username().unwrap().to_string());
channel.topic.time = Some(server_time(&message));
}
}
Command::Numeric(RPL_TOPIC, args) => {
if let Some(channel) = self.chanmap.get_mut(&args[1]) {
channel.topic.text = Some(args.get(2)?.to_owned());
}
// Exclude topic message from history to prevent spam during dev
#[cfg(feature = "dev")]
return None;
}
Command::Numeric(RPL_TOPICWHOTIME, args) => {
if let Some(channel) = self.chanmap.get_mut(&args[1]) {
channel.topic.who = Some(args.get(2)?.to_string());
channel.topic.time = Some(
args.get(3)?
.parse::<u64>()
.ok()
.map(Posix::from_seconds)?
.datetime()?,
);
}
// Exclude topic message from history to prevent spam during dev
#[cfg(feature = "dev")]
return None;
}
_ => {}
}

Expand All @@ -665,6 +697,10 @@ impl Client {
&self.channels
}

fn topic<'a>(&'a self, channel: &str) -> Option<&'a Topic> {
self.chanmap.get(channel).map(|channel| &channel.topic)
}

fn users<'a>(&'a self, channel: &str) -> &'a [User] {
self.users
.get(channel)
Expand Down Expand Up @@ -823,6 +859,12 @@ impl Map {
.unwrap_or_default()
}

pub fn get_channel_topic<'a>(&'a self, server: &Server, channel: &str) -> Option<&'a Topic> {
self.client(server)
.map(|client| client.topic(channel))
.unwrap_or_default()
}

pub fn get_channels<'a>(&'a self, server: &Server) -> &'a [String] {
self.client(server)
.map(|client| client.channels())
Expand Down Expand Up @@ -950,6 +992,14 @@ enum RegistrationStep {
pub struct Channel {
pub users: HashSet<User>,
pub last_who: Option<WhoStatus>,
pub topic: Topic,
}

#[derive(Default, Debug, Clone)]
pub struct Topic {
pub text: Option<String>,
pub who: Option<String>,
pub time: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, Copy)]
Expand Down
11 changes: 7 additions & 4 deletions data/src/config/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub enum Exclude {

#[derive(Debug, Clone, Default, Deserialize)]
pub struct ServerMessages {
#[serde(default)]
pub topic: ServerMessage,
#[serde(default)]
pub join: ServerMessage,
#[serde(default)]
Expand All @@ -40,11 +42,12 @@ pub struct ServerMessages {
}

impl ServerMessages {
pub fn get(&self, server: &source::Server) -> ServerMessage {
pub fn get(&self, server: &source::Server) -> Option<ServerMessage> {
match server.kind() {
source::server::Kind::Join => self.join,
source::server::Kind::Part => self.part,
source::server::Kind::Quit => self.quit,
source::server::Kind::ReplyTopic => Some(self.topic),
source::server::Kind::Part => Some(self.part),
source::server::Kind::Quit => Some(self.quit),
source::server::Kind::Join => Some(self.join),
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions data/src/config/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::channel::Position;
#[derive(Debug, Clone, Default, Deserialize)]
pub struct Channel {
pub users: Users,
pub topic: Topic,
}

#[derive(Debug, Clone, Copy, Deserialize)]
pub struct Users {
pub(crate) visible: bool,
Expand All @@ -25,3 +27,15 @@ impl Default for Users {
}
}
}

#[derive(Debug, Clone, Copy, Default, Deserialize)]
pub struct Topic {
#[serde(default)]
pub(crate) visible: bool,
#[serde(default = "default_topic_banner_max_lines")]
pub max_lines: u16,
}

fn default_topic_banner_max_lines() -> u16 {
2
}
67 changes: 35 additions & 32 deletions data/src/history/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use futures::{future, Future, FutureExt};
use itertools::Itertools;
use tokio::time::Instant;

use crate::config::buffer::{Exclude, ServerMessages};
use crate::config;
use crate::config::buffer::Exclude;
use crate::history::{self, History};
use crate::message::{self, Limit};
use crate::time::Posix;
Expand Down Expand Up @@ -189,38 +190,38 @@ impl Manager {
server: &Server,
channel: &str,
limit: Option<Limit>,
server_messages: &ServerMessages,
buffer_config: &config::Buffer,
) -> Option<history::View<'_>> {
self.data.history_view(
server,
&history::Kind::Channel(channel.to_string()),
limit,
server_messages,
buffer_config,
)
}

pub fn get_server_messages(
&self,
server: &Server,
limit: Option<Limit>,
server_messages: &ServerMessages,
buffer_config: &config::Buffer,
) -> Option<history::View<'_>> {
self.data
.history_view(server, &history::Kind::Server, limit, server_messages)
.history_view(server, &history::Kind::Server, limit, buffer_config)
}

pub fn get_query_messages(
&self,
server: &Server,
nick: &Nick,
limit: Option<Limit>,
server_messages: &ServerMessages,
buffer_config: &config::Buffer,
) -> Option<history::View<'_>> {
self.data.history_view(
server,
&history::Kind::Query(nick.clone()),
limit,
server_messages,
buffer_config,
)
}

Expand Down Expand Up @@ -427,7 +428,7 @@ impl Data {
server: &server::Server,
kind: &history::Kind,
limit: Option<Limit>,
server_messages: &ServerMessages,
buffer_config: &config::Buffer,
) -> Option<history::View> {
let History::Full {
messages,
Expand All @@ -444,32 +445,34 @@ impl Data {
.iter()
.filter(|message| match message.target.source() {
message::Source::Server(Some(source)) => {
let source_config = server_messages.get(source);

match source_config.exclude {
Exclude::All => false,
Exclude::None => true,
Exclude::Smart(seconds) => {
if let Some(nick) = source.nick() {
!smart_filter_message(
message,
&seconds,
most_recent_messages.get(nick),
)
} else if let Some(nickname) =
message.text.split(' ').collect::<Vec<_>>().get(1)
{
let nick = Nick::from(*nickname);

!smart_filter_message(
message,
&seconds,
most_recent_messages.get(&nick),
)
} else {
true
if let Some(source_config) = buffer_config.server_messages.get(source) {
match source_config.exclude {
Exclude::All => false,
Exclude::None => true,
Exclude::Smart(seconds) => {
if let Some(nick) = source.nick() {
!smart_filter_message(
message,
&seconds,
most_recent_messages.get(nick),
)
} else if let Some(nickname) =
message.text.split(' ').collect::<Vec<_>>().get(1)
{
let nick = Nick::from(*nickname);

!smart_filter_message(
message,
&seconds,
most_recent_messages.get(&nick),
)
} else {
true
}
}
}
} else {
true
}
}
crate::message::Source::User(message_user) => {
Expand Down
14 changes: 12 additions & 2 deletions data/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,17 @@ fn target(message: Encoded, our_nick: &Nick) -> Option<Target> {
Some(user?.nickname().to_owned()),
))),
}),
Command::Numeric(RPL_TOPIC | RPL_TOPICWHOTIME | RPL_CHANNELMODEIS, params) => {
Command::Numeric(RPL_TOPIC | RPL_TOPICWHOTIME, params) => {
let channel = params.get(1)?.clone();
Some(Target::Channel {
channel,
source: source::Source::Server(Some(source::Server::new(
source::server::Kind::ReplyTopic,
None,
))),
})
}
Command::Numeric(RPL_CHANNELMODEIS, params) => {
let channel = params.get(1)?.clone();
Some(Target::Channel {
channel,
Expand Down Expand Up @@ -253,7 +263,7 @@ fn target(message: Encoded, our_nick: &Nick) -> Option<Target> {
}
}

fn server_time(message: &Encoded) -> DateTime<Utc> {
pub fn server_time(message: &Encoded) -> DateTime<Utc> {
message
.tags
.iter()
Expand Down
11 changes: 9 additions & 2 deletions data/src/message/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,14 @@ pub mod server {
}

impl Server {
pub fn new(kind: Kind, nick: Option<Nick>) -> Self {
Self::Details(Details { kind, nick })
pub fn new(
kind: Kind,
nick: Option<Nick>,
) -> Self {
Self::Details(Details {
kind,
nick,
})
}

pub fn kind(&self) -> Kind {
Expand All @@ -63,6 +69,7 @@ pub mod server {
Join,
Part,
Quit,
ReplyTopic,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down
Loading
Loading