This repository was archived by the owner on Jul 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathMixerChatConnection.java
More file actions
166 lines (141 loc) · 6.47 KB
/
MixerChatConnection.java
File metadata and controls
166 lines (141 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.mixer.api.resource.chat.ws;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.mixer.api.MixerAPI;
import com.mixer.api.http.ws.MixerWebsocketClient;
import com.mixer.api.resource.chat.*;
import com.mixer.api.resource.chat.events.EventHandler;
import com.mixer.api.resource.chat.events.data.IncomingMessageData;
import com.mixer.api.resource.chat.replies.ReplyHandler;
import java.lang.reflect.ParameterizedType;
import java.util.Map;
import java.util.concurrent.Callable;
public class MixerChatConnection extends MixerWebsocketClient {
protected final MixerChatConnectable producer;
protected final MixerChat chat;
protected final Map<Integer, ReplyPair> replyHandlers;
protected final Multimap<Class<? extends AbstractChatEvent>, EventHandler> eventHandlers;
public MixerChatConnection(MixerChatConnectable producer, MixerAPI mixer, MixerChat chat) {
super(mixer, chat.endpoint());
this.producer = producer;
this.chat = chat;
this.replyHandlers = Maps.newConcurrentMap();
this.eventHandlers = HashMultimap.create();
}
public void inherit(MixerChatConnection other) {
for (Map.Entry<Class<? extends AbstractChatEvent>, EventHandler> entry : other.eventHandlers.entries()) {
this.eventHandlers.put(entry.getKey(), entry.getValue());
}
for (Map.Entry<Integer, ReplyPair> entry : other.replyHandlers.entrySet()) {
this.replyHandlers.put(entry.getKey(), entry.getValue());
}
}
public <T extends AbstractChatEvent> boolean on(Class<T> eventType, EventHandler<T> handler) {
return this.eventHandlers.put(eventType, handler);
}
/**
* Send sends a chat method with no given response handler.
* @param method The method to send.
*/
public void send(AbstractChatMethod method) {
this.send(method, null);
}
/**
* Send sends a chat method with a response handler that will be invoked when a response
* is received from the chat server.
*
* @param method The method to send.
* @param handler The reply handler.
* @param <T> The type on which to bind the method and reply handler together with.
*/
public <T extends AbstractChatReply> void send(final AbstractChatMethod method, ReplyHandler<T> handler) {
if (handler != null) {
this.replyHandlers.put(method.id, ReplyPair.from(handler));
}
this.mixer.executor.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
byte[] data = MixerChatConnection.this.mixer.gson.toJson(method).getBytes();
MixerChatConnection.this.send(data);
return null;
}
});
}
/**
* Delete deletes a message by making an API call.
*
* @param message The message to delete.
*/
@Deprecated
public void delete(IncomingMessageData message) {
String path = this.mixer.basePath.resolve("chats/" + message.channel + "/message/" + message.id).toString();
this.mixer.http.delete(path, null);
}
@Override
public void onMessage(String s) {
// XXX: terrible
ReplyPair replyPair = null;
try {
// Parse out the generic JsonObject so we can pull out the ID element from it,
// since we cannot yet parse as a generic class.
JsonObject e = new JsonParser().parse(s).getAsJsonObject();
if (e.has("id")) {
int id = e.get("id").getAsInt();
// Try and remove a reply-pair, execute the #onSuccess method if we find
// a matching reply-pair.
if ((replyPair = this.replyHandlers.remove(id)) != null) {
Class<? extends AbstractChatDatagram> type = replyPair.type;
// Now that we have the type, we can appropriately parse out the value
// And call the #onSuccess method with the value.
AbstractChatDatagram datagram = this.mixer.gson.fromJson(s, type);
replyPair.handler.onSuccess(type.cast(datagram));
}
} else if (e.has("event")) {
// Handles cases of mixer widgets (GiveawayBot) sending ChatMessage events
if(e.getAsJsonObject("data").has("user_id") && e.getAsJsonObject("data").get("user_id").getAsInt() == -1) {
Class<? extends AbstractChatEvent> type = AbstractChatEvent.EventType.fromSerializedName("WidgetMessage").getCorrespondingClass();
this.dispatchEvent(this.mixer.gson.fromJson(e, type));
} else {
// Default ChatMessage event handling
String eventName = e.get("event").getAsString();
Class<? extends AbstractChatEvent> type = AbstractChatEvent.EventType.fromSerializedName(eventName).getCorrespondingClass();
this.dispatchEvent(this.mixer.gson.fromJson(e, type));
}
}
} catch (JsonSyntaxException e) {
// If an exception was called and we do have a reply handler to catch it,
// call the #onFailure method with the throwable.
if (replyPair != null) {
replyPair.handler.onFailure(e);
} else {
throw e;
}
}
}
@Override public void onClose(int i, String s, boolean b) {
this.close(i);
this.producer.notifyClose(i, s, b);
}
private <T extends AbstractChatEvent> void dispatchEvent(T event) {
Class<? extends AbstractChatEvent> eventType = event.getClass();
for (EventHandler handler : this.eventHandlers.get(eventType)) {
handler.onEvent(event);
}
}
private static class ReplyPair<T extends AbstractChatReply> {
public ReplyHandler<T> handler;
public Class<T> type;
private static <T extends AbstractChatReply> ReplyPair<T> from(ReplyHandler<T> handler) {
ReplyPair<T> pair = new ReplyPair<>();
pair.handler = handler;
pair.type = (Class<T>) ((ParameterizedType) handler.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
return pair;
}
}
}