webchathelpers.cpp 6.42 KB
Newer Older
1
/***************************************************************************
2
 * Copyright (C) 2017-2019 by Savoir-faire Linux                           *
3
 * Author: Alexandre Viau <alexandre.viau@savoirfairelinux.com>            *
4
 * Author: S�bastien Blin <sebastien.blin@savoirfairelinux.com>            *
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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
 * Author: Hugo Lefeuvre <hugo.lefeuvre@savoirfairelinux.com>              *
 * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>          *

 *                                                                         *
 * This program 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 3 of the License, or       *
 * (at your option) any later version.                                     *
 *                                                                         *
 * This program 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 program.  If not, see <http://www.gnu.org/licenses/>.   *
 **************************************************************************/

#include "webchathelpers.h"

QJsonObject
buildInteractionJson(lrc::api::ConversationModel& conversationModel,
    const uint64_t msgId,
    const lrc::api::interaction::Info& interaction)
{
    auto sender = QString(interaction.authorUri.c_str());
    auto timestamp = QString::number(interaction.timestamp);
    auto direction = lrc::api::interaction::isOutgoing(interaction) ? QString("out") : QString("in");

    QJsonObject interactionObject = QJsonObject();
    interactionObject.insert("text", QJsonValue(QString(interaction.body.c_str())));
    interactionObject.insert("id", QJsonValue(QString::number(msgId)));
    interactionObject.insert("sender", QJsonValue(sender));
    interactionObject.insert("sender_contact_method", QJsonValue(sender));
    interactionObject.insert("timestamp", QJsonValue(timestamp));
    interactionObject.insert("direction", QJsonValue(direction));

    switch (interaction.type)
    {
    case lrc::api::interaction::Type::TEXT:
        interactionObject.insert("type", QJsonValue("text"));
        break;
    case lrc::api::interaction::Type::CALL:
        interactionObject.insert("type", QJsonValue("call"));
        break;
    case lrc::api::interaction::Type::CONTACT:
        interactionObject.insert("type", QJsonValue("contact"));
        break;
53
    case lrc::api::interaction::Type::DATA_TRANSFER: {
54 55 56 57 58 59 60 61 62 63 64
        interactionObject.insert("type", QJsonValue("data_transfer"));
        lrc::api::datatransfer::Info info = {};
        conversationModel.getTransferInfo(msgId, info);
        if (info.status != lrc::api::datatransfer::Status::INVALID) {
            interactionObject.insert("totalSize", QJsonValue(qint64(info.totalSize)));
            interactionObject.insert("progress", QJsonValue(qint64(info.progress)));
        }
        break;
    }
    case lrc::api::interaction::Type::INVALID:
    default:
65 66 67 68 69
        return {};
    }

    if (interaction.isRead) {
        interactionObject.insert("delivery_status", QJsonValue("read"));
70 71 72 73
    }

    switch (interaction.status)
    {
74
    case lrc::api::interaction::Status::SUCCESS:
75 76
        interactionObject.insert("delivery_status", QJsonValue("sent"));
        break;
77
    case lrc::api::interaction::Status::FAILURE:
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    case lrc::api::interaction::Status::TRANSFER_ERROR:
        interactionObject.insert("delivery_status", QJsonValue("failure"));
        break;
    case lrc::api::interaction::Status::TRANSFER_UNJOINABLE_PEER:
        interactionObject.insert("delivery_status", QJsonValue("unjoinable peer"));
        break;
    case lrc::api::interaction::Status::SENDING:
        interactionObject.insert("delivery_status", QJsonValue("sending"));
        break;
    case lrc::api::interaction::Status::TRANSFER_CREATED:
        interactionObject.insert("delivery_status", QJsonValue("connecting"));
        break;
    case lrc::api::interaction::Status::TRANSFER_ACCEPTED:
        interactionObject.insert("delivery_status", QJsonValue("accepted"));
        break;
    case lrc::api::interaction::Status::TRANSFER_CANCELED:
        interactionObject.insert("delivery_status", QJsonValue("canceled"));
        break;
    case lrc::api::interaction::Status::TRANSFER_ONGOING:
        interactionObject.insert("delivery_status", QJsonValue("ongoing"));
        break;
    case lrc::api::interaction::Status::TRANSFER_AWAITING_PEER:
        interactionObject.insert("delivery_status", QJsonValue("awaiting peer"));
        break;
    case lrc::api::interaction::Status::TRANSFER_AWAITING_HOST:
        interactionObject.insert("delivery_status", QJsonValue("awaiting host"));
        break;
    case lrc::api::interaction::Status::TRANSFER_TIMEOUT_EXPIRED:
        interactionObject.insert("delivery_status", QJsonValue("awaiting peer timeout"));
        break;
    case lrc::api::interaction::Status::TRANSFER_FINISHED:
        interactionObject.insert("delivery_status", QJsonValue("finished"));
        break;
    case lrc::api::interaction::Status::INVALID:
    case lrc::api::interaction::Status::UNKNOWN:
    default:
        interactionObject.insert("delivery_status", QJsonValue("unknown"));
        break;
    }
    return interactionObject;
}

QString
interactionToJsonInteractionObject(lrc::api::ConversationModel& conversationModel,
    const uint64_t msgId,
    const lrc::api::interaction::Info& interaction)
{
    auto interactionObject = buildInteractionJson(conversationModel, msgId, interaction);
    return QString(QJsonDocument(interactionObject).toJson(QJsonDocument::Compact));
}

QString
interactionsToJsonArrayObject(lrc::api::ConversationModel& conversationModel,
    const std::map<uint64_t,
    lrc::api::interaction::Info> interactions)
{
    QJsonArray array;
    for (const auto& interaction : interactions) {
136 137 138 139
        auto interactionObject = buildInteractionJson(conversationModel, interaction.first, interaction.second);
        if (!interactionObject.isEmpty()) {
            array.append(interactionObject);
        }
140 141 142
    }
    return QString(QJsonDocument(array).toJson(QJsonDocument::Compact));
}