Skip to content

Commit

Permalink
issue 84: implement conversation log export (#93)
Browse files Browse the repository at this point in the history
- fixed bug in to_string(tego_file_hash_t* const)
- chat log can now be exported via the 'Export Conversation...'
    contact menu item
- logging timestamps are all calculated by the local user
    (ie timestamps from messages are ignored)

Co-authored-by: Richard Pospesel <[email protected]>
  • Loading branch information
m-simonelli and morganava committed Jun 27, 2021
1 parent 559762c commit 8cfea82
Show file tree
Hide file tree
Showing 38 changed files with 3,414 additions and 2,759 deletions.
4 changes: 3 additions & 1 deletion src/libtego/include/tego/tego.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ namespace tego

if (fileHash == nullptr) return {};

// size of string including null terminator
const auto hashSize = tego_file_hash_string_size(fileHash, tego::throw_on_error());

std::string hashString(hashSize, 0);
// std::string expects length as arg, not buffer size
std::string hashString(hashSize-1, 0);
tego_file_hash_to_string(fileHash, hashString.data(), hashSize, tego::throw_on_error());

return hashString;
Expand Down
13 changes: 6 additions & 7 deletions src/libtego/source/core/ConversationModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ std::tuple<tego_file_transfer_id_t, std::unique_ptr<tego_file_hash_t>, tego_file
{
logger::println("Sending file: {}", file_uri);

MessageData message(file_uri, QDateTime::currentDateTime(), lastMessageId++, Queued);
message.type = ConversationModel::MessageData::Type::File;
MessageData message(File, file_uri, QDateTime::currentDateTime(), lastMessageId++, Queued);
message.type = ConversationModel::MessageType::File;

std::unique_ptr<tego_file_hash_t> fileHash;

Expand Down Expand Up @@ -184,8 +184,7 @@ tego_message_id_t ConversationModel::sendMessage(const QString &text)
if (text.isEmpty())
return 0;

MessageData message(text, QDateTime::currentDateTime(), lastMessageId++, Queued);
message.type = ConversationModel::MessageData::Type::Message;
MessageData message(Message, text, QDateTime::currentDateTime(), lastMessageId++, Queued);

if (m_contact->connection())
{
Expand Down Expand Up @@ -287,14 +286,14 @@ void ConversationModel::sendQueuedMessages()
bool attempted = false;
switch (m.type)
{
case ConversationModel::MessageData::Type::Message:
case ConversationModel::MessageType::Message:
if (chat_channel->isOpened())
{
m.status = chat_channel->sendChatMessageWithId(m.text, m.time, m.identifier) ? Sending : Error;
attempted = true;
}
break;
case ConversationModel::MessageData::Type::File:
case ConversationModel::MessageType::File:
if (file_channel->isOpened())
{
logger::println("Attempted to send queued file: {}", m.text);
Expand Down Expand Up @@ -344,7 +343,7 @@ void ConversationModel::messageReceived(const QString &text, const QDateTime &ti
}

beginInsertRows(QModelIndex(), row, row);
MessageData message(text, time, id, Received);
MessageData message(Message, text, time, id, Received);
messages.insert(row, message);
endInsertRows();
prune();
Expand Down
14 changes: 8 additions & 6 deletions src/libtego/source/core/ConversationModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class ConversationModel : public QAbstractListModel
Error
};

enum MessageType {
Message,
File
};

ConversationModel(QObject *parent = 0);

ContactUser *contact() const { return m_contact; }
Expand Down Expand Up @@ -100,19 +105,16 @@ private slots:

private:
struct MessageData {
enum Type {
Message,
File
} type;
MessageType type;
QString text;
tego_file_hash_t fileHash;
QDateTime time;
MessageId identifier;
MessageStatus status;
quint8 attemptCount;

MessageData(const QString &text, const QDateTime &time, MessageId id, MessageStatus status)
: text(text), time(time), identifier(id), status(status), attemptCount(0)
MessageData(MessageType type, const QString &text, const QDateTime &time, MessageId id, MessageStatus status)
: type(type), text(text), time(time), identifier(id), status(status), attemptCount(0)
{
}
};
Expand Down
3 changes: 3 additions & 0 deletions src/libtego_ui/libtego_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ namespace
auto userIdentity = shims::UserIdentity::userIdentity;
auto contactsManager = userIdentity->getContacts();
auto contact = contactsManager->getShimContactByContactId(serviceIdToContactId(serviceId));
auto conversation = contact->conversation();

if (contact != nullptr)
{
Expand All @@ -383,10 +384,12 @@ namespace
case tego_user_status_online:
contact->setStatus(shims::ContactUser::Online);
contactsManager->setContactStatus(contact, shims::ContactUser::Online);
conversation->setStatus(shims::ContactUser::Online);
break;
case tego_user_status_offline:
contact->setStatus(shims::ContactUser::Offline);
contactsManager->setContactStatus(contact, shims::ContactUser::Offline);
conversation->setStatus(shims::ContactUser::Offline);
break;
default:
break;
Expand Down
6 changes: 6 additions & 0 deletions src/libtego_ui/precomp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
#include <type_traits>
#include <cstdint>
#include <functional>
#include <fstream>
#include <iterator>

// fmt
#include <fmt/format.h>
#include <fmt/ostream.h>

// Qt
#include <QClipboard>
Expand Down
5 changes: 5 additions & 0 deletions src/libtego_ui/shims/ContactUser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ namespace shims
this->conversationModel->sendFile();
}

bool ContactUser::exportConversation()
{
return this->conversationModel->exportConversation();
}

std::unique_ptr<tego_user_id_t> ContactUser::toTegoUserId() const
{
logger::println("serviceId : {}", this->serviceId);
Expand Down
1 change: 1 addition & 0 deletions src/libtego_ui/shims/ContactUser.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace shims

Q_INVOKABLE void deleteContact();
Q_INVOKABLE void sendFile();
Q_INVOKABLE bool exportConversation();

std::unique_ptr<tego_user_id_t> toTegoUserId() const;

Expand Down
Loading

0 comments on commit 8cfea82

Please sign in to comment.