summaryrefslogtreecommitdiff
path: root/src/api/common
diff options
context:
space:
mode:
authorこぴなたみぽ <Syuilotan@yahoo.co.jp>2017-11-06 19:14:04 +0900
committerGitHub <noreply@github.com>2017-11-06 19:14:04 +0900
commit9b7edcefb991f9c463b330cc7f87d99f9a357196 (patch)
tree0f4451044edf5d265e84adfc13c4c3f013ffb007 /src/api/common
parentchore(package): update @types/body-parser to version 1.16.7 (diff)
parentMerge pull request #875 from syuilo/greenkeeper/@types/gulp-util-3.0.33 (diff)
downloadmisskey-9b7edcefb991f9c463b330cc7f87d99f9a357196.tar.gz
misskey-9b7edcefb991f9c463b330cc7f87d99f9a357196.tar.bz2
misskey-9b7edcefb991f9c463b330cc7f87d99f9a357196.zip
Merge branch 'master' into greenkeeper/@types/body-parser-1.16.7
Diffstat (limited to 'src/api/common')
-rw-r--r--src/api/common/add-file-to-drive.ts35
-rw-r--r--src/api/common/read-notification.ts52
2 files changed, 73 insertions, 14 deletions
diff --git a/src/api/common/add-file-to-drive.ts b/src/api/common/add-file-to-drive.ts
index 714eeb520d..f9c22ccacd 100644
--- a/src/api/common/add-file-to-drive.ts
+++ b/src/api/common/add-file-to-drive.ts
@@ -4,14 +4,27 @@ import * as gm from 'gm';
import * as debug from 'debug';
import fileType = require('file-type');
import prominence = require('prominence');
-import DriveFile from '../models/drive-file';
+import DriveFile, { getGridFSBucket } from '../models/drive-file';
import DriveFolder from '../models/drive-folder';
import serialize from '../serializers/drive-file';
import event from '../event';
import config from '../../conf';
+import { Duplex } from 'stream';
const log = debug('misskey:register-drive-file');
+const addToGridFS = (name, binary, metadata): Promise<any> => new Promise(async (resolve, reject) => {
+ const dataStream = new Duplex();
+ dataStream.push(binary);
+ dataStream.push(null);
+
+ const bucket = await getGridFSBucket();
+ const writeStream = bucket.openUploadStream(name, { metadata });
+ writeStream.once('finish', (doc) => { resolve(doc); });
+ writeStream.on('error', reject);
+ dataStream.pipe(writeStream);
+});
+
/**
* Add file to drive
*
@@ -58,7 +71,7 @@ export default (
// Generate hash
const hash = crypto
- .createHash('sha256')
+ .createHash('md5')
.update(data)
.digest('hex') as string;
@@ -67,8 +80,8 @@ export default (
if (!force) {
// Check if there is a file with the same hash
const much = await DriveFile.findOne({
- user_id: user._id,
- hash: hash
+ md5: hash,
+ 'metadata.user_id': user._id
});
if (much !== null) {
@@ -82,13 +95,13 @@ export default (
// Calculate drive usage
const usage = ((await DriveFile
.aggregate([
- { $match: { user_id: user._id } },
+ { $match: { 'metadata.user_id': user._id } },
{ $project: {
- datasize: true
+ length: true
}},
{ $group: {
_id: null,
- usage: { $sum: '$datasize' }
+ usage: { $sum: '$length' }
}}
]))[0] || {
usage: 0
@@ -131,21 +144,15 @@ export default (
}
// Create DriveFile document
- const file = await DriveFile.insert({
- created_at: new Date(),
+ const file = await addToGridFS(`${user._id}/${name}`, data, {
user_id: user._id,
folder_id: folder !== null ? folder._id : null,
- data: data,
- datasize: size,
type: mime,
name: name,
comment: comment,
- hash: hash,
properties: properties
});
- delete file.data;
-
log(`drive file has been created ${file._id}`);
resolve(file);
diff --git a/src/api/common/read-notification.ts b/src/api/common/read-notification.ts
new file mode 100644
index 0000000000..3009cc5d08
--- /dev/null
+++ b/src/api/common/read-notification.ts
@@ -0,0 +1,52 @@
+import * as mongo from 'mongodb';
+import { default as Notification, INotification } from '../models/notification';
+import publishUserStream from '../event';
+
+/**
+ * Mark as read notification(s)
+ */
+export default (
+ user: string | mongo.ObjectID,
+ message: string | string[] | INotification | INotification[] | mongo.ObjectID | mongo.ObjectID[]
+) => new Promise<any>(async (resolve, reject) => {
+
+ const userId = mongo.ObjectID.prototype.isPrototypeOf(user)
+ ? user
+ : new mongo.ObjectID(user);
+
+ const ids: mongo.ObjectID[] = Array.isArray(message)
+ ? mongo.ObjectID.prototype.isPrototypeOf(message[0])
+ ? (message as mongo.ObjectID[])
+ : typeof message[0] === 'string'
+ ? (message as string[]).map(m => new mongo.ObjectID(m))
+ : (message as INotification[]).map(m => m._id)
+ : mongo.ObjectID.prototype.isPrototypeOf(message)
+ ? [(message as mongo.ObjectID)]
+ : typeof message === 'string'
+ ? [new mongo.ObjectID(message)]
+ : [(message as INotification)._id];
+
+ // Update documents
+ await Notification.update({
+ _id: { $in: ids },
+ is_read: false
+ }, {
+ $set: {
+ is_read: true
+ }
+ }, {
+ multi: true
+ });
+
+ // Calc count of my unread notifications
+ const count = await Notification
+ .count({
+ notifiee_id: userId,
+ is_read: false
+ });
+
+ if (count == 0) {
+ // 全ての(いままで未読だった)通知を(これで)読みましたよというイベントを発行
+ publishUserStream(userId, 'read_all_notifications');
+ }
+});