summaryrefslogtreecommitdiff
path: root/src/api/models/channel.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2018-02-04 15:02:14 +0900
committerGitHub <noreply@github.com>2018-02-04 15:02:14 +0900
commit744c3e2ef8ef89355f72262b1e8c8f2fbb020f2a (patch)
treede3630065fcddeb1916668ef3b0b43a219340e2e /src/api/models/channel.ts
parentUpdate dependencies :rocket: (diff)
parentwip (diff)
downloadmisskey-744c3e2ef8ef89355f72262b1e8c8f2fbb020f2a.tar.gz
misskey-744c3e2ef8ef89355f72262b1e8c8f2fbb020f2a.tar.bz2
misskey-744c3e2ef8ef89355f72262b1e8c8f2fbb020f2a.zip
Merge pull request #1097 from syuilo/refactor
Refactor
Diffstat (limited to 'src/api/models/channel.ts')
-rw-r--r--src/api/models/channel.ts66
1 files changed, 63 insertions, 3 deletions
diff --git a/src/api/models/channel.ts b/src/api/models/channel.ts
index c80e84dbc8..815d53593c 100644
--- a/src/api/models/channel.ts
+++ b/src/api/models/channel.ts
@@ -1,9 +1,11 @@
import * as mongo from 'mongodb';
+import deepcopy = require('deepcopy');
+import { IUser } from './user';
+import Watching from './channel-watching';
import db from '../../db/mongodb';
-const collection = db.get('channels');
-
-export default collection as any; // fuck type definition
+const Channel = db.get<IChannel>('channels');
+export default Channel;
export type IChannel = {
_id: mongo.ObjectID;
@@ -12,3 +14,61 @@ export type IChannel = {
user_id: mongo.ObjectID;
index: number;
};
+
+/**
+ * Pack a channel for API response
+ *
+ * @param channel target
+ * @param me? serializee
+ * @return response
+ */
+export const pack = (
+ 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);
+});