summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/channels
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2020-08-18 22:44:21 +0900
committerGitHub <noreply@github.com>2020-08-18 22:44:21 +0900
commit9855405b8989713b81709fc1700e2ead97423467 (patch)
tree54254d2159378d1903e962f0fb37c799bb0f4464 /src/server/api/endpoints/channels
parentSign (request-target) Fix #6652 (#6656) (diff)
downloadmisskey-9855405b8989713b81709fc1700e2ead97423467.tar.gz
misskey-9855405b8989713b81709fc1700e2ead97423467.tar.bz2
misskey-9855405b8989713b81709fc1700e2ead97423467.zip
Channel (#6621)
* wip * wip * wip * wip * wip * wip * wip * wip * wop * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * add notes * wip * wip * wip * wip * sound * wip * add kick_gaba2 * wip
Diffstat (limited to 'src/server/api/endpoints/channels')
-rw-r--r--src/server/api/endpoints/channels/create.ts68
-rw-r--r--src/server/api/endpoints/channels/featured.ts28
-rw-r--r--src/server/api/endpoints/channels/follow.ts45
-rw-r--r--src/server/api/endpoints/channels/followed.ts28
-rw-r--r--src/server/api/endpoints/channels/owned.ts28
-rw-r--r--src/server/api/endpoints/channels/pin-note.ts0
-rw-r--r--src/server/api/endpoints/channels/show.ts43
-rw-r--r--src/server/api/endpoints/channels/timeline.ts99
-rw-r--r--src/server/api/endpoints/channels/unfollow.ts42
-rw-r--r--src/server/api/endpoints/channels/update.ts93
10 files changed, 474 insertions, 0 deletions
diff --git a/src/server/api/endpoints/channels/create.ts b/src/server/api/endpoints/channels/create.ts
new file mode 100644
index 0000000000..53436e703d
--- /dev/null
+++ b/src/server/api/endpoints/channels/create.ts
@@ -0,0 +1,68 @@
+import $ from 'cafy';
+import define from '../../define';
+import { ApiError } from '../../error';
+import { Channels, DriveFiles } from '../../../../models';
+import { Channel } from '../../../../models/entities/channel';
+import { genId } from '../../../../misc/gen-id';
+import { ID } from '../../../../misc/cafy-id';
+
+export const meta = {
+ tags: ['channels'],
+
+ requireCredential: true as const,
+
+ kind: 'write:channels',
+
+ params: {
+ name: {
+ validator: $.str.range(1, 128)
+ },
+
+ description: {
+ validator: $.nullable.optional.str.range(1, 2048)
+ },
+
+ bannerId: {
+ validator: $.nullable.optional.type(ID),
+ }
+ },
+
+ res: {
+ type: 'object' as const,
+ optional: false as const, nullable: false as const,
+ ref: 'Channel',
+ },
+
+ errors: {
+ noSuchFile: {
+ message: 'No such file.',
+ code: 'NO_SUCH_FILE',
+ id: 'cd1e9f3e-5a12-4ab4-96f6-5d0a2cc32050'
+ },
+ }
+};
+
+export default define(meta, async (ps, user) => {
+ let banner = null;
+ if (ps.bannerId != null) {
+ banner = await DriveFiles.findOne({
+ id: ps.bannerId,
+ userId: user.id
+ });
+
+ if (banner == null) {
+ throw new ApiError(meta.errors.noSuchFile);
+ }
+ }
+
+ const channel = await Channels.save({
+ id: genId(),
+ createdAt: new Date(),
+ userId: user.id,
+ name: ps.name,
+ description: ps.description || null,
+ bannerId: banner ? banner.id : null,
+ } as Channel);
+
+ return await Channels.pack(channel, user);
+});
diff --git a/src/server/api/endpoints/channels/featured.ts b/src/server/api/endpoints/channels/featured.ts
new file mode 100644
index 0000000000..abb0a19e2d
--- /dev/null
+++ b/src/server/api/endpoints/channels/featured.ts
@@ -0,0 +1,28 @@
+import define from '../../define';
+import { Channels } from '../../../../models';
+
+export const meta = {
+ tags: ['channels'],
+
+ 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: 'Channel',
+ }
+ },
+};
+
+export default define(meta, async (ps, me) => {
+ const query = Channels.createQueryBuilder('channel')
+ .where('channel.lastNotedAt IS NOT NULL')
+ .orderBy('channel.lastNotedAt', 'DESC');
+
+ const channels = await query.take(10).getMany();
+
+ return await Promise.all(channels.map(x => Channels.pack(x, me)));
+});
diff --git a/src/server/api/endpoints/channels/follow.ts b/src/server/api/endpoints/channels/follow.ts
new file mode 100644
index 0000000000..bf2f2bbb57
--- /dev/null
+++ b/src/server/api/endpoints/channels/follow.ts
@@ -0,0 +1,45 @@
+import $ from 'cafy';
+import { ID } from '../../../../misc/cafy-id';
+import define from '../../define';
+import { ApiError } from '../../error';
+import { Channels, ChannelFollowings } from '../../../../models';
+import { genId } from '../../../../misc/gen-id';
+
+export const meta = {
+ tags: ['channels'],
+
+ requireCredential: true as const,
+
+ kind: 'write:channels',
+
+ params: {
+ channelId: {
+ validator: $.type(ID),
+ },
+ },
+
+ errors: {
+ noSuchChannel: {
+ message: 'No such channel.',
+ code: 'NO_SUCH_CHANNEL',
+ id: 'c0031718-d573-4e85-928e-10039f1fbb68'
+ },
+ }
+};
+
+export default define(meta, async (ps, user) => {
+ const channel = await Channels.findOne({
+ id: ps.channelId,
+ });
+
+ if (channel == null) {
+ throw new ApiError(meta.errors.noSuchChannel);
+ }
+
+ await ChannelFollowings.save({
+ id: genId(),
+ createdAt: new Date(),
+ followerId: user.id,
+ followeeId: channel.id,
+ });
+});
diff --git a/src/server/api/endpoints/channels/followed.ts b/src/server/api/endpoints/channels/followed.ts
new file mode 100644
index 0000000000..05c2ec6a75
--- /dev/null
+++ b/src/server/api/endpoints/channels/followed.ts
@@ -0,0 +1,28 @@
+import define from '../../define';
+import { Channels, ChannelFollowings } from '../../../../models';
+
+export const meta = {
+ tags: ['channels', 'account'],
+
+ requireCredential: true as const,
+
+ kind: 'read:channels',
+
+ 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: 'Channel',
+ }
+ },
+};
+
+export default define(meta, async (ps, me) => {
+ const followings = await ChannelFollowings.find({
+ followerId: me.id,
+ });
+
+ return await Promise.all(followings.map(x => Channels.pack(x.followeeId, me)));
+});
diff --git a/src/server/api/endpoints/channels/owned.ts b/src/server/api/endpoints/channels/owned.ts
new file mode 100644
index 0000000000..9e563c0ac5
--- /dev/null
+++ b/src/server/api/endpoints/channels/owned.ts
@@ -0,0 +1,28 @@
+import define from '../../define';
+import { Channels } from '../../../../models';
+
+export const meta = {
+ tags: ['channels', 'account'],
+
+ requireCredential: true as const,
+
+ kind: 'read:channels',
+
+ 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: 'Channel',
+ }
+ },
+};
+
+export default define(meta, async (ps, me) => {
+ const channels = await Channels.find({
+ userId: me.id,
+ });
+
+ return await Promise.all(channels.map(x => Channels.pack(x, me)));
+});
diff --git a/src/server/api/endpoints/channels/pin-note.ts b/src/server/api/endpoints/channels/pin-note.ts
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/src/server/api/endpoints/channels/pin-note.ts
diff --git a/src/server/api/endpoints/channels/show.ts b/src/server/api/endpoints/channels/show.ts
new file mode 100644
index 0000000000..63057dd57f
--- /dev/null
+++ b/src/server/api/endpoints/channels/show.ts
@@ -0,0 +1,43 @@
+import $ from 'cafy';
+import { ID } from '../../../../misc/cafy-id';
+import define from '../../define';
+import { ApiError } from '../../error';
+import { Channels } from '../../../../models';
+
+export const meta = {
+ tags: ['channels'],
+
+ requireCredential: false as const,
+
+ params: {
+ channelId: {
+ validator: $.type(ID),
+ },
+ },
+
+ res: {
+ type: 'object' as const,
+ optional: false as const, nullable: false as const,
+ ref: 'Channel',
+ },
+
+ errors: {
+ noSuchChannel: {
+ message: 'No such channel.',
+ code: 'NO_SUCH_CHANNEL',
+ id: '6f6c314b-7486-4897-8966-c04a66a02923'
+ },
+ }
+};
+
+export default define(meta, async (ps, me) => {
+ const channel = await Channels.findOne({
+ id: ps.channelId,
+ });
+
+ if (channel == null) {
+ throw new ApiError(meta.errors.noSuchChannel);
+ }
+
+ return await Channels.pack(channel, me);
+});
diff --git a/src/server/api/endpoints/channels/timeline.ts b/src/server/api/endpoints/channels/timeline.ts
new file mode 100644
index 0000000000..3ae28fc67a
--- /dev/null
+++ b/src/server/api/endpoints/channels/timeline.ts
@@ -0,0 +1,99 @@
+import $ from 'cafy';
+import { ID } from '../../../../misc/cafy-id';
+import define from '../../define';
+import { ApiError } from '../../error';
+import { Notes, Channels } from '../../../../models';
+import { makePaginationQuery } from '../../common/make-pagination-query';
+import { activeUsersChart } from '../../../../services/chart';
+
+export const meta = {
+ tags: ['notes', 'channels'],
+
+ requireCredential: false as const,
+
+ params: {
+ channelId: {
+ validator: $.type(ID),
+ desc: {
+ 'ja-JP': 'チャンネルのID'
+ }
+ },
+
+ limit: {
+ validator: $.optional.num.range(1, 100),
+ default: 10,
+ desc: {
+ 'ja-JP': '最大数'
+ }
+ },
+
+ sinceId: {
+ validator: $.optional.type(ID),
+ desc: {
+ 'ja-JP': '指定すると、その投稿を基点としてより新しい投稿を取得します'
+ }
+ },
+
+ untilId: {
+ validator: $.optional.type(ID),
+ desc: {
+ 'ja-JP': '指定すると、その投稿を基点としてより古い投稿を取得します'
+ }
+ },
+
+ sinceDate: {
+ validator: $.optional.num,
+ desc: {
+ 'ja-JP': '指定した時間を基点としてより新しい投稿を取得します。数値は、1970年1月1日 00:00:00 UTC から指定した日時までの経過時間をミリ秒単位で表します。'
+ }
+ },
+
+ untilDate: {
+ validator: $.optional.num,
+ desc: {
+ 'ja-JP': '指定した時間を基点としてより古い投稿を取得します。数値は、1970年1月1日 00:00:00 UTC から指定した日時までの経過時間をミリ秒単位で表します。'
+ }
+ },
+ },
+
+ 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',
+ }
+ },
+
+ errors: {
+ noSuchChannel: {
+ message: 'No such channel.',
+ code: 'NO_SUCH_CHANNEL',
+ id: '4d0eeeba-a02c-4c3c-9966-ef60d38d2e7f'
+ }
+ }
+};
+
+export default define(meta, async (ps, user) => {
+ const channel = await Channels.findOne({
+ id: ps.channelId,
+ });
+
+ if (channel == null) {
+ throw new ApiError(meta.errors.noSuchChannel);
+ }
+
+ //#region Construct query
+ const query = makePaginationQuery(Notes.createQueryBuilder('note'), ps.sinceId, ps.untilId)
+ .andWhere('note.channelId = :channelId', { channelId: channel.id })
+ .leftJoinAndSelect('note.user', 'user')
+ .leftJoinAndSelect('note.channel', 'channel');
+ //#endregion
+
+ const timeline = await query.take(ps.limit!).getMany();
+
+ activeUsersChart.update(user);
+
+ return await Notes.packMany(timeline, user);
+});
diff --git a/src/server/api/endpoints/channels/unfollow.ts b/src/server/api/endpoints/channels/unfollow.ts
new file mode 100644
index 0000000000..8cab5c36a6
--- /dev/null
+++ b/src/server/api/endpoints/channels/unfollow.ts
@@ -0,0 +1,42 @@
+import $ from 'cafy';
+import { ID } from '../../../../misc/cafy-id';
+import define from '../../define';
+import { ApiError } from '../../error';
+import { Channels, ChannelFollowings } from '../../../../models';
+
+export const meta = {
+ tags: ['channels'],
+
+ requireCredential: true as const,
+
+ kind: 'write:channels',
+
+ params: {
+ channelId: {
+ validator: $.type(ID),
+ },
+ },
+
+ errors: {
+ noSuchChannel: {
+ message: 'No such channel.',
+ code: 'NO_SUCH_CHANNEL',
+ id: '19959ee9-0153-4c51-bbd9-a98c49dc59d6'
+ },
+ }
+};
+
+export default define(meta, async (ps, user) => {
+ const channel = await Channels.findOne({
+ id: ps.channelId,
+ });
+
+ if (channel == null) {
+ throw new ApiError(meta.errors.noSuchChannel);
+ }
+
+ await ChannelFollowings.delete({
+ followerId: user.id,
+ followeeId: channel.id,
+ });
+});
diff --git a/src/server/api/endpoints/channels/update.ts b/src/server/api/endpoints/channels/update.ts
new file mode 100644
index 0000000000..8b94646ad1
--- /dev/null
+++ b/src/server/api/endpoints/channels/update.ts
@@ -0,0 +1,93 @@
+import $ from 'cafy';
+import { ID } from '../../../../misc/cafy-id';
+import define from '../../define';
+import { ApiError } from '../../error';
+import { Channels, DriveFiles } from '../../../../models';
+
+export const meta = {
+ tags: ['channels'],
+
+ requireCredential: true as const,
+
+ kind: 'write:channels',
+
+ params: {
+ channelId: {
+ validator: $.type(ID),
+ },
+
+ name: {
+ validator: $.optional.str.range(1, 128)
+ },
+
+ description: {
+ validator: $.nullable.optional.str.range(1, 2048)
+ },
+
+ bannerId: {
+ validator: $.nullable.optional.type(ID),
+ }
+ },
+
+ res: {
+ type: 'object' as const,
+ optional: false as const, nullable: false as const,
+ ref: 'Channel',
+ },
+
+ errors: {
+ noSuchChannel: {
+ message: 'No such channel.',
+ code: 'NO_SUCH_CHANNEL',
+ id: 'f9c5467f-d492-4c3c-9a8d-a70dacc86512'
+ },
+
+ accessDenied: {
+ message: 'You do not have edit privilege of the channel.',
+ code: 'ACCESS_DENIED',
+ id: '1fb7cb09-d46a-4fdf-b8df-057788cce513'
+ },
+
+ noSuchFile: {
+ message: 'No such file.',
+ code: 'NO_SUCH_FILE',
+ id: 'e86c14a4-0da2-4032-8df3-e737a04c7f3b'
+ },
+ }
+};
+
+export default define(meta, async (ps, me) => {
+ const channel = await Channels.findOne({
+ id: ps.channelId,
+ });
+
+ if (channel == null) {
+ throw new ApiError(meta.errors.noSuchChannel);
+ }
+
+ if (channel.userId !== me.id) {
+ throw new ApiError(meta.errors.accessDenied);
+ }
+
+ let banner = undefined;
+ if (ps.bannerId != null) {
+ banner = await DriveFiles.findOne({
+ id: ps.bannerId,
+ userId: me.id
+ });
+
+ if (banner == null) {
+ throw new ApiError(meta.errors.noSuchFile);
+ }
+ } else if (ps.bannerId === null) {
+ banner = null;
+ }
+
+ await Channels.update(channel.id, {
+ ...(ps.name !== undefined ? { name: ps.name } : {}),
+ ...(ps.description !== undefined ? { description: ps.description } : {}),
+ ...(banner ? { bannerId: banner.id } : {}),
+ });
+
+ return await Channels.pack(channel.id, me);
+});