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

using ToxID in groupchats

This commit is contained in:
apprb 2014-11-24 00:55:49 +09:00
parent f59a67f08a
commit 56ea1f1e66
No known key found for this signature in database
GPG Key ID: B001911B5B22FB9B
8 changed files with 78 additions and 77 deletions

View File

@ -457,8 +457,7 @@ void Core::onAction(Tox*/* tox*/, int friendId, const uint8_t *cMessage, uint16_
void Core::onGroupAction(Tox*, int groupnumber, int peernumber, const uint8_t *action, uint16_t length, void* _core)
{
Core* core = static_cast<Core*>(_core);
emit core->groupMessageReceived(groupnumber, CString::toString(action, length),
core->getGroupPeerName(groupnumber, peernumber), true);
emit core->groupMessageReceived(groupnumber, peernumber, CString::toString(action, length), true);
}
void Core::onGroupInvite(Tox*, int friendnumber, uint8_t type, const uint8_t *data, uint16_t length,void *core)
@ -483,8 +482,8 @@ void Core::onGroupInvite(Tox*, int friendnumber, uint8_t type, const uint8_t *da
void Core::onGroupMessage(Tox*, int groupnumber, int peernumber, const uint8_t * message, uint16_t length, void *_core)
{
Core* core = static_cast<Core*>(_core);
emit core->groupMessageReceived(groupnumber, CString::toString(message, length),
core->getGroupPeerName(groupnumber, peernumber), false);
emit core->groupMessageReceived(groupnumber, peernumber, CString::toString(message, length), false);
}
void Core::onGroupNamelistChange(Tox*, int groupnumber, int peernumber, uint8_t change, void *core)
@ -1463,6 +1462,22 @@ QString Core::getGroupPeerName(int groupId, int peerId) const
return name;
}
ToxID Core::getGroupPeerToxID(int groupId, int peerId) const
{
ToxID peerToxID;
uint8_t rawID[TOX_CLIENT_ID_SIZE];
int res = tox_group_peer_pubkey(tox, groupId, peerId, rawID);
if (res == -1)
{
qWarning() << "Core::getGroupPeerToxID: Unknown error";
return peerToxID;
}
peerToxID = ToxID::fromString(CUserId::toString(rawID));
return peerToxID;
}
QList<QString> Core::getGroupPeerNames(int groupId) const
{
QList<QString> names;

View File

@ -53,6 +53,7 @@ public:
int getGroupNumberPeers(int groupId) const; ///< Return the number of peers in the group chat on success, or -1 on failure
QString getGroupPeerName(int groupId, int peerId) const; ///< Get the name of a peer of a group
ToxID getGroupPeerToxID(int groupId, int peerId) const; ///< Get the ToxID of a peer of a group
QList<QString> getGroupPeerNames(int groupId) const; ///< Get the names of the peers of a group
QString getFriendAddress(int friendNumber) const; ///< Get the full address if known, or Tox ID of a friend
QString getFriendUsername(int friendNumber) const; ///< Get the username of a friend
@ -160,7 +161,7 @@ signals:
void emptyGroupCreated(int groupnumber);
void groupInviteReceived(int friendnumber, uint8_t type, QByteArray publicKey);
void groupMessageReceived(int groupnumber, const QString& message, const QString& author, bool isAction);
void groupMessageReceived(int groupnumber, int peernumber, const QString& message, bool isAction);
void groupNamelistChanged(int groupnumber, int peernumber, uint8_t change);
void groupTitleChanged(int groupnumber, const QString& author, const QString& title);

View File

@ -67,6 +67,9 @@ void Group::removePeer(int peerId)
void Group::updatePeer(int peerId, QString name)
{
peers[peerId] = name;
QString toxid = Core::getInstance()->getGroupPeerToxID(groupId, peerId).publicKey;
toxids[toxid] = name;
widget->onUserListChanged();
chatForm->onUserListChanged();
}
@ -84,9 +87,14 @@ void Group::regeneratePeerList()
{
QList<QString> peerLst = Core::getInstance()->getGroupPeerNames(groupId);
peers.clear();
toxids.clear();
nPeers = peerLst.size();
for (int i = 0; i < peerLst.size(); i++)
{
peers[i] = peerLst.at(i);
QString toxid = Core::getInstance()->getGroupPeerToxID(groupId, i).publicKey;
toxids[toxid] = peerLst.at(i);
}
widget->onUserListChanged();
chatForm->onUserListChanged();
@ -141,3 +149,16 @@ int Group::getMentionedFlag() const
{
return userWasMentioned;
}
QString Group::resolveToxID(const ToxID &id) const
{
QString key = id.publicKey;
auto it = toxids.find(key);
if (it != toxids.end())
{
return *it;
}
return QString();
}

View File

@ -25,6 +25,7 @@
struct Friend;
class GroupWidget;
class GroupChatForm;
struct ToxID;
class Group : public QObject
{
@ -54,10 +55,13 @@ public:
void updatePeer(int peerId, QString newName);
void setName(const QString& name);
QString resolveToxID(const ToxID &id) const;
private:
GroupWidget* widget;
GroupChatForm* chatForm;
QMap<int, QString> peers;
QMap<QString, QString> toxids;
int hasNewMessages, userWasMentioned;
int groupId;
int nPeers;

View File

@ -31,6 +31,8 @@
#include "src/widget/tool/chattextedit.h"
#include "src/widget/maskablepixmapwidget.h"
#include "src/core.h"
#include "src/grouplist.h"
#include "src/group.h"
#include "src/friendlist.h"
#include "src/friend.h"
@ -201,16 +203,6 @@ void GenericChatForm::onSaveLogClicked()
file.close();
}
/**
* @deprecated The only reason it's still alive is because the groupchat API is a bit limited
*/
void GenericChatForm::addMessage(const QString& author, const QString &message, bool isAction, const QDateTime &datetime)
{
MessageActionPtr ca = genMessageActionAction(author, message, isAction, datetime);
ca->markAsSent();
chatWidget->insertMessage(ca);
}
MessageActionPtr GenericChatForm::addMessage(const ToxID& author, const QString &message, bool isAction,
const QDateTime &datetime, bool isSent)
{
@ -230,22 +222,12 @@ MessageActionPtr GenericChatForm::addSelfMessage(const QString &message, bool is
return ca;
}
/**
* @deprecated The only reason it's still alive is because the groupchat API is a bit limited
*/
void GenericChatForm::addAlertMessage(const QString& author, QString message, QDateTime datetime)
{
QString date = datetime.toString(Settings::getInstance().getTimestampFormat());
AlertAction *alact = new AlertAction(author, message, date);
alact->markAsSent();
chatWidget->insertMessage(ChatActionPtr(alact));
previousId.publicKey = author;
}
void GenericChatForm::addAlertMessage(const ToxID &author, QString message, QDateTime datetime)
{
QString authorStr = Core::getInstance()->getPeerName(author);
QString authorStr = resolveToxID(author);
if (authorStr.isEmpty())
authorStr = author.publicKey;
QString date = datetime.toString(Settings::getInstance().getTimestampFormat());
chatWidget->insertMessage(ChatActionPtr(new AlertAction(authorStr, message, date)));
previousId = author;
@ -314,42 +296,6 @@ void GenericChatForm::clearChatArea(bool notinform)
emit chatAreaCleared();
}
/**
* @deprecated The only reason it's still alive is because the groupchat API is a bit limited
*/
MessageActionPtr GenericChatForm::genMessageActionAction(const QString &author, QString message, bool isAction,
const QDateTime &datetime)
{
if (earliestMessage == nullptr)
{
earliestMessage = new QDateTime(datetime);
}
QString date = datetime.toString(Settings::getInstance().getTimestampFormat());
bool isMe = (author == Widget::getInstance()->getUsername());
if (!isAction && message.startsWith("/me "))
{ // always render actions regardless of what core thinks
isAction = true;
message = message.right(message.length()-4);
}
if (isAction)
{
previousId = ToxID(); // next msg has a name regardless
return MessageActionPtr(new ActionAction (author, message, date, isMe));
}
MessageActionPtr res;
if (previousId.publicKey == author)
res = MessageActionPtr(new MessageAction(QString(), message, date, isMe));
else
res = MessageActionPtr(new MessageAction(getElidedName(author), message, date, isMe));
previousId.publicKey = author;
return res;
}
MessageActionPtr GenericChatForm::genMessageActionAction(const ToxID& author, QString message, bool isAction, const QDateTime &datetime)
{
if (earliestMessage == nullptr)
@ -365,11 +311,7 @@ MessageActionPtr GenericChatForm::genMessageActionAction(const ToxID& author, QS
if (isMe)
authorStr = core->getUsername();
else {
Friend *f = FriendList::findFriend(author);
if (f)
authorStr = f->getDisplayedName();
else
authorStr = core->getPeerName(author);
authorStr = resolveToxID(author);
}
if (authorStr.isEmpty()) // Fallback if we can't find a username
@ -438,3 +380,21 @@ ChatActionPtr GenericChatForm::genSystemInfoAction(const QString &message, const
return ChatActionPtr(new SystemMessageAction(message, type, date));
}
QString GenericChatForm::resolveToxID(const ToxID &id)
{
Friend *f = FriendList::findFriend(id);
if (f)
{
return f->getDisplayedName();
} else {
for (auto it : GroupList::getAllGroups())
{
QString res = it->resolveToxID(id);
if (res.size())
return res;
}
}
return QString();
}

View File

@ -49,11 +49,9 @@ public:
virtual void setName(const QString &newName);
virtual void show(Ui::MainWindow &ui);
void addMessage(const QString& author, const QString &message, bool isAction, const QDateTime &datetime); ///< Deprecated
MessageActionPtr addMessage(const ToxID& author, const QString &message, bool isAction, const QDateTime &datetime, bool isSent);
MessageActionPtr addSelfMessage(const QString &message, bool isAction, const QDateTime &datetime, bool isSent);
void addSystemInfoMessage(const QString &message, const QString &type, const QDateTime &datetime);
void addAlertMessage(const QString& author, QString message, QDateTime datetime); ///< Deprecated
void addAlertMessage(const ToxID& author, QString message, QDateTime datetime);
bool isEmpty();
@ -74,11 +72,12 @@ protected slots:
protected:
QString getElidedName(const QString& name);
MessageActionPtr genMessageActionAction(const QString& author, QString message, bool isAction, const QDateTime &datetime); ///< Deprecated
MessageActionPtr genMessageActionAction(const ToxID& author, QString message, bool isAction, const QDateTime &datetime);
MessageActionPtr genSelfActionAction(QString message, bool isAction, const QDateTime &datetime);
ChatActionPtr genSystemInfoAction(const QString &message, const QString &type, const QDateTime &datetime);
QString resolveToxID(const ToxID &id);
ToxID previousId;
QMenu menu;
int curRow;

View File

@ -910,19 +910,20 @@ void Widget::onGroupInviteReceived(int32_t friendId, uint8_t type, QByteArray in
}
}
void Widget::onGroupMessageReceived(int groupnumber, const QString& message, const QString& author, bool isAction)
void Widget::onGroupMessageReceived(int groupnumber, int peernumber, const QString& message, bool isAction)
{
Group* g = GroupList::findGroup(groupnumber);
if (!g)
return;
ToxID author = Core::getInstance()->getGroupPeerToxID(groupnumber, peernumber);
QString name = core->getUsername();
bool targeted = (author != name) && message.contains(name, Qt::CaseInsensitive);
bool targeted = (!author.isMine()) && message.contains(name, Qt::CaseInsensitive);
if (targeted)
g->getChatForm()->addAlertMessage(author, message, QDateTime::currentDateTime());
else
g->getChatForm()->addMessage(author, message, isAction, QDateTime::currentDateTime());
g->getChatForm()->addMessage(author, message, isAction, QDateTime::currentDateTime(), true);
if ((static_cast<GenericChatroomWidget*>(g->getGroupWidget()) != activeChatroomWidget) || isMinimized() || !isActiveWindow())
{

View File

@ -116,7 +116,7 @@ private slots:
void onReceiptRecieved(int friendId, int receipt);
void onEmptyGroupCreated(int groupId);
void onGroupInviteReceived(int32_t friendId, uint8_t type, QByteArray invite);
void onGroupMessageReceived(int groupnumber, const QString& message, const QString& author, bool isAction);
void onGroupMessageReceived(int groupnumber, int peernumber, const QString& message, bool isAction);
void onGroupNamelistChanged(int groupnumber, int peernumber, uint8_t change);
void onGroupTitleChanged(int groupnumber, const QString& author, const QString& title);
void removeFriend(int friendId);