Skip to content

Commit

Permalink
Merge branch 'main' into enhancement/config-path-issue-33
Browse files Browse the repository at this point in the history
  • Loading branch information
lleadbet authored Aug 14, 2021
2 parents 3c6528c + de5d1ff commit ada48aa
Show file tree
Hide file tree
Showing 71 changed files with 3,216 additions and 188 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ twitch-cli
*.html

# Editor configs
.vsvode/
.vscode/
.idea/

# junk files
Expand Down
5 changes: 0 additions & 5 deletions .vscode/settings.json

This file was deleted.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ The CLI currently supports the following products:
- [api](./docs/api.md)
- [configure](./docs/configure.md)
- [event](docs/event.md)
- [mock-api](docs/mock-api.md)
- [token](docs/token.md)
- [version](docs/version.md)

Expand Down
3 changes: 3 additions & 0 deletions cmd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (
cost int64
count int
description string
gameID string
)

var eventCmd = &cobra.Command{
Expand Down Expand Up @@ -90,6 +91,7 @@ func init() {
triggerCmd.Flags().StringVarP(&itemName, "item-name", "n", "", "Manually set the name of the event payload item (for example the reward ID in redemption events). For stream events, this is the game title.")
triggerCmd.Flags().Int64VarP(&cost, "cost", "C", 0, "Amount of bits or channel points redeemed/used in the event.")
triggerCmd.Flags().StringVarP(&description, "description", "d", "", "Title the stream should be updated with.")
triggerCmd.Flags().StringVarP(&gameID, "game-id", "G", "", "Sets the game/category ID for applicable events.")

// retrigger flags
retriggerCmd.Flags().StringVarP(&forwardAddress, "forward-address", "F", "", "Forward address for mock event.")
Expand Down Expand Up @@ -134,6 +136,7 @@ func triggerCmdRun(cmd *cobra.Command, args []string) {
Cost: cost,
Description: description,
ItemName: itemName,
GameID: gameID,
})

if err != nil {
Expand Down
152 changes: 89 additions & 63 deletions docs/event.md

Large diffs are not rendered by default.

21 changes: 8 additions & 13 deletions docs/mock-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ The `mock-api` product has two primary functions. The first is to generate so-ca
* Teams
* Users

The second is the actual server used to mock the endpoints. In the next iteration, you will be able to edit these and add further ones manually (for example, making a user with specific attributes), but for the beta we won't be providing this functionality as the current `generate` feature will make all of these (and more),
The second is the actual server used to mock the endpoints. In the next iteration, you will be able to edit these and add further ones manually (for example, making a user with specific attributes), but for the beta we won't be providing this functionality as the current `generate` feature will make all of these (and more).

As of the 1.1 release, this product is in an **open beta** and any bugs should be filed via GitHub Issues. Given the breadth of the tool, it is likely you may run across oddities; please fill out an issue if that is the case.

## generate

Expand All @@ -40,18 +41,12 @@ None.

## start

The `start` function starts a new mock server for use with testing functionality. Currently, this replicates a large majority of the current API endpoints on the new API, but are ommitting:

* GET /analytics/extensions
* GET /analytics/games
* GET /extensions/transactions
* POST /eventsub/subscriptions
* DELETE /eventsub/subscriptions
* GET /eventsub/subscriptions
* GET /users/extensions/list
* GET /users/extensions
* PUT /users/extensions
* GET /webhooks/subscriptions
The `start` function starts a new mock server for use with testing functionality. Currently, this replicates a large majority of the current API endpoints on the new API, but are omitting:

* Extensions endpoints
* Code entitlement endpoints
* Websub endpoints
* EventSub endpoints

For many of these, we are exploring how to better integrate this with existing features (for example, allowing events to be triggered on unit creation or otherwise), and for others, the value is minimal compared to the docs. All other endpoints should be currently supported, however it is possible to be out of date- if so, [please raise an issue](https://github.com/twitchdev/twitch-cli/issues).

Expand Down
18 changes: 14 additions & 4 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ func NewRequest(method string, path string, queryParameters []string, body []byt
}

q := u.Query()
for _, param := range queryParameters {
value := strings.Split(param, "=")
q.Add(value[0], value[1])
for _, paramStr := range queryParameters {
var value string
param := strings.Split(paramStr, "=")
if len(param) == 2 {
value = param[1]
}
q.Add(param[0], value)
}

if cursor != "" {
Expand Down Expand Up @@ -97,13 +101,19 @@ func NewRequest(method string, path string, queryParameters []string, body []byt
return
}

data.Template = apiResponse.Template

if resp.StatusCode > 299 || resp.StatusCode < 200 {
data = apiResponse
break
}

d := data.Data.([]interface{})
data.Data = append(d, apiResponse.Data)
if strings.Contains(path, "schedule") || apiResponse.Data == nil {
data.Data = append(d, apiResponse.Data)
} else {
data.Data = append(d, apiResponse.Data.([]interface{})...)
}

if apiResponse.Pagination == nil || *&apiResponse.Pagination.Cursor == "" {
break
Expand Down
17 changes: 16 additions & 1 deletion internal/database/_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ create table drops_entitlements(
benefit_id text not null,
timestamp text not null,
user_id text not null,
game_id text not null,
game_id text not null,
status text not null default 'CLAIMED',
foreign key (user_id) references users(id),
foreign key (game_id) references categories(id)
);
Expand Down Expand Up @@ -289,3 +290,17 @@ create table clips (
foreign key (broadcaster_id) references users(id),
foreign key (creator_id) references users(id)
);
create table stream_schedule(
id text not null primary key,
broadcaster_id text not null,
starttime text not null,
endtime text not null,
timezone text not null,
is_vacation boolean not null default false,
is_recurring boolean not null default false,
is_canceled boolean not null default false,
title text,
category_id text,
foreign key (broadcaster_id) references users(id),
foreign key (category_id) references categories(id)
);
2 changes: 1 addition & 1 deletion internal/database/categories.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type Category struct {
ID string `db:"id" json:"id"`
Name string `db:"category_name" json:"name"`
BoxartURL string `json:"boxart_url"`
BoxartURL string `json:"box_art_url"`
ViewerCount int `db:"vc" json:"-"`
}

Expand Down
6 changes: 6 additions & 0 deletions internal/database/drops.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type DropsEntitlement struct {
BenefitID string `db:"benefit_id" json:"benefit_id"`
GameID string `db:"game_id" json:"game_id"`
Timestamp string `db:"timestamp" json:"timestamp"`
Status string `db:"status" json:"fulfillment_status"`
}

func (q *Query) GetDropsEntitlements(de DropsEntitlement) (*DBResponse, error) {
Expand Down Expand Up @@ -51,3 +52,8 @@ func (q *Query) InsertDropsEntitlement(d DropsEntitlement) error {
_, err := q.DB.NamedExec(stmt, d)
return err
}

func (q *Query) UpdateDropsEntitlement(d DropsEntitlement) error {
_, err := q.DB.NamedExec(generateUpdateSQL("drops_entitlements", []string{"id"}, d), d)
return err
}
8 changes: 6 additions & 2 deletions internal/database/init.go

Large diffs are not rendered by default.

59 changes: 43 additions & 16 deletions internal/database/moderation.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ModeratorAction struct {
ID string `db:"id" json:"id"`
EventType string `db:"event_type" json:"event_type"`
EventTimestamp string `db:"event_timestamp" json:"event_timestamp"`
EventVersion string `db:"event_version" json:"event_version"`
EventVersion string `db:"event_version" json:"version"`
ModeratorActionEvent `json:"event_data"`
}

Expand All @@ -40,27 +40,35 @@ type ModeratorActionEvent struct {
}

type BanActionEvent struct {
BroadcasterID string `db:"broadcaster_id" json:"broadcaster_id"`
BroadcasterLogin string `db:"broadcaster_login" json:"broadcaster_login"`
BroadcasterName string `db:"broadcaster_name" json:"broadcaster_name"`
UserID string `db:"user_id" json:"user_id"`
UserLogin string `db:"user_login" json:"user_login"`
UserName string `db:"user_name" json:"user_name"`
ExpiresAt *string `db:"expires_at" json:"expires_at"`
BroadcasterID string `db:"broadcaster_id" json:"broadcaster_id"`
BroadcasterLogin string `db:"broadcaster_login" json:"broadcaster_login"`
BroadcasterName string `db:"broadcaster_name" json:"broadcaster_name"`
UserID string `db:"user_id" json:"user_id"`
UserLogin string `db:"user_login" json:"user_login"`
UserName string `db:"user_name" json:"user_name"`
ExpiresAt *string `db:"expires_at" json:"expires_at"`
Reason string `json:"reason"`
ModeratorID string `json:"moderator_id"`
ModeratorUserLogin string `json:"moderator_login"`
ModeratorUserName string `json:"moderator_name"`
}

type BanEvent struct {
ID string `db:"id" json:"id"`
EventType string `db:"event_type" json:"event_type"`
EventTimestamp string `db:"event_timestamp" json:"event_timestamp"`
EventVersion string `db:"event_version" json:"event_version"`
EventVersion string `db:"event_version" json:"version"`
BanActionEvent `json:"event_data"`
}
type Ban struct {
UserID string `db:"user_id" json:"user_id"`
UserLogin string `db:"user_login" json:"user_login"`
UserName string `db:"user_name" json:"user_name"`
ExpiresAt *string `db:"expires_at" json:"expires_at"`
UserID string `db:"user_id" json:"user_id"`
UserLogin string `db:"user_login" json:"user_login"`
UserName string `db:"user_name" json:"user_name"`
ExpiresAt *string `db:"expires_at" json:"expires_at"`
Reason string `json:"reason"`
ModeratorID string `json:"moderator_id"`
ModeratorUserLogin string `json:"moderator_login"`
ModeratorUserName string `json:"moderator_name"`
}

var es = ""
Expand Down Expand Up @@ -105,7 +113,7 @@ func (q *Query) AddModerator(p UserRequestParams) error {

tx := q.DB.MustBegin()
tx.NamedExec(stmt, p)
tx.NamedExec(`INSERT INTO moderator_actions VALUES(:id, :event_type, :event_timestamp, :event_version, :broadcaster_id, :user_id)`, ma)
tx.NamedExec(`INSERT INTO moderator_actions VALUES(:id, :event_timestamp, :event_type, :event_version, :broadcaster_id, :user_id)`, ma)
return tx.Commit()
}

Expand Down Expand Up @@ -148,7 +156,7 @@ func (q *Query) RemoveModerator(broadcaster string, user string) error {

tx := q.DB.MustBegin()
tx.Exec(`delete from moderators where broadcaster_id=$1 and user_id=$2`, broadcaster, user)
tx.NamedExec(`INSERT INTO moderator_actions VALUES(:id, :event_type, :event_timestamp, :event_version, :broadcaster_id, :user_id)`, ma)
tx.NamedExec(`INSERT INTO moderator_actions VALUES(:id, :event_timestamp, :event_type, :event_version, :broadcaster_id, :user_id)`, ma)
return tx.Commit()
}

Expand All @@ -170,7 +178,7 @@ func (q *Query) InsertBan(p UserRequestParams) error {

tx := q.DB.MustBegin()
tx.NamedExec(stmt, p)
tx.NamedExec(`INSERT INTO ban_events VALUES(:id, :event_type, :event_timestamp, :event_version, :broadcaster_id, :user_id, :expires_at)`, ma)
tx.NamedExec(`INSERT INTO ban_events VALUES(:id, :event_timestamp, :event_type, :event_version, :broadcaster_id, :user_id, :expires_at)`, ma)
return tx.Commit()
}

Expand All @@ -192,6 +200,7 @@ func (q *Query) GetBans(p UserRequestParams) (*DBResponse, error) {
if b.ExpiresAt == nil {
b.ExpiresAt = &es
}
b.Reason = "CLI ban"
r = append(r, b)
}
dbr := DBResponse{
Expand Down Expand Up @@ -225,9 +234,19 @@ func (q *Query) GetBanEvents(p UserRequestParams) (*DBResponse, error) {
return nil, err
}
es := ""

if b.ExpiresAt == nil {
b.ExpiresAt = &es
}
// shim for https://github.com/twitchdev/twitch-cli/issues/83
_, err = time.Parse(time.RFC3339, b.EventTimestamp)
if err != nil {
ts := b.EventType
b.EventType = b.EventTimestamp
b.EventTimestamp = ts
}

b.Reason = "CLI ban"
r = append(r, b)
}
dbr := DBResponse{
Expand Down Expand Up @@ -260,6 +279,14 @@ func (q *Query) GetModeratorEvents(p UserRequestParams) (*DBResponse, error) {
log.Print(err)
return nil, err
}
// shim for https://github.com/twitchdev/twitch-cli/issues/83
_, err = time.Parse(time.RFC3339, ma.EventTimestamp)
if err != nil {
ts := ma.EventType
ma.EventType = ma.EventTimestamp
ma.EventTimestamp = ts
}

r = append(r, ma)
}
dbr := DBResponse{
Expand Down
Loading

0 comments on commit ada48aa

Please sign in to comment.