fixed missing call to update comments in GxsChannelPostsModel

This commit is contained in:
csoler 2020-11-08 14:57:17 +01:00
parent 08e2987154
commit d472053960
3 changed files with 57 additions and 38 deletions

View File

@ -328,7 +328,10 @@ public:
std::vector<RsGxsChannelGroup>& channelsInfo ) = 0;
/**
* @brief Get all channel messages and comments in a given channel
* @brief Get all channel messages, comments and votes in a given channel
* @note It's the client's responsibility to figure out which message (resp. comment)
* a comment (resp. vote) refers to.
*
* @jsonapi{development}
* @param[in] channelId id of the channel of which the content is requested
* @param[out] posts storage for posts
@ -342,10 +345,12 @@ public:
std::vector<RsGxsVote>& votes ) = 0;
/**
* @brief Get channel messages and comments corresponding to the given IDs.
* If the set is empty, nothing is returned.
* @note Since comments are internally themselves messages, it is possible
* to get comments only by supplying their IDs.
* @brief Get channel messages, comments and votes corresponding to the given IDs.
* @note Since comments are internally themselves messages, this function actually
* returns the data for messages, comments or votes that have the given ID.
* It *does not* automatically retrieve the comments or votes for a given message
* which Id you supplied.
*
* @jsonapi{development}
* @param[in] channelId id of the channel of which the content is requested
* @param[in] contentsIds ids of requested contents
@ -361,8 +366,9 @@ public:
std::vector<RsGxsVote>& votes ) = 0;
/**
* @brief Get channel comments corresponding to the given IDs.
* @brief Get channel comments corresponding to the given message IDs.
* If the set is empty, nothing is returned.
*
* @jsonapi{development}
* @param[in] channelId id of the channel of which the content is requested
* @param[in] contentIds ids of requested contents

View File

@ -485,8 +485,7 @@ bool p3GxsChannels::groupShareKeys(
* at the moment - fix it up later
*/
bool p3GxsChannels::getPostData(
const uint32_t &token, std::vector<RsGxsChannelPost> &msgs,
bool p3GxsChannels::getPostData( const uint32_t& token, std::vector<RsGxsChannelPost>& msgs,
std::vector<RsGxsComment>& cmts,
std::vector<RsGxsVote>& vots)
{

View File

@ -72,6 +72,31 @@ void RsGxsChannelPostsModel::setMode(TreeMode mode)
triggerViewUpdate();
}
void updateCommentCounts( std::vector<RsGxsChannelPost>& posts, std::vector<RsGxsComment>& comments)
{
// Store posts IDs in a std::map to avoid a quadratic cost
std::cerr << "Updating comment counts for " << posts.size() << " posts." << std::endl;
std::map<RsGxsMessageId,uint32_t> post_indices;
for(uint32_t i=0;i<posts.size();++i)
{
post_indices[posts[i].mMeta.mMsgId] = i;
posts[i].mCommentCount = 0; // should be 0 already, but we secure that value.
std::cerr << " Zeroing comments for post " << posts[i].mMeta.mMsgId << std::endl;
}
// now look into comments and increase the count
for(uint32_t i=0;i<comments.size();++i)
{
std::cerr << " Found new comment " << comments[i].mMeta.mMsgId << " for post" << comments[i].mMeta.mThreadId << std::endl;
++posts[post_indices[comments[i].mMeta.mThreadId]].mCommentCount;
}
}
void RsGxsChannelPostsModel::handleEvent_main_thread(std::shared_ptr<const RsEvent> event)
{
const RsGxsChannelEvent *e = dynamic_cast<const RsGxsChannelEvent*>(event.get());
@ -91,7 +116,7 @@ void RsGxsChannelPostsModel::handleEvent_main_thread(std::shared_ptr<const RsEve
if(e->mChannelGroupId == mChannelGroup.mMeta.mGroupId)
RsThread::async([this, e]()
{
// 1 - get message data from p3GxsChannels
// 1 - get message data from p3GxsChannels. No need for pointers here, because we send only a single post to postToObject()
std::vector<RsGxsChannelPost> posts;
std::vector<RsGxsComment> comments;
@ -103,9 +128,17 @@ void RsGxsChannelPostsModel::handleEvent_main_thread(std::shared_ptr<const RsEve
return;
}
if(!rsGxsChannels->getChannelComments(mChannelGroup.mMeta.mGroupId,std::set<RsGxsMessageId>{ e->mChannelMsgId },comments))
{
std::cerr << __PRETTY_FUNCTION__ << " failed to retrieve message comment data for channel/msg " << e->mChannelGroupId << "/" << e->mChannelMsgId << std::endl;
return;
}
updateCommentCounts(posts,comments);
// 2 - update the model in the UI thread.
RsQThreadUtils::postToObject( [posts,comments,votes,this]()
RsQThreadUtils::postToObject( [posts,this]()
{
for(uint32_t i=0;i<posts.size();++i)
{
@ -326,7 +359,7 @@ bool RsGxsChannelPostsModel::setNumColumns(int n)
RsErr() << __PRETTY_FUNCTION__ << " Attempt to set a number of column of 0. This is wrong." << std::endl;
return false;
}
if(mColumns == n)
if((int)mColumns == n)
return false;
preMods();
@ -522,29 +555,6 @@ void RsGxsChannelPostsModel::setPosts(const RsGxsChannelGroup& group, std::vecto
emit channelPostsLoaded();
}
static void updateCommentCounts( std::vector<RsGxsChannelPost>& posts, std::vector<RsGxsComment>& comments)
{
// Store posts IDs in a std::map to avoid a quadratic cost
std::map<RsGxsMessageId,uint32_t> post_indices;
for(uint32_t i=0;i<posts.size();++i)
{
post_indices[posts[i].mMeta.mMsgId] = i;
posts[i].mCommentCount = 0; // should be 0 already, but we secure that value.
std::cerr << "Zeroing comments for post " << posts[i].mMeta.mMsgId << std::endl;
}
// now look into comments and increase the count
for(uint32_t i=0;i<comments.size();++i)
{
std::cerr << "Found new comment " << comments[i].mMeta.mMsgId << " for post" << comments[i].mMeta.mThreadId << std::endl;
++posts[post_indices[comments[i].mMeta.mThreadId]].mCommentCount;
}
}
void RsGxsChannelPostsModel::update_posts(const RsGxsGroupId& group_id)
{
if(group_id.isNull())
@ -579,6 +589,10 @@ void RsGxsChannelPostsModel::update_posts(const RsGxsGroupId& group_id)
std::cerr << __PRETTY_FUNCTION__ << " failed to retrieve channel messages for channel " << group_id << std::endl;
return;
}
std::cerr << "Got channel all content for channel " << group_id << std::endl;
std::cerr << " posts : " << posts->size() << std::endl;
std::cerr << " comments: " << comments->size() << std::endl;
std::cerr << " votes : " << votes->size() << std::endl;
// This shouldn't be needed normally. We need it until a background process computes the number of comments per
// post and stores it in the service string. Since we request all data, this process isn't costing much anyway.