/*
 * Purple Zulip - Zulip Protocol Plugin
 * Copyright (C) Pidgin Developers <devel@pidgin.im>
 *
 * Purple Zulip is the legal property of its developers, whose names are too
 * numerous to list here. Please refer to the COPYRIGHT file distributed with
 * this source distribution.
 *
 * This plugin is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This plugin is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this plugin; if not, see <https://www.gnu.org/licenses/>.
 */

#include <glib/gi18n-lib.h>

#include <json-glib/json-glib.h>

#include <birb.h>

#include "purplezuliptypes.h"

#include "purplezulipconnection.h"

#define PURPLE_ZULIP_TYPE_COPY_STRING(sym) copy->sym = g_strdup(source->sym)
#define PURPLE_ZULIP_TYPE_COPY_VALUE(sym) copy->sym = source->sym

#define PURPLE_ZULIP_TYPE_GET_BOOLEAN(inst, sym) G_STMT_START { \
	inst->sym = json_object_get_boolean_member_with_default(object, \
	                                                        G_STRINGIFY_ARG(sym), \
	                                                        FALSE); \
} G_STMT_END

#define PURPLE_ZULIP_TYPE_GET_INT(inst, sym) G_STMT_START { \
	inst->sym = json_object_get_int_member_with_default(object, \
	                                                    G_STRINGIFY_ARG(sym), \
	                                                    G_GINT64_CONSTANT(0)); \
} G_STMT_END

#define PURPLE_ZULIP_TYPE_GET_STRING(inst, sym) G_STMT_START { \
	const char *value = NULL; \
	value = json_object_get_string_member_with_default(object, \
	                                                   G_STRINGIFY_ARG(sym), \
	                                                   NULL); \
	inst->sym = g_strdup(value); \
} G_STMT_END

/******************************************************************************
 * DisplayRecipient
 *****************************************************************************/
PurpleZulipDisplayRecipient *
purple_zulip_display_recipient_copy(PurpleZulipDisplayRecipient *source) {
	PurpleZulipDisplayRecipient *copy = NULL;

	g_return_val_if_fail(source != NULL, NULL);

	copy = g_new0(PurpleZulipDisplayRecipient, 1);

	PURPLE_ZULIP_TYPE_COPY_VALUE(id);
	PURPLE_ZULIP_TYPE_COPY_STRING(email);
	PURPLE_ZULIP_TYPE_COPY_STRING(full_name);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_mirror_dummy);

	return copy;
}

void
purple_zulip_display_recipient_free(PurpleZulipDisplayRecipient *display_recipient)
{
	g_return_if_fail(display_recipient != NULL);

	g_free(display_recipient->email);
	g_free(display_recipient->full_name);

	g_free(display_recipient);
}

static gpointer
purple_zulip_display_recipient_deserialize(JsonNode *node) {
	PurpleZulipDisplayRecipient *display_recipient = NULL;
	JsonObject *object = NULL;

	display_recipient = g_new0(PurpleZulipDisplayRecipient, 1);

	object = json_node_get_object(node);

	PURPLE_ZULIP_TYPE_GET_INT(display_recipient, id);
	PURPLE_ZULIP_TYPE_GET_STRING(display_recipient, email);
	PURPLE_ZULIP_TYPE_GET_STRING(display_recipient, full_name);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(display_recipient, is_mirror_dummy);

	return display_recipient;
}

G_DEFINE_BOXED_TYPE_WITH_CODE(PurpleZulipDisplayRecipient,
                              purple_zulip_display_recipient,
                              purple_zulip_display_recipient_copy,
                              purple_zulip_display_recipient_free,
                              json_boxed_register_deserialize_func(g_define_type_id,
                                                                   JSON_NODE_OBJECT,
                                                                   purple_zulip_display_recipient_deserialize))

/******************************************************************************
 * Message
 *****************************************************************************/
static gpointer
purple_zulip_message_copy_display_recipient(gconstpointer source,
                                            G_GNUC_UNUSED gpointer data)
{
	return purple_zulip_display_recipient_copy((PurpleZulipDisplayRecipient *)source);
}

