|
| 1 | +use crate::impls::dovewing::{get_platform_user, DovewingSource}; |
| 2 | +use crate::impls::target_types::TargetType; |
| 3 | +use crate::impls::utils::get_entity_managers; |
| 4 | +use crate::panelapi::auth::check_auth; |
| 5 | +use crate::panelapi::core::{AppState, Error}; |
| 6 | +use crate::panelapi::types::entity::{PartialBot, PartialEntity, PartialServer}; |
| 7 | +use axum::{ |
| 8 | + http::StatusCode, |
| 9 | + response::{IntoResponse, Response}, |
| 10 | + Json, |
| 11 | +}; |
| 12 | + |
| 13 | +pub async fn search_entitys( |
| 14 | + state: &AppState, |
| 15 | + login_token: String, |
| 16 | + target_type: TargetType, |
| 17 | + query: String, |
| 18 | +) -> Result<Response, Error> { |
| 19 | + check_auth(&state.pool, &login_token) |
| 20 | + .await |
| 21 | + .map_err(Error::new)?; |
| 22 | + |
| 23 | + match target_type { |
| 24 | + TargetType::Bot => { |
| 25 | + let queue = sqlx::query!( |
| 26 | + " |
| 27 | + SELECT bot_id, client_id, type, approximate_votes, shards, library, invite_clicks, clicks, |
| 28 | + servers, last_claimed, claimed_by, approval_note, short, invite FROM bots |
| 29 | + INNER JOIN internal_user_cache__discord discord_users ON bots.bot_id = discord_users.id |
| 30 | + WHERE bot_id = $1 OR client_id = $1 OR discord_users.username ILIKE $2 ORDER BY bots.created_at |
| 31 | + ", |
| 32 | + query, |
| 33 | + format!("%{}%", query) |
| 34 | + ) |
| 35 | + .fetch_all(&state.pool) |
| 36 | + .await |
| 37 | + .map_err(Error::new)?; |
| 38 | + |
| 39 | + let mut bots = Vec::new(); |
| 40 | + |
| 41 | + for bot in queue { |
| 42 | + let owners = get_entity_managers(TargetType::Bot, &bot.bot_id, &state.pool) |
| 43 | + .await |
| 44 | + .map_err(Error::new)?; |
| 45 | + |
| 46 | + let user = get_platform_user( |
| 47 | + &state.pool, |
| 48 | + DovewingSource::Discord(state.cache_http.clone()), |
| 49 | + &bot.bot_id, |
| 50 | + ) |
| 51 | + .await |
| 52 | + .map_err(Error::new)?; |
| 53 | + |
| 54 | + bots.push(PartialEntity::Bot(PartialBot { |
| 55 | + bot_id: bot.bot_id, |
| 56 | + client_id: bot.client_id, |
| 57 | + user, |
| 58 | + r#type: bot.r#type, |
| 59 | + votes: bot.approximate_votes, |
| 60 | + shards: bot.shards, |
| 61 | + library: bot.library, |
| 62 | + invite_clicks: bot.invite_clicks, |
| 63 | + clicks: bot.clicks, |
| 64 | + servers: bot.servers, |
| 65 | + claimed_by: bot.claimed_by, |
| 66 | + last_claimed: bot.last_claimed, |
| 67 | + approval_note: bot.approval_note, |
| 68 | + short: bot.short, |
| 69 | + mentionable: owners.mentionables(), |
| 70 | + invite: bot.invite, |
| 71 | + })); |
| 72 | + } |
| 73 | + |
| 74 | + Ok((StatusCode::OK, Json(bots)).into_response()) |
| 75 | + } |
| 76 | + TargetType::Server => { |
| 77 | + let queue = sqlx::query!( |
| 78 | + " |
| 79 | + SELECT server_id, name, total_members, online_members, short, type, approximate_votes, invite_clicks, |
| 80 | + clicks, nsfw, tags, premium, claimed_by, last_claimed FROM servers |
| 81 | + WHERE server_id = $1 OR name ILIKE $2 ORDER BY created_at |
| 82 | + ", |
| 83 | + query, |
| 84 | + format!("%{}%", query) |
| 85 | + ) |
| 86 | + .fetch_all(&state.pool) |
| 87 | + .await |
| 88 | + .map_err(Error::new)?; |
| 89 | + |
| 90 | + let mut servers = Vec::new(); |
| 91 | + |
| 92 | + for server in queue { |
| 93 | + let owners = |
| 94 | + get_entity_managers(TargetType::Server, &server.server_id, &state.pool) |
| 95 | + .await |
| 96 | + .map_err(Error::new)?; |
| 97 | + |
| 98 | + servers.push(PartialEntity::Server(PartialServer { |
| 99 | + server_id: server.server_id.clone(), |
| 100 | + name: server.name, |
| 101 | + avatar: format!( |
| 102 | + "{}/servers/avatars/{}.webp", |
| 103 | + crate::config::CONFIG.cdn_url, |
| 104 | + server.server_id |
| 105 | + ), |
| 106 | + total_members: server.total_members, |
| 107 | + online_members: server.online_members, |
| 108 | + short: server.short, |
| 109 | + r#type: server.r#type, |
| 110 | + votes: server.approximate_votes, |
| 111 | + invite_clicks: server.invite_clicks, |
| 112 | + clicks: server.clicks, |
| 113 | + nsfw: server.nsfw, |
| 114 | + tags: server.tags, |
| 115 | + premium: server.premium, |
| 116 | + claimed_by: server.claimed_by, |
| 117 | + last_claimed: server.last_claimed, |
| 118 | + mentionable: owners.mentionables(), |
| 119 | + })); |
| 120 | + } |
| 121 | + |
| 122 | + Ok((StatusCode::OK, Json(servers)).into_response()) |
| 123 | + } |
| 124 | + _ => Ok(( |
| 125 | + StatusCode::NOT_IMPLEMENTED, |
| 126 | + "Searching this target type is not implemented".to_string(), |
| 127 | + ) |
| 128 | + .into_response()), |
| 129 | + } |
| 130 | +} |
0 commit comments