« get me outta code hell

scratchrlol - Simple HTML-based Scratch client
summary refs log tree commit diff
diff options
context:
space:
mode:
-rwxr-xr-xindex.js60
1 files changed, 59 insertions, 1 deletions
diff --git a/index.js b/index.js
index 0b364b6..b396421 100755
--- a/index.js
+++ b/index.js
@@ -1,4 +1,5 @@
 #!/usr/bin/env node
+'use strict'
 
 const fetch = require('node-fetch')
 const fixWS = require('fix-whitespace')
@@ -195,6 +196,56 @@ const templates = {
     return text
   },
 
+  activity: activity => {
+    let text = '<li>'
+
+    if (activity.type !== 'becomeownerstudio') {
+      text += templates.user(activity.actor_username)
+    }
+
+    const projectText = () => templates.project(activity.title || activity.project_title, activity.project_id)
+    const studioText = () => templates.studio(activity.title || activity.gallery_title, activity.gallery_id)
+
+    if (activity.type === 'shareproject') {
+      text += ' shared '
+      text += projectText()
+    } else if (activity.type === 'remixproject') {
+      text += ' shared '
+      text += projectText()
+      text += ', a remix of '
+      text += templates.project(activity.parent_title, activity.parent_id)
+    } else if (activity.type === 'loveproject') {
+      text += ' loved '
+      text += projectText()
+    } else if (activity.type === 'favoriteproject') {
+      text += ' favorited '
+      text += projectText()
+    } else if (activity.type === 'followuser') {
+      text += ' followed '
+      text += templates.user(activity.followed_username)
+    } else if (activity.type === 'becomecurator') {
+      text += ' became a curator of '
+      text += studioText()
+    } else if (activity.type === 'becomeownerstudio') {
+      text += templates.user(activity.recipient_username)
+      text += ' became a manager of '
+      text += studioText()
+    } else if (activity.type === 'followstudio') {
+      text += ' followed the studio '
+      text += studioText()
+    } else {
+      text += ` did something I just don't understand (${activity.type})`
+    }
+
+    const lastChar = text.endsWith('</a>') ? text[text.length - 5] : last(text)
+    if (lastChar !== '.') {
+      text += '.'
+    }
+
+    text += '</li>'
+    return text
+  },
+
   projectList: async (url, pathname, pageNumber) => {
     const offset = (pageNumber - 1) * limit
     const projects = await fetch(`${url}?limit=${limit}&offset=${offset}`).then(res => res.json())
@@ -518,8 +569,15 @@ const handleRequest = async (request, response) => {
   }
 
   if (urlParts.length === 0) {
+    const activity = await fetch(`https://api.scratch.mit.edu/users/${cookie.username}/following/users/activity?limit=8&x-token=${cookie.token}`).then(res => res.json())
+    const activityText = activity.map(templates.activity).join('\n')
+
     return page(request, response, fixWS`
-      You are at the homepage. Sorry, I haven't implmented any content for it yet.
+      <h1>Scratch Unofficial HTML Client</h1>
+      <h2>What's Happening?</h2>
+      <ul>
+        ${activityText}
+      </ul>
     `, 'Scratch Homepage')
   }