diff --git a/lib/api/bttv_api.dart b/lib/api/bttv_api.dart index 8c5e837c..68473044 100644 --- a/lib/api/bttv_api.dart +++ b/lib/api/bttv_api.dart @@ -5,27 +5,23 @@ import 'package:http/http.dart' as http; class BTTV { /// Returns a map of global BTTV emotes to their URL. - static Future?> getEmotesGlobal() async { + static Future> getEmotesGlobal() async { final url = Uri.parse('https://api.betterttv.net/3/cached/emotes/global'); final response = await http.get(url); if (response.statusCode == 200) { final decoded = jsonDecode(response.body) as List; - final emotes = decoded.map((emote) => EmoteBTTVGlobal.fromJson(emote)).toList(); + final emotes = decoded.map((emote) => EmoteBTTV.fromJson(emote)).toList(); - final emoteToUrl = {}; - for (final emote in emotes) { - emoteToUrl[emote.code] = 'https://cdn.betterttv.net/emote/${emote.id}/3x'; - } - - return emoteToUrl; + return emotes.map((emote) => Emote.fromBTTV(emote, EmoteType.bttvGlobal)).toList(); } else { debugPrint('Failed to get global BTTV emotes. Error code: ${response.statusCode}'); + return []; } } /// Returns a map of a channel's BTTV emotes to their URL. - static Future?> getEmotesChannel({required String id}) async { + static Future> getEmotesChannel({required String id}) async { final url = Uri.parse('https://api.betterttv.net/3/cached/users/twitch/$id'); final response = await http.get(url); @@ -33,17 +29,14 @@ class BTTV { final decoded = jsonDecode(response.body); final result = EmoteBTTVChannel.fromJson(decoded); - final emoteToUrl = {}; - for (final emote in result.channelEmotes) { - emoteToUrl[emote.code] = 'https://cdn.betterttv.net/emote/${emote.id}/3x'; - } - for (final emote in result.sharedEmotes) { - emoteToUrl[emote.code] = 'https://cdn.betterttv.net/emote/${emote.id}/3x'; - } + final emoteToUrl = []; + emoteToUrl.addAll(result.channelEmotes.map((emote) => Emote.fromBTTV(emote, EmoteType.bttvChannel))); + emoteToUrl.addAll(result.sharedEmotes.map((emote) => Emote.fromBTTV(emote, EmoteType.bttvShared))); return emoteToUrl; } else { debugPrint('Failed to get BTTV emotes for id: $id. Error code: ${response.statusCode}'); + return []; } } } diff --git a/lib/api/ffz_api.dart b/lib/api/ffz_api.dart index 6f035f30..e8d34f36 100644 --- a/lib/api/ffz_api.dart +++ b/lib/api/ffz_api.dart @@ -5,7 +5,7 @@ import 'package:http/http.dart' as http; class FFZ { /// Returns a map of global FFZ emotes to their URL. - static Future?> getEmotesGlobal() async { + static Future> getEmotesGlobal() async { final url = Uri.parse('https://api.betterttv.net/3/cached/frankerfacez/emotes/global'); final response = await http.get(url); @@ -13,19 +13,15 @@ class FFZ { final decoded = jsonDecode(response.body) as List; final emotes = decoded.map((emote) => EmoteFFZ.fromJson(emote)).toList(); - final emoteToUrl = {}; - for (final emote in emotes) { - emoteToUrl[emote.code] = emote.images.url4x ?? emote.images.url1x; - } - - return emoteToUrl; + return emotes.map((emote) => Emote.fromFFZ(emote, EmoteType.ffzGlobal)).toList(); } else { debugPrint('Failed to get global FFZ emotes. Error code: ${response.statusCode}'); + return []; } } /// Returns a map of a channel's FFZ emotes to their URL. - static Future?> getEmotesChannel({required String id}) async { + static Future> getEmotesChannel({required String id}) async { final url = Uri.parse('https://api.betterttv.net/3/cached/frankerfacez/users/twitch/$id'); final response = await http.get(url); @@ -33,14 +29,10 @@ class FFZ { final decoded = jsonDecode(response.body) as List; final emotes = decoded.map((emote) => EmoteFFZ.fromJson(emote)).toList(); - final emoteToUrl = {}; - for (final emote in emotes) { - emoteToUrl[emote.code] = emote.images.url4x ?? emote.images.url1x; - } - - return emoteToUrl; + return emotes.map((emote) => Emote.fromFFZ(emote, EmoteType.ffzChannel)).toList(); } else { debugPrint('Failed to get FFZ emotes for id: $id. Error code: ${response.statusCode}'); + return []; } } } diff --git a/lib/api/irc_api.dart b/lib/api/irc_api.dart index 693d55a6..e4d44c3c 100644 --- a/lib/api/irc_api.dart +++ b/lib/api/irc_api.dart @@ -1,6 +1,7 @@ import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/material.dart'; import 'package:frosty/models/badges.dart'; +import 'package:frosty/models/emotes.dart'; import 'package:frosty/models/irc.dart'; class IRC { @@ -52,7 +53,7 @@ class IRC { /// Returns an [InlineSpan] list that corresponds to the badges, username, words, and emotes of the given [IRCMessage]. static List generateSpan({ required IRCMessage ircMessage, - required Map emoteToUrl, + required Map emoteToObject, required Map badgeToObject, bool hideMessage = false, }) { @@ -154,7 +155,7 @@ class IRC { for (final word in words) { buffer.write(' '); - final emoteUrl = emoteToUrl[word] ?? localEmoteToUrl[word]; + final emoteUrl = emoteToObject[word]?.url ?? localEmoteToUrl[word]; if (emoteUrl != null) { span.add(TextSpan(text: buffer.toString())); diff --git a/lib/api/seventv_api.dart b/lib/api/seventv_api.dart index 8d25c544..fce989ef 100644 --- a/lib/api/seventv_api.dart +++ b/lib/api/seventv_api.dart @@ -6,7 +6,7 @@ import 'package:http/http.dart' as http; // A class for general requests. class SevenTV { /// Returns a map of global 7TV emotes to their URL. - static Future?> getEmotesGlobal() async { + static Future> getEmotesGlobal() async { final url = Uri.parse('https://api.7tv.app/v2/emotes/global'); final response = await http.get(url); @@ -14,18 +14,15 @@ class SevenTV { final decoded = jsonDecode(response.body) as List; final emotes = decoded.map((emote) => Emote7TV.fromJson(emote)).toList(); - final emoteToUrl = {}; - for (final emote in emotes) { - emoteToUrl[emote.name] = emote.urls[3][1]; - } - return emoteToUrl; + return emotes.map((emote) => Emote.from7TV(emote, EmoteType.sevenTvGlobal)).toList(); } else { debugPrint('Failed to get global 7TV emotes. Error code: ${response.statusCode}'); + return []; } } /// Returns a map of a channel's 7TV emotes to their URL. - static Future?> getEmotesChannel({required String user}) async { + static Future> getEmotesChannel({required String user}) async { final url = Uri.parse('https://api.7tv.app/v2/users/$user/emotes'); final response = await http.get(url); @@ -33,13 +30,10 @@ class SevenTV { final decoded = jsonDecode(response.body) as List; final emotes = decoded.map((emote) => Emote7TV.fromJson(emote)).toList(); - final emoteToUrl = {}; - for (final emote in emotes) { - emoteToUrl[emote.name] = emote.urls[3][1]; - } - return emoteToUrl; + return emotes.map((emote) => Emote.from7TV(emote, EmoteType.sevenTvChannel)).toList(); } else { debugPrint('Failed to get channel 7TV emotes. Error code: ${response.statusCode}'); + return []; } } } diff --git a/lib/api/twitch_api.dart b/lib/api/twitch_api.dart index a029077d..9f2deee3 100644 --- a/lib/api/twitch_api.dart +++ b/lib/api/twitch_api.dart @@ -11,7 +11,7 @@ import 'package:frosty/models/user.dart'; import 'package:http/http.dart' as http; class Twitch { - static Future?> getEmotesGlobal({required Map? headers}) async { + static Future> getEmotesGlobal({required Map? headers}) async { final url = Uri.parse('https://api.twitch.tv/helix/chat/emotes/global'); final response = await http.get(url, headers: headers); @@ -19,19 +19,15 @@ class Twitch { final decoded = jsonDecode(response.body)['data'] as List; final emotes = decoded.map((emote) => EmoteTwitch.fromJson(emote)).toList(); - final emoteToUrl = {}; - for (final emote in emotes) { - emoteToUrl[emote.name] = 'https://static-cdn.jtvnw.net/emoticons/v2/${emote.id}/default/dark/3.0'; - } - - return emoteToUrl; + return emotes.map((emote) => Emote.fromTwitch(emote, EmoteType.twitchGlobal)).toList(); } else { debugPrint('Failed to get global Twitch emotes. Error code: ${response.statusCode}'); + return []; } } /// Returns a map of a channel's Twitch emotes to their URL. - static Future?> getEmotesChannel({required String id, required Map? headers}) async { + static Future> getEmotesChannel({required String id, required Map? headers}) async { final url = Uri.parse('https://api.twitch.tv/helix/chat/emotes?broadcaster_id=$id'); final response = await http.get(url, headers: headers); @@ -39,14 +35,10 @@ class Twitch { final decoded = jsonDecode(response.body)['data'] as List; final emotes = decoded.map((emote) => EmoteTwitch.fromJson(emote)).toList(); - final emoteToUrl = {}; - for (final emote in emotes) { - emoteToUrl[emote.name] = 'https://static-cdn.jtvnw.net/emoticons/v2/${emote.id}/default/dark/3.0'; - } - - return emoteToUrl; + return emotes.map((emote) => Emote.fromTwitch(emote, EmoteType.twitchChannel)).toList(); } else { debugPrint('Failed to get Twitch emotes for id: $id. Error code: ${response.statusCode}'); + return []; } }