Skip to content

Commit

Permalink
test(messages): revive messaging test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
andyholmes committed Oct 21, 2024
1 parent b9cd23c commit e29679c
Show file tree
Hide file tree
Showing 36 changed files with 1,188 additions and 830 deletions.
2 changes: 1 addition & 1 deletion REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SPDX-FileCopyrightText = "Andy Holmes <andrew.g.r.holmes@gmail.com>"
SPDX-License-Identifier = "GPL-3.0-or-later"

[[annotations]]
path = [".github/**", ".gitignore", ".gitmodules", "build-aux/**", "CHANGELOG.md", "CONTRIBUTING.md", "README.md", "**.ontology", "**.png", "**.svg", "**.vcf", "**.wrap"]
path = [".github/**", ".gitignore", ".gitmodules", "build-aux/**", "CHANGELOG.md", "CONTRIBUTING.md", "README.md", "**.jpg", "**.ontology", "**.png", "**.svg", "**.vcf", "**.wrap"]
precedence = "aggregate"
SPDX-FileCopyrightText = "No rights reserved"
SPDX-License-Identifier = "CC0-1.0"
Expand Down
35 changes: 23 additions & 12 deletions src/libvalent/contacts/valent-contacts.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ struct _ValentContacts
{
ValentComponent parent_instance;

GPtrArray *adapters;
/* list model */
GPtrArray *items;
};

static void g_list_model_iface_init (GListModelInterface *iface);
static void valent_contacts_unbind_extension (ValentComponent *component,
GObject *extension);
static void g_list_model_iface_init (GListModelInterface *iface);

G_DEFINE_FINAL_TYPE_WITH_CODE (ValentContacts, valent_contacts, VALENT_TYPE_COMPONENT,
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, g_list_model_iface_init))
Expand All @@ -53,10 +56,10 @@ valent_contacts_get_item (GListModel *list,

g_assert (VALENT_IS_CONTACTS (self));

if G_UNLIKELY (position >= self->adapters->len)
if G_UNLIKELY (position >= self->items->len)
return NULL;

return g_object_ref (g_ptr_array_index (self->adapters, position));
return g_object_ref (g_ptr_array_index (self->items, position));
}

static GType
Expand All @@ -72,7 +75,7 @@ valent_contacts_get_n_items (GListModel *list)

g_assert (VALENT_IS_CONTACTS (self));

return self->adapters->len;
return self->items->len;
}

