summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/channels
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/api/endpoints/channels')
-rw-r--r--src/server/api/endpoints/channels/create.ts39
-rw-r--r--src/server/api/endpoints/channels/posts.ts78
-rw-r--r--src/server/api/endpoints/channels/show.ts30
-rw-r--r--src/server/api/endpoints/channels/unwatch.ts60
-rw-r--r--src/server/api/endpoints/channels/watch.ts58
5 files changed, 265 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..695b4515b3
--- /dev/null
+++ b/src/server/api/endpoints/channels/create.ts
@@ -0,0 +1,39 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Channel from '../../models/channel';
+import Watching from '../../models/channel-watching';
+import { pack } from '../../models/channel';
+
+/**
+ * Create a channel
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = async (params, user) => new Promise(async (res, rej) => {
+ // Get 'title' parameter
+ const [title, titleErr] = $(params.title).string().range(1, 100).$;
+ if (titleErr) return rej('invalid title param');
+
+ // Create a channel
+ const channel = await Channel.insert({
+ created_at: new Date(),
+ user_id: user._id,
+ title: title,
+ index: 0,
+ watching_count: 1
+ });
+
+ // Response
+ res(await pack(channel));
+
+ // Create Watching
+ await Watching.insert({
+ created_at: new Date(),
+ user_id: user._id,
+ channel_id: channel._id
+ });
+});
diff --git a/src/server/api/endpoints/channels/posts.ts b/src/server/api/endpoints/channels/posts.ts
new file mode 100644
index 0000000000..d722589c20
--- /dev/null
+++ b/src/server/api/endpoints/channels/posts.ts
@@ -0,0 +1,78 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import { default as Channel, IChannel } from '../../models/channel';
+import Post, { pack } from '../../models/post';
+
+/**
+ * Show a posts of a channel
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'limit' parameter
+ const [limit = 1000, limitErr] = $(params.limit).optional.number().range(1, 1000).$;
+ if (limitErr) return rej('invalid limit param');
+
+ // Get 'since_id' parameter
+ const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
+ if (sinceIdErr) return rej('invalid since_id param');
+
+ // Get 'until_id' parameter
+ const [untilId, untilIdErr] = $(params.until_id).optional.id().$;
+ if (untilIdErr) return rej('invalid until_id param');
+
+ // Check if both of since_id and until_id is specified
+ if (sinceId && untilId) {
+ return rej('cannot set since_id and until_id');
+ }
+
+ // Get 'channel_id' parameter
+ const [channelId, channelIdErr] = $(params.channel_id).id().$;
+ if (channelIdErr) return rej('invalid channel_id param');
+
+ // Fetch channel
+ const channel: IChannel = await Channel.findOne({
+ _id: channelId
+ });
+
+ if (channel === null) {
+ return rej('channel not found');
+ }
+
+ //#region Construct query
+ const sort = {
+ _id: -1
+ };
+
+ const query = {
+ channel_id: channel._id
+ } as any;
+
+ if (sinceId) {
+ sort._id = 1;
+ query._id = {
+ $gt: sinceId
+ };
+ } else if (untilId) {
+ query._id = {
+ $lt: untilId
+ };
+ }
+ //#endregion Construct query
+
+ // Issue query
+ const posts = await Post
+ .find(query, {
+ limit: limit,
+ sort: sort
+ });
+
+ // Serialize
+ res(await Promise.all(posts.map(async (post) =>
+ await pack(post, user)
+ )));
+});
diff --git a/src/server/api/endpoints/channels/show.ts b/src/server/api/endpoints/channels/show.ts
new file mode 100644
index 0000000000..332da64675
--- /dev/null
+++ b/src/server/api/endpoints/channels/show.ts
@@ -0,0 +1,30 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Channel, { IChannel, pack } from '../../models/channel';
+
+/**
+ * Show a channel
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'channel_id' parameter
+ const [channelId, channelIdErr] = $(params.channel_id).id().$;
+ if (channelIdErr) return rej('invalid channel_id param');
+
+ // Fetch channel
+ const channel: IChannel = await Channel.findOne({
+ _id: channelId
+ });
+
+ if (channel === null) {
+ return rej('channel not found');
+ }
+
+ // Serialize
+ res(await pack(channel, user));
+});
diff --git a/src/server/api/endpoints/channels/unwatch.ts b/src/server/api/endpoints/channels/unwatch.ts
new file mode 100644
index 0000000000..19d3be118a
--- /dev/null
+++ b/src/server/api/endpoints/channels/unwatch.ts
@@ -0,0 +1,60 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Channel from '../../models/channel';
+import Watching from '../../models/channel-watching';
+
+/**
+ * Unwatch a channel
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'channel_id' parameter
+ const [channelId, channelIdErr] = $(params.channel_id).id().$;
+ if (channelIdErr) return rej('invalid channel_id param');
+
+ //#region Fetch channel
+ const channel = await Channel.findOne({
+ _id: channelId
+ });
+
+ if (channel === null) {
+ return rej('channel not found');
+ }
+ //#endregion
+
+ //#region Check whether not watching
+ const exist = await Watching.findOne({
+ user_id: user._id,
+ channel_id: channel._id,
+ deleted_at: { $exists: false }
+ });
+
+ if (exist === null) {
+ return rej('already not watching');
+ }
+ //#endregion
+
+ // Delete watching
+ await Watching.update({
+ _id: exist._id
+ }, {
+ $set: {
+ deleted_at: new Date()
+ }
+ });
+
+ // Send response
+ res();
+
+ // Decrement watching count
+ Channel.update(channel._id, {
+ $inc: {
+ watching_count: -1
+ }
+ });
+});
diff --git a/src/server/api/endpoints/channels/watch.ts b/src/server/api/endpoints/channels/watch.ts
new file mode 100644
index 0000000000..030e0dd411
--- /dev/null
+++ b/src/server/api/endpoints/channels/watch.ts
@@ -0,0 +1,58 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Channel from '../../models/channel';
+import Watching from '../../models/channel-watching';
+
+/**
+ * Watch a channel
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'channel_id' parameter
+ const [channelId, channelIdErr] = $(params.channel_id).id().$;
+ if (channelIdErr) return rej('invalid channel_id param');
+
+ //#region Fetch channel
+ const channel = await Channel.findOne({
+ _id: channelId
+ });
+
+ if (channel === null) {
+ return rej('channel not found');
+ }
+ //#endregion
+
+ //#region Check whether already watching
+ const exist = await Watching.findOne({
+ user_id: user._id,
+ channel_id: channel._id,
+ deleted_at: { $exists: false }
+ });
+
+ if (exist !== null) {
+ return rej('already watching');
+ }
+ //#endregion
+
+ // Create Watching
+ await Watching.insert({
+ created_at: new Date(),
+ user_id: user._id,
+ channel_id: channel._id
+ });
+
+ // Send response
+ res();
+
+ // Increment watching count
+ Channel.update(channel._id, {
+ $inc: {
+ watching_count: 1
+ }
+ });
+});