PurpleZulipMessage *
purple_zulip_message_copy(PurpleZulipMessage *source) {
	PurpleZulipMessage *copy = NULL;

	g_return_val_if_fail(source != NULL, NULL);

	copy = g_new0(PurpleZulipMessage, 1);

	PURPLE_ZULIP_TYPE_COPY_STRING(avatar_url);
	PURPLE_ZULIP_TYPE_COPY_STRING(client);
	PURPLE_ZULIP_TYPE_COPY_STRING(content);
	PURPLE_ZULIP_TYPE_COPY_STRING(content_type);
	PURPLE_ZULIP_TYPE_COPY_VALUE(id);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_me_message);
	PURPLE_ZULIP_TYPE_COPY_VALUE(recipient_id);
	PURPLE_ZULIP_TYPE_COPY_STRING(sender_email);
	PURPLE_ZULIP_TYPE_COPY_STRING(sender_full_name);
	PURPLE_ZULIP_TYPE_COPY_VALUE(sender_id);
	PURPLE_ZULIP_TYPE_COPY_STRING(sender_realm_str);
	PURPLE_ZULIP_TYPE_COPY_VALUE(stream_id);
	PURPLE_ZULIP_TYPE_COPY_STRING(subject);
	PURPLE_ZULIP_TYPE_COPY_VALUE(timestamp);
	PURPLE_ZULIP_TYPE_COPY_STRING(type);

	copy->display_recipient = g_ptr_array_copy(source->display_recipient,
	                                           purple_zulip_message_copy_display_recipient,
	                                           NULL);

	return copy;
}

void
purple_zulip_message_free(PurpleZulipMessage *message) {
	g_return_if_fail(message != NULL);

	g_free(message->avatar_url);
	g_free(message->client);
	g_free(message->content);
	g_free(message->content_type);
	g_clear_pointer(&message->display_recipient, g_ptr_array_unref);
	g_free(message->sender_email);
	g_free(message->sender_full_name);
	g_free(message->sender_realm_str);
	g_free(message->subject);
	g_free(message->type);

	g_free(message);
}

static gpointer
purple_zulip_message_deserialize(JsonNode *node) {
	PurpleZulipMessage *message = NULL;
	JsonObject *object = NULL;

	message = g_new0(PurpleZulipMessage, 1);

	object = json_node_get_object(node);

	PURPLE_ZULIP_TYPE_GET_STRING(message, avatar_url);
	PURPLE_ZULIP_TYPE_GET_STRING(message, client);
	PURPLE_ZULIP_TYPE_GET_STRING(message, content);
	PURPLE_ZULIP_TYPE_GET_STRING(message, content_type);
	PURPLE_ZULIP_TYPE_GET_INT(message, id);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(message, is_me_message);
	PURPLE_ZULIP_TYPE_GET_INT(message, recipient_id);
	PURPLE_ZULIP_TYPE_GET_STRING(message, sender_email);
	PURPLE_ZULIP_TYPE_GET_STRING(message, sender_full_name);
	PURPLE_ZULIP_TYPE_GET_INT(message, sender_id);
	PURPLE_ZULIP_TYPE_GET_STRING(message, sender_realm_str);
	PURPLE_ZULIP_TYPE_GET_INT(message, stream_id);
	PURPLE_ZULIP_TYPE_GET_STRING(message, subject);
	PURPLE_ZULIP_TYPE_GET_INT(message, timestamp);
	PURPLE_ZULIP_TYPE_GET_STRING(message, type);

	if(json_object_has_member(object, "display_recipient")) {
		JsonArray *array = NULL;
		guint n_items = 0;

		message->display_recipient = g_ptr_array_new_full(1,
		                                                  (GDestroyNotify)purple_zulip_display_recipient_free);
		array = json_object_get_array_member(object, "display_recipient");
		n_items = json_array_get_length(array);
		for(guint i = 0; i < n_items; i++) {
			PurpleZulipDisplayRecipient *display_recipient = NULL;
			JsonNode *item_node = NULL;

			item_node = json_array_get_element(array, i);
			display_recipient = json_boxed_deserialize(PURPLE_ZULIP_TYPE_DISPLAY_RECIPIENT,
			                                           item_node);
			if(display_recipient != NULL) {
				g_ptr_array_add(message->display_recipient, display_recipient);
			}
		}
	}

	return message;
}

G_DEFINE_BOXED_TYPE_WITH_CODE(PurpleZulipMessage,
                              purple_zulip_message,
                              purple_zulip_message_copy,
                              purple_zulip_message_free,
                              json_boxed_register_deserialize_func(g_define_type_id,
                                                                   JSON_NODE_OBJECT,
                                                                   purple_zulip_message_deserialize))

