Skip to content

Commit 96e181a

Browse files
authored
Merge pull request #981 from MaterArc/recent-followers-and-following
Recent followers and following
2 parents c441798 + 80d2a80 commit 96e181a

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

features/features.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
[
22
{
3+
"version": 2,
4+
"id": "recent-followers-and-following",
5+
"versionAdded": "v5.0.0"
6+
},
7+
{
38
"version": 2,
49
"id": "picture-in-picture",
510
"versionAdded": "v5.0.0"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"title": "Show Recent Followers and Followings",
3+
"description": "Displays the most recent followers and followings of a user on their profile page.",
4+
"credits": [
5+
{
6+
"username": "-Brass_Glass-",
7+
"url": "https://scratch.mit.edu/users/-Brass_Glass-/"
8+
},
9+
{ "username": "MaterArc", "url": "https://scratch.mit.edu/users/MaterArc/" }
10+
],
11+
"type": ["Website"],
12+
"tags": ["New", "Featured"],
13+
"dynamic": true,
14+
"scripts": [{ "file": "script.js", "runOn": "/users/*" }]
15+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
export default async function ({ feature, console }) {
2+
const username = window.location.pathname.split('/')[2];
3+
if (!username) return;
4+
5+
const followersEndpoint = `https://api.scratch.mit.edu/users/${username}/followers/`;
6+
const followingEndpoint = `https://api.scratch.mit.edu/users/${username}/following/`;
7+
8+
try {
9+
const followersResponse = await fetch(followersEndpoint);
10+
if (!followersResponse.ok) return;
11+
12+
const followersData = await followersResponse.json();
13+
if (!Array.isArray(followersData)) return;
14+
15+
const mostRecentFollowers = followersData
16+
.slice(0, 9)
17+
.filter(follower => follower.username && follower.profile && follower.profile.images)
18+
.map(follower => ({
19+
username: follower.username,
20+
profileImage: follower.profile.images['90x90'] || follower.profile.images['50x50'] || '',
21+
}));
22+
23+
const followingResponse = await fetch(followingEndpoint);
24+
if (!followingResponse.ok) return;
25+
26+
const followingData = await followingResponse.json();
27+
if (!Array.isArray(followingData)) return;
28+
29+
const mostRecentFollowing = followingData
30+
.slice(0, 9)
31+
.filter(follow => follow.username && follow.profile && follow.profile.images)
32+
.map(follow => ({
33+
username: follow.username,
34+
profileImage: follow.profile.images['90x90'] || follow.profile.images['50x50'] || '',
35+
}));
36+
37+
const allFeaturedElements = document.querySelectorAll("#featured");
38+
if (allFeaturedElements.length === 0) return;
39+
40+
const lastFeaturedElement = allFeaturedElements[allFeaturedElements.length - 1];
41+
lastFeaturedElement.innerHTML = "";
42+
43+
mostRecentFollowers.forEach(follower => {
44+
const li = document.createElement("li");
45+
li.className = "user thumb item";
46+
47+
const link = document.createElement("a");
48+
link.href = `/users/${follower.username}/`;
49+
link.title = follower.username;
50+
51+
const img = document.createElement("img");
52+
img.className = "lazy";
53+
img.src = follower.profileImage;
54+
img.alt = follower.username;
55+
img.width = 60;
56+
img.height = 60;
57+
58+
const span = document.createElement("span");
59+
span.className = "title";
60+
61+
const spanLink = document.createElement("a");
62+
spanLink.href = `/users/${follower.username}/`;
63+
spanLink.textContent = follower.username;
64+
65+
link.appendChild(img);
66+
span.appendChild(spanLink);
67+
li.appendChild(link);
68+
li.appendChild(span);
69+
70+
lastFeaturedElement.appendChild(li);
71+
});
72+
73+
if (allFeaturedElements.length > 1) {
74+
const secondLastFeaturedElement = allFeaturedElements[allFeaturedElements.length - 2];
75+
secondLastFeaturedElement.innerHTML = "";
76+
77+
mostRecentFollowing.forEach(follow => {
78+
const li = document.createElement("li");
79+
li.className = "user thumb item";
80+
81+
const link = document.createElement("a");
82+
link.href = `/users/${follow.username}/`;
83+
link.title = follow.username;
84+
85+
const img = document.createElement("img");
86+
img.className = "lazy";
87+
img.src = follow.profileImage;
88+
img.alt = follow.username;
89+
img.width = 60;
90+
img.height = 60;
91+
92+
const span = document.createElement("span");
93+
span.className = "title";
94+
95+
const spanLink = document.createElement("a");
96+
spanLink.href = `/users/${follow.username}/`;
97+
spanLink.textContent = follow.username;
98+
99+
link.appendChild(img);
100+
span.appendChild(spanLink);
101+
li.appendChild(link);
102+
li.appendChild(span);
103+
104+
secondLastFeaturedElement.appendChild(li);
105+
});
106+
}
107+
} catch (error) {
108+
return;
109+
}
110+
}

0 commit comments

Comments
 (0)