chatty-ma-account.c 33.7 KB
Newer Older
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
/* chatty-ma-account.c
 *
 * Copyright 2020 Purism SPC
 *
 * Author(s):
 *   Mohammed Sadiq <sadiq@sadiqpk.org>
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#define G_LOG_DOMAIN "chatty-ma-account"

14
#include <libsecret/secret.h>
Mohammed Sadiq's avatar
Mohammed Sadiq committed
15 16
#include <glib/gi18n.h>

17
#include "chatty-secret-store.h"
Mohammed Sadiq's avatar
Mohammed Sadiq committed
18 19 20 21 22 23 24 25
#include "chatty-history.h"
#include "matrix-api.h"
#include "matrix-enc.h"
#include "matrix-db.h"
#include "matrix-utils.h"
#include "chatty-utils.h"
#include "chatty-ma-chat.h"
#include "chatty-ma-account.h"
26
#include "chatty-log.h"
Mohammed Sadiq's avatar
Mohammed Sadiq committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

/**
 * SECTION: chatty-mat-account
 * @title: ChattyMaAccount
 * @short_description: An abstraction for Matrix accounts
 * @include: "chatty-mat-account.h"
 */

#define SYNC_TIMEOUT 30000 /* milliseconds */

struct _ChattyMaAccount
{
  ChattyAccount   parent_instance;

  char           *name;

  MatrixApi      *matrix_api;
  MatrixEnc      *matrix_enc;
  MatrixDb       *matrix_db;

  ChattyHistory  *history_db;

  char           *pickle_key;
  char           *next_batch;

  GListStore     *chat_list;
53 54
  /* this will be moved to chat_list after login succeeds */
  GPtrArray      *db_chat_list;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
55 56 57 58 59 60 61 62 63 64 65 66
  GdkPixbuf      *avatar;

  ChattyStatus   status;
  gboolean       homeserver_valid;
  gboolean       account_enabled;

  gboolean       is_loading;
  gboolean       save_account_pending;
  gboolean       save_password_pending;

  /* for sending events, incremented for each event */
  int            event_id;
67
  guint          connect_id;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
};

G_DEFINE_TYPE (ChattyMaAccount, chatty_ma_account, CHATTY_TYPE_ACCOUNT)


static ChattyMaChat *
matrix_find_chat_with_id (ChattyMaAccount *self,
                          const char       *room_id,
                          guint            *index)
{
  guint n_items;

  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  if (!room_id || !*room_id)
    return NULL;

  n_items = g_list_model_get_n_items (G_LIST_MODEL (self->chat_list));
  for (guint i = 0; i < n_items; i++) {
    g_autoptr(ChattyMaChat) chat = NULL;

    chat = g_list_model_get_item (G_LIST_MODEL (self->chat_list), i);
    if (chatty_ma_chat_matches_id (chat, room_id)) {
      if (index)
        *index = i;

      return chat;
    }
  }

  return NULL;
}

static void
matrix_parse_device_data (ChattyMaAccount *self,
                          JsonObject      *to_device)
{
  JsonObject *object;
  JsonArray *array;
  guint length = 0;

109 110
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
  g_assert (CHATTY_IS_MA_ACCOUNT (self));
  g_assert (to_device);

  array = matrix_utils_json_object_get_array (to_device, "events");
  if (array)
    length = json_array_get_length (array);

  for (guint i = 0; i < length; i++) {
    const char *type;

    object = json_array_get_object_element (array, i);
    type = matrix_utils_json_object_get_string (object, "type");

    if (g_strcmp0 (type, "m.room.encrypted") == 0)
      matrix_enc_handle_room_encrypted (self->matrix_enc, object);
  }
127 128

  CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
129 130 131 132 133 134 135 136 137
}

static void
matrix_parse_room_data (ChattyMaAccount *self,
                        JsonObject       *rooms)
{
  JsonObject *joined_rooms, *left_rooms;
  ChattyMaChat *chat;

138 139
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  g_assert (CHATTY_IS_MA_ACCOUNT (self));
  g_assert (rooms);

  joined_rooms = matrix_utils_json_object_get_object (rooms, "join");

  if (joined_rooms) {
    g_autoptr(GList) joined_room_ids = NULL;
    JsonObject *room_data;

    joined_room_ids = json_object_get_members (joined_rooms);

    for (GList *room_id = joined_room_ids; room_id; room_id = room_id->next) {
      chat = matrix_find_chat_with_id (self, room_id->data, NULL);
      room_data = matrix_utils_json_object_get_object (joined_rooms, room_id->data);

      if (!chat) {
        chat = g_object_new (CHATTY_TYPE_MA_CHAT, "room-id", room_id->data, NULL);
        chatty_ma_chat_set_matrix_db (chat, self->matrix_db);
        chatty_ma_chat_set_history_db (chat, self->history_db);
        /* TODO */
        /* chatty_ma_chat_set_last_batch (chat, self->next_batch); */
        chatty_ma_chat_set_data (chat, CHATTY_ACCOUNT (self), self->matrix_api, self->matrix_enc);
        g_object_set (chat, "json-data", room_data, NULL);
        g_list_store_append (self->chat_list, chat);
        g_object_unref (chat);
      } else if (room_data) {
        g_object_set (chat, "json-data", room_data, NULL);
      }
    }
  }

  left_rooms = matrix_utils_json_object_get_object (rooms, "leave");

  if (left_rooms) {
    g_autoptr(GList) left_room_ids = NULL;

    left_room_ids = json_object_get_members (left_rooms);

    for (GList *room_id = left_room_ids; room_id; room_id = room_id->next) {
      chat = matrix_find_chat_with_id (self, room_id->data, NULL);

181 182 183
      if (chat) {
        chatty_item_set_state (CHATTY_ITEM (chat), CHATTY_ITEM_HIDDEN);
        chatty_history_update_chat (self->history_db, CHATTY_CHAT (chat));
Mohammed Sadiq's avatar
Mohammed Sadiq committed
184
        chatty_utils_remove_list_item (self->chat_list, chat);
185
      }
Mohammed Sadiq's avatar
Mohammed Sadiq committed
186 187
    }
  }
188 189

  CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
190 191 192 193 194 195 196
}

