Skip to content

Commit

Permalink
Fixed the broken mentions channel.
Browse files Browse the repository at this point in the history
The problem was that I was messing too much with pointers and then the
actual value would get wiped off by GC? By filtering the comments this
problem disappears.

Signed-off-by: Sandy <[email protected]>
  • Loading branch information
thecsw committed Sep 22, 2019
1 parent da7b64c commit 6a24e2a
Showing 1 changed file with 34 additions and 16 deletions.
50 changes: 34 additions & 16 deletions streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ import (
"github.com/thecsw/mira/models"
)

// // c is the channel with all unread messages
// // stop is the channel to stop the stream. Do stop <- true to stop the loop
// c is the channel with all unread messages
// stop is the channel to stop the stream. Do stop <- true to stop the loop
func (r *Reddit) StreamCommentReplies() (<-chan *models.Comment, chan bool) {
c := make(chan *models.Comment, 25)
stop := make(chan bool, 1)
go func() {
for {
stop <- false
un, _ := r.Me().ListUnreadMessages()
un = onlyReplies(un)
for _, v := range un {
// Only process comment replies and
// mark them as read.
if v.IsCommentReply() {
c <- &v
// You can read the message with
r.Me().ReadMessage(v.GetId())
}
c <- &v
// You can read the message with
r.Me().ReadMessage(v.GetId())
}
time.Sleep(r.Stream.CommentListInterval * time.Second)
if <-stop {
Expand All @@ -33,23 +32,22 @@ func (r *Reddit) StreamCommentReplies() (<-chan *models.Comment, chan bool) {
return c, stop
}

// // c is the channel with all unread messages
// // stop is the channel to stop the stream. Do stop <- true to stop the loop
// c is the channel with all unread messages
// stop is the channel to stop the stream. Do stop <- true to stop the loop
func (r *Reddit) StreamMentions() (<-chan *models.Comment, chan bool) {
c := make(chan *models.Comment, 25)
stop := make(chan bool, 1)
go func() {
for {
stop <- false
un, _ := r.Me().ListUnreadMessages()
un = onlyMentions(un)
for _, v := range un {
// Only process comment replies and
// mark them as read.
if v.IsMention() {
c <- &v
// You can read the message with
r.Me().ReadMessage(v.GetId())
}
c <- &v
// You can read the message with
r.Me().ReadMessage(v.GetId())
}
time.Sleep(r.Stream.CommentListInterval * time.Second)
if <-stop {
Expand All @@ -60,8 +58,8 @@ func (r *Reddit) StreamMentions() (<-chan *models.Comment, chan bool) {
return c, stop
}

// // c is the channel with all comments
// // stop is the channel to stop the stream. Do stop <- true to stop the loop
// c is the channel with all comments
// stop is the channel to stop the stream. Do stop <- true to stop the loop
func (r *Reddit) StreamComments() (<-chan *models.Comment, chan bool, error) {
name, ttype, err := r.checkType("subreddit", "redditor")
if err != nil {
Expand Down Expand Up @@ -209,3 +207,23 @@ func (r *Reddit) streamRedditorSubmissions(redditor string) (<-chan *models.Post
}()
return c, stop, nil
}

func onlyMentions(comments []models.Comment) []models.Comment {
newComments := make([]models.Comment, 0, 8)
for _, v := range comments {
if v.IsMention() {
newComments = append(newComments, v)
}
}
return newComments
}

func onlyReplies(comments []models.Comment) []models.Comment {
newComments := make([]models.Comment, 0, 8)
for _, v := range comments {
if v.IsCommentReply() {
newComments = append(newComments, v)
}
}
return newComments
}

0 comments on commit 6a24e2a

Please sign in to comment.