1 use crate::{ApubObject, Crud};
2 use diesel::{dsl::*, result::Error, *};
5 schema::person::dsl::*,
6 source::person::{Person, PersonForm},
13 use lemmy_db_schema::{schema::person::columns::*, source::person::Person};
35 impl ToSafe for Person {
36 type SafeColumns = Columns;
37 fn safe_columns_tuple() -> Self::SafeColumns {
61 mod safe_type_alias_1 {
63 use lemmy_db_schema::{schema::person_alias_1::columns::*, source::person::PersonAlias1};
85 impl ToSafe for PersonAlias1 {
86 type SafeColumns = Columns;
87 fn safe_columns_tuple() -> Self::SafeColumns {
111 mod safe_type_alias_2 {
113 use lemmy_db_schema::{schema::person_alias_2::columns::*, source::person::PersonAlias2};
135 impl ToSafe for PersonAlias2 {
136 type SafeColumns = Columns;
137 fn safe_columns_tuple() -> Self::SafeColumns {
161 impl Crud for Person {
162 type Form = PersonForm;
163 type IdType = PersonId;
164 fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
166 .filter(deleted.eq(false))
170 fn delete(conn: &PgConnection, person_id: PersonId) -> Result<usize, Error> {
171 diesel::delete(person.find(person_id)).execute(conn)
173 fn create(conn: &PgConnection, form: &PersonForm) -> Result<Self, Error> {
174 insert_into(person).values(form).get_result::<Self>(conn)
176 fn update(conn: &PgConnection, person_id: PersonId, form: &PersonForm) -> Result<Self, Error> {
177 diesel::update(person.find(person_id))
179 .get_result::<Self>(conn)
183 impl ApubObject for Person {
184 type Form = PersonForm;
185 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
186 use lemmy_db_schema::schema::person::dsl::*;
188 .filter(deleted.eq(false))
189 .filter(actor_id.eq(object_id))
193 fn upsert(conn: &PgConnection, person_form: &PersonForm) -> Result<Person, Error> {
196 .on_conflict(actor_id)
199 .get_result::<Self>(conn)
204 fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Person, Error>;
205 fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> Result<Person, Error>;
206 fn find_by_name(conn: &PgConnection, name: &str) -> Result<Person, Error>;
207 fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
208 fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
211 impl Person_ for Person {
212 fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Self, Error> {
213 diesel::update(person.find(person_id))
215 .get_result::<Self>(conn)
218 fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> Result<Self, Error> {
219 diesel::update(person.find(person_id))
220 .set(admin.eq(added))
221 .get_result::<Self>(conn)
224 fn find_by_name(conn: &PgConnection, from_name: &str) -> Result<Person, Error> {
226 .filter(deleted.eq(false))
227 .filter(local.eq(true))
228 .filter(name.ilike(from_name))
229 .first::<Person>(conn)
232 fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error> {
233 diesel::update(person.find(person_id))
234 .set((last_refreshed_at.eq(naive_now()),))
235 .get_result::<Self>(conn)
238 fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error> {
239 use lemmy_db_schema::schema::local_user;
241 // Set the local user info to none
242 diesel::update(local_user::table.filter(local_user::person_id.eq(person_id)))
244 local_user::email.eq::<Option<String>>(None),
245 local_user::validator_time.eq(naive_now()),
249 diesel::update(person.find(person_id))
251 display_name.eq::<Option<String>>(None),
252 bio.eq::<Option<String>>(None),
253 matrix_user_id.eq::<Option<String>>(None),
255 updated.eq(naive_now()),
257 .get_result::<Self>(conn)
263 use crate::{establish_unpooled_connection, source::person::*};
267 let conn = establish_unpooled_connection();
269 let new_person = PersonForm {
270 name: "holly".into(),
271 ..PersonForm::default()
274 let inserted_person = Person::create(&conn, &new_person).unwrap();
276 let expected_person = Person {
277 id: inserted_person.id,
278 name: "holly".into(),
284 published: inserted_person.published,
286 actor_id: inserted_person.actor_id.to_owned(),
293 last_refreshed_at: inserted_person.published,
294 inbox_url: inserted_person.inbox_url.to_owned(),
295 shared_inbox_url: None,
296 matrix_user_id: None,
299 let read_person = Person::read(&conn, inserted_person.id).unwrap();
300 let updated_person = Person::update(&conn, inserted_person.id, &new_person).unwrap();
301 let num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
303 assert_eq!(expected_person, read_person);
304 assert_eq!(expected_person, inserted_person);
305 assert_eq!(expected_person, updated_person);
306 assert_eq!(1, num_deleted);