Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix #38

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

Merged
merged 9 commits into from
Oct 13, 2017
Merged

fix #38

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
2 changes: 1 addition & 1 deletion config/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ CREATE TABLE IF NOT EXISTS `articles` (
KEY (`top`),
KEY (`author_txt`),
KEY (`domain`),
KEY (`ctime`)
KEY (`mtime`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '网络文章聚合表';

CREATE TABLE IF NOT EXISTS `crawl_rule` (
Expand Down
1 change: 0 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ go install server/indexer
go install server/crawler

export GOPATH="$OLDGOPATH"
export PATH="$OLDPATH"

echo 'finished'

101 changes: 101 additions & 0 deletions src/http/controller/feed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2016 The StudyGolang Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// http://studygolang.com
// Author: polaris [email protected]

package controller

import (
"fmt"
"logic"
"model"
"net/http"
"time"

. "http"

"github.com/gorilla/feeds"
"github.com/labstack/echo"
)

type FeedController struct{}

// 注册路由
func (self FeedController) RegisterRoute(g *echo.Group) {
g.Get("/feed.html", self.Atom)
g.Get("/feed.xml", self.List)
}

func (self FeedController) Atom(ctx echo.Context) error {
return Render(ctx, "atom.html", map[string]interface{}{})
}

func (self FeedController) List(ctx echo.Context) error {
link := logic.WebsiteSetting.Domain
if logic.WebsiteSetting.OnlyHttps {
link = "https://" + link + "/"
} else {
link = "http://" + link + "/"
}

now := time.Now()

feed := &feeds.Feed{
Title: logic.WebsiteSetting.Name,
Link: &feeds.Link{Href: link},
Description: logic.WebsiteSetting.Slogan,
Author: &feeds.Author{Name: "polaris", Email: "[email protected]"},
Created: now,
Updated: now,
}

respBody, err := logic.DefaultSearcher.FindAtomFeeds(50)
if err != nil {
return err
}

feed.Items = make([]*feeds.Item, len(respBody.Docs))

for i, doc := range respBody.Docs {
url := ""

switch doc.Objtype {
case model.TypeTopic:
url = fmt.Sprintf("%s/topics/%d", link, doc.Objid)
case model.TypeArticle:
url = fmt.Sprintf("%s/articles/%d", link, doc.Objid)
case model.TypeResource:
url = fmt.Sprintf("%s/resources/%d", link, doc.Objid)
case model.TypeProject:
url = fmt.Sprintf("%s/p/%d", link, doc.Objid)
case model.TypeWiki:
url = fmt.Sprintf("%s/wiki/%d", link, doc.Objid)
case model.TypeBook:
url = fmt.Sprintf("%s/book/%d", link, doc.Objid)
}
feed.Items[i] = &feeds.Item{
Title: doc.Title,
Link: &feeds.Link{Href: url},
Author: &feeds.Author{Name: doc.Author},
Description: doc.Content,
Created: time.Time(doc.SortTime),
Updated: time.Time(doc.UpdatedAt),
}
}

atom, err := feed.ToAtom()
if err != nil {
return err
}

return self.responseXML(ctx, atom)
}

func (FeedController) responseXML(ctx echo.Context, data string) (err error) {
response := ctx.Response()
response.Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
response.WriteHeader(http.StatusOK)
_, err = response.Write([]byte(data))
return
}
2 changes: 2 additions & 0 deletions src/http/controller/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func RegisterRoutes(g *echo.Group) {
new(DownloadController).RegisterRoute(g)
new(LinkController).RegisterRoute(g)

new(FeedController).RegisterRoute(g)

new(WechatController).RegisterRoute(g)

new(InstallController).RegisterRoute(g)
Expand Down
44 changes: 40 additions & 4 deletions src/logic/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ func CanEdit(me *model.Me, curModel interface{}) bool {
return false
}

if me.IsAdmin {
return true
}

canEditTime := time.Duration(UserSetting["can_edit_time"]) * time.Second

switch entity := curModel.(type) {
case *model.Topic:
if me.Uid != entity.Uid && me.IsAdmin {
return true
}

if time.Now().Sub(time.Time(entity.Ctime)) > canEditTime {
return false
}
Expand All @@ -78,6 +78,10 @@ func CanEdit(me *model.Me, curModel interface{}) bool {
return true
}
case *model.Article:
if me.IsAdmin {
return true
}

// 文章的能编辑时间是15天
if time.Now().Sub(time.Time(entity.Ctime)) > 15*86400*time.Second {
return false
Expand All @@ -95,6 +99,10 @@ func CanEdit(me *model.Me, curModel interface{}) bool {
return true
}
case *model.OpenProject:
if me.IsAdmin {
return true
}

// 开源项目的能编辑时间是30天
if time.Now().Sub(time.Time(entity.Ctime)) > 30*86400*time.Second {
return false
Expand All @@ -104,6 +112,9 @@ func CanEdit(me *model.Me, curModel interface{}) bool {
return true
}
case *model.Wiki:
if me.IsAdmin {
return true
}
if time.Now().Sub(time.Time(entity.Ctime)) > canEditTime {
return false
}
Expand All @@ -112,6 +123,9 @@ func CanEdit(me *model.Me, curModel interface{}) bool {
return true
}
case *model.Book:
if me.IsAdmin {
return true
}
if time.Now().Sub(time.Time(entity.CreatedAt)) > canEditTime {
return false
}
Expand All @@ -120,6 +134,10 @@ func CanEdit(me *model.Me, curModel interface{}) bool {
return true
}
case map[string]interface{}:
if adminCanEdit(entity, me) {
return true
}

if ctime, ok := entity["ctime"]; ok {
if time.Now().Sub(time.Time(ctime.(model.OftenTime))) > canEditTime {
return false
Expand Down Expand Up @@ -182,3 +200,21 @@ func website() string {
}
return host + WebsiteSetting.Domain
}

func adminCanEdit(entity map[string]interface{}, me *model.Me) bool {
if uid, ok := entity["uid"]; ok {
if me.Uid != uid.(int) && me.IsAdmin {
return true
}
return false
}

if username, ok := entity["username"]; ok {
if me.Username != username.(string) && me.IsAdmin {
return true
}
return false
}

return false
}
Loading