Expose comment editing capability in JSON API

This commit is contained in:
Gioacchino Mazzurco 2019-07-30 18:50:56 +02:00
parent 3ffb57f998
commit ddde80ddff
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
3 changed files with 68 additions and 24 deletions

View File

@ -142,10 +142,13 @@ public:
* posted * posted
* @param[in] threadId Id of the post (that is a thread) in the channel * @param[in] threadId Id of the post (that is a thread) in the channel
* where the comment is placed * 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 * @param[in] parentId Id of the parent of the comment that is either a
* channel post Id or the Id of another comment. * channel post Id or the Id of another comment.
* @param[in] authorId Id of the author of the comment * @param[in] origCommentId If this is supposed to replace an already
* @param[in] comment UTF-8 string containing the comment itself * 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 * @param[out] commentMessageId Optional storage for the id of the comment
* that was created, meaningful only on success. * that was created, meaningful only on success.
* @param[out] errorMessage Optional storage for error message, meaningful * @param[out] errorMessage Optional storage for error message, meaningful
@ -155,9 +158,10 @@ public:
virtual bool createCommentV2( virtual bool createCommentV2(
const RsGxsGroupId& channelId, const RsGxsGroupId& channelId,
const RsGxsMessageId& threadId, const RsGxsMessageId& threadId,
const RsGxsMessageId& parentId,
const RsGxsId& authorId,
const std::string& comment, const std::string& comment,
const RsGxsId& authorId,
const RsGxsMessageId& parentId = RsGxsMessageId(),
const RsGxsMessageId& origCommentId = RsGxsMessageId(),
RsGxsMessageId& commentMessageId = RS_DEFAULT_STORAGE_PARAM(RsGxsMessageId), RsGxsMessageId& commentMessageId = RS_DEFAULT_STORAGE_PARAM(RsGxsMessageId),
std::string& errorMessage = RS_DEFAULT_STORAGE_PARAM(std::string) std::string& errorMessage = RS_DEFAULT_STORAGE_PARAM(std::string)
) = 0; ) = 0;

View File

@ -1433,13 +1433,15 @@ bool p3GxsChannels::createPostV2(
return false; return false;
} }
bool p3GxsChannels::createCommentV2(const RsGxsGroupId& channelId, bool p3GxsChannels::createCommentV2(
const RsGxsMessageId& threadId, const RsGxsGroupId& channelId,
const RsGxsMessageId& parentId, const RsGxsMessageId& threadId,
const RsGxsId& authorId, const std::string& comment,
const std::string& comment, const RsGxsId& authorId,
RsGxsMessageId& commentMessageId, const RsGxsMessageId& parentId,
std::string& errorMessage) const RsGxsMessageId& origCommentId,
RsGxsMessageId& commentMessageId,
std::string& errorMessage )
{ {
std::vector<RsGxsChannelGroup> channelsInfo; std::vector<RsGxsChannelGroup> channelsInfo;
if(!getChannelsInfo(std::list<RsGxsGroupId>({channelId}),channelsInfo)) if(!getChannelsInfo(std::list<RsGxsGroupId>({channelId}),channelsInfo))
@ -1476,6 +1478,7 @@ bool p3GxsChannels::createCommentV2(const RsGxsGroupId& channelId,
} }
if(!parentId.isNull()) if(!parentId.isNull())
{
if(!getChannelContent( // does the post thread exist? if(!getChannelContent( // does the post thread exist?
channelId,std::set<RsGxsMessageId>({parentId}),posts,comments )) channelId,std::set<RsGxsMessageId>({parentId}),posts,comments ))
{ {
@ -1485,17 +1488,50 @@ bool p3GxsChannels::createCommentV2(const RsGxsGroupId& channelId,
<< std::endl; << std::endl;
return false; return false;
} }
else if(comments.size() != 1 || comments[0].mMeta.mParentId.isNull()) else
{ // is the comment parent actually a comment? {
errorMessage = "You cannot comment post " + parentId.toStdString() if(comments.size() != 1 || comments[0].mMeta.mParentId.isNull())
+ " of channel with Id " + channelId.toStdString() + { // is the comment parent actually a comment?
": supplied mParentMsgId is not a comment Id!"; errorMessage = "You cannot comment post "
std::cerr << __PRETTY_FUNCTION__ << " Error: " << errorMessage + parentId.toStdString()
<< std::endl; + " 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<RsGxsMessageId> s({origCommentId});
std::vector<RsGxsChannelPost> posts;
std::vector<RsGxsComment> 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; 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 " + errorMessage = "You cannot comment to channel with Id " +
channelId.toStdString() + " with identity " + channelId.toStdString() + " with identity " +
@ -1511,6 +1547,7 @@ bool p3GxsChannels::createCommentV2(const RsGxsGroupId& channelId,
cmt.mMeta.mThreadId = threadId; cmt.mMeta.mThreadId = threadId;
cmt.mMeta.mParentId = parentId; cmt.mMeta.mParentId = parentId;
cmt.mMeta.mAuthorId = authorId; cmt.mMeta.mAuthorId = authorId;
cmt.mMeta.mOrigMsgId = origCommentId;
cmt.mComment = comment; cmt.mComment = comment;
uint32_t token; uint32_t token;

View File

@ -28,7 +28,7 @@
#include "gxs/rsgenexchange.h" #include "gxs/rsgenexchange.h"
#include "gxs/gxstokenqueue.h" #include "gxs/gxstokenqueue.h"
#include "util/rsmemory.h" #include "util/rsmemory.h"
#include "util/rsdebug.h"
#include "util/rstickevent.h" #include "util/rstickevent.h"
#include <map> #include <map>
@ -213,11 +213,14 @@ virtual bool ExtraFileRemove(const RsFileHash &hash);
/// Implementation of @see RsGxsChannels::createComment /// Implementation of @see RsGxsChannels::createComment
virtual bool createCommentV2( virtual bool createCommentV2(
const RsGxsGroupId& channelId, const RsGxsMessageId& threadId, const RsGxsGroupId& channelId,
const RsGxsMessageId& parentId, const RsGxsId& authorId, const RsGxsMessageId& threadId,
const std::string& comment, const std::string& comment,
RsGxsMessageId& commentMessageId = RS_DEFAULT_STORAGE_PARAM(RsGxsMessageId), const RsGxsId& authorId,
std::string& errorMessage = RS_DEFAULT_STORAGE_PARAM(std::string) 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; ) override;
/// Implementation of @see RsGxsChannels::editChannel /// Implementation of @see RsGxsChannels::editChannel