From 09b26542752b4c6b13e91ceae3255b67bd98f82c Mon Sep 17 00:00:00 2001
From: Victor Borges <victor1882@outlook.com>
Date: Wed, 30 Apr 2025 00:10:42 -0300
Subject: add pagination support

---
 src/PartnerEventStorePatch.tsx | 365 ++++++++++++++++++++++++++---------------
 1 file changed, 237 insertions(+), 128 deletions(-)

(limited to 'src/PartnerEventStorePatch.tsx')

diff --git a/src/PartnerEventStorePatch.tsx b/src/PartnerEventStorePatch.tsx
index 2307283..4674e5f 100644
--- a/src/PartnerEventStorePatch.tsx
+++ b/src/PartnerEventStorePatch.tsx
@@ -1,5 +1,5 @@
-import { findModuleExport } from "@decky/ui";
-import { afterPatch } from "decky-frontend-lib";
+import { findModuleExport, Patch } from "@decky/ui";
+import { replacePatch } from "decky-frontend-lib";
 import remarkHtml from "remark-html"
 import remarkParse from "remark-parse"
 import remarkGfm from "remark-gfm"
@@ -32,6 +32,7 @@ const steamClanSteamID = "103582791470414830";
 const steamClanID = "40893422";
 const steamOSAppId = 1675200;
 const githubReleasesURI = "https://api.github.com/repos/ublue-os/bazzite/releases";
