summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/clips
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2021-11-12 02:02:25 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2021-11-12 02:02:25 +0900
commit0e4a111f81cceed275d9bec2695f6e401fb654d8 (patch)
tree40874799472fa07416f17b50a398ac33b7771905 /packages/backend/src/server/api/endpoints/clips
parentupdate deps (diff)
downloadsharkey-0e4a111f81cceed275d9bec2695f6e401fb654d8.tar.gz
sharkey-0e4a111f81cceed275d9bec2695f6e401fb654d8.tar.bz2
sharkey-0e4a111f81cceed275d9bec2695f6e401fb654d8.zip
refactoring
Resolve #7779
Diffstat (limited to 'packages/backend/src/server/api/endpoints/clips')
-rw-r--r--packages/backend/src/server/api/endpoints/clips/add-note.ts76
-rw-r--r--packages/backend/src/server/api/endpoints/clips/create.ts45
-rw-r--r--packages/backend/src/server/api/endpoints/clips/delete.ts40
-rw-r--r--packages/backend/src/server/api/endpoints/clips/list.ts28
-rw-r--r--packages/backend/src/server/api/endpoints/clips/notes.ts93
-rw-r--r--packages/backend/src/server/api/endpoints/clips/show.ts50
-rw-r--r--packages/backend/src/server/api/endpoints/clips/update.ts65
7 files changed, 397 insertions, 0 deletions
diff --git a/packages/backend/src/server/api/endpoints/clips/add-note.ts b/packages/backend/src/server/api/endpoints/clips/add-note.ts
new file mode 100644
index 0000000000..79d7b8adde
--- /dev/null
+++ b/packages/backend/src/server/api/endpoints/clips/add-note.ts
@@ -0,0 +1,76 @@
+import $ from 'cafy';
+import { ID } from '@/misc/cafy-id';
+import define from '../../define';
+import { ClipNotes, Clips } from '@/models/index';
+import { ApiError } from '../../error';
+import { genId } from '@/misc/gen-id';
+import { getNote } from '../../common/getters';
+
+export const meta = {
+ tags: ['account', 'notes', 'clips'],
+
+ requireCredential: true as const,
+
+ kind: 'write:account',
+
+ params: {
+ clipId: {
+ validator: $.type(ID),
+ },
+
+ noteId: {
+ validator: $.type(ID),
+ },
+ },
+
+ errors: {
+ noSuchClip: {
+ message: 'No such clip.',
+ code: 'NO_SUCH_CLIP',
+ id: 'd6e76cc0-a1b5-4c7c-a287-73fa9c716dcf'
+ },
+
+ noSuchNote: {
+ message: 'No such note.',
+ code: 'NO_SUCH_NOTE',
+ id: 'fc8c0b49-c7a3-4664-a0a6-b418d386bb8b'
+ },
+
+ alreadyClipped: {
+ message: 'The note has already been clipped.',
+ code: 'ALREADY_CLIPPED',
+ id: '734806c4-542c-463a-9311-15c512803965'
+ },
+ }
+};
+
+export default define(meta, async (ps, user) => {
+ const clip = await Clips.findOne({
+ id: ps.clipId,
+ userId: user.id
+ });
+
+ if (clip == null) {
+ throw new ApiError(meta.errors.noSuchClip);
+ }
+
+ const note = await getNote(ps.noteId).catch(e => {
+ if (e.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote);
+ throw e;
+ });
+
+ const exist = await ClipNotes.findOne({
+ noteId: note.id,
+ clipId: clip.id
+ });
+
+ if (exist != null) {
+ throw new ApiError(meta.errors.alreadyClipped);
+ }
+
+ await ClipNotes.insert({
+ id: genId(),
+ noteId: note.id,
+ clipId: clip.id
+ });
+});
diff --git a/packages/backend/src/server/api/endpoints/clips/create.ts b/packages/backend/src/server/api/endpoints/clips/create.ts
new file mode 100644
index 0000000000..02d2773709
--- /dev/null
+++ b/packages/backend/src/server/api/endpoints/clips/create.ts
@@ -0,0 +1,45 @@
+import $ from 'cafy';
+import define from '../../define';
+import { genId } from '@/misc/gen-id';
+import { Clips } from '@/models/index';
+
+export const meta = {
+ tags: ['clips'],
+
+ requireCredential: true as const,
+
+ kind: 'write:account',
+
+ params: {
+ name: {
+ validator: $.str.range(1, 100)
+ },
+
+ isPublic: {
+ validator: $.optional.bool
+ },
+
+ description: {
+ validator: $.optional.nullable.str.range(1, 2048)
+ }
+ },
+
+ res: {
+ type: 'object' as const,
+ optional: false as const, nullable: false as const,
+ ref: 'Clip'
+ }
+};
+
+export default define(meta, async (ps, user) => {
+ const clip = await Clips.insert({
+ id: genId(),
+ createdAt: new Date(),
+ userId: user.id,
+ name: ps.name,
+ isPublic: ps.isPublic,
+ description: ps.description,
+ }).then(x => Clips.findOneOrFail(x.identifiers[0]));
+
+ return await Clips.pack(clip);
+});
diff --git a/packages/backend/src/server/api/endpoints/clips/delete.ts b/packages/backend/src/server/api/endpoints/clips/delete.ts
new file mode 100644
index 0000000000..ca489af3bf
--- /dev/null
+++ b/packages/backend/src/server/api/endpoints/clips/delete.ts
@@ -0,0 +1,40 @@
+import $ from 'cafy';
+import { ID } from '@/misc/cafy-id';
+import define from '../../define';
+import { ApiError } from '../../error';
+import { Clips } from '@/models/index';
+
+export const meta = {
+ tags: ['clips'],
+
+ requireCredential: true as const,
+
+ kind: 'write:account',
+
+ params: {
+ clipId: {
+ validator: $.type(ID),
+ }
+ },
+
+ errors: {
+ noSuchClip: {
+ message: 'No such clip.',
+ code: 'NO_SUCH_CLIP',
+ id: '70ca08ba-6865-4630-b6fb-8494759aa754'
+ }
+ }
+};
+
+export default define(meta, async (ps, user) => {
+ const clip = await Clips.findOne({
+ id: ps.clipId,
+ userId: user.id
+ });
+
+ if (clip == null) {
+ throw new ApiError(meta.errors.noSuchClip);
+ }
+
+ await Clips.delete(clip.id);
+});
diff --git a/packages/backend/src/server/api/endpoints/clips/list.ts b/packages/backend/src/server/api/endpoints/clips/list.ts
new file mode 100644
index 0000000000..1f6db9b979
--- /dev/null
+++ b/packages/backend/src/server/api/endpoints/clips/list.ts
@@ -0,0 +1,28 @@
+import define from '../../define';
+import { Clips } from '@/models/index';
+
+export const meta = {
+ tags: ['clips', 'account'],
+
+ requireCredential: true as const,
+
+ kind: 'read:account',
+
+ 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: 'Clip'
+ }
+ }
+};
+
+export default define(meta, async (ps, me) => {
+ const clips = await Clips.find({
+ userId: me.id,
+ });
+
+ return await Promise.all(clips.map(x => Clips.pack(x)));
+});
diff --git a/packages/backend/src/server/api/endpoints/clips/notes.ts b/packages/backend/src/server/api/endpoints/clips/notes.ts
new file mode 100644
index 0000000000..5a9fed52fa
--- /dev/null
+++ b/packages/backend/src/server/api/endpoints/clips/notes.ts
@@ -0,0 +1,93 @@
+import $ from 'cafy';
+import { ID } from '@/misc/cafy-id';
+import define from '../../define';
+import { ClipNotes, Clips, Notes } from '@/models/index';
+import { makePaginationQuery } from '../../common/make-pagination-query';
+import { generateVisibilityQuery } from '../../common/generate-visibility-query';
+import { generateMutedUserQuery } from '../../common/generate-muted-user-query';
+import { ApiError } from '../../error';
+import { generateBlockedUserQuery } from '../../common/generate-block-query';
+
+export const meta = {
+ tags: ['account', 'notes', 'clips'],
+
+ requireCredential: false as const,
+
+ kind: 'read:account',
+
+ params: {
+ clipId: {
+ validator: $.type(ID),
+ },
+
+ limit: {
+ validator: $.optional.num.range(1, 100),
+ default: 10
+ },
+
+ sinceId: {
+ validator: $.optional.type(ID),
+ },
+
+ untilId: {
+ validator: $.optional.type(ID),
+ },
+ },
+
+ errors: {
+ noSuchClip: {
+ message: 'No such clip.',
+ code: 'NO_SUCH_CLIP',
+ id: '1d7645e6-2b6d-4635-b0fe-fe22b0e72e00'
+ }
+ },
+
+ 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: 'Note'
+ }
+ }
+};
+
+export default define(meta, async (ps, user) => {
+ const clip = await Clips.findOne({
+ id: ps.clipId,
+ });
+
+ if (clip == null) {
+ throw new ApiError(meta.errors.noSuchClip);
+ }
+
+ if (!clip.isPublic && (user == null || (clip.userId !== user.id))) {
+ throw new ApiError(meta.errors.noSuchClip);
+ }
+
+ const clipQuery = ClipNotes.createQueryBuilder('joining')
+ .select('joining.noteId')
+ .where('joining.clipId = :clipId', { clipId: clip.id });
+
+ const query = makePaginationQuery(Notes.createQueryBuilder('note'), ps.sinceId, ps.untilId)
+ .andWhere(`note.id IN (${ clipQuery.getQuery() })`)
+ .innerJoinAndSelect('note.user', 'user')
+ .leftJoinAndSelect('note.reply', 'reply')
+ .leftJoinAndSelect('note.renote', 'renote')
+ .leftJoinAndSelect('reply.user', 'replyUser')
+ .leftJoinAndSelect('renote.user', 'renoteUser')
+ .setParameters(clipQuery.getParameters());
+
+ if (user) {
+ generateVisibilityQuery(query, user);
+ generateMutedUserQuery(query, user);
+ generateBlockedUserQuery(query, user);
+ }
+
+ const notes = await query
+ .take(ps.limit!)
+ .getMany();
+
+ return await Notes.packMany(notes, user);
+});
diff --git a/packages/backend/src/server/api/endpoints/clips/show.ts b/packages/backend/src/server/api/endpoints/clips/show.ts
new file mode 100644
index 0000000000..8f245cd18e
--- /dev/null
+++ b/packages/backend/src/server/api/endpoints/clips/show.ts
@@ -0,0 +1,50 @@
+import $ from 'cafy';
+import { ID } from '@/misc/cafy-id';
+import define from '../../define';
+import { ApiError } from '../../error';
+import { Clips } from '@/models/index';
+
+export const meta = {
+ tags: ['clips', 'account'],
+
+ requireCredential: false as const,
+
+ kind: 'read:account',
+
+ params: {
+ clipId: {
+ validator: $.type(ID),
+ },
+ },
+
+ errors: {
+ noSuchClip: {
+ message: 'No such clip.',
+ code: 'NO_SUCH_CLIP',
+ id: 'c3c5fe33-d62c-44d2-9ea5-d997703f5c20'
+ },
+ },
+
+ res: {
+ type: 'object' as const,
+ optional: false as const, nullable: false as const,
+ ref: 'Clip'
+ }
+};
+
+export default define(meta, async (ps, me) => {
+ // Fetch the clip
+ const clip = await Clips.findOne({
+ id: ps.clipId,
+ });
+
+ if (clip == null) {
+ throw new ApiError(meta.errors.noSuchClip);
+ }
+
+ if (!clip.isPublic && (me == null || (clip.userId !== me.id))) {
+ throw new ApiError(meta.errors.noSuchClip);
+ }
+
+ return await Clips.pack(clip);
+});
diff --git a/packages/backend/src/server/api/endpoints/clips/update.ts b/packages/backend/src/server/api/endpoints/clips/update.ts
new file mode 100644
index 0000000000..7f645560bb
--- /dev/null
+++ b/packages/backend/src/server/api/endpoints/clips/update.ts
@@ -0,0 +1,65 @@
+import $ from 'cafy';
+import { ID } from '@/misc/cafy-id';
+import define from '../../define';
+import { ApiError } from '../../error';
+import { Clips } from '@/models/index';
+
+export const meta = {
+ tags: ['clips'],
+
+ requireCredential: true as const,
+
+ kind: 'write:account',
+
+ params: {
+ clipId: {
+ validator: $.type(ID),
+ },
+
+ name: {
+ validator: $.str.range(1, 100),
+ },
+
+ isPublic: {
+ validator: $.optional.bool
+ },
+
+ description: {
+ validator: $.optional.nullable.str.range(1, 2048)
+ }
+ },
+
+ errors: {
+ noSuchClip: {
+ message: 'No such clip.',
+ code: 'NO_SUCH_CLIP',
+ id: 'b4d92d70-b216-46fa-9a3f-a8c811699257'
+ },
+ },
+
+ res: {
+ type: 'object' as const,
+ optional: false as const, nullable: false as const,
+ ref: 'Clip'
+ }
+};
+
+export default define(meta, async (ps, user) => {
+ // Fetch the clip
+ const clip = await Clips.findOne({
+ id: ps.clipId,
+ userId: user.id
+ });
+
+ if (clip == null) {
+ throw new ApiError(meta.errors.noSuchClip);
+ }
+
+ await Clips.update(clip.id, {
+ name: ps.name,
+ description: ps.description,
+ isPublic: ps.isPublic,
+ });
+
+ return await Clips.pack(clip.id);
+});