Skip to content

Commit 6bb4c18

Browse files
committed
Rewrite: Use versions and branches to control the public and private status of the article. Happy Lantern Festiva🎉🎉🎉. see ##1
1 parent 7912020 commit 6bb4c18

16 files changed

+60
-484
lines changed

ent/schema/article.go

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func (Article) Fields() []ent.Field {
2828
func (Article) Edges() []ent.Edge {
2929
return []ent.Edge{
3030
edge.To("versions", Version.Type),
31+
edge.To("branches", Draft.Type),
3132
edge.To("reactions", Reaction.Type),
3233
edge.To("quote", Quote.Type).Unique().StorageKey(edge.Column("response_id")),
3334
edge.To("assets", Asset.Type),

ent/schema/content.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,32 @@ func (Content) Fields() []ent.Field {
1919
field.String("title").Optional(),
2020
field.String("gist").Optional(),
2121
field.String("body").Default(""),
22-
field.String("versionName").Optional().Unique(),
22+
field.String("seo").Optional(),
23+
field.Enum("state").
24+
Values("draft", "review", "release").
25+
Default("draft").
26+
Comment("release: all users can access.").
27+
Comment("review: only voters can access.").
28+
Comment("draft: only the editor can access."),
2329
field.Time("created_at").Default(time.Now),
2430
}
2531
}
2632

2733
// Edges of the Content.
2834
func (Content) Edges() []ent.Edge {
2935
return []ent.Edge{
30-
edge.From("version", Version.Type).Ref("history").Unique().Required(),
36+
edge.To("lang", Language.Type).Unique().Required(),
37+
edge.To("last", Content.Type).Unique().Required(),
38+
edge.To("tags", Tag.Type),
3139
edge.To("sections", Section.Type),
32-
edge.To("lang", Language.Type),
40+
edge.From("branche", Draft.Type).
41+
Ref("snapshots").
42+
Unique().
43+
Required().
44+
Comment("Snapshot of a branch."),
45+
edge.From("version", Version.Type).
46+
Ref("content").
47+
Unique().
48+
Comment("The content of a published version. If the article is public, a vote is required. If the article is private, it is created directly."),
3349
}
3450
}

ent/schema/draft.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package schema
2+
3+
import (
4+
"github.com/facebook/ent"
5+
"github.com/facebook/ent/schema/edge"
6+
"github.com/facebook/ent/schema/field"
7+
)
8+
9+
// Draft holds the schema definition for the Draft entity.
10+
type Draft struct {
11+
ent.Schema
12+
}
13+
14+
// Fields of the Draft.
15+
func (Draft) Fields() []ent.Field {
16+
return []ent.Field{
17+
field.Enum("status").Values("read", "write").Default("write"),
18+
}
19+
}
20+
21+
// Edges of the Draft.
22+
func (Draft) Edges() []ent.Edge {
23+
return []ent.Edge{
24+
edge.To("snapshots", Content.Type),
25+
edge.From("user", User.Type).Ref("drafts").Unique().Required(),
26+
edge.From("article", Article.Type).Ref("branches").Unique().Required(),
27+
}
28+
}

ent/schema/user.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (User) Edges() []ent.Edge {
2626
return []ent.Edge{
2727
edge.To("assets", Asset.Type).StorageKey(edge.Column("owner_id")),
2828
edge.To("archives", Archive.Type).StorageKey(edge.Column("owner_id")),
29-
edge.To("drafts", Version.Type).StorageKey(edge.Column("editor_id")),
29+
edge.To("drafts", Draft.Type).StorageKey(edge.Column("editor_id")),
3030
edge.To("tags", Tag.Type),
3131
edge.To("languages", Language.Type),
3232
edge.From("rass", RAS.Type).Ref("voters"),

ent/schema/version.go

+10-25
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package schema
22

33
import (
4+
"time"
5+
46
"github.com/facebook/ent"
57
"github.com/facebook/ent/schema/edge"
68
"github.com/facebook/ent/schema/field"
7-
"github.com/facebook/ent/schema/index"
89
)
910

1011
// Version holds the schema definition for the Version entity.
@@ -15,35 +16,19 @@ type Version struct {
1516
// Fields of the Version.
1617
func (Version) Fields() []ent.Field {
1718
return []ent.Field{
18-
field.Enum("status").
19-
Values("new", "modify", "translate", "review", "release").
20-
Default("new").
21-
Comment("new: The first version of the new article. Other users can't access and except the editor.").
22-
Comment("modify: Modified version for a language version. Other users can't access and except the editor.").
23-
Comment("translate: Translated version for a language version. Other users can't access and except the editor.").
24-
Comment("review: After the public article is pushed, the version that receives the vote before it is published. Except for editors and voters, no other users can access. They can't edit, but editor can withdraw publish.").
25-
Comment("release: If the article is public, everyone can access and edit it. if the article is private, only the source user can access it."),
26-
field.String("title").Optional(),
27-
field.String("seo").Optional(),
19+
field.String("name").Optional().Unique(),
20+
field.String("comment").Optional().Unique(),
21+
field.Time("created_at").Default(time.Now),
2822
}
2923
}
3024

3125
// Edges of the Version.
3226
func (Version) Edges() []ent.Edge {
3327
return []ent.Edge{
34-
edge.To("content", Content.Type).Unique(),
35-
edge.To("lang", Language.Type).Unique().Required(),
36-
edge.To("history", Content.Type),
37-
edge.To("main", Version.Type).Unique(),
38-
edge.From("tags", Tag.Type).Ref("versions"),
39-
edge.From("article", Article.Type).Ref("versions").Unique().Required(),
40-
edge.From("user", User.Type).Ref("drafts").Unique(),
41-
}
42-
}
43-
44-
// Indexs of the Version.
45-
func (Version) Indexs() []ent.Index {
46-
return []ent.Index{
47-
index.Fields("status").Edges("article").Edges("lang").Edges("user").Unique(),
28+
edge.To("content", Content.Type).Unique().Required(),
29+
edge.From("article", Article.Type).
30+
Ref("versions").
31+
Unique().
32+
Required(),
4833
}
4934
}

main.go

+1-30
Original file line numberDiff line numberDiff line change
@@ -175,36 +175,7 @@ func main() {
175175

176176
v1 := router.Group("/api/v1")
177177

178-
v1.PUT("/draft", authorizeRequired, handle(newDraft))
179-
v1.PUT("/draft/content", authorizeRequired, handle(putDraftContent))
180-
// v1.PUT("/response")
181-
// v1.PUT("/response/content")
182-
183-
// v1.POST("/reaction")
184-
// v1.POST("/star")
185-
// v1.POST("/node")
186-
// v1.POST("/publish", authorizeRequired, handle(publishArticle))
187-
188-
// v1.GET("/article", authentication, handle(getArticle))
189-
// v1.GET("/article/responses")
190-
// v1.GET("/article/versions")
191-
// v1.GET("/article/reactions")
192-
193-
// v1.GET("/articles")
194-
195-
// v1.GET("/tags", authentication, handle(getTags))
196-
// v1.GET("/tag/articles")
197-
// v1.GET("/tag/nodes")
198-
199-
// v1.GET("/node/articles")
200-
// v1.GET("/node")
201-
202-
// v1.GET("/user/tags")
203-
// v1.GET("/user/nodes")
204-
v1.GET("/user/draft", authorizeRequired, handle(getUserDraft))
205-
v1.GET("/user/drafts", authorizeRequired, handle(getUserDrafts))
206-
// v1.GET("/user/articles", authorizeRequired, handle(getUserArticles))
207-
// v1.GET("/search")
178+
v1.PUT("/article", authorizeRequired)
208179

209180
router.Run(fmt.Sprint(":", config.Port))
210181
}

res/js/main.js

-97
Original file line numberDiff line numberDiff line change
@@ -92,50 +92,6 @@ function postBlur() {
9292
postArticleContent()
9393
}
9494

95-
function postArticleContent() {
96-
var currentContent = getCurrentContent()
97-
if (currentContent != lastContent) {
98-
axios({
99-
method: "PUT",
100-
url: "/api/v1/draft/content?lang=" + getLang(),
101-
data: {
102-
body: content_layout.body,
103-
DraftID: articleID,
104-
tags: tag_layout.tags,
105-
},
106-
}).then(function (resp) {
107-
console.log(resp.status, resp.data)
108-
}).catch(function (resp) {
109-
console.log(resp.status, resp.data)
110-
})
111-
lastContent = currentContent
112-
}
113-
}
114-
115-
function editArticleContent(_articleID, _contentID) {
116-
axios({
117-
method: "GET",
118-
url: encodeQueryData("/api/v1/article", { id: _articleID, content_id: _contentID }),
119-
}).then(function (resp) {
120-
const content = resp.data
121-
articleID = _articleID
122-
setContent(content)
123-
}).catch(function (resp) {
124-
console.log(resp.status, resp.data)
125-
})
126-
}
127-
128-
function getTags() {
129-
axios({
130-
method: "GET",
131-
url: encodeQueryData("/api/v1/tags"),
132-
}).then(function (resp) {
133-
tag_layout.all = resp.data
134-
}).catch(function (resp) {
135-
console.log(resp.status, resp.data)
136-
})
137-
}
138-
13995
function tagChanged() {
14096
if ("" == tag_layout.tag) {
14197
if (TagStateSafe == tag_layout.state) {
@@ -216,21 +172,6 @@ function cancelDeleteTag() {
216172
tag_layout.state = TagStateMayDel
217173
}
218174

219-
function setLang(lang) {
220-
Cookies.set("user-lang", lang)
221-
axios({
222-
method: "GET",
223-
url: encodeQueryData("/api/v1/article", { id: articleID, lang: lang }),
224-
}).then(function (resp) {
225-
const content = resp.data
226-
setContent(content)
227-
}).catch(function (resp) {
228-
console.log(resp.status, resp.data)
229-
content_layout.body = ""
230-
lastContent = getCurrentContent()
231-
})
232-
}
233-
234175
function setContent(content = undefined) {
235176
if (content == undefined) {
236177
content_layout.body = ""
@@ -255,44 +196,6 @@ function getCurrentContent() {
255196
return content_layout.body + tag_layout.tags.join(",")
256197
}
257198

258-
var articleID;
259-
function onNewArticle() {
260-
axios({
261-
method: "PUT",
262-
url: encodeQueryData("/api/v1/draft", { status: "private", lang: lang_layout.lang }),
263-
}).then(function (resp) {
264-
console.log(resp.status, resp.data)
265-
articleID = resp.data
266-
setContent()
267-
}).catch(function (resp) {
268-
console.log(resp.status, resp.data)
269-
})
270-
}
271-
272-
function onGetArticles() {
273-
axios({
274-
method: "GET",
275-
url: "/api/v1/user/articles",
276-
}).then(function (resp) {
277-
articles_layout.seen = true
278-
articles_layout.articles = resp.data
279-
}).catch(function (resp) {
280-
console.log(resp.status, resp.data)
281-
})
282-
}
283-
284-
function onGetDrafts() {
285-
axios({
286-
method: "GET",
287-
url: "/api/v1/user/drafts",
288-
}).then(function (resp) {
289-
articles_layout.seen = true
290-
articles_layout.articles = resp.data
291-
}).catch(function (resp) {
292-
console.log(resp.status, resp.data)
293-
})
294-
}
295-
296199
function onSignout() {
297200
const githubOAuthAPI = "/signout"
298201
window.open(githubOAuthAPI, "_self")

rest_get_article.go

-54
This file was deleted.

rest_get_user_articles.go

-38
This file was deleted.

0 commit comments

Comments
 (0)