openserve/models/mod.rs
1//! Models Module
2//!
3//! This module defines the primary data structures used throughout the application.
4//! These models are used for serialization, database mapping, and API responses.
5
6pub mod ai;
7pub mod auth;
8pub mod file;
9pub mod search;
10
11pub use ai::*;
12pub use auth::*;
13
14pub use search::*;
15
16use serde::{Deserialize, Serialize};
17use chrono::{DateTime, Utc};
18
19/// Represents a file in the file system.
20///
21/// This struct contains metadata about a file that is used throughout
22/// the application for API responses and internal operations.
23#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct File {
25 /// Name of the file
26 pub name: String,
27 /// Full path to the file
28 pub path: String,
29 /// Size of the file in bytes
30 pub size: u64,
31 /// Last modified timestamp
32 pub modified: DateTime<Utc>,
33 /// MIME type of the file
34 pub mime_type: String,
35}
36
37/// Represents a directory in the file system.
38///
39/// This struct contains metadata about a directory and its contents,
40/// used for directory listing operations.
41#[derive(Debug, Clone, Serialize, Deserialize)]
42pub struct Directory {
43 /// Name of the directory
44 pub name: String,
45 /// Full path to the directory
46 pub path: String,
47 /// Last modified timestamp
48 pub modified: DateTime<Utc>,
49 /// Directory entries (files and subdirectories)
50 pub entries: Vec<crate::services::file::DirectoryEntry>,
51}
52
53/// Detailed metadata information about a file or directory.
54///
55/// This struct provides comprehensive metadata that extends beyond
56/// basic file information to include permissions and timestamps.
57#[derive(Debug, Clone, Serialize, Deserialize)]
58pub struct FileMetadata {
59 /// Size of the file in bytes
60 pub size: u64,
61 /// Last modified timestamp
62 pub modified: DateTime<Utc>,
63 /// Creation timestamp
64 pub created: DateTime<Utc>,
65 /// Whether this is a file
66 pub is_file: bool,
67 /// Whether this is a directory
68 pub is_directory: bool,
69 /// File permissions as string
70 pub permissions: String,
71}
72
73/// Represents a user account in the system.
74///
75/// This struct contains all the information needed to identify and
76/// authorize a user within the application.
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct User {
79 /// Unique user identifier
80 pub id: String,
81 /// Username for authentication
82 pub username: String,
83 /// User email address
84 pub email: String,
85 /// User role for authorization
86 pub role: UserRole,
87 /// Account creation timestamp
88 pub created_at: DateTime<Utc>,
89 /// Last login timestamp
90 pub last_login: Option<DateTime<Utc>>,
91}
92
93/// Defines the different user roles and their access levels.
94///
95/// This enum is used for authorization decisions throughout the application,
96/// determining what operations a user is allowed to perform.
97#[derive(Debug, Clone, Serialize, Deserialize)]
98pub enum UserRole {
99 /// Administrator with full access
100 Admin,
101 /// Regular user with standard access
102 User,
103 /// Read-only user with limited access
104 ReadOnly,
105}
106
107/// Represents a generic API response for success cases.
108///
109/// This structure is used to provide a consistent response format
110/// for successful API calls that return a JSON object.
111#[derive(Debug, Serialize, Deserialize)]
112pub struct ApiResponse<T> {
113 /// Indicates the success of the request.
114 pub success: bool,
115 /// The main data payload of the response.
116 #[serde(skip_serializing_if = "Option::is_none")]
117 pub data: Option<T>,
118 /// An optional message providing more information.
119 #[serde(skip_serializing_if = "Option::is_none")]
120 pub message: Option<String>,
121}
122
123impl<T> ApiResponse<T> {
124 /// Creates a new successful `ApiResponse` with data.
125 pub fn new(data: T, message: Option<String>) -> Self {
126 Self {
127 success: true,
128 data: Some(data),
129 message,
130 }
131 }
132}
133
134/// Represents a generic API response for error cases.
135///
136/// This structure is used to provide a consistent response format
137/// for failed API calls.
138#[derive(Debug, Serialize, Deserialize)]
139pub struct ErrorResponse {
140 /// Indicates the failure of the request.
141 pub success: bool,
142 /// A descriptive error message.
143 pub error: String,
144}
145
146/// WebSocket message types
147#[derive(Debug, Clone, Serialize, Deserialize)]
148#[serde(tag = "type")]
149pub enum WebSocketMessage {
150 /// File change notification
151 FileChanged {
152 /// Path of the changed file
153 path: String,
154 /// Type of change event
155 event: String
156 },
157 /// Directory change notification
158 DirectoryChanged {
159 /// Path of the changed directory
160 path: String,
161 /// Type of change event
162 event: String
163 },
164 /// Search results notification
165 SearchResults {
166 /// Search query that was executed
167 query: String,
168 /// List of search results
169 results: Vec<SearchResult>
170 },
171 /// Error notification
172 Error {
173 /// Error message
174 message: String
175 },
176}
177
178/// Request structure for file upload operations.
179///
180/// This struct defines the parameters needed to upload a file to the server,
181/// including the target location and upload behavior.
182#[derive(Debug, Clone, Serialize, Deserialize)]
183pub struct UploadRequest {
184 /// Target path for the upload
185 pub path: String,
186 /// Whether to overwrite existing files
187 pub overwrite: bool,
188}
189
190/// Upload response
191#[derive(Debug, Clone, Serialize, Deserialize)]
192pub struct UploadResponse {
193 /// Path where the file was uploaded
194 pub path: String,
195 /// Size of the uploaded file in bytes
196 pub size: u64,
197 /// Success message
198 pub message: String,
199}
200
201/// Request structure for search operations.
202///
203/// This struct defines the parameters for performing searches across
204/// the file system, including query terms and optional filters.
205#[derive(Debug, Clone, Serialize, Deserialize)]
206pub struct SearchRequest {
207 /// Search query string
208 pub query: String,
209 /// Maximum number of results to return
210 pub limit: Option<usize>,
211 /// Optional search filters
212 pub filters: Option<SearchFilters>,
213}
214
215/// Filtering options for search operations.
216///
217/// This struct allows users to narrow down search results based on
218/// various file attributes like type, size, and modification date.
219#[derive(Debug, Clone, Serialize, Deserialize)]
220pub struct SearchFilters {
221 /// Filter by MIME types
222 pub mime_types: Option<Vec<String>>,
223 /// Minimum file size in bytes
224 pub size_min: Option<u64>,
225 /// Maximum file size in bytes
226 pub size_max: Option<u64>,
227 /// Filter files modified after this date
228 pub modified_after: Option<DateTime<Utc>>,
229 /// Filter files modified before this date
230 pub modified_before: Option<DateTime<Utc>>,
231}
232
233/// Request structure for AI content analysis operations.
234///
235/// This struct defines the parameters needed to analyze file content
236/// using AI services, including the target file and analysis options.
237#[derive(Debug, Clone, Serialize, Deserialize)]
238pub struct AnalysisRequest {
239 /// Path of the file to analyze
240 pub path: String,
241 /// Analysis options
242 pub options: AnalysisOptions,
243}
244
245/// Configuration options for AI content analysis.
246///
247/// This struct allows users to specify which types of analysis
248/// should be performed on the content.
249#[derive(Debug, Clone, Serialize, Deserialize)]
250pub struct AnalysisOptions {
251 /// Whether to generate a summary
252 pub summarize: bool,
253 /// Whether to extract entities
254 pub extract_entities: bool,
255 /// Whether to generate tags
256 pub generate_tags: bool,
257 /// Whether to detect language
258 pub detect_language: bool,
259}
260
261impl Default for AnalysisOptions {
262 fn default() -> Self {
263 Self {
264 summarize: true,
265 extract_entities: false,
266 generate_tags: false,
267 detect_language: false,
268 }
269 }
270}
271
272/// Request structure for AI chat operations.
273///
274/// This struct defines the parameters for interacting with the AI
275/// chat functionality, including the message and optional context.
276#[derive(Debug, Clone, Serialize, Deserialize)]
277pub struct ChatRequest {
278 /// Chat message
279 pub message: String,
280 /// Optional context file paths
281 pub context_paths: Option<Vec<String>>,
282 /// Maximum context length
283 pub max_context_length: Option<usize>,
284}
285
286/// Response structure for AI chat operations.
287///
288/// This struct contains the AI's response along with metadata about
289/// the interaction, such as context used and token consumption.
290#[derive(Debug, Clone, Serialize, Deserialize)]
291pub struct ChatResponse {
292 /// AI response message
293 pub response: String,
294 /// Context files that were used
295 pub context_used: Vec<String>,
296 /// Number of tokens used in the request
297 pub tokens_used: Option<u32>,
298}