static void
handle_get_homeserver (ChattyMaAccount *self,
                       JsonObject      *object,
                       GError          *error)
{
197 198
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
199 200
  g_assert (CHATTY_IS_MA_ACCOUNT (self));

201 202 203 204 205
  if (error) {
    self->status = CHATTY_DISCONNECTED;
    g_object_notify (G_OBJECT (self), "status");
  }

Mohammed Sadiq's avatar
Mohammed Sadiq committed
206 207 208 209
  if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND)) {
    g_warning ("Couldn't connect to ‘/.well-known/matrix/client’ ");
    matrix_api_set_homeserver (self->matrix_api, "https://chat.librem.one");
  }
210 211

  CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
212 213
}

214 215 216 217 218
static void
handle_verify_homeserver (ChattyMaAccount *self,
                          JsonObject      *object,
                          GError          *error)
{
219 220
  CHATTY_ENTRY;

221 222 223 224 225 226 227 228
  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  if (error) {
    self->status = CHATTY_DISCONNECTED;
    g_object_notify (G_OBJECT (self), "status");
  }
}

Mohammed Sadiq's avatar
Mohammed Sadiq committed
229 230 231 232 233
static void
handle_password_login (ChattyMaAccount *self,
                       JsonObject      *object,
                       GError          *error)
{
234 235
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  /* If no error, Api is informing us that logging in succeeded.
   * Let’s update matrix_enc & set device keys to upload */
  if (g_error_matches (error, MATRIX_ERROR, M_BAD_PASSWORD)) {
    GtkWidget *dialog, *content, *header_bar;
    GtkWidget *cancel_btn, *entry;
    g_autofree char *label = NULL;
    const char *password;
    int response;

    dialog = gtk_dialog_new_with_buttons (_("Incorrect password"),
                                          gtk_application_get_active_window (GTK_APPLICATION (g_application_get_default ())),
                                          GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_USE_HEADER_BAR,
                                          _("_OK"), GTK_RESPONSE_ACCEPT,
                                          _("_Cancel"), GTK_RESPONSE_REJECT,
                                          NULL);

    content = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
    gtk_container_set_border_width (GTK_CONTAINER (content), 18);
    gtk_box_set_spacing (GTK_BOX (content), 12);
    label = g_strdup_printf (_("Please enter password for “%s”"),
                             matrix_api_get_username (self->matrix_api));
    gtk_container_add (GTK_CONTAINER (content), gtk_label_new (label));
    entry = gtk_entry_new ();
    gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
    gtk_container_add (GTK_CONTAINER (content), entry);
    gtk_widget_show_all (content);

    header_bar = gtk_dialog_get_header_bar (GTK_DIALOG (dialog));
    gtk_header_bar_set_show_close_button (GTK_HEADER_BAR (header_bar), FALSE);

    cancel_btn = gtk_dialog_get_widget_for_response (GTK_DIALOG (dialog),
                                                     GTK_RESPONSE_REJECT);
    g_object_ref (cancel_btn);
    gtk_container_remove (GTK_CONTAINER (header_bar), cancel_btn);
    gtk_header_bar_pack_start (GTK_HEADER_BAR (header_bar), cancel_btn);
    g_object_unref (cancel_btn);

    response = gtk_dialog_run (GTK_DIALOG (dialog));
    password = gtk_entry_get_text (GTK_ENTRY (entry));


    if (response != GTK_RESPONSE_ACCEPT || !password || !*password) {
      chatty_account_set_enabled (CHATTY_ACCOUNT (self), FALSE);
    } else {
      matrix_api_set_password (self->matrix_api, password);
      self->is_loading = TRUE;
      chatty_account_set_enabled (CHATTY_ACCOUNT (self), FALSE);
      self->is_loading = FALSE;
      chatty_account_set_enabled (CHATTY_ACCOUNT (self), TRUE);
    }

    gtk_widget_destroy (dialog);
  }

  if (!error) {
    self->save_password_pending = TRUE;
    chatty_account_save (CHATTY_ACCOUNT (self));
295 296 297

    self->status = CHATTY_CONNECTED;
    g_object_notify (G_OBJECT (self), "status");
Mohammed Sadiq's avatar
Mohammed Sadiq committed
298
  }
299 300

  CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
}

