-
Notifications
You must be signed in to change notification settings - Fork 0
/
SourceController.go
89 lines (72 loc) · 1.89 KB
/
SourceController.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package controller
import (
"errors"
"fmt"
"sync/atomic"
"time"
"github.com/yujiahaol68/rossy/app/database"
"github.com/yujiahaol68/rossy/socket"
"github.com/yujiahaol68/rossy/app/entity"
"github.com/yujiahaol68/rossy/app/service/category"
postService "github.com/yujiahaol68/rossy/app/service/post"
sourceService "github.com/yujiahaol68/rossy/app/service/source"
"github.com/yujiahaol68/rossy/feed"
)
type SourceController struct{}
var Source SourceController = SourceController{}
func (ctrl *SourceController) Add(url string, categoryID int64) (*entity.Source, error) {
source, err := feed.GetSourceByURL(url)
if err != nil {
return nil, err
}
c := category.FindOne(categoryID)
if c == nil {
return nil, errors.New("ID out of range")
}
s := &entity.Source{
Category: categoryID,
URL: url,
ETag: source.ETag,
LastModified: source.LastModified,
Alias: source.Alias,
Kind: source.Type,
Updated: time.Now(),
}
es := sourceService.FindByURL(url)
if es.ID != 0 {
return nil, errors.New("Source exist")
}
_, err = database.Conn().InsertOne(s)
if err == nil {
go post.AddNewFeeder(feed.RequestCache[url], s.ID)
}
return s, err
}
func (ctrl *SourceController) Unsubscribe(id int64) error {
es := sourceService.FindOne(id)
if es.ID == 0 {
return errors.New("Not Found")
}
go sourceService.Del(es.ID)
return postService.DelBySource(es.ID)
}
func (ctrl *SourceController) UpdateAll() {
sl := sourceService.All()
if len(sl) == 0 {
return
}
var counter int64
c := make(chan *[]*entity.Post)
go func() {
for pl := range c {
fmt.Printf("Receiving %d new posts", len(*pl))
atomic.AddInt64(&counter, int64(len(*pl)))
for _, p := range *pl {
database.Conn().InsertOne(p)
}
feed.WaitDone()
}
}()
feed.Update(sl, c)
socket.Notices.Push(fmt.Sprintf("feed-list-refresh;success;Update %d new feeds", counter))
}