128
packages/backend/src/server/api/endpoints/pages/create.ts
Normal file
128
packages/backend/src/server/api/endpoints/pages/create.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import $ from 'cafy';
|
||||
import * as ms from 'ms';
|
||||
import define from '../../define';
|
||||
import { ID } from '@/misc/cafy-id';
|
||||
import { Pages, DriveFiles } from '@/models/index';
|
||||
import { genId } from '@/misc/gen-id';
|
||||
import { Page } from '@/models/entities/page';
|
||||
import { ApiError } from '../../error';
|
||||
|
||||
export const meta = {
|
||||
tags: ['pages'],
|
||||
|
||||
requireCredential: true as const,
|
||||
|
||||
kind: 'write:pages',
|
||||
|
||||
limit: {
|
||||
duration: ms('1hour'),
|
||||
max: 300
|
||||
},
|
||||
|
||||
params: {
|
||||
title: {
|
||||
validator: $.str,
|
||||
},
|
||||
|
||||
name: {
|
||||
validator: $.str.min(1),
|
||||
},
|
||||
|
||||
summary: {
|
||||
validator: $.optional.nullable.str,
|
||||
},
|
||||
|
||||
content: {
|
||||
validator: $.arr($.obj())
|
||||
},
|
||||
|
||||
variables: {
|
||||
validator: $.arr($.obj())
|
||||
},
|
||||
|
||||
script: {
|
||||
validator: $.str,
|
||||
},
|
||||
|
||||
eyeCatchingImageId: {
|
||||
validator: $.optional.nullable.type(ID),
|
||||
},
|
||||
|
||||
font: {
|
||||
validator: $.optional.str.or(['serif', 'sans-serif']),
|
||||
default: 'sans-serif'
|
||||
},
|
||||
|
||||
alignCenter: {
|
||||
validator: $.optional.bool,
|
||||
default: false
|
||||
},
|
||||
|
||||
hideTitleWhenPinned: {
|
||||
validator: $.optional.bool,
|
||||
default: false
|
||||
},
|
||||
},
|
||||
|
||||
res: {
|
||||
type: 'object' as const,
|
||||
optional: false as const, nullable: false as const,
|
||||
ref: 'Page',
|
||||
},
|
||||
|
||||
errors: {
|
||||
noSuchFile: {
|
||||
message: 'No such file.',
|
||||
code: 'NO_SUCH_FILE',
|
||||
id: 'b7b97489-0f66-4b12-a5ff-b21bd63f6e1c'
|
||||
},
|
||||
nameAlreadyExists: {
|
||||
message: 'Specified name already exists.',
|
||||
code: 'NAME_ALREADY_EXISTS',
|
||||
id: '4650348e-301c-499a-83c9-6aa988c66bc1'
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default define(meta, async (ps, user) => {
|
||||
let eyeCatchingImage = null;
|
||||
if (ps.eyeCatchingImageId != null) {
|
||||
eyeCatchingImage = await DriveFiles.findOne({
|
||||
id: ps.eyeCatchingImageId,
|
||||
userId: user.id
|
||||
});
|
||||
|
||||
if (eyeCatchingImage == null) {
|
||||
throw new ApiError(meta.errors.noSuchFile);
|
||||
}
|
||||
}
|
||||
|
||||
await Pages.find({
|
||||
userId: user.id,
|
||||
name: ps.name
|
||||
}).then(result => {
|
||||
if (result.length > 0) {
|
||||
throw new ApiError(meta.errors.nameAlreadyExists);
|
||||
}
|
||||
});
|
||||
|
||||
const page = await Pages.save(new Page({
|
||||
id: genId(),
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
title: ps.title,
|
||||
name: ps.name,
|
||||
summary: ps.summary,
|
||||
content: ps.content,
|
||||
variables: ps.variables,
|
||||
script: ps.script,
|
||||
eyeCatchingImageId: eyeCatchingImage ? eyeCatchingImage.id : null,
|
||||
userId: user.id,
|
||||
visibility: 'public',
|
||||
alignCenter: ps.alignCenter,
|
||||
hideTitleWhenPinned: ps.hideTitleWhenPinned,
|
||||
font: ps.font
|
||||
}));
|
||||
|
||||
return await Pages.pack(page);
|
||||
});
|
45
packages/backend/src/server/api/endpoints/pages/delete.ts
Normal file
45
packages/backend/src/server/api/endpoints/pages/delete.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import $ from 'cafy';
|
||||
import define from '../../define';
|
||||
import { ApiError } from '../../error';
|
||||
import { Pages } from '@/models/index';
|
||||
import { ID } from '@/misc/cafy-id';
|
||||
|
||||
export const meta = {
|
||||
tags: ['pages'],
|
||||
|
||||
requireCredential: true as const,
|
||||
|
||||
kind: 'write:pages',
|
||||
|
||||
params: {
|
||||
pageId: {
|
||||
validator: $.type(ID),
|
||||
},
|
||||
},
|
||||
|
||||
errors: {
|
||||
noSuchPage: {
|
||||
message: 'No such page.',
|
||||
code: 'NO_SUCH_PAGE',
|
||||
id: 'eb0c6e1d-d519-4764-9486-52a7e1c6392a'
|
||||
},
|
||||
|
||||
accessDenied: {
|
||||
message: 'Access denied.',
|
||||
code: 'ACCESS_DENIED',
|
||||
id: '8b741b3e-2c22-44b3-a15f-29949aa1601e'
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
export default define(meta, async (ps, user) => {
|
||||
const page = await Pages.findOne(ps.pageId);
|
||||
if (page == null) {
|
||||
throw new ApiError(meta.errors.noSuchPage);
|
||||
}
|
||||
if (page.userId !== user.id) {
|
||||
throw new ApiError(meta.errors.accessDenied);
|
||||
}
|
||||
|
||||
await Pages.delete(page.id);
|
||||
});
|
29
packages/backend/src/server/api/endpoints/pages/featured.ts
Normal file
29
packages/backend/src/server/api/endpoints/pages/featured.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import define from '../../define';
|
||||
import { Pages } from '@/models/index';
|
||||
|
||||
export const meta = {
|
||||
tags: ['pages'],
|
||||
|
||||
requireCredential: false as const,
|
||||
|
||||
res: {
|
||||
type: 'array' as const,
|
||||
optional: false as const, nullable: false as const,
|
||||
items: {
|
||||
type: 'object' as const,
|
||||
optional: false as const, nullable: false as const,
|
||||
ref: 'Page',
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default define(meta, async (ps, me) => {
|
||||
const query = Pages.createQueryBuilder('page')
|
||||
.where('page.visibility = \'public\'')
|
||||
.andWhere('page.likedCount > 0')
|
||||
.orderBy('page.likedCount', 'DESC');
|
||||
|
||||
const pages = await query.take(10).getMany();
|
||||
|
||||
return await Pages.packMany(pages, me);
|
||||
});
|
71
packages/backend/src/server/api/endpoints/pages/like.ts
Normal file
71
packages/backend/src/server/api/endpoints/pages/like.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import $ from 'cafy';
|
||||
import { ID } from '@/misc/cafy-id';
|
||||
import define from '../../define';
|
||||
import { ApiError } from '../../error';
|
||||
import { Pages, PageLikes } from '@/models/index';
|
||||
import { genId } from '@/misc/gen-id';
|
||||
|
||||
export const meta = {
|
||||
tags: ['pages'],
|
||||
|
||||
requireCredential: true as const,
|
||||
|
||||
kind: 'write:page-likes',
|
||||
|
||||
params: {
|
||||
pageId: {
|
||||
validator: $.type(ID),
|
||||
}
|
||||
},
|
||||
|
||||
errors: {
|
||||
noSuchPage: {
|
||||
message: 'No such page.',
|
||||
code: 'NO_SUCH_PAGE',
|
||||
id: 'cc98a8a2-0dc3-4123-b198-62c71df18ed3'
|
||||
},
|
||||
|
||||
yourPage: {
|
||||
message: 'You cannot like your page.',
|
||||
code: 'YOUR_PAGE',
|
||||
id: '28800466-e6db-40f2-8fae-bf9e82aa92b8'
|
||||
},
|
||||
|
||||
alreadyLiked: {
|
||||
message: 'The page has already been liked.',
|
||||
code: 'ALREADY_LIKED',
|
||||
id: 'cc98a8a2-0dc3-4123-b198-62c71df18ed3'
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
export default define(meta, async (ps, user) => {
|
||||
const page = await Pages.findOne(ps.pageId);
|
||||
if (page == null) {
|
||||
throw new ApiError(meta.errors.noSuchPage);
|
||||
}
|
||||
|
||||
if (page.userId === user.id) {
|
||||
throw new ApiError(meta.errors.yourPage);
|
||||
}
|
||||
|
||||
// if already liked
|
||||
const exist = await PageLikes.findOne({
|
||||
pageId: page.id,
|
||||
userId: user.id
|
||||
});
|
||||
|
||||
if (exist != null) {
|
||||
throw new ApiError(meta.errors.alreadyLiked);
|
||||
}
|
||||
|
||||
// Create like
|
||||
await PageLikes.insert({
|
||||
id: genId(),
|
||||
createdAt: new Date(),
|
||||
pageId: page.id,
|
||||
userId: user.id
|
||||
});
|
||||
|
||||
Pages.increment({ id: page.id }, 'likedCount', 1);
|
||||
});
|
65
packages/backend/src/server/api/endpoints/pages/show.ts
Normal file
65
packages/backend/src/server/api/endpoints/pages/show.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import $ from 'cafy';
|
||||
import define from '../../define';
|
||||
import { ApiError } from '../../error';
|
||||
import { Pages, Users } from '@/models/index';
|
||||
import { ID } from '@/misc/cafy-id';
|
||||
import { Page } from '@/models/entities/page';
|
||||
|
||||
export const meta = {
|
||||
tags: ['pages'],
|
||||
|
||||
requireCredential: false as const,
|
||||
|
||||
params: {
|
||||
pageId: {
|
||||
validator: $.optional.type(ID),
|
||||
},
|
||||
|
||||
name: {
|
||||
validator: $.optional.str,
|
||||
},
|
||||
|
||||
username: {
|
||||
validator: $.optional.str,
|
||||
},
|
||||
},
|
||||
|
||||
res: {
|
||||
type: 'object' as const,
|
||||
optional: false as const, nullable: false as const,
|
||||
ref: 'Page',
|
||||
},
|
||||
|
||||
errors: {
|
||||
noSuchPage: {
|
||||
message: 'No such page.',
|
||||
code: 'NO_SUCH_PAGE',
|
||||
id: '222120c0-3ead-4528-811b-b96f233388d7'
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default define(meta, async (ps, user) => {
|
||||
let page: Page | undefined;
|
||||
|
||||
if (ps.pageId) {
|
||||
page = await Pages.findOne(ps.pageId);
|
||||
} else if (ps.name && ps.username) {
|
||||
const author = await Users.findOne({
|
||||
host: null,
|
||||
usernameLower: ps.username.toLowerCase()
|
||||
});
|
||||
if (author) {
|
||||
page = await Pages.findOne({
|
||||
name: ps.name,
|
||||
userId: author.id
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (page == null) {
|
||||
throw new ApiError(meta.errors.noSuchPage);
|
||||
}
|
||||
|
||||
return await Pages.pack(page, user);
|
||||
});
|
54
packages/backend/src/server/api/endpoints/pages/unlike.ts
Normal file
54
packages/backend/src/server/api/endpoints/pages/unlike.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import $ from 'cafy';
|
||||
import { ID } from '@/misc/cafy-id';
|
||||
import define from '../../define';
|
||||
import { ApiError } from '../../error';
|
||||
import { Pages, PageLikes } from '@/models/index';
|
||||
|
||||
export const meta = {
|
||||
tags: ['pages'],
|
||||
|
||||
requireCredential: true as const,
|
||||
|
||||
kind: 'write:page-likes',
|
||||
|
||||
params: {
|
||||
pageId: {
|
||||
validator: $.type(ID),
|
||||
}
|
||||
},
|
||||
|
||||
errors: {
|
||||
noSuchPage: {
|
||||
message: 'No such page.',
|
||||
code: 'NO_SUCH_PAGE',
|
||||
id: 'a0d41e20-1993-40bd-890e-f6e560ae648e'
|
||||
},
|
||||
|
||||
notLiked: {
|
||||
message: 'You have not liked that page.',
|
||||
code: 'NOT_LIKED',
|
||||
id: 'f5e586b0-ce93-4050-b0e3-7f31af5259ee'
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
export default define(meta, async (ps, user) => {
|
||||
const page = await Pages.findOne(ps.pageId);
|
||||
if (page == null) {
|
||||
throw new ApiError(meta.errors.noSuchPage);
|
||||
}
|
||||
|
||||
const exist = await PageLikes.findOne({
|
||||
pageId: page.id,
|
||||
userId: user.id
|
||||
});
|
||||
|
||||
if (exist == null) {
|
||||
throw new ApiError(meta.errors.notLiked);
|
||||
}
|
||||
|
||||
// Delete like
|
||||
await PageLikes.delete(exist.id);
|
||||
|
||||
Pages.decrement({ id: page.id }, 'likedCount', 1);
|
||||
});
|
141
packages/backend/src/server/api/endpoints/pages/update.ts
Normal file
141
packages/backend/src/server/api/endpoints/pages/update.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
import $ from 'cafy';
|
||||
import * as ms from 'ms';
|
||||
import define from '../../define';
|
||||
import { ApiError } from '../../error';
|
||||
import { Pages, DriveFiles } from '@/models/index';
|
||||
import { ID } from '@/misc/cafy-id';
|
||||
import { Not } from 'typeorm';
|
||||
|
||||
export const meta = {
|
||||
tags: ['pages'],
|
||||
|
||||
requireCredential: true as const,
|
||||
|
||||
kind: 'write:pages',
|
||||
|
||||
limit: {
|
||||
duration: ms('1hour'),
|
||||
max: 300
|
||||
},
|
||||
|
||||
params: {
|
||||
pageId: {
|
||||
validator: $.type(ID),
|
||||
},
|
||||
|
||||
title: {
|
||||
validator: $.str,
|
||||
},
|
||||
|
||||
name: {
|
||||
validator: $.str.min(1),
|
||||
},
|
||||
|
||||
summary: {
|
||||
validator: $.optional.nullable.str,
|
||||
},
|
||||
|
||||
content: {
|
||||
validator: $.arr($.obj())
|
||||
},
|
||||
|
||||
variables: {
|
||||
validator: $.arr($.obj())
|
||||
},
|
||||
|
||||
script: {
|
||||
validator: $.str,
|
||||
},
|
||||
|
||||
eyeCatchingImageId: {
|
||||
validator: $.optional.nullable.type(ID),
|
||||
},
|
||||
|
||||
font: {
|
||||
validator: $.optional.str.or(['serif', 'sans-serif']),
|
||||
},
|
||||
|
||||
alignCenter: {
|
||||
validator: $.optional.bool,
|
||||
},
|
||||
|
||||
hideTitleWhenPinned: {
|
||||
validator: $.optional.bool,
|
||||
},
|
||||
},
|
||||
|
||||
errors: {
|
||||
noSuchPage: {
|
||||
message: 'No such page.',
|
||||
code: 'NO_SUCH_PAGE',
|
||||
id: '21149b9e-3616-4778-9592-c4ce89f5a864'
|
||||
},
|
||||
|
||||
accessDenied: {
|
||||
message: 'Access denied.',
|
||||
code: 'ACCESS_DENIED',
|
||||
id: '3c15cd52-3b4b-4274-967d-6456fc4f792b'
|
||||
},
|
||||
|
||||
noSuchFile: {
|
||||
message: 'No such file.',
|
||||
code: 'NO_SUCH_FILE',
|
||||
id: 'cfc23c7c-3887-490e-af30-0ed576703c82'
|
||||
},
|
||||
nameAlreadyExists: {
|
||||
message: 'Specified name already exists.',
|
||||
code: 'NAME_ALREADY_EXISTS',
|
||||
id: '2298a392-d4a1-44c5-9ebb-ac1aeaa5a9ab'
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default define(meta, async (ps, user) => {
|
||||
const page = await Pages.findOne(ps.pageId);
|
||||
if (page == null) {
|
||||
throw new ApiError(meta.errors.noSuchPage);
|
||||
}
|
||||
if (page.userId !== user.id) {
|
||||
throw new ApiError(meta.errors.accessDenied);
|
||||
}
|
||||
|
||||
let eyeCatchingImage = null;
|
||||
if (ps.eyeCatchingImageId != null) {
|
||||
eyeCatchingImage = await DriveFiles.findOne({
|
||||
id: ps.eyeCatchingImageId,
|
||||
userId: user.id
|
||||
});
|
||||
|
||||
if (eyeCatchingImage == null) {
|
||||
throw new ApiError(meta.errors.noSuchFile);
|
||||
}
|
||||
}
|
||||
|
||||
await Pages.find({
|
||||
id: Not(ps.pageId),
|
||||
userId: user.id,
|
||||
name: ps.name
|
||||
}).then(result => {
|
||||
if (result.length > 0) {
|
||||
throw new ApiError(meta.errors.nameAlreadyExists);
|
||||
}
|
||||
});
|
||||
|
||||
await Pages.update(page.id, {
|
||||
updatedAt: new Date(),
|
||||
title: ps.title,
|
||||
name: ps.name === undefined ? page.name : ps.name,
|
||||
summary: ps.name === undefined ? page.summary : ps.summary,
|
||||
content: ps.content,
|
||||
variables: ps.variables,
|
||||
script: ps.script,
|
||||
alignCenter: ps.alignCenter === undefined ? page.alignCenter : ps.alignCenter,
|
||||
hideTitleWhenPinned: ps.hideTitleWhenPinned === undefined ? page.hideTitleWhenPinned : ps.hideTitleWhenPinned,
|
||||
font: ps.font === undefined ? page.font : ps.font,
|
||||
eyeCatchingImageId: ps.eyeCatchingImageId === null
|
||||
? null
|
||||
: ps.eyeCatchingImageId === undefined
|
||||
? page.eyeCatchingImageId
|
||||
: eyeCatchingImage!.id,
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user