static void
handle_upload_key (ChattyMaAccount *self,
                   JsonObject      *object,
                   GError          *error)
{
  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  if (object) {
    /* XXX: check later */
    matrix_enc_publish_one_time_keys (self->matrix_enc);

    self->save_account_pending = TRUE;
    chatty_account_save (CHATTY_ACCOUNT (self));
  }
}

static void
handle_get_joined_rooms (ChattyMaAccount *self,
                         JsonObject      *object,
                         GError          *error)
{
  JsonArray *array;
  guint length = 0;

327 328
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  array = matrix_utils_json_object_get_array (object, "joined_rooms");

  if (array)
    length = json_array_get_length (array);

  for (guint i = 0; i < length; i++) {
    g_autoptr(ChattyMaChat) chat = NULL;
    const char *room_id;

    room_id = json_array_get_string_element (array, i);
    chat = g_object_new (CHATTY_TYPE_MA_CHAT, "room-id", room_id, NULL);
    chatty_ma_chat_set_matrix_db (chat, self->matrix_db);
    chatty_ma_chat_set_history_db (chat, self->history_db);
    chatty_ma_chat_set_data (chat, CHATTY_ACCOUNT (self), self->matrix_api, self->matrix_enc);
    g_list_store_append (self->chat_list, chat);
  }
347 348

  CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
349 350 351 352 353 354 355 356 357
}

static void
handle_red_pill (ChattyMaAccount *self,
                 JsonObject      *root,
                 GError          *error)
{
  JsonObject *object;

358 359
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
360 361 362
  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  if (error)
363
    CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
364 365 366 367 368 369

  if (self->status != CHATTY_CONNECTED) {
    self->status = CHATTY_CONNECTED;
    g_object_notify (G_OBJECT (self), "status");
  }

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
  /* Copy chat list loaded from db to main chat list */
  if (self->db_chat_list) {
    GPtrArray *chats = self->db_chat_list;

    for (guint i = 0; i < chats->len; i++) {
      ChattyMaChat *chat = chats->pdata[i];
      chatty_ma_chat_set_matrix_db (chat, self->matrix_db);
      chatty_ma_chat_set_history_db (chat, self->history_db);
      chatty_ma_chat_set_data (chat, CHATTY_ACCOUNT (self), self->matrix_api, self->matrix_enc);
    }

    g_list_store_splice (self->chat_list, 0, 0, chats->pdata, chats->len);
    g_clear_pointer (&self->db_chat_list, g_ptr_array_unref);
  }

Mohammed Sadiq's avatar
Mohammed Sadiq committed
385 386 387 388 389 390 391 392 393 394
  object = matrix_utils_json_object_get_object (root, "to_device");
  if (object)
    matrix_parse_device_data (self, object);

  object = matrix_utils_json_object_get_object (root, "rooms");
  if (object)
    matrix_parse_room_data (self, object);

  self->save_account_pending = TRUE;
  chatty_account_save (CHATTY_ACCOUNT (self));
395
  CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
396 397 398 399 400 401 402 403 404
}

static void
matrix_account_sync_cb (ChattyMaAccount *self,
                        MatrixApi       *api,
                        MatrixAction     action,
                        JsonObject      *object,
                        GError          *error)
{
405 406
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
407 408 409 410 411
  g_assert (CHATTY_IS_MA_ACCOUNT (self));
  g_assert (MATRIX_IS_API (api));
  g_assert (self->matrix_api == api);

  if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
412
    CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
413 414 415 416 417 418 419

  if (error)
    g_debug ("%s Error %d: %s", g_quark_to_string (error->domain),
             error->code, error->message);

  switch (action) {
  case MATRIX_BLUE_PILL:
420
    CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
421 422 423

  case MATRIX_GET_HOMESERVER:
    handle_get_homeserver (self, object, error);
424
    CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
425 426

  case MATRIX_VERIFY_HOMESERVER:
427
    handle_verify_homeserver (self, object, error);
428
    CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
429 430 431

  case MATRIX_PASSWORD_LOGIN:
    handle_password_login (self, object, error);
432
    CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
433 434 435

  case MATRIX_UPLOAD_KEY:
    handle_upload_key (self, object, error);
436
    CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
437 438 439

  case MATRIX_GET_JOINED_ROOMS:
    handle_get_joined_rooms (self, object, error);
440
    CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
441 442 443

  case MATRIX_RED_PILL:
    handle_red_pill (self, object, error);
444
    CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
445 446 447 448 449 450 451 452 453 454

  case MATRIX_ACCESS_TOKEN_LOGIN:
  case MATRIX_SET_TYPING:
  case MATRIX_SEND_MESSAGE:
  case MATRIX_SEND_IMAGE:
  case MATRIX_SEND_VIDEO:
  case MATRIX_SEND_FILE:
  default:
    break;
  }
455 456

  CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
}

static const char *
chatty_ma_account_get_protocol_name (ChattyAccount *account)
{
  return "Matrix";
}

