Skip to content

Commit

Permalink
Merge pull request #302 from Simply-Stream/fix/mock-api
Browse files Browse the repository at this point in the history
Mock API updates
  • Loading branch information
Xemdo authored Dec 22, 2023
2 parents bca3aec + 0ae4052 commit 90a6af4
Show file tree
Hide file tree
Showing 17 changed files with 381 additions and 383 deletions.
6 changes: 3 additions & 3 deletions internal/database/_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ 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,
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,
Expand Down
8 changes: 4 additions & 4 deletions internal/database/channel_points_redemptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type ChannelPointsRedemption struct {
UserLogin string `db:"user_login" dbi:"false" json:"user_login"`
UserName string `db:"user_name" dbi:"false" json:"user_name"`
UserInput sql.NullString `db:"user_input" json:"-"`
RealUserInput *string `json:"user_input"`
RealUserInput string `json:"user_input"`
RedemptionStatus string `db:"redemption_status" json:"status"`
RedeemedAt string `db:"redeemed_at" json:"redeemed_at"`
RewardID string `db:"reward_id" json:"-"`
Expand All @@ -25,7 +25,7 @@ type ChannelPointsRedemption struct {
type ChannelPointsRedemptionRewardInfo struct {
ID string `dbi:"false" db:"red_id" json:"id" dbs:"red.id"`
Title string `dbi:"false" db:"title" json:"title"`
RewardPrompt string `dbi:"false" db:"reward_prompt" json:"reward_prompt"`
RewardPrompt string `dbi:"false" db:"reward_prompt" json:"prompt"`
Cost int `dbi:"false" db:"cost" json:"cost"`
}

Expand All @@ -50,9 +50,9 @@ func (q *Query) GetChannelPointsRedemption(cpr ChannelPointsRedemption, sort str
if err != nil {
return nil, err
}
red.RealUserInput = &red.UserInput.String
red.RealUserInput = red.UserInput.String
if !red.UserInput.Valid {
red.RealUserInput = nil
red.RealUserInput = ""
}
r = append(r, red)
}
Expand Down
8 changes: 6 additions & 2 deletions internal/database/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/jmoiron/sqlx"
)

const currentVersion = 6
const currentVersion = 7

type migrateMap struct {
SQL string
Expand Down Expand Up @@ -53,6 +53,10 @@ ALTER TABLE users ADD COLUMN content_labels text not null default '';`,
SQL: `DROP TABLE IF EXISTS stream_tags;`,
Message: `Removing deprecated stream_tags from database.`,
},
7: {
SQL: `ALTER TABLE stream_schedule DROP COLUMN timezone;`,
Message: `Removing deprecated stream_schedule.timezone from database`,
},
}

func checkAndUpdate(db sqlx.DB) error {
Expand Down Expand Up @@ -120,7 +124,7 @@ create table predictions ( id text not null primary key, broadcaster_id text not
create table prediction_outcomes ( id text not null primary key, title text not null, users int not null default 0, channel_points int not null default 0, color text not null, prediction_id text not null, foreign key (prediction_id) references predictions(id) );
create table prediction_predictions ( prediction_id text not null, user_id text not null, amount int not null, outcome_id text not null, primary key(prediction_id, user_id), foreign key(user_id) references users(id), foreign key(prediction_id) references predictions(id), foreign key(outcome_id) references prediction_outcomes(id) );
create table clips ( id text not null primary key, broadcaster_id text not null, creator_id text not null, video_id text not null, game_id text not null, title text not null, view_count int default 0, created_at text not null, duration real not null, vod_offset int default 0, 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));
create table stream_schedule( id text not null primary key, broadcaster_id text not null, starttime text not null, endtime 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));
create table chat_settings( broadcaster_id text not null primary key, slow_mode boolean not null default 0, slow_mode_wait_time int not null default 10, follower_mode boolean not null default 0, follower_mode_duration int not null default 60, subscriber_mode boolean not null default 0, emote_mode boolean not null default 0, unique_chat_mode boolean not null default 0, non_moderator_chat_delay boolean not null default 0, non_moderator_chat_delay_duration int not null default 10, shieldmode_is_active boolean not null default 0, shieldmode_moderator_id text not null default '', shieldmode_moderator_login text not null default '', shieldmode_moderator_name text not null default '', shieldmode_last_activated text not null default '' );
create table vips ( broadcaster_id text not null, user_id text not null, created_at text not null default '', primary key (broadcaster_id, user_id), foreign key (broadcaster_id) references users(id), foreign key (user_id) references users(id) );`

Expand Down
2 changes: 1 addition & 1 deletion internal/database/predictions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Prediction struct {
WinningOutcomeID *string `db:"winning_outcome_id" json:"winning_outcome_id"`
PredictionWindow int `db:"prediction_window" json:"prediction_window"`
Status string `db:"status" json:"status"`
StartedAt string `db:"created_at" json:"started_at"`
StartedAt string `db:"created_at" json:"created_at"`
EndedAt *string `db:"ended_at" json:"ended_at"`
LockedAt *string `db:"locked_at" json:"locked_at"`
Outcomes []PredictionOutcome `json:"outcomes"`
Expand Down
1 change: 0 additions & 1 deletion internal/database/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ type ScheduleSegment struct {
IsVacation bool `db:"is_vacation" json:"-"`
Category *SegmentCategory `json:"category"`
UserID string `db:"broadcaster_id" json:"-"`
Timezone string `db:"timezone" json:"timezone,omitempty"`
CategoryID *string `db:"category_id" json:"-"`
CategoryName *string `db:"category_name" dbi:"false" json:"-"`
IsCanceled *bool `db:"is_canceled" json:"-"`
Expand Down
6 changes: 3 additions & 3 deletions internal/database/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type Subscription struct {
UserLogin string `db:"user_login" json:"user_login"`
UserName string `db:"user_name" json:"user_name"`
IsGift bool `db:"is_gift" json:"is_gift"`
GifterID *sql.NullString `db:"gifter_id" json:"gifter_id,omitempty"`
GifterName *sql.NullString `db:"gifter_name" json:"gifter_name,omitempty"`
GifterLogin *sql.NullString `db:"gifter_login" json:"gifter_login,omitempty"`
GifterID *sql.NullString `db:"gifter_id" json:"gifter_id"`
GifterName *sql.NullString `db:"gifter_name" json:"gifter_name"`
GifterLogin *sql.NullString `db:"gifter_login" json:"gifter_login"`
Tier string `db:"tier" json:"tier"`
CreatedAt string `db:"created_at" json:"-"`
// calculated fields
Expand Down
7 changes: 4 additions & 3 deletions internal/database/videos.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Video struct {
Viewable string `db:"viewable" json:"viewable"`
ViewCount int `db:"view_count" json:"view_count"`
Duration string `db:"duration" json:"duration"`
VideoLanguage string `db:"video_language" json:"video_language"`
VideoLanguage string `db:"video_language" json:"language"`
MutedSegments []VideoMutedSegment `json:"muted_segments"`
CategoryID *string `db:"category_id" dbs:"v.category_id" json:"-"`
Type string `db:"type" json:"type"`
Expand All @@ -32,7 +32,7 @@ type Video struct {

type VideoMutedSegment struct {
VideoID string `db:"video_id" json:"-"`
VideoOffset int `db:"video_offset" json:"video_offset"`
VideoOffset int `db:"video_offset" json:"offset"`
Duration int `db:"duration" json:"duration"`
}

Expand All @@ -54,7 +54,7 @@ type Clip struct {
// calculated fields
URL string `json:"url"`
ThumbnailURL string `json:"thumbnail_url"`
EmbedURL string `json:"embed_urL"`
EmbedURL string `json:"embed_url"`
StartedAt string `db:"started_at" dbi:"false" json:"-"`
EndedAt string `db:"ended_at" dbi:"false" json:"-"`
}
Expand Down Expand Up @@ -129,6 +129,7 @@ func (q *Query) InsertVideo(v Video) error {

func (q *Query) DeleteVideo(id string) error {
tx := q.DB.MustBegin()
tx.MustExec("delete from stream_markers where video_id=$1", id)
tx.MustExec("delete from video_muted_segments where video_id=$1", id)
tx.MustExec("delete from videos where id = $1", id)
return tx.Commit()
Expand Down
13 changes: 6 additions & 7 deletions internal/mock_api/endpoints/bits/leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ func getBitsLeaderboard(w http.ResponseWriter, r *http.Request) {
// check if the started_at date is valid and then add it to the start/end range
if period != "all" {
if startedAt == "" {
startedAt = time.Now().Format(time.RFC3339)
w.Write(mock_errors.GetErrorBytes(http.StatusBadRequest, errors.New("Bad Request"), "invalid value provided for started_at"))
w.WriteHeader(http.StatusBadRequest)
return
}

sa, err := time.Parse(time.RFC3339, startedAt)
Expand Down Expand Up @@ -199,12 +201,9 @@ func getBitsLeaderboard(w http.ResponseWriter, r *http.Request) {

length := len(bl)
apiR := models.APIResponse{
Data: bl,
Total: &length,
}

if dateRange.StartedAt != "" {
apiR.DateRange = &dateRange
Data: bl,
DateRange: &dateRange,
Total: &length,
}

body, _ := json.Marshal(apiR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func TestRedemption(t *testing.T) {
a.Equal(400, resp.StatusCode)

q.Set("broadcaster_id", "2")
q.Set("status", "FULFILLED")
req.URL.RawQuery = q.Encode()
resp, err = http.DefaultClient.Do(req)
a.Nil(err)
Expand Down
5 changes: 5 additions & 0 deletions internal/mock_api/endpoints/channel_points/redemptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func getRedemptions(w http.ResponseWriter, r *http.Request) {
status := r.URL.Query().Get("status")
sort := r.URL.Query().Get("sort")

if id == "" && status == "" {
mock_errors.WriteBadRequest(w, "The status query parameter is required if you don't specify the id query parameter.")
return
}

if !userCtx.MatchesBroadcasterIDParam(r) {
mock_errors.WriteUnauthorized(w, "Broadcaster ID mismatch")
return
Expand Down
Loading

0 comments on commit 90a6af4

Please sign in to comment.