Skip to content

Commit

Permalink
Merge pull request #254 from twitchdev/freeform-tags-updates
Browse files Browse the repository at this point in the history
Freeform tags updates
  • Loading branch information
Xemdo authored Jul 18, 2023
2 parents 90b03e8 + e0add2e commit 8dd48ae
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 369 deletions.
6 changes: 5 additions & 1 deletion internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ func getDatabase() (sqlx.DB, error) {
}
}
db.SetMaxOpenConns(1)
checkAndUpdate(*db)
err = checkAndUpdate(*db)
if err != nil {
os.Exit(99)
}

return *db, nil
}
return sqlx.DB{}, nil
Expand Down
11 changes: 0 additions & 11 deletions internal/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,14 +619,6 @@ func TestStreams(t *testing.T) {
tags := dbr.Data.([]Tag)
a.GreaterOrEqual(len(tags), 1)

err = q.InsertStreamTag(StreamTag{TagID: tag.ID, UserID: TEST_USER_ID})
a.Nil(err)

dbr, err = q.GetStreamTags(TEST_USER_ID)
a.Nil(err)
tags = dbr.Data.([]Tag)
a.GreaterOrEqual(len(tags), 0)

dbr, err = q.GetFollowedStreams(s.UserID)
a.Nil(err)
streams := dbr.Data.([]Stream)
Expand All @@ -647,9 +639,6 @@ func TestStreams(t *testing.T) {
stream := streams[0]
a.GreaterOrEqual(len(stream.TagIDs), 0)

err = q.DeleteAllStreamTags(s.UserID)
a.Nil(err)

v := Video{
ID: util.RandomGUID(),
StreamID: &s.ID,
Expand Down
9 changes: 7 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 = 5
const currentVersion = 6

type migrateMap struct {
SQL string
Expand Down Expand Up @@ -49,6 +49,10 @@ ALTER TABLE users ADD COLUMN branded_content boolean not null default false;
ALTER TABLE users ADD COLUMN content_labels text not null default '';`,
Message: `Updating database to include Content Classification Label field.`,
},
6: {
SQL: `DROP TABLE IF EXISTS stream_tags;`,
Message: `Removing deprecated stream_tags from database.`,
},
}

func checkAndUpdate(db sqlx.DB) error {
Expand All @@ -70,6 +74,8 @@ func checkAndUpdate(db sqlx.DB) error {
for i := v; i < len(migrateSQL); i++ {
_, err = db.Exec(migrateSQL[i].SQL)
if err != nil {
fmt.Printf("DB Upgrade Error - %v\n", err)
fmt.Println("Exiting program. Please report this bug, and delete your Twitch CLI db file to regenerate it.")
return err
}

Expand Down Expand Up @@ -99,7 +105,6 @@ create table channel_points_rewards( id text not null primary key, broadcaster_i
create table channel_points_redemptions( id text not null primary key, reward_id text not null, broadcaster_id text not null, user_id text not null, user_input text, redemption_status text not null, redeemed_at text, foreign key (reward_id) references channel_points_rewards(id), foreign key (broadcaster_id) references users(id), foreign key (user_id) references users(id) );
create table streams( id text not null primary key, broadcaster_id id text not null, stream_type text not null default 'live', viewer_count int not null, started_at text not null, is_mature boolean not null default false, foreign key (broadcaster_id) references users(id) );
create table tags( id text not null primary key, is_auto boolean not null default false, tag_name text not null );
create table stream_tags( user_id text not null, tag_id text not null, primary key(user_id, tag_id), foreign key(user_id) references users(id), foreign key(tag_id) references tags(id) );
create table teams( id text not null primary key, background_image_url text, banner text, created_at text not null, updated_at text, info text, thumbnail_url text, team_name text, team_display_name text );
create table team_members( team_id text not null, user_id text not null, primary key (team_id, user_id) foreign key (team_id) references teams(id), foreign key (user_id) references users(id) );
create table videos( id text not null primary key, stream_id text, broadcaster_id text not null, title text not null, video_description text not null, created_at text not null, published_at text, viewable text not null, view_count int not null default 0, duration text not null, video_language text not null default 'en', category_id text, type text default 'archive', foreign key (stream_id) references streams(id), foreign key (broadcaster_id) references users(id), foreign key (category_id) references categories(id) );
Expand Down
44 changes: 0 additions & 44 deletions internal/database/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ type Stream struct {
ThumbnailURL string `json:"thumbnail_url"`
}

type StreamTag struct {
TagID string `db:"tag_id" json:"tag_id"`
UserID string `db:"user_id" json:"-"`
}

type Tag struct {
ID string `db:"id" json:"id"`
IsAuto bool `db:"is_auto" dbi:"false" json:"is_auto"`
Expand Down Expand Up @@ -145,45 +140,12 @@ func (q *Query) GetTags(t Tag) (*DBResponse, error) {
return &dbr, err
}

func (q *Query) GetStreamTags(id string) (*DBResponse, error) {
r := []Tag{}
err := q.DB.Select(&r, "select t.* from tags t join stream_tags st on st.tag_id = t.id where st.user_id=$1", id)
if err != nil {
return nil, err
}

dbr := DBResponse{
Data: r,
Limit: q.Limit,
Total: len(r),
}

if len(r) != q.Limit {
q.PaginationCursor = ""
}

dbr.Cursor = q.PaginationCursor

return &dbr, err
}

func (q *Query) InsertTag(t Tag) error {
stmt := generateInsertSQL("tags", "", t, false)
_, err := q.DB.NamedExec(stmt, t)
return err
}

func (q *Query) InsertStreamTag(st StreamTag) error {
stmt := generateInsertSQL("stream_tags", "", st, false)
_, err := q.DB.NamedExec(stmt, st)
return err
}

func (q *Query) DeleteAllStreamTags(userID string) error {
_, err := q.DB.Exec("delete from stream_tags where user_id = $1", userID)
return err
}

func (q *Query) GetFollowedStreams(userID string) (*DBResponse, error) {
var r = []Stream{}
sql := "select s.*, u1.user_login as broadcaster_login, u1.display_name as broadcaster_name, u1.category_id as category_id, c.category_name, u1.stream_language as stream_language, u1.title as title from streams s join users u1 on s.broadcaster_id = u1.id left join categories c on c.id = u1.category_id join follows f on f.broadcaster_id = s.broadcaster_id where f.user_id = $1"
Expand All @@ -195,7 +157,6 @@ func (q *Query) GetFollowedStreams(userID string) (*DBResponse, error) {
}

for i, s := range r {
var st []string
if err != nil {
return nil, err
}
Expand All @@ -205,11 +166,6 @@ func (q *Query) GetFollowedStreams(userID string) (*DBResponse, error) {
if s.CategoryName.Valid {
r[i].RealCategoryName = s.CategoryName.String
}
err = q.DB.Select(&st, "select tag_id from stream_tags where user_id=$1", s.UserID)
if err != nil {
return nil, err
}
r[i].TagIDs = st
}

dbr := DBResponse{
Expand Down
7 changes: 0 additions & 7 deletions internal/database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,10 @@ func (q *Query) SearchChannels(query string, live_only bool) (*DBResponse, error
}

for i, c := range r {
st := []string{}
err = q.DB.Select(&st, "select tag_id from stream_tags where user_id=$1", c.ID)
if err != nil {
return nil, err
}

emptyString := ""
if c.StartedAt == nil {
r[i].StartedAt = &emptyString
}
r[i].TagIDs = st // // Needs to be removed from db when this is fully removed from API
r[i].Tags = []string{"English", "CLI Tag"}
r[i].ThumbNailURL = "https://static-cdn.jtvnw.net/jtv_user_pictures/3f13ab61-ec78-4fe6-8481-8682cb3b0ac2-channel_offline_image-300x300.png"
}
Expand Down
2 changes: 1 addition & 1 deletion internal/events/types/channel_update_v2/channel_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,5 @@ func (e Event) GetEventSubAlias(t string) string {
}

func (e Event) SubscriptionVersion() string {
return "beta"
return "2"
}
25 changes: 23 additions & 2 deletions internal/mock_api/endpoints/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,10 @@ func All() []mock_api.MockEndpoint {
schedule.ScheduleSettings{},
search.SearchCategories{},
search.SearchChannels{},
streams.AllTags{},
streams.FollowedStreams{},
streams.Markers{},
streams.StreamKey{},
streams.Streams{},
streams.StreamTags{},
subscriptions.BroadcasterSubscriptions{},
subscriptions.UserSubscriptions{},
teams.ChannelTeams{},
Expand All @@ -91,3 +89,26 @@ func All() []mock_api.MockEndpoint {
whispers.Whispers{},
}
}

// All these endpoints return 410 Gone
func Gone() map[string][]string {
return map[string][]string{
"/tags/streams": {
"GET",
},
"/streams/tags": {
"GET",
"POST",
"PUT",
},
"/soundtrack/current_track": {
"GET",
},
"/soundtrack/playlist": {
"GET",
},
"/soundtrack/playlists": {
"GET",
},
}
}
98 changes: 0 additions & 98 deletions internal/mock_api/endpoints/streams/all_tags.go

This file was deleted.

Loading

0 comments on commit 8dd48ae

Please sign in to comment.