Skip to content

Commit

Permalink
jmap_contact.c: accept "created"/"updated" as given but add is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
ksmurchison committed Aug 11, 2023
1 parent c35f758 commit 625fe4e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 18 deletions.
2 changes: 2 additions & 0 deletions cassandane/tiny-tests/JMAPContacts/card-set-create-addresses
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ sub test_card_set_create_addresses
]);

$self->assert_not_null($res->[0][1]{created}{1});
$self->assert_not_null($res->[0][1]{created}{1}{created});

$res = $carddav->Request('GET', $href, '',
'Accept' => 'text/vcard; version=4.0');
Expand All @@ -127,5 +128,6 @@ sub test_card_set_create_addresses

$self->assert_matches(qr/ADR;JSCOMPS="s,\^n;11;s, ;10;3;s,\\, ;4;5;6";PROP-ID=k23;TYPE=WORK;CC=US;LABEL=54321 Oak St\^nReston\^nVA\^n20190\^nUSA:;;54321,Oak St;Reston;VA;20190;USA;;;;Oak St;54321;;;;;;/, $card);
$self->assert_matches(qr/ADR;PROP-ID=k24;TYPE=HOME;TZ=America\/New_York;CC=US;LABEL=12345 Elm St\^nReston\^nVA\^n20190\^nUSA:;;12345,Elm St;Reston;VA;20190;USA;;;;Elm St;12345;;;;;;/, $card);
$self->assert_matches(qr/CREATED:/, $card);
$self->assert_does_not_match(qr|JSPROP|, $card);
}
1 change: 1 addition & 0 deletions cassandane/tiny-tests/JMAPContacts/card-set-create-basic
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ sub test_card_set_create_basic
]);

$self->assert_not_null($res->[0][1]{created}{1});
$self->assert_not_null($res->[0][1]{created}{1}{id});

$res = $carddav->Request('GET', $href, '',
'Accept' => 'text/vcard; version=4.0');
Expand Down
2 changes: 2 additions & 0 deletions cassandane/tiny-tests/JMAPContacts/card-set-update
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ sub test_card_set_update
]);

$self->assert_not_null($res->[0][1]{updated}{$id});
$self->assert_not_null($res->[0][1]{updated}{$id}{updated});

$res = $carddav->Request('GET', $href, '',
'Accept' => 'text/vcard; version=4.0');
Expand All @@ -157,6 +158,7 @@ sub test_card_set_update
$self->assert_matches(qr/UID:$id/, $card);
$self->assert_matches(qr/NICKNAME;PROP-ID=foo:Doey/, $card);
$self->assert_matches(qr/CATEGORIES:foo/, $card);
$self->assert_matches(qr/REV:/, $card);

$res = $jmap->CallMethods([
['ContactCard/set', {
Expand Down
20 changes: 2 additions & 18 deletions imap/carddav_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -1498,25 +1498,9 @@ EXPORTED int carddav_store_x(struct mailbox *mailbox, vcardcomponent *vcard,
const char *userid, struct auth_state *authstate,
int ignorequota, uint32_t oldsize)
{
vcardtimetype now = vcardtime_current_utc_time();
vcardproperty *prop;

/* set the REVision time */
prop = vcardcomponent_get_first_property(vcard, VCARD_REV_PROPERTY);
if (prop) {
vcardproperty_set_rev(prop, now);
}
else {
prop = vcardproperty_new_rev(now);
vcardcomponent_add_property(vcard, prop);
}

/* get important properties */
prop = vcardcomponent_get_first_property(vcard, VCARD_UID_PROPERTY);
const char *uid = vcardproperty_get_uid(prop);

prop = vcardcomponent_get_first_property(vcard, VCARD_FN_PROPERTY);
const char *fullname = vcardproperty_get_fn(prop);
const char *uid = vcardcomponent_get_uid(vcard);
const char *fullname = vcardcomponent_get_fn(vcard);

/* serialize the card */
struct buf *buf = vcard_as_buf_x(vcard);
Expand Down
25 changes: 25 additions & 0 deletions imap/jmap_contact.c
Original file line number Diff line number Diff line change
Expand Up @@ -10628,6 +10628,17 @@ static int _card_set_create(jmap_req_t *req,
media = json_deep_copy(json_object_get(jcard, "media"));
keys = json_deep_copy(json_object_get(jcard, "cryptoKeys"));

if (!json_object_get(jcard, "created")) {
/* set the CREATED time */
char datestr[ISO8601_DATETIME_MAX+1] = "";
time_t now = time(NULL);

time_to_iso8601(now, datestr, sizeof(datestr), 1);

json_object_set_new(jcard, "created", json_string(datestr));
json_object_set_new(item, "created", json_string(datestr));
}

const char *name = NULL;
jmap_readprop(json_object_get(jcard, "name"),
"full", 0, invalid, "s", &name);
Expand Down Expand Up @@ -10845,6 +10856,9 @@ static int _card_set_update(jmap_req_t *req, unsigned kind,
IGNORE_VCARD_VERSION | IGNORE_DERIVED_PROPS);
vcardcomponent_free(vcard);

/* Remove old "updated" */
json_object_del(old_obj, "updated");

/* Apply the patch as provided */
json_t *new_obj = jmap_patchobject_apply(old_obj, jcard, invalid, 0);

Expand All @@ -10866,6 +10880,17 @@ static int _card_set_update(jmap_req_t *req, unsigned kind,

*item = json_object();

if (!json_object_get(new_obj, "updated")) {
/* set the REVision time */
char datestr[ISO8601_DATETIME_MAX+1] = "";
time_t now = time(NULL);

time_to_iso8601(now, datestr, sizeof(datestr), 1);

json_object_set_new(new_obj, "updated", json_string(datestr));
json_object_set_new(*item, "updated", json_string(datestr));
}

r = _jscard_to_vcard(req, cdata, mailbox_name(*mailbox), vcard,
new_obj, &annots, &blobs, errors);
json_decref(new_obj);
Expand Down

0 comments on commit 625fe4e

Please sign in to comment.