cwtch/peer/hooks.go

53 lines
1.6 KiB
Go
Raw Permalink Normal View History

2023-01-05 21:52:43 +00:00
package peer
import (
"cwtch.im/cwtch/event"
"cwtch.im/cwtch/model"
"cwtch.im/cwtch/model/attr"
"cwtch.im/cwtch/settings"
2023-01-05 21:52:43 +00:00
)
type ProfileHooks interface {
2023-01-25 20:34:58 +00:00
// EventsToRegister returns a set of events that the extension is interested hooking
EventsToRegister() []event.Type
2023-01-05 21:52:43 +00:00
2023-01-25 20:34:58 +00:00
// ExperimentsToRegister returns a set of experiments that the extension is interested in being notified about
ExperimentsToRegister() []string
2023-01-05 21:52:43 +00:00
// OnEvent is called whenever an event Registered with RegisterEvents is called
OnEvent(event event.Event, profile CwtchPeer)
// OnContactRequestValue is Hooked when a contact sends a request for the given path
OnContactRequestValue(profile CwtchPeer, conversation model.Conversation, eventID string, path attr.ScopedZonedPath)
// OnContactReceiveValue is Hooked after a profile receives a response to a Get/Val Request
OnContactReceiveValue(profile CwtchPeer, conversation model.Conversation, path attr.ScopedZonedPath, value string, exists bool)
// NotifySettingsUpdate allow profile hooks to access configs e.g. download folder
NotifySettingsUpdate(settings settings.GlobalSettings)
2023-01-05 21:52:43 +00:00
}
type ProfileHook struct {
extension ProfileHooks
events map[event.Type]bool
experiments map[string]bool
}
func ConstructHook(extension ProfileHooks) ProfileHook {
events := make(map[event.Type]bool)
2023-04-17 19:05:02 +00:00
for _, e := range extension.EventsToRegister() {
events[e] = true
2023-01-05 21:52:43 +00:00
}
experiments := make(map[string]bool)
2023-01-25 20:34:58 +00:00
for _, experiment := range extension.ExperimentsToRegister() {
2023-01-05 21:52:43 +00:00
experiments[experiment] = true
}
return ProfileHook{
extension,
events,
experiments,
}
}