add: channel mutes

This commit is contained in:
trisua 2025-07-18 20:04:26 -04:00
parent 02f3d08926
commit 884a89904e
17 changed files with 149 additions and 7 deletions

View file

@ -222,6 +222,8 @@ version = "1.0.0"
"chats:action.add_someone" = "Add someone"
"chats:action.kick_member" = "Kick member"
"chats:action.mention_user" = "Mention user"
"chats:action.mute" = "Mute"
"chats:action.unmute" = "Unmute"
"stacks:link.stacks" = "Stacks"
"stacks:label.my_stacks" = "My stacks"

View file

@ -210,6 +210,30 @@
});
};
globalThis.mute_channel = async (id, mute = true) => {
await trigger(\"atto::debounce\", [\"channels::mute\"]);
fetch(`/api/v1/channels/${id}/mute`, {
method: mute ? \"POST\" : \"DELETE\",
})
.then((res) => res.json())
.then((res) => {
trigger(\"atto::toast\", [
res.ok ? \"success\" : \"error\",
res.message,
]);
if (res.ok) {
if (mute) {
document.querySelector(`[ui_ident=channel\\\\.mute\\\\:${id}]`).classList.add(\"hidden\");
document.querySelector(`[ui_ident=channel\\\\.unmute\\\\:${id}]`).classList.remove(\"hidden\");
} else {
document.querySelector(`[ui_ident=channel\\\\.mute\\\\:${id}]`).classList.remove(\"hidden\");
document.querySelector(`[ui_ident=channel\\\\.unmute\\\\:${id}]`).classList.add(\"hidden\");
}
}
});
};
globalThis.update_channel_title = async (id) => {
await trigger(\"atto::debounce\", [\"channels::update_title\"]);
const title = await trigger(\"atto::prompt\", [\"New channel title:\"]);

View file

@ -31,6 +31,22 @@
(text "{{ icon \"user-plus\" }}")
(span
(text "{{ text \"chats:action.add_someone\" }}")))
; mute/unmute
(button
("class" "lowered small {% if channel.id in user.channel_mutes -%} hidden {%- endif %}")
("ui_ident" "channel.mute:{{ channel.id }}")
("onclick" "mute_channel('{{ channel.id }}')")
(icon (text "bell-off"))
(span
(str (text "chats:action.mute"))))
(button
("class" "lowered small {% if not channel.id in user.channel_mutes -%} hidden {%- endif %}")
("ui_ident" "channel.unmute:{{ channel.id }}")
("onclick" "mute_channel('{{ channel.id }}', false)")
(icon (text "bell-ring"))
(span
(str (text "chats:action.unmute"))))
; ...
(text "{%- endif %}")
(button
("class" "lowered small")

View file

@ -113,6 +113,12 @@
("style" "color: var(--color-primary)")
("class" "flex items-center")
(text "{{ icon \"badge-check\" }}"))
(text "{%- endif %} {% if user.permissions|has_staff_badge -%}")
(span
("title" "Staff")
("style" "color: var(--color-primary);")
("class" "flex items-center")
(text "{{ icon \"shield-user\" }}"))
(text "{%- endif %}"))
(text "{%- endif %} {%- endmacro %} {% macro repost(repost, post, owner, secondary=false, community=false, show_community=true, can_manage_post=false) -%}")
(div

View file

@ -689,7 +689,7 @@ media_theme_pref();
});
self.define("hooks::check_message_reactions", async ({ $ }) => {
const observer = $.offload_work_to_client_when_in_view(
const observer = await $.offload_work_to_client_when_in_view(
async (element) => {
const reactions = await (
await fetch(

View file

@ -293,3 +293,62 @@ pub async fn get_request(
Err(e) => Json(e.into()),
}
}
pub async fn mute_channel_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(id): Path<usize>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let mut user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageChannelMutes) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
if user.channel_mutes.contains(&id) {
return Json(Error::MiscError("Channel already muted".to_string()).into());
}
user.channel_mutes.push(id);
match data
.update_user_channel_mutes(user.id, user.channel_mutes)
.await
{
Ok(_) => Json(ApiReturn {
ok: true,
message: "Channel muted".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}
pub async fn unmute_channel_request(
jar: CookieJar,
Extension(data): Extension<State>,
Path(id): Path<usize>,
) -> impl IntoResponse {
let data = &(data.read().await).0;
let mut user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageChannelMutes) {
Some(ua) => ua,
None => return Json(Error::NotAllowed.into()),
};
let pos = match user.channel_mutes.iter().position(|x| *x == id) {
Some(x) => x,
None => return Json(Error::MiscError("Channel not muted".to_string()).into()),
};
user.channel_mutes.remove(pos);
match data
.update_user_channel_mutes(user.id, user.channel_mutes)
.await
{
Ok(_) => Json(ApiReturn {
ok: true,
message: "Channel muted".to_string(),
payload: (),
}),
Err(e) => Json(e.into()),
}
}

View file

@ -17,6 +17,8 @@ use tetratto_core::model::{
/// Expand a unicode emoji into its Gemoji shortcode.
pub async fn get_emoji_shortcode(emoji: String) -> impl IntoResponse {
match emoji.as_str() {
// matches `CustomEmoji::replace`
"💯" => "100".to_string(),
"👍" => "thumbs_up".to_string(),
"👎" => "thumbs_down".to_string(),
_ => match emojis::get(&emoji) {

View file

@ -570,6 +570,14 @@ pub fn routes() -> Router {
"/channels/{id}/kick",
post(channels::channels::kick_member_request),
)
.route(
"/channels/{id}/mute",
post(channels::channels::mute_channel_request),
)
.route(
"/channels/{id}/mute",
delete(channels::channels::unmute_channel_request),
)
.route("/channels/{id}", get(channels::channels::get_request))
.route(
"/channels/community/{id}",

View file

@ -120,6 +120,7 @@ impl DataManager {
browser_session: get!(x->26(String)),
seller_data: serde_json::from_str(&get!(x->27(String)).to_string()).unwrap(),
ban_reason: get!(x->28(String)),
channel_mutes: serde_json::from_str(&get!(x->29(String)).to_string()).unwrap(),
}
}
@ -276,7 +277,7 @@ impl DataManager {
let res = execute!(
&conn,
"INSERT INTO users VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29)",
"INSERT INTO users VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30)",
params![
&(data.id as i64),
&(data.created as i64),
@ -306,7 +307,8 @@ impl DataManager {
&if data.was_purchased { 1_i32 } else { 0_i32 },
&data.browser_session,
&serde_json::to_string(&data.seller_data).unwrap(),
&data.ban_reason
&data.ban_reason,
&serde_json::to_string(&data.channel_mutes).unwrap(),
]
);
@ -1004,6 +1006,7 @@ impl DataManager {
auto_method!(update_user_browser_session(&str)@get_user_by_id -> "UPDATE users SET browser_session = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_user);
auto_method!(update_user_seller_data(StripeSellerData)@get_user_by_id -> "UPDATE users SET seller_data = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_user);
auto_method!(update_user_ban_reason(&str)@get_user_by_id -> "UPDATE users SET ban_reason = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_user);
auto_method!(update_user_channel_mutes(Vec<usize>)@get_user_by_id -> "UPDATE users SET channel_mutes = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_user);
auto_method!(get_user_by_stripe_id(&str)@get_user_from_row -> "SELECT * FROM users WHERE stripe_id = $1" --name="user" --returns=User);
auto_method!(update_user_stripe_id(&str)@get_user_by_id -> "UPDATE users SET stripe_id = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_user);

View file

@ -44,6 +44,7 @@ impl DataManager {
execute!(&conn, common::CREATE_TABLE_SERVICES).unwrap();
execute!(&conn, common::CREATE_TABLE_PRODUCTS).unwrap();
execute!(&conn, common::CREATE_TABLE_APP_DATA).unwrap();
execute!(&conn, common::VERSION_MIGRATIONS).unwrap();
self.0
.1

View file

@ -1,3 +1,4 @@
pub const VERSION_MIGRATIONS: &str = include_str!("./sql/version_migrations.sql");
pub const CREATE_TABLE_USERS: &str = include_str!("./sql/create_users.sql");
pub const CREATE_TABLE_COMMUNITIES: &str = include_str!("./sql/create_communities.sql");
pub const CREATE_TABLE_POSTS: &str = include_str!("./sql/create_posts.sql");

View file

@ -27,5 +27,6 @@ CREATE TABLE IF NOT EXISTS users (
was_purchased INT NOT NULL,
browser_session TEXT NOT NULL,
seller_data TEXT NOT NULL,
ban_reason TEXT NOT NULL
ban_reason TEXT NOT NULL,
channel_mutes TEXT NOT NULL
)

View file

@ -0,0 +1,3 @@
-- users channel_mutes
ALTER TABLE users
ADD COLUMN IF NOT EXISTS channel_mutes TEXT DEFAULT '[]';

View file

@ -190,6 +190,11 @@ impl DataManager {
continue;
}
let user = self.get_user_by_id(member).await?;
if user.channel_mutes.contains(&channel.id) {
continue;
}
let mut notif = Notification::new(
"You've received a new message!".to_string(),
format!(

View file

@ -86,6 +86,9 @@ pub struct User {
/// The reason the user was banned.
#[serde(default)]
pub ban_reason: String,
/// IDs of channels the user has muted.
#[serde(default)]
pub channel_mutes: Vec<usize>,
}
pub type UserConnections =
@ -387,6 +390,7 @@ impl User {
browser_session: String::new(),
seller_data: StripeSellerData::default(),
ban_reason: String::new(),
channel_mutes: Vec::new(),
}
}

View file

@ -144,6 +144,8 @@ pub enum AppScope {
UserManageServices,
/// Manage the user's products.
UserManageProducts,
/// Manage the user's channel mutes.
UserManageChannelMutes,
/// Edit posts created by the user.
UserEditPosts,
/// Edit drafts created by the user.

View file

@ -131,10 +131,15 @@ impl CustomEmoji {
if emoji.1 == 0 {
out = out.replace(
&emoji.0,
match emojis::get_by_shortcode(&emoji.2) {
match emoji.2.as_str() {
"100" => "💯",
"thumbs_up" => "👍",
"thumbs_down" => "👎",
_ => match emojis::get_by_shortcode(&emoji.2) {
Some(e) => e.as_str(),
None => &emoji.0,
},
},
);
} else {
out = out.replace(