forked from hexbear-collective/lemmy-hexbear
Browse Source
Merge pull request 'fix/better-ratelimits' (#316) from fix/better-ratelimits into main
Merge pull request 'fix/better-ratelimits' (#316) from fix/better-ratelimits into main
Reviewed-on: https://git.chapo.chat/hexbear-collective/lemmy-hexbear/pulls/316main v0.7.60.hex
62 changed files with 548 additions and 694 deletions
-
24server/config/config.hjson
-
6server/config/defaults.hjson
-
2server/lemmy_api_structs/src/community.rs
-
2server/lemmy_api_structs/src/lib.rs
-
19server/lemmy_api_structs/src/site.rs
-
4server/lemmy_db/src/activity.rs
-
35server/lemmy_db/src/comment.rs
-
17server/lemmy_db/src/comment_view.rs
-
7server/lemmy_db/src/community.rs
-
2server/lemmy_db/src/lib.rs
-
21server/lemmy_db/src/moderator.rs
-
7server/lemmy_db/src/password_reset_request.rs
-
36server/lemmy_db/src/post.rs
-
12server/lemmy_db/src/post_view.rs
-
6server/lemmy_db/src/private_message.rs
-
79server/lemmy_db/src/schema.rs
-
2server/lemmy_db/src/site.rs
-
2server/lemmy_db/src/site_view.rs
-
14server/lemmy_db/src/user.rs
-
174server/lemmy_db/src/user_ban_id.rs
-
10server/lemmy_db/src/user_mention.rs
-
52server/lemmy_db/src/user_view.rs
-
39server/lemmy_rate_limit/src/lib.rs
-
12server/lemmy_rate_limit/src/rate_limiter.rs
-
14server/lemmy_utils/src/lib.rs
-
6server/lemmy_utils/src/settings.rs
-
15server/migrations/2021-05-13-171343_comms_to_autosubscribe/down.sql
-
17server/migrations/2021-05-13-171343_comms_to_autosubscribe/up.sql
-
45server/src/api/comment.rs
-
39server/src/api/community.rs
-
5server/src/api/community_settings.rs
-
20server/src/api/mod.rs
-
39server/src/api/post.rs
-
8server/src/api/report.rs
-
2server/src/api/site.rs
-
132server/src/api/user.rs
-
8server/src/apub/activity_queue.rs
-
34server/src/apub/comment.rs
-
22server/src/apub/community.rs
-
11server/src/apub/fetcher.rs
-
9server/src/apub/inbox/activities/announce.rs
-
8server/src/apub/inbox/activities/create.rs
-
13server/src/apub/inbox/activities/delete.rs
-
7server/src/apub/inbox/activities/dislike.rs
-
7server/src/apub/inbox/activities/like.rs
-
13server/src/apub/inbox/activities/remove.rs
-
16server/src/apub/inbox/activities/undo.rs
-
8server/src/apub/inbox/activities/update.rs
-
10server/src/apub/inbox/community_inbox.rs
-
12server/src/apub/inbox/shared_inbox.rs
-
6server/src/apub/inbox/user_inbox.rs
-
9server/src/apub/mod.rs
-
24server/src/apub/post.rs
-
23server/src/apub/private_message.rs
-
20server/src/apub/user.rs
-
19server/src/main.rs
-
10server/src/routes/api.rs
-
3server/src/routes/feeds.rs
-
5server/src/routes/images.rs
-
5server/src/routes/webfinger.rs
-
11server/src/websocket/chat_server.rs
-
3server/src/websocket/handlers.rs
@ -1,102 +1,136 @@ |
|||
use diesel::{dsl::*, result::Error, *};
|
|||
use crate::schema::{
|
|||
{ban_id, ban_id::dsl::*},
|
|||
{user_ban_id, user_ban_id::dsl::*},
|
|||
{ban_id, ban_id::dsl::*},
|
|||
{user_ban_id, user_ban_id::dsl::*},
|
|||
};
|
|||
use uuid::Uuid;
|
|||
use crate::user_view::UserViewSafe;
|
|||
use diesel::{dsl::*, result::Error, *};
|
|||
use uuid::Uuid;
|
|||
|
|||
#[derive(Queryable, Insertable)]
|
|||
#[table_name = "ban_id"]
|
|||
pub struct BanId {
|
|||
pub id: Uuid,
|
|||
pub created: chrono::NaiveDateTime,
|
|||
pub aliased_to: Option<Uuid>,
|
|||
pub id: Uuid,
|
|||
pub created: chrono::NaiveDateTime,
|
|||
pub aliased_to: Option<Uuid>,
|
|||
}
|
|||
|
|||
#[derive(Queryable, Insertable)]
|
|||
#[table_name = "user_ban_id"]
|
|||
pub struct UserBanId {
|
|||
pub bid: Uuid,
|
|||
pub uid: i32,
|
|||
pub bid: Uuid,
|
|||
pub uid: i32,
|
|||
}
|
|||
|
|||
#[derive(Queryable)]
|
|||
pub struct UserRelationResp {
|
|||
pub bid: Uuid,
|
|||
pub uid: i32,
|
|||
pub name: String,
|
|||
pub banned: bool,
|
|||
pub bid: Uuid,
|
|||
pub uid: i32,
|
|||
pub name: String,
|
|||
pub banned: bool,
|
|||
}
|
|||
|
|||
impl BanId {
|
|||
pub fn create(conn: &PgConnection) -> Result<Self, Error> {
|
|||
insert_into(ban_id).default_values().get_result::<Self>(conn)
|
|||
}
|
|||
pub fn create(conn: &PgConnection) -> Result<Self, Error> {
|
|||
insert_into(ban_id)
|
|||
.default_values()
|
|||
.get_result::<Self>(conn)
|
|||
}
|
|||
|
|||
pub fn read(conn: &PgConnection, ban_id_val: Uuid) -> Result<Self, Error> {
|
|||
ban_id.find(ban_id_val).first::<Self>(conn)
|
|||
}
|
|||
pub fn read(conn: &PgConnection, ban_id_val: Uuid) -> Result<Self, Error> {
|
|||
ban_id.find(ban_id_val).first::<Self>(conn)
|
|||
}
|
|||
|
|||
pub fn read_opt(conn: &PgConnection, ban_id_val: Uuid) -> Result<Option<Self>, Error> {
|
|||
ban_id.find(ban_id_val).first::<Self>(conn).optional()
|
|||
}
|
|||
pub fn read_opt(conn: &PgConnection, ban_id_val: Uuid) -> Result<Option<Self>, Error> {
|
|||
ban_id.find(ban_id_val).first::<Self>(conn).optional()
|
|||
}
|
|||
|
|||
pub fn update_alias(conn: &PgConnection, old_bid_val: Uuid, new_bid_val: Uuid) -> Result<Vec<Self>, Error> {
|
|||
update(ban_id.filter(ban_id::id.eq(old_bid_val).or(aliased_to.eq(old_bid_val)))).set(aliased_to.eq(new_bid_val)).get_results(conn)
|
|||
}
|
|||
pub fn update_alias(
|
|||
conn: &PgConnection,
|
|||
old_bid_val: Uuid,
|
|||
new_bid_val: Uuid,
|
|||
) -> Result<Vec<Self>, Error> {
|
|||
update(ban_id.filter(ban_id::id.eq(old_bid_val).or(aliased_to.eq(old_bid_val))))
|
|||
.set(aliased_to.eq(new_bid_val))
|
|||
.get_results(conn)
|
|||
}
|
|||
}
|
|||
|
|||
impl UserBanId {
|
|||
fn simple_associate(conn: &PgConnection, ban_id_val: Uuid, user_id_val: i32) -> Result<Self, Error> {
|
|||
insert_into(user_ban_id)
|
|||
.values(UserBanId { bid: ban_id_val, uid: user_id_val })
|
|||
.get_result::<Self>(conn)
|
|||
}
|
|||
fn simple_associate(
|
|||
conn: &PgConnection,
|
|||
ban_id_val: Uuid,
|
|||
user_id_val: i32,
|
|||
) -> Result<Self, Error> {
|
|||
insert_into(user_ban_id)
|
|||
.values(UserBanId {
|
|||
bid: ban_id_val,
|
|||
uid: user_id_val,
|
|||
})
|
|||
.get_result::<Self>(conn)
|
|||
}
|
|||
|
|||
fn overwriting_associate(conn: &PgConnection, old_bid_val: Uuid, new_bid_val: Uuid) -> Result<Self, Error> {
|
|||
BanId::update_alias(conn, old_bid_val, new_bid_val)?;
|
|||
update(user_ban_id.filter(bid.eq(old_bid_val))).set(bid.eq(new_bid_val)).get_result(conn)
|
|||
}
|
|||
fn overwriting_associate(
|
|||
conn: &PgConnection,
|
|||
old_bid_val: Uuid,
|
|||
new_bid_val: Uuid,
|
|||
) -> Result<Self, Error> {
|
|||
BanId::update_alias(conn, old_bid_val, new_bid_val)?;
|
|||
update(user_ban_id.filter(bid.eq(old_bid_val)))
|
|||
.set(bid.eq(new_bid_val))
|
|||
.get_result(conn)
|
|||
}
|
|||
|
|||
pub fn associate(conn: &PgConnection, ban_id_val: Uuid, user_id_val: i32) -> Result<Self, Error> {
|
|||
match Self::get_by_user(conn, &user_id_val) {
|
|||
//UserBanId found attached to user, which is not the same as the incoming one.
|
|||
Ok(Some(old_bid)) if old_bid.bid != ban_id_val => {
|
|||
let incoming_bid = BanId::read(conn, ban_id_val)?;
|
|||
//the incoming bid isn't aliased to the new one.
|
|||
if incoming_bid.aliased_to.is_none() || incoming_bid.aliased_to.unwrap() != old_bid.bid {
|
|||
return Self::overwriting_associate(conn, old_bid.bid, ban_id_val);
|
|||
}
|
|||
Ok(old_bid)
|
|||
},
|
|||
//UserBanId found, but it's the same as the incoming one.
|
|||
Ok(Some(k)) => Ok(k),
|
|||
//There wasn't any UBID attached to the user. Associate and move on.
|
|||
Ok(None) => {
|
|||
//Check for an alias
|
|||
let bid_read = BanId::read_opt(conn, ban_id_val)?;
|
|||
if let Some(BanId { aliased_to: Some(alias), .. }) = bid_read {
|
|||
Self::simple_associate(conn, alias, user_id_val)
|
|||
} else {
|
|||
Self::simple_associate(conn, ban_id_val, user_id_val)
|
|||
}
|
|||
},
|
|||
//Breaking error, bubble it up.
|
|||
Err(e) => Err(e),
|
|||
pub fn associate(conn: &PgConnection, ban_id_val: Uuid, user_id_val: i32) -> Result<Self, Error> {
|
|||
match Self::get_by_user(conn, &user_id_val) {
|
|||
//UserBanId found attached to user, which is not the same as the incoming one.
|
|||
Ok(Some(old_bid)) if old_bid.bid != ban_id_val => {
|
|||
let incoming_bid = BanId::read(conn, ban_id_val)?;
|
|||
//the incoming bid isn't aliased to the new one.
|
|||
if incoming_bid.aliased_to.is_none() || incoming_bid.aliased_to.unwrap() != old_bid.bid {
|
|||
return Self::overwriting_associate(conn, old_bid.bid, ban_id_val);
|
|||
}
|
|||
Ok(old_bid)
|
|||
}
|
|||
//UserBanId found, but it's the same as the incoming one.
|
|||
Ok(Some(k)) => Ok(k),
|
|||
//There wasn't any UBID attached to the user. Associate and move on.
|
|||
Ok(None) => {
|
|||
//Check for an alias
|
|||
let bid_read = BanId::read_opt(conn, ban_id_val)?;
|
|||
if let Some(BanId {
|
|||
aliased_to: Some(alias),
|
|||
..
|
|||
}) = bid_read
|
|||
{
|
|||
Self::simple_associate(conn, alias, user_id_val)
|
|||
} else {
|
|||
Self::simple_associate(conn, ban_id_val, user_id_val)
|
|||
}
|
|||
}
|
|||
//Breaking error, bubble it up.
|
|||
Err(e) => Err(e),
|
|||
}
|
|||
}
|
|||
|
|||
pub fn create_then_associate(conn: &PgConnection, user_id_val: i32) -> Result<Self, Error> {
|
|||
Self::simple_associate(conn, BanId::create(conn)?.id, user_id_val)
|
|||
}
|
|||
pub fn create_then_associate(conn: &PgConnection, user_id_val: i32) -> Result<Self, Error> {
|
|||
Self::simple_associate(conn, BanId::create(conn)?.id, user_id_val)
|
|||
}
|
|||
|
|||
pub fn get_by_user(conn: &PgConnection, user_id_val: &i32) -> Result<Option<Self>, Error> {
|
|||
user_ban_id.filter(uid.eq(user_id_val)).first::<Self>(conn).optional()
|
|||
}
|
|||
pub fn get_by_user(conn: &PgConnection, user_id_val: &i32) -> Result<Option<Self>, Error> {
|
|||
user_ban_id
|
|||
.filter(uid.eq(user_id_val))
|
|||
.first::<Self>(conn)
|
|||
.optional()
|
|||
}
|
|||
|
|||
pub fn get_users_by_bid(conn: &PgConnection, ban_id_val: Uuid) -> Result<Vec<UserViewSafe>, Error> {
|
|||
let uids = user_ban_id.filter(bid.eq(ban_id_val)).select(uid).load(conn)?;
|
|||
UserViewSafe::read_mult(conn, uids)
|
|||
}
|
|||
}
|
|||
pub fn get_users_by_bid(
|
|||
conn: &PgConnection,
|
|||
ban_id_val: Uuid,
|
|||
) -> Result<Vec<UserViewSafe>, Error> {
|
|||
let uids = user_ban_id
|
|||
.filter(bid.eq(ban_id_val))
|
|||
.select(uid)
|
|||
.load(conn)?;
|
|||
UserViewSafe::read_mult(conn, uids)
|
|||
}
|
|||
}
|
@ -0,0 +1,15 @@ |
|||
-- This file should undo anything in `up.sql` |
|||
drop view site_view; |
|||
alter table site drop column autosubscribe_comms; |
|||
|
|||
create view site_view as |
|||
select s.*, |
|||
u.name as creator_name, |
|||
u.preferred_username as creator_preferred_username, |
|||
u.avatar as creator_avatar, |
|||
(select count(*) from user_) as number_of_users, |
|||
(select count(*) from post) as number_of_posts, |
|||
(select count(*) from comment) as number_of_comments, |
|||
(select count(*) from community) as number_of_communities |
|||
from site s |
|||
left join user_ u on s.creator_id = u.id; |
@ -0,0 +1,17 @@ |
|||
-- Your SQL goes here |
|||
|
|||
alter table site add column autosubscribe_comms integer[] default '{}' not null; |
|||
|
|||
drop view site_view; |
|||
|
|||
create view site_view as |
|||
select s.*, |
|||
u.name as creator_name, |
|||
u.preferred_username as creator_preferred_username, |
|||
u.avatar as creator_avatar, |
|||
(select count(*) from user_) as number_of_users, |
|||
(select count(*) from post) as number_of_posts, |
|||
(select count(*) from comment) as number_of_comments, |
|||
(select count(*) from community) as number_of_communities |
|||
from site s |
|||
left join user_ u on s.creator_id = u.id; |