eventky-app-specs 0.1.0

Eventky Data Model Specifications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
use crate::{
    common::timestamp,
    traits::{HasIdPath, TimestampId, Validatable},
    validation::{is_valid_hex_color, is_valid_timezone},
    EVENTKY_PATH, PUBLIC_PATH,
};
use serde::{Deserialize, Serialize};
use url::Url;

#[cfg(target_arch = "wasm32")]
use crate::traits::Json;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

#[cfg(feature = "openapi")]
use utoipa::ToSchema;

// Validation constants
const MIN_NAME_LENGTH: usize = 1;
const MAX_NAME_LENGTH: usize = 100;
const MAX_DESCRIPTION_LENGTH: usize = 10_000;
const MAX_AUTHORS: usize = 20;

/// Represents styled description with metadata for rich content
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct StyledDescription {
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub content: String, // Raw content
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub format: String, // Format type: "markdown", "html", "plain"
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub attachments: Option<Vec<String>>, // URI references to attached files
}

#[cfg(target_arch = "wasm32")]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl StyledDescription {
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(constructor))]
    pub fn new(content: String, format: String, attachments: Option<Vec<String>>) -> Self {
        StyledDescription {
            content,
            format,
            attachments,
        }
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn content(&self) -> String {
        self.content.clone()
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn format(&self) -> String {
        self.format.clone()
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn attachments(&self) -> Option<Vec<String>> {
        self.attachments.clone()
    }
}

/// Calendar container - collection of events
/// URI: /pub/eventky.app/calendars/:calendar_id
/// Where calendar_id is a timestamp-based ID (like events)
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct PubkyAppCalendar {
    // RFC 7986 - Calendar Properties
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub name: String, // REQUIRED - calendar display name
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub color: Option<String>, // CSS color value (hex format #RRGGBB)
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub image_uri: Option<String>, // Calendar image/logo URI (pubky:// or https)

    // RFC 5545 - Calendar Metadata
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub timezone: String, // REQUIRED - IANA timezone ID (e.g., "Europe/Zurich")
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub description: Option<String>, // Calendar description
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub url: Option<String>, // Calendar homepage/details URL
    pub created: Option<i64>, // Creation timestamp (Unix microseconds)

    // Versioning fields (like events) for edit tracking
    pub sequence: Option<i32>, // Version number, incremented on each edit
    pub last_modified: Option<i64>, // Last modification timestamp (Unix microseconds)

    // Pubky Extensions (all custom fields use x_pubky_ prefix)
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(skip))]
    pub x_pubky_authors: Option<Vec<String>>, // Pubky URIs of users who can add events to this calendar (only owner can edit calendar itself)
}

impl PubkyAppCalendar {
    /// Creates a new `PubkyAppCalendar` instance with required fields and sensible defaults.
    pub fn new(name: String, timezone: String) -> Self {
        let created = Some(timestamp());
        let last_modified = Some(timestamp());
        Self {
            name,
            timezone,
            // Sensible defaults for optional fields
            color: None,
            image_uri: None,
            description: None,
            url: None,
            created,
            x_pubky_authors: None,
            // Versioning fields (like events)
            sequence: Some(0),
            last_modified,
        }
        .sanitize()
    }

    /// Helper methods for setting optional fields
    pub fn with_color(mut self, color: String) -> Self {
        self.color = Some(color);
        self.sanitize()
    }

    pub fn with_description(mut self, description: String) -> Self {
        self.description = Some(description);
        self.sanitize()
    }

    pub fn with_image_uri(mut self, image_uri: String) -> Self {
        self.image_uri = Some(image_uri);
        self.sanitize()
    }

    pub fn with_url(mut self, url: String) -> Self {
        self.url = Some(url);
        self.sanitize()
    }

    pub fn with_authors(mut self, authors: Vec<String>) -> Self {
        self.x_pubky_authors = Some(authors);
        self.sanitize()
    }

    /// Set sequence number (for versioning)
    pub fn with_sequence(mut self, sequence: i32) -> Self {
        self.sequence = Some(sequence);
        self
    }

    /// Set last_modified timestamp (for versioning)
    pub fn with_last_modified(mut self, last_modified: i64) -> Self {
        self.last_modified = Some(last_modified);
        self
    }

    /// Increment sequence and update last_modified (call on edit)
    pub fn increment_version(mut self) -> Self {
        self.sequence = Some(self.sequence.unwrap_or(0) + 1);
        self.last_modified = Some(timestamp());
        self
    }
}

#[cfg(target_arch = "wasm32")]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
impl PubkyAppCalendar {
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn name(&self) -> String {
        self.name.clone()
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn color(&self) -> Option<String> {
        self.color.clone()
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn image_uri(&self) -> Option<String> {
        self.image_uri.clone()
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn timezone(&self) -> String {
        self.timezone.clone()
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn description(&self) -> Option<String> {
        self.description.clone()
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn url(&self) -> Option<String> {
        self.url.clone()
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter, js_name = "getCreated"))]
    pub fn created(&self) -> Option<i64> {
        self.created
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter))]
    pub fn x_pubky_authors(&self) -> Option<Vec<String>> {
        self.x_pubky_authors.clone()
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(getter, js_name = "getSequence"))]
    pub fn sequence(&self) -> Option<i32> {
        self.sequence
    }