static void
Expand All @@ -98,16 +101,22 @@ valent_contacts_bind_extension (ValentComponent *component,
g_assert (VALENT_IS_CONTACTS (self));
g_assert (VALENT_IS_CONTACTS_ADAPTER (extension));

if (g_ptr_array_find (self->adapters, extension, &position))
if (g_ptr_array_find (self->items, extension, &position))
{
g_warning ("Adapter \"%s\" already exported in \"%s\"",
G_OBJECT_TYPE_NAME (extension),
G_OBJECT_TYPE_NAME (component));
return;
}

position = self->adapters->len;
g_ptr_array_add (self->adapters, g_object_ref (extension));
g_signal_connect_object (extension,
"destroy",
G_CALLBACK (valent_contacts_unbind_extension),
self,
G_CONNECT_SWAPPED);

position = self->items->len;
g_ptr_array_add (self->items, g_object_ref (extension));
g_list_model_items_changed (G_LIST_MODEL (self), position, 0, 1);

VALENT_EXIT;
Expand All @@ -118,22 +127,24 @@ valent_contacts_unbind_extension (ValentComponent *component,
GObject *extension)
{
ValentContacts *self = VALENT_CONTACTS (component);
g_autoptr (ValentExtension) item = NULL;
unsigned int position = 0;

VALENT_ENTRY;

g_assert (VALENT_IS_CONTACTS (self));
g_assert (VALENT_IS_CONTACTS_ADAPTER (extension));

if (!g_ptr_array_find (self->adapters, extension, &position))
if (!g_ptr_array_find (self->items, extension, &position))
{
g_warning ("Adapter \"%s\" not found in \"%s\"",
G_OBJECT_TYPE_NAME (extension),
G_OBJECT_TYPE_NAME (component));
return;
}

g_ptr_array_remove (self->adapters, extension);
g_signal_handlers_disconnect_by_func (extension, valent_contacts_unbind_extension, self);
item = g_ptr_array_steal_index (self->items, position);
g_list_model_items_changed (G_LIST_MODEL (self), position, 1, 0);

VALENT_EXIT;
Expand All @@ -147,7 +158,7 @@ valent_contacts_finalize (GObject *object)
{
ValentContacts *self = VALENT_CONTACTS (object);

g_clear_pointer (&self->adapters, g_ptr_array_unref);
g_clear_pointer (&self->items, g_ptr_array_unref);

G_OBJECT_CLASS (valent_contacts_parent_class)->finalize (object);
}
Expand All @@ -167,7 +178,7 @@ valent_contacts_class_init (ValentContactsClass *klass)
static void
valent_contacts_init (ValentContacts *self)
{
self->adapters = g_ptr_array_new_with_free_func (g_object_unref);
self->items = g_ptr_array_new_with_free_func (g_object_unref);
}

/**
Expand Down
14 changes: 8 additions & 6 deletions src/libvalent/messages/data/sparql/get-message-attachments.rq
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ SELECT
?encodedThumbnail
?fileUri
WHERE {
BIND(IRI(xsd:string(~iri)) AS ?message)

?message nmo:hasAttachment ?attachment .
OPTIONAL { ?attachment rdf:type nfo:Attachment }
OPTIONAL { ?attachment vmo:encoded_thumbnail ?encodedThumbnail }
OPTIONAL { ?attachment nie:url ?fileUri }
GRAPH <valent:messages> {
BIND(IRI(xsd:string(~iri)) AS ?message)
?message rdf:type vmo:PhoneMessage ;
nmo:hasAttachment ?attachment .
OPTIONAL { ?attachment rdf:type nfo:Attachment }
OPTIONAL { ?attachment vmo:encoded_thumbnail ?encodedThumbnail }
OPTIONAL { ?attachment nie:url ?fileUri }
}
}

15 changes: 7 additions & 8 deletions src/libvalent/messages/data/sparql/get-message.rq
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,29 @@ SELECT
WHERE {
BIND(IRI(xsd:string(~iri)) AS ?message)
?message rdf:type vmo:PhoneMessage ;
vmo:phoneMessageBox ?phoneMessageBoxId ;
dc:date ?date ;
vmo:phoneMessageBox ?phoneMessageBox ;
vmo:phoneMessageId ?messageId ;
nmo:isRead ?read ;
vmo:communicationChannel ?communicationChannel ;
vmo:subscriptionId ?subscriptionId .
?communicationChannel vmo:communicationChannelId ?threadId .
vmo:subscriptionId ?subscriptionId ;
vmo:communicationChannel ?communicationChannel .
?phoneMessageBox vmo:phoneMessageBoxId ?box .
?communicationChannel vmo:communicationChannelId ?threadId .
OPTIONAL {
?message nmo:hasAttachment ?attachment .
OPTIONAL { ?attachment rdf:type nfo:Attachment }
OPTIONAL { ?attachment vmo:encoded_thumbnail ?encoded_thumbnail }
OPTIONAL { ?attachment nie:url ?fileUri }
}
OPTIONAL {
?message nmo:messageSender ?contactMedium .
OPTIONAL { ?contactMedium (nco:phoneNumber|nco:emailAddress) ?sender }
?message nmo:primaryMessageRecipient/(nco:phoneNumber|nco:emailAddress) ?recipient
}
OPTIONAL {
?message nmo:primaryMessageRecipient ?contactMedium .
OPTIONAL { ?contactMedium (nco:phoneNumber|nco:emailAddress) ?recipient }
?message nmo:messageSender/(nco:phoneNumber|nco:emailAddress) ?sender
}
OPTIONAL {
?message nmo:plainTextMessageContent ?text
}
}
GROUP BY ?message ?attachment

18 changes: 10 additions & 8 deletions src/libvalent/messages/data/sparql/get-thread-attachments.rq
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ SELECT
?encodedThumbnail
?fileUri
WHERE {
BIND(IRI(xsd:string(~iri)) AS ?thread)

?message vmo:communicationChannel ?thread ;
nmo:hasAttachment ?attachment .

OPTIONAL { ?attachment rdf:type nfo:Attachment }
OPTIONAL { ?attachment vmo:encoded_thumbnail ?encodedThumbnail }
OPTIONAL { ?attachment nie:url ?fileUri }
GRAPH <valent:messages> {
BIND(IRI(xsd:string(~iri)) AS ?communicationChannel)
?message rdf:type vmo:PhoneMessage ;
vmo:communicationChannel ?communicationChannel ;
nmo:hasAttachment ?attachment .
OPTIONAL { ?attachment rdf:type nfo:Attachment }
OPTIONAL { ?attachment vmo:encoded_thumbnail ?encodedThumbnail }
OPTIONAL { ?attachment nie:url ?fileUri }
}
}

1 change: 1 addition & 0 deletions src/libvalent/messages/data/sparql/get-thread-messages.rq
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ WHERE {
}
GROUP BY ?message ?attachment
ORDER BY ASC(?date)

17 changes: 8 additions & 9 deletions src/libvalent/messages/data/sparql/get-thread.rq
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Inputs: iri
# Outputs: message, box, date, messageId, read, recipients, sender, subscriptionId, text, threadId
# communicationChannel,
# communicationChannel, participants
SELECT
?message
?box
Expand All @@ -27,26 +27,25 @@ WHERE {
}
GROUP BY ?communicationChannel
}
FILTER(?date = ?latestDate)
?message rdf:type vmo:PhoneMessage ;
vmo:phoneMessageBox/vmo:phoneMessageBoxId ?box ;
dc:date ?date ;
vmo:phoneMessageBox ?phoneMessageBox ;
vmo:phoneMessageId ?messageId ;
nmo:isRead ?read ;
vmo:communicationChannel ?communicationChannel ;
vmo:subscriptionId ?subscriptionId .
vmo:subscriptionId ?subscriptionId ;
vmo:communicationChannel ?communicationChannel .
?communicationChannel rdf:type vmo:CommunicationChannel ;
vmo:communicationChannelId ?threadId ;
vmo:hasParticipant ?participant .
?phoneMessageBox vmo:phoneMessageBoxId ?box .
OPTIONAL {
?message nmo:messageSender ?contactMedium .
OPTIONAL { ?contactMedium (nco:phoneNumber|nco:emailAddress) ?sender }
?message nmo:primaryMessageRecipient/(nco:phoneNumber|nco:emailAddress) ?recipient
}
OPTIONAL {
?message nmo:primaryMessageRecipient ?contactMedium .
OPTIONAL { ?contactMedium (nco:phoneNumber|nco:emailAddress) ?recipient }
?message nmo:messageSender/(nco:phoneNumber|nco:emailAddress) ?sender
}
OPTIONAL {
?message nmo:plainTextMessageContent ?text
}
}

10 changes: 6 additions & 4 deletions src/libvalent/messages/data/sparql/get-timestamp.rq
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
# Outputs: date
SELECT (MAX(?date) AS ?latestDate)
WHERE {
?message rdf:type vmo:PhoneMessage ;
vmo:communicationChannel/vmo:communicationChannelId ?threadId ;
dc:date ?date .
FILTER(?threadId = ~threadId^^xsd:integer)
GRAPH <valent:messages> {
?message rdf:type vmo:PhoneMessage ;
vmo:communicationChannel/vmo:communicationChannelId ?threadId ;
dc:date ?date .
FILTER(?threadId = ~threadId^^xsd:integer)
}
}

10 changes: 5 additions & 5 deletions src/libvalent/messages/data/sparql/remove-thread.rq
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
# Outputs: None
DELETE
WHERE {
BIND(IRI(xsd:string(~iri)) AS ?channel)
?channel rdf:type vmo:CommunicationChannel ;
?channelProperty ?channelValue .
BIND(IRI(xsd:string(~iri)) AS ?communicationChannel)
?communicationChannel rdf:type vmo:CommunicationChannel ;
?channelProperty ?channelValue .
OPTIONAL { ?channel ?channelProperty ?channelValue . }
?message rdf:type vmo:PhoneMessage ;
vmo:communicationChannel ?channel ;
vmo:communicationChannel ?communicationChannel ;
?messageProperty ?messageValue .
OPTIONAL { ?message ?messageProperty ?messageValue . }
OPTIONAL { ?channel ?channelProperty ?channelValue . }
}

2 changes: 1 addition & 1 deletion src/libvalent/messages/data/sparql/search-messages.rq
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ WHERE {
?message nmo:plainTextMessageContent ?text
}
}
GROUP BY ?message
GROUP BY ?message ?attachment
ORDER BY DESC(fts:rank(?message))
LIMIT 5

47 changes: 21 additions & 26 deletions src/libvalent/messages/valent-message-thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,10 @@ on_notifier_event (TrackerNotifier *notifier,
VALENT_NOTE ("CREATE: %s", urn);
// HACK: if the thread hasn't been loaded, assume newer messages sort
// last and pick one to update the last-message.
if (self->cancellable == NULL)
{
if (latest_urn == NULL || g_utf8_collate (latest_urn, urn) < 0)
latest_urn = urn;
}
else
{
valent_message_thread_load_message (self, urn);
}
if (self->cancellable != NULL)
valent_message_thread_load_message (self, urn);
else if (latest_urn == NULL || g_utf8_collate (latest_urn, urn) < 0)
latest_urn = urn;
break;

case TRACKER_NOTIFIER_EVENT_DELETE:
Expand Down Expand Up @@ -323,6 +318,7 @@ valent_message_thread_load_cb (GObject *object,
ValentMessageThread *self = VALENT_MESSAGE_THREAD (object);
g_autoptr (GPtrArray) messages = NULL;
g_autoptr (GError) error = NULL;
unsigned int position = 0;

messages = g_task_propagate_pointer (G_TASK (result), &error);
if (messages == NULL)
Expand All @@ -333,10 +329,11 @@ valent_message_thread_load_cb (GObject *object,
return;
}

position = g_sequence_get_length (self->items);
for (unsigned int i = 0; i < messages->len; i++)
g_sequence_append (self->items, g_object_ref (g_ptr_array_index (messages, i)));

g_list_model_items_changed (G_LIST_MODEL (self), 0, 0, messages->len);
g_list_model_items_changed (G_LIST_MODEL (self), position, 0, messages->len);
}

static void
Expand Down Expand Up @@ -514,7 +511,7 @@ valent_message_thread_constructed (GObject *object)
g_object_get (VALENT_OBJECT (self), "iri", &self->iri, NULL);
if (self->connection != NULL)
{
g_autoptr (GString) iri_string = NULL;
g_autofree char *iri_pattern = NULL;

self->notifier = tracker_sparql_connection_create_notifier (self->connection);
g_signal_connect_object (self->notifier,
Expand All @@ -523,14 +520,8 @@ valent_message_thread_constructed (GObject *object)
self,
G_CONNECT_DEFAULT);

/* FIXME: this relies on IRIs use the pattern `/<thread-id>/<message-id>`,
* which may not be universally applicable
*/
iri_string = g_string_new (self->iri);
g_string_replace (iri_string, "/", "\\/", 0);
g_string_prepend_c (iri_string, '^');
g_string_append (iri_string, "\\/([^\\/]+)$");
self->iri_pattern = g_regex_new (iri_string->str,
iri_pattern = g_strdup_printf ("^%s:([^:]+)$", self->iri);
self->iri_pattern = g_regex_new (iri_pattern,
G_REGEX_OPTIMIZE,
G_REGEX_MATCH_DEFAULT,
NULL);
Expand All @@ -542,7 +533,17 @@ valent_message_thread_destroy (ValentObject *object)
{
ValentMessageThread *self = VALENT_MESSAGE_THREAD (object);

g_signal_handlers_disconnect_by_func (self->notifier, on_notifier_event, self);
g_clear_object (&self->get_message_stmt);
g_clear_object (&self->get_thread_messages_stmt);
g_clear_pointer (&self->iri_pattern, g_regex_unref);
g_clear_pointer (&self->iri, g_free);

if (self->notifier != NULL)
{
g_signal_handlers_disconnect_by_func (self->notifier, on_notifier_event, self);
g_clear_object (&self->notifier);
}
g_clear_object (&self->connection);

VALENT_OBJECT_CLASS (valent_message_thread_parent_class)->destroy (object);
}
Expand All @@ -552,14 +553,8 @@ valent_message_thread_finalize (GObject *object)
{
ValentMessageThread *self = VALENT_MESSAGE_THREAD (object);

g_clear_object (&self->connection);
g_clear_object (&self->latest_message);
g_clear_pointer (&self->participants, g_strfreev);
g_clear_pointer (&self->iri, g_free);
g_clear_object (&self->notifier);
g_clear_object (&self->get_message_stmt);
g_clear_object (&self->get_thread_messages_stmt);
g_clear_pointer (&self->iri_pattern, g_regex_unref);
g_clear_object (&self->cancellable);
g_clear_pointer (&self->items, g_sequence_free);

Expand Down
Loading

0 comments on commit e29679c

Please sign in to comment.