Skip to content

Commit

Permalink
data: rewrite twitch-search-channel model
Browse files Browse the repository at this point in the history
  • Loading branch information
bastimeyer committed Jan 12, 2022
1 parent 12534fd commit d1d935e
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 87 deletions.
80 changes: 77 additions & 3 deletions src/app/data/models/twitch/search-channel/model.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,84 @@
import { computed } from "@ember/object";
import { inject as service } from "@ember/service";
import attr from "ember-data/attr";
import Model from "ember-data/model";
import { belongsTo } from "ember-data/relationships";
import { DEFAULT_VODCAST_REGEXP } from "data/models/settings/streams/fragment";


export default Model.extend({
channel: belongsTo( "twitchChannel", { async: false } )
// noinspection JSValidateTypes
export default Model.extend( /** @class TwitchSearchChannel */ {
/** @type {IntlService} */
intl: service(),
/** @type {SettingsService} */
settings: service(),

/** @type {TwitchUser} */
user: belongsTo( "twitch-user", { async: true } ),
/** @type {string} */
broadcaster_language: attr( "string" ),
/** @type {string} */
broadcaster_login: attr( "string" ),
/** @type {string} */
display_name: attr( "string" ),
/** @type {string} */
game: belongsTo( "twitch-game", { async: true } ),
/** @type {string} */
game_name: attr( "string" ),
/** @type {boolean} */
is_live: attr( "boolean" ),
//** @type {TwitchStreamTag[]} */
//tag_ids: hasMany( "twitch-stream-tag", { async: true } ),
/** @type {string} */
thumbnail_url: attr( "string" ),
/** @type {string} */
title: attr( "string" ),
/** @type {Date} */
started_at: attr( "date" ),


/** @type {(null|RegExp)} */
reVodcast: computed( "settings.content.streams.vodcast_regexp", function() {
/** @this {TwitchStream} */
const vodcast_regexp = this.settings.content.streams.vodcast_regexp;

if ( vodcast_regexp.length && !vodcast_regexp.trim().length ) {
return null;
}
try {
return new RegExp( vodcast_regexp || DEFAULT_VODCAST_REGEXP, "i" );
} catch ( e ) {
return null;
}
}),

/** @type {boolean} */
isVodcast: computed( "reVodcast", "title", function() {
/** @this {TwitchStream} */
const { reVodcast, title } = this;

return reVodcast && title && reVodcast.test( title );
}),

/** @type {string} */
titleStartedAt: computed( "intl.locale", "started_at", function() {
/** @this {TwitchStream} */
const { started_at } = this;
return !started_at
? this.intl.t(
"models.twitch.search-channel.started-at.offline"
)
: new Date() - started_at < 24 * 3600 * 1000
? this.intl.t(
"models.twitch.search-channel.started-at.less-than-24h",
{ started_at }
)
: this.intl.t(
"models.twitch.search-channel.started-at.more-than-24h",
{ started_at }
);
})

}).reopenClass({
toString() { return "kraken/search/channels"; }
toString() { return "helix/search/channels"; }
});
20 changes: 4 additions & 16 deletions src/app/data/models/twitch/search-channel/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,13 @@ import TwitchSerializer from "data/models/twitch/serializer";

export default TwitchSerializer.extend({
modelNameFromPayloadKey() {
return "twitchSearchChannel";
},

attrs: {
channel: { deserialize: "records" }
},

normalizeResponse( store, primaryModelClass, payload, id, requestType ) {
// fix payload format
payload.channels = ( payload.channels || [] ).map( channel => ({ channel }) );

return this._super( store, primaryModelClass, payload, id, requestType );
return "twitch-search-channel";
},

normalize( modelClass, resourceHash, prop ) {
const foreignKey = this.store.serializerFor( "twitchChannel" ).primaryKey;

// get the id of the embedded TwitchChannel record and apply it here
resourceHash[ this.primaryKey ] = resourceHash.channel[ foreignKey ];
resourceHash.user = resourceHash[ this.primaryKey ];
resourceHash.game = resourceHash.game_id;
delete resourceHash[ "game_id" ];

return this._super( modelClass, resourceHash, prop );
}
Expand Down
5 changes: 5 additions & 0 deletions src/app/locales/de/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ twitch:
one {Eine Person schaut zu}
other {# Leute schauen zu}
}
search-channel:
started-at:
offline: "Offline"
less-than-24h: "Online seit {started_at, time, medium}"
more-than-24h: "Online seit {started_at, time, long}"
5 changes: 5 additions & 0 deletions src/app/locales/en/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ twitch:
one {One person is watching}
other {# people are watching}
}
search-channel:
started-at:
offline: "Offline"
less-than-24h: "Online since {started_at, time, medium}"
more-than-24h: "Online since {started_at, time, long}"
5 changes: 5 additions & 0 deletions src/app/locales/es/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ twitch:
one {Una persona está viendo}
other {# personas están viendo}
}
search-channel:
started-at:
offline: "Offline"
less-than-24h: "Online desde las {started_at, time, medium}"
more-than-24h: "Online desde las {started_at, time, long}"
5 changes: 5 additions & 0 deletions src/app/locales/fr/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ twitch:
one {Une personne est en train de regarder}
other {# personnes sont en train de regarder}
}
search-channel:
started-at:
offline: "Hors ligne"
less-than-24h: "En ligne depuis le {started_at, time, medium}"
more-than-24h: "En ligne depuis le {started_at, time, long}"
5 changes: 5 additions & 0 deletions src/app/locales/it/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ twitch:
one {Una persona sta guardando}
other {# persone stanno guardando}
}
search-channel:
started-at:
offline: "Offline"
less-than-24h: "Online da {started_at, time, medium}"
more-than-24h: "Online da {started_at, time, long}"
5 changes: 5 additions & 0 deletions src/app/locales/pt-br/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ twitch:
one {Uma pessoa está assistindo}
other {# estão assistindo}
}
search-channel:
started-at:
offline: "Offline"
less-than-24h: "Online desde {started_at, time, medium}"
more-than-24h: "Online desde {started_at, time, long}"
5 changes: 5 additions & 0 deletions src/app/locales/ru/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ twitch:
many {# зрителей}
other {Зрителей: #}
}
search-channel:
started-at:
offline: "не в сети"
less-than-24h: "Запущен в {started_at, time, medium}"
more-than-24h: "Запущен в {started_at, time, long}"
5 changes: 5 additions & 0 deletions src/app/locales/zh-tw/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ twitch:
one {目前有 1 位觀眾}
other {目前有 # 位觀眾}
}
search-channel:
started-at:
offline: "離線"
less-than-24h: "從 {started_at, time, medium} 開始"
more-than-24h: "從 {started_at, time, long} 開始"
19 changes: 0 additions & 19 deletions src/test/fixtures/data/models/twitch/search-channel.json

This file was deleted.

52 changes: 52 additions & 0 deletions src/test/fixtures/data/models/twitch/search-channel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
search-channel:
request:
method: "GET"
url: "https://api.twitch.tv/helix/search/channels"
query:
query: "foo"
live_only: true
response:
data:
- id: "1"
broadcaster_language: "en"
broadcaster_login: "foo"
display_name: "FOO"
game_id: "1"
game_name: "some game"
is_live: true
tag_ids: []
thumbnail_url: "https://localhost/data/twitch-search-stream/1/thumbnail-{width}x{height}.jpg"
title: "some title"
started_at: "2000-01-01T00:00:00Z"
- id: "2"
broadcaster_language: "de"
broadcaster_login: "bar"
display_name: "BAR"
game_id: "2"
game_name: "another game"
is_live: true
tag_ids: []
thumbnail_url: "https://localhost/data/twitch-search-stream/2/thumbnail-{width}x{height}.jpg"
title: "another title"
started_at: "1999-12-31T23:59:59Z"
pagination: {}
user:
request:
method: "GET"
url: "https://api.twitch.tv/helix/users"
query:
id: "1,2"
response:
data:
- id: "1"
- id: "2"
game:
request:
method: "GET"
url: "https://api.twitch.tv/helix/games"
query:
id: "1,2"
response:
data:
- id: "1"
- id: "2"
Loading

0 comments on commit d1d935e

Please sign in to comment.