From 20346fc30eda58532eea6a21530bfee7811ce492 Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 12 Aug 2020 12:02:28 +0200 Subject: [PATCH] fixed voting in Boards --- libretroshare/src/services/p3posted.cc | 36 +++++++++++++++++-- libretroshare/src/services/p3posted.h | 6 ++-- .../gui/Posted/PostedListWidgetWithModel.cpp | 4 +-- .../gui/Posted/PostedListWidgetWithModel.h | 5 +-- 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/libretroshare/src/services/p3posted.cc b/libretroshare/src/services/p3posted.cc index 39b350243..50ff890f1 100644 --- a/libretroshare/src/services/p3posted.cc +++ b/libretroshare/src/services/p3posted.cc @@ -442,9 +442,41 @@ bool p3Posted::createBoard(RsPostedGroup& board) return true; } -bool p3Posted::voteForPost(bool up,const RsGxsGroupId& postGrpId,const RsGxsMessageId& postMsgId,const RsGxsId& voterId) +bool p3Posted::voteForPost(bool up,const RsGxsGroupId& postGrpId,const RsGxsMessageId& postMsgId,const RsGxsId& authorId) { - std::cerr << "(EE) " << __PRETTY_FUNCTION__ << ": Not implemented yet"<< std::endl; + // Do some basic tests + + if(!rsIdentity->isOwnId(authorId)) // This is ruled out before waitToken complains. Not sure it's needed. + { + std::cerr << __PRETTY_FUNCTION__ << ": vote submitted with an ID that is not yours! This cannot work." << std::endl; + return false; + } + + RsGxsVote vote; + + vote.mMeta.mGroupId = postGrpId; + vote.mMeta.mThreadId = postMsgId; + vote.mMeta.mParentId = postMsgId; + vote.mMeta.mAuthorId = authorId; + + if (up) + vote.mVoteType = GXS_VOTE_UP; + else + vote.mVoteType = GXS_VOTE_DOWN; + + uint32_t token; + + if(!createNewVote(token, vote)) + { + std::cerr << __PRETTY_FUNCTION__ << " Error! Failed submitting vote to (group,msg) " << postGrpId << "," << postMsgId << " from author " << authorId << std::endl; + return false; + } + + if(waitToken(token) != RsTokenService::COMPLETE) + { + std::cerr << __PRETTY_FUNCTION__ << " Error! GXS operation failed." << std::endl; + return false; + } return true; } diff --git a/libretroshare/src/services/p3posted.h b/libretroshare/src/services/p3posted.h index 2bd3c3a0b..005ed592a 100644 --- a/libretroshare/src/services/p3posted.h +++ b/libretroshare/src/services/p3posted.h @@ -128,17 +128,17 @@ virtual void setMessageReadStatus(uint32_t& token, const RsGxsGrpMsgIdPair& msgI return mCommentService->createGxsComment(token, msg) && waitToken(token) == RsTokenService::COMPLETE ; } - virtual bool createNewVote(uint32_t &token, RsGxsVote &msg) + virtual bool createNewVote(uint32_t &token, RsGxsVote &msg) override { return mCommentService->createGxsVote(token, msg); } virtual bool acknowledgeComment( - uint32_t token, std::pair& msgId ) + uint32_t token, std::pair& msgId ) override { return acknowledgeMsg(token, msgId); } virtual bool acknowledgeVote( - uint32_t token, std::pair& msgId ) + uint32_t token, std::pair& msgId ) override { if (mCommentService->acknowledgeVote(token, msgId)) return true; return acknowledgeMsg(token, msgId); diff --git a/retroshare-gui/src/gui/Posted/PostedListWidgetWithModel.cpp b/retroshare-gui/src/gui/Posted/PostedListWidgetWithModel.cpp index f949a33f1..3dccf6e6f 100644 --- a/retroshare-gui/src/gui/Posted/PostedListWidgetWithModel.cpp +++ b/retroshare-gui/src/gui/Posted/PostedListWidgetWithModel.cpp @@ -145,7 +145,7 @@ QWidget *PostedPostDelegate::createEditor(QWidget *parent, const QStyleOptionVie { QWidget *w = new BoardPostDisplayWidget(post,mDisplayMode,parent); - QObject::connect(w,SIGNAL(vote(RsGxsGrpMsgIdPair,bool)),this,SLOT(voteMsg(RsGxsGrpMsgIdPair,bool))); + QObject::connect(w,SIGNAL(vote(RsGxsGrpMsgIdPair,bool)),mPostListWidget,SLOT(voteMsg(RsGxsGrpMsgIdPair,bool))); w->adjustSize(); w->setFixedSize(option.rect.size()); @@ -169,7 +169,7 @@ PostedListWidgetWithModel::PostedListWidgetWithModel(const RsGxsGroupId& postedI ui->setupUi(this); ui->postsTree->setModel(mPostedPostsModel = new RsPostedPostsModel()); - ui->postsTree->setItemDelegate(mPostedPostsDelegate = new PostedPostDelegate()); + ui->postsTree->setItemDelegate(mPostedPostsDelegate = new PostedPostDelegate(this)); ui->postsTree->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // prevents bug on w10, since row size depends on widget width ui->postsTree->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);// more beautiful if we scroll at pixel level ui->postsTree->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); diff --git a/retroshare-gui/src/gui/Posted/PostedListWidgetWithModel.h b/retroshare-gui/src/gui/Posted/PostedListWidgetWithModel.h index f0218d942..30cd9d40a 100644 --- a/retroshare-gui/src/gui/Posted/PostedListWidgetWithModel.h +++ b/retroshare-gui/src/gui/Posted/PostedListWidgetWithModel.h @@ -38,13 +38,14 @@ class PostedListWidgetWithModel; class QTreeWidgetItem; class QSortFilterProxyModel; class RsPostedPostsModel; +class PostedListWidgetWithModel; class PostedPostDelegate: public QAbstractItemDelegate { Q_OBJECT public: - PostedPostDelegate(QObject *parent=0) : QAbstractItemDelegate(parent),mCellWidthPix(100),mDisplayMode(BoardPostDisplayWidget::DISPLAY_MODE_COMPACT){} + PostedPostDelegate(PostedListWidgetWithModel *p,QObject *parent=0) : QAbstractItemDelegate(parent),mCellWidthPix(100),mPostListWidget(p),mDisplayMode(BoardPostDisplayWidget::DISPLAY_MODE_COMPACT){} virtual ~PostedPostDelegate(){} void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const override; @@ -60,8 +61,8 @@ class PostedPostDelegate: public QAbstractItemDelegate private: QSize cellSize(const QSize& w) const; // Converts the supplied size to the cell size for the current container. // The client should then scale its widget to fit the given size. - int mCellWidthPix; + PostedListWidgetWithModel *mPostListWidget; // used for sending vote signals and so on. BoardPostDisplayWidget::DisplayMode mDisplayMode; };