Make it public!
This commit is contained in:
41
routes/api/contacts/add.tsx
Normal file
41
routes/api/contacts/add.tsx
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { Contact, FreshContextState } from '/lib/types.ts';
|
||||
import { createContact, getContacts } from '/lib/data/contacts.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
page: number;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
contacts: Pick<Contact, 'id' | 'first_name' | 'last_name'>[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (requestBody.firstName) {
|
||||
const contact = await createContact(context.state.user.id, requestBody.firstName, requestBody.lastName);
|
||||
|
||||
if (!contact) {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
}
|
||||
|
||||
const contacts = await getContacts(context.state.user.id, requestBody.page - 1);
|
||||
|
||||
const responseBody: ResponseBody = { success: true, contacts };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
42
routes/api/contacts/delete.tsx
Normal file
42
routes/api/contacts/delete.tsx
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { Contact, FreshContextState } from '/lib/types.ts';
|
||||
import { deleteContact, getContact, getContacts } from '/lib/data/contacts.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
contactId: string;
|
||||
page: number;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
contacts: Pick<Contact, 'id' | 'first_name' | 'last_name'>[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (requestBody.contactId) {
|
||||
const contact = await getContact(requestBody.contactId, context.state.user.id);
|
||||
|
||||
if (!contact) {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
|
||||
await deleteContact(requestBody.contactId, context.state.user.id);
|
||||
}
|
||||
|
||||
const contacts = await getContacts(context.state.user.id, requestBody.page - 1);
|
||||
|
||||
const responseBody: ResponseBody = { success: true, contacts };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
27
routes/api/contacts/get.tsx
Normal file
27
routes/api/contacts/get.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { Contact, FreshContextState } from '/lib/types.ts';
|
||||
import { getAllContacts } from '/lib/data/contacts.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
contacts: Contact[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const contacts = await getAllContacts(context.state.user.id);
|
||||
|
||||
const responseBody: ResponseBody = { success: true, contacts };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
60
routes/api/contacts/import.tsx
Normal file
60
routes/api/contacts/import.tsx
Normal file
@@ -0,0 +1,60 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { Contact, FreshContextState } from '/lib/types.ts';
|
||||
import { concurrentPromises } from '/lib/utils.ts';
|
||||
import { createContact, getContacts, updateContact } from '/lib/data/contacts.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
partialContacts: Partial<Contact>[];
|
||||
page: number;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
contacts: Pick<Contact, 'id' | 'first_name' | 'last_name'>[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (requestBody.partialContacts) {
|
||||
if (requestBody.partialContacts.length === 0) {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
|
||||
await concurrentPromises(
|
||||
requestBody.partialContacts.map((partialContact) => async () => {
|
||||
if (partialContact.first_name) {
|
||||
const contact = await createContact(
|
||||
context.state.user!.id,
|
||||
partialContact.first_name,
|
||||
partialContact.last_name || '',
|
||||
);
|
||||
|
||||
const parsedExtra = JSON.stringify(partialContact.extra || {});
|
||||
|
||||
if (parsedExtra !== '{}') {
|
||||
contact.extra = partialContact.extra!;
|
||||
|
||||
await updateContact(contact);
|
||||
}
|
||||
}
|
||||
}),
|
||||
5,
|
||||
);
|
||||
}
|
||||
|
||||
const contacts = await getContacts(context.state.user.id, requestBody.page - 1);
|
||||
|
||||
const responseBody: ResponseBody = { success: true, contacts };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
40
routes/api/dashboard/save-links.tsx
Normal file
40
routes/api/dashboard/save-links.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { DashboardLink, FreshContextState } from '/lib/types.ts';
|
||||
import { getDashboardByUserId, updateDashboard } from '/lib/data/dashboard.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
links: DashboardLink[];
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const userDashboard = await getDashboardByUserId(context.state.user.id);
|
||||
|
||||
if (!userDashboard) {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (typeof requestBody.links !== 'undefined') {
|
||||
userDashboard.data.links = requestBody.links;
|
||||
|
||||
await updateDashboard(userDashboard);
|
||||
}
|
||||
|
||||
const responseBody: ResponseBody = { success: true };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
40
routes/api/dashboard/save-notes.tsx
Normal file
40
routes/api/dashboard/save-notes.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { FreshContextState } from '/lib/types.ts';
|
||||
import { getDashboardByUserId, updateDashboard } from '/lib/data/dashboard.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
notes: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const userDashboard = await getDashboardByUserId(context.state.user.id);
|
||||
|
||||
if (!userDashboard) {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (typeof requestBody.notes !== 'undefined' && userDashboard.data.notes !== requestBody.notes) {
|
||||
userDashboard.data.notes = requestBody.notes;
|
||||
|
||||
await updateDashboard(userDashboard);
|
||||
}
|
||||
|
||||
const responseBody: ResponseBody = { success: true };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
42
routes/api/news/add-feed.tsx
Normal file
42
routes/api/news/add-feed.tsx
Normal file
@@ -0,0 +1,42 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { FreshContextState, NewsFeed } from '/lib/types.ts';
|
||||
import { createNewsFeed, getNewsFeeds } from '/lib/data/news.ts';
|
||||
import { fetchNewArticles } from '/crons/news.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
feedUrl: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newFeeds: NewsFeed[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (requestBody.feedUrl) {
|
||||
const newFeed = await createNewsFeed(context.state.user.id, requestBody.feedUrl);
|
||||
|
||||
if (!newFeed) {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
}
|
||||
|
||||
await fetchNewArticles();
|
||||
|
||||
const newFeeds = await getNewsFeeds(context.state.user.id);
|
||||
|
||||
const responseBody: ResponseBody = { success: true, newFeeds };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
41
routes/api/news/delete-feed.tsx
Normal file
41
routes/api/news/delete-feed.tsx
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { FreshContextState, NewsFeed } from '/lib/types.ts';
|
||||
import { deleteNewsFeed, getNewsFeed, getNewsFeeds } from '/lib/data/news.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
feedId: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newFeeds: NewsFeed[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (requestBody.feedId) {
|
||||
const newsFeed = await getNewsFeed(requestBody.feedId, context.state.user.id);
|
||||
|
||||
if (!newsFeed) {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
|
||||
await deleteNewsFeed(requestBody.feedId);
|
||||
}
|
||||
|
||||
const newFeeds = await getNewsFeeds(context.state.user.id);
|
||||
|
||||
const responseBody: ResponseBody = { success: true, newFeeds };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
46
routes/api/news/import-feeds.tsx
Normal file
46
routes/api/news/import-feeds.tsx
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { FreshContextState, NewsFeed } from '/lib/types.ts';
|
||||
import { concurrentPromises } from '/lib/utils.ts';
|
||||
import { createNewsFeed, getNewsFeeds } from '/lib/data/news.ts';
|
||||
import { fetchNewArticles } from '/crons/news.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
feedUrls: string[];
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newFeeds: NewsFeed[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (requestBody.feedUrls) {
|
||||
if (requestBody.feedUrls.length === 0) {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
|
||||
await concurrentPromises(
|
||||
requestBody.feedUrls.map((feedUrl) => () => createNewsFeed(context.state.user!.id, feedUrl)),
|
||||
5,
|
||||
);
|
||||
}
|
||||
|
||||
await fetchNewArticles();
|
||||
|
||||
const newFeeds = await getNewsFeeds(context.state.user.id);
|
||||
|
||||
const responseBody: ResponseBody = { success: true, newFeeds };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
44
routes/api/news/mark-read.tsx
Normal file
44
routes/api/news/mark-read.tsx
Normal file
@@ -0,0 +1,44 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { FreshContextState } from '/lib/types.ts';
|
||||
import { getNewsArticle, markAllArticlesRead, updateNewsArticle } from '/lib/data/news.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {
|
||||
articleId: string;
|
||||
}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const requestBody = await request.clone().json() as RequestBody;
|
||||
|
||||
if (requestBody.articleId) {
|
||||
if (requestBody.articleId === 'all') {
|
||||
await markAllArticlesRead(context.state.user.id);
|
||||
} else {
|
||||
const article = await getNewsArticle(requestBody.articleId, context.state.user.id);
|
||||
|
||||
if (!article) {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
|
||||
article.is_read = true;
|
||||
|
||||
await updateNewsArticle(article);
|
||||
}
|
||||
}
|
||||
|
||||
const responseBody: ResponseBody = { success: true };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
36
routes/api/news/refresh-articles.tsx
Normal file
36
routes/api/news/refresh-articles.tsx
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Handlers } from 'fresh/server.ts';
|
||||
|
||||
import { FreshContextState, NewsFeedArticle } from '/lib/types.ts';
|
||||
import { getNewsArticles, getNewsFeeds } from '/lib/data/news.ts';
|
||||
import { fetchNewArticles } from '/crons/news.ts';
|
||||
|
||||
interface Data {}
|
||||
|
||||
export interface RequestBody {}
|
||||
|
||||
export interface ResponseBody {
|
||||
success: boolean;
|
||||
newArticles: NewsFeedArticle[];
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data, FreshContextState> = {
|
||||
async POST(request, context) {
|
||||
if (!context.state.user) {
|
||||
return new Response('Unauthorized', { status: 401 });
|
||||
}
|
||||
|
||||
const newsFeeds = await getNewsFeeds(context.state.user.id);
|
||||
|
||||
if (!newsFeeds.length) {
|
||||
return new Response('Not found', { status: 404 });
|
||||
}
|
||||
|
||||
await fetchNewArticles(true);
|
||||
|
||||
const newArticles = await getNewsArticles(context.state.user.id);
|
||||
|
||||
const responseBody: ResponseBody = { success: true, newArticles };
|
||||
|
||||
return new Response(JSON.stringify(responseBody));
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user