125 lines
3.9 KiB
TypeScript
125 lines
3.9 KiB
TypeScript
// x-poster.controller.ts
|
|
import {Body, Controller, Get, HttpException, Post, Query, Req} from '@nestjs/common';
|
|
import {CreateTweetDto, ReplyTweetDto} from './dto/create-tweet.dto';
|
|
import {XPosterRouterService} from "./x-poster.router.service";
|
|
import {XCookieService} from "./x-cookie.service";
|
|
import {XApiService} from "./x-api.service";
|
|
import {XCacheService} from "../x-cache/x-cache.service";
|
|
import {XBrowserService} from "./x-browser.service";
|
|
|
|
@Controller('')
|
|
export class XPosterController {
|
|
constructor(
|
|
private readonly service: XPosterRouterService,
|
|
private readonly xCookieService: XCookieService,
|
|
private readonly xBrowserService: XBrowserService,
|
|
private readonly xCacheService: XCacheService,
|
|
private readonly xApiService: XApiService
|
|
) {
|
|
}
|
|
|
|
|
|
@Get('tw_callback')
|
|
async twitterAuthCallback(
|
|
@Query('code') code: string,
|
|
@Query('state') state: string,
|
|
@Req() request: Request) {
|
|
|
|
console.log('twitterAuthCallback==>')
|
|
const cacheAuthData = await this.xCacheService.getCacheTwAuthorize();
|
|
console.log({cacheAuthData});
|
|
//@ts-ignore
|
|
const {codeVerifier, state: sessionState} = cacheAuthData || {}
|
|
|
|
if (!codeVerifier || !state || !sessionState || !code) {
|
|
throw new HttpException('You denied the app or your session expired!', 400)
|
|
}
|
|
if (state !== sessionState) {
|
|
throw new HttpException('Stored tokens didnt match!!', 400)
|
|
}
|
|
|
|
const client = await this.xApiService.getTwitterClientV2();
|
|
|
|
const {client: loggedClient, accessToken, refreshToken} = await client.loginWithOAuth2({
|
|
code,
|
|
codeVerifier: codeVerifier,
|
|
redirectUri: `http://localhost:${process.env.PORT}/tw_callback`
|
|
});
|
|
|
|
await this.xCacheService.setCacheTwAccessToken(accessToken);
|
|
await this.xCacheService.setCacheTwRefreshToken('' + refreshToken);
|
|
console.log({loggedClient, accessToken, refreshToken});
|
|
// @ts-ignore
|
|
const {data: userObject} = loggedClient.v2.me();
|
|
|
|
return userObject;
|
|
|
|
}
|
|
|
|
@Get('tw_authorize')
|
|
async twitterAuthorize() {
|
|
console.log('twitterAuthorize==>', await this.xCacheService.getCacheTwAuthorize());
|
|
|
|
const client = await this.xApiService.getTwitterClientV2();
|
|
const {
|
|
url,
|
|
codeVerifier,
|
|
state
|
|
} = client.generateOAuth2AuthLink(`http://localhost:${process.env.PORT}/tw_callback`,
|
|
{
|
|
scope:
|
|
[
|
|
'tweet.read',
|
|
'tweet.write',
|
|
'users.read',
|
|
'offline.access',
|
|
'media.write',
|
|
|
|
]
|
|
});
|
|
// Redirect your client to {url}
|
|
console.log('Please go to', url);
|
|
console.log({codeVerifier, state});
|
|
await this.xCacheService.setCacheTwAuthorize({codeVerifier, state});
|
|
|
|
return {url, codeVerifier, state};
|
|
}
|
|
|
|
|
|
@Get('/tw/me')
|
|
async testTwitterMe() {
|
|
const client = await this.xApiService.getTwitterClientV2ViaAccessToken();
|
|
return client.v2.me();
|
|
}
|
|
|
|
|
|
@Post('xreply')
|
|
async xreply(
|
|
@Body() tweet: ReplyTweetDto,
|
|
) {
|
|
|
|
// return this.xBrowserService.postReply(account, tweet.tweetUrl, tweet.text)
|
|
}
|
|
|
|
@Post('tweet')
|
|
async tweet(
|
|
@Body() tweet: CreateTweetDto,
|
|
) {
|
|
}
|
|
|
|
@Get('like')
|
|
async likeTweet(@Query('xurl') url: string) {
|
|
console.log('xurl==>', url);
|
|
if (!url) {
|
|
throw new HttpException('xUrl not found', 400);
|
|
}
|
|
await this.xBrowserService.likeTweet(url);
|
|
return 'done';
|
|
// const account = {
|
|
// authToken: process.env.X_COOKIE_AUTH_TOKEN!, // auth_token cookie
|
|
// ct0: process.env.X_COOKIE_CT0!,
|
|
// }
|
|
// return this.xCookieService.verifyCookie(account);
|
|
}
|
|
}
|