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); bool targeted = !author.isMine() && (message.contains(nameMention) || message.contains(sanitizedNameMention)); if (targeted && !isAction) g->getChatForm()->addAlertMessage(author, message, QDateTime::currentDateTime()); else g->getChatForm()->addMessage(author, message, isAction, QDateTime::currentDateTime(), true); g->setEventFlag(static_cast<GenericChatroomWidget*>(g->getGroupWidget()) != activeChatroomWidget); if (targeted || Settings::getInstance().getGroupAlwaysNotify()) newMessageAlert(g->getGroupWidget()); if (targeted) g->setMentionedFlag(true); // useful for highlighting line or desktop notifications g->getGroupWidget()->updateStatusLight(); }
void ChatForm::loadHistory(QDateTime since, bool processUndelivered) { QDateTime now = QDateTime::currentDateTime(); if (since > now) return; if (earliestMessage) { if (*earliestMessage < since) return; if (*earliestMessage < now) { now = *earliestMessage; now = now.addMSecs(-1); } } auto msgs = HistoryKeeper::getInstance()->getChatHistory(HistoryKeeper::ctSingle, f->getToxID().publicKey, since, now); ToxID storedPrevId; std::swap(storedPrevId, previousId); QList<ChatActionPtr> historyMessages; QDate lastDate(1,0,0); for (const auto &it : msgs) { // Show the date every new day QDateTime msgDateTime = it.timestamp.toLocalTime(); QDate msgDate = msgDateTime.date(); if (msgDate > lastDate) { lastDate = msgDate; historyMessages.append(genSystemInfoAction(msgDate.toString(),"",QDateTime())); } // Show each messages ToxID msgSender = ToxID::fromString(it.sender); MessageActionPtr ca = genMessageActionAction(msgSender, it.message, false, msgDateTime); if (it.isSent || !msgSender.isMine()) { ca->markAsSent(); } else { if (processUndelivered) { int rec; if (ca->isAction()) rec = Core::getInstance()->sendAction(f->getFriendID(), ca->getRawMessage()); else rec = Core::getInstance()->sendMessage(f->getFriendID(), ca->getRawMessage()); registerReceipt(rec, it.id, ca); } } historyMessages.append(ca); } std::swap(storedPrevId, previousId); int savedSliderPos = chatWidget->verticalScrollBar()->maximum() - chatWidget->verticalScrollBar()->value(); if (earliestMessage != nullptr) *earliestMessage = since; chatWidget->insertMessagesTop(historyMessages); savedSliderPos = chatWidget->verticalScrollBar()->maximum() - savedSliderPos; chatWidget->verticalScrollBar()->setValue(savedSliderPos); }