/******************************************************************************
 * PrivateConversation
 *****************************************************************************/
PurpleZulipPrivateConversation *
purple_zulip_private_conversation_copy(PurpleZulipPrivateConversation *source)
{
	PurpleZulipPrivateConversation *copy = NULL;

	g_return_val_if_fail(source != NULL, NULL);

	copy = g_new0(PurpleZulipPrivateConversation, 1);

	copy->user_ids = g_array_copy(source->user_ids);

	PURPLE_ZULIP_TYPE_COPY_VALUE(max_message_id);

	return copy;
}

void
purple_zulip_private_conversation_free(PurpleZulipPrivateConversation *private_conversation)
{
	g_return_if_fail(private_conversation != NULL);

	g_clear_pointer(&private_conversation->user_ids, g_array_unref);

	g_free(private_conversation);
}

static gpointer
purple_zulip_private_conversation_deserialize(JsonNode *node) {
	PurpleZulipPrivateConversation *private_conversation = NULL;
	JsonObject *object = NULL;
	JsonArray *array = NULL;
	guint n_users = 0;

	private_conversation = g_new0(PurpleZulipPrivateConversation, 1);

	object = json_node_get_object(node);

	private_conversation->user_ids = g_array_new(FALSE, FALSE, sizeof(gint64));
	array = json_object_get_array_member(object, "user_ids");
	n_users = json_array_get_length(array);
	for(guint i = 0; i < n_users; i++) {
		gint64 user_id = G_GINT64_CONSTANT(0);

		user_id = json_array_get_int_element(array, i);
		g_array_append_val(private_conversation->user_ids, user_id);
	}

	PURPLE_ZULIP_TYPE_GET_INT(private_conversation, max_message_id);

	return private_conversation;
}

PurpleConversation *
purple_zulip_private_conversation_to_conversation(PurpleZulipPrivateConversation *private_conversation,
                                                  PurpleAccount *account)
{
	PurpleConnection *connection = NULL;

	connection = purple_account_get_connection(account);
	return purple_zulip_connection_find_or_create_dm(PURPLE_ZULIP_CONNECTION(connection),
	                                                 private_conversation->user_ids);
}

G_DEFINE_BOXED_TYPE_WITH_CODE(PurpleZulipPrivateConversation,
                              purple_zulip_private_conversation,
                              purple_zulip_private_conversation_copy,
                              purple_zulip_private_conversation_free,
                              json_boxed_register_deserialize_func(g_define_type_id,
                                                                   JSON_NODE_OBJECT,
                                                                   purple_zulip_private_conversation_deserialize))

/******************************************************************************
 * SimpleUser
 *****************************************************************************/
PurpleZulipSimpleUser *
purple_zulip_simple_user_copy(PurpleZulipSimpleUser *source) {
	PurpleZulipSimpleUser *copy = NULL;

	g_return_val_if_fail(source != NULL, NULL);

	copy = g_new0(PurpleZulipSimpleUser, 1);

	PURPLE_ZULIP_TYPE_COPY_STRING(email);
	PURPLE_ZULIP_TYPE_COPY_VALUE(user_id);

	return copy;
}

void
purple_zulip_simple_user_free(PurpleZulipSimpleUser *simple_user) {
	g_return_if_fail(simple_user != NULL);

	g_free(simple_user->email);

	g_free(simple_user);
}

static gpointer
purple_zulip_simple_user_deserialize(JsonNode *node) {
	PurpleZulipSimpleUser *simple_user = NULL;
	JsonObject *object = NULL;

	simple_user = g_new(PurpleZulipSimpleUser, 1);

	object = json_node_get_object(node);

	PURPLE_ZULIP_TYPE_GET_STRING(simple_user, email);
	PURPLE_ZULIP_TYPE_GET_INT(simple_user, user_id);

	return simple_user;
}

G_DEFINE_BOXED_TYPE_WITH_CODE(PurpleZulipSimpleUser,
                              purple_zulip_simple_user,
                              purple_zulip_simple_user_copy,
                              purple_zulip_simple_user_free,
                              json_boxed_register_deserialize_func(g_define_type_id,
                                                                   JSON_NODE_OBJECT,
                                                                   purple_zulip_simple_user_deserialize))

