Skip to content

Commit

Permalink
livenote: support pin and expiry time
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Nov 11, 2024
1 parent c289c2d commit 0b2fdd4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
40 changes: 39 additions & 1 deletion pkg/livenote/livenote.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package livenote

import "sync"
import (
"sync"
"time"
)

type Object interface {
ObjectID() string
Expand All @@ -13,9 +16,15 @@ type LiveNote struct {

ChannelID string `json:"channelId"`

Pin bool `json:"pin"`

TimeToLive time.Duration `json:"timeToLive"`

Object Object

cachedObjID string

postedTime time.Time
}

func NewLiveNote(object Object) *LiveNote {
Expand All @@ -33,6 +42,14 @@ func (n *LiveNote) ObjectID() string {
return n.cachedObjID
}

func (n *LiveNote) SetTimeToLive(du time.Duration) {
n.TimeToLive = du
}

func (n *LiveNote) SetPostedTime(tt time.Time) {
n.postedTime = tt
}

func (n *LiveNote) SetObject(object Object) {
n.Object = object
}
Expand All @@ -45,6 +62,19 @@ func (n *LiveNote) SetChannelID(channelID string) {
n.ChannelID = channelID
}

func (n *LiveNote) SetPin(enabled bool) {
n.Pin = enabled
}

func (n *LiveNote) IsExpired(now time.Time) bool {
if n.postedTime.IsZero() || n.TimeToLive == 0 {
return false
}

expiryTime := n.postedTime.Add(n.TimeToLive)
return now.After(expiryTime)
}

type Pool struct {
notes map[string]*LiveNote
mu sync.Mutex
Expand All @@ -64,6 +94,10 @@ func (p *Pool) Get(obj Object) *LiveNote {

for _, note := range p.notes {
if note.ObjectID() == objID {
if note.IsExpired(time.Now()) {
return nil
}

return note
}
}
Expand All @@ -79,6 +113,10 @@ func (p *Pool) Update(obj Object) *LiveNote {

for _, note := range p.notes {
if note.ObjectID() == objID {
if note.IsExpired(time.Now()) {
break
}

// update the object inside the note
note.SetObject(obj)
return note
Expand Down
20 changes: 20 additions & 0 deletions pkg/livenote/options.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package livenote

import "time"

type Option interface{}

type OptionCompare struct {
Expand Down Expand Up @@ -29,3 +31,21 @@ func Comment(text string, users ...string) *OptionComment {
Users: users,
}
}

type OptionTimeToLive struct {
Duration time.Duration
}

func TimeToLive(du time.Duration) *OptionTimeToLive {
return &OptionTimeToLive{Duration: du}
}

type OptionPin struct {
Value bool
}

func Pin(value bool) *OptionPin {
return &OptionPin{
Value: value,
}
}
25 changes: 25 additions & 0 deletions pkg/notifier/slacknotifier/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func (t *notifyTask) addMsgOption(opts ...slack.MsgOption) {
// To use this notifier, you need to setup the slack app permissions:
// - channels:read
// - chat:write
//
// When using "pins", you will need permission: "pins:write"
type Notifier struct {
ctx context.Context
cancel context.CancelFunc
Expand Down Expand Up @@ -236,6 +238,9 @@ func (n *Notifier) PostLiveNote(obj livenote.Object, opts ...livenote.Option) er
var commentHandles []string
var comments []string
var shouldCompare bool
var shouldPin bool
var ttl time.Duration = 0

for _, opt := range opts {
switch val := opt.(type) {
case *livenote.OptionOneTimeMention:
Expand All @@ -245,6 +250,10 @@ func (n *Notifier) PostLiveNote(obj livenote.Object, opts ...livenote.Option) er
commentHandles = append(commentHandles, val.Users...)
case *livenote.OptionCompare:
shouldCompare = val.Value
case *livenote.OptionPin:
shouldPin = val.Value
case *livenote.OptionTimeToLive:
ttl = val.Duration
}
}

Expand All @@ -260,6 +269,10 @@ func (n *Notifier) PostLiveNote(obj livenote.Object, opts ...livenote.Option) er
note := n.liveNotePool.Update(obj)
curObj = note.Object

if ttl > 0 {
note.SetTimeToLive(ttl)
}

if shouldCompare && prevObj != nil {
diffs, err := dynamic.Compare(curObj, prevObj)
if err != nil {
Expand Down Expand Up @@ -331,6 +344,18 @@ func (n *Notifier) PostLiveNote(obj livenote.Object, opts ...livenote.Option) er

note.SetChannelID(respCh)
note.SetMessageID(respTs)
note.SetPostedTime(time.Now())

if shouldPin {
note.SetPin(true)

if err := n.client.AddPinContext(ctx, respCh, slack.ItemRef{
Channel: respCh,
Timestamp: respTs,
}); err != nil {
log.WithError(err).Warnf("unable to pin the slack message: %s", respTs)
}
}

if len(firstTimeTags) > 0 {
n.queueTask(n.ctx, notifyTask{
Expand Down
6 changes: 5 additions & 1 deletion pkg/strategy/example/livenote/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package livenote

import (
"context"
"time"

"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -66,7 +67,10 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
bbgo.PostLiveNote(&k,
livenote.OneTimeMention(s.UserID),
livenote.Comment("please check the deposit"),
livenote.CompareObject(true))
livenote.CompareObject(true),
livenote.TimeToLive(time.Minute),
// livenote.Pin(true),
)
}

// register our kline event handler
Expand Down

0 comments on commit 0b2fdd4

Please sign in to comment.