session based posting
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
'use client';
|
||||
|
||||
import * as React from 'react';
|
||||
import { ThemeProvider as NextThemesProvider } from 'next-themes';
|
||||
import { type ThemeProviderProps } from 'next-themes';
|
||||
|
||||
export function ThemeProvider({ children, ...props }: ThemeProviderProps) {
|
||||
return <NextThemesProvider {...props}>{children}</NextThemesProvider>;
|
||||
}
|
||||
@@ -28,7 +28,7 @@ export async function POST(req: NextRequest) {
|
||||
{ status: 409 }
|
||||
);
|
||||
|
||||
await database.users.insertOne({ email, password, name, platforms: [] });
|
||||
await database.users.insertOne({ email, password, name, platforms: {} });
|
||||
|
||||
const sessionId = (await database.sessions.insertOne({ user: email }))
|
||||
.insertedId;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { MongoClient } from 'mongodb';
|
||||
import config from './env';
|
||||
import { Schedule, Session, User } from '@/utils';
|
||||
import { Schedule, ScheduleRequest, Session, User } from '@/utils';
|
||||
|
||||
const client = new MongoClient(config.mongoUrl);
|
||||
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
import { TwitterApi } from 'twitter-api-v2';
|
||||
import config from '@/app/api/env';
|
||||
import { inspect } from 'util';
|
||||
import { PostRequest } from '@/utils';
|
||||
import { Post } from '@/utils';
|
||||
import database from '../database';
|
||||
import { format } from 'date-fns';
|
||||
import cron from 'node-cron';
|
||||
import { ObjectId } from 'mongodb';
|
||||
|
||||
export default async function post({ platforms, content }: PostRequest) {
|
||||
export default async function post({ platforms, content, account }: Post) {
|
||||
try {
|
||||
for (const platform of platforms) {
|
||||
const user = await database.users.findOne({
|
||||
_id: new ObjectId(account),
|
||||
});
|
||||
|
||||
if (!user) return;
|
||||
for (const platformKey of platforms) {
|
||||
const platform = user.platforms[platformKey];
|
||||
switch (platform.platform) {
|
||||
// X / Twitter
|
||||
case 'x':
|
||||
|
||||
@@ -1,14 +1,39 @@
|
||||
import { NextResponse, NextRequest } from 'next/server';
|
||||
import database from '../database';
|
||||
import { PostRequest, Schedule } from '@/utils';
|
||||
import { PostRequest, ScheduleRequest } from '@/utils';
|
||||
import post from './post';
|
||||
import { ObjectId } from 'mongodb';
|
||||
import { use } from 'react';
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
const request: PostRequest = await req.json();
|
||||
const { sessionId, content, platforms }: PostRequest = await req.json();
|
||||
|
||||
post(request);
|
||||
console.log(sessionId);
|
||||
|
||||
if (typeof sessionId !== 'string')
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: 'Invalid structure, required param `id=`',
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
|
||||
const session = await database.sessions.findOne({
|
||||
_id: new ObjectId(sessionId),
|
||||
});
|
||||
|
||||
if (!session)
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: 'Invalid session',
|
||||
},
|
||||
{ status: 404 }
|
||||
);
|
||||
|
||||
post({
|
||||
account: session.user,
|
||||
content,
|
||||
platforms,
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
@@ -19,9 +44,35 @@ export async function POST(req: NextRequest) {
|
||||
}
|
||||
|
||||
export async function PUT(req: NextRequest) {
|
||||
const request: Schedule = await req.json();
|
||||
const { sessionId, scheduled, content, platforms }: ScheduleRequest =
|
||||
await req.json();
|
||||
|
||||
database.schedules.insertOne(request);
|
||||
if (typeof sessionId !== 'string')
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: 'Invalid structure, required param `id=`',
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
|
||||
const session = await database.sessions.findOne({
|
||||
_id: new ObjectId(sessionId),
|
||||
});
|
||||
|
||||
if (!session)
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: 'Invalid session',
|
||||
},
|
||||
{ status: 404 }
|
||||
);
|
||||
|
||||
database.schedules.insertOne({
|
||||
account: session.user,
|
||||
scheduled,
|
||||
content,
|
||||
platforms,
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
|
||||
@@ -22,7 +22,7 @@ import { format, parse } from 'date-fns';
|
||||
import PostDialog, { today } from './postDialog';
|
||||
import axios from 'axios';
|
||||
import Cookies from 'js-cookie';
|
||||
import { Schedule } from '@/utils';
|
||||
import { ScheduleRequest } from '@/utils';
|
||||
|
||||
function closestChild(children: any, clientX: number, clientY: number) {
|
||||
let closestElement = null;
|
||||
@@ -59,7 +59,7 @@ function CalendarApp() {
|
||||
useEffect(() => {
|
||||
axios.get(`/api/posts/?id=${sessionId}`).then((response) => {
|
||||
for (const [id, sched] of (
|
||||
response.data.schedules as Schedule[]
|
||||
response.data.schedules as ScheduleRequest[]
|
||||
).entries()) {
|
||||
const date = parse(
|
||||
sched.scheduled,
|
||||
@@ -160,6 +160,7 @@ function CalendarApp() {
|
||||
setEventCount(eventCount + 1);
|
||||
}}
|
||||
eventId={eventCount}
|
||||
sessionId={sessionId}
|
||||
/>
|
||||
<ContextMenu>
|
||||
<ContextMenuTrigger onContextMenu={handleDialog}>
|
||||
|
||||
@@ -25,7 +25,7 @@ import { Textarea } from '@/components/ui/textarea';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { useState } from 'react';
|
||||
import axios from 'axios';
|
||||
import { Schedule } from '@/utils';
|
||||
import { PostRequest, ScheduleRequest } from '@/utils';
|
||||
|
||||
type PostDialogProps = {
|
||||
newPostDialogOpen: boolean;
|
||||
@@ -34,6 +34,7 @@ type PostDialogProps = {
|
||||
setDate: (d: Date) => void;
|
||||
eventId: number;
|
||||
setEvents: (e: CalendarEventExternal) => void;
|
||||
sessionId: string;
|
||||
};
|
||||
|
||||
export const today = new Date();
|
||||
@@ -46,6 +47,7 @@ export default function PostDialog({
|
||||
setDate,
|
||||
setEvents: addEvent,
|
||||
eventId,
|
||||
sessionId,
|
||||
}: PostDialogProps) {
|
||||
const [content, setContent] = useState('');
|
||||
|
||||
@@ -133,10 +135,10 @@ export default function PostDialog({
|
||||
});
|
||||
axios.put('http://localhost:3000/api/posts', {
|
||||
scheduled: format(date, 'y-MM-dd hh:mm'),
|
||||
account: '',
|
||||
platforms: [],
|
||||
sessionId,
|
||||
platforms: ['andjksds'],
|
||||
content,
|
||||
} satisfies Schedule);
|
||||
} satisfies ScheduleRequest);
|
||||
|
||||
setNewPostDialogOpen(false);
|
||||
}}
|
||||
|
||||
+2
-2
@@ -1,8 +1,9 @@
|
||||
import type { Metadata } from 'next';
|
||||
import { cn } from '@/lib/utils';
|
||||
import './globals.css';
|
||||
import { ThemeProvider } from './ThemeProvider';
|
||||
import { Toaster } from '@/components/ui/toaster';
|
||||
import { ThemeProvider, useTheme } from 'next-themes';
|
||||
import ThemeToggle from '@/components/themeToggle';
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'Create Next App',
|
||||
@@ -24,7 +25,6 @@ export default function RootLayout({
|
||||
>
|
||||
<ThemeProvider
|
||||
attribute="class"
|
||||
defaultTheme="dark"
|
||||
enableSystem
|
||||
disableTransitionOnChange
|
||||
>
|
||||
|
||||
+26
-8
@@ -1,4 +1,20 @@
|
||||
import { ObjectId } from 'mongodb';
|
||||
/*
|
||||
Request types
|
||||
*/
|
||||
|
||||
export interface PostRequest {
|
||||
sessionId: string;
|
||||
content: string;
|
||||
platforms: string[];
|
||||
}
|
||||
|
||||
export interface ScheduleRequest extends PostRequest {
|
||||
scheduled: string;
|
||||
}
|
||||
|
||||
/*
|
||||
Database types
|
||||
*/
|
||||
|
||||
export type PlatformKind = 'x' | 'linkedin';
|
||||
|
||||
@@ -8,20 +24,22 @@ export interface Platform {
|
||||
secret: string;
|
||||
}
|
||||
|
||||
export interface PostRequest {
|
||||
account: string;
|
||||
content: string;
|
||||
platforms: Platform[];
|
||||
}
|
||||
export type Platforms = { [key: string]: Platform };
|
||||
|
||||
export interface User {
|
||||
name: string;
|
||||
email: string;
|
||||
password: string;
|
||||
platforms: Platform[];
|
||||
platforms: Platforms;
|
||||
}
|
||||
|
||||
export interface Schedule extends PostRequest {
|
||||
export interface Post {
|
||||
account: string;
|
||||
content: string;
|
||||
platforms: string[];
|
||||
}
|
||||
|
||||
export interface Schedule extends Post {
|
||||
scheduled: string;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user