8000 feat: add mastodon profile pic by hanadi92 · Pull Request #45 · TechForPalestine/profile-pic-maker · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add mastodon profile pic #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const nextConfig = {
{
hostname: 'gitlab.com',
},
{
protocol: 'https',
// todo this does not work. is it possible to include all mastodon servers...
hostname: '**',
pathname: '/**/account/avatar/**',
},
],
},
};
Expand Down
23 changes: 22 additions & 1 deletion src/app/api/retrieve-profile-pic/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ export async function GET(request: NextRequest) {
const searchParams = request.nextUrl.searchParams;
const username = searchParams.get('username');
const platform = searchParams.get('platform') as SocialPlatform;
const servername = searchParams.get('servername');

let profilePicUrl = '/user.jpg';

if (
!username ||
!platform ||
!Object.values(SocialPlatform).includes(platform)
!Object.values(SocialPlatform).includes(platform) ||
(platform == SocialPlatform.Mastodon && !servername)
) {
// just return back default image for now - not handling this kind of error yet
return NextResponse.json({ profilePicUrl }, { status: 200 });
Expand All @@ -28,6 +30,9 @@ export async function GET(request: NextRequest) {
case SocialPlatform.Gitlab:
profilePicUrl = await fetchGitlabProfilePic(username);
break;
case SocialPlatform.Mastodon:
profilePicUrl = await fetchMastodonProfilePic(username, servername);
break;
}

if (profilePicUrl === null) {
Expand Down Expand Up @@ -74,3 +79,19 @@ const fetchGitlabProfilePic = async (username: string) => {
}
return response[0].avatar_url;
};

const fetchMastodonProfilePic = async (
username: string,
servername: string | null,
) => {
const endpoint = `https://${servername}/api/v1/accounts/lookup?acct=${username}`;
const response = await fetch(endpoint).then((res) =>
res.ok ? res.json() : null,
);

if (response === null) {
return null;
}

return response.avatar;
};
23 changes: 21 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
FaGithub,
FaGitlab,
FaXTwitter,
FaMastodon,
} from 'react-icons/fa6';

export default function Home() {
Expand Down Expand Up @@ -60,13 +61,23 @@ export default function Home() {

const handleRetrieveProfilePicture = async (platform: SocialPlatform) => {
const userProvidedUsername = prompt(`Enter your ${platform} username:`);
const userProvidedServername =
platform == SocialPlatform.Mastodon
? prompt(`Enter your ${platform} servername:`)
: null;

if (userProvidedUsername) {
if (
(userProvidedUsername && platform != SocialPlatform.Mastodon) ||
// mastodon needs a servername and a username
(platform == SocialPlatform.Mastodon &&
userProvidedServername &&
userProvidedUsername)
) {
setFilePostfix(platform);
try {
setLoader(true);
const response = await fetch(
`/api/retrieve-profile-pic?username=${userProvidedUsername}&platform=${platform}`,
`/api/retrieve-profile-pic?username=${userProvidedUsername}&platform=${platform}&servername=${userProvidedServername}`,
).then((res) => (res.ok ? res.json() : null));
setLoader(false);
if (response === null) {
Expand Down Expand Up @@ -252,6 +263,14 @@ export default function Home() {
>
Use <FaGitlab className="inline mb-1" /> Profile Pic
</button>
<button
() =>
await handleRetrieveProfilePicture(SocialPlatform.Mastodon)
}
className="rounded-full my-2 py-3 px-2 w-full border border-gray-900 text-xl"
>
Use <FaMastodon className="inline mb-1" /> Profile Pic
</button>
</>
)}
</div>
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export enum SocialPlatform {
Twitter = 'twitter',
Github = 'github',
Gitlab = 'gitlab',
Mastodon = 'Mastodon',
}
0