/******************************************************************************
 * Subscription
 *****************************************************************************/
PurpleZulipSubscription *
purple_zulip_subscription_copy(PurpleZulipSubscription *source) {
	PurpleZulipSubscription *copy = NULL;

	g_return_val_if_fail(source != NULL, NULL);

	copy = g_new0(PurpleZulipSubscription, 1);

	PURPLE_ZULIP_TYPE_COPY_VALUE(audible_notifications);
	PURPLE_ZULIP_TYPE_COPY_VALUE(can_add_subscribers_group);
	PURPLE_ZULIP_TYPE_COPY_VALUE(can_administer_channel_group);
	PURPLE_ZULIP_TYPE_COPY_VALUE(can_create_topic_group);
	PURPLE_ZULIP_TYPE_COPY_VALUE(can_delete_any_message_group);
	PURPLE_ZULIP_TYPE_COPY_VALUE(can_delete_own_message_group);
	PURPLE_ZULIP_TYPE_COPY_VALUE(can_move_messages_out_of_channel_group);
	PURPLE_ZULIP_TYPE_COPY_VALUE(can_move_messages_within_channel_group);
	PURPLE_ZULIP_TYPE_COPY_VALUE(can_remove_subscribers_group);
	PURPLE_ZULIP_TYPE_COPY_VALUE(can_resolve_topics_group);
	PURPLE_ZULIP_TYPE_COPY_VALUE(can_send_message_group);
	PURPLE_ZULIP_TYPE_COPY_VALUE(can_subscribe_group);
	PURPLE_ZULIP_TYPE_COPY_STRING(color);
	PURPLE_ZULIP_TYPE_COPY_VALUE(creator_id);
	PURPLE_ZULIP_TYPE_COPY_VALUE(date_created);
	PURPLE_ZULIP_TYPE_COPY_STRING(description);
	PURPLE_ZULIP_TYPE_COPY_VALUE(desktop_notifications);
	PURPLE_ZULIP_TYPE_COPY_VALUE(email_notifications);
	PURPLE_ZULIP_TYPE_COPY_VALUE(first_message_id);
	PURPLE_ZULIP_TYPE_COPY_VALUE(folder_id);
	PURPLE_ZULIP_TYPE_COPY_VALUE(history_public_to_subscribers);
	PURPLE_ZULIP_TYPE_COPY_VALUE(in_home_view);
	PURPLE_ZULIP_TYPE_COPY_VALUE(invite_only);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_announcement_only);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_archived);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_muted);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_recently_active);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_web_public);
	PURPLE_ZULIP_TYPE_COPY_VALUE(message_retention_days);
	PURPLE_ZULIP_TYPE_COPY_STRING(name);
	PURPLE_ZULIP_TYPE_COPY_VALUE(pin_to_top);
	PURPLE_ZULIP_TYPE_COPY_VALUE(push_notifications);
	PURPLE_ZULIP_TYPE_COPY_STRING(rendered_description);
	PURPLE_ZULIP_TYPE_COPY_VALUE(stream_id);
	PURPLE_ZULIP_TYPE_COPY_VALUE(stream_post_policy);
	PURPLE_ZULIP_TYPE_COPY_VALUE(stream_weekly_traffic);
	PURPLE_ZULIP_TYPE_COPY_VALUE(subscriber_count);
	PURPLE_ZULIP_TYPE_COPY_STRING(topics_policy);
	PURPLE_ZULIP_TYPE_COPY_VALUE(wildcard_mentions_notify);

	return copy;
}

void
purple_zulip_subscription_free(PurpleZulipSubscription *subscription) {
	g_return_if_fail(subscription != NULL);

	g_free(subscription->color);
	g_free(subscription->description);
	g_free(subscription->name);
	g_free(subscription->rendered_description);
	g_free(subscription->topics_policy);

	g_free(subscription);
}