static ChattyStatus
chatty_ma_account_get_status (ChattyAccount *account)
{
  ChattyMaAccount *self = (ChattyMaAccount *)account;

  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  return self->status;
}

static const char *
chatty_ma_account_get_username (ChattyAccount *account)
{
  ChattyMaAccount *self = (ChattyMaAccount *)account;

  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  if (matrix_api_get_username (self->matrix_api))
    return matrix_api_get_username (self->matrix_api);

  return "";
}

static void
chatty_ma_account_set_username (ChattyAccount *account,
                                const char    *username)
{
  ChattyMaAccount *self = (ChattyMaAccount *)account;

  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  matrix_api_set_username (self->matrix_api, username);
}

static gboolean
chatty_ma_account_get_enabled (ChattyAccount *account)
{
  ChattyMaAccount *self = (ChattyMaAccount *)account;

  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  return self->account_enabled;
}

static void
chatty_ma_account_set_enabled (ChattyAccount *account,
                               gboolean       enable)
{
  ChattyMaAccount *self = (ChattyMaAccount *)account;
514
  GNetworkMonitor *network_monitor;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
515

516 517
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
518 519 520
  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  if (self->account_enabled == enable)
521
    CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
522

523 524
  g_clear_handle_id (&self->connect_id, g_source_remove);

Mohammed Sadiq's avatar
Mohammed Sadiq committed
525 526 527 528 529 530
  if (!self->matrix_enc && enable) {
    self->matrix_enc = matrix_enc_new (self->matrix_db, NULL, NULL);
    matrix_api_set_enc (self->matrix_api, self->matrix_enc);
  }

  self->account_enabled = enable;
531
  network_monitor = g_network_monitor_get_default ();
Mohammed Sadiq's avatar
Mohammed Sadiq committed
532

533 534
  if (self->account_enabled &&
      g_network_monitor_get_connectivity (network_monitor) == G_NETWORK_CONNECTIVITY_FULL) {
Mohammed Sadiq's avatar
Mohammed Sadiq committed
535 536
    self->status = CHATTY_CONNECTING;
    matrix_api_start_sync (self->matrix_api);
537
  } else if (!self->account_enabled){
Mohammed Sadiq's avatar
Mohammed Sadiq committed
538 539 540 541 542 543 544 545 546 547 548
    self->status = CHATTY_DISCONNECTED;
    matrix_api_stop_sync (self->matrix_api);
  }

  g_object_notify (G_OBJECT (self), "enabled");
  g_object_notify (G_OBJECT (self), "status");

  if (!self->is_loading) {
    self->save_account_pending = TRUE;
    chatty_account_save (account);
  }
549 550

  CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
}

static const char *
chatty_ma_account_get_password (ChattyAccount *account)
{
  const char *password;
  ChattyMaAccount *self = (ChattyMaAccount *)account;

  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  password = matrix_api_get_password (self->matrix_api);

  if (password)
    return password;

  return "";
}

static void
chatty_ma_account_set_password (ChattyAccount *account,
                                const char    *password)
{
  ChattyMaAccount *self = (ChattyMaAccount *)account;

  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  if (g_strcmp0 (password, matrix_api_get_password (self->matrix_api)) == 0)
    return;

  matrix_api_set_password (self->matrix_api, password);

  if (matrix_api_get_homeserver (self->matrix_api)) {
    self->save_password_pending = TRUE;
    chatty_account_save (account);
  }
}

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
static gboolean
account_connect (gpointer user_data)
{
  g_autoptr(ChattyMaAccount) self = user_data;

  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  self->connect_id = 0;
  self->status = CHATTY_CONNECTING;
  matrix_api_start_sync (self->matrix_api);
  g_object_notify (G_OBJECT (self), "status");

  return G_SOURCE_REMOVE;
}

/* XXX: We always delay regardless of the value of @delay */
604 605 606 607 608 609
static void
chatty_ma_account_connect (ChattyAccount *account,
                           gboolean       delay)
{
  ChattyMaAccount *self = (ChattyMaAccount *)account;

610 611
  CHATTY_ENTRY;

612 613
  g_assert (CHATTY_IS_MA_ACCOUNT (self));

614
  if (!chatty_account_get_enabled (account))
615
    CHATTY_EXIT;
616

617 618
  g_clear_handle_id (&self->connect_id, g_source_remove);
  self->connect_id = g_timeout_add (300, account_connect, g_object_ref (account));
619
  CHATTY_EXIT;
620 621 622 623 624 625 626 627 628 629 630 631 632 633
}

static void
chatty_ma_account_disconnect (ChattyAccount *account)
{
  ChattyMaAccount *self = (ChattyMaAccount *)account;

  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  self->status = CHATTY_DISCONNECTED;
  matrix_api_stop_sync (self->matrix_api);
  g_object_notify (G_OBJECT (self), "status");
}

Mohammed Sadiq's avatar
Mohammed Sadiq committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
static gboolean
chatty_ma_account_get_remember_password (ChattyAccount *self)
{
  /* password is always remembered */
  return TRUE;
}

