add: use RemoteAddr for ip blocks as well

This commit is contained in:
trisua 2025-06-30 15:35:18 -04:00
parent 14936b8b90
commit f5faed7762
12 changed files with 127 additions and 28 deletions

View file

@ -23,7 +23,7 @@ impl DataManager {
///
/// # Arguments
/// * `prefix`
pub async fn get_ipban_by_addr(&self, addr: RemoteAddr) -> Result<IpBan> {
pub async fn get_ipban_by_addr(&self, addr: &RemoteAddr) -> Result<IpBan> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),

View file

@ -1,10 +1,8 @@
use oiseau::cache::Cache;
use crate::model::addr::RemoteAddr;
use crate::model::{Error, Result, auth::User, auth::IpBlock, permissions::FinePermission};
use crate::{auto_method, DataManager};
use oiseau::{query_rows, PostgresRow};
use oiseau::{execute, get, query_row, params};
use oiseau::{query_rows, PostgresRow, execute, get, query_row, params};
impl DataManager {
/// Get an [`IpBlock`] from an SQL row.
@ -23,7 +21,7 @@ impl DataManager {
pub async fn get_ipblock_by_initiator_receiver(
&self,
initiator: usize,
receiver: &str,
receiver: &RemoteAddr,
) -> Result<IpBlock> {
let conn = match self.0.connect().await {
Ok(c) => c,
@ -32,8 +30,8 @@ impl DataManager {
let res = query_row!(
&conn,
"SELECT * FROM ipblocks WHERE initiator = $1 AND receiver = $2",
params![&(initiator as i64), &receiver],
"SELECT * FROM ipblocks WHERE initiator = $1 AND receiver LIKE $2",
params![&(initiator as i64), &format!("{}%", receiver.prefix(None))],
|x| { Ok(Self::get_ipblock_from_row(x)) }
);

View file

@ -1,6 +1,7 @@
use std::collections::HashMap;
use oiseau::cache::Cache;
use tetratto_shared::unix_epoch_timestamp;
use crate::model::addr::RemoteAddr;
use crate::model::communities_permissions::CommunityPermission;
use crate::model::uploads::{MediaType, MediaUpload};
use crate::model::{
@ -368,7 +369,10 @@ impl DataManager {
// check for ip block
if self
.get_ipblock_by_initiator_receiver(receiver.id, &data.ip)
.get_ipblock_by_initiator_receiver(
receiver.id,
&RemoteAddr::from(data.ip.as_str()),
)
.await
.is_ok()
{
@ -393,7 +397,7 @@ impl DataManager {
// check for ip block
if self
.get_ipblock_by_initiator_receiver(receiver.id, &data.ip)
.get_ipblock_by_initiator_receiver(receiver.id, &RemoteAddr::from(data.ip.as_str()))
.await
.is_ok()
{

View file

@ -1,5 +1,6 @@
use oiseau::cache::Cache;
use crate::model::{
addr::RemoteAddr,
auth::{AchievementName, Notification, User},
permissions::FinePermission,
reactions::{AssetType, Reaction},
@ -127,7 +128,12 @@ impl DataManager {
///
/// # Arguments
/// * `data` - a mock [`Reaction`] object to insert
pub async fn create_reaction(&self, data: Reaction, user: &User) -> Result<()> {
pub async fn create_reaction(
&self,
data: Reaction,
user: &User,
addr: &RemoteAddr,
) -> Result<()> {
let conn = match self.0.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
@ -140,10 +146,14 @@ impl DataManager {
.get_userblock_by_initiator_receiver(post.owner, user.id)
.await
.is_ok()
| self
| (self
.get_user_stack_blocked_users(post.owner)
.await
.contains(&user.id))
.contains(&user.id)
| self
.get_ipblock_by_initiator_receiver(post.owner, &addr)
.await
.is_ok()))
&& !user.permissions.check(FinePermission::MANAGE_POSTS)
{
return Err(Error::NotAllowed);

View file

@ -85,7 +85,18 @@ impl DataManager {
}
/// Get the receiver of all user blocks for the given `initiator`.
pub async fn get_userblocks_receivers(&self, initiator: usize) -> Vec<usize> {
pub async fn get_userblocks_receivers(
&self,
initiator: usize,
associated: &Vec<usize>,
) -> Vec<usize> {
let mut associated_str = String::new();
for id in associated {
associated_str.push_str(&(" OR initiator = ".to_string() + &id.to_string()));
}
// ...
let conn = match self.0.connect().await {
Ok(c) => c,
Err(_) => return Vec::new(),
@ -93,7 +104,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM userblocks WHERE initiator = $1",
&format!("SELECT * FROM userblocks WHERE initiator = $1{associated_str}"),
&[&(initiator as i64)],
|x| { Self::get_userblock_from_row(x) }
);