static gpointer
purple_zulip_subscription_deserialize(JsonNode *node) {
	PurpleZulipSubscription *subscription = NULL;
	JsonObject *object = NULL;

	subscription = g_new(PurpleZulipSubscription, 1);

	object = json_node_get_object(node);

	PURPLE_ZULIP_TYPE_GET_BOOLEAN(subscription, audible_notifications);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, can_add_subscribers_group);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, can_administer_channel_group);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, can_create_topic_group);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, can_delete_any_message_group);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, can_delete_own_message_group);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, can_move_messages_out_of_channel_group);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, can_move_messages_within_channel_group);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, can_remove_subscribers_group);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, can_resolve_topics_group);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, can_send_message_group);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, can_subscribe_group);
	PURPLE_ZULIP_TYPE_GET_STRING(subscription, color);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, creator_id);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, date_created);
	PURPLE_ZULIP_TYPE_GET_STRING(subscription, description);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(subscription, desktop_notifications);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(subscription, email_notifications);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, first_message_id);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, folder_id);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(subscription, history_public_to_subscribers);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(subscription, in_home_view);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(subscription, invite_only);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(subscription, is_announcement_only);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(subscription, is_archived);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(subscription, is_muted);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(subscription, is_recently_active);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(subscription, is_web_public);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, message_retention_days);
	PURPLE_ZULIP_TYPE_GET_STRING(subscription, name);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(subscription, pin_to_top);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(subscription, push_notifications);
	PURPLE_ZULIP_TYPE_GET_STRING(subscription, rendered_description);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, stream_id);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, stream_post_policy);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, stream_weekly_traffic);
	PURPLE_ZULIP_TYPE_GET_INT(subscription, subscriber_count);
	PURPLE_ZULIP_TYPE_GET_STRING(subscription, topics_policy);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(subscription, wildcard_mentions_notify);

	return subscription;
}

G_DEFINE_BOXED_TYPE_WITH_CODE(PurpleZulipSubscription,
                              purple_zulip_subscription,
                              purple_zulip_subscription_copy,
                              purple_zulip_subscription_free,
                              json_boxed_register_deserialize_func(g_define_type_id,
                                                                   JSON_NODE_OBJECT,
                                                                   purple_zulip_subscription_deserialize))

PurpleConversation *
purple_zulip_subscription_to_conversation(PurpleZulipSubscription *subscription,
                                          PurpleAccount *account)
{
	PurpleContact *creator = NULL;
	PurpleContactManager *contact_manager = NULL;
	PurpleConversation *conversation = NULL;
	PurpleConversationManager *conversation_manager = NULL;
	PurpleCore *core = NULL;
	GDateTime *created_on = NULL;
	char *creator_id = NULL;
	char *conversation_id = NULL;
	gboolean created = FALSE;

	g_return_val_if_fail(subscription != NULL, NULL);
	g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);

	core = purple_core_get_default();
	contact_manager = purple_core_get_contact_manager(core);
	conversation_manager = purple_core_get_conversation_manager(core);

	/* Turn the conversation id into a string. */
	conversation_id = g_strdup_printf("%" G_GINT64_FORMAT, subscription->stream_id);

	conversation = purple_conversation_manager_find(conversation_manager,
	                                                account,
	                                                PURPLE_CONVERSATION_TYPE_CHANNEL,
	                                                conversation_id);
	if(!PURPLE_IS_CONVERSATION(conversation)) {
		conversation = purple_conversation_new(account,
		                                       PURPLE_CONVERSATION_TYPE_CHANNEL,
		                                       conversation_id);
		created = TRUE;
	}

	g_clear_pointer(&conversation_id, g_free);

	/* Update all the fields. */
	purple_conversation_set_can_send_message(conversation, FALSE);
	purple_conversation_set_color(conversation, subscription->color);

	creator_id = g_strdup_printf("%" G_GINT64_FORMAT, subscription->creator_id);
	creator = purple_contact_manager_find_with_id(contact_manager, account,
	                                              creator_id);
	purple_conversation_set_creator(conversation,
	                                PURPLE_CONTACT_INFO(creator));
	g_clear_pointer(&creator_id, g_free);
	g_clear_object(&creator);

	created_on = g_date_time_new_from_unix_utc(subscription->date_created);
	purple_conversation_set_created_on(conversation, created_on);
	birb_date_time_clear(&created_on);

	purple_conversation_set_favorite(conversation, subscription->pin_to_top);

	purple_conversation_set_status_message(conversation,
	                                       _("Select a thread to send a message"));

	purple_conversation_set_title(conversation, subscription->name);
	purple_conversation_set_topic(conversation, subscription->description);

	if(created) {
		purple_conversation_manager_add(conversation_manager, conversation);

		/* Remove the reference from creating the conversation as the manager
		 * still has a reference.
		 */
		g_object_unref(conversation);
	}

	return conversation;
}

