Skip to content

Commit 66fc7af

Browse files
committed
Provide new and old topic in event
1 parent d7f0e00 commit 66fc7af

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

src/main/java/org/kitteh/irc/client/library/defaults/listener/DefaultTopicListener.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,25 @@
3232
import org.kitteh.irc.client.library.event.client.ClientReceiveNumericEvent;
3333
import org.kitteh.irc.client.library.feature.filter.CommandFilter;
3434
import org.kitteh.irc.client.library.feature.filter.NumericFilter;
35+
import org.kitteh.irc.client.library.util.CIKeyMap;
3536

37+
import java.util.Map;
3638
import java.util.Optional;
3739

3840
/**
3941
* Default TOPIC listener, producing events using default classes.
4042
*/
4143
public class DefaultTopicListener extends AbstractDefaultListenerBase {
44+
private final Map<String, Channel.Topic> oldTopics;
45+
4246
/**
4347
* Constructs the listener.
4448
*
4549
* @param client client
4650
*/
4751
public DefaultTopicListener(Client.@NonNull WithManagement client) {
4852
super(client);
53+
this.oldTopics = new CIKeyMap<>(client);
4954
}
5055

5156
@NumericFilter(332) // Topic
@@ -57,7 +62,9 @@ public void topic(ClientReceiveNumericEvent event) {
5762
}
5863
Optional<Channel> topicChannel = this.getTracker().getChannel(event.getParameters().get(1));
5964
if (topicChannel.isPresent()) {
60-
this.getTracker().setChannelTopic(topicChannel.get().getName(), event.getParameters().get(2));
65+
Channel oldChannel = topicChannel.get();
66+
this.oldTopics.put(oldChannel.getName(), oldChannel.getTopic());
67+
this.getTracker().setChannelTopic(oldChannel.getName(), event.getParameters().get(2));
6168
} else {
6269
this.trackException(event, "Topic message sent for invalid channel name");
6370
}
@@ -72,8 +79,9 @@ public void topicInfo(ClientReceiveNumericEvent event) {
7279
}
7380
Optional<Channel> topicSetChannel = this.getTracker().getChannel(event.getParameters().get(1));
7481
if (topicSetChannel.isPresent()) {
75-
this.getTracker().setChannelTopicInfo(topicSetChannel.get().getName(), Long.parseLong(event.getParameters().get(3)) * 1000, this.getTracker().getActor(event.getParameters().get(2)));
76-
this.fire(new ChannelTopicEvent(this.getClient(), event.getSource(), topicSetChannel.get(), false));
82+
Channel oldChannel = topicSetChannel.get();
83+
this.getTracker().setChannelTopicInfo(oldChannel.getName(), Long.parseLong(event.getParameters().get(3)) * 1000, this.getTracker().getActor(event.getParameters().get(2)));
84+
this.fire(new ChannelTopicEvent(this.getClient(), event.getSource(), oldChannel, this.oldTopics.remove(oldChannel.getName()), oldChannel.getLatest().get().getTopic(), false));
7785
} else {
7886
this.trackException(event, "Topic message sent for invalid channel name");
7987
}
@@ -88,9 +96,10 @@ public void topic(ClientReceiveCommandEvent event) {
8896
}
8997
Optional<Channel> channel = this.getTracker().getChannel(event.getParameters().get(0));
9098
if (channel.isPresent()) {
91-
this.getTracker().setChannelTopic(channel.get().getName(), event.getParameters().get(1));
92-
this.getTracker().setChannelTopicInfo(channel.get().getName(), System.currentTimeMillis(), event.getActor());
93-
this.fire(new ChannelTopicEvent(this.getClient(), event.getSource(), channel.get(), true));
99+
Channel oldChannel = channel.get();
100+
this.getTracker().setChannelTopic(oldChannel.getName(), event.getParameters().get(1));
101+
this.getTracker().setChannelTopicInfo(oldChannel.getName(), System.currentTimeMillis(), event.getActor());
102+
this.fire(new ChannelTopicEvent(this.getClient(), event.getSource(), oldChannel, oldChannel.getTopic(), oldChannel.getLatest().get().getTopic(), true));
94103
} else {
95104
this.trackException(event, "TOPIC message sent for invalid channel name");
96105
}

src/main/java/org/kitteh/irc/client/library/event/channel/ChannelTopicEvent.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,59 @@
3737
*/
3838
public class ChannelTopicEvent extends ChannelEventBase {
3939
private final boolean updated;
40+
private final Channel.Topic oldTopic;
41+
private final Channel.Topic newTopic;
4042

4143
/**
4244
* Creates the event.
4345
*
4446
* @param client client for which this is occurring
4547
* @param sourceMessage source message
4648
* @param channel channel the topic is about
49+
* @param oldTopic old topic
50+
* @param newTopic new topic
4751
* @param updated if this is a new change
48-
* @see Channel#getTopic()
4952
*/
50-
public ChannelTopicEvent(@NonNull Client client, @NonNull ServerMessage sourceMessage, @NonNull Channel channel, boolean updated) {
53+
public ChannelTopicEvent(@NonNull Client client, @NonNull ServerMessage sourceMessage, @NonNull Channel channel, Channel.@NonNull Topic oldTopic, Channel.@NonNull Topic newTopic, boolean updated) {
5154
super(client, sourceMessage, channel);
55+
this.oldTopic = oldTopic;
56+
this.newTopic = newTopic;
5257
this.updated = updated;
5358
}
5459

60+
/**
61+
* Creates the event.
62+
*
63+
* @param client client for which this is occurring
64+
* @param sourceMessage source message
65+
* @param channel channel the topic is about
66+
* @param updated if this is a new change
67+
* @deprecated Use new constructor
68+
*/
69+
@Deprecated
70+
public ChannelTopicEvent(@NonNull Client client, @NonNull ServerMessage sourceMessage, @NonNull Channel channel, boolean updated) {
71+
this(client, sourceMessage, channel, null, null, updated);
72+
}
73+
5574
/**
5675
* Gets the channel's topic.
5776
*
5877
* @return the channel topic
59-
* @see Channel#getTopic()
78+
* @deprecated Use {@link #getNewTopic()} and {@link #getOldTopic()}
6079
*/
80+
@Deprecated
6181
public Channel.@NonNull Topic getTopic() {
6282
return this.getChannel().getTopic();
6383
}
6484

85+
public Channel.@NonNull Topic getNewTopic() {
86+
return this.newTopic;
87+
}
88+
89+
public Channel.@NonNull Topic getOldTopic() {
90+
return this.oldTopic;
91+
}
92+
6593
/**
6694
* Gets if this is a new topic update, or just the server informing us of
6795
* a change from the past.

0 commit comments

Comments
 (0)