From 3546621b3f523948a7b16407c173ccbb48f26c30 Mon Sep 17 00:00:00 2001 From: Florrie Date: Thu, 15 Nov 2018 17:38:09 -0400 Subject: "What's Happening?" on homepage --- index.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) 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 = '
  • ' + + 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('') ? text[text.length - 5] : last(text) + if (lastChar !== '.') { + text += '.' + } + + text += '
  • ' + 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. +

    Scratch Unofficial HTML Client

    +

    What's Happening?

    + `, 'Scratch Homepage') } -- cgit 1.3.0-6-gf8a5