summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-07-04 13:21:30 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-07-04 13:21:30 +0900
commit67afe968b4385584e373b9e619617051159759ea (patch)
tree44993d1d638814818daf5b3db2b857f061423a4c /src
parentMerge pull request #1839 from syuilo/greenkeeper/elasticsearch-15.1.1 (diff)
downloadsharkey-67afe968b4385584e373b9e619617051159759ea.tar.gz
sharkey-67afe968b4385584e373b9e619617051159759ea.tar.bz2
sharkey-67afe968b4385584e373b9e619617051159759ea.zip
wip
Diffstat (limited to 'src')
-rw-r--r--src/config/types.ts1
-rw-r--r--src/index.ts4
-rw-r--r--src/server/api/endpoints/notes/search.ts63
-rw-r--r--src/services/note/create.ts17
4 files changed, 82 insertions, 3 deletions
diff --git a/src/config/types.ts b/src/config/types.ts
index 49eeac508b..b0776fd9c1 100644
--- a/src/config/types.ts
+++ b/src/config/types.ts
@@ -34,7 +34,6 @@ export type Source = {
pass: string;
};
elasticsearch: {
- enable: boolean;
host: string;
port: number;
pass: string;
diff --git a/src/index.ts b/src/index.ts
index c89252dfc2..1a76044572 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -19,7 +19,7 @@ import MachineInfo from './utils/machineInfo';
import DependencyInfo from './utils/dependencyInfo';
import serverStats from './daemons/server-stats';
import notesStats from './daemons/notes-stats';
-
+import db from './db/mongodb';
import loadConfig from './config/load';
import { Config } from './config/types';
@@ -204,4 +204,6 @@ process.on('uncaughtException', err => {
// Dying away...
process.on('exit', code => {
Logger.info(`The process is going exit (${code})`);
+
+ db.close();
});
diff --git a/src/server/api/endpoints/notes/search.ts b/src/server/api/endpoints/notes/search.ts
new file mode 100644
index 0000000000..20c628b84d
--- /dev/null
+++ b/src/server/api/endpoints/notes/search.ts
@@ -0,0 +1,63 @@
+import $ from 'cafy';
+import * as mongo from 'mongodb';
+import Note from '../../../../models/note';
+import { ILocalUser } from '../../../../models/user';
+import { pack } from '../../../../models/note';
+import es from '../../../../db/elasticsearch';
+
+module.exports = (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
+ // Get 'query' parameter
+ const [query, queryError] = $.str.get(params.query);
+ if (queryError) return rej('invalid query param');
+
+ // Get 'offset' parameter
+ const [offset = 0, offsetErr] = $.num.optional().min(0).get(params.offset);
+ if (offsetErr) return rej('invalid offset param');
+
+ // Get 'limit' parameter
+ const [limit = 10, limitErr] = $.num.optional().range(1, 30).get(params.limit);
+ if (limitErr) return rej('invalid limit param');
+
+ es.search({
+ index: 'misskey',
+ type: 'note',
+ body: {
+ size: limit,
+ from: offset,
+ query: {
+ simple_query_string: {
+ fields: ['text'],
+ query: query,
+ default_operator: 'and'
+ }
+ },
+ sort: [
+ { _doc: 'desc' }
+ ]
+ }
+ }, async (error, response) => {
+ if (error) {
+ console.error(error);
+ return res(500);
+ }
+
+ if (response.hits.total === 0) {
+ return res([]);
+ }
+
+ const hits = response.hits.hits.map(hit => new mongo.ObjectID(hit._id));
+
+ // Fetch found notes
+ const notes = await Note.find({
+ _id: {
+ $in: hits
+ }
+ }, {
+ sort: {
+ _id: -1
+ }
+ });
+
+ res(await Promise.all(notes.map(note => pack(note, me))));
+ });
+});
diff --git a/src/services/note/create.ts b/src/services/note/create.ts
index a793c8e580..ea20b063d5 100644
--- a/src/services/note/create.ts
+++ b/src/services/note/create.ts
@@ -18,6 +18,7 @@ import { IApp } from '../../models/app';
import UserList from '../../models/user-list';
import resolveUser from '../../remote/resolve-user';
import Meta from '../../models/meta';
+import config from '../../config';
type Type = 'reply' | 'renote' | 'quote' | 'mention';
@@ -366,7 +367,7 @@ export default async (user: IUser, data: {
watch(user._id, data.reply);
}
- // (自分自身へのリプライでない限りは)通知を作成
+ // 通知
nm.push(data.reply.userId, 'reply');
}
@@ -427,4 +428,18 @@ export default async (user: IUser, data: {
});
}
}
+
+ // Register to search database
+ if (note.text && config.elasticsearch) {
+ const es = require('../../../db/elasticsearch');
+
+ es.index({
+ index: 'misskey',
+ type: 'note',
+ id: note._id.toString(),
+ body: {
+ text: note.text
+ }
+ });
+ }
});