mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-12-23 11:21:32 +00:00
Avoid using database views in federation code
This commit is contained in:
parent
6a7a262912
commit
c16458b728
5 changed files with 36 additions and 26 deletions
2
docker/federation-test/nginx.conf
vendored
2
docker/federation-test/nginx.conf
vendored
|
@ -45,7 +45,7 @@ http {
|
||||||
client_max_body_size 50M;
|
client_max_body_size 50M;
|
||||||
|
|
||||||
location / {
|
location / {
|
||||||
proxy_pass http://lemmy_beta:8540;
|
proxy_pass http://lemmy_beta:8550;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
use crate::api::community::ListCommunities;
|
|
||||||
use crate::api::{Oper, Perform};
|
|
||||||
use crate::apub::puller::{fetch_remote_object, format_community_name};
|
use crate::apub::puller::{fetch_remote_object, format_community_name};
|
||||||
use crate::apub::{
|
use crate::apub::{
|
||||||
create_apub_response, get_apub_protocol_string, make_apub_endpoint, EndpointType, GroupExt,
|
create_apub_response, get_apub_protocol_string, make_apub_endpoint, EndpointType, GroupExt,
|
||||||
|
@ -8,7 +6,7 @@ use crate::convert_datetime;
|
||||||
use crate::db::community::Community;
|
use crate::db::community::Community;
|
||||||
use crate::db::community_view::{CommunityFollowerView, CommunityView};
|
use crate::db::community_view::{CommunityFollowerView, CommunityView};
|
||||||
use crate::db::establish_unpooled_connection;
|
use crate::db::establish_unpooled_connection;
|
||||||
use crate::db::post_view::{PostQueryBuilder, PostView};
|
use crate::db::post::Post;
|
||||||
use crate::settings::Settings;
|
use crate::settings::Settings;
|
||||||
use activitystreams::actor::properties::ApActorProperties;
|
use activitystreams::actor::properties::ApActorProperties;
|
||||||
use activitystreams::collection::OrderedCollection;
|
use activitystreams::collection::OrderedCollection;
|
||||||
|
@ -34,20 +32,10 @@ pub async fn get_apub_community_list(
|
||||||
db: web::Data<Pool<ConnectionManager<PgConnection>>>,
|
db: web::Data<Pool<ConnectionManager<PgConnection>>>,
|
||||||
) -> Result<HttpResponse<Body>, Error> {
|
) -> Result<HttpResponse<Body>, Error> {
|
||||||
// TODO: implement pagination
|
// TODO: implement pagination
|
||||||
let query = ListCommunities {
|
let communities = Community::list(&db.get().unwrap())?
|
||||||
sort: "Hot".to_string(),
|
|
||||||
page: None,
|
|
||||||
limit: None,
|
|
||||||
auth: None,
|
|
||||||
local_only: Some(true),
|
|
||||||
};
|
|
||||||
let communities = Oper::new(query)
|
|
||||||
.perform(&db.get().unwrap())
|
|
||||||
.unwrap()
|
|
||||||
.communities
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(|c| c.as_group())
|
.map(|c| c.as_group())
|
||||||
.collect::<Result<Vec<GroupExt>, failure::Error>>()?;
|
.collect::<Result<Vec<GroupExt>, Error>>()?;
|
||||||
let mut collection = UnorderedCollection::default();
|
let mut collection = UnorderedCollection::default();
|
||||||
let oprops: &mut ObjectProperties = collection.as_mut();
|
let oprops: &mut ObjectProperties = collection.as_mut();
|
||||||
oprops.set_context_xsd_any_uri(context())?.set_id(format!(
|
oprops.set_context_xsd_any_uri(context())?.set_id(format!(
|
||||||
|
@ -63,7 +51,7 @@ pub async fn get_apub_community_list(
|
||||||
Ok(create_apub_response(&collection))
|
Ok(create_apub_response(&collection))
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CommunityView {
|
impl Community {
|
||||||
fn as_group(&self) -> Result<GroupExt, Error> {
|
fn as_group(&self) -> Result<GroupExt, Error> {
|
||||||
let base_url = make_apub_endpoint(EndpointType::Community, &self.name);
|
let base_url = make_apub_endpoint(EndpointType::Community, &self.name);
|
||||||
|
|
||||||
|
@ -97,7 +85,9 @@ impl CommunityView {
|
||||||
|
|
||||||
Ok(group.extend(actor_props))
|
Ok(group.extend(actor_props))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CommunityView {
|
||||||
pub fn from_group(group: &GroupExt, domain: &str) -> Result<CommunityView, Error> {
|
pub fn from_group(group: &GroupExt, domain: &str) -> Result<CommunityView, Error> {
|
||||||
let followers_uri = &group.extension.get_followers().unwrap().to_string();
|
let followers_uri = &group.extension.get_followers().unwrap().to_string();
|
||||||
let outbox_uri = &group.extension.get_outbox().to_string();
|
let outbox_uri = &group.extension.get_outbox().to_string();
|
||||||
|
@ -147,8 +137,7 @@ pub async fn get_apub_community_http(
|
||||||
db: web::Data<Pool<ConnectionManager<PgConnection>>>,
|
db: web::Data<Pool<ConnectionManager<PgConnection>>>,
|
||||||
) -> Result<HttpResponse<Body>, Error> {
|
) -> Result<HttpResponse<Body>, Error> {
|
||||||
let community = Community::read_from_name(&&db.get()?, info.community_name.to_owned())?;
|
let community = Community::read_from_name(&&db.get()?, info.community_name.to_owned())?;
|
||||||
let community_view = CommunityView::read(&&db.get()?, community.id, None)?;
|
let c = community.as_group()?;
|
||||||
let c = community_view.as_group()?;
|
|
||||||
Ok(create_apub_response(&c))
|
Ok(create_apub_response(&c))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,10 +173,7 @@ pub async fn get_apub_community_outbox(
|
||||||
|
|
||||||
let connection = establish_unpooled_connection();
|
let connection = establish_unpooled_connection();
|
||||||
//As we are an object, we validated that the community id was valid
|
//As we are an object, we validated that the community id was valid
|
||||||
let community_posts: Vec<PostView> = PostQueryBuilder::create(&connection)
|
let community_posts: Vec<Post> = Post::list_for_community(&connection, community.id)?;
|
||||||
.for_community_id(community.id)
|
|
||||||
.list()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let mut collection = OrderedCollection::default();
|
let mut collection = OrderedCollection::default();
|
||||||
let oprops: &mut ObjectProperties = collection.as_mut();
|
let oprops: &mut ObjectProperties = collection.as_mut();
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::apub::{create_apub_response, make_apub_endpoint, EndpointType};
|
use crate::apub::{create_apub_response, make_apub_endpoint, EndpointType};
|
||||||
|
use crate::db::post::Post;
|
||||||
use crate::db::post_view::PostView;
|
use crate::db::post_view::PostView;
|
||||||
use crate::{convert_datetime, naive_now};
|
use crate::{convert_datetime, naive_now};
|
||||||
use activitystreams::{object::properties::ObjectProperties, object::Page};
|
use activitystreams::{object::properties::ObjectProperties, object::Page};
|
||||||
|
@ -20,12 +21,11 @@ pub async fn get_apub_post(
|
||||||
db: web::Data<Pool<ConnectionManager<PgConnection>>>,
|
db: web::Data<Pool<ConnectionManager<PgConnection>>>,
|
||||||
) -> Result<HttpResponse<Body>, Error> {
|
) -> Result<HttpResponse<Body>, Error> {
|
||||||
let id = info.post_id.parse::<i32>()?;
|
let id = info.post_id.parse::<i32>()?;
|
||||||
// TODO: shows error: missing field `user_name`
|
let post = Post::read(&&db.get()?, id)?;
|
||||||
let post = PostView::read(&&db.get()?, id, None)?;
|
|
||||||
Ok(create_apub_response(&post.as_page()?))
|
Ok(create_apub_response(&post.as_page()?))
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PostView {
|
impl Post {
|
||||||
pub fn as_page(&self) -> Result<Page, Error> {
|
pub fn as_page(&self) -> Result<Page, Error> {
|
||||||
let base_url = make_apub_endpoint(EndpointType::Post, &self.id.to_string());
|
let base_url = make_apub_endpoint(EndpointType::Post, &self.id.to_string());
|
||||||
let mut page = Page::default();
|
let mut page = Page::default();
|
||||||
|
@ -59,7 +59,9 @@ impl PostView {
|
||||||
|
|
||||||
Ok(page)
|
Ok(page)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PostView {
|
||||||
pub fn from_page(page: &Page) -> Result<PostView, Error> {
|
pub fn from_page(page: &Page) -> Result<PostView, Error> {
|
||||||
let oprops = &page.object_props;
|
let oprops = &page.object_props;
|
||||||
Ok(PostView {
|
Ok(PostView {
|
||||||
|
|
|
@ -79,6 +79,11 @@ impl Community {
|
||||||
.first::<Self>(conn)
|
.first::<Self>(conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn list(conn: &PgConnection) -> Result<Vec<Self>, Error> {
|
||||||
|
use crate::schema::community::dsl::*;
|
||||||
|
community.load::<Community>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_url(&self) -> String {
|
pub fn get_url(&self) -> String {
|
||||||
format!("https://{}/c/{}", Settings::get().hostname, self.name)
|
format!("https://{}/c/{}", Settings::get().hostname, self.name)
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,23 @@ pub struct PostForm {
|
||||||
pub thumbnail_url: Option<String>,
|
pub thumbnail_url: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Post {
|
||||||
|
pub fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
|
||||||
|
use crate::schema::post::dsl::*;
|
||||||
|
post.filter(id.eq(post_id)).first::<Self>(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn list_for_community(
|
||||||
|
conn: &PgConnection,
|
||||||
|
the_community_id: i32,
|
||||||
|
) -> Result<Vec<Self>, Error> {
|
||||||
|
use crate::schema::post::dsl::*;
|
||||||
|
post
|
||||||
|
.filter(community_id.eq(the_community_id))
|
||||||
|
.load::<Self>(conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Crud<PostForm> for Post {
|
impl Crud<PostForm> for Post {
|
||||||
fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
|
fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
|
||||||
use crate::schema::post::dsl::*;
|
use crate::schema::post::dsl::*;
|
||||||
|
|
Loading…
Reference in a new issue