Implement mostly decent JSON API for channels and post creation

Now the possible id of the cannel/post is returned and channel/post is
indexed on creation without having to wait for gxsutil scan to happen

Note that the id is correct in normal usage, but if one try to overload
the API per eventually get the id of another channel/post with same data
but created by a different call, it is not dangerous so one trying to
overload the API is just getting a possibly wrong but equivalent
channel/post id returned.
This commit is contained in:
Gioacchino Mazzurco 2018-10-10 06:40:56 +02:00
parent c05c376351
commit 321fc8a8d4
No known key found for this signature in database
GPG Key ID: A1FBCA3872E87051
3 changed files with 89 additions and 18 deletions

View File

@ -4,7 +4,7 @@
* *
* libretroshare: retroshare core library *
* *
* Copyright 2012-2012 by Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2012 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2018 Gioacchino Mazzurco <gio@eigenlab.org> *
* *
* This program is free software: you can redistribute it and/or modify *

View File

@ -3,7 +3,8 @@
* *
* libretroshare: retroshare core library *
* *
* Copyright 2012-2012 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2012 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2018 Gioacchino Mazzurco <gio@eigenlab.org> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
@ -1044,29 +1045,102 @@ bool p3GxsChannels::getChannelsContent(
bool p3GxsChannels::createChannel(RsGxsChannelGroup& channel)
{
uint32_t token;
time_t beginCreation = time(nullptr);
if( !createGroup(token, channel)
|| waitToken(token) != RsTokenService::COMPLETE ) return false;
|| waitToken(token) != RsTokenService::COMPLETE )
return false;
time_t endCreation = time(nullptr);
// TODO: Need a way to get the channel id back!
//#ifdef RS_DEEP_SEARCH
// DeepSearch::indexChannelGroup(channel);
//#endif // RS_DEEP_SEARCH
std::list<RsGroupMetaData> channels;
if(!getChannelsSummaries(channels)) return false;
return true;
/* This is ugly but after digging and doing many tries of doing it the right
* way ending always into too big refactor chain reaction, I think this is
* not that bad, moreover seems the last created group tend to end up near
* the beginning of the list so it is fast founding it.
* The shortcoming of this is that if groups with same data are created in
* a burst (more then once in a second) is that the id of another similar
* group can be returned, but this is a pointy case.
* Order of conditions in the `if` matter for performances */
bool found = false;
for(const RsGroupMetaData& chan : channels)
{
if( IS_GROUP_ADMIN(chan.mSubscribeFlags)
&& IS_GROUP_SUBSCRIBED(chan.mSubscribeFlags)
&& chan.mPublishTs >= beginCreation
&& chan.mPublishTs <= endCreation
&& chan.mGroupFlags == channel.mMeta.mGroupFlags
&& chan.mSignFlags == channel.mMeta.mSignFlags
&& chan.mCircleType == channel.mMeta.mCircleType
&& chan.mAuthorId == channel.mMeta.mAuthorId
&& chan.mCircleId == channel.mMeta.mCircleId
&& chan.mServiceString == channel.mMeta.mServiceString
&& chan.mGroupName == channel.mMeta.mGroupName )
{
channel.mMeta = chan;
found = true;
break;
}
}
#ifdef RS_DEEP_SEARCH
if(found) DeepSearch::indexChannelGroup(channel);
#endif // RS_DEEP_SEARCH
return found;
}
bool p3GxsChannels::createPost(RsGxsChannelPost& post)
{
uint32_t token;
time_t beginCreation = time(nullptr);
if( !createPost(token, post)
|| waitToken(token) != RsTokenService::COMPLETE ) return false;
time_t endCreation = time(nullptr);
// TODO: Need a way to get the post id back!
//#ifdef RS_DEEP_SEARCH
// DeepSearch::indexChannelPost(post);
//#endif // RS_DEEP_SEARCH
std::list<RsGxsGroupId> chanIds; chanIds.push_back(post.mMeta.mGroupId);
std::vector<RsGxsChannelPost> posts;
std::vector<RsGxsComment> comments;
if(!getChannelsContent(chanIds, posts, comments)) return false;
return true;
/* This is ugly but after digging and doing many tries of doing it the right
* way ending always into too big refactor chain reaction, I think this is
* not that bad.
* The shortcoming of this is that if posts with same data are created in
* a burst (more then once in a second) is that the id of another similar
* post could be returned, but this is a pointy case.
* Order of conditions in the `if` matter for performances */
bool found = false;
for(const RsGxsChannelPost& itPost : posts)
{
std::cout << __PRETTY_FUNCTION__ << " " << beginCreation << " "
<< itPost.mMeta.mPublishTs << " " << endCreation << " "
<< itPost.mMeta.mMsgId << std::endl;
if( itPost.mMeta.mPublishTs >= beginCreation
&& itPost.mMeta.mPublishTs <= endCreation
&& itPost.mMeta.mMsgFlags == post.mMeta.mMsgFlags
&& itPost.mMeta.mGroupId == post.mMeta.mGroupId
&& itPost.mMeta.mThreadId == post.mMeta.mThreadId
&& itPost.mMeta.mParentId == post.mMeta.mParentId
&& itPost.mMeta.mAuthorId == post.mMeta.mAuthorId
&& itPost.mMeta.mMsgName == post.mMeta.mMsgName
&& itPost.mFiles.size() == post.mFiles.size()
&& itPost.mMeta.mServiceString == post.mMeta.mServiceString
&& itPost.mOlderVersions == post.mOlderVersions
&& itPost.mMsg == post.mMsg )
{
post = itPost;
found = true;
break;
}
}
#ifdef RS_DEEP_SEARCH
if(found) DeepSearch::indexChannelPost(post);
#endif // RS_DEEP_SEARCH
return found;
}

View File

@ -3,7 +3,8 @@
* *
* libretroshare: retroshare core library *
* *
* Copyright 2012-2012 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2012 Robert Fernie <retroshare@lunamutt.com> *
* Copyright (C) 2018 Gioacchino Mazzurco <gio@eigenlab.org> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
@ -33,10 +34,6 @@
#include <map>
#include <string>
/*
*
*/
class SSGxsChannelGroup
{