comment_view::*,
moderator::*,
post::*,
- site_view::*,
user::*,
+ views::site_view::SiteView,
Crud,
Likeable,
ListingType,
// Don't do a downvote if site has downvotes disabled
if data.score == -1 {
- let site = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
- if !site.enable_downvotes {
+ let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
+ if !site_view.site.enable_downvotes {
return Err(APIError::err("downvotes_disabled").into());
}
}
post::*,
post_report::*,
post_view::*,
- site_view::*,
+ views::site_view::SiteView,
Crud,
Likeable,
ListingType,
// Don't do a downvote if site has downvotes disabled
if data.score == -1 {
- let site = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
- if !site.enable_downvotes {
+ let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
+ if !site_view.site.enable_downvotes {
return Err(APIError::err("downvotes_disabled").into());
}
}
naive_now,
post_view::*,
site::*,
- site_view::*,
user_view::*,
+ views::site_view::SiteView,
Crud,
SearchType,
SortType,
// Make sure the site creator is the top admin
if let Some(site_view) = site_view.to_owned() {
- let site_creator_id = site_view.creator_id;
+ let site_creator_id = site_view.creator.id;
// TODO investigate why this is sometimes coming back null
// Maybe user_.admin isn't being set to true?
if let Some(creator_index) = admins.iter().position(|r| r.id == site_creator_id) {
version: version::VERSION.to_string(),
my_user,
federated_instances: linked_instances(context.pool()).await?,
+ // TODO
+ number_of_users: 0,
+ number_of_posts: 0,
+ number_of_comments: 0,
+ number_of_communities: 0,
})
}
}
let mut admins = blocking(context.pool(), move |conn| UserView::admins(conn)).await??;
let creator_index = admins
.iter()
- .position(|r| r.id == site_view.creator_id)
+ .position(|r| r.id == site_view.creator.id)
.context(location_info!())?;
let creator_user = admins.remove(creator_index);
admins.insert(0, creator_user);
version: version::VERSION.to_string(),
my_user: Some(user),
federated_instances: linked_instances(context.pool()).await?,
+ // TODO
+ number_of_users: 0,
+ number_of_posts: 0,
+ number_of_comments: 0,
+ number_of_communities: 0,
})
}
}
private_message::*,
private_message_view::*,
site::*,
- site_view::*,
user::*,
user_mention::*,
user_mention_view::*,
user_view::*,
+ views::site_view::SiteView,
Crud,
Followable,
Joinable,
let data: &Register = &self;
// Make sure site has open registration
- if let Ok(site) = blocking(context.pool(), move |conn| SiteView::read(conn)).await? {
- let site: SiteView = site;
- if !site.open_registration {
+ if let Ok(site_view) = blocking(context.pool(), move |conn| SiteView::read(conn)).await? {
+ if !site_view.site.open_registration {
return Err(APIError::err("registration_closed").into());
}
}
pub mod user_mention;
pub mod user_mention_view;
pub mod user_view;
+pub mod views;
pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
use crate::{naive_now, schema::site, Crud};
use diesel::{dsl::*, result::Error, *};
+use serde::Serialize;
-#[derive(Queryable, Identifiable, PartialEq, Debug)]
+#[derive(Queryable, Identifiable, PartialEq, Debug, Clone, Serialize)]
#[table_name = "site"]
pub struct Site {
pub id: i32,
pub banner: Option<Option<String>>,
}
+/// A safe representation of user, without the sensitive info
+#[derive(Clone, Debug, Serialize)]
+pub struct UserSafe {
+ pub id: i32,
+ pub name: String,
+ pub preferred_username: Option<String>,
+ pub avatar: Option<String>,
+ pub admin: bool,
+ pub banned: bool,
+ pub published: chrono::NaiveDateTime,
+ pub updated: Option<chrono::NaiveDateTime>,
+ pub matrix_user_id: Option<String>,
+ pub actor_id: String,
+ pub bio: Option<String>,
+ pub local: bool,
+ pub banner: Option<String>,
+ pub deleted: bool,
+}
+
impl Crud<UserForm> for User_ {
fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
user_
))
.get_result::<Self>(conn)
}
+
+ pub fn to_safe(&self) -> UserSafe {
+ UserSafe {
+ id: self.id,
+ name: self.name.to_owned(),
+ preferred_username: self.preferred_username.to_owned(),
+ avatar: self.avatar.to_owned(),
+ admin: self.admin,
+ banned: self.banned,
+ published: self.published,
+ updated: self.updated,
+ matrix_user_id: self.matrix_user_id.to_owned(),
+ actor_id: self.actor_id.to_owned(),
+ bio: self.bio.to_owned(),
+ local: self.local,
+ banner: self.banner.to_owned(),
+ deleted: self.deleted,
+ }
+ }
}
#[cfg(test)]
private_key: None,
public_key: None,
last_refreshed_at: inserted_user.published,
+ deleted: false,
};
let read_user = User_::read(&conn, inserted_user.id).unwrap();
--- /dev/null
+pub mod site_view;
--- /dev/null
+use crate::{
+ schema::{site as site_table, user_},
+ site::Site,
+ user::{UserSafe, User_},
+};
+use diesel::{result::Error, *};
+use serde::Serialize;
+
+#[derive(Debug, Serialize, Clone)]
+pub struct SiteView {
+ pub site: Site,
+ pub creator: UserSafe,
+}
+
+impl SiteView {
+ pub fn read(conn: &PgConnection) -> Result<Self, Error> {
+ let site_join = site_table::table
+ .inner_join(user_::table)
+ .first::<(Site, User_)>(conn)?;
+
+ Ok(SiteView {
+ site: site_join.0,
+ creator: site_join.1.to_safe(),
+ })
+ }
+}
community_view::*,
moderator_views::*,
post_view::*,
- site_view::*,
user::*,
user_view::*,
+ views::site_view::SiteView,
};
use serde::{Deserialize, Serialize};
pub auth: Option<String>,
}
+// TODO combine siteresponse and getsiteresponse
#[derive(Serialize, Clone)]
pub struct SiteResponse {
pub site: SiteView,
#[derive(Serialize)]
pub struct GetSiteResponse {
- pub site: Option<SiteView>,
+ pub site: Option<SiteView>, // Because the site might not be set up yet
+ pub number_of_users: i64,
+ pub number_of_posts: i64,
+ pub number_of_comments: i64,
+ pub number_of_communities: i64,
pub admins: Vec<UserView>,
pub banned: Vec<UserView>,
pub online: usize,
--- /dev/null
+-- This file should undo anything in `up.sql`
\ No newline at end of file
--- /dev/null
+-- Your SQL goes here
\ No newline at end of file
comment_view::{ReplyQueryBuilder, ReplyView},
community::Community,
post_view::{PostQueryBuilder, PostView},
- site_view::SiteView,
user::User_,
user_mention_view::{UserMentionQueryBuilder, UserMentionView},
+ views::site_view::SiteView,
ListingType,
SortType,
};
.namespaces(RSS_NAMESPACE.to_owned())
.title(&format!(
"{} - {}",
- site_view.name,
+ site_view.site.name,
listing_type.to_string()
))
.link(Settings::get().get_protocol_and_hostname())
.items(items);
- if let Some(site_desc) = site_view.description {
+ if let Some(site_desc) = site_view.site.description {
channel_builder.description(&site_desc);
}
let mut channel_builder = ChannelBuilder::default();
channel_builder
.namespaces(RSS_NAMESPACE.to_owned())
- .title(&format!("{} - {}", site_view.name, user.name))
+ .title(&format!("{} - {}", site_view.site.name, user.name))
.link(user_url)
.items(items);
let mut channel_builder = ChannelBuilder::default();
channel_builder
.namespaces(RSS_NAMESPACE.to_owned())
- .title(&format!("{} - {}", site_view.name, community.name))
+ .title(&format!("{} - {}", site_view.site.name, community.name))
.link(community.actor_id)
.items(items);
let mut channel_builder = ChannelBuilder::default();
channel_builder
.namespaces(RSS_NAMESPACE.to_owned())
- .title(&format!("{} - Subscribed", site_view.name))
+ .title(&format!("{} - Subscribed", site_view.site.name))
.link(Settings::get().get_protocol_and_hostname())
.items(items);
- if let Some(site_desc) = site_view.description {
+ if let Some(site_desc) = site_view.site.description {
channel_builder.description(&site_desc);
}
let mut channel_builder = ChannelBuilder::default();
channel_builder
.namespaces(RSS_NAMESPACE.to_owned())
- .title(&format!("{} - Inbox", site_view.name))
+ .title(&format!("{} - Inbox", site_view.site.name))
.link(format!(
"{}/inbox",
Settings::get().get_protocol_and_hostname()
))
.items(items);
- if let Some(site_desc) = site_view.description {
+ if let Some(site_desc) = site_view.site.description {
channel_builder.description(&site_desc);
}
use actix_web::{body::Body, error::ErrorBadRequest, *};
use anyhow::anyhow;
use lemmy_api::version;
-use lemmy_db::site_view::SiteView;
+use lemmy_db::views::site_view::SiteView;
use lemmy_structs::blocking;
use lemmy_utils::{settings::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
},
protocols,
usage: NodeInfoUsage {
- users: NodeInfoUsers {
- total: site_view.number_of_users,
- },
- local_posts: site_view.number_of_posts,
- local_comments: site_view.number_of_comments,
- open_registrations: site_view.open_registration,
+ // TODO get these again
+ users: NodeInfoUsers { total: 0 },
+ local_posts: 0,
+ local_comments: 0,
+ open_registrations: site_view.site.open_registration,
},
};