Line data Source code
1 : // Copyright (C) 2022 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.extensions.events; 16 : 17 : import com.google.common.flogger.FluentLogger; 18 : import com.google.gerrit.entities.AttentionSetUpdate; 19 : import com.google.gerrit.extensions.api.changes.NotifyHandling; 20 : import com.google.gerrit.extensions.common.AccountInfo; 21 : import com.google.gerrit.extensions.common.ChangeInfo; 22 : import com.google.gerrit.extensions.events.AttentionSetListener; 23 : import com.google.gerrit.server.account.AccountCache; 24 : import com.google.gerrit.server.account.AccountState; 25 : import com.google.gerrit.server.plugincontext.PluginSetContext; 26 : import com.google.gerrit.server.query.change.ChangeData; 27 : import com.google.inject.Inject; 28 : import com.google.inject.Singleton; 29 : import java.time.Instant; 30 : import java.util.HashSet; 31 : import java.util.Set; 32 : 33 : /** Helper class to fire an event when an attention set changes. */ 34 : @Singleton 35 : public class AttentionSetObserver { 36 146 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 37 : 38 : private final PluginSetContext<AttentionSetListener> listeners; 39 : private final EventUtil util; 40 : private final AccountCache accountCache; 41 : 42 146 : public static final AttentionSetObserver DISABLED = 43 146 : new AttentionSetObserver() { 44 : @Override 45 : public void fire( 46 : ChangeData changeData, 47 : AccountState accountState, 48 : AttentionSetUpdate update, 49 0 : Instant when) {} 50 : }; 51 : 52 : @Inject 53 : AttentionSetObserver( 54 146 : PluginSetContext<AttentionSetListener> listeners, EventUtil util, AccountCache accountCache) { 55 146 : this.listeners = listeners; 56 146 : this.util = util; 57 146 : this.accountCache = accountCache; 58 146 : } 59 : 60 : /** Constructor only for DISABLED version of the AttentionSetObserver. */ 61 146 : private AttentionSetObserver() { 62 146 : this.listeners = null; 63 146 : this.util = null; 64 146 : this.accountCache = null; 65 146 : } 66 : 67 : /** 68 : * Notify all listening plugins 69 : * 70 : * @param changeData is current data of the change 71 : * @param accountState is the initiator of the change 72 : * @param update is the update that caused the event 73 : * @param when is the time of the event 74 : */ 75 : public void fire( 76 : ChangeData changeData, AccountState accountState, AttentionSetUpdate update, Instant when) { 77 51 : if (listeners.isEmpty()) { 78 50 : return; 79 : } 80 2 : AccountState target = accountCache.get(update.account()).get(); 81 : 82 2 : HashSet<Integer> added = new HashSet<>(); 83 2 : HashSet<Integer> removed = new HashSet<>(); 84 2 : switch (update.operation()) { 85 : case ADD: 86 2 : added.add(target.account().id().get()); 87 2 : break; 88 : case REMOVE: 89 1 : removed.add(target.account().id().get()); 90 : break; 91 : } 92 : 93 : try { 94 2 : Event event = 95 : new Event( 96 2 : util.changeInfo(changeData), util.accountInfo(accountState), added, removed, when); 97 2 : listeners.runEach(l -> l.onAttentionSetChanged(event)); 98 0 : } catch (Exception e) { 99 0 : logger.atSevere().withCause(e).log("Exception while firing AttentionSet changed event"); 100 2 : } 101 2 : } 102 : 103 : /** Event to be fired when an attention set changes */ 104 : public static class Event extends AbstractChangeEvent implements AttentionSetListener.Event { 105 : private final Set<Integer> added; 106 : private final Set<Integer> removed; 107 : 108 : public Event( 109 : ChangeInfo change, 110 : AccountInfo editor, 111 : Set<Integer> added, 112 : Set<Integer> removed, 113 : Instant when) { 114 2 : super(change, editor, when, NotifyHandling.ALL); 115 2 : this.added = added; 116 2 : this.removed = removed; 117 2 : } 118 : 119 : @Override 120 : public Set<Integer> usersAdded() { 121 2 : return added; 122 : } 123 : 124 : @Override 125 : public Set<Integer> usersRemoved() { 126 2 : return removed; 127 : } 128 : } 129 : }