/
ForumPostActionImpl.java
171 lines (151 loc) · 5.76 KB
/
ForumPostActionImpl.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* 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.internal.requests.restaction;
import gnu.trove.set.TLongSet;
import gnu.trove.set.hash.TLongHashSet;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.entities.channel.forums.ForumPost;
import net.dv8tion.jda.api.entities.channel.forums.ForumTagSnowflake;
import net.dv8tion.jda.api.requests.Request;
import net.dv8tion.jda.api.requests.Response;
import net.dv8tion.jda.api.requests.Route;
import net.dv8tion.jda.api.requests.restaction.ForumPostAction;
import net.dv8tion.jda.api.utils.data.DataObject;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import net.dv8tion.jda.internal.entities.EntityBuilder;
import net.dv8tion.jda.internal.requests.RestActionImpl;
import net.dv8tion.jda.internal.utils.Checks;
import net.dv8tion.jda.internal.utils.message.MessageCreateBuilderMixin;
import okhttp3.RequestBody;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.function.BooleanSupplier;
public class ForumPostActionImpl extends RestActionImpl<ForumPost> implements ForumPostAction, MessageCreateBuilderMixin<ForumPostAction>
{
private final MessageCreateBuilder builder;
private final ForumChannel channel;
private final TLongSet appliedTags = new TLongHashSet();
private String name;
private ThreadChannel.AutoArchiveDuration autoArchiveDuration;
public ForumPostActionImpl(ForumChannel channel, String name, MessageCreateBuilder builder)
{
super(channel.getJDA(), Route.Channels.CREATE_THREAD.compile(channel.getId()));
this.builder = builder;
this.channel = channel;
setName(name);
}
@Nonnull
@Override
public ForumPostAction setCheck(BooleanSupplier checks)
{
return (ForumPostAction) super.setCheck(checks);
}
@Nonnull
@Override
public ForumPostAction addCheck(@Nonnull BooleanSupplier checks)
{
return (ForumPostAction) super.addCheck(checks);
}
@Nonnull
@Override
public ForumPostAction deadline(long timestamp)
{
return (ForumPostAction) super.deadline(timestamp);
}
@Nonnull
@Override
public Guild getGuild()
{
return channel.getGuild();
}
@Nonnull
@Override
public ForumChannel getChannel()
{
return channel;
}
@Nonnull
@Override
public ForumPostAction setTags(@Nonnull Collection<? extends ForumTagSnowflake> tags)
{
Checks.noneNull(tags, "Tags");
Checks.check(tags.size() <= ForumChannel.MAX_POST_TAGS, "Provided more than %d tags.", ForumChannel.MAX_POST_TAGS);
Checks.check(!channel.isTagRequired() || !tags.isEmpty(), "This forum requires at least one tag per post! See ForumChannel#isRequireTag()");
this.appliedTags.clear();
tags.forEach(t -> this.appliedTags.add(t.getIdLong()));
return this;
}
@Nonnull
@Override
public ChannelType getType()
{
return ChannelType.GUILD_PUBLIC_THREAD;
}
@Nonnull
@Override
public ForumPostAction setName(@Nonnull String name)
{
Checks.notEmpty(name, "Name");
Checks.notLonger(name, Channel.MAX_NAME_LENGTH, "Name");
this.name = name.trim();
return this;
}
@Nonnull
@Override
public ForumPostAction setAutoArchiveDuration(@Nonnull ThreadChannel.AutoArchiveDuration autoArchiveDuration)
{
Checks.notNull(autoArchiveDuration, "AutoArchiveDuration");
this.autoArchiveDuration = autoArchiveDuration;
return this;
}
@Override
public MessageCreateBuilder getBuilder()
{
return builder;
}
@Override
protected RequestBody finalizeData()
{
try (MessageCreateData message = builder.build())
{
DataObject json = DataObject.empty();
json.put("message", message);
json.put("name", name);
if (autoArchiveDuration != null)
json.put("auto_archive_duration", autoArchiveDuration.getMinutes());
if (!appliedTags.isEmpty())
json.put("applied_tags", appliedTags.toArray());
else if (getChannel().isTagRequired())
throw new IllegalStateException("Cannot create posts without a tag in this forum. Apply at least one tag!");
return getMultipartBody(message.getFiles(), json);
}
}
@Override
protected void handleSuccess(Response response, Request<ForumPost> request)
{
DataObject json = response.getObject();
EntityBuilder entityBuilder = api.getEntityBuilder();
ThreadChannel thread = entityBuilder.createThreadChannel(json, getGuild().getIdLong());
Message message = entityBuilder.createMessageWithChannel(json.getObject("message"), thread, false);
request.onSuccess(new ForumPost(message, thread));
}
}