| 222 | } |
| 223 | |
| 224 | async fn global_search( |
| 225 | &self, |
| 226 | request: Request<api::GlobalSearchRequest>, |
| 227 | ) -> Result<Response<api::GlobalSearchResponse>, Status> { |
| 228 | let req = request.get_ref(); |
| 229 | self.validator |
| 230 | .validate(request.extensions(), validator::ValidateActiveUser::new()) |
| 231 | .await?; |
| 232 | |
| 233 | let auth_id = request.extensions().get::<AuthID>().unwrap(); |
| 234 | let user_id = match auth_id { |
| 235 | AuthID::User(id) => id, |
| 236 | _ => { |
| 237 | return Err(Status::unauthenticated("no user id")); |
| 238 | } |
| 239 | }; |
| 240 | |
| 241 | let u = user::get(user_id).await.map_err(|e| e.status())?; |
| 242 | |
| 243 | let items = search::global_search( |
| 244 | &u.id, |
| 245 | u.is_admin, |
| 246 | &req.search, |
| 247 | req.limit as usize, |
| 248 | req.offset as usize, |
| 249 | ) |
| 250 | .await |
| 251 | .map_err(|e| e.status())?; |
| 252 | |
| 253 | Ok(Response::new(api::GlobalSearchResponse { |
| 254 | result: items |
| 255 | .iter() |
| 256 | .map(|r| api::GlobalSearchResult { |
| 257 | kind: r.kind.clone(), |
| 258 | score: r.score, |
| 259 | tenant_id: match &r.tenant_id { |
| 260 | Some(v) => v.to_string(), |
| 261 | None => "".to_string(), |
| 262 | }, |
| 263 | tenant_name: match &r.tenant_name { |
| 264 | Some(v) => v.clone(), |
| 265 | None => "".to_string(), |
| 266 | }, |
| 267 | application_id: match &r.application_id { |
| 268 | Some(v) => v.to_string(), |
| 269 | None => "".to_string(), |
| 270 | }, |
| 271 | application_name: match &r.application_name { |
| 272 | Some(v) => v.clone(), |
| 273 | None => "".to_string(), |
| 274 | }, |
| 275 | device_dev_eui: match &r.device_dev_eui { |
| 276 | Some(v) => v.to_string(), |
| 277 | None => "".to_string(), |
| 278 | }, |
| 279 | device_name: match &r.device_name { |
| 280 | Some(v) => v.clone(), |
| 281 | None => "".to_string(), |