From a6e8a27fc0355235f7e0dcf20152faa4eda14195 Mon Sep 17 00:00:00 2001 From: Gioacchino Mazzurco Date: Sat, 16 Feb 2019 11:41:31 -0300 Subject: [PATCH] Add RsReputations JSON API --- libretroshare/src/retroshare/rsreputations.h | 164 +++++++++++++++--- libretroshare/src/services/p3gxsreputation.cc | 16 +- libretroshare/src/services/p3gxsreputation.h | 18 +- libretroshare/src/services/p3idservice.cc | 2 +- 4 files changed, 162 insertions(+), 38 deletions(-) diff --git a/libretroshare/src/retroshare/rsreputations.h b/libretroshare/src/retroshare/rsreputations.h index 68f49c058..8ea6ff213 100644 --- a/libretroshare/src/retroshare/rsreputations.h +++ b/libretroshare/src/retroshare/rsreputations.h @@ -3,7 +3,8 @@ * * * libretroshare: retroshare core library * * * - * Copyright 2015 by Cyril Soler * + * Copyright (C) 2015 Cyril Soler * + * 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 * @@ -23,6 +24,7 @@ #include "retroshare/rsids.h" #include "retroshare/rsgxsifacetypes.h" +#include "serialiser/rsserializable.h" class RsReputations; @@ -33,8 +35,7 @@ class RsReputations; extern RsReputations* rsReputations; -const float REPUTATION_THRESHOLD_DEFAULT = 1.0f; -const float REPUTATION_THRESHOLD_ANTI_SPAM = 1.4f; +constexpr float RS_REPUTATION_THRESHOLD_DEFAULT = 1.0f; enum struct RsOpinion : uint8_t { @@ -64,13 +65,14 @@ enum struct RsReputationLevel : uint8_t UNKNOWN = 0x05 }; -struct RsReputationInfo +struct RsReputationInfo : RsSerializable { RsReputationInfo() : mOwnOpinion(RsOpinion::NEUTRAL), mFriendsPositiveVotes(0), mFriendsNegativeVotes(0), - mFriendAverageScore(REPUTATION_THRESHOLD_DEFAULT), + mFriendAverageScore(RS_REPUTATION_THRESHOLD_DEFAULT), mOverallReputationLevel(RsReputationLevel::NEUTRAL) {} + virtual ~RsReputationInfo() {} RsOpinion mOwnOpinion; @@ -81,6 +83,17 @@ struct RsReputationInfo /// this should help clients in taking decisions RsReputationLevel mOverallReputationLevel; + + /// @see RsSerializable + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) override + { + RS_SERIAL_PROCESS(mOwnOpinion); + RS_SERIAL_PROCESS(mFriendsPositiveVotes); + RS_SERIAL_PROCESS(mFriendsNegativeVotes); + RS_SERIAL_PROCESS(mFriendAverageScore); + RS_SERIAL_PROCESS(mOverallReputationLevel); + } }; @@ -89,34 +102,143 @@ class RsReputations public: virtual ~RsReputations() {} - virtual bool setOwnOpinion(const RsGxsId& key_id, RsOpinion op) = 0; - virtual bool getOwnOpinion(const RsGxsId& key_id, RsOpinion& op) = 0; + /** + * @brief Set own opinion about the given identity + * @jsonapi{development} + * @param[in] id Id of the identity + * @param[in] op Own opinion + * @return false on error, true otherwise + */ + virtual bool setOwnOpinion(const RsGxsId& id, RsOpinion op) = 0; + + /** + * @brief Get own opition about the given identity + * @jsonapi{development} + * @param[in] id Id of the identity + * @param[out] op Own opinion + * @return false on error, true otherwise + */ + virtual bool getOwnOpinion(const RsGxsId& id, RsOpinion& op) = 0; + + /** + * @brief Get reputation data of given identity + * @jsonapi{development} + * @param[in] id Id of the identity + * @param[in] ownerNode Optiona PGP id of the signed identity, accept a null + * (all zero/noninitialized) PGP id + * @param[out] info storage for the information + * @param[in] stamp if true, timestamo the information + * @return false on error, true otherwise + */ virtual bool getReputationInfo( const RsGxsId& id, const RsPgpId& ownerNode, RsReputationInfo& info, bool stamp = true ) = 0; - /** This returns the reputation level and also the flags of the identity - * service for that id. This is useful in order to get these flags without - * relying on the async method of p3Identity */ - RS_DEPRECATED - virtual RsReputationLevel overallReputationLevel( - const RsGxsId& id, uint32_t* identity_flags = nullptr) = 0; + /** + * @brief Get overall reputation level of given identity + * @jsonapi{development} + * @param[in] id Id of the identity + * @return the calculated reputation level based on available information + */ + virtual RsReputationLevel overallReputationLevel(const RsGxsId& id) = 0; - virtual void setNodeAutoPositiveOpinionForContacts(bool b) = 0; - virtual bool nodeAutoPositiveOpinionForContacts() = 0; + /** + * @brief Enable giving automatic positive opinion when flagging as contact + * @jsonapi{development} + * @param[in] b true to enable, false to disable + */ + virtual void setAutoPositiveOpinionForContacts(bool b) = 0; - virtual uint32_t thresholdForRemotelyNegativeReputation() = 0; - virtual uint32_t thresholdForRemotelyPositiveReputation() = 0; + /** + * @brief check if giving automatic positive opinion when flagging as + * contact is enbaled + * @jsonapi{development} + * @return true if enabled, false otherwise + */ + virtual bool autoPositiveOpinionForContacts() = 0; + + /** + * @brief Set threshold on remote reputation to consider it remotely + * negative + * @jsonapi{development} + * @param[in] thresh Threshold value + */ virtual void setThresholdForRemotelyNegativeReputation(uint32_t thresh) = 0; + + /** + * * @brief Get threshold on remote reputation to consider it remotely + * negative + * @jsonapi{development} + * @return Threshold value + */ + virtual uint32_t thresholdForRemotelyNegativeReputation() = 0; + + /** + * @brief Set threshold on remote reputation to consider it remotely + * positive + * @jsonapi{development} + * @param[in] thresh Threshold value + */ virtual void setThresholdForRemotelyPositiveReputation(uint32_t thresh) = 0; - virtual void setRememberDeletedNodesThreshold(uint32_t days) = 0; - virtual uint32_t rememberDeletedNodesThreshold() = 0; + /** + * @brief Get threshold on remote reputation to consider it remotely + * negative + * @jsonapi{development} + * @return Threshold value + */ + virtual uint32_t thresholdForRemotelyPositiveReputation() = 0; - /** This one is a proxy designed to allow fast checking of a GXS id. - * It basically returns true if assessment is not ASSESSMENT_OK */ + /** + * @brief Get number of days to wait before deleting a banned identity from + * local storage + * @jsonapi{development} + * @return number of days to wait, 0 means never delete + */ + virtual uint32_t rememberBannedIdThreshold() = 0; + + /** + * @brief Set number of days to wait before deleting a banned identity from + * local storage + * @jsonapi{development} + * @param[in] days number of days to wait, 0 means never delete + */ + virtual void setRememberBannedIdThreshold(uint32_t days) = 0; + + /** + * @brief This method allow fast checking if a GXS identity is banned. + * @jsonapi{development} + * @param[in] id Id of the identity to check + * @return true if identity is banned, false otherwise + */ virtual bool isIdentityBanned(const RsGxsId& id) = 0; + /** + * @brief Check if automatic banning of all identities signed by the given + * node is enabled + * @jsonapi{development} + * @param[in] id PGP id of the node + * @return true if enabled, false otherwise + */ virtual bool isNodeBanned(const RsPgpId& id) = 0; + + /** + * @brief Enable automatic banning of all identities signed by the given + * node + * @jsonapi{development} + * @param[in] id PGP id of the node + * @param[in] b true to enable, false to disable + */ virtual void banNode(const RsPgpId& id, bool b) = 0; + + + /** + * @deprecated + * This returns the reputation level and also the flags of the identity + * service for that id. This is useful in order to get these flags without + * relying on the async method of p3Identity + */ + RS_DEPRECATED + virtual RsReputationLevel overallReputationLevel( + const RsGxsId& id, uint32_t* identity_flags ) = 0; }; diff --git a/libretroshare/src/services/p3gxsreputation.cc b/libretroshare/src/services/p3gxsreputation.cc index e99c07b2e..a6fb352d2 100644 --- a/libretroshare/src/services/p3gxsreputation.cc +++ b/libretroshare/src/services/p3gxsreputation.cc @@ -236,7 +236,7 @@ int p3GxsReputation::tick() return 0; } -void p3GxsReputation::setNodeAutoPositiveOpinionForContacts(bool b) +void p3GxsReputation::setAutoPositiveOpinionForContacts(bool b) { RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ @@ -249,13 +249,13 @@ void p3GxsReputation::setNodeAutoPositiveOpinionForContacts(bool b) IndicateConfigChanged() ; } } -bool p3GxsReputation::nodeAutoPositiveOpinionForContacts() +bool p3GxsReputation::autoPositiveOpinionForContacts() { RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ return mAutoSetPositiveOptionToContacts ; } -void p3GxsReputation::setRememberDeletedNodesThreshold(uint32_t days) +void p3GxsReputation::setRememberBannedIdThreshold(uint32_t days) { RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ @@ -265,7 +265,7 @@ void p3GxsReputation::setRememberDeletedNodesThreshold(uint32_t days) IndicateConfigChanged(); } } -uint32_t p3GxsReputation::rememberDeletedNodesThreshold() +uint32_t p3GxsReputation::rememberBannedIdThreshold() { RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ @@ -831,7 +831,7 @@ bool p3GxsReputation::getReputationInfo( if(it == mReputations.end()) { info.mOwnOpinion = RsOpinion::NEUTRAL ; - info.mFriendAverageScore = REPUTATION_THRESHOLD_DEFAULT ; + info.mFriendAverageScore = RS_REPUTATION_THRESHOLD_DEFAULT ; info.mFriendsNegativeVotes = 0 ; info.mFriendsPositiveVotes = 0 ; @@ -979,9 +979,13 @@ void p3GxsReputation::banNode(const RsPgpId& id,bool b) } } } + +RsReputationLevel p3GxsReputation::overallReputationLevel(const RsGxsId& id) +{ return overallReputationLevel(id, nullptr); } + bool p3GxsReputation::isNodeBanned(const RsPgpId& id) { - RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ + RsStackMutex stack(mReputationMtx); /****** LOCKED MUTEX *******/ return mBannedPgpIds.find(id) != mBannedPgpIds.end(); } diff --git a/libretroshare/src/services/p3gxsreputation.h b/libretroshare/src/services/p3gxsreputation.h index 688ec9711..e9a14d4e7 100644 --- a/libretroshare/src/services/p3gxsreputation.h +++ b/libretroshare/src/services/p3gxsreputation.h @@ -93,11 +93,6 @@ public: //!The p3GxsReputation service. - /** - * - * - */ - class p3GxsReputation: public p3Service, public p3Config, public RsGixsReputation, public RsReputations /* , public pqiMonitor */ { public: @@ -114,14 +109,17 @@ public: virtual bool isNodeBanned(const RsPgpId& id); virtual void banNode(const RsPgpId& id,bool b) ; + + RsReputationLevel overallReputationLevel(const RsGxsId& id) override; + virtual RsReputationLevel overallReputationLevel( - const RsGxsId& id, uint32_t* identity_flags = nullptr ); + const RsGxsId& id, uint32_t* identity_flags ); - virtual void setNodeAutoPositiveOpinionForContacts(bool b) ; - virtual bool nodeAutoPositiveOpinionForContacts() ; + virtual void setAutoPositiveOpinionForContacts(bool b) ; + virtual bool autoPositiveOpinionForContacts() ; - virtual void setRememberDeletedNodesThreshold(uint32_t days) ; - virtual uint32_t rememberDeletedNodesThreshold() ; + virtual void setRememberBannedIdThreshold(uint32_t days) ; + virtual uint32_t rememberBannedIdThreshold() ; uint32_t thresholdForRemotelyNegativeReputation(); uint32_t thresholdForRemotelyPositiveReputation(); diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index bd7ca905a..de9517843 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -711,7 +711,7 @@ bool p3IdService::getIdDetails(const RsGxsId &id, RsIdentityDetails &details) // This step is needed, because p3GxsReputation does not know all identities, and might not have any data for // the ones in the contact list. So we change them on demand. - if(is_a_contact && rsReputations->nodeAutoPositiveOpinionForContacts()) + if(is_a_contact && rsReputations->autoPositiveOpinionForContacts()) { RsOpinion op; if( rsReputations->getOwnOpinion(id,op) &&