From 321fc8a8d450fd6c6b97683e8c5b2cdcf111d334 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Wed, 10 Oct 2018 06:40:56 +0200 Subject: [PATCH] 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. --- libretroshare/src/retroshare/rsgxschannels.h | 2 +- libretroshare/src/services/p3gxschannels.cc | 98 +++++++++++++++++--- libretroshare/src/services/p3gxschannels.h | 7 +- 3 files changed, 89 insertions(+), 18 deletions(-) diff --git a/libretroshare/src/retroshare/rsgxschannels.h b/libretroshare/src/retroshare/rsgxschannels.h index 50e8e47d2..16dcbd5e0 100644 --- a/libretroshare/src/retroshare/rsgxschannels.h +++ b/libretroshare/src/retroshare/rsgxschannels.h @@ -4,7 +4,7 @@ * * * libretroshare: retroshare core library * * * - * Copyright 2012-2012 by Robert Fernie * + * Copyright (C) 2012 Robert Fernie * * Copyright (C) 2018 Gioacchino Mazzurco * * * * This program is free software: you can redistribute it and/or modify * diff --git a/libretroshare/src/services/p3gxschannels.cc b/libretroshare/src/services/p3gxschannels.cc index 35caff413..22be12902 100644 --- a/libretroshare/src/services/p3gxschannels.cc +++ b/libretroshare/src/services/p3gxschannels.cc @@ -3,7 +3,8 @@ * * * libretroshare: retroshare core library * * * - * Copyright 2012-2012 Robert Fernie * + * Copyright (C) 2012 Robert Fernie * + * Copyright (C) 2018 Gioacchino Mazzurco * * * * 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 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 chanIds; chanIds.push_back(post.mMeta.mGroupId); + std::vector posts; + std::vector 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; } diff --git a/libretroshare/src/services/p3gxschannels.h b/libretroshare/src/services/p3gxschannels.h index b3c027342..0aad40071 100644 --- a/libretroshare/src/services/p3gxschannels.h +++ b/libretroshare/src/services/p3gxschannels.h @@ -3,7 +3,8 @@ * * * libretroshare: retroshare core library * * * - * Copyright 2012-2012 Robert Fernie * + * Copyright (C) 2012 Robert Fernie * + * Copyright (C) 2018 Gioacchino Mazzurco * * * * 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 #include -/* - * - */ - class SSGxsChannelGroup {