cloud-notifications: add support for delete vote event

Change-Id: I4a7b7dfabed27a47b536af5de99e74ff05323872
Signed-off-by: Jorge Ruesga <j.ruesga.criado@gmail.com>
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/ApiModule.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/ApiModule.java
index 7117d03..9947f29 100644
--- a/src/main/java/com/ruesga/gerrit/plugins/fcm/ApiModule.java
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/ApiModule.java
@@ -33,6 +33,7 @@
 import com.google.gerrit.extensions.events.ReviewerDeletedListener;
 import com.google.gerrit.extensions.events.RevisionCreatedListener;
 import com.google.gerrit.extensions.events.TopicEditedListener;
+import com.google.gerrit.extensions.events.VoteDeletedListener;
 import com.google.gerrit.extensions.registration.DynamicMap;
 import com.google.gerrit.extensions.registration.DynamicSet;
 import com.google.gerrit.extensions.restapi.RestApiModule;
@@ -51,6 +52,7 @@
 import com.ruesga.gerrit.plugins.fcm.handlers.ReviewerDeletedEventHandler;
 import com.ruesga.gerrit.plugins.fcm.handlers.RevisionCreatedEventHandler;
 import com.ruesga.gerrit.plugins.fcm.handlers.TopicEditedEventHandler;
+import com.ruesga.gerrit.plugins.fcm.handlers.VoteDeletedEventHandler;
 import com.ruesga.gerrit.plugins.fcm.server.DeleteToken;
 import com.ruesga.gerrit.plugins.fcm.server.Devices;
 import com.ruesga.gerrit.plugins.fcm.server.GetCloudNotificationsConfigInfo;
@@ -99,6 +101,8 @@
                 .to(RevisionCreatedEventHandler.class);
         DynamicSet.bind(binder(), TopicEditedListener.class)
                 .to(TopicEditedEventHandler.class);
+        DynamicSet.bind(binder(), VoteDeletedListener.class)
+                .to(VoteDeletedEventHandler.class);
 
         // Configure the Rest API
         DynamicMap.mapOf(binder(), DEVICE_KIND);
@@ -111,4 +115,5 @@
         post(DEVICE_KIND, TOKEN_ENTRY_POINT).to(PostToken.class);
         delete(TOKEN_KIND).to(DeleteToken.class);
     }
-}
\ No newline at end of file
+}
+
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/EventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/EventHandler.java
index db56c43..9c28cb2 100644
--- a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/EventHandler.java
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/EventHandler.java
@@ -278,4 +278,4 @@
         }
         return account.email;
     }