static void
chatty_ma_account_save (ChattyAccount *account)
{
  ChattyMaAccount *self = (ChattyMaAccount *)account;

  g_assert (CHATTY_IS_MA_ACCOUNT (self));
  g_return_if_fail (matrix_api_get_username (self->matrix_api));

  chatty_ma_account_save_async (self, FALSE, NULL, NULL, NULL);
}

static void
chatty_ma_account_delete (ChattyAccount *account)
{
  ChattyMaAccount *self = (ChattyMaAccount *)account;

  g_assert (CHATTY_IS_MA_ACCOUNT (self));
}

static ChattyProtocol
chatty_ma_account_get_protocols (ChattyItem *item)
{
  return CHATTY_PROTOCOL_MATRIX;
}

static const char *
chatty_ma_account_get_name (ChattyItem *item)
{
  ChattyMaAccount *self = (ChattyMaAccount *)item;

  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  if (self->name)
    return self->name;

  return "";
}

static void
chatty_ma_account_set_name (ChattyItem *item,
                            const char *name)
{
  ChattyMaAccount *self = (ChattyMaAccount *)item;

  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  g_free (self->name);
  self->name = g_strdup (name);
}

static void
chatty_ma_account_finalize (GObject *object)
{
  ChattyMaAccount *self = (ChattyMaAccount *)object;

696
  g_clear_handle_id (&self->connect_id, g_source_remove);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
697 698 699 700 701 702
  g_list_store_remove_all (self->chat_list);

  g_clear_object (&self->matrix_api);
  g_clear_object (&self->matrix_enc);
  g_clear_object (&self->chat_list);
  g_clear_object (&self->avatar);
703
  g_clear_pointer (&self->db_chat_list, g_ptr_array_unref);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

  G_OBJECT_CLASS (chatty_ma_account_parent_class)->finalize (object);
}

static void
chatty_ma_account_class_init (ChattyMaAccountClass *klass)
{
  GObjectClass *object_class  = G_OBJECT_CLASS (klass);
  ChattyItemClass *item_class = CHATTY_ITEM_CLASS (klass);
  ChattyAccountClass *account_class = CHATTY_ACCOUNT_CLASS (klass);

  object_class->finalize = chatty_ma_account_finalize;

  item_class->get_protocols = chatty_ma_account_get_protocols;
  item_class->get_name = chatty_ma_account_get_name;
  item_class->set_name = chatty_ma_account_set_name;

  account_class->get_protocol_name = chatty_ma_account_get_protocol_name;
  account_class->get_status   = chatty_ma_account_get_status;
  account_class->get_username = chatty_ma_account_get_username;
  account_class->set_username = chatty_ma_account_set_username;
  account_class->get_enabled  = chatty_ma_account_get_enabled;
  account_class->set_enabled  = chatty_ma_account_set_enabled;
  account_class->get_password = chatty_ma_account_get_password;
  account_class->set_password = chatty_ma_account_set_password;
729 730
  account_class->connect      = chatty_ma_account_connect;
  account_class->disconnect   = chatty_ma_account_disconnect;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
  account_class->get_remember_password = chatty_ma_account_get_remember_password;
  account_class->save = chatty_ma_account_save;
  account_class->delete = chatty_ma_account_delete;
}

static void
chatty_ma_account_init (ChattyMaAccount *self)
{
  self->chat_list = g_list_store_new (CHATTY_TYPE_MA_CHAT);

  self->matrix_api = matrix_api_new (NULL);
  matrix_api_set_sync_callback (self->matrix_api,
                                (MatrixCallback)matrix_account_sync_cb, self);
}

ChattyMaAccount *
chatty_ma_account_new (const char *username,
                       const char *password)
{
  ChattyMaAccount *self;

  g_return_val_if_fail (username, NULL);

  self = g_object_new (CHATTY_TYPE_MA_ACCOUNT, NULL);

  chatty_account_set_username (CHATTY_ACCOUNT (self), username);
  chatty_account_set_password (CHATTY_ACCOUNT (self), password);

  return self;
}

762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
static char *
ma_account_get_value (const char *str,
                      const char *key)
{
  const char *start, *end;

  if (!str || !*str)
    return NULL;

  g_assert (key && *key);

  start = strstr (str, key);
  if (start) {
    start = start + strlen (key);
    while (*start && *start++ != '"')
      ;

    end = start - 1;
    do {
      end++;
      end = strchr (end, '"');
    } while (end && *(end - 1) == '\\' && *(end - 2) != '\\');

    if (end && end > start)
      return g_strndup (start, end - start);
  }

  return NULL;
}

