-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription.go
122 lines (96 loc) · 2.22 KB
/
subscription.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"sync"
"time"
"github.com/mmcdole/gofeed"
)
var savedFilePath = path.Join("/", "rss-email", "user")
type userURLInfo struct {
LastHash string
}
func newUserURLInfo() *userURLInfo {
return &userURLInfo{}
}
type userSubscriptionType map[string]*userURLInfo
func newUserSubscription() *userSubscriptionType {
return &userSubscriptionType{}
}
// printToUser produce email body to user
func (userSubscription *userSubscriptionType) printToUser() (str string, err error) {
str += fmt.Sprintf("<div>your subscribed RSS count: %d </div><br>", len(*userSubscription))
str += "<div>subscribed RSS url list:</div>"
for k := range *userSubscription {
str += "<div>" + k + "</div>"
}
return str, nil
}
type userSubscriptionsType struct {
sync.RWMutex
m map[string]*userSubscriptionType
}
func newUserSubscriptions() userSubscriptionsType {
return userSubscriptionsType{m: make(map[string]*userSubscriptionType)}
}
// save userSubscription.m to disc.
func (userSubscriptions *userSubscriptionsType) saveToDisk() error {
dir, _ := path.Split(savedFilePath)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
file, err := os.Create(savedFilePath)
if err != nil {
return err
}
b, err := json.Marshal(userSubscriptions.m)
if err != nil {
return err
}
if _, err := file.Write(b); err != nil {
return err
}
if err := file.Close(); err != nil {
return err
}
return nil
}
// save userSubscription.m to disc.
func (userSubscriptions *userSubscriptionsType) restoreFromDisk() error {
file, err := os.Open(savedFilePath)
if err != nil {
log.Println("no file to restore from")
return nil
}
b, err := ioutil.ReadAll(file)
if err != nil {
return err
}
if err := json.Unmarshal(b, &userSubscriptions.m); err != nil {
return err
}
if err := file.Close(); err != nil {
return err
}
return nil
}
type urlInfo struct {
raw string
lastUpdate time.Time
feed *gofeed.Feed
error error
}
func newURLInfo() *urlInfo {
return &urlInfo{}
}
type subscriptionType struct {
sync.RWMutex
m map[string]*urlInfo
}
func newSubscription() subscriptionType {
return subscriptionType{m: make(map[string]*urlInfo)}
}