Skip to content

Commit

Permalink
[marker] Add marker info to actor result
Browse files Browse the repository at this point in the history
in order to enable rules to access i.e. the position of the marker

Signed-off-by: Knut Ahlers <[email protected]>
  • Loading branch information
Luzifer committed Sep 11, 2024
1 parent e0a8ce3 commit 740a71a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/content/configuration/actors.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Triggers the creation of a Clip from the given channel owned by the creator (sub
## Create Marker
Creates a marker on the currently running stream of the given channel. The marker will be created on behalf of the channel owner and requires matching scope.
Creates a marker on the currently running stream of the given channel. The marker will be created on behalf of the channel owner and requires matching scope. (Subsequent actions can use variable `marker` to access marker details.)

```yaml
- type: marker
Expand Down
7 changes: 5 additions & 2 deletions internal/actors/marker/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Register(args plugins.RegistrationArguments) error {
args.RegisterActor(actorName, func() plugins.Actor { return &actor{} })

args.RegisterActorDocumentation(plugins.ActionDocumentation{
Description: "Creates a marker on the currently running stream of the given channel. The marker will be created on behalf of the channel owner and requires matching scope.",
Description: "Creates a marker on the currently running stream of the given channel. The marker will be created on behalf of the channel owner and requires matching scope. (Subsequent actions can use variable `marker` to access marker details.)",
Name: "Create Marker",
Type: actorName,
Fields: []plugins.ActionDocumentationField{
Expand Down Expand Up @@ -86,10 +86,13 @@ func (actor) Execute(_ *irc.Client, m *irc.Message, r *plugins.Rule, eventData *
return false, fmt.Errorf("getting Twitch client for %q: %w", channel, err)
}

if err = tc.CreateStreamMarker(context.TODO(), description); err != nil {
var marker twitch.StreamMarkerInfo
if marker, err = tc.CreateStreamMarker(context.TODO(), description); err != nil {
return false, fmt.Errorf("creating marker: %w", err)
}

eventData.Set("marker", marker)

return false, nil
}

Expand Down
23 changes: 18 additions & 5 deletions pkg/twitch/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ type (
TagIds []string `json:"tag_ids"` //revive:disable-line:var-naming // Disabled to prevent breaking change
IsMature bool `json:"is_mature"`
}

// StreamMarkerInfo contains information about a marker on a stream
StreamMarkerInfo struct {
ID int64 `json:"id"`
CreatedAt time.Time `json:"created_at"`
Description string `json:"description"`
PositionSeconds int64 `json:"position_seconds"`
}
)

// ErrNoStreamsFound allows to differntiate between an HTTP error and
Expand All @@ -38,12 +46,12 @@ var ErrNoStreamsFound = errors.New("no streams found")
// CreateStreamMarker creates a marker for the currently running stream.
// The stream must be live, no VoD, no upload and no re-run.
// The description may be up to 140 chars and can be omitted.
func (c *Client) CreateStreamMarker(ctx context.Context, description string) (err error) {
func (c *Client) CreateStreamMarker(ctx context.Context, description string) (marker StreamMarkerInfo, err error) {
body := new(bytes.Buffer)

userID, _, err := c.GetAuthorizedUser(ctx)
if err != nil {
return fmt.Errorf("getting ID for current user: %w", err)
return marker, fmt.Errorf("getting ID for current user: %w", err)
}

if err = json.NewEncoder(body).Encode(struct {
Expand All @@ -53,20 +61,25 @@ func (c *Client) CreateStreamMarker(ctx context.Context, description string) (er
UserID: userID,
Description: description,
}); err != nil {
return fmt.Errorf("encoding payload: %w", err)
return marker, fmt.Errorf("encoding payload: %w", err)
}

var payload struct {
Data []StreamMarkerInfo `json:"data"`
}

if err := c.Request(ctx, ClientRequestOpts{
AuthType: AuthTypeBearerToken,
Body: body,
Method: http.MethodPost,
OKStatus: http.StatusOK,
Out: &payload,
URL: "https://api.twitch.tv/helix/streams/markers",
}); err != nil {
return fmt.Errorf("creating marker: %w", err)
return marker, fmt.Errorf("creating marker: %w", err)
}

return nil
return payload.Data[0], nil
}

// GetCurrentStreamInfo returns the StreamInfo of the currently running
Expand Down

0 comments on commit 740a71a

Please sign in to comment.