Skip to content

Commit 6a24e2a

Browse files
committed
Fixed the broken mentions channel.
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 <thecsw@ku.edu>
1 parent da7b64c commit 6a24e2a

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

streaming.go

+34-16
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@ import (
66
"github.com/thecsw/mira/models"
77
)
88

9-
// // c is the channel with all unread messages
10-
// // stop is the channel to stop the stream. Do stop <- true to stop the loop
9+
// c is the channel with all unread messages
10+
// stop is the channel to stop the stream. Do stop <- true to stop the loop
1111
func (r *Reddit) StreamCommentReplies() (<-chan *models.Comment, chan bool) {
1212
c := make(chan *models.Comment, 25)
1313
stop := make(chan bool, 1)
1414
go func() {
1515
for {
1616
stop <- false
1717
un, _ := r.Me().ListUnreadMessages()
18+
un = onlyReplies(un)
1819
for _, v := range un {
1920
// Only process comment replies and
2021
// mark them as read.
21-
if v.IsCommentReply() {
22-
c <- &v
23-
// You can read the message with
24-
r.Me().ReadMessage(v.GetId())
25-
}
22+
c <- &v
23+
// You can read the message with
24+
r.Me().ReadMessage(v.GetId())
2625
}
2726
time.Sleep(r.Stream.CommentListInterval * time.Second)
2827
if <-stop {
@@ -33,23 +32,22 @@ func (r *Reddit) StreamCommentReplies() (<-chan *models.Comment, chan bool) {
3332
return c, stop
3433
}
3534

36-
// // c is the channel with all unread messages
37-
// // stop is the channel to stop the stream. Do stop <- true to stop the loop
35+
// c is the channel with all unread messages
36+
// stop is the channel to stop the stream. Do stop <- true to stop the loop
3837
func (r *Reddit) StreamMentions() (<-chan *models.Comment, chan bool) {
3938
c := make(chan *models.Comment, 25)
4039
stop := make(chan bool, 1)
4140
go func() {
4241
for {
4342
stop <- false
4443
un, _ := r.Me().ListUnreadMessages()
44+
un = onlyMentions(un)
4545
for _, v := range un {
4646
// Only process comment replies and
4747
// mark them as read.
48-
if v.IsMention() {
49-
c <- &v
50-
// You can read the message with
51-
r.Me().ReadMessage(v.GetId())
52-
}
48+
c <- &v
49+
// You can read the message with
50+
r.Me().ReadMessage(v.GetId())
5351
}
5452
time.Sleep(r.Stream.CommentListInterval * time.Second)
5553
if <-stop {
@@ -60,8 +58,8 @@ func (r *Reddit) StreamMentions() (<-chan *models.Comment, chan bool) {
6058
return c, stop
6159
}
6260

63-
// // c is the channel with all comments
64-
// // stop is the channel to stop the stream. Do stop <- true to stop the loop
61+
// c is the channel with all comments
62+
// stop is the channel to stop the stream. Do stop <- true to stop the loop
6563
func (r *Reddit) StreamComments() (<-chan *models.Comment, chan bool, error) {
6664
name, ttype, err := r.checkType("subreddit", "redditor")
6765
if err != nil {
@@ -209,3 +207,23 @@ func (r *Reddit) streamRedditorSubmissions(redditor string) (<-chan *models.Post
209207
}()
210208
return c, stop, nil
211209
}
210+
211+
func onlyMentions(comments []models.Comment) []models.Comment {
212+
newComments := make([]models.Comment, 0, 8)
213+
for _, v := range comments {
214+
if v.IsMention() {
215+
newComments = append(newComments, v)
216+
}
217+
}
218+
return newComments
219+
}
220+
221+
func onlyReplies(comments []models.Comment) []models.Comment {
222+
newComments := make([]models.Comment, 0, 8)
223+
for _, v := range comments {
224+
if v.IsCommentReply() {
225+
newComments = append(newComments, v)
226+
}
227+
}
228+
return newComments
229+
}

0 commit comments

Comments
 (0)