partly resurrected post versions

This commit is contained in:
csoler 2018-11-27 19:21:49 +01:00
parent 561db00255
commit 23302ea469
No known key found for this signature in database
GPG Key ID: 7BCA522266C0804C
4 changed files with 65 additions and 43 deletions

View File

@ -48,11 +48,22 @@ int RsGxsForumModel::rowCount(const QModelIndex& parent) const
else
return getChildrenCount(parent.internalPointer());
}
int RsGxsForumModel::columnCount(const QModelIndex &parent) const
{
return COLUMN_THREAD_NB_COLUMNS ;
}
std::vector<std::pair<time_t,RsGxsMessageId> > RsGxsForumModel::getPostVersions(const RsGxsMessageId& mid) const
{
auto it = mPostVersions.find(mid);
if(it != mPostVersions.end())
return it->second;
else
return std::vector<std::pair<time_t,RsGxsMessageId> >();
}
bool RsGxsForumModel::getPostData(const QModelIndex& i,ForumModelPostEntry& fmpe) const
{
if(!i.isValid())
@ -488,12 +499,13 @@ void RsGxsForumModel::setForum(const RsGxsGroupId& forum_group_id)
update_posts(forum_group_id);
}
void RsGxsForumModel::setPosts(const RsGxsForumGroup& group, const std::vector<ForumModelPostEntry>& posts)
void RsGxsForumModel::setPosts(const RsGxsForumGroup& group, const std::vector<ForumModelPostEntry>& posts,const std::map<RsGxsMessageId,std::vector<std::pair<time_t,RsGxsMessageId> > >& post_versions)
{
emit dataChanged(createIndex(0,0,(void*)NULL), createIndex(0,COLUMN_THREAD_NB_COLUMNS-1,(void*)NULL));
mForumGroup = group;
mPosts = posts;
mPostVersions = post_versions;
// now update prow for all posts
@ -539,14 +551,15 @@ void RsGxsForumModel::update_posts(const RsGxsGroupId& group_id)
// 2 - sort the messages into a proper hierarchy
auto post_versions = new std::map<RsGxsMessageId,std::vector<std::pair<time_t, RsGxsMessageId> > >() ;
std::vector<ForumModelPostEntry> *vect = new std::vector<ForumModelPostEntry>();
RsGxsForumGroup group = groups[0];
computeMessagesHierarchy(group,messages,*vect);
computeMessagesHierarchy(group,messages,*vect,*post_versions);
// 3 - update the model in the UI thread.
RsQThreadUtils::postToObject( [group,vect,this]()
RsQThreadUtils::postToObject( [group,vect,post_versions,this]()
{
/* Here it goes any code you want to be executed on the Qt Gui
* thread, for example to update the data model with new information
@ -554,8 +567,10 @@ void RsGxsForumModel::update_posts(const RsGxsGroupId& group_id)
* Qt::QueuedConnection is important!
*/
setPosts(group,*vect) ;
setPosts(group,*vect,*post_versions) ;
delete vect;
delete post_versions;
}, this );
@ -651,11 +666,13 @@ void RsGxsForumModel::convertMsgToPostEntry(const RsGxsForumGroup& mForumGroup,c
#endif
}
static bool decreasing_time_comp(const QPair<time_t,RsGxsMessageId>& e1,const QPair<time_t,RsGxsMessageId>& e2) { return e2.first < e1.first ; }
static bool decreasing_time_comp(const std::pair<time_t,RsGxsMessageId>& e1,const std::pair<time_t,RsGxsMessageId>& e2) { return e2.first < e1.first ; }
void RsGxsForumModel::computeMessagesHierarchy(const RsGxsForumGroup& forum_group,
const std::vector<RsGxsForumMsg>& msgs_array,
std::vector<ForumModelPostEntry>& posts)
std::vector<ForumModelPostEntry>& posts,
std::map<RsGxsMessageId,std::vector<std::pair<time_t,RsGxsMessageId> > >& mPostVersions
)
{
std::cerr << "updating messages data with " << msgs_array.size() << " messages" << std::endl;
@ -687,7 +704,6 @@ void RsGxsForumModel::computeMessagesHierarchy(const RsGxsForumGroup& forum_grou
int step = 0;
initEmptyHierarchy(posts);
QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > > mPostVersions ;
// ThreadList contains the list of parent threads. The algorithm below iterates through all messages
// and tries to establish parenthood relationships between them, given that we only know the
@ -740,31 +756,31 @@ void RsGxsForumModel::computeMessagesHierarchy(const RsGxsForumGroup& forum_grou
// always add the post a self version
if(mPostVersions[msgIt->second.mMeta.mOrigMsgId].empty())
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(std::make_pair(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(std::make_pair(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(auto it(mPostVersions.begin());it!=mPostVersions.end();++it)
{
QVector<QPair<time_t,RsGxsMessageId> >& v(*it) ;
auto& v(it->second) ;
for(int32_t i=0;i<v.size();++i)
{
if(v[i].second != it.key())
if(v[i].second != it->first)
{
RsGxsMessageId sub_msg_id = v[i].second ;
QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > >::iterator it2 = mPostVersions.find(sub_msg_id);
auto it2 = mPostVersions.find(sub_msg_id);
if(it2 != mPostVersions.end())
{
for(int32_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]) ;
for(int32_t j=0;j<it2->second.size();++j)
if(it2->second[j].second != sub_msg_id) // dont copy it, since it is already present at slot i
v.push_back(it2->second[j]) ;
mPostVersions.erase(it2) ; // it2 is never equal to it
}
@ -779,37 +795,37 @@ void RsGxsForumModel::computeMessagesHierarchy(const RsGxsForumGroup& forum_grou
#ifdef DEBUG_FORUMS
std::cerr << "Final post versions: " << std::endl;
#endif
QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > > mTmp;
std::map<RsGxsMessageId,std::vector<std::pair<time_t,RsGxsMessageId> > > mTmp;
std::map<RsGxsMessageId,RsGxsMessageId> most_recent_versions ;
for(QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > >::iterator it(mPostVersions.begin());it!=mPostVersions.end();++it)
for(auto it(mPostVersions.begin());it!=mPostVersions.end();++it)
{
#ifdef DEBUG_FORUMS
std::cerr << "Original post: " << it.key() << std::endl;
#endif
// Finally, sort the posts from newer to older
qSort((*it).begin(),(*it).end(),decreasing_time_comp) ;
std::sort(it->second.begin(),it->second.end(),decreasing_time_comp) ;
#ifdef DEBUG_FORUMS
std::cerr << " most recent version " << (*it)[0].first << " " << (*it)[0].second << std::endl;
#endif
for(int32_t i=1;i<(*it).size();++i)
for(int32_t i=1;i<it->second.size();++i)
{
msgs.erase((*it)[i].second) ;
msgs.erase(it->second[i].second) ;
#ifdef DEBUG_FORUMS
std::cerr << " older version " << (*it)[i].first << " " << (*it)[i].second << std::endl;
#endif
}
mTmp[(*it)[0].second] = *it ; // index the versions map by the ID of the most recent post.
mTmp[it->second[0].second] = it->second ; // index the versions map by the ID of the most recent post.
// Now make sure that message parents are consistent. Indeed, an old post may have the old version of a post as parent. So we need to change that parent
// to the newest version. So we create a map of which is the most recent version of each message, so that parent messages can be searched in it.
for(int i=1;i<(*it).size();++i)
most_recent_versions[(*it)[i].second] = (*it)[0].second ;
for(int i=1;i<it->second.size();++i)
most_recent_versions[it->second[i].second] = it->second[0].second ;
}
mPostVersions = mTmp ;

View File

@ -83,6 +83,8 @@ public:
QModelIndex root() const{ return createIndex(0,0,(void*)NULL) ;}
QModelIndex getIndexOfMessage(const RsGxsMessageId& mid) const;
std::vector<std::pair<time_t,RsGxsMessageId> > getPostVersions(const RsGxsMessageId& mid) const;
#ifdef TO_REMOVE
QModelIndex getNextIndex(const QModelIndex& i,bool unread_only) const;
@ -176,11 +178,12 @@ private:
static ForumModelIndex addEntry(std::vector<ForumModelPostEntry>& posts,const ForumModelPostEntry& entry,ForumModelIndex parent);
static void convertMsgToPostEntry(const RsGxsForumGroup &mForumGroup, const RsGxsForumMsg& msg, bool useChildTS, uint32_t filterColumn, ForumModelPostEntry& fentry);
void computeMessagesHierarchy(const RsGxsForumGroup& forum_group,const std::vector<RsGxsForumMsg>& msgs_array,std::vector<ForumModelPostEntry>& posts);
void setPosts(const RsGxsForumGroup &group, const std::vector<ForumModelPostEntry>& posts); // this method *must* be called from UI thread.
void computeMessagesHierarchy(const RsGxsForumGroup& forum_group, const std::vector<RsGxsForumMsg>& msgs_array, std::vector<ForumModelPostEntry>& posts, std::map<RsGxsMessageId, std::vector<std::pair<time_t, RsGxsMessageId> > > &mPostVersions);
void setPosts(const RsGxsForumGroup& group, const std::vector<ForumModelPostEntry>& posts,const std::map<RsGxsMessageId,std::vector<std::pair<time_t,RsGxsMessageId> > >& post_versions);
void initEmptyHierarchy(std::vector<ForumModelPostEntry>& posts);
std::vector<ForumModelPostEntry> mPosts ; // store the list of posts updated from rsForums.
std::map<RsGxsMessageId,std::vector<std::pair<time_t,RsGxsMessageId> > > mPostVersions;
QColor mTextColorRead ;
QColor mTextColorUnread ;

View File

@ -484,7 +484,6 @@ GxsForumThreadWidget::GxsForumThreadWidget(const RsGxsGroupId &forumId, QWidget
ui->threadTreeWidget->enableColumnCustomize(true);
#endif
ui->threadTreeWidget->sortByColumn(RsGxsForumModel::COLUMN_THREAD_DATE, Qt::DescendingOrder);
}
void GxsForumThreadWidget::blank()
@ -1883,9 +1882,9 @@ void GxsForumThreadWidget::insertMessage()
if (index.isValid())
{
QModelIndex parentIndex = mThreadProxyModel->mapToSource(index).parent();
QModelIndex parentIndex = index.parent();
int curr_index = index.row();
int count = mThreadModel->rowCount(parentIndex);
int count = mThreadProxyModel->rowCount(parentIndex);
ui->previousButton->setEnabled(curr_index > 0);
ui->nextButton->setEnabled(curr_index < count - 1);
@ -1911,32 +1910,31 @@ void GxsForumThreadWidget::insertMessage()
// 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;
#ifdef TODO
QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > >::const_iterator it = mPostVersions.find(mOrigThreadId) ;
std::vector<std::pair<time_t,RsGxsMessageId> > post_versions = mThreadModel->getPostVersions(mThreadId);
std::cerr << "Looking into existing versions for post " << mThreadId << ", thread history: " << post_versions.size() << std::endl;
ui->versions_CB->blockSignals(true) ;
while(ui->versions_CB->count() > 0)
ui->versions_CB->removeItem(0);
if(it != mPostVersions.end())
if(!post_versions.empty())
{
std::cerr << (*it).size() << " versions found " << std::endl;
std::cerr << post_versions.size() << " versions found " << std::endl;
ui->versions_CB->setVisible(true) ;
ui->time_label->hide();
int current_index = 0 ;
for(int i=0;i<(*it).size();++i)
for(int i=0;i<post_versions.size();++i)
{
ui->versions_CB->insertItem(i, ((i==0)?tr("(Latest) "):tr("(Old) "))+" "+DateTime::formatLongDateTime( (*it)[i].first));
ui->versions_CB->setItemData(i,QString::fromStdString((*it)[i].second.toStdString()));
ui->versions_CB->insertItem(i, ((i==0)?tr("(Latest) "):tr("(Old) "))+" "+DateTime::formatLongDateTime( post_versions[i].first));
ui->versions_CB->setItemData(i,QString::fromStdString(post_versions[i].second.toStdString()));
std::cerr << " added new post version " << (*it)[i].first << " " << (*it)[i].second << std::endl;
std::cerr << " added new post version " << post_versions[i].first << " " << post_versions[i].second << std::endl;
if(mThreadId == (*it)[i].second)
if(mThreadId == post_versions[i].second)
current_index = i ;
}
@ -1947,7 +1945,6 @@ void GxsForumThreadWidget::insertMessage()
ui->versions_CB->hide();
ui->time_label->show();
}
#endif
ui->versions_CB->blockSignals(false) ;
@ -2238,7 +2235,7 @@ void GxsForumThreadWidget::markMsgAsReadUnread (bool read, bool children, bool f
QModelIndex index = *selectedIndexes.begin();
mThreadModel->setMsgReadStatus(index,read,children);
mThreadModel->setMsgReadStatus(mThreadProxyModel->mapToSource(index),read,children);
}
#ifdef TODO
@ -2723,13 +2720,21 @@ bool GxsForumThreadWidget::filterItem(QTreeWidgetItem *item, const QString &text
void GxsForumThreadWidget::updateGroupName()
{
ui->threadTreeWidget->selectionModel()->clear();
ui->threadTreeWidget->selectionModel()->reset();
mThreadId.clear();
ui->forumName->setText(QString::fromUtf8(mForumGroup.mMeta.mGroupName.c_str()));
ui->threadTreeWidget->sortByColumn(RsGxsForumModel::COLUMN_THREAD_DATE, Qt::DescendingOrder);
ui->threadTreeWidget->update();
}
void GxsForumThreadWidget::updateGroupData()
{
mSubscribeFlags = 0;
mSignFlags = 0;
mThreadId.clear();
mForumDescription.clear();
ui->threadTreeWidget->selectionModel()->clear();
ui->threadTreeWidget->selectionModel()->reset();
emit groupChanged(this);

View File

@ -237,8 +237,6 @@ private:
RsGxsMessageId mNavigatePendingMsgId;
QList<RsGxsMessageId> mIgnoredMsgId;
QMap<RsGxsMessageId,QVector<QPair<time_t,RsGxsMessageId> > > mPostVersions ; // holds older versions of posts
RsGxsForumModel *mThreadModel;
QSortFilterProxyModel *mThreadProxyModel;