/******************************************************************************
 * Typing
 *****************************************************************************/
static PurpleZulipSimpleUser *
purple_zulip_typing_copy_simple_user(gconstpointer *source,
                                     G_GNUC_UNUSED gpointer data)
{
	return purple_zulip_simple_user_copy((PurpleZulipSimpleUser *)source);
}

PurpleZulipTyping *
purple_zulip_typing_copy(PurpleZulipTyping *source) {
	PurpleZulipTyping *copy = NULL;

	g_return_val_if_fail(source != NULL, NULL);

	copy = g_new0(PurpleZulipTyping, 1);

	PURPLE_ZULIP_TYPE_COPY_STRING(message_type);
	PURPLE_ZULIP_TYPE_COPY_STRING(op);

	copy->recipients = g_ptr_array_copy(source->recipients,
	                                    (GCopyFunc)purple_zulip_typing_copy_simple_user,
	                                    NULL);
	copy->sender = purple_zulip_simple_user_copy(source->sender);

	PURPLE_ZULIP_TYPE_COPY_VALUE(stream_id);
	PURPLE_ZULIP_TYPE_COPY_STRING(topic);

	return copy;
}

void
purple_zulip_typing_free(PurpleZulipTyping *typing) {
	g_return_if_fail(typing != NULL);

	g_free(typing->message_type);
	g_free(typing->op);
	g_ptr_array_unref(typing->recipients);
	purple_zulip_simple_user_free(typing->sender);
	g_free(typing->topic);

	g_free(typing);
}

static gpointer
purple_zulip_typing_deserialize(JsonNode *node) {
	PurpleZulipTyping *typing = NULL;
	JsonNode *sender = NULL;
	JsonObject *object = NULL;

	typing = g_new(PurpleZulipTyping, 1);

	object = json_node_get_object(node);

	PURPLE_ZULIP_TYPE_GET_STRING(typing, message_type);
	PURPLE_ZULIP_TYPE_GET_STRING(typing, op);

	if(json_object_has_member(object, "recipients")) {
		JsonArray *array = NULL;
		guint n_items = 0;

		typing->recipients = g_ptr_array_new_full(1,
		                                          (GDestroyNotify)purple_zulip_simple_user_free);

		array = json_object_get_array_member(object, "recipients");
		n_items = json_array_get_length(array);
		for(guint i = 0; i < n_items; i++) {
			PurpleZulipSimpleUser *simple_user = NULL;
			JsonNode *item_node = NULL;

			item_node = json_array_get_element(array, i);
			simple_user = json_boxed_deserialize(PURPLE_ZULIP_TYPE_SIMPLE_USER,
			                                     item_node);
			if(simple_user != NULL) {
				g_ptr_array_add(typing->recipients, simple_user);
			}
		}
	}

	sender = json_object_get_member(object, "sender");
	typing->sender = json_boxed_deserialize(PURPLE_ZULIP_TYPE_SIMPLE_USER,
	                                        sender);

	PURPLE_ZULIP_TYPE_GET_INT(typing, stream_id);
	PURPLE_ZULIP_TYPE_GET_STRING(typing, topic);

	return typing;
}

G_DEFINE_BOXED_TYPE_WITH_CODE(PurpleZulipTyping,
                              purple_zulip_typing,
                              purple_zulip_typing_copy,
                              purple_zulip_typing_free,
                              json_boxed_register_deserialize_func(g_define_type_id,
                                                                   JSON_NODE_OBJECT,
                                                                   purple_zulip_typing_deserialize))

/******************************************************************************
 * User
 *****************************************************************************/
PurpleZulipUser *
purple_zulip_user_copy(PurpleZulipUser *source) {
	PurpleZulipUser *copy = NULL;

	g_return_val_if_fail(source != NULL, NULL);

	copy = g_new0(PurpleZulipUser, 1);

	PURPLE_ZULIP_TYPE_COPY_STRING(avatar_url);
	PURPLE_ZULIP_TYPE_COPY_VALUE(avatar_version);
	PURPLE_ZULIP_TYPE_COPY_VALUE(bot_type);
	PURPLE_ZULIP_TYPE_COPY_VALUE(bot_owner_id);
	PURPLE_ZULIP_TYPE_COPY_STRING(date_joined);
	PURPLE_ZULIP_TYPE_COPY_STRING(delivery_email);
	PURPLE_ZULIP_TYPE_COPY_STRING(email);
	PURPLE_ZULIP_TYPE_COPY_STRING(full_name);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_active);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_admin);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_bot);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_guest);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_imported_stub);
	PURPLE_ZULIP_TYPE_COPY_VALUE(is_owner);
	PURPLE_ZULIP_TYPE_COPY_VALUE(role);
	PURPLE_ZULIP_TYPE_COPY_STRING(timezone);
	PURPLE_ZULIP_TYPE_COPY_VALUE(user_id);

	return copy;
}

