selectable platform
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import config from '@/lib/config';
|
||||
import { Platform } from '@/lib/types';
|
||||
import { TwitterApi } from 'twitter-api-v2';
|
||||
|
||||
export default async function getPlatformIcon(platform: Platform) {
|
||||
switch (platform.platform) {
|
||||
case 'x':
|
||||
const twitterClient = new TwitterApi({
|
||||
appKey: config.twitter.key,
|
||||
appSecret: config.twitter.secret,
|
||||
accessToken: platform.token,
|
||||
accessSecret: platform.secret,
|
||||
});
|
||||
|
||||
const user = await twitterClient.v1.verifyCredentials();
|
||||
|
||||
return user.profile_image_url_https;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import { NextResponse, NextRequest } from 'next/server';
|
||||
import database from '../../../lib/database';
|
||||
import {
|
||||
PlatformKind,
|
||||
Platforms,
|
||||
PostRequest,
|
||||
ScheduleRequest,
|
||||
UserPlatforms,
|
||||
} from '@/lib/types';
|
||||
import { ObjectId } from 'mongodb';
|
||||
import { HttpStatusCode } from 'axios';
|
||||
import getPlatformIcon from './getPlatformIcon';
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const sessionId = req.cookies.get('session_id');
|
||||
|
||||
if (!sessionId)
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: 'No session',
|
||||
},
|
||||
{ status: HttpStatusCode.Unauthorized }
|
||||
);
|
||||
|
||||
const session = await database.sessions.findOne({
|
||||
_id: new ObjectId(sessionId.value),
|
||||
});
|
||||
|
||||
if (!session)
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: 'Invalid session',
|
||||
},
|
||||
{ status: HttpStatusCode.NotFound }
|
||||
);
|
||||
|
||||
const user = await database.users.findOne({
|
||||
_id: new ObjectId(session.user),
|
||||
});
|
||||
|
||||
if (!user)
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: 'User not found',
|
||||
},
|
||||
{ status: HttpStatusCode.NotFound }
|
||||
);
|
||||
|
||||
const platforms: UserPlatforms = {};
|
||||
|
||||
for (const [name, platform] of Object.entries(user.platforms)) {
|
||||
const icon = await getPlatformIcon(platform);
|
||||
if (!icon) continue;
|
||||
platforms[name] = {
|
||||
...platform,
|
||||
icon,
|
||||
};
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: 'ok',
|
||||
platforms,
|
||||
},
|
||||
{ status: HttpStatusCode.Ok }
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user