-}
\ No newline at end of file
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/VoteDeletedEventHandler.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/VoteDeletedEventHandler.java
new file mode 100644
index 0000000..a54b3c9
--- /dev/null
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/handlers/VoteDeletedEventHandler.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2016 Jorge Ruesga
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.ruesga.gerrit.plugins.fcm.handlers;
+
+import java.util.Collection;
+
+import com.google.gerrit.extensions.annotations.PluginName;
+import com.google.gerrit.extensions.common.ApprovalInfo;
+import com.google.gerrit.extensions.events.VoteDeletedListener;
+import com.google.gerrit.server.IdentifiedUser.GenericFactory;
+import com.google.gerrit.server.account.CapabilityControl;
+import com.google.gerrit.server.account.WatchConfig.NotifyType;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.query.account.InternalAccountQuery;
+import com.google.gerrit.server.query.change.ChangeQueryBuilder;
+import com.google.gerrit.server.query.change.ChangeQueryProcessor;
+import com.google.gson.annotations.SerializedName;
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.ruesga.gerrit.plugins.fcm.messaging.Notification;
+import com.ruesga.gerrit.plugins.fcm.rest.CloudNotificationEvents;
+import com.ruesga.gerrit.plugins.fcm.workers.FcmUploaderWorker;
+
+public class VoteDeletedEventHandler extends EventHandler
+        implements VoteDeletedListener {
+
+    private static class VoteDeletedEntryInfo {
+        @SerializedName("vote") public String vote;
+        @SerializedName("account") public String account;
+    }
+
+    private static class VoteDeletedInfo {
+        @SerializedName("votes") public VoteDeletedEntryInfo[] votes;
+    }
+
+    @Inject
+    public VoteDeletedEventHandler(
+            @PluginName String pluginName,
+            FcmUploaderWorker uploader,
+            AllProjectsName allProjectsName,
+            ChangeQueryBuilder cqb,
+            ChangeQueryProcessor cqp,
+            Provider<InternalAccountQuery> accountQueryProvider,
+            CapabilityControl.Factory capabilityControlFactory,
+            GenericFactory identifiedUserFactory) {
+        super(pluginName,
+                uploader,
+                allProjectsName,
+                cqb, cqp,
+                accountQueryProvider,
+                capabilityControlFactory,
+                identifiedUserFactory);
+    }
+
+    protected int getEventType() {
+        return CloudNotificationEvents.VOTE_DELETED_EVENT;
+    }
+
+    protected NotifyType getNotifyType() {
+        return NotifyType.ALL_COMMENTS;
+    }
+
+    @Override
+    public void onVoteDeleted(Event event) {
+        VoteDeletedInfo info = toVoteDeletedInfo(event);
+        final String msg;
+        if (info.votes.length == 1) {
+            msg = " remove " + info.votes[0].vote + " by " + info.votes[0].account;
+        } else {
+            msg = " remove multiple votes";
+        }
+        Notification notification = createNotification(event);
+        notification.extra = getSerializer().toJson(info);
+        notification.body = formatAccount(event.getWho()) + msg;
+
+        notify(notification, event);
+    }
+
+    private VoteDeletedInfo toVoteDeletedInfo(Event event) {
+        VoteDeletedInfo info = new VoteDeletedInfo();
+        Collection<ApprovalInfo> approvals = event.getRemoved().values();
+        int count = approvals.size();
+        info.votes = new VoteDeletedEntryInfo[count];
+        for (int i = 0; i < count; i++)
+        for (ApprovalInfo approval : event.getRemoved().values()) {
+            info.votes[i] = new VoteDeletedEntryInfo();
+            info.votes[i].vote = approval.tag + + approval.value;
+            info.votes[i].account = formatAccount(approval);
+        }
+        return info;
+    }
+}
diff --git a/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationEvents.java b/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationEvents.java
index 4487788..0aa40b3 100644
--- a/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationEvents.java
+++ b/src/main/java/com/ruesga/gerrit/plugins/fcm/rest/CloudNotificationEvents.java
@@ -28,4 +28,5 @@
     public static final int PATCHSET_CREATED_EVENT = 0x200;
     public static final int TOPIC_CHANGED_EVENT = 0x400;
     public static final int ASSIGNEE_CHANGED_EVENT = 0x800;
+    public static final int VOTE_DELETED_EVENT = 0x1000;
 }
diff --git a/src/main/resources/Documentation/api.md b/src/main/resources/Documentation/api.md
index eb838ce..d61d4c1 100644
--- a/src/main/resources/Documentation/api.md
+++ b/src/main/resources/Documentation/api.md
@@ -116,7 +116,7 @@
       "responseMode": "NOTIFICATION"
     }
 
-As a response, this method returs the registered *CloudNotificationInfo* entity (see below).
+As a response, this method returns the registered *CloudNotificationInfo* entity (see below).
 
 *Response*
 
@@ -212,6 +212,10 @@
 
 `TOPIC_CHANGED_EVENT = 0x200`
 
+`ASSIGNEE_CHANGED_EVENT = 0x800`
+
+`VOTE_DELETED_EVENT = 0x1000`
+
 ***
 
 **CloudNotificationResponseMode**