Skip to content

Commit

Permalink
Rewrite: Use versions and branches to control the public and private …
Browse files Browse the repository at this point in the history
…status of the article. Happy Lantern Festiva🎉🎉🎉. see ##1
  • Loading branch information
excing committed Feb 25, 2021
1 parent 7912020 commit 6bb4c18
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 484 deletions.
1 change: 1 addition & 0 deletions ent/schema/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (Article) Fields() []ent.Field {
func (Article) Edges() []ent.Edge {
return []ent.Edge{
edge.To("versions", Version.Type),
edge.To("branches", Draft.Type),
edge.To("reactions", Reaction.Type),
edge.To("quote", Quote.Type).Unique().StorageKey(edge.Column("response_id")),
edge.To("assets", Asset.Type),
Expand Down
22 changes: 19 additions & 3 deletions ent/schema/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,32 @@ func (Content) Fields() []ent.Field {
field.String("title").Optional(),
field.String("gist").Optional(),
field.String("body").Default(""),
field.String("versionName").Optional().Unique(),
field.String("seo").Optional(),
field.Enum("state").
Values("draft", "review", "release").
Default("draft").
Comment("release: all users can access.").
Comment("review: only voters can access.").
Comment("draft: only the editor can access."),
field.Time("created_at").Default(time.Now),
}
}

// Edges of the Content.
func (Content) Edges() []ent.Edge {
return []ent.Edge{
edge.From("version", Version.Type).Ref("history").Unique().Required(),
edge.To("lang", Language.Type).Unique().Required(),
edge.To("last", Content.Type).Unique().Required(),
edge.To("tags", Tag.Type),
edge.To("sections", Section.Type),
edge.To("lang", Language.Type),
edge.From("branche", Draft.Type).
Ref("snapshots").
Unique().
Required().
Comment("Snapshot of a branch."),
edge.From("version", Version.Type).
Ref("content").
Unique().
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."),
}
}
28 changes: 28 additions & 0 deletions ent/schema/draft.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package schema

import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/edge"
"github.com/facebook/ent/schema/field"
)

// Draft holds the schema definition for the Draft entity.
type Draft struct {
ent.Schema
}

// Fields of the Draft.
func (Draft) Fields() []ent.Field {
return []ent.Field{
field.Enum("status").Values("read", "write").Default("write"),
}
}

// Edges of the Draft.
func (Draft) Edges() []ent.Edge {
return []ent.Edge{
edge.To("snapshots", Content.Type),
edge.From("user", User.Type).Ref("drafts").Unique().Required(),
edge.From("article", Article.Type).Ref("branches").Unique().Required(),
}
}
2 changes: 1 addition & 1 deletion ent/schema/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (User) Edges() []ent.Edge {
return []ent.Edge{
edge.To("assets", Asset.Type).StorageKey(edge.Column("owner_id")),
edge.To("archives", Archive.Type).StorageKey(edge.Column("owner_id")),
edge.To("drafts", Version.Type).StorageKey(edge.Column("editor_id")),
edge.To("drafts", Draft.Type).StorageKey(edge.Column("editor_id")),
edge.To("tags", Tag.Type),
edge.To("languages", Language.Type),
edge.From("rass", RAS.Type).Ref("voters"),
Expand Down
35 changes: 10 additions & 25 deletions ent/schema/version.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package schema

import (
"time"

"github.com/facebook/ent"
"github.com/facebook/ent/schema/edge"
"github.com/facebook/ent/schema/field"
"github.com/facebook/ent/schema/index"
)

// Version holds the schema definition for the Version entity.
Expand All @@ -15,35 +16,19 @@ type Version struct {
// Fields of the Version.
func (Version) Fields() []ent.Field {
return []ent.Field{
field.Enum("status").
Values("new", "modify", "translate", "review", "release").
Default("new").
Comment("new: The first version of the new article. Other users can't access and except the editor.").
Comment("modify: Modified version for a language version. Other users can't access and except the editor.").
Comment("translate: Translated version for a language version. Other users can't access and except the editor.").
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.").
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."),
field.String("title").Optional(),
field.String("seo").Optional(),
field.String("name").Optional().Unique(),
field.String("comment").Optional().Unique(),
field.Time("created_at").Default(time.Now),
}
}

// Edges of the Version.
func (Version) Edges() []ent.Edge {
return []ent.Edge{
edge.To("content", Content.Type).Unique(),
edge.To("lang", Language.Type).Unique().Required(),
edge.To("history", Content.Type),
edge.To("main", Version.Type).Unique(),
edge.From("tags", Tag.Type).Ref("versions"),
edge.From("article", Article.Type).Ref("versions").Unique().Required(),
edge.From("user", User.Type).Ref("drafts").Unique(),
}
}

// Indexs of the Version.
func (Version) Indexs() []ent.Index {
return []ent.Index{
index.Fields("status").Edges("article").Edges("lang").Edges("user").Unique(),
edge.To("content", Content.Type).Unique().Required(),
edge.From("article", Article.Type).
Ref("versions").
Unique().
Required(),
}
}
31 changes: 1 addition & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,36 +175,7 @@ func main() {

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

v1.PUT("/draft", authorizeRequired, handle(newDraft))
v1.PUT("/draft/content", authorizeRequired, handle(putDraftContent))
// v1.PUT("/response")
// v1.PUT("/response/content")

// v1.POST("/reaction")
// v1.POST("/star")
// v1.POST("/node")
// v1.POST("/publish", authorizeRequired, handle(publishArticle))

// v1.GET("/article", authentication, handle(getArticle))
// v1.GET("/article/responses")
// v1.GET("/article/versions")
// v1.GET("/article/reactions")

// v1.GET("/articles")

// v1.GET("/tags", authentication, handle(getTags))
// v1.GET("/tag/articles")
// v1.GET("/tag/nodes")

// v1.GET("/node/articles")
// v1.GET("/node")

// v1.GET("/user/tags")
// v1.GET("/user/nodes")
v1.GET("/user/draft", authorizeRequired, handle(getUserDraft))
v1.GET("/user/drafts", authorizeRequired, handle(getUserDrafts))
// v1.GET("/user/articles", authorizeRequired, handle(getUserArticles))
// v1.GET("/search")
v1.PUT("/article", authorizeRequired)

router.Run(fmt.Sprint(":", config.Port))
}
97 changes: 0 additions & 97 deletions res/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,50 +92,6 @@ function postBlur() {
postArticleContent()
}

function postArticleContent() {
var currentContent = getCurrentContent()
if (currentContent != lastContent) {
axios({
method: "PUT",
url: "/api/v1/draft/content?lang=" + getLang(),
data: {
body: content_layout.body,
DraftID: articleID,
tags: tag_layout.tags,
},
}).then(function (resp) {
console.log(resp.status, resp.data)
}).catch(function (resp) {
console.log(resp.status, resp.data)
})
lastContent = currentContent
}
}

function editArticleContent(_articleID, _contentID) {
axios({
method: "GET",
url: encodeQueryData("/api/v1/article", { id: _articleID, content_id: _contentID }),
}).then(function (resp) {
const content = resp.data
articleID = _articleID
setContent(content)
}).catch(function (resp) {
console.log(resp.status, resp.data)
})
}

function getTags() {
axios({
method: "GET",
url: encodeQueryData("/api/v1/tags"),
}).then(function (resp) {
tag_layout.all = resp.data
}).catch(function (resp) {
console.log(resp.status, resp.data)
})
}

function tagChanged() {
if ("" == tag_layout.tag) {
if (TagStateSafe == tag_layout.state) {
Expand Down Expand Up @@ -216,21 +172,6 @@ function cancelDeleteTag() {
tag_layout.state = TagStateMayDel
}

function setLang(lang) {
Cookies.set("user-lang", lang)
axios({
method: "GET",
url: encodeQueryData("/api/v1/article", { id: articleID, lang: lang }),
}).then(function (resp) {
const content = resp.data
setContent(content)
}).catch(function (resp) {
console.log(resp.status, resp.data)
content_layout.body = ""
lastContent = getCurrentContent()
})
}

function setContent(content = undefined) {
if (content == undefined) {
content_layout.body = ""
Expand All @@ -255,44 +196,6 @@ function getCurrentContent() {
return content_layout.body + tag_layout.tags.join(",")
}

var articleID;
function onNewArticle() {
axios({
method: "PUT",
url: encodeQueryData("/api/v1/draft", { status: "private", lang: lang_layout.lang }),
}).then(function (resp) {
console.log(resp.status, resp.data)
articleID = resp.data
setContent()
}).catch(function (resp) {
console.log(resp.status, resp.data)
})
}

function onGetArticles() {
axios({
method: "GET",
url: "/api/v1/user/articles",
}).then(function (resp) {
articles_layout.seen = true
articles_layout.articles = resp.data
}).catch(function (resp) {
console.log(resp.status, resp.data)
})
}

function onGetDrafts() {
axios({
method: "GET",
url: "/api/v1/user/drafts",
}).then(function (resp) {
articles_layout.seen = true
articles_layout.articles = resp.data
}).catch(function (resp) {
console.log(resp.status, resp.data)
})
}

function onSignout() {
const githubOAuthAPI = "/signout"
window.open(githubOAuthAPI, "_self")
Expand Down
54 changes: 0 additions & 54 deletions rest_get_article.go

This file was deleted.

38 changes: 0 additions & 38 deletions rest_get_user_articles.go

This file was deleted.

Loading

0 comments on commit 6bb4c18

Please sign in to comment.