From ae932b605a416186867b241aebcb27573912ee1b Mon Sep 17 00:00:00 2001 From: csoler Date: Wed, 4 Mar 2020 19:17:09 +0100 Subject: [PATCH] fixed 2 bugs causing forums and channels to be notified when already known and added notification list for posted --- libretroshare/src/rsitems/rsserviceids.h | 3 + libretroshare/src/rsserver/rsinit.cc | 1 + libretroshare/src/services/p3gxschannels.cc | 14 +-- libretroshare/src/services/p3gxsforums.cc | 14 +-- libretroshare/src/services/p3postbase.cc | 111 ++++++++++++++++++-- libretroshare/src/services/p3postbase.h | 46 ++++---- 6 files changed, 148 insertions(+), 41 deletions(-) diff --git a/libretroshare/src/rsitems/rsserviceids.h b/libretroshare/src/rsitems/rsserviceids.h index 3f690cf45..21c399fc0 100644 --- a/libretroshare/src/rsitems/rsserviceids.h +++ b/libretroshare/src/rsitems/rsserviceids.h @@ -64,6 +64,7 @@ enum class RsServiceType : uint16_t GXS_TRANS = 0x0230, JSONAPI = 0x0240, FORUMS_CONFIG = 0x0315, + POSTED_CONFIG = 0x0316, CHANNELS_CONFIG = 0x0317, RTT = 0x1011, /// Round Trip Time @@ -123,7 +124,9 @@ RS_DEPRECATED_FOR(RsServiceType) const uint16_t RS_SERVICE_GXS_TYPE_REPUTATION RS_DEPRECATED_FOR(RsServiceType) const uint16_t RS_SERVICE_TYPE_GXS_RECOGN = 0x0220; RS_DEPRECATED_FOR(RsServiceType) const uint16_t RS_SERVICE_TYPE_GXS_TRANS = 0x0230; RS_DEPRECATED_FOR(RsServiceType) const uint16_t RS_SERVICE_TYPE_JSONAPI = 0x0240; +/// used to save notification records in GXS and possible other service-based configuration RS_DEPRECATED_FOR(RsServiceType) const uint16_t RS_SERVICE_GXS_TYPE_FORUMS_CONFIG = 0x0315; +RS_DEPRECATED_FOR(RsServiceType) const uint16_t RS_SERVICE_GXS_TYPE_POSTED_CONFIG = 0x0316; RS_DEPRECATED_FOR(RsServiceType) const uint16_t RS_SERVICE_GXS_TYPE_CHANNELS_CONFIG = 0x0317; // Experimental Services. diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index 5de23251a..a4e652c4c 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -1632,6 +1632,7 @@ int RsServer::StartupRetroShare() mConfigMgr->addConfiguration("gxschannels_srv.cfg", mGxsChannels); mConfigMgr->addConfiguration("gxscircles.cfg" , gxscircles_ns); mConfigMgr->addConfiguration("posted.cfg" , posted_ns); + mConfigMgr->addConfiguration("gxsposted_srv.cfg", mPosted); #ifdef RS_USE_WIKI mConfigMgr->addConfiguration("wiki.cfg", wiki_ns); #endif diff --git a/libretroshare/src/services/p3gxschannels.cc b/libretroshare/src/services/p3gxschannels.cc index 2a63b3cdb..2d141f57e 100644 --- a/libretroshare/src/services/p3gxschannels.cc +++ b/libretroshare/src/services/p3gxschannels.cc @@ -136,14 +136,14 @@ uint32_t p3GxsChannels::channelsAuthenPolicy() static const uint32_t GXS_CHANNELS_CONFIG_MAX_TIME_NOTIFY_STORAGE = 86400*30*2 ; // ignore notifications for 2 months static const uint8_t GXS_CHANNELS_CONFIG_SUBTYPE_NOTIFY_RECORD = 0x01 ; -struct RsGxsGroupNotifyRecordsItem: public RsItem +struct RsGxsChannelNotifyRecordsItem: public RsItem { - RsGxsGroupNotifyRecordsItem() + RsGxsChannelNotifyRecordsItem() : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_GXS_TYPE_CHANNELS_CONFIG,GXS_CHANNELS_CONFIG_SUBTYPE_NOTIFY_RECORD) {} - virtual ~RsGxsGroupNotifyRecordsItem() {} + virtual ~RsGxsChannelNotifyRecordsItem() {} void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) @@ -167,7 +167,7 @@ public: switch(item_sub_id) { - case GXS_CHANNELS_CONFIG_SUBTYPE_NOTIFY_RECORD: return new RsGxsGroupNotifyRecordsItem(); + case GXS_CHANNELS_CONFIG_SUBTYPE_NOTIFY_RECORD: return new RsGxsChannelNotifyRecordsItem(); default: return NULL; } @@ -178,7 +178,7 @@ bool p3GxsChannels::saveList(bool &cleanup, std::list&saveList) { cleanup = true ; - RsGxsGroupNotifyRecordsItem *item = new RsGxsGroupNotifyRecordsItem ; + RsGxsChannelNotifyRecordsItem *item = new RsGxsChannelNotifyRecordsItem ; { RS_STACK_MUTEX(mKnownChannelsMutex); @@ -198,7 +198,7 @@ bool p3GxsChannels::loadList(std::list& loadList) rstime_t now = time(NULL); - RsGxsGroupNotifyRecordsItem *fnr = dynamic_cast(item) ; + RsGxsChannelNotifyRecordsItem *fnr = dynamic_cast(item) ; if(fnr) { @@ -206,7 +206,7 @@ bool p3GxsChannels::loadList(std::list& loadList) mKnownChannels.clear(); for(auto it(fnr->records.begin());it!=fnr->records.end();++it) - if( it->second + GXS_CHANNELS_CONFIG_MAX_TIME_NOTIFY_STORAGE < now) + if( now < it->second + GXS_CHANNELS_CONFIG_MAX_TIME_NOTIFY_STORAGE) mKnownChannels.insert(*it) ; } diff --git a/libretroshare/src/services/p3gxsforums.cc b/libretroshare/src/services/p3gxsforums.cc index b8c414df0..5a6b0e568 100644 --- a/libretroshare/src/services/p3gxsforums.cc +++ b/libretroshare/src/services/p3gxsforums.cc @@ -96,14 +96,14 @@ uint32_t p3GxsForums::forumsAuthenPolicy() static const uint32_t GXS_FORUMS_CONFIG_MAX_TIME_NOTIFY_STORAGE = 86400*30*2 ; // ignore notifications for 2 months static const uint8_t GXS_FORUMS_CONFIG_SUBTYPE_NOTIFY_RECORD = 0x01 ; -struct RsGxsGroupNotifyRecordsItem: public RsItem +struct RsGxsForumNotifyRecordsItem: public RsItem { - RsGxsGroupNotifyRecordsItem() + RsGxsForumNotifyRecordsItem() : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_GXS_TYPE_FORUMS_CONFIG,GXS_FORUMS_CONFIG_SUBTYPE_NOTIFY_RECORD) {} - virtual ~RsGxsGroupNotifyRecordsItem() {} + virtual ~RsGxsForumNotifyRecordsItem() {} void serial_process( RsGenericSerializer::SerializeJob j, RsGenericSerializer::SerializeContext& ctx ) @@ -127,7 +127,7 @@ public: switch(item_sub_id) { - case GXS_FORUMS_CONFIG_SUBTYPE_NOTIFY_RECORD: return new RsGxsGroupNotifyRecordsItem(); + case GXS_FORUMS_CONFIG_SUBTYPE_NOTIFY_RECORD: return new RsGxsForumNotifyRecordsItem(); default: return NULL; } @@ -138,7 +138,7 @@ bool p3GxsForums::saveList(bool &cleanup, std::list&saveList) { cleanup = true ; - RsGxsGroupNotifyRecordsItem *item = new RsGxsGroupNotifyRecordsItem ; + RsGxsForumNotifyRecordsItem *item = new RsGxsForumNotifyRecordsItem ; item->records = mKnownForums ; @@ -155,14 +155,14 @@ bool p3GxsForums::loadList(std::list& loadList) rstime_t now = time(NULL); - RsGxsGroupNotifyRecordsItem *fnr = dynamic_cast(item) ; + RsGxsForumNotifyRecordsItem *fnr = dynamic_cast(item) ; if(fnr != NULL) { mKnownForums.clear(); for(auto it(fnr->records.begin());it!=fnr->records.end();++it) - if( it->second + GXS_FORUMS_CONFIG_MAX_TIME_NOTIFY_STORAGE < now) + if( now < it->second + GXS_FORUMS_CONFIG_MAX_TIME_NOTIFY_STORAGE) mKnownForums.insert(*it) ; } diff --git a/libretroshare/src/services/p3postbase.cc b/libretroshare/src/services/p3postbase.cc index faaa1efb3..8dd48c174 100644 --- a/libretroshare/src/services/p3postbase.cc +++ b/libretroshare/src/services/p3postbase.cc @@ -157,15 +157,26 @@ void p3PostBase::notifyChanges(std::vector &changes) const std::list& grpList = grpChange->mGrpIdList; for (auto git = grpList.begin(); git != grpList.end(); ++git) - { + { + if(mKnownPosted.find(*git) == mKnownPosted.end()) + { + mKnownPosted.insert(std::make_pair(*git, time(nullptr))); + IndicateConfigChanged(); + + auto ev = std::make_shared(); + ev->mPostedGroupId = *git; + ev->mPostedEventCode = RsPostedEventCode::NEW_POSTED_GROUP; + rsEvents->postEvent(ev); + #ifdef POSTBASE_DEBUG - std::cerr << "p3PostBase::notifyChanges() Incoming Group: " << *git; - std::cerr << std::endl; + std::cerr << "p3PostBase::notifyChanges() Incoming Group: " << *git; + std::cerr << std::endl; #endif - auto ev = std::make_shared(); - ev->mPostedGroupId = *git; - ev->mPostedEventCode = RsPostedEventCode::NEW_POSTED_GROUP; - rsEvents->postEvent(ev); + } + else + RsInfo() << __PRETTY_FUNCTION__ + << " Not notifying already known forum " + << *git << std::endl; } } break; @@ -828,3 +839,89 @@ void p3PostBase::handleResponse(uint32_t token, uint32_t req_type) } } +static const uint32_t GXS_POSTED_CONFIG_MAX_TIME_NOTIFY_STORAGE = 86400*30*2 ; // ignore notifications for 2 months +static const uint8_t GXS_POSTED_CONFIG_SUBTYPE_NOTIFY_RECORD = 0x01 ; + +struct RsGxsPostedNotifyRecordsItem: public RsItem +{ + + RsGxsPostedNotifyRecordsItem() + : RsItem(RS_PKT_VERSION_SERVICE,RS_SERVICE_GXS_TYPE_POSTED_CONFIG,GXS_POSTED_CONFIG_SUBTYPE_NOTIFY_RECORD) + {} + + virtual ~RsGxsPostedNotifyRecordsItem() {} + + void serial_process( RsGenericSerializer::SerializeJob j, + RsGenericSerializer::SerializeContext& ctx ) + { RS_SERIAL_PROCESS(records); } + + void clear() {} + + std::map records; +}; + +class GxsPostedConfigSerializer : public RsServiceSerializer +{ +public: + GxsPostedConfigSerializer() : RsServiceSerializer(RS_SERVICE_GXS_TYPE_POSTED_CONFIG) {} + virtual ~GxsPostedConfigSerializer() {} + + RsItem* create_item(uint16_t service_id, uint8_t item_sub_id) const + { + if(service_id != RS_SERVICE_GXS_TYPE_POSTED_CONFIG) + return NULL; + + switch(item_sub_id) + { + case GXS_POSTED_CONFIG_SUBTYPE_NOTIFY_RECORD: return new RsGxsPostedNotifyRecordsItem(); + default: + return NULL; + } + } +}; + +bool p3PostBase::saveList(bool &cleanup, std::list&saveList) +{ + cleanup = true ; + + RsGxsPostedNotifyRecordsItem *item = new RsGxsPostedNotifyRecordsItem ; + + item->records = mKnownPosted ; + + saveList.push_back(item) ; + return true; +} + +bool p3PostBase::loadList(std::list& loadList) +{ + while(!loadList.empty()) + { + RsItem *item = loadList.front(); + loadList.pop_front(); + + rstime_t now = time(NULL); + + RsGxsPostedNotifyRecordsItem *fnr = dynamic_cast(item) ; + + if(fnr != NULL) + { + mKnownPosted.clear(); + + for(auto it(fnr->records.begin());it!=fnr->records.end();++it) + if( now < it->second + GXS_POSTED_CONFIG_MAX_TIME_NOTIFY_STORAGE) + mKnownPosted.insert(*it) ; + } + + delete item ; + } + return true; +} + +RsSerialiser* p3PostBase::setupSerialiser() +{ + RsSerialiser* rss = new RsSerialiser; + rss->addSerialType(new GxsPostedConfigSerializer()); + + return rss; +} + diff --git a/libretroshare/src/services/p3postbase.h b/libretroshare/src/services/p3postbase.h index 8fc305b45..d90f94a8a 100644 --- a/libretroshare/src/services/p3postbase.h +++ b/libretroshare/src/services/p3postbase.h @@ -63,39 +63,44 @@ bool encodePostCache(std::string &str, const PostStats &s); bool extractPostCache(const std::string &str, PostStats &s); -class p3PostBase: public RsGenExchange, public GxsTokenQueue, public RsTickEvent +class p3PostBase: public RsGenExchange, public GxsTokenQueue, public RsTickEvent, public p3Config { - public: +public: p3PostBase(RsGeneralDataService *gds, RsNetworkExchangeService *nes, RsGixs* gixs, - RsSerialType* serviceSerialiser, uint16_t serviceType); + RsSerialType* serviceSerialiser, uint16_t serviceType); -virtual void service_tick(); + virtual void service_tick(); - protected: +protected: -virtual void notifyChanges(std::vector& changes); + virtual void notifyChanges(std::vector& changes); - // Overloaded from GxsTokenQueue for Request callbacks. -virtual void handleResponse(uint32_t token, uint32_t req_type); + // Overloaded from GxsTokenQueue for Request callbacks. + virtual void handleResponse(uint32_t token, uint32_t req_type); - // Overloaded from RsTickEvent. -virtual void handle_event(uint32_t event_type, const std::string &elabel); + // Overloaded from RsTickEvent. + virtual void handle_event(uint32_t event_type, const std::string &elabel); - public: + // overloads p3Config + virtual RsSerialiser* setupSerialiser() override; // @see p3Config::setupSerialiser() + virtual bool saveList(bool &cleanup, std::list&saveList) override; // @see p3Config::saveList(bool &cleanup, std::list&) + virtual bool loadList(std::list& loadList) override; // @see p3Config::loadList(std::list&) - ////////////////////////////////////////////////////////////////////////////// +public: -virtual void setMessageReadStatus(uint32_t& token, const RsGxsGrpMsgIdPair& msgId, bool read); + ////////////////////////////////////////////////////////////////////////////// + + virtual void setMessageReadStatus(uint32_t& token, const RsGxsGrpMsgIdPair& msgId, bool read); - protected: +protected: - p3GxsCommentService *mCommentService; + p3GxsCommentService *mCommentService; - private: +private: -static uint32_t postBaseAuthenPolicy(); + static uint32_t postBaseAuthenPolicy(); // Background processing. void background_tick(); @@ -116,13 +121,14 @@ static uint32_t postBaseAuthenPolicy(); bool background_cleanup(); - RsMutex mPostBaseMtx; + RsMutex mPostBaseMtx; bool mBgProcessing; bool mBgIncremental; - std::list mBgGroupList; - std::map mBgStatsMap; + std::list mBgGroupList; + std::map mBgStatsMap; + std::map mKnownPosted; }; #endif