-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Post.java
274 lines (211 loc) · 7.74 KB
/
Post.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
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
package run.halo.app.core.extension.content;
import static java.lang.Boolean.parseBoolean;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.media.Schema.RequiredMode;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import run.halo.app.extension.AbstractExtension;
import run.halo.app.extension.GVK;
import run.halo.app.extension.GroupVersionKind;
import run.halo.app.extension.MetadataOperator;
import run.halo.app.infra.ConditionList;
/**
* <p>Post extension.</p>
*
* @author guqing
* @see <a href="https://github.com/halo-dev/halo/issues/2322">issue#2322</a>
* @since 2.0.0
*/
@Data
@ToString(callSuper = true)
@GVK(group = Constant.GROUP, version = Constant.VERSION, kind = Post.KIND,
plural = "posts", singular = "post")
@EqualsAndHashCode(callSuper = true)
public class Post extends AbstractExtension {
public static final String KIND = "Post";
public static final String REQUIRE_SYNC_ON_STARTUP_INDEX_NAME = "requireSyncOnStartup";
public static final GroupVersionKind GVK = GroupVersionKind.fromExtension(Post.class);
public static final String CATEGORIES_ANNO = "content.halo.run/categories";
public static final String LAST_RELEASED_SNAPSHOT_ANNO =
"content.halo.run/last-released-snapshot";
public static final String TAGS_ANNO = "content.halo.run/tags";
public static final String DELETED_LABEL = "content.halo.run/deleted";
public static final String PUBLISHED_LABEL = "content.halo.run/published";
public static final String OWNER_LABEL = "content.halo.run/owner";
public static final String VISIBLE_LABEL = "content.halo.run/visible";
public static final String ARCHIVE_YEAR_LABEL = "content.halo.run/archive-year";
public static final String ARCHIVE_MONTH_LABEL = "content.halo.run/archive-month";
public static final String ARCHIVE_DAY_LABEL = "content.halo.run/archive-day";
@Schema(requiredMode = RequiredMode.REQUIRED)
private PostSpec spec;
@Schema
private PostStatus status;
@JsonIgnore
public PostStatus getStatusOrDefault() {
if (this.status == null) {
this.status = new PostStatus();
}
return status;
}
@JsonIgnore
public boolean isDeleted() {
return Objects.equals(true, spec.getDeleted())
|| getMetadata().getDeletionTimestamp() != null;
}
@JsonIgnore
public boolean isPublished() {
return isPublished(this.getMetadata());
}
public static boolean isPublished(MetadataOperator metadata) {
var labels = metadata.getLabels();
return labels != null && parseBoolean(labels.getOrDefault(PUBLISHED_LABEL, "false"));
}
public static boolean isPublic(PostSpec spec) {
return spec.getVisible() == null || VisibleEnum.PUBLIC.equals(spec.getVisible());
}
@Data
public static class PostSpec {
@Schema(requiredMode = RequiredMode.REQUIRED, minLength = 1)
private String title;
@Schema(requiredMode = RequiredMode.REQUIRED, minLength = 1)
private String slug;
/**
* 文章引用到的已发布的内容,用于主题端显示.
*/
private String releaseSnapshot;
private String headSnapshot;
private String baseSnapshot;
private String owner;
private String template;
private String cover;
@Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "false")
private Boolean deleted;
@Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "false")
private Boolean publish;
private Instant publishTime;
@Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "false")
private Boolean pinned;
@Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "true")
private Boolean allowComment;
@Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "PUBLIC")
private VisibleEnum visible;
@Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "0")
private Integer priority;
@Schema(requiredMode = RequiredMode.REQUIRED)
private Excerpt excerpt;
private List<String> categories;
private List<String> tags;
private List<Map<String, String>> htmlMetas;
}
@Data
public static class PostStatus {
@Schema(requiredMode = RequiredMode.REQUIRED)
private String phase;
@Schema
private ConditionList conditions;
private String permalink;
private String excerpt;
private Boolean inProgress;
private Integer commentsCount;
private List<String> contributors;
private Instant lastModifyTime;
private long observedVersion;
@JsonIgnore
public ConditionList getConditionsOrDefault() {
if (this.conditions == null) {
this.conditions = new ConditionList();
}
return conditions;
}
}
@Data
public static class Excerpt {
@Schema(requiredMode = RequiredMode.REQUIRED, defaultValue = "true")
private Boolean autoGenerate;
private String raw;
}
public enum PostPhase {
DRAFT,
PENDING_APPROVAL,
PUBLISHED,
FAILED;
/**
* Convert string value to {@link PostPhase}.
*
* @param value enum value string
* @return {@link PostPhase} if found, otherwise null
*/
public static PostPhase from(String value) {
for (PostPhase phase : PostPhase.values()) {
if (phase.name().equalsIgnoreCase(value)) {
return phase;
}
}
return null;
}
}
public enum VisibleEnum {
PUBLIC,
INTERNAL,
PRIVATE;
/**
* Convert value string to {@link VisibleEnum}.
*
* @param value enum value string
* @return {@link VisibleEnum} if found, otherwise null
*/
public static VisibleEnum from(String value) {
for (VisibleEnum visible : VisibleEnum.values()) {
if (visible.name().equalsIgnoreCase(value)) {
return visible;
}
}
return null;
}
}
@Data
public static class CompactPost {
private String name;
private VisibleEnum visible;
private Boolean published;
public static Builder builder() {
return new Builder();
}
/**
* <p>Compact post builder.</p>
* <p>Can not replace with lombok builder.</p>
* <p>The class used by subclasses of {@link AbstractExtension} must have a no-args
* constructor.</p>
*/
public static class Builder {
private String name;
private VisibleEnum visible;
private Boolean published;
public Builder name(String name) {
this.name = name;
return this;
}
public Builder visible(VisibleEnum visible) {
this.visible = visible;
return this;
}
public Builder published(Boolean published) {
this.published = published;
return this;
}
public CompactPost build() {
CompactPost compactPost = new CompactPost();
compactPost.setName(name);
compactPost.setVisible(visible);
compactPost.setPublished(published);
return compactPost;
}
}
}
}