ChattyMaAccount *
793
chatty_ma_account_new_secret (gpointer secret_retrievable)
794 795 796
{
  ChattyMaAccount *self = NULL;
  g_autoptr(GHashTable) attributes = NULL;
797
  SecretRetrievable *item = secret_retrievable;
798 799 800
  SecretValue *value;
  const char *username, *homeserver, *credentials;
  char *password, *token, *device_id;
801
  char *password_str, *token_str = NULL;
802

803
  g_return_val_if_fail (SECRET_IS_RETRIEVABLE (item), NULL);
804

805
  value = secret_retrievable_retrieve_secret_sync (item, NULL, NULL);
806 807
  credentials = secret_value_get_text (value);

808
  attributes = secret_retrievable_get_attributes (item);
809 810 811 812 813
  username = g_hash_table_lookup (attributes, CHATTY_USERNAME_ATTRIBUTE);
  homeserver = g_hash_table_lookup (attributes, CHATTY_SERVER_ATTRIBUTE);

  password = ma_account_get_value (credentials, "\"password\"");
  g_return_val_if_fail (password, NULL);
814
  password_str = g_strcompress (password);
815

816
  self = chatty_ma_account_new (username, password_str);
817 818 819 820
  token = ma_account_get_value (credentials, "\"access-token\"");
  device_id = ma_account_get_value (credentials, "\"device-id\"");
  chatty_ma_account_set_homeserver (self, homeserver);

821 822 823
  if (token)
    token_str = g_strcompress (token);

824 825
  if (token && device_id) {
    self->pickle_key = ma_account_get_value (credentials, "\"pickle-key\"");
826
    matrix_api_set_access_token (self->matrix_api, token_str, device_id);
827 828 829 830
  }

  matrix_utils_free_buffer (device_id);
  matrix_utils_free_buffer (password);
831
  matrix_utils_free_buffer (password_str);
832
  matrix_utils_free_buffer (token);
833
  matrix_utils_free_buffer (token_str);
834 835 836 837

  return self;
}

Mohammed Sadiq's avatar
Mohammed Sadiq committed
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
void
chatty_ma_account_set_history_db (ChattyMaAccount *self,
                                  gpointer         history_db)
{
  g_return_if_fail (CHATTY_IS_MA_ACCOUNT (self));
  g_return_if_fail (CHATTY_IS_HISTORY (history_db));
  g_return_if_fail (!self->history_db);

  self->history_db = g_object_ref (history_db);
}

static void
db_load_account_cb (GObject      *object,
                    GAsyncResult *result,
                    gpointer      user_data)
{
  ChattyMaAccount *self = user_data;
  GTask *task = (GTask *)result;
  g_autoptr(GError) error = NULL;
  gboolean enabled;

859 860
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
  g_assert (CHATTY_IS_MA_ACCOUNT (self));
  g_assert (G_IS_TASK (task));

  if (!self->matrix_enc) {
    const char *pickle;

    pickle = g_object_get_data (G_OBJECT (task), "pickle");
    self->matrix_enc = matrix_enc_new (self->matrix_db, pickle, self->pickle_key);
    matrix_api_set_enc (self->matrix_api, self->matrix_enc);
    if (!pickle)
      matrix_api_set_access_token (self->matrix_api, NULL, NULL);
    g_clear_pointer (&self->pickle_key, matrix_utils_free_buffer);
  }

  if (!matrix_db_load_account_finish (self->matrix_db, result, &error)) {
    if (error && !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
      g_warning ("Error loading account %s: %s",
                 chatty_account_get_username (CHATTY_ACCOUNT (self)),
                 error->message);
880
    CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
881 882 883 884 885 886 887 888 889
  }

  enabled = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (task), "enabled"));
  self->next_batch = g_strdup (g_object_get_data (G_OBJECT (task), "batch"));

  self->is_loading = TRUE;
  matrix_api_set_next_batch (self->matrix_api, self->next_batch);
  chatty_account_set_enabled (CHATTY_ACCOUNT (self), enabled);
  self->is_loading = FALSE;
890
  CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
891 892
}

893 894 895 896 897 898 899 900 901 902
static void
db_load_chats_cb (GObject      *object,
                  GAsyncResult *result,
                  gpointer      user_data)
{
  ChattyMaAccount *self = user_data;
  GTask *task = (GTask *)result;
  GPtrArray *chats = NULL;
  g_autoptr(GError) error = NULL;

903 904
  CHATTY_ENTRY;

905 906 907 908 909 910 911 912 913 914 915
  g_assert (CHATTY_IS_MA_ACCOUNT (self));
  g_assert (G_IS_TASK (task));

  chats = chatty_history_get_chats_finish (self->history_db, result, &error);
  self->db_chat_list = chats;

  if (error)
    g_warning ("Error getting chats: %s", error->message);

  matrix_db_load_account_async (self->matrix_db, CHATTY_ACCOUNT (self),
                                db_load_account_cb, self);
916
  CHATTY_EXIT;
917 918
}

Mohammed Sadiq's avatar
Mohammed Sadiq committed
919 920 921 922 923 924 925
void
chatty_ma_account_set_db (ChattyMaAccount *self,
                          gpointer         matrix_db)
{
  g_return_if_fail (CHATTY_IS_MA_ACCOUNT (self));
  g_return_if_fail (MATRIX_IS_DB (matrix_db));
  g_return_if_fail (!self->matrix_db);
926
  g_return_if_fail (self->history_db);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
927 928

  self->matrix_db = g_object_ref (matrix_db);
929 930
  chatty_history_get_chats_async (self->history_db, CHATTY_ACCOUNT (self),
                                  db_load_chats_cb, self);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
931 932 933 934 935 936 937 938 939 940 941 942
}

