Skip to content

Commit

Permalink
feat(Craiyon): add presence (PreMiD#6747)
Browse files Browse the repository at this point in the history
  • Loading branch information
theusaf authored Sep 5, 2022
1 parent 0acfde9 commit 58d9bb0
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
18 changes: 18 additions & 0 deletions websites/C/Craiyon/dist/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "https://schemas.premid.app/metadata/1.7",
"author": {
"name": "theusaf",
"id": "193714715631812608"
},
"service": "Craiyon",
"description": {
"en": "Craiyon is an AI model that can draw images from any text prompt!"
},
"url": "www.craiyon.com",
"version": "1.0.0",
"logo": "https://i.imgur.com/uCqmcTv.png",
"thumbnail": "https://i.imgur.com/cDreT5W.png",
"color": "#ea580c",
"category": "other",
"tags": ["ai", "dalle", "image", "generation"]
}
68 changes: 68 additions & 0 deletions websites/C/Craiyon/presence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const presence = new Presence({
clientId: "1015402986534608948",
});

type State = "start" | "generation" | "results";

let browsingTimestamp: number = Date.now() / 1000,
oldPrompt: string = null,
activityState: State = "start";

presence.on("UpdateData", () => {
const presenceData: PresenceData = {
largeImageKey: "https://i.imgur.com/uCqmcTv.png",
startTimestamp: browsingTimestamp,
},
{ pathname } = window.location;
switch (pathname) {
case "/": {
const input = document.querySelector<HTMLDivElement>("#prompt"),
container = document.querySelector(
".h-full.w-full > .relative > div > div"
);
presenceData.state = input.textContent
? `"${input.textContent}"`
: "Waiting for input...";
if (container.querySelector("svg.text-gray-300"))
presenceData.details = "Thinking of a prompt";
else if (
input.nextElementSibling
.querySelector("img")
.classList.contains("animate-wiggle")
) {
if (activityState !== "generation") {
presenceData.startTimestamp = browsingTimestamp = Date.now() / 1000;
oldPrompt = input.textContent;
activityState = "generation";
}
presenceData.details = "Generating images";
presenceData.state = `"${oldPrompt}"`;
} else {
if (activityState !== "results") {
presenceData.startTimestamp = browsingTimestamp = Date.now() / 1000;
activityState = "results";
}
if (document.activeElement === input && input.textContent !== oldPrompt)
presenceData.details = "Thinking of a new prompt";
else if (container.childElementCount > 3) {
presenceData.details = "Viewing results";
presenceData.state = `"${oldPrompt}"`;
} else {
presenceData.details = "Viewing a generated image";
presenceData.state = `"${oldPrompt}"`;
}
}
break;
}
case "/privacy": {
presenceData.details = "Reading privacy policy";
break;
}
case "/terms": {
presenceData.details = "Reading terms and conditions";
break;
}
}
if (presenceData.details) presence.setActivity(presenceData);
else presence.setActivity();
});
6 changes: 6 additions & 0 deletions websites/C/Craiyon/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist/"
}
}

0 comments on commit 58d9bb0

Please sign in to comment.