1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

save alias

This commit is contained in:
PKEv 2015-08-28 00:07:24 +03:00
parent bae8a0d0c8
commit 564b32eb7c
5 changed files with 32 additions and 19 deletions

View File

@ -638,7 +638,7 @@ void Core::requestFriendship(const QString& friendAddress, const QString& messag
if (message.length()) if (message.length())
inviteStr = tr("/me offers friendship, \"%1\"").arg(message); inviteStr = tr("/me offers friendship, \"%1\"").arg(message);
HistoryKeeper::getInstance()->addChatEntry(userId, inviteStr, getSelfId().publicKey, QDateTime::currentDateTime(), true); HistoryKeeper::getInstance()->addChatEntry(userId, inviteStr, getSelfId().publicKey, QDateTime::currentDateTime(), true, QString());
emit friendAdded(friendId, userId); emit friendAdded(friendId, userId);
emit friendshipChanged(friendId); emit friendshipChanged(friendId);
} }

View File

@ -42,7 +42,7 @@ HistoryKeeper *HistoryKeeper::getInstance()
{ {
QList<QString> initLst; QList<QString> initLst;
initLst.push_back(QString("CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER NOT NULL, ") + initLst.push_back(QString("CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER NOT NULL, ") +
QString("chat_id INTEGER NOT NULL, sender INTEGER NOT NULL, message TEXT NOT NULL);")); QString("chat_id INTEGER NOT NULL, sender INTEGER NOT NULL, message TEXT NOT NULL, alias TEXT);"));
initLst.push_back(QString("CREATE TABLE IF NOT EXISTS aliases (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id TEXT UNIQUE NOT NULL);")); initLst.push_back(QString("CREATE TABLE IF NOT EXISTS aliases (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id TEXT UNIQUE NOT NULL);"));
initLst.push_back(QString("CREATE TABLE IF NOT EXISTS chats (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE NOT NULL, ctype INTEGER NOT NULL);")); initLst.push_back(QString("CREATE TABLE IF NOT EXISTS chats (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE NOT NULL, ctype INTEGER NOT NULL);"));
initLst.push_back(QString("CREATE TABLE IF NOT EXISTS sent_status (id INTEGER PRIMARY KEY AUTOINCREMENT, status INTEGER NOT NULL DEFAULT 0);")); initLst.push_back(QString("CREATE TABLE IF NOT EXISTS sent_status (id INTEGER PRIMARY KEY AUTOINCREMENT, status INTEGER NOT NULL DEFAULT 0);"));
@ -106,6 +106,7 @@ HistoryKeeper::HistoryKeeper(GenericDdInterface *db_) :
chat_id -- current chat ID (resolves from chats table) chat_id -- current chat ID (resolves from chats table)
sender -- sender's ID (resolves from aliases table) sender -- sender's ID (resolves from aliases table)
message message
alias -- sender's alias in
*/ */
// for old tables: // for old tables:
@ -124,6 +125,15 @@ HistoryKeeper::HistoryKeeper(GenericDdInterface *db_) :
db->exec(cmd); db->exec(cmd);
} }
} }
//check table stuct
ans = db->exec("PRAGMA table_info(\"history\")");
ans.seek(5);
if (!ans.value(1).toString().contains("alias"))
{
//add collum in table
db->exec("ALTER TABLE history ADD COLUMN alias TEXT");
}
updateChatsID(); updateChatsID();
updateAliases(); updateAliases();
@ -159,9 +169,9 @@ void HistoryKeeper::removeFriendHistory(const QString& chat)
db->exec("COMMIT TRANSACTION;"); db->exec("COMMIT TRANSACTION;");
} }
qint64 HistoryKeeper::addChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt, bool isSent) qint64 HistoryKeeper::addChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt, bool isSent, QString dispName)
{ {
QList<QString> cmds = generateAddChatEntryCmd(chat, message, sender, dt, isSent); QList<QString> cmds = generateAddChatEntryCmd(chat, message, sender, dt, isSent, dispName);
db->exec("BEGIN TRANSACTION;"); db->exec("BEGIN TRANSACTION;");
for (auto &it : cmds) for (auto &it : cmds)
@ -186,7 +196,7 @@ QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::C
QSqlQuery dbAnswer; QSqlQuery dbAnswer;
if (ct == ctSingle) if (ct == ctSingle)
{ {
dbAnswer = db->exec(QString("SELECT history.id, timestamp, user_id, message, status FROM history LEFT JOIN sent_status ON history.id = sent_status.id ") + dbAnswer = db->exec(QString("SELECT history.id, timestamp, user_id, message, status, alias FROM history LEFT JOIN sent_status ON history.id = sent_status.id ") +
QString("INNER JOIN aliases ON history.sender = aliases.id AND timestamp BETWEEN %1 AND %2 AND chat_id = %3;") QString("INNER JOIN aliases ON history.sender = aliases.id AND timestamp BETWEEN %1 AND %2 AND chat_id = %3;")
.arg(time64_from).arg(time64_to).arg(chat_id)); .arg(time64_from).arg(time64_to).arg(chat_id));
} }
@ -200,6 +210,7 @@ QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::C
qint64 id = dbAnswer.value(0).toLongLong(); qint64 id = dbAnswer.value(0).toLongLong();
qint64 timeInt = dbAnswer.value(1).toLongLong(); qint64 timeInt = dbAnswer.value(1).toLongLong();
QString sender = dbAnswer.value(2).toString(); QString sender = dbAnswer.value(2).toString();
QString senderName = dbAnswer.value(5).toString();
QString message = unWrapMessage(dbAnswer.value(3).toString()); QString message = unWrapMessage(dbAnswer.value(3).toString());
bool isSent = true; bool isSent = true;
if (!dbAnswer.value(4).isNull()) if (!dbAnswer.value(4).isNull())
@ -207,7 +218,7 @@ QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::C
QDateTime time = QDateTime::fromMSecsSinceEpoch(timeInt); QDateTime time = QDateTime::fromMSecsSinceEpoch(timeInt);
res.push_back(HistMessage(id, "", sender, message, time, isSent)); res.push_back(HistMessage(id, "", sender, message, time, isSent, senderName));
} }
return res; return res;
@ -216,7 +227,7 @@ QList<HistoryKeeper::HistMessage> HistoryKeeper::getChatHistory(HistoryKeeper::C
QList<HistoryKeeper::HistMessage> HistoryKeeper::exportMessages() QList<HistoryKeeper::HistMessage> HistoryKeeper::exportMessages()
{ {
QSqlQuery dbAnswer; QSqlQuery dbAnswer;
dbAnswer = db->exec(QString("SELECT history.id, timestamp, user_id, message, status, name FROM history LEFT JOIN sent_status ON history.id = sent_status.id ") + dbAnswer = db->exec(QString("SELECT history.id, timestamp, user_id, message, status, name, alias FROM history LEFT JOIN sent_status ON history.id = sent_status.id ") +
QString("INNER JOIN aliases ON history.sender = aliases.id INNER JOIN chats ON history.chat_id = chats.id;")); QString("INNER JOIN aliases ON history.sender = aliases.id INNER JOIN chats ON history.chat_id = chats.id;"));
QList<HistMessage> res; QList<HistMessage> res;
@ -231,9 +242,10 @@ QList<HistoryKeeper::HistMessage> HistoryKeeper::exportMessages()
if (!dbAnswer.value(4).isNull()) if (!dbAnswer.value(4).isNull())
isSent = dbAnswer.value(4).toBool(); isSent = dbAnswer.value(4).toBool();
QString chat = dbAnswer.value(5).toString(); QString chat = dbAnswer.value(5).toString();
QString dispName = dbAnswer.value(6).toString();
QDateTime time = QDateTime::fromMSecsSinceEpoch(timeInt); QDateTime time = QDateTime::fromMSecsSinceEpoch(timeInt);
res.push_back(HistMessage(id, chat, sender, message, time, isSent)); res.push_back(HistMessage(id, chat, sender, message, time, isSent, dispName));
} }
return res; return res;
@ -244,7 +256,7 @@ void HistoryKeeper::importMessages(const QList<HistoryKeeper::HistMessage> &lst)
db->exec("BEGIN TRANSACTION;"); db->exec("BEGIN TRANSACTION;");
for (const HistMessage &msg : lst) for (const HistMessage &msg : lst)
{ {
QList<QString> cmds = generateAddChatEntryCmd(msg.chat, msg.message, msg.sender, msg.timestamp, msg.isSent); QList<QString> cmds = generateAddChatEntryCmd(msg.chat, msg.message, msg.sender, msg.timestamp, msg.isSent, QString()); //!!!
for (auto &it : cmds) for (auto &it : cmds)
db->exec(it); db->exec(it);
@ -253,15 +265,15 @@ void HistoryKeeper::importMessages(const QList<HistoryKeeper::HistMessage> &lst)
db->exec("COMMIT TRANSACTION;"); db->exec("COMMIT TRANSACTION;");
} }
QList<QString> HistoryKeeper::generateAddChatEntryCmd(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt, bool isSent) QList<QString> HistoryKeeper::generateAddChatEntryCmd(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt, bool isSent, QString dispName)
{ {
QList<QString> cmds; QList<QString> cmds;
int chat_id = getChatID(chat, ctSingle).first; int chat_id = getChatID(chat, ctSingle).first;
int sender_id = getAliasID(sender); int sender_id = getAliasID(sender);
cmds.push_back(QString("INSERT INTO history (timestamp, chat_id, sender, message) VALUES (%1, %2, %3, '%4');") cmds.push_back(QString("INSERT INTO history (timestamp, chat_id, sender, message, alias) VALUES (%1, %2, %3, '%4', '%5');")
.arg(dt.toMSecsSinceEpoch()).arg(chat_id).arg(sender_id).arg(wrapMessage(message))); .arg(dt.toMSecsSinceEpoch()).arg(chat_id).arg(sender_id).arg(wrapMessage(message)).arg(dispName));
cmds.push_back(QString("INSERT INTO sent_status (status) VALUES (%1);").arg(isSent)); cmds.push_back(QString("INSERT INTO sent_status (status) VALUES (%1);").arg(isSent));
return cmds; return cmds;

View File

@ -35,8 +35,8 @@ public:
struct HistMessage struct HistMessage
{ {
HistMessage(qint64 id, QString chat, QString sender, QString message, QDateTime timestamp, bool isSent) : HistMessage(qint64 id, QString chat, QString sender, QString message, QDateTime timestamp, bool isSent, QString dispName) :
id(id), chat(chat), sender(sender), message(message), timestamp(timestamp), isSent(isSent) {} id(id), chat(chat), sender(sender), message(message), timestamp(timestamp), isSent(isSent), dispName(dispName) {}
qint64 id; qint64 id;
QString chat; QString chat;
@ -44,6 +44,7 @@ public:
QString message; QString message;
QDateTime timestamp; QDateTime timestamp;
bool isSent; bool isSent;
QString dispName;
}; };
virtual ~HistoryKeeper(); virtual ~HistoryKeeper();
@ -59,7 +60,7 @@ public:
static QList<HistMessage> exportMessagesDeleteFile(int encrypted = -1); static QList<HistMessage> exportMessagesDeleteFile(int encrypted = -1);
void removeFriendHistory(const QString& chat); void removeFriendHistory(const QString& chat);
qint64 addChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt, bool isSent); qint64 addChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt, bool isSent, QString dispName);
qint64 addGroupChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt); qint64 addGroupChatEntry(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt);
QList<HistMessage> getChatHistory(ChatType ct, const QString &chat, const QDateTime &time_from, const QDateTime &time_to); QList<HistMessage> getChatHistory(ChatType ct, const QString &chat, const QDateTime &time_from, const QDateTime &time_to);
void markAsSent(int m_id); void markAsSent(int m_id);
@ -81,7 +82,7 @@ private:
int getAliasID(const QString &id_str); int getAliasID(const QString &id_str);
QString wrapMessage(const QString &str); QString wrapMessage(const QString &str);
QString unWrapMessage(const QString &str); QString unWrapMessage(const QString &str);
QList<QString> generateAddChatEntryCmd(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt, bool isSent); QList<QString> generateAddChatEntryCmd(const QString& chat, const QString& message, const QString& sender, const QDateTime &dt, bool isSent, QString dispName);
ChatType convertToChatType(int); ChatType convertToChatType(int);

