static void on_message_received()

in prpl/messages.c [156:190]


static void on_message_received(ChimeObject *obj, JsonNode *node, struct chime_msgs *msgs)
{
	ChimeConnection *cxn = PURPLE_CHIME_CXN(msgs->conn);
	const gchar *id;
	if (!parse_string(node, "MessageId", &id))
		return;
	if (msgs->msg_gather) {
		/* If we're still fetching ancient messages and a new message comes
		 * in, then ignore it. We'll fetch it again when our fetch reaches
		 * the present day. */
		if (msgs->fetch_until && msg_newer_than(node, msgs->fetch_until))
			return;

		/* Still gathering messages. Add to the table, to avoid dupes */
		JsonNode *old_node = g_hash_table_lookup(msgs->msg_gather, id);
		if (old_node) {
			if (!msg_newer(node, old_node))
				return;
			/* Remove first because the key belongs to the value */
			g_hash_table_remove(msgs->msg_gather, id);
		}
		g_hash_table_insert(msgs->msg_gather, (gchar *)id, json_node_ref(node));
		return;
	}
	GTimeVal tv;
	const gchar *created;
	if (!parse_time(node, "CreatedOn", &created, &tv))
		return;

	if (!msgs->msgs_failed)
		chime_update_last_msg(cxn, msgs, created, id);

	if (is_msg_unseen(msgs->seen_msgs, id))
		msgs->cb(cxn, msgs, node, tv.tv_sec, TRUE);
}