static void
ma_account_db_save_cb (GObject      *object,
                       GAsyncResult *result,
                       gpointer      user_data)
{
  ChattyMaAccount *self;
  g_autoptr(GTask) task = user_data;
  GError *error = NULL;
  gboolean status;

943 944
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
  g_assert (G_IS_ASYNC_RESULT (result));
  g_assert (G_IS_TASK (task));

  self = g_task_get_source_object (task);
  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  status = matrix_db_save_account_finish (self->matrix_db, result, &error);

  if (error || !status)
    self->save_account_pending = TRUE;

  if (error)
    g_task_return_error (task, error);
  else
    g_task_return_boolean (task, status);
960 961

  CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
962 963
}

964 965 966 967 968 969 970 971 972 973
static void
ma_account_save_cb (GObject      *object,
                    GAsyncResult *result,
                    gpointer      user_data)
{
  ChattyMaAccount *self;
  g_autoptr(GTask) task = user_data;
  GError *error = NULL;
  gboolean status;

974 975
  CHATTY_ENTRY;

976 977 978 979 980 981 982 983 984 985 986 987 988 989
  g_assert (G_IS_ASYNC_RESULT (result));
  g_assert (G_IS_TASK (task));

  self = g_task_get_source_object (task);
  g_assert (CHATTY_IS_MA_ACCOUNT (self));

  status = chatty_secret_store_save_finish (result, &error);

  if (error || !status)
    self->save_password_pending = TRUE;

  if (error) {
    g_task_return_error (task, error);
  } else if (self->save_account_pending) {
990 991 992 993 994
    char *pickle = NULL;

    if (matrix_api_get_access_token (self->matrix_api))
      pickle = matrix_enc_get_account_pickle (self->matrix_enc);

995 996 997
    self->save_account_pending = FALSE;
    matrix_db_save_account_async (self->matrix_db, CHATTY_ACCOUNT (self),
                                  chatty_account_get_enabled (CHATTY_ACCOUNT (self)),
998
                                  pickle,
999 1000 1001 1002 1003 1004
                                  matrix_api_get_device_id (self->matrix_api),
                                  matrix_api_get_next_batch (self->matrix_api),
                                  ma_account_db_save_cb, g_steal_pointer (&task));
  } else {
    g_task_return_boolean (task, status);
  }
1005 1006

  CHATTY_EXIT;
1007 1008
}

Mohammed Sadiq's avatar
Mohammed Sadiq committed
1009 1010 1011 1012 1013 1014 1015 1016 1017
void
chatty_ma_account_save_async (ChattyMaAccount     *self,
                              gboolean             force,
                              GCancellable        *cancellable,
                              GAsyncReadyCallback  callback,
                              gpointer             user_data)
{
  GTask *task;

1018 1019
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
1020 1021 1022 1023 1024 1025 1026 1027
  g_return_if_fail (CHATTY_IS_MA_ACCOUNT (self));
  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
  g_return_if_fail (*chatty_account_get_username (CHATTY_ACCOUNT (self)));
  g_return_if_fail (*chatty_account_get_password (CHATTY_ACCOUNT (self)));
  g_return_if_fail (*chatty_ma_account_get_homeserver (self));

  task = g_task_new (self, cancellable, callback, user_data);
  if (self->save_password_pending || force) {
1028 1029
    char *key = NULL;

1030
    if (self->matrix_enc && matrix_api_get_access_token (self->matrix_api))
1031
      key = matrix_enc_get_pickle_key (self->matrix_enc);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1032 1033

    self->save_password_pending = FALSE;
1034 1035 1036 1037 1038
    chatty_secret_store_save_async (CHATTY_ACCOUNT (self),
                                    g_strdup (matrix_api_get_access_token (self->matrix_api)),
                                    matrix_api_get_device_id (self->matrix_api),
                                    key, cancellable,
                                    ma_account_save_cb, task);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1039
  } else if (self->save_account_pending) {
1040 1041 1042 1043 1044
    char *pickle = NULL;

    if (matrix_api_get_access_token (self->matrix_api))
      pickle = matrix_enc_get_account_pickle (self->matrix_enc);

Mohammed Sadiq's avatar
Mohammed Sadiq committed
1045 1046 1047
    self->save_account_pending = FALSE;
    matrix_db_save_account_async (self->matrix_db, CHATTY_ACCOUNT (self),
                                  chatty_account_get_enabled (CHATTY_ACCOUNT (self)),
1048
                                  pickle,
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1049 1050 1051 1052
                                  matrix_api_get_device_id (self->matrix_api),
                                  matrix_api_get_next_batch (self->matrix_api),
                                  ma_account_db_save_cb, task);
  }
1053 1054

  CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1055 1056 1057 1058 1059 1060 1061
}

gboolean
chatty_ma_account_save_finish (ChattyMaAccount  *self,
                               GAsyncResult     *result,
                               GError          **error)
{
1062 1063
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
1064 1065 1066
  g_return_val_if_fail (CHATTY_IS_MA_ACCOUNT (self), FALSE);
  g_return_val_if_fail (G_IS_TASK (result), FALSE);

1067
  CHATTY_RETURN (g_task_propagate_boolean (G_TASK (result), error));
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
}

