summaryrefslogtreecommitdiff
path: root/src/api/serializers
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/serializers')
-rw-r--r--src/api/serializers/channel.ts66
-rw-r--r--src/api/serializers/post.ts12
2 files changed, 75 insertions, 3 deletions
diff --git a/src/api/serializers/channel.ts b/src/api/serializers/channel.ts
new file mode 100644
index 0000000000..3cba39aa16
--- /dev/null
+++ b/src/api/serializers/channel.ts
@@ -0,0 +1,66 @@
+/**
+ * Module dependencies
+ */
+import * as mongo from 'mongodb';
+import deepcopy = require('deepcopy');
+import { IUser } from '../models/user';
+import { default as Channel, IChannel } from '../models/channel';
+import Watching from '../models/channel-watching';
+
+/**
+ * Serialize a channel
+ *
+ * @param channel target
+ * @param me? serializee
+ * @return response
+ */
+export default (
+ channel: string | mongo.ObjectID | IChannel,
+ me?: string | mongo.ObjectID | IUser
+) => new Promise<any>(async (resolve, reject) => {
+
+ let _channel: any;
+
+ // Populate the channel if 'channel' is ID
+ if (mongo.ObjectID.prototype.isPrototypeOf(channel)) {
+ _channel = await Channel.findOne({
+ _id: channel
+ });
+ } else if (typeof channel === 'string') {
+ _channel = await Channel.findOne({
+ _id: new mongo.ObjectID(channel)
+ });
+ } else {
+ _channel = deepcopy(channel);
+ }
+
+ // Rename _id to id
+ _channel.id = _channel._id;
+ delete _channel._id;
+
+ // Remove needless properties
+ delete _channel.user_id;
+
+ // Me
+ const meId: mongo.ObjectID = me
+ ? mongo.ObjectID.prototype.isPrototypeOf(me)
+ ? me as mongo.ObjectID
+ : typeof me === 'string'
+ ? new mongo.ObjectID(me)
+ : (me as IUser)._id
+ : null;
+
+ if (me) {
+ //#region Watchしているかどうか
+ const watch = await Watching.findOne({
+ user_id: meId,
+ channel_id: _channel.id,
+ deleted_at: { $exists: false }
+ });
+
+ _channel.is_watching = watch !== null;
+ //#endregion
+ }
+
+ resolve(_channel);
+});
diff --git a/src/api/serializers/post.ts b/src/api/serializers/post.ts
index df917a8595..7c3690ef79 100644
--- a/src/api/serializers/post.ts
+++ b/src/api/serializers/post.ts
@@ -8,6 +8,7 @@ import Reaction from '../models/post-reaction';
import { IUser } from '../models/user';
import Vote from '../models/poll-vote';
import serializeApp from './app';
+import serializeChannel from './channel';
import serializeUser from './user';
import serializeDriveFile from './drive-file';
import parse from '../common/text';
@@ -76,8 +77,13 @@ const self = (
_post.app = await serializeApp(_post.app_id);
}
+ // Populate channel
+ if (_post.channel_id) {
+ _post.channel = await serializeChannel(_post.channel_id);
+ }
+
+ // Populate media
if (_post.media_ids) {
- // Populate media
_post.media = await Promise.all(_post.media_ids.map(async fileId =>
await serializeDriveFile(fileId)
));
@@ -117,9 +123,9 @@ const self = (
});
_post.next = next ? next._id : null;
- if (_post.reply_to_id) {
+ if (_post.reply_id) {
// Populate reply to post
- _post.reply_to = await self(_post.reply_to_id, meId, {
+ _post.reply = await self(_post.reply_id, meId, {
detail: false
});
}