    #[cfg_attr(
        target_arch = "wasm32",
        wasm_bindgen(getter, js_name = "getLastModified")
    )]
    pub fn last_modified(&self) -> Option<i64> {
        self.last_modified
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = fromJson))]
    pub fn from_json(js_value: &JsValue) -> Result<Self, String> {
        Self::import_json(js_value)
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen(js_name = toJson))]
    pub fn to_json(&self) -> Result<JsValue, String> {
        self.export_json()
    }
}

#[cfg(target_arch = "wasm32")]
impl Json for PubkyAppCalendar {}

// Use TimestampId (like events) instead of HashId
// This allows calendars to be edited while keeping the same ID
impl TimestampId for PubkyAppCalendar {}

impl HasIdPath for PubkyAppCalendar {
    const PATH_SEGMENT: &'static str = "calendars/";

    fn create_path(id: &str) -> String {
        [PUBLIC_PATH, EVENTKY_PATH, Self::PATH_SEGMENT, id].concat()
    }
}

impl Validatable for PubkyAppCalendar {
    fn sanitize(self) -> Self {
        // Sanitize name
        let name = self.name.trim().chars().take(MAX_NAME_LENGTH).collect();

        // Sanitize timezone
        let timezone = self.timezone.trim().to_string();

        // Sanitize description
        let description = self
            .description
            .map(|desc| desc.trim().chars().take(MAX_DESCRIPTION_LENGTH).collect());

        // Sanitize color (keep only valid hex colors)
        let color = self.color.and_then(|c| {
            let c = c.trim().to_lowercase();
            if is_valid_hex_color(&c) {
                Some(c)
            } else {
                None
            }
        });

        // Sanitize image_uri and url
        let image_uri = self.image_uri.and_then(|uri| match Url::parse(uri.trim()) {
            Ok(url) => Some(url.to_string()),
            Err(_) => None,
        });

        let url = self.url.and_then(|uri| match Url::parse(uri.trim()) {
            Ok(url) => Some(url.to_string()),
            Err(_) => None,
        });

        // Sanitize author identifiers - accepts both full pubky URIs and plain public keys
        let x_pubky_authors = self
            .x_pubky_authors
            .map(|authors| {
                authors
                    .into_iter()
                    .take(MAX_AUTHORS)
                    .filter_map(|author_value| {
                        let trimmed = author_value.trim();
                        // If it's a valid URL (full pubky URI), use it
                        if let Ok(url) = Url::parse(trimmed) {
                            return Some(url.to_string());
                        }
                        // If it looks like a public key (alphanumeric, reasonable length), accept it as-is
                        // Public keys are typically 52 characters (z-base-32 encoded)
                        if trimmed.len() >= 32
                            && trimmed.len() <= 64
                            && trimmed.chars().all(|c| c.is_alphanumeric())
                        {
                            return Some(trimmed.to_string());
                        }
                        None
                    })
                    .collect::<Vec<_>>()
            })
            .filter(|authors| !authors.is_empty());

        Self {
            name,
            color,
            image_uri,
            timezone,
            description,
            url,
            created: self.created,
            x_pubky_authors,
            sequence: self.sequence,
            last_modified: self.last_modified,
        }
    }

