mirror of
https://github.com/binwiederhier/ntfy.git
synced 2024-12-28 04:32:30 +01:00
Keep track of lastVisitor to a topic
This commit is contained in:
parent
d686e1ee77
commit
28b654ae27
2 changed files with 37 additions and 11 deletions
|
@ -114,13 +114,15 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
firebaseControlTopic = "~control" // See Android if changed
|
firebaseControlTopic = "~control" // See Android if changed
|
||||||
firebasePollTopic = "~poll" // See iOS if changed
|
firebasePollTopic = "~poll" // See iOS if changed
|
||||||
emptyMessageBody = "triggered" // Used if message body is empty
|
emptyMessageBody = "triggered" // Used if message body is empty
|
||||||
newMessageBody = "New message" // Used in poll requests as generic message
|
newMessageBody = "New message" // Used in poll requests as generic message
|
||||||
defaultAttachmentMessage = "You received a file: %s" // Used if message body is empty, and there is an attachment
|
defaultAttachmentMessage = "You received a file: %s" // Used if message body is empty, and there is an attachment
|
||||||
encodingBase64 = "base64" // Used mainly for binary UnifiedPush messages
|
encodingBase64 = "base64" // Used mainly for binary UnifiedPush messages
|
||||||
jsonBodyBytesLimit = 16384
|
jsonBodyBytesLimit = 16384
|
||||||
|
subscriberBilledTopicPrefix = "up_"
|
||||||
|
subscriberBilledValidity = 12 * time.Hour
|
||||||
)
|
)
|
||||||
|
|
||||||
// WebSocket constants
|
// WebSocket constants
|
||||||
|
|
|
@ -1,17 +1,21 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"heckel.io/ntfy/log"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"heckel.io/ntfy/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// topic represents a channel to which subscribers can subscribe, and publishers
|
// topic represents a channel to which subscribers can subscribe, and publishers
|
||||||
// can publish a message
|
// can publish a message
|
||||||
type topic struct {
|
type topic struct {
|
||||||
ID string
|
ID string
|
||||||
subscribers map[int]*topicSubscriber
|
subscribers map[int]*topicSubscriber
|
||||||
mu sync.Mutex
|
lastVisitor *visitor
|
||||||
|
lastVisitorExpires time.Time
|
||||||
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type topicSubscriber struct {
|
type topicSubscriber struct {
|
||||||
|
@ -44,10 +48,30 @@ func (t *topic) Subscribe(s subscriber, visitor *visitor, cancel func()) int {
|
||||||
return subscriberID
|
return subscriberID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *topic) Stale() bool {
|
||||||
|
return t.getBillee() == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *topic) getBillee() *visitor {
|
||||||
|
for _, this_subscriber := range t.subscribers {
|
||||||
|
return this_subscriber.visitor
|
||||||
|
}
|
||||||
|
if t.lastVisitor != nil && t.lastVisitorExpires.After(time.Now()) {
|
||||||
|
t.lastVisitor = nil
|
||||||
|
}
|
||||||
|
return t.lastVisitor
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Unsubscribe removes the subscription from the list of subscribers
|
// Unsubscribe removes the subscription from the list of subscribers
|
||||||
func (t *topic) Unsubscribe(id int) {
|
func (t *topic) Unsubscribe(id int) {
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
defer t.mu.Unlock()
|
defer t.mu.Unlock()
|
||||||
|
|
||||||
|
if len(t.subscribers) == 1 {
|
||||||
|
t.lastVisitor = t.subscribers[id].visitor
|
||||||
|
t.lastVisitorExpires = time.Now().Add(subscriberBilledValidity)
|
||||||
|
}
|
||||||
delete(t.subscribers, id)
|
delete(t.subscribers, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue