From ddde80ddff00affe5d3d861c2ce5ba83d30759ab Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Tue, 30 Jul 2019 18:50:56 +0200 Subject: [PATCH] Expose comment editing capability in JSON API --- libretroshare/src/retroshare/rsgxschannels.h | 12 ++-- libretroshare/src/services/p3gxschannels.cc | 67 +++++++++++++++----- libretroshare/src/services/p3gxschannels.h | 13 ++-- 3 files changed, 68 insertions(+), 24 deletions(-) diff --git a/libretroshare/src/retroshare/rsgxschannels.h b/libretroshare/src/retroshare/rsgxschannels.h index 655d87ff2..1b90f7924 100644 --- a/libretroshare/src/retroshare/rsgxschannels.h +++ b/libretroshare/src/retroshare/rsgxschannels.h @@ -142,10 +142,13 @@ public: * posted * @param[in] threadId Id of the post (that is a thread) in the channel * where the comment is placed + * @param[in] comment UTF-8 string containing the comment itself + * @param[in] authorId Id of the author of the comment * @param[in] parentId Id of the parent of the comment that is either a * channel post Id or the Id of another comment. - * @param[in] authorId Id of the author of the comment - * @param[in] comment UTF-8 string containing the comment itself + * @param[in] origCommentId If this is supposed to replace an already + * existent comment, the id of the old post. + * If left blank a new post will be created. * @param[out] commentMessageId Optional storage for the id of the comment * that was created, meaningful only on success. * @param[out] errorMessage Optional storage for error message, meaningful @@ -155,9 +158,10 @@ public: virtual bool createCommentV2( const RsGxsGroupId& channelId, const RsGxsMessageId& threadId, - const RsGxsMessageId& parentId, - const RsGxsId& authorId, const std::string& comment, + const RsGxsId& authorId, + const RsGxsMessageId& parentId = RsGxsMessageId(), + const RsGxsMessageId& origCommentId = RsGxsMessageId(), RsGxsMessageId& commentMessageId = RS_DEFAULT_STORAGE_PARAM(RsGxsMessageId), std::string& errorMessage = RS_DEFAULT_STORAGE_PARAM(std::string) ) = 0; diff --git a/libretroshare/src/services/p3gxschannels.cc b/libretroshare/src/services/p3gxschannels.cc index 367023cad..8b6f6a372 100644 --- a/libretroshare/src/services/p3gxschannels.cc +++ b/libretroshare/src/services/p3gxschannels.cc @@ -1433,13 +1433,15 @@ bool p3GxsChannels::createPostV2( return false; } -bool p3GxsChannels::createCommentV2(const RsGxsGroupId& channelId, - const RsGxsMessageId& threadId, - const RsGxsMessageId& parentId, - const RsGxsId& authorId, - const std::string& comment, - RsGxsMessageId& commentMessageId, - std::string& errorMessage) +bool p3GxsChannels::createCommentV2( + const RsGxsGroupId& channelId, + const RsGxsMessageId& threadId, + const std::string& comment, + const RsGxsId& authorId, + const RsGxsMessageId& parentId, + const RsGxsMessageId& origCommentId, + RsGxsMessageId& commentMessageId, + std::string& errorMessage ) { std::vector channelsInfo; if(!getChannelsInfo(std::list({channelId}),channelsInfo)) @@ -1476,6 +1478,7 @@ bool p3GxsChannels::createCommentV2(const RsGxsGroupId& channelId, } if(!parentId.isNull()) + { if(!getChannelContent( // does the post thread exist? channelId,std::set({parentId}),posts,comments )) { @@ -1485,17 +1488,50 @@ bool p3GxsChannels::createCommentV2(const RsGxsGroupId& channelId, << std::endl; return false; } - else if(comments.size() != 1 || comments[0].mMeta.mParentId.isNull()) - { // is the comment parent actually a comment? - errorMessage = "You cannot comment post " + parentId.toStdString() - + " of channel with Id " + channelId.toStdString() + - ": supplied mParentMsgId is not a comment Id!"; - std::cerr << __PRETTY_FUNCTION__ << " Error: " << errorMessage - << std::endl; + else + { + if(comments.size() != 1 || comments[0].mMeta.mParentId.isNull()) + { // is the comment parent actually a comment? + errorMessage = "You cannot comment post " + + parentId.toStdString() + + " of channel with Id " + channelId.toStdString() + + ": supplied mParentMsgId is not a comment Id!"; + std::cerr << __PRETTY_FUNCTION__ << " Error: " << errorMessage + << std::endl; + return false; + } + } + } + + if(!origCommentId.isNull()) + { + std::set s({origCommentId}); + std::vector posts; + std::vector comments; + + if( !getChannelContent(channelId, s, posts, comments) || + comments.size() != 1 ) + { + errorMessage = "You cannot edit comment " + + origCommentId.toStdString() + + " of channel with Id " + channelId.toStdString() + + ": this post does not exist locally!"; + RsErr() << __PRETTY_FUNCTION__ << " " << errorMessage << std::endl; return false; } - if(!rsIdentity->isOwnId(authorId)) // is the voter ID actually ours? + const RsGxsId& commentAuthor = comments[0].mMeta.mAuthorId; + if(commentAuthor != authorId) + { + errorMessage = "Editor identity and creator doesn't match " + + authorId.toStdString() + " != " + + commentAuthor.toStdString(); + RsErr() << __PRETTY_FUNCTION__ << " " << errorMessage << std::endl; + return false; + } + } + + if(!rsIdentity->isOwnId(authorId)) // is the author ID actually ours? { errorMessage = "You cannot comment to channel with Id " + channelId.toStdString() + " with identity " + @@ -1511,6 +1547,7 @@ bool p3GxsChannels::createCommentV2(const RsGxsGroupId& channelId, cmt.mMeta.mThreadId = threadId; cmt.mMeta.mParentId = parentId; cmt.mMeta.mAuthorId = authorId; + cmt.mMeta.mOrigMsgId = origCommentId; cmt.mComment = comment; uint32_t token; diff --git a/libretroshare/src/services/p3gxschannels.h b/libretroshare/src/services/p3gxschannels.h index dffc73c32..766049624 100644 --- a/libretroshare/src/services/p3gxschannels.h +++ b/libretroshare/src/services/p3gxschannels.h @@ -28,7 +28,7 @@ #include "gxs/rsgenexchange.h" #include "gxs/gxstokenqueue.h" #include "util/rsmemory.h" - +#include "util/rsdebug.h" #include "util/rstickevent.h" #include @@ -213,11 +213,14 @@ virtual bool ExtraFileRemove(const RsFileHash &hash); /// Implementation of @see RsGxsChannels::createComment virtual bool createCommentV2( - const RsGxsGroupId& channelId, const RsGxsMessageId& threadId, - const RsGxsMessageId& parentId, const RsGxsId& authorId, + const RsGxsGroupId& channelId, + const RsGxsMessageId& threadId, const std::string& comment, - RsGxsMessageId& commentMessageId = RS_DEFAULT_STORAGE_PARAM(RsGxsMessageId), - std::string& errorMessage = RS_DEFAULT_STORAGE_PARAM(std::string) + const RsGxsId& authorId, + const RsGxsMessageId& parentId = RsGxsMessageId(), + const RsGxsMessageId& origCommentId = RsGxsMessageId(), + RsGxsMessageId& commentMessageId = RS_DEFAULT_STORAGE_PARAM(RsGxsMessageId), + std::string& errorMessage = RS_DEFAULT_STORAGE_PARAM(std::string) ) override; /// Implementation of @see RsGxsChannels::editChannel