From f7199f1f1cf785bc968c912a80993fb53a870415 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 26 Apr 2020 23:35:37 +0200 Subject: [PATCH] moved GxsNotify classes to rsgxsnotify.h internal to gxs (not visible in API) since it is unused in GUI. Added event item in forum to handle moderator list changes --- libretroshare/src/gxs/rsgenexchange.cc | 15 ++-- libretroshare/src/gxs/rsgenexchange.h | 6 +- libretroshare/src/libretroshare.pro | 1 + libretroshare/src/retroshare/rsgxsforums.h | 6 ++ libretroshare/src/retroshare/rsgxsiface.h | 2 + .../src/retroshare/rsgxsifacehelper.h | 2 + libretroshare/src/retroshare/rsgxsservice.h | 80 ------------------- libretroshare/src/services/p3gxsforums.cc | 44 ++++++++++ libretroshare/src/services/p3posted.h | 2 + 9 files changed, 69 insertions(+), 89 deletions(-) diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index 6a95168d1..7ed1b498a 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -1110,6 +1110,7 @@ static void addMessageChanged(std::map > } } +#ifdef TO_REMOVE void RsGenExchange::receiveChanges(std::vector& changes) { std::cerr << "*********************************** RsGenExchange::receiveChanges()" << std::endl; @@ -1155,6 +1156,7 @@ void RsGenExchange::receiveChanges(std::vector& changes) if(rsEvents) rsEvents->postEvent(std::move(evt)); } +#endif bool RsGenExchange::subscribeToGroup(uint32_t& token, const RsGxsGroupId& grpId, bool subscribe) { @@ -3263,6 +3265,7 @@ void RsGenExchange::performUpdateValidation() continue; } RsGxsGrpMetaData *oldGrpMeta(mit->second->metaData); + RsNxsGrp *oldGrp(mit->second); if(updateValid(*oldGrpMeta, *gu.newGrp)) { @@ -3285,21 +3288,21 @@ void RsGenExchange::performUpdateValidation() RsGxsGroupUpdate *c = new RsGxsGroupUpdate(); - c->mGroupId = gu.newGrp->grpId ; - c->mNewGroup = gu.newGrp->clone(); // make a copy because mDataStore will destroy it on update - c->mOldGroup = mit->second; // do not make a copy since we own the memory + c->mNewGroupItem = dynamic_cast(mSerialiser->deserialise(gu.newGrp->grp.bin_data,&gu.newGrp->grp.bin_len)); + c->mNewGroupItem->meta = *gu.newGrp->metaData; // gu.newGrp will be deleted because mDataStore will destroy it on update - mit->second = nullptr; // prevents deletion in auto delete map + c->mOldGroupItem = dynamic_cast(mSerialiser->deserialise(oldGrp->grp.bin_data,&oldGrp->grp.bin_len)); + c->mOldGroupItem->meta = *oldGrpMeta; // no need to delete mit->second, as it will be deleted automatically in the temporary map mNotifications.push_back(c); - // finally, add the group to + // finally, add the group to the list to send to mDataStore grps.push_back(gu.newGrp); } else { - delete gu.newGrp; + delete gu.newGrp; // delete here because mDataStore will not take care of this one. no need to delete mit->second, as it will be deleted automatically in the temporary map gu.newGrp = NULL ; } } diff --git a/libretroshare/src/gxs/rsgenexchange.h b/libretroshare/src/gxs/rsgenexchange.h index 840dc97b3..4a05e61c4 100644 --- a/libretroshare/src/gxs/rsgenexchange.h +++ b/libretroshare/src/gxs/rsgenexchange.h @@ -33,6 +33,7 @@ #include "rsnxsobserver.h" #include "retroshare/rsgxsservice.h" #include "rsitems/rsnxsitems.h" +#include "gxs/rsgxsnotify.h" #include "rsgxsutil.h" template @@ -262,12 +263,14 @@ public: */ bool getPublishedMsgMeta(const uint32_t& token,RsMsgMetaData& meta); +#ifdef TO_REMOVE /*! * Gxs services should call this for automatic handling of * changes, send * @param changes */ virtual void receiveChanges(std::vector& changes); +#endif /*! * \brief acceptNewGroup @@ -748,9 +751,6 @@ protected: */ virtual void notifyChanges(std::vector& changes) = 0; - - - private: void processRecvdData(); diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 54a4989ec..c7b38ce0b 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -672,6 +672,7 @@ HEADERS += rsitems/rsnxsitems.h \ util/rsdbbind.h \ util/contentvalue.h \ gxs/rsgxsutil.h \ + gxs/rsgxsnotify.h \ gxs/gxssecurity.h \ gxs/rsgds.h \ gxs/rsgxs.h \ diff --git a/libretroshare/src/retroshare/rsgxsforums.h b/libretroshare/src/retroshare/rsgxsforums.h index f7c8153ba..c033bf3ac 100644 --- a/libretroshare/src/retroshare/rsgxsforums.h +++ b/libretroshare/src/retroshare/rsgxsforums.h @@ -112,6 +112,7 @@ enum class RsForumEventCode: uint8_t SUBSCRIBE_STATUS_CHANGED = 0x05, /// forum was subscribed or unsubscribed READ_STATUS_CHANGED = 0x06, /// msg was read or marked unread STATISTICS_CHANGED = 0x07, /// suppliers and how many messages they have changed + MODERATOR_LIST_CHANGED = 0x08, /// forum moderation list has changed. }; struct RsGxsForumEvent: RsEvent @@ -123,6 +124,8 @@ struct RsGxsForumEvent: RsEvent RsForumEventCode mForumEventCode; RsGxsGroupId mForumGroupId; RsGxsMessageId mForumMsgId; + std::list mModeratorsAdded; + std::list mModeratorsRemoved; ///* @see RsEvent @see RsSerializable void serial_process( @@ -133,6 +136,9 @@ struct RsGxsForumEvent: RsEvent RS_SERIAL_PROCESS(mForumEventCode); RS_SERIAL_PROCESS(mForumGroupId); RS_SERIAL_PROCESS(mForumMsgId); + RS_SERIAL_PROCESS(mForumMsgId); + RS_SERIAL_PROCESS(mModeratorsAdded); + RS_SERIAL_PROCESS(mModeratorsRemoved); } ~RsGxsForumEvent() override; diff --git a/libretroshare/src/retroshare/rsgxsiface.h b/libretroshare/src/retroshare/rsgxsiface.h index 7bb6bba28..36bee517e 100644 --- a/libretroshare/src/retroshare/rsgxsiface.h +++ b/libretroshare/src/retroshare/rsgxsiface.h @@ -116,12 +116,14 @@ struct RsGxsIface */ virtual uint16_t serviceType() const =0; +#ifdef TO_REMOVE /*! * Gxs services should call this for automatic handling of * changes, send * @param changes */ virtual void receiveChanges(std::vector& changes) = 0; +#endif /*! * @return handle to token service for this GXS service diff --git a/libretroshare/src/retroshare/rsgxsifacehelper.h b/libretroshare/src/retroshare/rsgxsifacehelper.h index 0addbf6e5..d2f588623 100644 --- a/libretroshare/src/retroshare/rsgxsifacehelper.h +++ b/libretroshare/src/retroshare/rsgxsifacehelper.h @@ -72,6 +72,7 @@ public: ~RsGxsIfaceHelper() = default; +#ifdef TO_REMOVE /*! * Gxs services should call this for automatic handling of * changes, send @@ -81,6 +82,7 @@ public: { mGxs.receiveChanges(changes); } +#endif /* Generic Lists */ diff --git a/libretroshare/src/retroshare/rsgxsservice.h b/libretroshare/src/retroshare/rsgxsservice.h index 0549eacbd..152b018c9 100644 --- a/libretroshare/src/retroshare/rsgxsservice.h +++ b/libretroshare/src/retroshare/rsgxsservice.h @@ -32,86 +32,6 @@ typedef uint32_t TurtleRequestId; typedef std::map > GxsMsgMetaMap; typedef std::map > GxsMsgRelatedMetaMap; -class RsNxsGrp; - -/*! - * The aim of this class is to abstract how changes are represented so they can - * be determined outside the client API without explcitly enumerating all - * possible changes at the interface - */ -struct RsGxsNotify -{ - enum NotifyType - { - TYPE_UNKNOWN = 0x00, - TYPE_PUBLISHED = 0x01, - TYPE_RECEIVED_NEW = 0x02, - TYPE_PROCESSED = 0x03, - TYPE_RECEIVED_PUBLISHKEY = 0x04, - TYPE_RECEIVED_DISTANT_SEARCH_RESULTS = 0x05, - TYPE_STATISTICS_CHANGED = 0x06, - TYPE_UPDATED = 0x07, - }; - - virtual ~RsGxsNotify() {} - virtual NotifyType getType() = 0; -}; - -/*! - * Relevant to group changes - */ -class RsGxsGroupChange : public RsGxsNotify -{ -public: - RsGxsGroupChange(NotifyType type, bool metaChange) : mNotifyType(type), mMetaChange(metaChange) {} - std::list mGrpIdList; - NotifyType getType() override { return mNotifyType;} - bool metaChange() { return mMetaChange; } - -protected: - NotifyType mNotifyType; - bool mMetaChange; -}; - -class RsGxsGroupUpdate : public RsGxsNotify -{ -public: - RsGxsGroupUpdate() : mOldGroup(nullptr),mNewGroup(nullptr) {} - - RsGxsGroupId mGroupId; - RsNxsGrp *mOldGroup; - RsNxsGrp *mNewGroup; - - NotifyType getType() override { return RsGxsNotify::TYPE_UPDATED;} -}; - - -class RsGxsDistantSearchResultChange: public RsGxsNotify -{ -public: - RsGxsDistantSearchResultChange(TurtleRequestId id,const RsGxsGroupId& group_id) : mRequestId(id),mGroupId(group_id){} - - NotifyType getType() { return TYPE_RECEIVED_DISTANT_SEARCH_RESULTS ; } - - TurtleRequestId mRequestId ; - RsGxsGroupId mGroupId; -}; - -/*! - * Relevant to message changes - */ -class RsGxsMsgChange : public RsGxsNotify -{ -public: - RsGxsMsgChange(NotifyType type, bool metaChange) : NOTIFY_TYPE(type), mMetaChange(metaChange) {} - std::map > msgChangeMap; - NotifyType getType(){ return NOTIFY_TYPE;} - bool metaChange() { return mMetaChange; } -private: - const NotifyType NOTIFY_TYPE; - bool mMetaChange; -}; - #endif // RSGXSSERVICE_H diff --git a/libretroshare/src/services/p3gxsforums.cc b/libretroshare/src/services/p3gxsforums.cc index 42b60fad2..6b8f91f9f 100644 --- a/libretroshare/src/services/p3gxsforums.cc +++ b/libretroshare/src/services/p3gxsforums.cc @@ -189,6 +189,7 @@ void p3GxsForums::notifyChanges(std::vector &changes) for(it = changes.begin(); it != changes.end(); ++it) { RsGxsMsgChange *msgChange = dynamic_cast(*it); + if (msgChange) { if (msgChange->getType() == RsGxsNotify::TYPE_RECEIVED_NEW || msgChange->getType() == RsGxsNotify::TYPE_PUBLISHED) /* message received */ @@ -328,6 +329,49 @@ void p3GxsForums::notifyChanges(std::vector &changes) #endif } } + + RsGxsGroupUpdate *grpUpdate = dynamic_cast(*it); + + if (grpUpdate && rsEvents) + { + // Happens when the group data has changed. In this case we need to analyse the old and new group in order to detect possible notifications for clients + + RsGxsForumGroupItem *old_forum_grp_item = dynamic_cast(grpUpdate->mOldGroupItem); + RsGxsForumGroupItem *new_forum_grp_item = dynamic_cast(grpUpdate->mNewGroupItem); + + if(old_forum_grp_item == nullptr || new_forum_grp_item == nullptr) + { + RsErr() << __PRETTY_FUNCTION__ << " received GxsGroupUpdate item with mOldGroup and mNewGroup not of type RsGxsForumGroupItem. This is inconsistent!" << std::endl; + delete grpUpdate; + continue; + } + + // First of all, we check if there is a difference between the old and new list of moderators + + std::list added_mods, removed_mods; + + for(auto& gxs_id: new_forum_grp_item->mGroup.mAdminList.ids) + if(old_forum_grp_item->mGroup.mAdminList.ids.find(gxs_id) == old_forum_grp_item->mGroup.mAdminList.ids.end()) + added_mods.push_back(gxs_id); + + for(auto& gxs_id: old_forum_grp_item->mGroup.mAdminList.ids) + if(new_forum_grp_item->mGroup.mAdminList.ids.find(gxs_id) == new_forum_grp_item->mGroup.mAdminList.ids.end()) + removed_mods.push_back(gxs_id); + + if(!added_mods.empty() || !removed_mods.empty()) + { + auto ev = std::make_shared(); + + ev->mForumGroupId = new_forum_grp_item->meta.mGroupId; + ev->mModeratorsAdded = added_mods; + ev->mModeratorsRemoved = removed_mods; + ev->mForumEventCode = RsForumEventCode::MODERATOR_LIST_CHANGED; + + rsEvents->postEvent(ev); + } + + } + } } diff --git a/libretroshare/src/services/p3posted.h b/libretroshare/src/services/p3posted.h index b6f3ccee5..a6dc4be73 100644 --- a/libretroshare/src/services/p3posted.h +++ b/libretroshare/src/services/p3posted.h @@ -53,10 +53,12 @@ virtual void notifyChanges(std::vector& changes) public: +#ifdef TO_REMOVE virtual void receiveHelperChanges(std::vector& changes) { return RsGxsIfaceHelper::receiveChanges(changes); } +#endif bool getBoardsInfo(const std::list& boardsIds, std::vector& groupsInfo ) override;