summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/clips
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2020-01-30 04:37:25 +0900
committerGitHub <noreply@github.com>2020-01-30 04:37:25 +0900
commitf6154dc0af1a0d65819e87240f4385f9573095cb (patch)
tree699a5ca07d6727b7f8497d4769f25d6d62f94b5a /src/server/api/endpoints/clips
parentAdd Event activity-type support (#5785) (diff)
downloadsharkey-f6154dc0af1a0d65819e87240f4385f9573095cb.tar.gz
sharkey-f6154dc0af1a0d65819e87240f4385f9573095cb.tar.bz2
sharkey-f6154dc0af1a0d65819e87240f4385f9573095cb.zip
v12 (#5712)
Co-authored-by: MeiMei <30769358+mei23@users.noreply.github.com> Co-authored-by: Satsuki Yanagi <17376330+u1-liquid@users.noreply.github.com>
Diffstat (limited to 'src/server/api/endpoints/clips')
-rw-r--r--src/server/api/endpoints/clips/create.ts29
-rw-r--r--src/server/api/endpoints/clips/delete.ts40
-rw-r--r--src/server/api/endpoints/clips/list.ts18
-rw-r--r--src/server/api/endpoints/clips/notes.ts67
-rw-r--r--src/server/api/endpoints/clips/show.ts41
-rw-r--r--src/server/api/endpoints/clips/update.ts49
6 files changed, 244 insertions, 0 deletions
diff --git a/src/server/api/endpoints/clips/create.ts b/src/server/api/endpoints/clips/create.ts
new file mode 100644
index 0000000000..a6761c5533
--- /dev/null
+++ b/src/server/api/endpoints/clips/create.ts
@@ -0,0 +1,29 @@
+import $ from 'cafy';
+import define from '../../define';
+import { genId } from '../../../../misc/gen-id';
+import { Clips } from '../../../../models';
+
+export const meta = {
+ tags: ['clips'],
+
+ requireCredential: true,
+
+ kind: 'write:account',
+
+ params: {
+ name: {
+ validator: $.str.range(1, 100)
+ }
+ },
+};
+
+export default define(meta, async (ps, user) => {
+ const clip = await Clips.save({
+ id: genId(),
+ createdAt: new Date(),
+ userId: user.id,
+ name: ps.name,
+ });
+
+ return await Clips.pack(clip);
+});
diff --git a/src/server/api/endpoints/clips/delete.ts b/src/server/api/endpoints/clips/delete.ts
new file mode 100644
index 0000000000..7e185e4652
--- /dev/null
+++ b/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';
+
+export const meta = {
+ tags: ['clips'],
+
+ requireCredential: true,
+
+ 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/src/server/api/endpoints/clips/list.ts b/src/server/api/endpoints/clips/list.ts
new file mode 100644
index 0000000000..aa16a18d42
--- /dev/null
+++ b/src/server/api/endpoints/clips/list.ts
@@ -0,0 +1,18 @@
+import define from '../../define';
+import { Clips } from '../../../../models';
+
+export const meta = {
+ tags: ['clips', 'account'],
+
+ requireCredential: true,
+
+ kind: 'read:account',
+};
+
+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/src/server/api/endpoints/clips/notes.ts b/src/server/api/endpoints/clips/notes.ts
new file mode 100644
index 0000000000..4e76a4d1f3
--- /dev/null
+++ b/src/server/api/endpoints/clips/notes.ts
@@ -0,0 +1,67 @@
+import $ from 'cafy';
+import { ID } from '../../../../misc/cafy-id';
+import define from '../../define';
+import { Clips, Notes } from '../../../../models';
+import { makePaginationQuery } from '../../common/make-pagination-query';
+import { generateVisibilityQuery } from '../../common/generate-visibility-query';
+import { generateMuteQuery } from '../../common/generate-mute-query';
+
+export const meta = {
+ tags: ['account', 'notes', 'clips'],
+
+ requireCredential: true,
+
+ kind: 'read:account',
+
+ params: {
+ limit: {
+ validator: $.optional.num.range(1, 100),
+ default: 10
+ },
+
+ sinceId: {
+ validator: $.optional.type(ID),
+ },
+
+ untilId: {
+ validator: $.optional.type(ID),
+ },
+ },
+
+ errors: {
+ noSuchClip: {
+ message: 'No such list.',
+ code: 'NO_SUCH_CLIP',
+ id: '1d7645e6-2b6d-4635-b0fe-fe22b0e72e00'
+ }
+ }
+};
+
+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 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() })`)
+ .leftJoinAndSelect('note.user', 'user')
+ .setParameters(clipQuery.getParameters());
+
+ generateVisibilityQuery(query, user);
+ generateMuteQuery(query, user);
+
+ const notes = await query
+ .take(ps.limit!)
+ .getMany();
+
+ return await Notes.packMany(notes, user);
+});
diff --git a/src/server/api/endpoints/clips/show.ts b/src/server/api/endpoints/clips/show.ts
new file mode 100644
index 0000000000..0766b3e929
--- /dev/null
+++ b/src/server/api/endpoints/clips/show.ts
@@ -0,0 +1,41 @@
+import $ from 'cafy';
+import { ID } from '../../../../misc/cafy-id';
+import define from '../../define';
+import { ApiError } from '../../error';
+import { Clips } from '../../../../models';
+
+export const meta = {
+ tags: ['clips', 'account'],
+
+ requireCredential: true,
+
+ kind: 'read:account',
+
+ params: {
+ clipId: {
+ validator: $.type(ID),
+ },
+ },
+
+ errors: {
+ noSuchClip: {
+ message: 'No such clip.',
+ code: 'NO_SUCH_CLIP',
+ id: 'c3c5fe33-d62c-44d2-9ea5-d997703f5c20'
+ },
+ }
+};
+
+export default define(meta, async (ps, me) => {
+ // Fetch the clip
+ const clip = await Clips.findOne({
+ id: ps.clipId,
+ userId: me.id,
+ });
+
+ if (clip == null) {
+ throw new ApiError(meta.errors.noSuchClip);
+ }
+
+ return await Clips.pack(clip);
+});
diff --git a/src/server/api/endpoints/clips/update.ts b/src/server/api/endpoints/clips/update.ts
new file mode 100644
index 0000000000..d1c31eb8e6
--- /dev/null
+++ b/src/server/api/endpoints/clips/update.ts
@@ -0,0 +1,49 @@
+import $ from 'cafy';
+import { ID } from '../../../../misc/cafy-id';
+import define from '../../define';
+import { ApiError } from '../../error';
+import { Clips } from '../../../../models';
+
+export const meta = {
+ tags: ['clips'],
+
+ requireCredential: true,
+
+ kind: 'write:account',
+
+ params: {
+ clipId: {
+ validator: $.type(ID),
+ },
+
+ name: {
+ validator: $.str.range(1, 100),
+ }
+ },
+
+ errors: {
+ noSuchClip: {
+ message: 'No such clip.',
+ code: 'NO_SUCH_CLIP',
+ id: 'b4d92d70-b216-46fa-9a3f-a8c811699257'
+ },
+ }
+};
+
+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
+ });
+
+ return await Clips.pack(clip.id);
+});