-
-
Notifications
You must be signed in to change notification settings - Fork 737
/
AuditableRestAction.java
152 lines (130 loc) · 5.04 KB
/
AuditableRestAction.java
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
/*
* Copyright 2015-2018 Austin Keener & Michael Ritter & Florian Spieß
*
* 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 net.dv8tion.jda.core.requests.restaction;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.core.requests.*;
import net.dv8tion.jda.core.utils.MiscUtil;
import okhttp3.RequestBody;
import org.apache.commons.collections4.map.CaseInsensitiveMap;
import org.json.JSONObject;
import javax.annotation.CheckReturnValue;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
/**
* Extension of RestAction to allow setting a reason, only available to accounts of {@link net.dv8tion.jda.core.AccountType#BOT AccountType.BOT}
*
* @param <T>
* The return type
*
* @since 3.3.0
*/
public abstract class AuditableRestAction<T> extends RestAction<T>
{
protected String reason = null;
public AuditableRestAction(JDA api, Route.CompiledRoute route)
{
super(api, route);
}
public AuditableRestAction(JDA api, Route.CompiledRoute route, RequestBody data)
{
super(api, route, data);
}
public AuditableRestAction(JDA api, Route.CompiledRoute route, JSONObject data)
{
super(api, route, data);
}
@Override
@SuppressWarnings("unchecked")
public AuditableRestAction<T> setCheck(BooleanSupplier checks)
{
return (AuditableRestAction) super.setCheck(checks);
}
/**
* Applies the specified reason as audit-log reason field.
* <br>When the provided reason is empty or {@code null} it will be treated as not set.
*
* <p>Reasons for any AuditableRestAction may be retrieved
* via {@link net.dv8tion.jda.core.audit.AuditLogEntry#getReason() AuditLogEntry.getReason()}
* in iterable {@link net.dv8tion.jda.core.requests.restaction.pagination.AuditLogPaginationAction AuditLogPaginationActions}
* from {@link net.dv8tion.jda.core.entities.Guild#getAuditLogs() Guild.getAuditLogs()}!
*
* <p>This will specify the reason via the {@code X-Audit-Log-Reason} Request Header.
* <br><b>Note: This may not be available to accounts for {@link net.dv8tion.jda.core.AccountType#CLIENT AccountType.CLIENT}.
* <br>Using methods with a reason parameter will always work and <u>override</u> this header.</b>
* (ct. {@link net.dv8tion.jda.core.managers.GuildController#ban(net.dv8tion.jda.core.entities.User, int, String) GuildController.ban(User, int, String)})
*
* @param reason
* The reason for this action which should be logged in the Guild's AuditLogs
*
* @return The current AuditableRestAction instance for chaining convenience
*/
@CheckReturnValue
public AuditableRestAction<T> reason(String reason)
{
this.reason = reason;
return this;
}
@Override
protected CaseInsensitiveMap<String, String> finalizeHeaders()
{
CaseInsensitiveMap<String, String> headers = super.finalizeHeaders();
if (reason == null || reason.isEmpty())
return headers;
if (headers == null)
headers = new CaseInsensitiveMap<>();
headers.put("X-Audit-Log-Reason", uriEncode(reason));
return headers;
}
private String uriEncode(String input)
{
String formEncode = MiscUtil.encodeUTF8(input);
return formEncode.replace('+', ' ');
}
/**
* Specialized form of {@link net.dv8tion.jda.core.requests.restaction.AuditableRestAction AuditableRestAction} that is used to provide information that
* has already been retrieved or generated so that another request does not need to be made to Discord.
* <br>Basically: Allows you to provide a value directly to the success returns.
*
* @param <T>
* The generic response type for this RestAction
*/
public static class EmptyRestAction<T> extends AuditableRestAction<T>
{
protected final T content;
public EmptyRestAction(JDA api)
{
this(api, null);
}
public EmptyRestAction(JDA api, T content)
{
super(api, null);
this.content = content;
}
@Override
public void queue(Consumer<? super T> success, Consumer<? super Throwable> failure)
{
if (success != null)
success.accept(content);
}
@Override
public RequestFuture<T> submit(boolean shouldQueue)
{
return new RestFuture<>(content);
}
@Override
protected void handleResponse(Response response, Request<T> request) { }
}
}