github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/notify/scraper.go (about)

     1  package notify
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
     7  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/uniq"
     8  )
     9  
    10  const (
    11  	MessageChunkWritten Message = "chunkWritten"
    12  	MessageStageUpdated Message = "stageUpdated"
    13  	MessageAppearance   Message = "appearance"
    14  )
    15  
    16  type NotificationPayloadAppearance struct {
    17  	Address string `json:"address"`
    18  	// We use string for block number to ensure it's never
    19  	// too big
    20  	BlockNumber      string `json:"blockNumber"`
    21  	TransactionIndex uint32 `json:"txid"`
    22  }
    23  
    24  func (n *NotificationPayloadAppearance) FromString(s string) (err error) {
    25  	var bn uint32
    26  	_, err = fmt.Sscanf(
    27  		s,
    28  		uniq.AppearanceFmt,
    29  		&n.Address,
    30  		&bn,
    31  		&n.TransactionIndex,
    32  	)
    33  
    34  	n.BlockNumber = fmt.Sprint(bn)
    35  	return
    36  }
    37  
    38  func NewChunkWrittenNotification(meta *types.MetaData, chunk string) *Notification[string] {
    39  	return &Notification[string]{
    40  		Msg:     MessageChunkWritten,
    41  		Meta:    meta,
    42  		Payload: chunk,
    43  	}
    44  }
    45  
    46  func NewStageUpdatedNotification(meta *types.MetaData, chunkRange string) *Notification[string] {
    47  	return &Notification[string]{
    48  		Msg:     MessageChunkWritten,
    49  		Meta:    meta,
    50  		Payload: chunkRange,
    51  	}
    52  }
    53  
    54  func NewAppearanceNotification(meta *types.MetaData, appearances []NotificationPayloadAppearance) *Notification[[]NotificationPayloadAppearance] {
    55  	return &Notification[[]NotificationPayloadAppearance]{
    56  		Msg:     MessageChunkWritten,
    57  		Meta:    meta,
    58  		Payload: appearances,
    59  	}
    60  }
    61  
    62  type NotificationPayloadChunkWritten struct {
    63  	Cid    string `json:"cid"`
    64  	Range  string `json:"range"`
    65  	Author string `json:"author"`
    66  }