diff --git a/TwitchNotify.c b/TwitchNotify.c index 1674b41..75aaf25 100644 --- a/TwitchNotify.c +++ b/TwitchNotify.c @@ -681,7 +681,8 @@ static void LoadUsers(void) // allowed image widths: 28, 50, 70, 150, 300, 600 // use 70 because for 100% dpi scale the toast size is 48 pixels, for 200% it is 96 - QuerySize = StrCatChainW(Query, ARRAYSIZE(Query), QuerySize, L"]){id,displayName,profileImageURL(width:70),stream{viewersCount}}}\"}"); + // including game info in the stream query so that we can immediately show notifications for users that are already live when the app is starting. + QuerySize = StrCatChainW(Query, ARRAYSIZE(Query), QuerySize, L"]){id,displayName,profileImageURL(width:70),stream{title,viewersCount,game{displayName}}}}\"}"); char QueryBytes[4096]; QuerySize = WideCharToMultiByte(CP_UTF8, 0, Query, QuerySize, QueryBytes, ARRAYSIZE(QueryBytes), NULL, NULL); @@ -1055,7 +1056,7 @@ static void OnUserStatus(int UserId, bool IsLive) ShowUserNotification(User); // start download of game/stream title - DownloadUserStream(UserId, 0); + DownloadUserStream(User->UserId, 0); } else { @@ -1080,20 +1081,8 @@ static void OnUserViewerCount(int UserId, int ViewerCount) User->ViewerCount = ViewerCount; } -static void OnUserStream(int UserId, JsonObject* Json) +static void ShowGameInfoFromStream(User* User, JsonObject* Stream) { - User* User = GetUser(UserId); - if (!User) - { - // no problem if user is not found - // probably user list was edited right before gql download finished - return; - } - - JsonObject* Data = JsonObject_GetObject(Json, JsonCSTR("data")); - JsonArray* Users = JsonObject_GetArray(Data, JsonCSTR("users")); - JsonObject* UserData = JsonArray_GetObject(Users, 0); - JsonObject* Stream = JsonObject_GetObject(UserData, JsonCSTR("stream")); JsonObject* Game = JsonObject_GetObject(Stream, JsonCSTR("game")); HSTRING GameName = JsonObject_GetString(Game, JsonCSTR("displayName")); HSTRING StreamName = JsonObject_GetString(Stream, JsonCSTR("title")); @@ -1112,13 +1101,32 @@ static void OnUserStream(int UserId, JsonObject* Json) // if notification is still up then retry a bit later - after 1 second if (User->Notification != NULL) { - DownloadUserStream(UserId, 1000); + DownloadUserStream(User->UserId, 1000); } } WindowsDeleteString(StreamName); WindowsDeleteString(GameName); JsonRelease(Game); +} + +static void OnUserStream(int UserId, JsonObject* Json) +{ + User* User = GetUser(UserId); + if (!User) + { + // no problem if user is not found + // probably user list was edited right before gql download finished + return; + } + + JsonObject* Data = JsonObject_GetObject(Json, JsonCSTR("data")); + JsonArray* Users = JsonObject_GetArray(Data, JsonCSTR("users")); + JsonObject* UserData = JsonArray_GetObject(Users, 0); + JsonObject* Stream = JsonObject_GetObject(UserData, JsonCSTR("stream")); + + ShowGameInfoFromStream(User, Stream); + JsonRelease(Stream); JsonRelease(UserData); JsonRelease(Users); @@ -1177,6 +1185,13 @@ static void OnUserInfo(JsonObject* Json) User->IsLive = Stream != NULL; + if (User->IsLive) + { + // show initial notification and then update it with game info if available. + ShowUserNotification(User); + ShowGameInfoFromStream(User, Stream); + } + // subscribe to user live events on websocket // if websocket is not connected yet, this subscription will // happen on WM_TWITCH_NOTIFY_WEBSOCKET message