fixed a few bugs in post version collecting

This commit is contained in:
csoler 2017-02-09 16:15:35 +01:00
parent f8056e5751
commit b93130ac77
2 changed files with 73 additions and 34 deletions

View File

@ -200,7 +200,7 @@ GxsForumThreadWidget::GxsForumThreadWidget(const RsGxsGroupId &forumId, QWidget
ui->threadTreeWidget->setItemDelegateForColumn(COLUMN_THREAD_DISTRIBUTION,new DistributionItemDelegate()) ; ui->threadTreeWidget->setItemDelegateForColumn(COLUMN_THREAD_DISTRIBUTION,new DistributionItemDelegate()) ;
connect(ui->versions_CB, SIGNAL(currentItemChanged(int)), this, SLOT(updateCurrentPostVersion(int))); connect(ui->versions_CB, SIGNAL(currentIndexChanged(int)), this, SLOT(changedThread()));
connect(ui->threadTreeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(threadListCustomPopupMenu(QPoint))); connect(ui->threadTreeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(threadListCustomPopupMenu(QPoint)));
connect(ui->postText, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuTextBrowser(QPoint))); connect(ui->postText, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuTextBrowser(QPoint)));
@ -698,6 +698,12 @@ void GxsForumThreadWidget::changedThread()
if (!item || !item->isSelected()) { if (!item || !item->isSelected()) {
mThreadId.clear(); mThreadId.clear();
} else { } else {
mThreadId.clear();
if(ui->versions_CB->count() > 0)
mThreadId = RsGxsMessageId(ui->versions_CB->itemData(ui->versions_CB->currentIndex()).toString().toStdString()) ;
if(mThreadId.isNull())
mThreadId = RsGxsMessageId(item->data(COLUMN_THREAD_MSGID, Qt::DisplayRole).toString().toStdString()); mThreadId = RsGxsMessageId(item->data(COLUMN_THREAD_MSGID, Qt::DisplayRole).toString().toStdString());
} }
@ -1024,13 +1030,13 @@ void GxsForumThreadWidget::fillThreadFinished()
mLastViewType = thread->mViewType; mLastViewType = thread->mViewType;
mLastForumID = groupId(); mLastForumID = groupId();
ui->threadTreeWidget->insertTopLevelItems(0, thread->mItems); ui->threadTreeWidget->insertTopLevelItems(0, thread->mItems);
mPostVersions = thread->mPostVersions;
// clear list // clear list
thread->mItems.clear(); thread->mItems.clear();
} else { } else {
fillThreads(thread->mItems, thread->mExpandNewMessages, thread->mItemToExpand);
mPostVersions = thread->mPostVersions; mPostVersions = thread->mPostVersions;
fillThreads(thread->mItems, thread->mExpandNewMessages, thread->mItemToExpand);
// cleanup list // cleanup list
cleanupItems(thread->mItems); cleanupItems(thread->mItems);
@ -1520,6 +1526,7 @@ void GxsForumThreadWidget::insertMessage()
mStateHelper->clear(mTokenTypeMessageData); mStateHelper->clear(mTokenTypeMessageData);
ui->versions_CB->hide(); ui->versions_CB->hide();
ui->time_label->show();
ui->postText->clear(); ui->postText->clear();
//ui->threadTitle->clear(); //ui->threadTitle->clear();
@ -1532,6 +1539,7 @@ void GxsForumThreadWidget::insertMessage()
mStateHelper->clear(mTokenTypeMessageData); mStateHelper->clear(mTokenTypeMessageData);
ui->versions_CB->hide(); ui->versions_CB->hide();
ui->time_label->show();
//ui->threadTitle->setText(tr("Forum Description")); //ui->threadTitle->setText(tr("Forum Description"));
ui->postText->setText(mForumDescription); ui->postText->setText(mForumDescription);
@ -1552,6 +1560,7 @@ void GxsForumThreadWidget::insertMessage()
mStateHelper->setWidgetEnabled(ui->previousButton, false); mStateHelper->setWidgetEnabled(ui->previousButton, false);
mStateHelper->setWidgetEnabled(ui->nextButton, false); mStateHelper->setWidgetEnabled(ui->nextButton, false);
ui->versions_CB->hide(); ui->versions_CB->hide();
ui->time_label->show();
return; return;
} }
@ -1568,19 +1577,33 @@ void GxsForumThreadWidget::insertMessage()
// add/show combobox for versions, if applicable, and enable it. If no older versions of the post available, hide the combobox. // add/show combobox for versions, if applicable, and enable it. If no older versions of the post available, hide the combobox.
std::cerr << "Looking into existing versions for post " << mThreadId << ", thread history: " << mPostVersions.size() << std::endl;
QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > >::const_iterator it = mPostVersions.find(mThreadId) ; QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > >::const_iterator it = mPostVersions.find(mThreadId) ;
ui->versions_CB->clear(); ui->versions_CB->clear();
if(it != mPostVersions.end()) if(it != mPostVersions.end())
{ {
std::cerr << (*it).size() << " versions found " << std::endl;
ui->versions_CB->setVisible(true) ; ui->versions_CB->setVisible(true) ;
ui->time_label->hide();
ui->versions_CB->blockSignals(true) ;
for(uint32_t i=0;i<(*it).size();++i) for(uint32_t i=0;i<(*it).size();++i)
ui->versions_CB->insertItem(i,QDateTime::fromTime_t( (*it)[i].first).toString()) ; {
ui->versions_CB->insertItem(i,DateTime::formatLongDateTime( (*it)[i].first));
ui->versions_CB->setItemData(i,QString::fromStdString((*it)[i].second.toStdString()));
std::cerr << " added new post version " << (*it)[i].first << " " << (*it)[i].second << std::endl;
}
ui->versions_CB->blockSignals(false) ;
} }
else else
{ {
ui->versions_CB->hide(); ui->versions_CB->hide();
ui->time_label->show();
} }
/* request Post */ /* request Post */
@ -2137,7 +2160,7 @@ void GxsForumThreadWidget::editForumMessageData(const RsGxsForumMsg& msg)
if (!msg.mMeta.mAuthorId.isNull()) if (!msg.mMeta.mAuthorId.isNull())
{ {
CreateGxsForumMsg *cfm = new CreateGxsForumMsg(groupId(), mThreadId, msg.mMeta.mMsgId); CreateGxsForumMsg *cfm = new CreateGxsForumMsg(groupId(), msg.mMeta.mParentId, msg.mMeta.mMsgId);
cfm->insertPastedText(QString::fromUtf8(msg.mMsg.c_str())) ; cfm->insertPastedText(QString::fromUtf8(msg.mMsg.c_str())) ;
cfm->show(); cfm->show();

View File

@ -89,6 +89,8 @@ void GxsForumsFillThread::calculateExpand(const RsGxsForumMsg &msg, QTreeWidgetI
} }
} }
static bool decreasing_time_comp(const QPair<time_t,RsGxsMessageId>& e1,const QPair<time_t,RsGxsMessageId>& e2) { return e2.first < e1.first ; }
void GxsForumsFillThread::run() void GxsForumsFillThread::run()
{ {
RsTokenService *service = rsGxsForums->getTokenService(); RsTokenService *service = rsGxsForums->getTokenService();
@ -200,31 +202,36 @@ void GxsForumsFillThread::run()
#ifdef DEBUG_FORUMS #ifdef DEBUG_FORUMS
std::cerr << " Post " << msgIt->second.mMeta.mMsgId << " is a new version of " << msgIt->second.mMeta.mOrigMsgId << std::endl; std::cerr << " Post " << msgIt->second.mMeta.mMsgId << " is a new version of " << msgIt->second.mMeta.mOrigMsgId << std::endl;
#endif #endif
std::map<RsGxsMessageId,RsGxsForumMsg>::iterator msgIt2 = msgs.find(msgIt->second.mMeta.mOrigMsgId);
// always add the post a a self version
if(msgIt2 != msgs.end())
{
// Ensuring that the post exists allows to only collect the existing data.
mPostVersions[msgIt->second.mMeta.mOrigMsgId].push_back(QPair<time_t,RsGxsMessageId>(msgIt2->second.mMeta.mPublishTs,msgIt2->second.mMeta.mMsgId)) ;
mPostVersions[msgIt->second.mMeta.mOrigMsgId].push_back(QPair<time_t,RsGxsMessageId>(msgIt->second.mMeta.mPublishTs,msgIt->second.mMeta.mMsgId)) ; mPostVersions[msgIt->second.mMeta.mOrigMsgId].push_back(QPair<time_t,RsGxsMessageId>(msgIt->second.mMeta.mPublishTs,msgIt->second.mMeta.mMsgId)) ;
} }
}
// The following code assembles all new versions of a given post into the same array, indexed by the oldest version of the post.
for(QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > >::iterator it(mPostVersions.begin());it!=mPostVersions.end();++it) for(QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > >::iterator it(mPostVersions.begin());it!=mPostVersions.end();++it)
{ {
QVector<QPair<time_t,RsGxsMessageId> >& v(*it) ; QVector<QPair<time_t,RsGxsMessageId> >& v(*it) ;
for(int32_t i=0;i<v.size();++i) for(int32_t i=0;i<v.size();++i)
if(v[i].second != it.key())
{ {
RsGxsMessageId sub_msg_id = v[i].second ; RsGxsMessageId sub_msg_id = v[i].second ;
// move the post in first position if it is more recent.
if(v[0].first < v[i].first) // works if i==0
{
QPair<time_t,RsGxsMessageId> tmp(v[0]) ;
v[0] = v[i] ;
v[i] = tmp ;
}
QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > >::iterator it2 = mPostVersions.find(sub_msg_id); QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > >::iterator it2 = mPostVersions.find(sub_msg_id);
if(it2 != mPostVersions.end()) if(it2 != mPostVersions.end())
{ {
for(uint32_t j=0;j<(*it2).size();++j) for(uint32_t j=0;j<(*it2).size();++j)
if((*it2)[j].second != sub_msg_id) // dont copy it, since it is already present at slot i
v.append((*it2)[j]) ; v.append((*it2)[j]) ;
mPostVersions.erase(it2) ; // it2 is never equal to it mPostVersions.erase(it2) ; // it2 is never equal to it
@ -232,29 +239,38 @@ void GxsForumsFillThread::run()
} }
} }
// Now remove from msg ids, all posts except the most recent one. // Now remove from msg ids, all posts except the most recent one. And make the mPostVersion be indexed by the most recent version of the post,
// which corresponds to the item in the tree widget.
#ifdef DEBUG_FORUMS #ifdef DEBUG_FORUMS
std::cerr << "Final post versions: " << std::endl; std::cerr << "Final post versions: " << std::endl;
#endif #endif
QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > > mTmp;
for(QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > >::iterator it(mPostVersions.begin());it!=mPostVersions.end();++it) for(QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > >::iterator it(mPostVersions.begin());it!=mPostVersions.end();++it)
{ {
#ifdef DEBUG_FORUMS #ifdef DEBUG_FORUMS
std::cerr << "Original post: " << it.key() << std::endl; std::cerr << "Original post: " << it.key() << std::endl;
#endif #endif
if(!(*it).empty()) // Finally, sort the posts from newer to older
msgs.erase(it.key()) ;
for(uint32_t i=0;i<(*it).size();++i) qSort((*it).begin(),(*it).end(),decreasing_time_comp) ;
#ifdef DEBUG_FORUMS
std::cerr << " most recent version " << (*it)[0].first << " " << (*it)[0].second << std::endl;
#endif
for(uint32_t i=1;i<(*it).size();++i)
{ {
if(i > 0)
msgs.erase((*it)[i].second) ; msgs.erase((*it)[i].second) ;
#ifdef DEBUG_FORUMS #ifdef DEBUG_FORUMS
std::cerr << " new version " << (*it)[i].first << " " << (*it)[i].second << std::endl; std::cerr << " older version " << (*it)[i].first << " " << (*it)[i].second << std::endl;
#endif #endif
} }
mTmp[(*it)[0].second] = *it ; // index the versions map by the ID of the most recent post.
} }
mPostVersions = mTmp ;
// The first step is to find the top level thread messages. These are defined as the messages without // The first step is to find the top level thread messages. These are defined as the messages without
// any parent message ID. // any parent message ID.