Move adding to channel to own function

This commit is contained in:
Peter Stuifzand 2018-04-07 20:12:57 +02:00
parent 5cab70b694
commit cf5d4c0a49
2 changed files with 32 additions and 32 deletions

View File

@ -48,7 +48,6 @@ func init() {
// Fetch3 fills stuff // Fetch3 fills stuff
func (b *memoryBackend) Fetch3(channel, fetchURL string) error { func (b *memoryBackend) Fetch3(channel, fetchURL string) error {
log.Printf("Fetching channel=%s fetchURL=%s\n", channel, fetchURL) log.Printf("Fetching channel=%s fetchURL=%s\n", channel, fetchURL)
channelKey := fmt.Sprintf("channel:%s:posts", channel)
md, err := Fetch2(fetchURL) md, err := Fetch2(fetchURL)
if err != nil { if err != nil {
@ -110,13 +109,20 @@ func (b *memoryBackend) Fetch3(channel, fetchURL string) error {
if _, e := r["published"]; e { if _, e := r["published"]; e {
item := mapToItem(r) item := mapToItem(r)
b.channelAddItem(channel, item)
}
}
return nil
}
func (b *memoryBackend) channelAddItem(channel string, item microsub.Item) {
// send to redis // send to redis
channelKey := fmt.Sprintf("channel:%s:posts", channel)
data, err := json.Marshal(item) data, err := json.Marshal(item)
if err != nil { if err != nil {
log.Printf("error while creating item for redis: %v\n", err) log.Printf("error while creating item for redis: %v\n", err)
continue return
} }
forRedis := redisItem{ forRedis := redisItem{
@ -125,21 +131,19 @@ func (b *memoryBackend) Fetch3(channel, fetchURL string) error {
Read: item.Read, Read: item.Read,
Data: data, Data: data,
} }
itemKey := fmt.Sprintf("item:%s", item.Id) itemKey := fmt.Sprintf("item:%s", item.Id)
_, err = redis.String(b.Redis.Do("HMSET", redis.Args{}.Add(itemKey).AddFlat(&forRedis)...)) _, err = redis.String(b.Redis.Do("HMSET", redis.Args{}.Add(itemKey).AddFlat(&forRedis)...))
if err != nil { if err != nil {
log.Printf("error while writing item for redis: %v\n", err) log.Printf("error while writing item for redis: %v\n", err)
continue return
} }
_, err = b.Redis.Do("SADD", channelKey, itemKey) _, err = b.Redis.Do("SADD", channelKey, itemKey)
if err != nil { if err != nil {
log.Printf("error while adding item %s to channel %s for redis: %v\n", itemKey, channelKey, err) log.Printf("error while adding item %s to channel %s for redis: %v\n", itemKey, channelKey, err)
continue return
} }
}
}
return nil
} }
type redisItem struct { type redisItem struct {

View File

@ -271,10 +271,6 @@ func (b *memoryBackend) TimelineGet(after, before, channel string) microsub.Time
items := []microsub.Item{} items := []microsub.Item{}
// for _, feed := range feeds {
// b.Fetch3(channel, feed.URL)
// }
channelKey := fmt.Sprintf("channel:%s:posts", channel) channelKey := fmt.Sprintf("channel:%s:posts", channel)
itemJsons, err := redis.ByteSlices(b.Redis.Do("SORT", channelKey, "BY", "*->Published", "GET", "*->Data", "ASC", "ALPHA")) itemJsons, err := redis.ByteSlices(b.Redis.Do("SORT", channelKey, "BY", "*->Published", "GET", "*->Data", "ASC", "ALPHA"))