Skip to content

Commit

Permalink
jmap_contact.c: add support for phonetics
Browse files Browse the repository at this point in the history
  • Loading branch information
ksmurchison committed Aug 1, 2023
1 parent 7fbed29 commit e2f2d17
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 5 deletions.
89 changes: 89 additions & 0 deletions cassandane/tiny-tests/JMAPContacts/card-get-phonetics
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!perl
use Cassandane::Tiny;
use utf8;

sub test_card_get_phonetics
:min_version_3_9 :needs_component_jmap
{
my ($self) = @_;
my $jmap = $self->{jmap};

my $service = $self->{instance}->get_service("http");
$ENV{DEBUGDAV} = 1;
my $carddav = Net::CardDAVTalk->new(
user => 'cassandane',
password => 'pass',
host => $service->host(),
port => $service->port(),
scheme => 'http',
url => '/',
expandurl => 1,
);

my $id = 'ae2640cc-234a-4dd9-95cc-3106258445b9';
my $href = "Default/$id.vcf";
my $card = <<EOF;
BEGIN:VCARD
VERSION:4.0
UID:$id
LANGUAGE:zho-Hant
FN:孫中山文逸仙
N;ALTID=1;LANGUAGE=zho-Hant:孫;中山;文,逸仙;;
N;ALTID=1;PHONETIC=jyut;SCRIPT=Latn;LANGUAGE=yue:
syun1;zung1saan1;man4,jat6sin1;;
END:VCARD
EOF

$card =~ s/\r?\n/\r\n/gs;
$carddav->Request('PUT', $href, $card, 'Content-Type' => 'text/vcard');

my $res = $jmap->CallMethods([
['ContactCard/get', {
}, 'R1']
]);

my $want_jscard = {
'@type' => 'Card',
version => '1.0',
addressBookId => 'Default',
'cyrusimap.org:href' => $carddav->fullpath() . $href,
id => $id,
uid => $id,
language => 'zho-Hant',
vCardProps => [
[ 'version', {}, 'text', '4.0' ]
],
name => {
full => '孫中山文逸仙',
components => [
{ kind => 'surname', value => '' },
{ kind => 'given', value => '中山' },
{ kind => 'given2', value => '' },
{ kind => 'given2', value => '逸仙' }
]
},
localizations => {
yue => {
"name/phoneticSystem" => "jyut",
"name/phoneticScript" => "Latn",
"name/components/0/phonetic" => "syun1",
"name/components/1/phonetic" => "zung1saan1",
"name/components/2/phonetic" => "man4",
"name/components/3/phonetic" => "jat6sin1"
}
}
};

my $have_jscard = $res->[0][1]{list}[0];

# Delete generated fields
delete $have_jscard->{blobId};
delete $have_jscard->{'cyrusimap.org:blobId'};
delete $have_jscard->{'cyrusimap.org:size'};

# Normalize and compare cards
normalize_jscard($want_jscard);
normalize_jscard($have_jscard);

$self->assert_deep_equals($want_jscard, $have_jscard);
}
42 changes: 37 additions & 5 deletions imap/jmap_contact.c
Original file line number Diff line number Diff line change
Expand Up @@ -6226,12 +6226,43 @@ static void jscomps_from_vcard(json_t *obj, vcardproperty *prop,
vcardstructuredtype *st,
const struct comp_kind *comp_kinds)
{
vcardparameter *param =
vcardproperty_get_first_parameter(prop, VCARD_JSCOMPS_PARAMETER);
vcardparameter *param;
vcardstrarray *sa;
const char *val;
const char *val, *val_prop_name = "value";
size_t i;

param = vcardproperty_get_first_parameter(prop, VCARD_PHONETIC_PARAMETER);
if (param) {
vcardparameter_phonetic phonetic = vcardparameter_get_phonetic(param);
struct buf buf = BUF_INITIALIZER;

if (phonetic == VCARD_PHONETIC_X) {
buf_setcstr(&buf, vcardparameter_get_xvalue(param));
}
else {
buf_setcstr(&buf, vcardparameter_enum_to_string(phonetic));
buf_lcase(&buf);
}
json_object_set_new(obj, "phoneticSystem",
json_string(buf_cstring(&buf)));
buf_free(&buf);

val_prop_name = "phonetic";

/* Remove PHONETIC parameter */
vcardproperty_remove_parameter_by_ref(prop, param);
}

param = vcardproperty_get_first_parameter(prop, VCARD_SCRIPT_PARAMETER);
if (param) {
json_object_set_new(obj, "phoneticScript",
json_string(vcardparameter_get_script(param)));

/* Remove SCRIPT parameter */
vcardproperty_remove_parameter_by_ref(prop, param);
}

param = vcardproperty_get_first_parameter(prop, VCARD_JSCOMPS_PARAMETER);
if (param) {
/* Order components per JSCOMPS */
vcardstructuredtype *jscomps = vcardparameter_get_jscomps(param);
Expand Down Expand Up @@ -6275,7 +6306,8 @@ static void jscomps_from_vcard(json_t *obj, vcardproperty *prop,
"components", "[]"),
json_pack("{s:s s:o}",
"kind", kind,
"value", jmap_utf8string(val)));
val_prop_name,
jmap_utf8string(val)));
}
}

Expand Down Expand Up @@ -6304,7 +6336,7 @@ static void jscomps_from_vcard(json_t *obj, vcardproperty *prop,
"components", "[]"),
json_pack("{s:s s:o}",
"kind", ckind->name,
"value",
val_prop_name,
jmap_utf8string(val)));
}
}
Expand Down

0 comments on commit e2f2d17

Please sign in to comment.