    fn validate(&self, id: Option<&str>) -> Result<(), String> {
        // Validate the calendar ID
        if let Some(id) = id {
            self.validate_id(id)?;
        }

        // Validate name
        let name_length = self.name.chars().count();
        if !(MIN_NAME_LENGTH..=MAX_NAME_LENGTH).contains(&name_length) {
            return Err(
                "Validation Error: Calendar name length must be between 1 and 100 characters"
                    .into(),
            );
        }

        // Validate timezone
        if self.timezone.trim().is_empty() {
            return Err("Validation Error: Timezone is required".into());
        }

        if !is_valid_timezone(&self.timezone) {
            return Err(
                "Validation Error: Invalid timezone format. Must be a valid IANA timezone ID"
                    .into(),
            );
        }

        // Validate description length
        if let Some(desc) = &self.description {
            if desc.chars().count() > MAX_DESCRIPTION_LENGTH {
                return Err("Validation Error: Description exceeds maximum length".into());
            }
        }

        // Validate color format
        if let Some(color) = &self.color {
            if !is_valid_hex_color(color) {
                return Err("Validation Error: Color must be a valid hex color (#RRGGBB)".into());
            }
        }

        // Validate author count
        if let Some(authors) = &self.x_pubky_authors {
            if authors.len() > MAX_AUTHORS {
                return Err("Validation Error: Too many calendar authors".into());
            }
        }

        // Additional validations can be added here
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::traits::Validatable;

    #[test]
    fn test_new() {
        let calendar =
            PubkyAppCalendar::new("Work Calendar".to_string(), "Europe/Zurich".to_string())
                .with_color("#3498db".to_string())
                .with_description("My work events".to_string());

        assert_eq!(calendar.name, "Work Calendar");
        assert_eq!(calendar.timezone, "Europe/Zurich");
        assert_eq!(calendar.color, Some("#3498db".to_string()));
        assert_eq!(calendar.description, Some("My work events".to_string()));
        assert!(calendar.created.is_some());

        // Check that created timestamp is recent
        let now = timestamp();
        assert!(calendar.created.unwrap() <= now && calendar.created.unwrap() >= now - 1_000_000);
    }

    #[test]
    fn test_create_id() {
        let calendar =
            PubkyAppCalendar::new("Work Calendar".to_string(), "Europe/Zurich".to_string())
                .with_color("#3498db".to_string())
                .with_description("My work events".to_string());

        let calendar_id = calendar.create_id();
        println!("Generated Calendar ID: {}", calendar_id);

        // The ID should not be empty
        assert!(!calendar_id.is_empty());
    }

    #[test]
    fn test_create_path() {
        let calendar =
            PubkyAppCalendar::new("Work Calendar".to_string(), "Europe/Zurich".to_string());

        let calendar_id = calendar.create_id();
        let path = PubkyAppCalendar::create_path(&calendar_id);

        // Check if the path starts with the expected prefix
        let prefix = format!("{}{}calendars/", PUBLIC_PATH, EVENTKY_PATH);
        assert!(path.starts_with(&prefix));

        let expected_path_len = prefix.len() + calendar_id.len();
        assert_eq!(path.len(), expected_path_len);
    }

    #[test]
    fn test_validate_valid() {
        let calendar =
            PubkyAppCalendar::new("Work Calendar".to_string(), "Europe/Zurich".to_string())
                .with_color("#3498db".to_string())
                .with_description("My work events".to_string());

        let id = calendar.create_id();
        let result = calendar.validate(Some(&id));
        assert!(result.is_ok());
    }

    #[test]
    fn test_validate_invalid_name() {
        let calendar = PubkyAppCalendar::new(
            "".to_string(), // Empty name
            "Europe/Zurich".to_string(),
        );

        let id = calendar.create_id();
        let result = calendar.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Calendar name length"));
    }

    #[test]
    fn test_validate_invalid_timezone() {
        let calendar = PubkyAppCalendar::new(
            "Work Calendar".to_string(),
            "invalid_timezone".to_string(), // Invalid timezone
        );

        let id = calendar.create_id();
        let result = calendar.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Invalid timezone format"));
    }

    #[test]
    fn test_validate_invalid_color() {
        let mut calendar =
            PubkyAppCalendar::new("Work Calendar".to_string(), "Europe/Zurich".to_string());

        // Manually set invalid color after creation to bypass sanitization
        calendar.color = Some("invalid_color".to_string());

        let id = calendar.create_id();
        let result = calendar.validate(Some(&id));
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("valid hex color"));
    }

    #[test]
    fn test_sanitize() {
        let calendar = PubkyAppCalendar::new(
            "  Work Calendar  ".to_string(),
            " Europe/Zurich ".to_string(),
        )
        .with_color("  #3498DB  ".to_string())
        .with_image_uri("invalid_url".to_string())
        .with_description("  Description  ".to_string())
        .with_authors(vec!["invalid_uri".to_string()]);

        assert_eq!(calendar.name, "Work Calendar");
        assert_eq!(calendar.timezone, "Europe/Zurich");
        assert_eq!(calendar.color, Some("#3498db".to_string()));
        assert!(calendar.image_uri.is_none()); // Invalid URL sanitized out
        assert_eq!(calendar.description, Some("Description".to_string()));
        assert!(calendar.x_pubky_authors.is_none()); // Invalid URIs filtered out
    }

    #[test]
    fn test_try_from_valid() {
        let calendar_json = r##"
        {
            "name": "Work Calendar",
            "color": "#3498db",
            "image_uri": null,
            "timezone": "Europe/Zurich",
            "description": "My work events",
            "url": null,
            "created": 1700000000,
            "x_pubky_authors": null
        }
        "##;

        let calendar =
            PubkyAppCalendar::new("Work Calendar".to_string(), "Europe/Zurich".to_string())
                .with_color("#3498db".to_string())
                .with_description("My work events".to_string());
        let id = calendar.create_id();

        let blob = calendar_json.as_bytes();
        let calendar_parsed = <PubkyAppCalendar as Validatable>::try_from(blob, &id).unwrap();

        assert_eq!(calendar_parsed.name, "Work Calendar");
        assert_eq!(calendar_parsed.timezone, "Europe/Zurich");
        assert_eq!(calendar_parsed.color, Some("#3498db".to_string()));
        assert_eq!(
            calendar_parsed.description,
            Some("My work events".to_string())
        );
    }
}