Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support vue syntax in html and fix user_yap using vue #77

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 96 additions & 63 deletions cmd/gopcomm/community_yap.gox
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import (
"context"
"time"
"os"
"encoding/json"
"fmt"
"io"
"net/http"
"fmt"
"os"
"strconv"
"strings"
"time"

"github.com/goplus/community/internal/core"
"github.com/goplus/community/markdown"
Expand All @@ -18,11 +20,11 @@ import (

var (
community *core.Community
trans *translation.Engine
trans *translation.Engine
)

const (
layoutUS = "January 2, 2006"
layoutUS = "January 2, 2006"
)

todo := context.TODO()
Expand Down Expand Up @@ -65,14 +67,14 @@ get "/p/:id", ctx => {
id := ctx.param("id")
article, _ := community.article(todo, id)
ctx.yap "article", {
"User": user,
"User": user,
"ID": id,
"Title": article.Title,
"Content": article.HtmlUrl,
"Tags": article.Tags,
"Cover": article.Cover,
"Mtime": article.Mtime.Format(layoutUS),
"Author": article.User,
"Tags": article.Tags,
"Cover": article.Cover,
"Mtime": article.Mtime.Format(layoutUS),
"Author": article.User,
// "User": article.User,
}
}
Expand All @@ -83,8 +85,8 @@ get "/getArticle/:id", ctx => {
ctx.json {
"code": 200,
"data": article,
}
}
}
}

get "/user/:id", ctx => {
id := ctx.param("id")
Expand All @@ -104,19 +106,46 @@ get "/user/:id", ctx => {
}
}
// get article list published by uid
items, _ :=community.GetArticlesByUid(todo, id)
items, _ := community.GetArticlesByUid(todo, id)
userClaimJson, _ := json.Marshal(&userClaim)
itemsJson, _ := json.Marshal(&items)
ctx.yap "user", {
"Id": id,
"CurrentUser": userClaim,
"User": user,
"Items": items,
"Id": id,
"CurrentUser": strings.Replace(string(userClaimJson), `\"`, `"`, -1),
"User": user,
"Items": strings.Replace(string(itemsJson), `\"`, `"`, -1),
}
}

get "/add", ctx => {
ctx.yap "edit", {}
}

get "/delete", ctx => {
id := ctx.param("id")
token, err := ctx.Request.Cookie("token")
uid, err := community.ParseJwtToken(token.Value)
if err != nil {
xLog.Error("token parse error")
ctx.json {
"code": 0,
"err": err.Error(),
}
}
err = community.deleteArticle(todo, uid, id)
if err != nil {
ctx.json {
"code": 0,
"err": "delete failed",
}
} else {
ctx.json {
"code": 200,
"msg": "delete success",
}
}
}

get "/", ctx => {
// from := ctx.param("from")
from := ctx.param("page")
Expand All @@ -142,11 +171,14 @@ get "/", ctx => {
limitInt = 20
}
// Get Article Info
articles, next, _ := community.articles(todo, page, limitInt, "")
// articles, next, _ := community.listArticle(todo, from, limitInt)
articles, total, _ := community.articles(todo, page, limitInt, "")
ctx.yap "home", {
"User": user,
"Items": articles,
"Next": next,
"User": user,
"Items": articles,
"Page": page,
"TotalPage": (total + limitInt - 1) / limitInt,
"Total": total,
}
}

Expand All @@ -155,7 +187,7 @@ get "/search", ctx => {
if searchValue == "" {
ctx.json {
"code": 400,
"err": "value can not be ''.",
"err": "value can not be ''.",
}
}

Expand All @@ -169,7 +201,7 @@ get "/search", ctx => {
if err != nil {
page = 1
}

// todo middleware
var user *core.User
token, err := ctx.Request.Cookie("token")
Expand All @@ -180,13 +212,14 @@ get "/search", ctx => {
}
}

articles, _ := community.searchArticle(todo, searchValue)
articles, total, _ := community.articles(todo, page, limitInt, searchValue)
ctx.yap "home", {
"User": user,
"Items": articles,
"Total": total,
"Value": searchValue,
"User": user,
"Items": articles,
"Value": searchValue,
"Page": page,
"TotalPage": (total + limitInt - 1) / limitInt,
"Total": total,
}
}

