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<PersonForm, PersonId> for Person {
162 fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
164 .filter(deleted.eq(false))
168 fn delete(conn: &PgConnection, person_id: PersonId) -> Result<usize, Error> {
169 diesel::delete(person.find(person_id)).execute(conn)
171 fn create(conn: &PgConnection, form: &PersonForm) -> Result<Self, Error> {
172 insert_into(person).values(form).get_result::<Self>(conn)
174 fn update(conn: &PgConnection, person_id: PersonId, form: &PersonForm) -> Result<Self, Error> {
175 diesel::update(person.find(person_id))
177 .get_result::<Self>(conn)
181 impl ApubObject<PersonForm> for Person {
182 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
183 use lemmy_db_schema::schema::person::dsl::*;
185 .filter(deleted.eq(false))
186 .filter(actor_id.eq(object_id))
190 fn upsert(conn: &PgConnection, person_form: &PersonForm) -> Result<Person, Error> {
193 .on_conflict(actor_id)
196 .get_result::<Self>(conn)
201 fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Person, Error>;
202 fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> Result<Person, Error>;
203 fn find_by_name(conn: &PgConnection, name: &str) -> Result<Person, Error>;
204 fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
205 fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
208 impl Person_ for Person {
209 fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Self, Error> {
210 diesel::update(person.find(person_id))
212 .get_result::<Self>(conn)
215 fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> Result<Self, Error> {
216 diesel::update(person.find(person_id))
217 .set(admin.eq(added))
218 .get_result::<Self>(conn)
221 fn find_by_name(conn: &PgConnection, from_name: &str) -> Result<Person, Error> {
223 .filter(deleted.eq(false))
224 .filter(local.eq(true))
225 .filter(name.ilike(from_name))
226 .first::<Person>(conn)
229 fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error> {
230 diesel::update(person.find(person_id))
231 .set((last_refreshed_at.eq(naive_now()),))
232 .get_result::<Self>(conn)
235 fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error> {
236 use lemmy_db_schema::schema::local_user;
238 // Set the local user info to none
239 diesel::update(local_user::table.filter(local_user::person_id.eq(person_id)))
241 local_user::email.eq::<Option<String>>(None),
242 local_user::validator_time.eq(naive_now()),
246 diesel::update(person.find(person_id))
248 display_name.eq::<Option<String>>(None),
249 bio.eq::<Option<String>>(None),
250 matrix_user_id.eq::<Option<String>>(None),
252 updated.eq(naive_now()),
254 .get_result::<Self>(conn)
260 use crate::{establish_unpooled_connection, source::person::*};
264 let conn = establish_unpooled_connection();
266 let new_person = PersonForm {
267 name: "holly".into(),
268 ..PersonForm::default()
271 let inserted_person = Person::create(&conn, &new_person).unwrap();
273 let expected_person = Person {
274 id: inserted_person.id,
275 name: "holly".into(),
281 published: inserted_person.published,
283 actor_id: inserted_person.actor_id.to_owned(),
290 last_refreshed_at: inserted_person.published,
291 inbox_url: inserted_person.inbox_url.to_owned(),
292 shared_inbox_url: None,
293 matrix_user_id: None,
296 let read_person = Person::read(&conn, inserted_person.id).unwrap();
297 let updated_person = Person::update(&conn, inserted_person.id, &new_person).unwrap();
298 let num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
300 assert_eq!(expected_person, read_person);
301 assert_eq!(expected_person, inserted_person);
302 assert_eq!(expected_person, updated_person);
303 assert_eq!(1, num_deleted);