void
purple_zulip_user_free(PurpleZulipUser *user) {
	g_return_if_fail(user != NULL);

	g_free(user->avatar_url);
	g_free(user->date_joined);
	g_free(user->delivery_email);
	g_free(user->email);
	g_free(user->full_name);
	g_free(user->timezone);

	g_free(user);
}

static gpointer
purple_zulip_user_deserialize(JsonNode *node) {
	PurpleZulipUser *user = NULL;
	JsonObject *object = NULL;

	user = g_new(PurpleZulipUser, 1);

	object = json_node_get_object(node);

	PURPLE_ZULIP_TYPE_GET_STRING(user, avatar_url);
	PURPLE_ZULIP_TYPE_GET_INT(user, avatar_version);
	PURPLE_ZULIP_TYPE_GET_INT(user, bot_type);
	PURPLE_ZULIP_TYPE_GET_INT(user, bot_owner_id);
	PURPLE_ZULIP_TYPE_GET_STRING(user, date_joined);
	PURPLE_ZULIP_TYPE_GET_STRING(user, delivery_email);
	PURPLE_ZULIP_TYPE_GET_STRING(user, email);
	PURPLE_ZULIP_TYPE_GET_STRING(user, full_name);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(user, is_active);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(user, is_admin);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(user, is_bot);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(user, is_guest);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(user, is_imported_stub);
	PURPLE_ZULIP_TYPE_GET_BOOLEAN(user, is_owner);
	PURPLE_ZULIP_TYPE_GET_INT(user, role);
	PURPLE_ZULIP_TYPE_GET_STRING(user, timezone);
	PURPLE_ZULIP_TYPE_GET_INT(user, user_id);

	return user;
}

G_DEFINE_BOXED_TYPE_WITH_CODE(PurpleZulipUser,
                              purple_zulip_user,
                              purple_zulip_user_copy,
                              purple_zulip_user_free,
                              json_boxed_register_deserialize_func(g_define_type_id,
                                                                   JSON_NODE_OBJECT,
                                                                   purple_zulip_user_deserialize))
PurpleContact *
purple_zulip_user_to_contact(PurpleZulipUser *user, PurpleAccount *account) {
	PurpleContact *contact = NULL;
	PurpleContactInfo *info = NULL;
	PurpleContactManager *contact_manager = NULL;
	char *id = NULL;

	g_return_val_if_fail(user != NULL, NULL);
	g_return_val_if_fail(PURPLE_IS_ACCOUNT(account), NULL);

	contact_manager = purple_core_get_contact_manager(purple_core_get_default());

	id = g_strdup_printf("%d", user->user_id);
	contact = purple_contact_manager_find_or_create(contact_manager, account,
	                                                id, NULL);
	g_clear_pointer(&id, g_free);

	info = PURPLE_CONTACT_INFO(contact);
	purple_contact_info_set_display_name(info, user->full_name);
	purple_contact_info_set_email(info, user->email);

	if(!birb_str_is_empty(user->date_joined)) {
		GDateTime *created_time = NULL;

		created_time = g_date_time_new_from_iso8601(user->date_joined, NULL);
		purple_contact_info_set_created_time(info, created_time);
		g_clear_pointer(&created_time, g_date_time_unref);
	} else {
		purple_contact_info_set_created_time(info, NULL);
	}

	if(!birb_str_is_empty(user->timezone)) {
		GTimeZone *time_zone = NULL;

		time_zone = g_time_zone_new_identifier(user->timezone);
		purple_contact_info_set_time_zone(info, time_zone);
		g_clear_pointer(&time_zone, g_time_zone_unref);
	} else {
		purple_contact_info_set_time_zone(info, NULL);
	}

	return contact;
}
