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};
34 impl ToSafe for Person {
35 type SafeColumns = Columns;
36 fn safe_columns_tuple() -> Self::SafeColumns {
59 mod safe_type_alias_1 {
61 use lemmy_db_schema::{schema::person_alias_1::columns::*, source::person::PersonAlias1};
82 impl ToSafe for PersonAlias1 {
83 type SafeColumns = Columns;
84 fn safe_columns_tuple() -> Self::SafeColumns {
107 mod safe_type_alias_2 {
109 use lemmy_db_schema::{schema::person_alias_2::columns::*, source::person::PersonAlias2};
130 impl ToSafe for PersonAlias2 {
131 type SafeColumns = Columns;
132 fn safe_columns_tuple() -> Self::SafeColumns {
155 impl Crud<PersonForm, PersonId> for Person {
156 fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
158 .filter(deleted.eq(false))
162 fn delete(conn: &PgConnection, person_id: PersonId) -> Result<usize, Error> {
163 diesel::delete(person.find(person_id)).execute(conn)
165 fn create(conn: &PgConnection, form: &PersonForm) -> Result<Self, Error> {
166 insert_into(person).values(form).get_result::<Self>(conn)
168 fn update(conn: &PgConnection, person_id: PersonId, form: &PersonForm) -> Result<Self, Error> {
169 diesel::update(person.find(person_id))
171 .get_result::<Self>(conn)
175 impl ApubObject<PersonForm> for Person {
176 fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
177 use lemmy_db_schema::schema::person::dsl::*;
179 .filter(deleted.eq(false))
180 .filter(actor_id.eq(object_id))
184 fn upsert(conn: &PgConnection, person_form: &PersonForm) -> Result<Person, Error> {
187 .on_conflict(actor_id)
190 .get_result::<Self>(conn)
195 fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Person, Error>;
196 fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> Result<Person, Error>;
197 fn find_by_name(conn: &PgConnection, name: &str) -> Result<Person, Error>;
198 fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
199 fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
202 impl Person_ for Person {
203 fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Self, Error> {
204 diesel::update(person.find(person_id))
206 .get_result::<Self>(conn)
209 fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> Result<Self, Error> {
210 diesel::update(person.find(person_id))
211 .set(admin.eq(added))
212 .get_result::<Self>(conn)
215 fn find_by_name(conn: &PgConnection, from_name: &str) -> Result<Person, Error> {
217 .filter(deleted.eq(false))
218 .filter(local.eq(true))
219 .filter(name.ilike(from_name))
220 .first::<Person>(conn)
223 fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error> {
224 diesel::update(person.find(person_id))
225 .set((last_refreshed_at.eq(naive_now()),))
226 .get_result::<Self>(conn)
229 fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error> {
230 use lemmy_db_schema::schema::local_user;
232 // Set the local user info to none
233 diesel::update(local_user::table.filter(local_user::person_id.eq(person_id)))
234 .set((local_user::email.eq::<Option<String>>(None),))
237 diesel::update(person.find(person_id))
239 preferred_username.eq::<Option<String>>(None),
240 bio.eq::<Option<String>>(None),
241 matrix_user_id.eq::<Option<String>>(None),
243 updated.eq(naive_now()),
245 .get_result::<Self>(conn)
251 use crate::{establish_unpooled_connection, source::person::*};
255 let conn = establish_unpooled_connection();
257 let new_person = PersonForm {
258 name: "holly".into(),
259 ..PersonForm::default()
262 let inserted_person = Person::create(&conn, &new_person).unwrap();
264 let expected_person = Person {
265 id: inserted_person.id,
266 name: "holly".into(),
267 preferred_username: None,
272 published: inserted_person.published,
274 actor_id: inserted_person.actor_id.to_owned(),
280 last_refreshed_at: inserted_person.published,
281 inbox_url: inserted_person.inbox_url.to_owned(),
282 shared_inbox_url: None,
283 matrix_user_id: None,
286 let read_person = Person::read(&conn, inserted_person.id).unwrap();
287 let updated_person = Person::update(&conn, inserted_person.id, &new_person).unwrap();
288 let num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
290 assert_eq!(expected_person, read_person);
291 assert_eq!(expected_person, inserted_person);
292 assert_eq!(expected_person, updated_person);
293 assert_eq!(1, num_deleted);