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};
33 impl ToSafe for Person {
34 type SafeColumns = Columns;
35 fn safe_columns_tuple() -> Self::SafeColumns {
57 mod safe_type_alias_1 {
59 use lemmy_db_schema::{schema::person_alias_1::columns::*, source::person::PersonAlias1};
79 impl ToSafe for PersonAlias1 {
80 type SafeColumns = Columns;
81 fn safe_columns_tuple() -> Self::SafeColumns {
103 mod safe_type_alias_2 {
105 use lemmy_db_schema::{schema::person_alias_2::columns::*, source::person::PersonAlias2};
125 impl ToSafe for PersonAlias2 {
126 type SafeColumns = Columns;
127 fn safe_columns_tuple() -> Self::SafeColumns {
149 impl Crud<PersonForm, PersonId> for Person {
150 fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
152 .filter(deleted.eq(false))
156 fn delete(conn: &PgConnection, person_id: PersonId) -> Result<usize, Error> {
157 diesel::delete(person.find(person_id)).execute(conn)
159 fn create(conn: &PgConnection, form: &PersonForm) -> Result<Self, Error> {
160 insert_into(person).values(form).get_result::<Self>(conn)
162 fn update(conn: &PgConnection, person_id: PersonId, form: &PersonForm) -> Result<Self, Error> {
163 diesel::update(person.find(person_id))
165 .get_result::<Self>(conn)
169 impl ApubObject<PersonForm> for Person {
170 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
171 use lemmy_db_schema::schema::person::dsl::*;
173 .filter(deleted.eq(false))
174 .filter(actor_id.eq(object_id))
178 fn upsert(conn: &PgConnection, person_form: &PersonForm) -> Result<Person, Error> {
181 .on_conflict(actor_id)
184 .get_result::<Self>(conn)
189 fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Person, Error>;
190 fn find_by_name(conn: &PgConnection, name: &str) -> Result<Person, Error>;
191 fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
192 fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
195 impl Person_ for Person {
196 fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Self, Error> {
197 diesel::update(person.find(person_id))
199 .get_result::<Self>(conn)
202 fn find_by_name(conn: &PgConnection, from_name: &str) -> Result<Person, Error> {
204 .filter(deleted.eq(false))
205 .filter(local.eq(true))
206 .filter(name.ilike(from_name))
207 .first::<Person>(conn)
210 fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error> {
211 diesel::update(person.find(person_id))
212 .set((last_refreshed_at.eq(naive_now()),))
213 .get_result::<Self>(conn)
216 fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error> {
217 use lemmy_db_schema::schema::local_user;
219 // Set the local user info to none
220 diesel::update(local_user::table.filter(local_user::person_id.eq(person_id)))
221 .set((local_user::email.eq::<Option<String>>(None),))
224 diesel::update(person.find(person_id))
226 preferred_username.eq::<Option<String>>(None),
227 bio.eq::<Option<String>>(None),
228 matrix_user_id.eq::<Option<String>>(None),
230 updated.eq(naive_now()),
232 .get_result::<Self>(conn)
238 use crate::{establish_unpooled_connection, source::person::*};
242 let conn = establish_unpooled_connection();
244 let new_person = PersonForm {
245 name: "holly".into(),
246 preferred_username: None,
258 last_refreshed_at: None,
260 shared_inbox_url: None,
261 matrix_user_id: None,
264 let inserted_person = Person::create(&conn, &new_person).unwrap();
266 let expected_person = Person {
267 id: inserted_person.id,
268 name: "holly".into(),
269 preferred_username: None,
274 published: inserted_person.published,
276 actor_id: inserted_person.actor_id.to_owned(),
281 last_refreshed_at: inserted_person.published,
282 inbox_url: inserted_person.inbox_url.to_owned(),
283 shared_inbox_url: None,
284 matrix_user_id: None,
287 let read_person = Person::read(&conn, inserted_person.id).unwrap();
288 let updated_person = Person::update(&conn, inserted_person.id, &new_person).unwrap();
289 let num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
291 assert_eq!(expected_person, read_person);
292 assert_eq!(expected_person, inserted_person);
293 assert_eq!(expected_person, updated_person);
294 assert_eq!(1, num_deleted);