const char *
chatty_ma_account_get_homeserver (ChattyMaAccount *self)
{
  const char *homeserver;

  g_return_val_if_fail (CHATTY_IS_MA_ACCOUNT (self), "");

  homeserver = matrix_api_get_homeserver (self->matrix_api);

  if (homeserver)
    return homeserver;

  return "";
}

void
chatty_ma_account_set_homeserver (ChattyMaAccount *self,
                                  const char      *server_url)
{
  g_return_if_fail (CHATTY_IS_MA_ACCOUNT (self));

  matrix_api_set_homeserver (self->matrix_api, server_url);
}

GListModel *
chatty_ma_account_get_chat_list (ChattyMaAccount *self)
{
  g_return_val_if_fail (CHATTY_IS_MA_ACCOUNT (self), NULL);

  return G_LIST_MODEL (self->chat_list);
}

void
chatty_ma_account_send_file (ChattyMaAccount *self,
                             ChattyChat      *chat,
                             const char      *file_name)
{
  /* TODO */
}

static void
1111 1112 1113
ma_account_leave_chat_cb (GObject      *object,
                          GAsyncResult *result,
                          gpointer      user_data)
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1114 1115 1116 1117 1118 1119
{
  ChattyMaAccount *self;
  g_autoptr(GTask) task = user_data;
  GError *error = NULL;
  gboolean success;

1120 1121
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
1122 1123 1124 1125 1126
  g_assert (G_IS_TASK (task));

  self = g_task_get_source_object (task);
  g_assert (CHATTY_IS_MA_ACCOUNT (self));

1127
  success = matrix_api_leave_chat_finish (self->matrix_api, result, &error);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1128 1129 1130 1131 1132 1133 1134

  if (error && !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
    g_warning ("Error deleting chat: %s", error->message);

  /* Failed deleting from server, re-add in local chat list */
  if (!success) {
    ChattyChat *chat;
1135
    ChattyItemState old_state;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1136 1137 1138

    chat = g_task_get_task_data (task);
    g_list_store_append (self->chat_list, chat);
1139 1140 1141 1142 1143
    chatty_item_set_state (CHATTY_ITEM (chat), CHATTY_ITEM_HIDDEN);

    old_state = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (task), "state"));
    chatty_item_set_state (CHATTY_ITEM (chat), old_state);
    chatty_history_update_chat (self->history_db, chat);
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1144 1145 1146 1147 1148 1149
  }

  if (error)
    g_task_return_error (task, error);
  else
    g_task_return_boolean (task, success);
1150
  CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1151 1152 1153
}

void
1154 1155 1156 1157
chatty_ma_account_leave_chat_async (ChattyMaAccount     *self,
                                    ChattyChat          *chat,
                                    GAsyncReadyCallback  callback,
                                    gpointer             user_data)
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1158 1159 1160
{
  g_autoptr(GTask) task = NULL;

1161 1162
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
  g_return_if_fail (CHATTY_IS_MA_ACCOUNT (self));
  g_return_if_fail (CHATTY_IS_MA_CHAT (chat));

  task = g_task_new (self, NULL, callback, user_data);
  g_task_set_task_data (task, g_object_ref (chat), g_object_unref);

  /* Remove the item so that it’s no longer listed in chat list */
  /* TODO: Handle edge case where the item was deleted from two
   * different sessions the same time */
  if (!chatty_utils_remove_list_item (self->chat_list, chat))
    g_return_if_reached ();

1175 1176 1177 1178
  g_object_set_data (G_OBJECT (task), "state",
                     GINT_TO_POINTER (chatty_item_get_state (CHATTY_ITEM (chat))));
  chatty_item_set_state (CHATTY_ITEM (chat), CHATTY_ITEM_HIDDEN);
  chatty_history_update_chat (self->history_db, chat);
1179
  matrix_api_leave_chat_async (self->matrix_api,
1180
                               chatty_chat_get_chat_name (chat),
1181 1182
                               ma_account_leave_chat_cb,
                               g_steal_pointer (&task));
1183
  CHATTY_EXIT;
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1184 1185 1186
}

gboolean
1187 1188 1189
chatty_ma_account_leave_chat_finish (ChattyMaAccount  *self,
                                     GAsyncResult     *result,
                                     GError          **error)
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1190
{
1191 1192
  CHATTY_ENTRY;

Mohammed Sadiq's avatar
Mohammed Sadiq committed
1193 1194 1195
  g_return_val_if_fail (CHATTY_IS_MA_ACCOUNT (self), FALSE);
  g_return_val_if_fail (G_IS_TASK (result), FALSE);

1196
  CHATTY_RETURN (g_task_propagate_boolean (G_TASK (result), error));
Mohammed Sadiq's avatar
Mohammed Sadiq committed
1197
}
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

void
chatty_ma_account_add_chat (ChattyMaAccount *self,
                            ChattyChat      *chat)
{
  g_return_if_fail (CHATTY_IS_MA_ACCOUNT (self));
  g_return_if_fail (CHATTY_IS_MA_CHAT (chat));

  chatty_ma_chat_set_data (CHATTY_MA_CHAT (chat), CHATTY_ACCOUNT (self),
                           self->matrix_api, self->matrix_enc);
  g_list_store_append (self->chat_list, chat);
}