libresapi: add basic forum read api (patch from Chozabu) close #32

This commit is contained in:
electron128 2015-08-31 19:44:15 +02:00
parent ef292faa22
commit 5d4e94c671
5 changed files with 175 additions and 0 deletions

View File

@ -225,6 +225,7 @@ public:
ApiServerMainModules(ResourceRouter& router, StateTokenServer* sts, const RsPlugInInterfaces &ifaces):
mPeersHandler(sts, ifaces.mNotify, ifaces.mPeers, ifaces.mMsgs),
mIdentityHandler(ifaces.mIdentity),
mForumHandler(ifaces.mGxsForums),
mServiceControlHandler(rsServiceControl), // TODO: don't use global variable here
mFileSearchHandler(sts, ifaces.mNotify, ifaces.mTurtle, ifaces.mFiles),
mTransfersHandler(sts, ifaces.mFiles),
@ -238,6 +239,8 @@ public:
&PeersHandler::handleRequest);
router.addResourceHandler("identity", dynamic_cast<ResourceRouter*>(&mIdentityHandler),
&IdentityHandler::handleRequest);
router.addResourceHandler("forums", dynamic_cast<ResourceRouter*>(&mForumHandler),
&ForumHandler::handleRequest);
router.addResourceHandler("servicecontrol", dynamic_cast<ResourceRouter*>(&mServiceControlHandler),
&ServiceControlHandler::handleRequest);
router.addResourceHandler("filesearch", dynamic_cast<ResourceRouter*>(&mFileSearchHandler),
@ -250,6 +253,7 @@ public:
PeersHandler mPeersHandler;
IdentityHandler mIdentityHandler;
ForumHandler mForumHandler;
ServiceControlHandler mServiceControlHandler;
FileSearchHandler mFileSearchHandler;
TransfersHandler mTransfersHandler;

View File

@ -5,6 +5,7 @@
#include "ApiTypes.h"
#include "PeersHandler.h"
#include "IdentityHandler.h"
#include "ForumHandler.h"
#include "ServiceControlHandler.h"
#include "StateTokenServer.h"
#include "FileSearchHandler.h"

View File

@ -0,0 +1,148 @@
#include "ForumHandler.h"
#include <retroshare/rsgxsforums.h>
#include <time.h>
#include "Operators.h"
#include "ApiTypes.h"
#include "GxsResponseTask.h"
#ifndef WINDOWS_SYS
#include "unistd.h"
#endif
namespace resource_api
{
ForumHandler::ForumHandler(RsGxsForums* forums):
mRsGxsForums(forums)
{
addResourceHandler("*", this, &ForumHandler::handleWildcard);
}
void ForumHandler::handleWildcard(Request &req, Response &resp)
{
bool ok = true;
if(!req.mPath.empty())
{
std::string str = req.mPath.top();
req.mPath.pop();
if(str != "")
{
//assume we have a groupID
RsGxsGroupId grpId(str);
std::list<RsGxsGroupId> groupIds;
groupIds.push_back(grpId);
uint32_t token;
RsTokReqOptions opts;
opts.mReqType = GXS_REQUEST_TYPE_MSG_DATA;
mRsGxsForums->getTokenService()->requestMsgInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts, groupIds);
time_t start = time(NULL);
while((mRsGxsForums->getTokenService()->requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE)
&&(mRsGxsForums->getTokenService()->requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED)
&&((time(NULL) < (start+10)))
)
{
#ifdef WINDOWS_SYS
Sleep(500);
#else
usleep(500*1000) ;
#endif
}
if(mRsGxsForums->getTokenService()->requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE)
{
std::vector<RsGxsForumMsg> grps;
ok &= mRsGxsForums->getMsgData(token, grps);
for(std::vector<RsGxsForumMsg>::iterator vit = grps.begin(); vit != grps.end(); vit++)
{
RsGxsForumMsg& grp = *vit;
KeyValueReference<RsGxsGroupId> group_id("group_id", grp.mMeta.mGroupId);
resp.mDataStream.getStreamToMember()
<< group_id
<< makeKeyValueReference("name", grp.mMeta.mMsgName)
<< makeKeyValueReference("id", grp.mMeta.mMsgId)
<< makeKeyValueReference("parent_id", grp.mMeta.mParentId)
<< makeKeyValueReference("author_id", grp.mMeta.mAuthorId)
<< makeKeyValueReference("orig_msg_id", grp.mMeta.mOrigMsgId)
<< makeKeyValueReference("thread_id", grp.mMeta.mThreadId)
<< makeKeyValueReference("message", grp.mMsg);
}
}
else
{
ok = false;
}
}
}
else
{
// no more path element
RsTokReqOptions opts;
opts.mReqType = GXS_REQUEST_TYPE_GROUP_DATA;
uint32_t token;
mRsGxsForums->getTokenService()->requestGroupInfo(token, RS_TOKREQ_ANSTYPE_DATA, opts);
time_t start = time(NULL);
while((mRsGxsForums->getTokenService()->requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE)
&&(mRsGxsForums->getTokenService()->requestStatus(token) != RsTokenService::GXS_REQUEST_V2_STATUS_FAILED)
&&((time(NULL) < (start+10)))
)
{
#ifdef WINDOWS_SYS
Sleep(500);
#else
usleep(500*1000) ;
#endif
}
if(mRsGxsForums->getTokenService()->requestStatus(token) == RsTokenService::GXS_REQUEST_V2_STATUS_COMPLETE)
{
std::vector<RsGxsForumGroup> grps;
ok &= mRsGxsForums->getGroupData(token, grps);
for(std::vector<RsGxsForumGroup>::iterator vit = grps.begin(); vit != grps.end(); vit++)
{
RsGxsForumGroup& grp = *vit;
KeyValueReference<RsGxsGroupId> id("id", grp.mMeta.mGroupId);
KeyValueReference<u_int32_t> vis_msg("visible_msg_count", grp.mMeta.mVisibleMsgCount);
//KeyValueReference<RsPgpId> pgp_id("pgp_id",grp.mPgpId );
// not very happy about this, i think the flags should stay hidden in rsidentities
bool own = (grp.mMeta.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN);
bool pgp_linked = (grp.mMeta.mGroupFlags & RSGXSID_GROUPFLAG_REALID);
bool subscribed = IS_GROUP_SUBSCRIBED(grp.mMeta.mSubscribeFlags);
resp.mDataStream.getStreamToMember()
<< id
//<< pgp_id
<< makeKeyValueReference("name", grp.mMeta.mGroupName)
//<< makeKeyValueReference("last_post", grp.mMeta.mLastPost)
<< makeKeyValueReference("pop", grp.mMeta.mPop)
//<< makeKeyValueReference("publish_ts", grp.mMeta.mPublishTs)
<< vis_msg
<< makeKeyValueReference("group_status", grp.mMeta.mGroupStatus)
<< makeKeyValueReference("author_id", grp.mMeta.mAuthorId)
<< makeKeyValueReference("parent_grp_id", grp.mMeta.mParentGrpId)
<< makeKeyValueReference("description", grp.mDescription)
<< makeKeyValueReference("own", own)
<< makeKeyValueReference("subscribed", subscribed)
<< makeKeyValueReference("pgp_linked", pgp_linked);
}
}
else
{
ok = false;
}
}
if(ok)
{
resp.setOk();
}
else
{
resp.setFail();
}
}
} // namespace resource_api

View File

@ -0,0 +1,20 @@
#ifndef FORUMHANDLER_H
#define FORUMHANDLER_H
#include "ResourceRouter.h"
class RsGxsForums;
namespace resource_api
{
class ForumHandler : public ResourceRouter
{
public:
ForumHandler(RsGxsForums* forums);
private:
RsGxsForums* mRsGxsForums;
void handleWildcard(Request& req, Response& resp);
};
} // namespace resource_api
#endif // FORUMHANDLER_H

View File

@ -29,6 +29,7 @@ SOURCES += \
api/PeersHandler.cpp \
api/Operators.cpp \
api/IdentityHandler.cpp \
api/ForumHandler.cpp \
api/ServiceControlHandler.cpp \
api/StateTokenServer.cpp \
api/GxsResponseTask.cpp \
@ -49,6 +50,7 @@ HEADERS += \
api/PeersHandler.h \
api/Operators.h \
api/IdentityHandler.h \
api/ForumHandler.h \
api/ServiceControlHandler.h \
api/GxsMetaOperators.h \
api/StateTokenServer.h \