Expand All @@ -195,15 +228,15 @@ get "/edit/:id", ctx => {
if err != nil {
ctx.json {
"code": 500,
"err": "no token",
"err": "no token",
}
}

uid, err := community.ParseJwtToken(token.Value)
if err != nil {
ctx.json {
"code": 500,
"err": err.Error(),
"err": err.Error(),
}
}
// uid := "70f6a615-c0d5-4315-a5ac-34ca845450ed"
Expand All @@ -224,7 +257,7 @@ get "/getTrans", ctx => {
if err != nil {
ctx.json {
"code": 500,
"err": err.Error(),
"err": err.Error(),
}
}
ctx.json {
Expand All @@ -236,7 +269,7 @@ get "/getTrans", ctx => {
// click "submit" button
post "/commit", ctx => {
// Whether article has been translated or not
trans := ctx.param("trans") // if trans != "", add article
trans := ctx.param("trans") // if trans != "", add article
id := ctx.param("id")
mdData := ctx.param("content")
htmlData := ctx.param("html")
Expand All @@ -245,28 +278,29 @@ post "/commit", ctx => {
if err != nil {
ctx.json {
"code": 500,
"err": "no token",
"err": "no token",
}
}
uid, err := community.ParseJwtToken(token.Value)
if err != nil {
ctx.json {
"code": 500,
"err": err.Error(),
"err": err.Error(),
}
}
// uid := "70f6a615-c0d5-4315-a5ac-34ca845450ed"
// add article
// add article
article := &core.Article{
ArticleEntry: core.ArticleEntry{
ID: id,
Title: ctx.param("title"),
UId: uid,
Cover: ctx.param("cover"),
Tags: ctx.param("tags"),
ID: id,
Title: ctx.param("title"),
UId: uid,
Cover: ctx.param("cover"),
Tags: ctx.param("tags"),
Abstract: ctx.param("abstract"),
},
Content: mdData,
HtmlData:htmlData,
Content: mdData,
HtmlData: htmlData,
}
id, _ = community.putArticle(todo, uid, trans, article)
ctx.json {
Expand All @@ -283,57 +317,57 @@ post "/translate", ctx => {
if err != nil {
ctx.json {
"code": 500,
"err": "no token",
"err": "no token",
}
}
uid, err := community.ParseJwtToken(token.Value)
if err != nil {
ctx.json {
"code": 500,
"err": err.Error(),
"err": err.Error(),
}
}
// uid := "70f6a615-c0d5-4315-a5ac-34ca845450ed"
mdData := ctx.param("content")
mdData := ctx.param("content")
htmlData := ctx.param("html")
id := ctx.param("id")
// get translation markdown
transData, err := trans.translateMarkdownText(mdData, language.Chinese, language.English)
transData, err := trans.translateMarkdownText(mdData, language.Chinese, language.English)
if err != nil {
ctx.json {
"code": 500,
"err": err.Error(),
"err": err.Error(),
}
}
id, _ = community.saveHtml(todo, uid, htmlData, mdData, id)
ctx.json {
"code": 200,
"id": id, //article id
"data": transData, // translation markdown content
"id": id, //article id
"data": transData, // translation markdown content
}
}

get "/getMedia/:id", ctx => {
mediaId := ctx.param("id")
// todo the err not empty return 404 for image
fileKey, _ := community.GetMediaUrl(context.Background(),mediaId)
fileKey, _ := community.GetMediaUrl(context.Background(), mediaId)

http.Redirect(ctx.ResponseWriter, ctx.Request, domain+fileKey, http.StatusTemporaryRedirect)
}

get "/getMediaUrl/:id",ctx => {
get "/getMediaUrl/:id", ctx => {
id := ctx.param("id")
fileKey, err := community.GetMediaUrl(todo, id)
htmlUrl := fmt.Sprintf("%s%s", domain, fileKey)
if err != nil {
ctx.json {
"code": 500,
"err": "have no html media",
"err": "have no html media",
}
}
ctx.json {
"code": 200,
"url": htmlUrl,
"url": htmlUrl,
}
}

Expand All @@ -349,7 +383,6 @@ post "/upload", ctx => {
return
}


dst, err := os.Create(filename)
if err != nil {
xLog.Error("create file error:", file)
Expand All @@ -366,7 +399,6 @@ post "/upload", ctx => {
}
}()


_, err = io.Copy(dst, file)
if err != nil {
xLog.Error("copy file errer:", filename)
Expand All @@ -383,14 +415,14 @@ post "/upload", ctx => {
if err != nil {
ctx.json {
"code": 500,
"err": "no token",
"err": "no token",
}
}
uid, err := community.ParseJwtToken(token.Value)
if err != nil {
ctx.json {
"code": 500,
"err": err.Error(),
"err": err.Error(),
}
}
id,err:=community.SaveMedia(context.Background(), uid, bytes)
Expand All @@ -401,7 +433,7 @@ post "/upload", ctx => {
}
// todo append current project ip and getMedia
// sample: 127.0.0.1:8080/getMedia/ + id
ctx.JSON(200,id)
ctx.JSON(200, id)
}

get "/login", ctx => {
Expand All @@ -415,6 +447,7 @@ get "/login", ctx => {
ctx.Redirect loginURL, http.StatusFound
}


get "/logout", ctx => {
tokenCookie, err := ctx.Request.Cookie("token")
if err != nil {
Expand All @@ -430,18 +463,18 @@ get "/logout", ctx => {
}

get "/callback", ctx => {
code :=ctx.URL.Query().Get("code")
state :=ctx.URL.Query().Get("state")
code := ctx.URL.Query().Get("code")
state := ctx.URL.Query().Get("state")

token, error := community.GetAccessToken(code, state)
if error != nil {
xLog.Error("err",error) // Redirect to login
}

cookie := http.Cookie{
Name: "token",
Value: token.AccessToken,
Path: "/",
Name: "token",
Value: token.AccessToken,
Path: "/",
MaxAge: 3600,
}
http.SetCookie(ctx.ResponseWriter, &cookie)
Expand Down
Loading
Loading