| 1 | package JMAP::Validation::Checks::Message; | ||
| 2 | |||
| 3 | use Test2::Bundle::Extended; | ||
| 4 | |||
| 5 | # record types {{{ | ||
| 6 | |||
| 7 | my %Message_checks = ( | ||
| 8 | id => $JMAP::Validation::Checks::String::is_id, | ||
| 9 | blobId => $JMAP::Validation::Checks::String::is_string, | ||
| 10 | threadId => $JMAP::Validation::Checks::String::is_string, | ||
| 11 | mailboxIds => array { all_items $JMAP::Validation::Checks::String::is_string }, | ||
| 12 | inReplyToMessageId => in_set($JMAP::Validation::Checks::String::is_string, U()), | ||
| 13 | isUnread => $JMAP::Validation::Checks::Boolean::is_boolean, | ||
| 14 | isFlagged => $JMAP::Validation::Checks::Boolean::is_boolean, | ||
| 15 | isAnswered => $JMAP::Validation::Checks::Boolean::is_boolean, | ||
| 16 | isDraft => $JMAP::Validation::Checks::Boolean::is_boolean, | ||
| 17 | hasAttachment => $JMAP::Validation::Checks::Boolean::is_boolean, | ||
| 18 | headers => hash { all_items $JMAP::Validation::Checks::String::is_string }, | ||
| 19 | sender => in_set($JMAP::Validation::Checks::Emaler::is_Emailer, U()), | ||
| 20 | from => in_set(array { all_items $JMAP::Validation::Checks::Emaler::is_Emailer }, U()), | ||
| 21 | to => in_set(array { all_items $JMAP::Validation::Checks::Emaler::is_Emailer }, U()), | ||
| 22 | cc => in_set(array { all_items $JMAP::Validation::Checks::Emaler::is_Emailer }, U()), | ||
| 23 | bcc => in_set(array { all_items $JMAP::Validation::Checks::Emaler::is_Emailer }, U()), | ||
| 24 | replyTo => in_set(array { all_items $JMAP::Validation::Checks::Emaler::is_Emailer }, U()), | ||
| 25 | subject => $JMAP::Validation::Checks::String::is_string, | ||
| 26 | date => $JMAP::Validation::Checks::String::is_datetime, | ||
| 27 | size => $JMAP::Validation::Checks::Number::is_number, | ||
| 28 | |||
| 29 | preview => check_set( | ||
| 30 | $JMAP::Validation::Checks::String::is_string, | ||
| 31 | $JMAP::Validation::Checks::String::has_at_most_256_bytes, | ||
| 32 | ); | ||
| 33 | |||
| 34 | textBody => in_set($JMAP::Validation::Checks::String::is_string, U()), | ||
| 35 | htmlBody => in_set($JMAP::Validation::Checks::String::is_string, U()), | ||
| 36 | attachments => in_set(array { all_items $JMAP::Validation::Checks::Attachment::is_Attachment }, U()), | ||
| 37 | |||
| 38 | # TODO: attachedMessages needs to be recursive | ||
| 39 | ); | ||
| 40 | |||
| 41 | our $is_Message = hash { | ||
| 42 | (field $_ => $Message_checks{$_}) for qw{ | ||
| 43 | id | ||
| 44 | blobId | ||
| 45 | threadId | ||
| 46 | mailboxIds | ||
| 47 | inReplyToMessageId | ||
| 48 | isUnread | ||
| 49 | isFlagged | ||
| 50 | isAnswered | ||
| 51 | isDraft | ||
| 52 | hasAttachment | ||
| 53 | headers | ||
| 54 | sender | ||
| 55 | }; | ||
| 56 | |||
| 57 | # TODO: attachedMessages | ||
| 58 | # TODO: end() can't happen until attachedMessages | ||
| 59 | }; | ||
| 60 | |||
| 61 | # }}} | ||
| 62 | |||
| 63 | # requests {{{ | ||
| 64 | |||
| 65 | our $getMessages_args = hash { | ||
| 66 | field accountId => $JMAP::Validation::Checks::String::is_string_or_null; | ||
| 67 | field ids => $JMAP::Validation::Checks::String::is_array_of_ids; | ||
| 68 | field properties => in_set( | ||
| 69 | array { | ||
| 70 | filter_items { exists $Message_checks{$_} }; | ||
| 71 | filter_items { /^(?:body|headers\.[.]+)$/ }; | ||
| 72 | end() | ||
| 73 | }, | ||
| 74 | U(), | ||
| 75 | ); | ||
| 76 | end(); | ||
| 77 | }; | ||
| 78 | |||
| 79 | our $setMessages_args = hash { | ||
| 80 | field accountId => $JMAP::Validation::Checks::String::is_string_or_null; | ||
| 81 | field ifInState => $JMAP::Validation::Checks::String::is_string_or_null; | ||
| 82 | |||
| 83 | field create => in_set( | ||
| 84 | hash { | ||
| 85 | all_keys $JMAP::Validation::Checks::String::is_id; | ||
| 86 | all_values $is_Message_for_create; | ||
| 87 | }, | ||
| 88 | U(), | ||
| 89 | ); | ||
| 90 | |||
| 91 | field update => in_set( | ||
| 92 | hash { | ||
| 93 | all_keys $JMAP::Validation::Checks::String::is_id; | ||
| 94 | all_values $is_Message_for_update; | ||
| 95 | }, | ||
| 96 | U(), | ||
| 97 | ); | ||
| 98 | |||
| 99 | field destroy => in_set(array { all_items $JMAP::Validation::Checks::String::is_id }, U()); | ||
| 100 | end(); | ||
| 101 | }; | ||
| 102 | |||
| 103 | # }}} | ||
| 104 | |||
| 105 | # response types {{{ | ||
| 106 | |||
| 107 | our $is_messages = hash { | ||
| 108 | field accountId => $JMAP::Validation::Checks::String::is_id; | ||
| 109 | field state => $JMAP::Validation::Checks::String::is_string; | ||
| 110 | field list => array { all_items $isMessage }; | ||
| 111 | field notFound => in_set(array { all_items $JMAP::Validation::Checks::String::is_array_of_ids }, U()); | ||
| 112 | end(); | ||
| 113 | }; | ||
| 114 | |||
| 115 | # }}} | ||
| 116 | |||
| 117 | 1; |