From a1a19e091a00644100e9475e88181c0158d79aec Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 20 Mar 2023 00:59:16 +0900 Subject: [PATCH] Define User::search_local_by_name() method --- plume-models/src/users.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index 76043baf..82b7ab19 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -15,7 +15,10 @@ use activitystreams::{ prelude::*, }; use chrono::{NaiveDateTime, Utc}; -use diesel::{self, BelongingToDsl, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl}; +use diesel::{ + self, BelongingToDsl, BoolExpressionMethods, ExpressionMethods, OptionalExtension, QueryDsl, + RunQueryDsl, TextExpressionMethods, +}; use ldap3::{LdapConn, Scope, SearchEntry}; use openssl::{ hash::MessageDigest, @@ -204,6 +207,27 @@ impl User { } } + pub fn search_local_by_name( + conn: &Connection, + name: &str, + (min, max): (i32, i32), + ) -> Result> { + users::table + .filter(users::instance_id.eq(Instance::get_local()?.id)) + .filter(users::role.ne(Role::Instance as i32)) + // TODO: use `ilike` instead of `like` for PostgreSQL + .filter( + users::username + .like(format!("%{}%", name)) + .or(users::display_name.like(format!("%{}%", name))), + ) + .order(users::username.asc()) + .offset(min.into()) + .limit((max - min).into()) + .load::(conn) + .map_err(Error::from) + } + /** * TODO: Should create user record with normalized(lowercased) email */