2013-03-11 16:40:28 -04:00
|
|
|
/*
|
|
|
|
* libretroshare/src/services p3posted.cc
|
|
|
|
*
|
|
|
|
* Posted interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2012-2012 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2.1 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "services/p3posted.h"
|
|
|
|
#include "serialiser/rsposteditems.h"
|
|
|
|
|
2013-06-04 17:00:43 -04:00
|
|
|
#include <math.h>
|
2013-03-11 16:40:28 -04:00
|
|
|
|
|
|
|
/****
|
|
|
|
* #define POSTED_DEBUG 1
|
|
|
|
****/
|
|
|
|
|
|
|
|
RsPosted *rsPosted = NULL;
|
|
|
|
|
|
|
|
/********************************************************************************/
|
|
|
|
/******************* Startup / Tick ******************************************/
|
|
|
|
/********************************************************************************/
|
|
|
|
|
|
|
|
p3Posted::p3Posted(RsGeneralDataService *gds, RsNetworkExchangeService *nes, RsGixs* gixs)
|
2014-01-20 06:42:27 -05:00
|
|
|
:p3PostBase(gds, nes, gixs, new RsGxsPostedSerialiser(), RS_SERVICE_GXSV2_TYPE_POSTED),
|
|
|
|
RsPosted(this)
|
2013-03-11 16:40:28 -04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-20 06:42:27 -05:00
|
|
|
|
2014-03-21 23:53:44 -04:00
|
|
|
const std::string GXS_POSTED_APP_NAME = "gxsposted";
|
|
|
|
const uint16_t GXS_POSTED_APP_MAJOR_VERSION = 1;
|
|
|
|
const uint16_t GXS_POSTED_APP_MINOR_VERSION = 0;
|
|
|
|
const uint16_t GXS_POSTED_MIN_MAJOR_VERSION = 1;
|
|
|
|
const uint16_t GXS_POSTED_MIN_MINOR_VERSION = 0;
|
|
|
|
|
|
|
|
RsServiceInfo p3Posted::getServiceInfo()
|
|
|
|
{
|
|
|
|
return RsServiceInfo(RS_SERVICE_GXSV2_TYPE_POSTED,
|
|
|
|
GXS_POSTED_APP_NAME,
|
|
|
|
GXS_POSTED_APP_MAJOR_VERSION,
|
|
|
|
GXS_POSTED_APP_MINOR_VERSION,
|
|
|
|
GXS_POSTED_MIN_MAJOR_VERSION,
|
|
|
|
GXS_POSTED_MIN_MINOR_VERSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-11 16:40:28 -04:00
|
|
|
bool p3Posted::getGroupData(const uint32_t &token, std::vector<RsPostedGroup> &groups)
|
|
|
|
{
|
|
|
|
std::vector<RsGxsGrpItem*> grpData;
|
|
|
|
bool ok = RsGenExchange::getGroupData(token, grpData);
|
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
std::vector<RsGxsGrpItem*>::iterator vit = grpData.begin();
|
|
|
|
|
|
|
|
for(; vit != grpData.end(); vit++)
|
|
|
|
{
|
|
|
|
RsGxsPostedGroupItem* item = dynamic_cast<RsGxsPostedGroupItem*>(*vit);
|
2013-07-20 21:50:13 -04:00
|
|
|
if (item)
|
|
|
|
{
|
|
|
|
RsPostedGroup grp = item->mGroup;
|
|
|
|
item->mGroup.mMeta = item->meta;
|
|
|
|
grp.mMeta = item->mGroup.mMeta;
|
|
|
|
delete item;
|
|
|
|
groups.push_back(grp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "Not a RsGxsPostedGroupItem, deleting!" << std::endl;
|
|
|
|
delete *vit;
|
|
|
|
}
|
2013-03-11 16:40:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3Posted::getPostData(const uint32_t &token, std::vector<RsPostedPost> &msgs)
|
|
|
|
{
|
|
|
|
GxsMsgDataMap msgData;
|
|
|
|
bool ok = RsGenExchange::getMsgData(token, msgData);
|
2013-06-04 17:00:43 -04:00
|
|
|
time_t now = time(NULL);
|
2013-07-20 21:50:13 -04:00
|
|
|
|
2013-03-11 16:40:28 -04:00
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
GxsMsgDataMap::iterator mit = msgData.begin();
|
|
|
|
|
|
|
|
for(; mit != msgData.end(); mit++)
|
|
|
|
{
|
|
|
|
RsGxsGroupId grpId = mit->first;
|
|
|
|
std::vector<RsGxsMsgItem*>& msgItems = mit->second;
|
|
|
|
std::vector<RsGxsMsgItem*>::iterator vit = msgItems.begin();
|
|
|
|
|
|
|
|
for(; vit != msgItems.end(); vit++)
|
|
|
|
{
|
|
|
|
RsGxsPostedPostItem* item = dynamic_cast<RsGxsPostedPostItem*>(*vit);
|
2013-07-20 21:50:13 -04:00
|
|
|
|
2013-03-11 16:40:28 -04:00
|
|
|
if(item)
|
|
|
|
{
|
|
|
|
RsPostedPost msg = item->mPost;
|
|
|
|
msg.mMeta = item->meta;
|
2013-06-04 17:00:43 -04:00
|
|
|
msg.calculateScores(now);
|
|
|
|
|
2013-03-11 16:40:28 -04:00
|
|
|
msgs.push_back(msg);
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "Not a PostedPostItem, deleting!" << std::endl;
|
|
|
|
delete *vit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool p3Posted::getRelatedPosts(const uint32_t &token, std::vector<RsPostedPost> &msgs)
|
|
|
|
{
|
|
|
|
GxsMsgRelatedDataMap msgData;
|
|
|
|
bool ok = RsGenExchange::getMsgRelatedData(token, msgData);
|
2013-06-04 17:00:43 -04:00
|
|
|
time_t now = time(NULL);
|
2013-03-11 16:40:28 -04:00
|
|
|
|
|
|
|
if(ok)
|
|
|
|
{
|
|
|
|
GxsMsgRelatedDataMap::iterator mit = msgData.begin();
|
|
|
|
|
|
|
|
for(; mit != msgData.end(); mit++)
|
|
|
|
{
|
|
|
|
std::vector<RsGxsMsgItem*>& msgItems = mit->second;
|
|
|
|
std::vector<RsGxsMsgItem*>::iterator vit = msgItems.begin();
|
|
|
|
|
|
|
|
for(; vit != msgItems.end(); vit++)
|
|
|
|
{
|
|
|
|
RsGxsPostedPostItem* item = dynamic_cast<RsGxsPostedPostItem*>(*vit);
|
|
|
|
|
|
|
|
if(item)
|
|
|
|
{
|
|
|
|
RsPostedPost msg = item->mPost;
|
|
|
|
msg.mMeta = item->meta;
|
2013-06-04 17:00:43 -04:00
|
|
|
msg.calculateScores(now);
|
|
|
|
|
2013-03-11 16:40:28 -04:00
|
|
|
msgs.push_back(msg);
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::cerr << "Not a PostedPostItem, deleting!" << std::endl;
|
|
|
|
delete *vit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
/********************************************************************************************/
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2013-06-04 17:00:43 -04:00
|
|
|
/* Switched from having explicit Ranking calculations to calculating the set of scores
|
|
|
|
* on each RsPostedPost item.
|
|
|
|
*
|
|
|
|
* TODO: move this function to be part of RsPostedPost - then the GUI
|
|
|
|
* can reuse is as necessary.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool RsPostedPost::calculateScores(time_t ref_time)
|
2013-03-11 16:40:28 -04:00
|
|
|
{
|
2013-06-04 17:00:43 -04:00
|
|
|
/* so we want to calculate all the scores for this Post. */
|
2013-03-11 16:40:28 -04:00
|
|
|
|
2013-06-04 17:00:43 -04:00
|
|
|
PostStats stats;
|
2014-01-20 06:42:27 -05:00
|
|
|
extractPostCache(mMeta.mServiceString, stats);
|
2013-03-11 16:40:28 -04:00
|
|
|
|
2013-06-04 17:00:43 -04:00
|
|
|
mUpVotes = stats.up_votes;
|
|
|
|
mDownVotes = stats.down_votes;
|
|
|
|
mComments = stats.comments;
|
|
|
|
mHaveVoted = (mMeta.mMsgStatus & GXS_SERV::GXS_MSG_STATUS_VOTE_MASK);
|
2013-03-11 16:40:28 -04:00
|
|
|
|
2013-06-04 17:00:43 -04:00
|
|
|
time_t age_secs = ref_time - mMeta.mPublishTs;
|
|
|
|
#define POSTED_AGESHIFT (2.0)
|
|
|
|
#define POSTED_AGEFACTOR (3600.0)
|
2013-03-11 16:40:28 -04:00
|
|
|
|
2013-06-04 17:00:43 -04:00
|
|
|
mTopScore = ((int) mUpVotes - (int) mDownVotes);
|
|
|
|
if (mTopScore > 0)
|
|
|
|
{
|
|
|
|
// score drops with time.
|
|
|
|
mHotScore = mTopScore / pow(POSTED_AGESHIFT + age_secs / POSTED_AGEFACTOR, 1.5);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// gets more negative with time.
|
|
|
|
mHotScore = mTopScore * pow(POSTED_AGESHIFT + age_secs / POSTED_AGEFACTOR, 1.5);
|
|
|
|
}
|
|
|
|
mNewScore = -age_secs;
|
2013-03-11 16:40:28 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
bool p3Posted::createGroup(uint32_t &token, RsPostedGroup &group)
|
|
|
|
{
|
|
|
|
std::cerr << "p3Posted::createGroup()" << std::endl;
|
|
|
|
|
|
|
|
RsGxsPostedGroupItem* grpItem = new RsGxsPostedGroupItem();
|
|
|
|
grpItem->mGroup = group;
|
|
|
|
grpItem->meta = group.mMeta;
|
|
|
|
|
|
|
|
RsGenExchange::publishGroup(token, grpItem);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-19 06:08:37 -05:00
|
|
|
bool p3Posted::updateGroup(uint32_t &token, RsPostedGroup &group)
|
2014-02-09 05:33:16 -05:00
|
|
|
{
|
|
|
|
std::cerr << "p3Posted::updateGroup()" << std::endl;
|
|
|
|
|
|
|
|
RsGxsPostedGroupItem* grpItem = new RsGxsPostedGroupItem();
|
|
|
|
grpItem->mGroup = group;
|
|
|
|
grpItem->meta = group.mMeta;
|
|
|
|
|
2014-02-19 06:08:37 -05:00
|
|
|
RsGenExchange::updateGroup(token, grpItem);
|
2014-02-09 05:33:16 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-11 16:40:28 -04:00
|
|
|
bool p3Posted::createPost(uint32_t &token, RsPostedPost &msg)
|
|
|
|
{
|
|
|
|
std::cerr << "p3Posted::createPost() GroupId: " << msg.mMeta.mGroupId;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
RsGxsPostedPostItem* msgItem = new RsGxsPostedPostItem();
|
|
|
|
msgItem->mPost = msg;
|
|
|
|
msgItem->meta = msg.mMeta;
|
|
|
|
|
|
|
|
RsGenExchange::publishMsg(token, msgItem);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************/
|