Reset the badge when another client connects
authorKyle Fuller <redacted>
Fri, 13 Sep 2013 17:11:38 +0000 (18:11 +0100)
committerKyle Fuller <redacted>
Fri, 13 Sep 2013 17:11:59 +0000 (18:11 +0100)
palaver.cpp

index 088d16f82c4ca20830de079fb18d19b6d1019acd..1269b01048ae8a4e868f7c2adf60712d153b9b68 100644 (file)
@@ -83,6 +83,7 @@ public:
        CDevice(const CString &sToken) {
                m_sToken = sToken;
                m_bInNegotiation = false;
+               m_uiBadge = 0;
        }
 
        CString GetVersion() const {
@@ -239,6 +240,8 @@ public:
                m_vIgnoreKeywords.clear();
                m_vIgnoreChannels.clear();
                m_vIgnoreNicks.clear();
+
+               m_uiBadge = 0;
        }
 
        void AddMentionKeyword(const CString& sKeyword) {
@@ -483,13 +486,16 @@ public:
 #pragma mark - Notifications
 
        void SendNotification(CModule& module, const CString& sSender, const CString& sNotification, const CChan *pChannel) {
+               ++m_uiBadge;
+
                MCString mcsHeaders;
 
                mcsHeaders["Authorization"] = CString("Bearer " + GetToken());
                mcsHeaders["Content-Type"] = "application/json";
 
                CString sJSON = "{";
-               sJSON += "\"message\": \"" + sNotification.Replace_n("\"", "\\\"") + "\"";
+               sJSON += "\"badge\": " + m_uiBadge;
+               sJSON += ",\"message\": \"" + sNotification.Replace_n("\"", "\\\"") + "\"";
                sJSON += ",\"sender\": \"" + sSender.Replace_n("\"", "\\\"") + "\"";
                if (pChannel) {
                        sJSON += ",\"channel\": \"" + pChannel->GetName().Replace_n("\"", "\\\"") + "\"";
@@ -500,6 +506,22 @@ public:
                module.AddSocket(pSocket);
        }
 
+       void ClearBadges(CModule& module) {
+               if (m_uiBadge != 0) {
+                       MCString mcsHeaders;
+
+                       mcsHeaders["Authorization"] = CString("Bearer " + GetToken());
+                       mcsHeaders["Content-Type"] = "application/json";
+
+                       CString sJSON = "{\"badge\": 0}";
+
+                       PLVHTTPSocket *pSocket = new PLVHTTPSocket(&module, "POST", GetPushEndpoint(), mcsHeaders, sJSON);
+                       module.AddSocket(pSocket);
+
+                       m_uiBadge = 0;
+               }
+       }
+
        std::map<CString, VCString> GetNetworks() const {
                return m_msvsNetworks;
        }
@@ -522,6 +544,7 @@ private:
        VCString m_vIgnoreNicks;
 
        bool m_bInNegotiation;
+       unsigned int m_uiBadge;
 };
 
 class CPalaverMod : public CModule {
@@ -630,6 +653,19 @@ public:
                                Save();
                        }
                }
+
+               if (m_pNetwork) {
+                       // Let's reset any other devices for this client
+
+                       for (std::vector<CDevice*>::const_iterator it = m_vDevices.begin();
+                                       it != m_vDevices.end(); ++it) {
+                               CDevice& device = **it;
+
+                               if (device.HasClient(*m_pClient) == false && device.HasNetwork(*m_pNetwork)) {
+                                       device.ClearBadges(*this);
+                               }
+                       }
+               }
        }
 
        virtual void OnClientDisconnect() {
@@ -904,4 +940,4 @@ template<> void TModInfo<CPalaverMod>(CModInfo& Info) {
        Info.SetWikiPage("palaver");
 }
 
-GLOBALMODULEDEFS(CPalaverMod, "Palaver support module")
\ No newline at end of file
+GLOBALMODULEDEFS(CPalaverMod, "Palaver support module")
git clone https://git.99rst.org/PROJECT