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

Skip to content

Commit 4212875

Browse files
authored
Merge pull request #38 from studygolang/master
fix
2 parents 2729602 + 958c98c commit 4212875

File tree

10 files changed

+1023
-305
lines changed

10 files changed

+1023
-305
lines changed

config/db.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ CREATE TABLE IF NOT EXISTS `articles` (
321321
KEY (`top`),
322322
KEY (`author_txt`),
323323
KEY (`domain`),
324-
KEY (`ctime`)
324+
KEY (`mtime`)
325325
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '网络文章聚合表';
326326

327327
CREATE TABLE IF NOT EXISTS `crawl_rule` (

install.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ go install server/indexer
2424
go install server/crawler
2525

2626
export GOPATH="$OLDGOPATH"
27-
export PATH="$OLDPATH"
2827

2928
echo 'finished'
3029

src/http/controller/feed.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2016 The StudyGolang Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
// http://studygolang.com
5+
// Author: polaris [email protected]
6+
7+
package controller
8+
9+
import (
10+
"fmt"
11+
"logic"
12+
"model"
13+
"net/http"
14+
"time"
15+
16+
. "http"
17+
18+
"github.com/gorilla/feeds"
19+
"github.com/labstack/echo"
20+
)
21+
22+
type FeedController struct{}
23+
24+
// 注册路由
25+
func (self FeedController) RegisterRoute(g *echo.Group) {
26+
g.Get("/feed.html", self.Atom)
27+
g.Get("/feed.xml", self.List)
28+
}
29+
30+
func (self FeedController) Atom(ctx echo.Context) error {
31+
return Render(ctx, "atom.html", map[string]interface{}{})
32+
}
33+
34+
func (self FeedController) List(ctx echo.Context) error {
35+
link := logic.WebsiteSetting.Domain
36+
if logic.WebsiteSetting.OnlyHttps {
37+
link = "https://" + link + "/"
38+
} else {
39+
link = "http://" + link + "/"
40+
}
41+
42+
now := time.Now()
43+
44+
feed := &feeds.Feed{
45+
Title: logic.WebsiteSetting.Name,
46+
Link: &feeds.Link{Href: link},
47+
Description: logic.WebsiteSetting.Slogan,
48+
Author: &feeds.Author{Name: "polaris", Email: "[email protected]"},
49+
Created: now,
50+
Updated: now,
51+
}
52+
53+
respBody, err := logic.DefaultSearcher.FindAtomFeeds(50)
54+
if err != nil {
55+
return err
56+
}
57+
58+
feed.Items = make([]*feeds.Item, len(respBody.Docs))
59+
60+
for i, doc := range respBody.Docs {
61+
url := ""
62+
63+
switch doc.Objtype {
64+
case model.TypeTopic:
65+
url = fmt.Sprintf("%s/topics/%d", link, doc.Objid)
66+
case model.TypeArticle:
67+
url = fmt.Sprintf("%s/articles/%d", link, doc.Objid)
68+
case model.TypeResource:
69+
url = fmt.Sprintf("%s/resources/%d", link, doc.Objid)
70+
case model.TypeProject:
71+
url = fmt.Sprintf("%s/p/%d", link, doc.Objid)
72+
case model.TypeWiki:
73+
url = fmt.Sprintf("%s/wiki/%d", link, doc.Objid)
74+
case model.TypeBook:
75+
url = fmt.Sprintf("%s/book/%d", link, doc.Objid)
76+
}
77+
feed.Items[i] = &feeds.Item{
78+
Title: doc.Title,
79+
Link: &feeds.Link{Href: url},
80+
Author: &feeds.Author{Name: doc.Author},
81+
Description: doc.Content,
82+
Created: time.Time(doc.SortTime),
83+
Updated: time.Time(doc.UpdatedAt),
84+
}
85+
}
86+
87+
atom, err := feed.ToAtom()
88+
if err != nil {
89+
return err
90+
}
91+
92+
return self.responseXML(ctx, atom)
93+
}
94+
95+
func (FeedController) responseXML(ctx echo.Context, data string) (err error) {
96+
response := ctx.Response()
97+
response.Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
98+
response.WriteHeader(http.StatusOK)
99+
_, err = response.Write([]byte(data))
100+
return
101+
}

src/http/controller/routes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func RegisterRoutes(g *echo.Group) {
3737
new(DownloadController).RegisterRoute(g)
3838
new(LinkController).RegisterRoute(g)
3939

40+
new(FeedController).RegisterRoute(g)
41+
4042
new(WechatController).RegisterRoute(g)
4143

4244
new(InstallController).RegisterRoute(g)

src/logic/common.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ func CanEdit(me *model.Me, curModel interface{}) bool {
6262
return false
6363
}
6464

65-
if me.IsAdmin {
66-
return true
67-
}
68-
6965
canEditTime := time.Duration(UserSetting["can_edit_time"]) * time.Second
7066

7167
switch entity := curModel.(type) {
7268
case *model.Topic:
69+
if me.Uid != entity.Uid && me.IsAdmin {
70+
return true
71+
}
72+
7373
if time.Now().Sub(time.Time(entity.Ctime)) > canEditTime {
7474
return false
7575
}
@@ -78,6 +78,10 @@ func CanEdit(me *model.Me, curModel interface{}) bool {
7878
return true
7979
}
8080
case *model.Article:
81+
if me.IsAdmin {
82+
return true
83+
}
84+
8185
// 文章的能编辑时间是15天
8286
if time.Now().Sub(time.Time(entity.Ctime)) > 15*86400*time.Second {
8387
return false
@@ -95,6 +99,10 @@ func CanEdit(me *model.Me, curModel interface{}) bool {
9599
return true
96100
}
97101
case *model.OpenProject:
102+
if me.IsAdmin {
103+
return true
104+
}
105+
98106
// 开源项目的能编辑时间是30天
99107
if time.Now().Sub(time.Time(entity.Ctime)) > 30*86400*time.Second {
100108
return false
@@ -104,6 +112,9 @@ func CanEdit(me *model.Me, curModel interface{}) bool {
104112
return true
105113
}
106114
case *model.Wiki:
115+
if me.IsAdmin {
116+
return true
117+
}
107118
if time.Now().Sub(time.Time(entity.Ctime)) > canEditTime {
108119
return false
109120
}
@@ -112,6 +123,9 @@ func CanEdit(me *model.Me, curModel interface{}) bool {
112123
return true
113124
}
114125
case *model.Book:
126+
if me.IsAdmin {
127+
return true
128+
}
115129
if time.Now().Sub(time.Time(entity.CreatedAt)) > canEditTime {
116130
return false
117131
}
@@ -120,6 +134,10 @@ func CanEdit(me *model.Me, curModel interface{}) bool {
120134
return true
121135
}
122136
case map[string]interface{}:
137+
if adminCanEdit(entity, me) {
138+
return true
139+
}
140+
123141
if ctime, ok := entity["ctime"]; ok {
124142
if time.Now().Sub(time.Time(ctime.(model.OftenTime))) > canEditTime {
125143
return false
@@ -182,3 +200,21 @@ func website() string {
182200
}
183201
return host + WebsiteSetting.Domain
184202
}
203+
204+
func adminCanEdit(entity map[string]interface{}, me *model.Me) bool {
205+
if uid, ok := entity["uid"]; ok {
206+
if me.Uid != uid.(int) && me.IsAdmin {
207+
return true
208+
}
209+
return false
210+
}
211+
212+
if username, ok := entity["username"]; ok {
213+
if me.Username != username.(string) && me.IsAdmin {
214+
return true
215+
}
216+
return false
217+
}
218+
219+
return false
220+
}

0 commit comments

Comments
 (0)