Skip to content

Commit

Permalink
Show notifications for live users when app starts
Browse files Browse the repository at this point in the history
  • Loading branch information
sir-pinecone committed Aug 6, 2023
1 parent 2870405 commit 3f52585
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions TwitchNotify.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
{
Expand All @@ -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"));
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3f52585

Please sign in to comment.