View File

@ -837,7 +837,7 @@ void ChatForm::loadHistory(QDateTime since, bool processUndelivered)
// Show each messages // Show each messages
ToxId authorId = ToxId(it.sender); ToxId authorId = ToxId(it.sender);
QString authorStr = authorId.isActiveProfile() ? Core::getInstance()->getUsername() : resolveToxId(authorId); QString authorStr = !it.dispName.isEmpty() ? it.dispName : (authorId.isActiveProfile() ? Core::getInstance()->getUsername() : resolveToxId(authorId));
bool isAction = it.message.startsWith("/me ", Qt::CaseInsensitive); bool isAction = it.message.startsWith("/me ", Qt::CaseInsensitive);
ChatMessage::Ptr msg = ChatMessage::createChatMessage(authorStr, ChatMessage::Ptr msg = ChatMessage::createChatMessage(authorStr,
@ -1045,7 +1045,7 @@ void ChatForm::SendMessageStr(QString msg)
bool status = !Settings::getInstance().getFauxOfflineMessaging(); bool status = !Settings::getInstance().getFauxOfflineMessaging();
int id = HistoryKeeper::getInstance()->addChatEntry(f->getToxId().publicKey, qt_msg_hist, int id = HistoryKeeper::getInstance()->addChatEntry(f->getToxId().publicKey, qt_msg_hist,
Core::getInstance()->getSelfId().publicKey, timestamp, status); Core::getInstance()->getSelfId().publicKey, timestamp, status, Core::getInstance()->getUsername());
ChatMessage::Ptr ma = addSelfMessage(qt_msg, isAction, timestamp, false); ChatMessage::Ptr ma = addSelfMessage(qt_msg, isAction, timestamp, false);

View File

@ -888,7 +888,7 @@ void Widget::onFriendMessageReceived(int friendId, const QString& message, bool
f->getChatForm()->addMessage(f->getToxId(), message, isAction, timestamp, true); f->getChatForm()->addMessage(f->getToxId(), message, isAction, timestamp, true);
HistoryKeeper::getInstance()->addChatEntry(f->getToxId().publicKey, isAction ? "/me " + f->getDisplayedName() + " " + message : message, HistoryKeeper::getInstance()->addChatEntry(f->getToxId().publicKey, isAction ? "/me " + f->getDisplayedName() + " " + message : message,
f->getToxId().publicKey, timestamp, true); f->getToxId().publicKey, timestamp, true, f->getDisplayedName());
f->setEventFlag(f->getFriendWidget() != activeChatroomWidget); f->setEventFlag(f->getFriendWidget() != activeChatroomWidget);
newMessageAlert(f->getFriendWidget()); newMessageAlert(f->getFriendWidget());