forked from intercom/intercom-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubscription.java
More file actions
419 lines (322 loc) · 13.2 KB
/
Copy pathSubscription.java
File metadata and controls
419 lines (322 loc) · 13.2 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package io.intercom.api;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings("UnusedDeclaration")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Subscription extends TypedData {
private static final HashMap<String, String> SENTINEL = Maps.newHashMap();
public static Subscription create(Subscription subscription) throws InvalidException, AuthorizationException {
return DataResource.create(subscription, "subscriptions", Subscription.class);
}
public static Subscription update(Subscription subscription) throws InvalidException, AuthorizationException {
return DataResource.update(subscription, "subscriptions", subscription.getId(), Subscription.class);
}
public static Subscription delete(Subscription subscription) throws InvalidException, AuthorizationException {
return DataResource.delete(subscription.getId(), "subscriptions", Subscription.class);
}
public static Subscription find(String id) throws InvalidException, AuthorizationException {
final HttpClient resource = new HttpClient(UriBuilder.newBuilder().path("subscriptions").path(id).build());
return resource.get(Subscription.class);
}
public static SubscriptionCollection list() throws InvalidException, AuthorizationException {
return DataResource.list(SENTINEL, "subscriptions", SubscriptionCollection.class);
}
public static NotificationCollection sentFeed(String id) throws InvalidException, AuthorizationException {
final URI feedURI = UriBuilder.newBuilder()
.path("subscriptions")
.path(id)
.path("sent")
.build();
final HttpClient resource = new HttpClient(feedURI);
return resource.get(NotificationCollection.class);
}
public static NotificationErrorCollection errorFeed(String id) throws InvalidException, AuthorizationException {
final URI feedURI = UriBuilder.newBuilder()
.path("subscriptions")
.path(id)
.path("error")
.build();
final HttpClient resource = new HttpClient(feedURI);
return resource.get(NotificationErrorCollection.class);
}
@SuppressWarnings("UnusedDeclaration")
public static class Topic {
private static final String SUBTYPE_WILDCARD = "*";
private static final String TEXT = "text";
private final static String COMPANY_NAME = "company";
private final static String CONVERSATION_NAME = "conversation";
private static final String USER_NAME = "user";
public static final Topic PING = new Topic("ping", SUBTYPE_WILDCARD);
public static final Topic COMPANY = new Topic(COMPANY_NAME, SUBTYPE_WILDCARD);
public static final Topic COMPANY_CREATED = new Topic(COMPANY_NAME, "created");
public static final Topic CONVERSATION = new Topic(CONVERSATION_NAME, SUBTYPE_WILDCARD);
public static final Topic CONVERSATION_USER_CREATED = new Topic(CONVERSATION_NAME, "user.created");
public static final Topic CONVERSATION_USER_REPLY = new Topic(CONVERSATION_NAME, "user.replied");
public static final Topic CONVERSATION_ADMIN_REPLY = new Topic(CONVERSATION_NAME, "admin.replied");
public static final Topic CONVERSATION_ADMIN_ASSIGNED = new Topic(CONVERSATION_NAME, "admin.assigned");
public static final Topic CONVERSATION_ADMIN_CLOSED = new Topic(CONVERSATION_NAME, "admin.closed");
public static final Topic CONVERSATION_ADMIN_OPEN = new Topic(CONVERSATION_NAME, "admin.open");
public static final Topic CONVERSATION_ADMIN_NOTED = new Topic(CONVERSATION_NAME, "admin.noted");
public static final Topic USER = new Topic(USER_NAME, SUBTYPE_WILDCARD);
public static final Topic USER_CREATED = new Topic(USER_NAME, "created");
public static final Topic USER_UNSUBSCRIBED = new Topic(USER_NAME, "unsubscribed");
public static final Topic USER_TAG_CREATED = new Topic(USER_NAME, "tag.created");
public static final Topic USER_TAG_DELETED = new Topic(USER_NAME, "tag.deleted");
public static final Topic ALL_TOPIC = valueOf("all");
public static Topic valueOf(String type) throws IllegalArgumentException {
if (type.contains(".")) {
final String[] split = type.split("\\.", 2); // fragile: assume one dot
return new Topic(split[0], split[1]);
} else {
return new Topic(type);
}
}
private String type;
private String subType;
private String toString;
public Topic(String type) {
this(type, SUBTYPE_WILDCARD);
}
public Topic(String type, String subType) {
Preconditions.checkArgument(type != null, "type part cannot be null");
Preconditions.checkArgument(subType != null, "subType part cannot be null");
this.type = type;
this.subType = subType;
// memo the string representation
if (SUBTYPE_WILDCARD.equals(subType)) {
toString = type;
} else {
toString = type + "." + subType;
}
}
public String getType() {
return type;
}
public String getSubType() {
return subType;
}
public boolean isCompatible(String type) {
return this.isCompatible(valueOf(type));
}
public boolean isCompatible(Topic other) {
if (other == null) {
return false;
}
//noinspection SimplifiableIfStatement
if (type.equalsIgnoreCase(other.type) && (subType.equals(SUBTYPE_WILDCARD) || other.subType.equals(SUBTYPE_WILDCARD))) {
return true;
} else {
return this.type.equalsIgnoreCase(other.type) && this.subType.equalsIgnoreCase(other.subType);
}
}
@Override
public int hashCode() {
int result = type != null ? type.hashCode() : 0;
result = 31 * result + (subType != null ? subType.hashCode() : 0);
return result;
}
@Override
public String toString() {
return toString;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Topic Topic = (Topic) o;
if (subType != null ? !subType.equals(Topic.subType) : Topic.subType != null) {
return false;
}
//noinspection RedundantIfStatement
if (type != null ? !type.equals(Topic.type) : Topic.type != null) {
return false;
}
return true;
}
}
@SuppressWarnings("FieldCanBeLocal")
@JsonProperty("type")
private final String type = "notification_subscription";
@JsonProperty("id")
private String id;
@JsonProperty("created_at")
private long createdAt;
@JsonProperty("updated_at")
private long updatedAt;
@JsonProperty("service_type")
private final String serviceType = "web";
@JsonProperty("app_id")
private String appID;
@JsonProperty("url")
private URI url;
@JsonProperty("self")
private URI self;
@JsonProperty("topics")
private List<Topic> topics = Lists.newArrayList();
@JsonProperty("metadata")
private Map<String, String> metadata = Maps.newHashMap();
@JsonProperty("active")
private boolean active;
@JsonProperty("hub_secret")
private String hubSecret;
@JsonProperty("mode")
private String mode = "point";
@JsonProperty("links")
private Map<String, URI> links = Maps.newHashMap();
@JsonProperty("notes")
private List<String> notes = Lists.newArrayList();
public Subscription() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public long getCreatedAt() {
return createdAt;
}
public long getUpdatedAt() {
return updatedAt;
}
public String getServiceType() {
return serviceType;
}
public String getAppID() {
return appID;
}
public void setAppID(String appID) {
this.appID = appID;
}
public URI getUrl() {
return url;
}
public void setUrl(URI url) {
this.url = url;
}
public URI getSelf() {
return self;
}
public List<Topic> getTopics() {
return topics;
}
public void setTopics(List<Topic> topics) {
this.topics = topics;
}
public void addTopic(Topic topic) {
this.topics.add(topic);
}
public boolean getActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
public Map<String, String> getMetadata() {
return metadata;
}
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}
public String getHubSecret() {
return hubSecret;
}
public void setHubSecret(String hubSecret) {
this.hubSecret = hubSecret;
}
public String getMode() {
return mode;
}
public List<String> getNotes() {
return notes;
}
public void setNotes(List<String> notes) {
this.notes = notes;
}
@JsonIgnore
public String getURLString() {
return url.toASCIIString();
}
@JsonIgnore
public boolean requiresSignature() {
return getHubSecret() != null;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Subscription that = (Subscription) o;
if (active != that.active) return false;
if (createdAt != that.createdAt) return false;
if (updatedAt != that.updatedAt) return false;
if (appID != null ? !appID.equals(that.appID) : that.appID != null) return false;
if (hubSecret != null ? !hubSecret.equals(that.hubSecret) : that.hubSecret != null) return false;
if (id != null ? !id.equals(that.id) : that.id != null) return false;
if (links != null ? !links.equals(that.links) : that.links != null) return false;
if (metadata != null ? !metadata.equals(that.metadata) : that.metadata != null) return false;
if (mode != null ? !mode.equals(that.mode) : that.mode != null) return false;
if (notes != null ? !notes.equals(that.notes) : that.notes != null) return false;
if (self != null ? !self.equals(that.self) : that.self != null) return false;
if (!serviceType.equals(that.serviceType)) return false;
if (topics != null ? !topics.equals(that.topics) : that.topics != null) return false;
if (!type.equals(that.type)) return false;
//noinspection RedundantIfStatement
if (url != null ? !url.equals(that.url) : that.url != null) return false;
return true;
}
@Override
public int hashCode() {
int result = type.hashCode();
result = 31 * result + (id != null ? id.hashCode() : 0);
result = 31 * result + (int) (createdAt ^ (createdAt >>> 32));
result = 31 * result + (int) (updatedAt ^ (updatedAt >>> 32));
result = 31 * result + (serviceType.hashCode());
result = 31 * result + (appID != null ? appID.hashCode() : 0);
result = 31 * result + (url != null ? url.hashCode() : 0);
result = 31 * result + (self != null ? self.hashCode() : 0);
result = 31 * result + (topics != null ? topics.hashCode() : 0);
result = 31 * result + (metadata != null ? metadata.hashCode() : 0);
result = 31 * result + (active ? 1 : 0);
result = 31 * result + (hubSecret != null ? hubSecret.hashCode() : 0);
result = 31 * result + (mode != null ? mode.hashCode() : 0);
result = 31 * result + (links != null ? links.hashCode() : 0);
result = 31 * result + (notes != null ? notes.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Subscription{" +
"id='" + id + '\'' +
", createdAt=" + createdAt +
", updatedAt=" + updatedAt +
", serviceType='" + serviceType + '\'' +
", appID='" + appID + '\'' +
", url=" + url +
", self=" + self +
", topics=" + topics +
", metadata=" + metadata +
", active=" + active +
", hubSecret='" + hubSecret + '\'' +
", mode='" + mode + '\'' +
", links=" + links +
", notes=" + notes +
"} " + super.toString();
}
}