Use new event API
This commit is contained in:
parent
df5f0c0f65
commit
9368689019
@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
|
"github.com/aerogo/aero/event"
|
||||||
"github.com/aerogo/api"
|
"github.com/aerogo/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -46,10 +47,8 @@ func (item *AnimeListItem) Edit(ctx aero.Context, key string, value reflect.Valu
|
|||||||
|
|
||||||
// Broadcast event to all users so they can reload the activity page if needed.
|
// Broadcast event to all users so they can reload the activity page if needed.
|
||||||
for receiver := range StreamUsers() {
|
for receiver := range StreamUsers() {
|
||||||
receiver.BroadcastEvent(&aero.Event{
|
activityEvent := event.New("activity", receiver.IsFollowing(user.ID))
|
||||||
Name: "activity",
|
receiver.BroadcastEvent(activityEvent)
|
||||||
Data: receiver.IsFollowing(user.ID),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if newEpisodes >= lastActivity.FromEpisode {
|
} else if newEpisodes >= lastActivity.FromEpisode {
|
||||||
|
10
arn/User.go
10
arn/User.go
@ -10,7 +10,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero/event"
|
||||||
"github.com/aerogo/http/client"
|
"github.com/aerogo/http/client"
|
||||||
"github.com/animenotifier/ffxiv"
|
"github.com/animenotifier/ffxiv"
|
||||||
"github.com/animenotifier/notify.moe/arn/autocorrect"
|
"github.com/animenotifier/notify.moe/arn/autocorrect"
|
||||||
@ -61,7 +61,7 @@ type User struct {
|
|||||||
|
|
||||||
eventStreams struct {
|
eventStreams struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
value []*aero.EventStream
|
value []*event.Stream
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,10 +219,8 @@ func (user *User) SendNotification(pushNotification *PushNotification) {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
// Send an event to the user's open tabs
|
// Send an event to the user's open tabs
|
||||||
user.BroadcastEvent(&aero.Event{
|
notificationCount := event.New("notificationCount", userNotifications.CountUnseen())
|
||||||
Name: "notificationCount",
|
user.BroadcastEvent(notificationCount)
|
||||||
Data: userNotifications.CountUnseen(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RealName returns the real name of the user.
|
// RealName returns the real name of the user.
|
||||||
|
@ -1,14 +1,11 @@
|
|||||||
package arn
|
package arn
|
||||||
|
|
||||||
import "github.com/aerogo/aero"
|
import (
|
||||||
|
"github.com/aerogo/aero/event"
|
||||||
// // EventStreams returns the user's active event streams.
|
)
|
||||||
// func (user *User) EventStreams() []*aero.EventStream {
|
|
||||||
// return user.eventStreams
|
|
||||||
// }
|
|
||||||
|
|
||||||
// AddEventStream adds an event stream to the given user.
|
// AddEventStream adds an event stream to the given user.
|
||||||
func (user *User) AddEventStream(stream *aero.EventStream) {
|
func (user *User) AddEventStream(stream *event.Stream) {
|
||||||
user.eventStreams.Lock()
|
user.eventStreams.Lock()
|
||||||
defer user.eventStreams.Unlock()
|
defer user.eventStreams.Unlock()
|
||||||
|
|
||||||
@ -17,7 +14,7 @@ func (user *User) AddEventStream(stream *aero.EventStream) {
|
|||||||
|
|
||||||
// RemoveEventStream removes an event stream from the given user
|
// RemoveEventStream removes an event stream from the given user
|
||||||
// and returns true if it was removed, otherwise false.
|
// and returns true if it was removed, otherwise false.
|
||||||
func (user *User) RemoveEventStream(stream *aero.EventStream) bool {
|
func (user *User) RemoveEventStream(stream *event.Stream) bool {
|
||||||
user.eventStreams.Lock()
|
user.eventStreams.Lock()
|
||||||
defer user.eventStreams.Unlock()
|
defer user.eventStreams.Unlock()
|
||||||
|
|
||||||
@ -32,14 +29,14 @@ func (user *User) RemoveEventStream(stream *aero.EventStream) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BroadcastEvent sends the given event to all event streams for the given user.
|
// BroadcastEvent sends the given event to all event streams for the given user.
|
||||||
func (user *User) BroadcastEvent(event *aero.Event) {
|
func (user *User) BroadcastEvent(evt *event.Event) {
|
||||||
user.eventStreams.Lock()
|
user.eventStreams.Lock()
|
||||||
defer user.eventStreams.Unlock()
|
defer user.eventStreams.Unlock()
|
||||||
|
|
||||||
for _, stream := range user.eventStreams.value {
|
for _, stream := range user.eventStreams.value {
|
||||||
// Non-blocking send because we don't know if our listeners are still active.
|
// Non-blocking send because we don't know if our listeners are still active.
|
||||||
select {
|
select {
|
||||||
case stream.Events <- event:
|
case stream.Events <- evt:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
|
"github.com/aerogo/aero/event"
|
||||||
"github.com/aerogo/mirror"
|
"github.com/aerogo/mirror"
|
||||||
"github.com/akyoto/color"
|
"github.com/akyoto/color"
|
||||||
"github.com/animenotifier/kitsu"
|
"github.com/animenotifier/kitsu"
|
||||||
@ -201,9 +202,9 @@ func DateToSeason(date time.Time) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// BroadcastEvent sends the given event to the event streams of all users.
|
// BroadcastEvent sends the given event to the event streams of all users.
|
||||||
func BroadcastEvent(event *aero.Event) {
|
func BroadcastEvent(evt *event.Event) {
|
||||||
for user := range StreamUsers() {
|
for user := range StreamUsers() {
|
||||||
user.BroadcastEvent(event)
|
user.BroadcastEvent(evt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
go.mod
2
go.mod
@ -6,7 +6,7 @@ require (
|
|||||||
cloud.google.com/go v0.97.0 // indirect
|
cloud.google.com/go v0.97.0 // indirect
|
||||||
github.com/PuerkitoBio/goquery v1.8.0 // indirect
|
github.com/PuerkitoBio/goquery v1.8.0 // indirect
|
||||||
github.com/StackExchange/wmi v1.2.1 // indirect
|
github.com/StackExchange/wmi v1.2.1 // indirect
|
||||||
github.com/aerogo/aero v1.3.58
|
github.com/aerogo/aero v1.3.59
|
||||||
github.com/aerogo/api v0.2.3
|
github.com/aerogo/api v0.2.3
|
||||||
github.com/aerogo/crawler v0.2.5
|
github.com/aerogo/crawler v0.2.5
|
||||||
github.com/aerogo/flow v0.1.5
|
github.com/aerogo/flow v0.1.5
|
||||||
|
4
go.sum
4
go.sum
@ -54,8 +54,8 @@ github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDO
|
|||||||
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
|
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
|
||||||
github.com/aerogo/aero v1.3.29/go.mod h1:nHuG9q4VrzH7ZtiE1TEIt9Lazp/w3WpC3nqAKREkg0Q=
|
github.com/aerogo/aero v1.3.29/go.mod h1:nHuG9q4VrzH7ZtiE1TEIt9Lazp/w3WpC3nqAKREkg0Q=
|
||||||
github.com/aerogo/aero v1.3.30/go.mod h1:iAzV2JRnKAFOo/c+4KIgf35JGS82qMU4tI06ocHeRrk=
|
github.com/aerogo/aero v1.3.30/go.mod h1:iAzV2JRnKAFOo/c+4KIgf35JGS82qMU4tI06ocHeRrk=
|
||||||
github.com/aerogo/aero v1.3.58 h1:LmQ8TYRwIaiMmUQlfD85bKNb+2LRm7ueXIoaw1EClLs=
|
github.com/aerogo/aero v1.3.59 h1:5yu+kk/uIXAXADKSLCFKhxAzThCehvpbF6gst+G32Fw=
|
||||||
github.com/aerogo/aero v1.3.58/go.mod h1:ehwj+mb117xQRTvp11jlnrRNPgbcYL6s6aBk9wbIZ0o=
|
github.com/aerogo/aero v1.3.59/go.mod h1:ehwj+mb117xQRTvp11jlnrRNPgbcYL6s6aBk9wbIZ0o=
|
||||||
github.com/aerogo/api v0.2.3 h1:REQR3a6WXzaHpNSF9NCjj4HjvSQdcHrMWGnp/xIpCh4=
|
github.com/aerogo/api v0.2.3 h1:REQR3a6WXzaHpNSF9NCjj4HjvSQdcHrMWGnp/xIpCh4=
|
||||||
github.com/aerogo/api v0.2.3/go.mod h1:cClK+FXNc0IRGdDxAH5XmtibBxwUXCM2lLKMF7jKB+8=
|
github.com/aerogo/api v0.2.3/go.mod h1:cClK+FXNc0IRGdDxAH5XmtibBxwUXCM2lLKMF7jKB+8=
|
||||||
github.com/aerogo/cluster v0.1.8 h1:N/jU2t7kQfKjXzQIArBQvtNkJCiaP9+jgdEid3P2Omc=
|
github.com/aerogo/cluster v0.1.8 h1:N/jU2t7kQfKjXzQIArBQvtNkJCiaP9+jgdEid3P2Omc=
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
|
"github.com/aerogo/aero/event"
|
||||||
"github.com/animenotifier/notify.moe/arn"
|
"github.com/animenotifier/notify.moe/arn"
|
||||||
"github.com/animenotifier/notify.moe/assets"
|
"github.com/animenotifier/notify.moe/assets"
|
||||||
)
|
)
|
||||||
@ -39,10 +40,7 @@ func MarkNotificationsAsSeen(ctx aero.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update the counter on all clients
|
// Update the counter on all clients
|
||||||
user.BroadcastEvent(&aero.Event{
|
user.BroadcastEvent(event.New("notificationCount", 0))
|
||||||
Name: "notificationCount",
|
|
||||||
Data: 0,
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
|
"github.com/aerogo/aero/event"
|
||||||
"github.com/animenotifier/notify.moe/arn"
|
"github.com/animenotifier/notify.moe/arn"
|
||||||
"github.com/animenotifier/notify.moe/components/css"
|
"github.com/animenotifier/notify.moe/components/css"
|
||||||
"github.com/animenotifier/notify.moe/components/js"
|
"github.com/animenotifier/notify.moe/components/js"
|
||||||
@ -22,35 +23,29 @@ func Events(ctx aero.Context) error {
|
|||||||
return ctx.Error(http.StatusUnauthorized, "Not logged in")
|
return ctx.Error(http.StatusUnauthorized, "Not logged in")
|
||||||
}
|
}
|
||||||
|
|
||||||
stream := aero.NewEventStream()
|
stream := event.NewStream()
|
||||||
user.AddEventStream(stream)
|
user.AddEventStream(stream)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer user.RemoveEventStream(stream)
|
defer user.RemoveEventStream(stream)
|
||||||
|
|
||||||
// Send the ETag for the scripts
|
// Send the ETag for the scripts
|
||||||
stream.Events <- &aero.Event{
|
stream.Events <- event.New("etag", struct {
|
||||||
Name: "etag",
|
URL string `json:"url"`
|
||||||
Data: struct {
|
ETag string `json:"etag"`
|
||||||
URL string `json:"url"`
|
}{
|
||||||
ETag string `json:"etag"`
|
URL: "/scripts",
|
||||||
}{
|
ETag: scriptsETag,
|
||||||
URL: "/scripts",
|
})
|
||||||
ETag: scriptsETag,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send the ETag for the styles
|
// Send the ETag for the styles
|
||||||
stream.Events <- &aero.Event{
|
stream.Events <- event.New("etag", struct {
|
||||||
Name: "etag",
|
URL string `json:"url"`
|
||||||
Data: struct {
|
ETag string `json:"etag"`
|
||||||
URL string `json:"url"`
|
}{
|
||||||
ETag string `json:"etag"`
|
URL: "/styles",
|
||||||
}{
|
ETag: stylesETag,
|
||||||
URL: "/styles",
|
})
|
||||||
ETag: stylesETag,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait until the user closes the tab or disconnects
|
// Wait until the user closes the tab or disconnects
|
||||||
<-stream.Closed
|
<-stream.Closed
|
||||||
|
Loading…
Reference in New Issue
Block a user