+const generator = fetchReleases();
 
 enum SteamEventType {
   SmallUpdate = 12,
@@ -39,155 +40,263 @@ enum SteamEventType {
   BigUpdate = 14,
 }
 
+type SteamTags = {
+    require_tags?: string[]
+}
+
+enum SteamOSChannel {
+    Stable = "stablechannel",
+    Beta = "betachannel",
+    Preview =  "previewchannel",
+}
+
 const mutex = new Mutex();
-let releases: any[];
-let channel: string;
+const cachedGithubReleases: { gid: string, release: any }[] = [];
 
-export function patchPartnerEventStore() {
-  return afterPatch(
+export function patchPartnerEventStore(): Patch[] {
+  const loadAdjacentPartnerEventsPatch = replacePatch(
     PartnerEventStore.prototype,
-    "InternalLoadAdjacentPartnerEvents",
-    async function(args, ret) {
-      let [, , , appId, , , c, ] = args;
+    "LoadAdjacentPartnerEvents",
+    async function(args) {
+      let [gidEvent, gidAnnouncement, appId, countBefore, countAfter, tags, token] = args;
+      const module = this;
 
       if (appId !== steamOSAppId) {
-        return ret;
+        return module.InternalLoadAdjacentPartnerEvents(gidEvent, void 0, gidAnnouncement, appId, countBefore, countAfter, tags, token);
       }
 
-      ret = await Promise.resolve(ret);
+      return mutex.runExclusive(async () => {
+        return LoadBazziteReleasesAsPartnerEvents(module, gidEvent?.GID || gidEvent?.AnnouncementGID, tags, countBefore, countAfter);
+      });
+    }
+  );
+
+  const loadAdjacentPartnerEventsByAnnouncementPatch = replacePatch(
+    PartnerEventStore.prototype,
+    "LoadAdjacentPartnerEventsByAnnouncement",
+    async function(args) {
+      let [gidEvent, gidAnnouncement, appId, countBefore, countAfter, tags, token] = args;
+      const module = this;
 
-      if (!Array.isArray(ret)) {
-        return ret;
+      if (appId !== steamOSAppId) {
+        return module.InternalLoadAdjacentPartnerEvents(void 0, gidEvent, gidAnnouncement, appId, countBefore, countAfter, tags, token);
       }
 
-      ret.length = 0;
+      return mutex.runExclusive(async () => {
+        return LoadBazziteReleasesAsPartnerEvents(module, gidEvent?.GID || gidEvent?.AnnouncementGID, tags, countBefore, countAfter);
+      });
+    }
+  );
+
+  const loadAdjacentPartnerEventsByEventPatch = replacePatch(
+    PartnerEventStore.prototype,
+    "LoadAdjacentPartnerEventsByEvent",
+    async function(args) {
+      let [gidEvent, gidAnnouncement, appId, countBefore, countAfter, tags, token] = args;
+      const module = this;
+
+      if (appId !== steamOSAppId) {
+        const clanId = gidAnnouncement || gidEvent.clanSteamID;
+
+        return gidEvent.bOldAnnouncement
+          ? module.InternalLoadAdjacentPartnerEvents(void 0, gidEvent.AnnouncementGID, clanId, appId, countBefore, countAfter, tags, token)
+          : module.InternalLoadAdjacentPartnerEvents(gidEvent.GID, gidEvent.AnnouncementGID, clanId, appId, countBefore, countAfter, tags, token);
+      }
+
+      return mutex.runExclusive(async () => {
+        return LoadBazziteReleasesAsPartnerEvents(module, gidEvent?.GID || gidEvent?.AnnouncementGID, tags, countBefore, countAfter);
+      });
+    }
+  );
+
+  return [
+    loadAdjacentPartnerEventsPatch,
+    loadAdjacentPartnerEventsByAnnouncementPatch,
+    loadAdjacentPartnerEventsByEventPatch
+  ]
+}
+
+async function LoadBazziteReleasesAsPartnerEvents(module: any, gid: any, tags: SteamTags, countBefore: number, countAfter: number) {
+  const ret: any[] = [];
+
+  // InternalLoadAdjacentPartnerEvents minified code, gets announcement from cache if it exists
+  if (module.m_mapAdjacentAnnouncementGIDs.has(gid)) {
+    // noinspection JSPrimitiveTypeWrapperUsage
+      let e = module.m_mapAdjacentAnnouncementGIDs.get(gid)
+      , r = new Array;
+    // noinspection CommaExpressionJS
+      if (e.forEach(((e: any) => {
+        if (module.m_mapAnnouncementBodyToEvent.has(e)) {
+            let t = module.m_mapAnnouncementBodyToEvent.get(e);
+            ret.push(module.m_mapExistingEvents.get(t))
+        } else
+            r.push(e)
+    }
+    )),
+    r.length > 0) {
+        (await module.LoadBatchPartnerEventsByEventGIDsOrAnnouncementGIDs(null, r, tags)).forEach(((e: any) => ret.push(e)))
+    }
+  }
+
+  if (cachedGithubReleases.length === 0) {
+    await fetchMoreReleases(countAfter, isBetaOrPreviewChannel(tags));
+  }
+
+  const releaseIndex = gid ? cachedGithubReleases.findIndex((e: any) => e.gid === gid) : -1;
+  let releases: { gid: any, release: any }[];
+
+  if (releaseIndex === -1) {
+    releases = cachedGithubReleases.slice(0, countAfter);
+  } else {
+    if (releaseIndex + countAfter + 1 > cachedGithubReleases.length) {
+      const toFetch = releaseIndex + countAfter + 1 - cachedGithubReleases.length;
+      await fetchMoreReleases(toFetch, isBetaOrPreviewChannel(tags));
+    }
+
+    releases = cachedGithubReleases.slice(Math.max(releaseIndex - countBefore + 1, 0), releaseIndex + countAfter + 1);
+  }
+
+  for (const { release } of releases) {
+    const releasePublishedAt = Math.floor((new Date(release.published_at)).getTime() / 1000);
+
+    const html = await unified()
+      .use(remarkParse)
+      .use(remarkGfm)
+      .use(remarkHtml)
+      .process(release.body);
 
-      await mutex.runExclusive(async () => {
-        if (releases && releases.length > 0)
-          return;
+    const converter = new (html2bbcode.HTML2BBCode)();
+    const bbcode = converter.feed(html.value);
+
+    // haven't found a way to hide likes and comments yet
+    const event = {
+      "gid": String(release.id),
+      "clan_steamid": steamClanSteamID,
+      "event_name": release.name,
+      "event_type": SteamEventType.Update,
+      "appid": steamOSAppId,
+      "server_address": "",
+      "server_password": "",
+      // start and end time are only used for certain event_type, not used for updates, but fill it anyway
+      "rtime32_start_time": releasePublishedAt,
+      "rtime32_end_time": releasePublishedAt,
+      "comment_count": 0,
+      "creator_steamid": "0",
+      "last_update_steamid": "0",
+      "event_notes": "see announcement body",
+      "jsondata": "",
+      "announcement_body": {
+          "gid": String(release.id),
+          "clanid": steamClanID,
+          "posterid": "0",
+          "headline": `Bazzite ${release.tag_name}`,
+          "posttime": releasePublishedAt,
+          "updatetime": releasePublishedAt,
+          "body": bbcode.toString(),
+          "commentcount": 0,
+          "tags": [
+              "patchnotes",
+              isBetaOrPreviewChannel(tags) ? SteamOSChannel.Beta : SteamOSChannel.Stable,
+          ],
+          "language": 0,
+          "hidden": 0,
+          "forum_topic_id": "0",
+          "event_gid": "0",
+          "voteupcount": 0,
+          "votedowncount": 0,
+          "ban_check_result": 0,
+          "banned": 0
+      },
+      "published": 1,
+      "hidden": 0,
+      "rtime32_visibility_start": 0,
+      "rtime32_visibility_end": 0,
+      "broadcaster_accountid": 0,
+      "follower_count": 0,
+      "ignore_count": 0,
+      "forum_topic_id": "0",
+      "rtime32_last_modified": releasePublishedAt,
+      "news_post_gid": "0",
+      "rtime_mod_reviewed": 0,
+      "featured_app_tagid": 0,
+      "referenced_appids": [],
+      "build_id": 0,
+      "build_branch": "",
+      "unlisted": 0,
+      "votes_up": 0,
+      "votes_down": 0,
+      "comment_type": "ForumTopic",
+      "gidfeature": "0",
+      "gidfeature2": "0"
+    };
+
+    // InternalLoadAdjacentPartnerEvents minified code, maps announcement and add it to cache
+    if (!module.m_mapExistingEvents.has(event.gid)) {
+      let steamId = new SteamID(event.clan_steamid);
+      module.InsertEventModelFromClanEventData(steamId, event)
+    }
+
+    ret.push(module.m_mapExistingEvents.get(event.gid));
+  }
+
+  return ret;
+}
+
+async function fetchMoreReleases(count: number, beta: boolean) {
+  const releases = [];
+  let iterator;
+
+  do {
+    iterator = await generator.next();
+    const release = iterator.value;
+
+    if ((beta && release.prerelease) || (!beta && !release.prerelease))
+      releases.push(release);
+    } while (releases.length < count && !iterator.done)
+
+  for (const release of releases) {
+    cachedGithubReleases.push({ gid: String(release.id), release });
+  }
+}
 
+async function* fetchReleases() {
+    let currentPage = 1;
+    let done = false;
+
+    while (!done) {
         let response: Response;
         let responseJson: any;
 
         try {
-          response = await fetch(githubReleasesURI);
+            response = await fetch(githubReleasesURI + `?page=${currentPage++}&per_page=10`);
 
-          if (!response.ok)
-            return;
-  
-          responseJson = await response.json();
+            if (response.ok) {
+                responseJson = await response.json();
+            } else {
+                responseJson = [];
+            }
         }
         catch {
-          responseJson = [];
+            responseJson = [];
         }
 
         if (!Array.isArray(responseJson) || responseJson.length == 0) {
-          return;
-        }
-
-        responseJson.sort((a, b) => (new Date(b.created_at)).getTime() - (new Date(a.created_at)).getTime());
-
-        if (c?.require_tags && c?.require_tags?.includes("stablechannel")) {
-          releases = responseJson.filter(r => !r.prerelease);
-          channel = "stablechannel";
-        } else if (c?.require_tags && (c?.require_tags?.includes("betachannel") || c?.require_tags?.includes("previewchannel"))) {
-          releases = responseJson.filter(r => r.prerelease);
-          channel = "betachannel";
+            done = true;
         } else {
-          releases = responseJson;
-          channel = "stablechannel";
-        }
-      });
-      
-      if (!releases || releases.length == 0)
-        return ret;
-
-      for (const release of releases) {
-        const releaseCreatedAt = Math.floor((new Date(release.created_at)).getTime() / 1000);
-        
-        const html = await unified()
-          .use(remarkParse)
-          .use(remarkGfm)
-          .use(remarkHtml)
-          .process(release.body);
-
-        const converter = new (html2bbcode.HTML2BBCode)();
-        const bbcode = converter.feed(html.value);
-
-        // @ts-ignore
-        const event = {
-          "gid": String(release.id),
-          "clan_steamid": steamClanSteamID,
-          "event_name": release.name,
-          "event_type": SteamEventType.Update,
-          "appid": steamOSAppId,
-          "server_address": "",
-          "server_password": "",
-          "rtime32_start_time": releaseCreatedAt,  // only used for certain event_type, not used for updates, but anyway
-          "rtime32_end_time": releaseCreatedAt, // only used for certain event_type, not used for updates, but anyway
-          "comment_count": 0,
-          "creator_steamid": "0",
-          "last_update_steamid": "0",
-          "event_notes": "see announcement body",
-          "jsondata": "{\n\t\"localized_subtitle\": [\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull\n\t]\n\t,\n\t\"localized_summary\": [\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull\n\t]\n\t,\n\t\"localized_title_image\": [\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull\n\t]\n\t,\n\t\"localized_capsule_image\": [\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull\n\t]\n\t,\n\t\"bSaleEnabled\": false,\n\t\"sale_show_creator\": false,\n\t\"sale_sections\": [\n\n\t]\n\t,\n\t\"sale_browsemore_text\": \"\",\n\t\"sale_browsemore_url\": \"\",\n\t\"sale_browsemore_color\": \"\",\n\t\"sale_browsemore_bgcolor\": \"\",\n\t\"localized_sale_header\": [\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull\n\t]\n\t,\n\t\"localized_sale_overlay\": [\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull\n\t]\n\t,\n\t\"localized_sale_product_banner\": [\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull\n\t]\n\t,\n\t\"localized_sale_product_mobile_banner\": [\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull\n\t]\n\t,\n\t\"localized_sale_logo\": [\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull\n\t]\n\t,\n\t\"sale_font\": \"\",\n\t\"sale_background_color\": \"\",\n\t\"sale_header_offset\": 150,\n\t\"referenced_appids\": [\n\n\t]\n\t,\n\t\"bBroadcastEnabled\": false,\n\t\"broadcastChatSetting\": \"hide\",\n\t\"default_broadcast_title\": \"#Broadcast_default_title_dev\",\n\t\"localized_broadcast_title\": [\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull\n\t]\n\t,\n\t\"localized_broadcast_left_image\": [\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull\n\t]\n\t,\n\t\"localized_broadcast_right_image\": [\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull,\n\t\tnull\n\t]\n\t,\n\t\"broadcast_whitelist\": [\n\n\t]\n\t,\n\t\"bScheduleEnabled\": false,\n\t\"scheduleEntries\": [\n\n\t]\n\t,\n\t\"valve_access_log\": [\n\t\t{\n\t\t\t\"strSteamID\": \"76561197979253178\",\n\t\t\t\"rtUpdated\": 1741648377\n\t\t},\n\t\t{\n\t\t\t\"strSteamID\": \"76561198840299494\",\n\t\t\t\"rtUpdated\": 1741740761\n\t\t}\n\t]\n\t,\n\t\"clone_from_event_gid\": \"519706073503894980\",\n\t\"clone_from_sale_enabled\": false,\n\t\"automatically_push_updated_source\": true\n}",
-          "announcement_body": {
-              "gid": String(release.id),
-              "clanid": steamClanID,
-              "posterid": "0",
-              "headline": `Bazzite ${release.name}`,
-              "posttime": releaseCreatedAt,
-              "updatetime": releaseCreatedAt,
-              "body": bbcode.toString(),
-              "commentcount": 0,
-              "tags": [
-                  "patchnotes",
-                  channel,
-              ],
-              "language": 0,
-              "hidden": 0,
-              "forum_topic_id": "0",
-              "event_gid": "0",
-              "voteupcount": 0,
-              "votedowncount": 0,
-              "ban_check_result": 0,
-              "banned": 0
-          },
-          "published": 1,
-          "hidden": 0,
-          "rtime32_visibility_start": 0,
-          "rtime32_visibility_end": 0,
-          "broadcaster_accountid": 0,
-          "follower_count": 0,
-          "ignore_count": 0,
-          "forum_topic_id": "0",
-          "rtime32_last_modified": releaseCreatedAt,
-          "news_post_gid": "0",
-          "rtime_mod_reviewed": 0,
-          "featured_app_tagid": 0,
-          "referenced_appids": [],
-          "build_id": 0,
-          "build_branch": "",
-          "unlisted": 0,
-          "votes_up": 0,
-          "votes_down": 0,
-          "comment_type": "ForumTopic", // haven't found a way to hide likes and comments
-          "gidfeature": "0",
-          "gidfeature2": "0"
-        };
-
-        // @ts-ignore
-        if (!this.m_mapExistingEvents.has(event.gid)) {
-          let steamId = new SteamID(event.clan_steamid);
-
-          // @ts-ignore
-          this.InsertEventModelFromClanEventData(steamId, event)
-        }
-        
-        // @ts-ignore
-        ret.push(this.m_mapExistingEvents.get(event.gid));
-      }
+            responseJson.sort((a, b) => (new Date(b.created_at)).getTime() - (new Date(a.created_at)).getTime());
 
-      return ret;
+            for (let release of responseJson) {
+                yield release;
+            }
+        }
     }
-  );
+
+    return undefined;
+}
+
+function isBetaOrPreviewChannel(tags: SteamTags): boolean {
+    return (tags?.require_tags
+            && (tags?.require_tags?.includes(SteamOSChannel.Beta)
+                || tags?.require_tags?.includes(SteamOSChannel.Preview)))
+        ?? false;
 }
-- 
cgit v1.2.3


From 929466b648cd4cbcdc671ccb2efa8e944a86a74b Mon Sep 17 00:00:00 2001
From: Victor Borges <victor1882@outlook.com>
Date: Wed, 30 Apr 2025 00:12:09 -0300
Subject: reformat code

---
 src/PartnerEventStorePatch.tsx | 145 ++++++++++++++++++++---------------------
 1 file changed, 72 insertions(+), 73 deletions(-)

(limited to 'src/PartnerEventStorePatch.tsx')

diff --git a/src/PartnerEventStorePatch.tsx b/src/PartnerEventStorePatch.tsx
index 4674e5f..d7ef75b 100644
--- a/src/PartnerEventStorePatch.tsx
+++ b/src/PartnerEventStorePatch.tsx
@@ -1,5 +1,5 @@
-import { findModuleExport, Patch } from "@decky/ui";
-import { replacePatch } from "decky-frontend-lib";
+import {findModuleExport, Patch} from "@decky/ui";
+import {replacePatch} from "decky-frontend-lib";
 import remarkHtml from "remark-html"
 import remarkParse from "remark-parse"
 import remarkGfm from "remark-gfm"
@@ -41,13 +41,13 @@ enum SteamEventType {
 }
 
 type SteamTags = {
-    require_tags?: string[]
+  require_tags?: string[]
 }
 
 enum SteamOSChannel {
-    Stable = "stablechannel",
-    Beta = "betachannel",
-    Preview =  "previewchannel",
+  Stable = "stablechannel",
+  Beta = "betachannel",
+  Preview = "previewchannel",
 }
 
 const mutex = new Mutex();
@@ -57,7 +57,7 @@ export function patchPartnerEventStore(): Patch[] {
   const loadAdjacentPartnerEventsPatch = replacePatch(
     PartnerEventStore.prototype,
     "LoadAdjacentPartnerEvents",
-    async function(args) {
+    async function (args) {
       let [gidEvent, gidAnnouncement, appId, countBefore, countAfter, tags, token] = args;
       const module = this;
 
@@ -74,7 +74,7 @@ export function patchPartnerEventStore(): Patch[] {
   const loadAdjacentPartnerEventsByAnnouncementPatch = replacePatch(
     PartnerEventStore.prototype,
     "LoadAdjacentPartnerEventsByAnnouncement",
-    async function(args) {
+    async function (args) {
       let [gidEvent, gidAnnouncement, appId, countBefore, countAfter, tags, token] = args;
       const module = this;
 
@@ -91,7 +91,7 @@ export function patchPartnerEventStore(): Patch[] {
   const loadAdjacentPartnerEventsByEventPatch = replacePatch(
     PartnerEventStore.prototype,
     "LoadAdjacentPartnerEventsByEvent",
-    async function(args) {
+    async function (args) {
       let [gidEvent, gidAnnouncement, appId, countBefore, countAfter, tags, token] = args;
       const module = this;
 
@@ -122,19 +122,19 @@ async function LoadBazziteReleasesAsPartnerEvents(module: any, gid: any, tags: S
   // InternalLoadAdjacentPartnerEvents minified code, gets announcement from cache if it exists
   if (module.m_mapAdjacentAnnouncementGIDs.has(gid)) {
     // noinspection JSPrimitiveTypeWrapperUsage
-      let e = module.m_mapAdjacentAnnouncementGIDs.get(gid)
+    let e = module.m_mapAdjacentAnnouncementGIDs.get(gid)
       , r = new Array;
     // noinspection CommaExpressionJS
-      if (e.forEach(((e: any) => {
+    if (e.forEach(((e: any) => {
         if (module.m_mapAnnouncementBodyToEvent.has(e)) {
-            let t = module.m_mapAnnouncementBodyToEvent.get(e);
-            ret.push(module.m_mapExistingEvents.get(t))
+          let t = module.m_mapAnnouncementBodyToEvent.get(e);
+          ret.push(module.m_mapExistingEvents.get(t))
         } else
-            r.push(e)
-    }
+          r.push(e)
+      }
     )),
     r.length > 0) {
-        (await module.LoadBatchPartnerEventsByEventGIDsOrAnnouncementGIDs(null, r, tags)).forEach(((e: any) => ret.push(e)))
+      (await module.LoadBatchPartnerEventsByEventGIDsOrAnnouncementGIDs(null, r, tags)).forEach(((e: any) => ret.push(e)))
     }
   }
 
@@ -156,7 +156,7 @@ async function LoadBazziteReleasesAsPartnerEvents(module: any, gid: any, tags: S
     releases = cachedGithubReleases.slice(Math.max(releaseIndex - countBefore + 1, 0), releaseIndex + countAfter + 1);
   }
 
-  for (const { release } of releases) {
+  for (const {release} of releases) {
     const releasePublishedAt = Math.floor((new Date(release.published_at)).getTime() / 1000);
 
     const html = await unified()
@@ -186,26 +186,26 @@ async function LoadBazziteReleasesAsPartnerEvents(module: any, gid: any, tags: S
       "event_notes": "see announcement body",
       "jsondata": "",
       "announcement_body": {
-          "gid": String(release.id),
-          "clanid": steamClanID,
-          "posterid": "0",
-          "headline": `Bazzite ${release.tag_name}`,
-          "posttime": releasePublishedAt,
-          "updatetime": releasePublishedAt,
-          "body": bbcode.toString(),
-          "commentcount": 0,
-          "tags": [
-              "patchnotes",
-              isBetaOrPreviewChannel(tags) ? SteamOSChannel.Beta : SteamOSChannel.Stable,
-          ],
-          "language": 0,
-          "hidden": 0,
-          "forum_topic_id": "0",
-          "event_gid": "0",
-          "voteupcount": 0,
-          "votedowncount": 0,
-          "ban_check_result": 0,
-          "banned": 0
+        "gid": String(release.id),
+        "clanid": steamClanID,
+        "posterid": "0",
+        "headline": `Bazzite ${release.tag_name}`,
+        "posttime": releasePublishedAt,
+        "updatetime": releasePublishedAt,
+        "body": bbcode.toString(),
+        "commentcount": 0,
+        "tags": [
+          "patchnotes",
+          isBetaOrPreviewChannel(tags) ? SteamOSChannel.Beta : SteamOSChannel.Stable,
+        ],
+        "language": 0,
+        "hidden": 0,
+        "forum_topic_id": "0",
+        "event_gid": "0",
+        "voteupcount": 0,
+        "votedowncount": 0,
+        "ban_check_result": 0,
+        "banned": 0
       },
       "published": 1,
       "hidden": 0,
@@ -252,51 +252,50 @@ async function fetchMoreReleases(count: number, beta: boolean) {
 
     if ((beta && release.prerelease) || (!beta && !release.prerelease))
       releases.push(release);
-    } while (releases.length < count && !iterator.done)
+  } while (releases.length < count && !iterator.done)
 
   for (const release of releases) {
-    cachedGithubReleases.push({ gid: String(release.id), release });
+    cachedGithubReleases.push({gid: String(release.id), release});
   }
 }
 
 async function* fetchReleases() {
-    let currentPage = 1;
-    let done = false;
-
-    while (!done) {
-        let response: Response;
-        let responseJson: any;
-
-        try {
-            response = await fetch(githubReleasesURI + `?page=${currentPage++}&per_page=10`);
-
-            if (response.ok) {
-                responseJson = await response.json();
-            } else {
-                responseJson = [];
-            }
-        }
-        catch {
-            responseJson = [];
-        }
-
-        if (!Array.isArray(responseJson) || responseJson.length == 0) {
-            done = true;
-        } else {
-            responseJson.sort((a, b) => (new Date(b.created_at)).getTime() - (new Date(a.created_at)).getTime());
-
-            for (let release of responseJson) {
-                yield release;
-            }
-        }
+  let currentPage = 1;
+  let done = false;
+
+  while (!done) {
+    let response: Response;
+    let responseJson: any;
+
+    try {
+      response = await fetch(githubReleasesURI + `?page=${currentPage++}&per_page=10`);
+
+      if (response.ok) {
+        responseJson = await response.json();
+      } else {
+        responseJson = [];
+      }
+    } catch {
+      responseJson = [];
     }
 
-    return undefined;
+    if (!Array.isArray(responseJson) || responseJson.length == 0) {
+      done = true;
+    } else {
+      responseJson.sort((a, b) => (new Date(b.created_at)).getTime() - (new Date(a.created_at)).getTime());
+
+      for (let release of responseJson) {
+        yield release;
+      }
+    }
+  }
+
+  return undefined;
 }
 
 function isBetaOrPreviewChannel(tags: SteamTags): boolean {
-    return (tags?.require_tags
-            && (tags?.require_tags?.includes(SteamOSChannel.Beta)
-                || tags?.require_tags?.includes(SteamOSChannel.Preview)))
-        ?? false;
+  return (tags?.require_tags
+      && (tags?.require_tags?.includes(SteamOSChannel.Beta)
+        || tags?.require_tags?.includes(SteamOSChannel.Preview)))
+    ?? false;
 }
-- 
cgit v1.2.3


From a0e5a71b4b604a16f0621400605e3f7005ac49fb Mon Sep 17 00:00:00 2001
From: Victor Borges <victor1882@outlook.com>
Date: Wed, 30 Apr 2025 00:39:58 -0300
Subject: add more undefined checks

---
 src/PartnerEventStorePatch.tsx | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

(limited to 'src/PartnerEventStorePatch.tsx')

diff --git a/src/PartnerEventStorePatch.tsx b/src/PartnerEventStorePatch.tsx
index d7ef75b..3aa2a94 100644
--- a/src/PartnerEventStorePatch.tsx
+++ b/src/PartnerEventStorePatch.tsx
@@ -32,7 +32,9 @@ const steamClanSteamID = "103582791470414830";
 const steamClanID = "40893422";
 const steamOSAppId = 1675200;
 const githubReleasesURI = "https://api.github.com/repos/ublue-os/bazzite/releases";
-const generator = fetchReleases();
+let generator: AsyncGenerator<any, undefined, unknown>;
+const mutex = new Mutex();
+const cachedGithubReleases: { gid: string, release: any }[] = [];
 
 enum SteamEventType {
   SmallUpdate = 12,
@@ -50,9 +52,6 @@ enum SteamOSChannel {
   Preview = "previewchannel",
 }
 
-const mutex = new Mutex();
-const cachedGithubReleases: { gid: string, release: any }[] = [];
-
 export function patchPartnerEventStore(): Patch[] {
   const loadAdjacentPartnerEventsPatch = replacePatch(
     PartnerEventStore.prototype,
@@ -244,13 +243,17 @@ async function LoadBazziteReleasesAsPartnerEvents(module: any, gid: any, tags: S
 
 async function fetchMoreReleases(count: number, beta: boolean) {
   const releases = [];
+
+  if (!generator && cachedGithubReleases.length === 0)
+    generator = fetchReleases();
+
   let iterator;
 
   do {
     iterator = await generator.next();
     const release = iterator.value;
 
-    if ((beta && release.prerelease) || (!beta && !release.prerelease))
+    if (release && ((beta && release.prerelease) || (!beta && !release.prerelease)))
       releases.push(release);
   } while (releases.length < count && !iterator.done)
 
-- 
cgit v1.2.3


From ba7a86099b321f378b9d18ea025af9730642d8bf Mon Sep 17 00:00:00 2001
From: Victor Borges <victor1882@outlook.com>
Date: Sat, 3 May 2025 13:57:43 -0300
Subject: refactor

---
 src/PartnerEventStorePatch.tsx | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

(limited to 'src/PartnerEventStorePatch.tsx')

diff --git a/src/PartnerEventStorePatch.tsx b/src/PartnerEventStorePatch.tsx
index 3aa2a94..75b8ed2 100644
--- a/src/PartnerEventStorePatch.tsx
+++ b/src/PartnerEventStorePatch.tsx
@@ -297,8 +297,9 @@ async function* fetchReleases() {
 }
 
 function isBetaOrPreviewChannel(tags: SteamTags): boolean {
-  return (tags?.require_tags
-      && (tags?.require_tags?.includes(SteamOSChannel.Beta)
-        || tags?.require_tags?.includes(SteamOSChannel.Preview)))
-    ?? false;
+  if (!tags?.require_tags)
+    return false;
+
+  return tags.require_tags.includes(SteamOSChannel.Beta)
+    || tags.require_tags.includes(SteamOSChannel.Preview);
 }
-- 
cgit v1.2.3


From d97d5f11e7807906cff2cf41cad815fb05b0096c Mon Sep 17 00:00:00 2001
From: Victor Borges <victor1882@outlook.com>
Date: Sat, 3 May 2025 15:19:10 -0300
Subject: gets the correct branch now

---
 src/PartnerEventStorePatch.tsx | 62 ++++++------------------------------------
 1 file changed, 9 insertions(+), 53 deletions(-)

(limited to 'src/PartnerEventStorePatch.tsx')

diff --git a/src/PartnerEventStorePatch.tsx b/src/PartnerEventStorePatch.tsx
index 75b8ed2..f7df4b3 100644
--- a/src/PartnerEventStorePatch.tsx
+++ b/src/PartnerEventStorePatch.tsx
@@ -6,6 +6,7 @@ import remarkGfm from "remark-gfm"
 import {unified} from "unified"
 import html2bbcode from "./html2bbcode";
 import {Mutex} from 'async-mutex';
+import {fetchReleases, isBazziteBranchTesting} from "./FetchReleases";
 
 const PartnerEventStore = findModuleExport(
   (e) => e?.prototype?.InternalLoadAdjacentPartnerEvents
@@ -31,15 +32,14 @@ const SteamID = findModuleExport(
 const steamClanSteamID = "103582791470414830";
 const steamClanID = "40893422";
 const steamOSAppId = 1675200;
-const githubReleasesURI = "https://api.github.com/repos/ublue-os/bazzite/releases";
 let generator: AsyncGenerator<any, undefined, unknown>;
 const mutex = new Mutex();
 const cachedGithubReleases: { gid: string, release: any }[] = [];
 
 enum SteamEventType {
-  SmallUpdate = 12,
+  // SmallUpdate = 12,
   Update = 13,
-  BigUpdate = 14,
+  // BigUpdate = 14,
 }
 
 type SteamTags = {
@@ -49,7 +49,7 @@ type SteamTags = {
 enum SteamOSChannel {
   Stable = "stablechannel",
   Beta = "betachannel",
-  Preview = "previewchannel",
+  // Preview = "previewchannel",
 }
 
 export function patchPartnerEventStore(): Patch[] {
@@ -138,7 +138,7 @@ async function LoadBazziteReleasesAsPartnerEvents(module: any, gid: any, tags: S
   }
 
   if (cachedGithubReleases.length === 0) {
-    await fetchMoreReleases(countAfter, isBetaOrPreviewChannel(tags));
+    await fetchMoreReleases(countAfter);
   }
 
   const releaseIndex = gid ? cachedGithubReleases.findIndex((e: any) => e.gid === gid) : -1;
@@ -149,7 +149,7 @@ async function LoadBazziteReleasesAsPartnerEvents(module: any, gid: any, tags: S
   } else {
     if (releaseIndex + countAfter + 1 > cachedGithubReleases.length) {
       const toFetch = releaseIndex + countAfter + 1 - cachedGithubReleases.length;
-      await fetchMoreReleases(toFetch, isBetaOrPreviewChannel(tags));
+      await fetchMoreReleases(toFetch);
     }
 
     releases = cachedGithubReleases.slice(Math.max(releaseIndex - countBefore + 1, 0), releaseIndex + countAfter + 1);
@@ -195,7 +195,7 @@ async function LoadBazziteReleasesAsPartnerEvents(module: any, gid: any, tags: S
         "commentcount": 0,
         "tags": [
           "patchnotes",
-          isBetaOrPreviewChannel(tags) ? SteamOSChannel.Beta : SteamOSChannel.Stable,
+          (await isBazziteBranchTesting()) ? SteamOSChannel.Beta : SteamOSChannel.Stable,
         ],
         "language": 0,
         "hidden": 0,
@@ -241,7 +241,7 @@ async function LoadBazziteReleasesAsPartnerEvents(module: any, gid: any, tags: S
   return ret;
 }
 
-async function fetchMoreReleases(count: number, beta: boolean) {
+async function fetchMoreReleases(count: number) {
   const releases = [];
 
   if (!generator && cachedGithubReleases.length === 0)
@@ -252,54 +252,10 @@ async function fetchMoreReleases(count: number, beta: boolean) {
   do {
     iterator = await generator.next();
     const release = iterator.value;
-
-    if (release && ((beta && release.prerelease) || (!beta && !release.prerelease)))
-      releases.push(release);
+    releases.push(release);
   } while (releases.length < count && !iterator.done)
 
   for (const release of releases) {
     cachedGithubReleases.push({gid: String(release.id), release});
   }
 }
-
-async function* fetchReleases() {
-  let currentPage = 1;
-  let done = false;
-
-  while (!done) {
-    let response: Response;
-    let responseJson: any;
-
-    try {
-      response = await fetch(githubReleasesURI + `?page=${currentPage++}&per_page=10`);
-
-      if (response.ok) {
-        responseJson = await response.json();
-      } else {
-        responseJson = [];
-      }
-    } catch {
-      responseJson = [];
-    }
-
-    if (!Array.isArray(responseJson) || responseJson.length == 0) {
-      done = true;
-    } else {
-      responseJson.sort((a, b) => (new Date(b.created_at)).getTime() - (new Date(a.created_at)).getTime());
-
-      for (let release of responseJson) {
-        yield release;
-      }
-    }
-  }
-
-  return undefined;
-}
-
-function isBetaOrPreviewChannel(tags: SteamTags): boolean {
-  if (!tags?.require_tags)
-    return false;
-
-  return tags.require_tags.includes(SteamOSChannel.Beta)
-    || tags.require_tags.includes(SteamOSChannel.Preview);
-}
-- 
cgit v1.2.3

