2017-09-07 22:13:01 +03:00
|
|
|
import * as mongo from 'mongodb';
|
2018-02-02 01:06:01 +02:00
|
|
|
import deepcopy = require('deepcopy');
|
|
|
|
import rap from '@prezzemolo/rap';
|
2018-03-29 14:32:18 +03:00
|
|
|
import db from '../db/mongodb';
|
2018-02-02 01:06:01 +02:00
|
|
|
import { IUser, pack as packUser } from './user';
|
|
|
|
import { pack as packApp } from './app';
|
|
|
|
import { pack as packChannel } from './channel';
|
2018-04-15 00:34:55 +03:00
|
|
|
import PollVote, { deletePollVote } from './poll-vote';
|
2018-04-11 21:46:32 +03:00
|
|
|
import Reaction, { deleteNoteReaction } from './note-reaction';
|
2018-02-02 01:06:01 +02:00
|
|
|
import { pack as packFile } from './drive-file';
|
2018-04-11 21:46:32 +03:00
|
|
|
import NoteWatching, { deleteNoteWatching } from './note-watching';
|
|
|
|
import NoteReaction from './note-reaction';
|
|
|
|
import Favorite, { deleteFavorite } from './favorite';
|
2018-04-15 00:34:55 +03:00
|
|
|
import Notification, { deleteNotification } from './notification';
|
2018-04-28 22:30:51 +03:00
|
|
|
import Following from './following';
|
2018-02-02 01:06:01 +02:00
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
const Note = db.get<INote>('notes');
|
2017-01-17 02:12:33 +02:00
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
Note.createIndex('uri', { sparse: true, unique: true });
|
2018-04-03 17:45:13 +03:00
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
export default Note;
|
2017-03-01 20:16:39 +02:00
|
|
|
|
|
|
|
export function isValidText(text: string): boolean {
|
|
|
|
return text.length <= 1000 && text.trim() != '';
|
|
|
|
}
|
2017-09-07 22:13:01 +03:00
|
|
|
|
2018-03-30 05:24:07 +03:00
|
|
|
export function isValidCw(text: string): boolean {
|
2018-04-22 11:04:52 +03:00
|
|
|
return text.length <= 100;
|
2018-03-30 05:24:07 +03:00
|
|
|
}
|
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
export type INote = {
|
2017-09-07 22:13:01 +03:00
|
|
|
_id: mongo.ObjectID;
|
2018-03-29 08:48:47 +03:00
|
|
|
channelId: mongo.ObjectID;
|
|
|
|
createdAt: Date;
|
2018-04-07 01:19:30 +03:00
|
|
|
deletedAt: Date;
|
2018-03-29 08:48:47 +03:00
|
|
|
mediaIds: mongo.ObjectID[];
|
|
|
|
replyId: mongo.ObjectID;
|
2018-04-07 20:30:37 +03:00
|
|
|
renoteId: mongo.ObjectID;
|
2018-02-04 07:52:33 +02:00
|
|
|
poll: any; // todo
|
2017-09-07 22:13:01 +03:00
|
|
|
text: string;
|
2018-04-01 12:07:29 +03:00
|
|
|
tags: string[];
|
2018-03-30 05:24:07 +03:00
|
|
|
cw: string;
|
2018-03-29 08:48:47 +03:00
|
|
|
userId: mongo.ObjectID;
|
|
|
|
appId: mongo.ObjectID;
|
|
|
|
viaMobile: boolean;
|
2018-04-07 20:30:37 +03:00
|
|
|
renoteCount: number;
|
2018-03-29 08:48:47 +03:00
|
|
|
repliesCount: number;
|
|
|
|
reactionCounts: any;
|
|
|
|
mentions: mongo.ObjectID[];
|
2018-04-28 11:25:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* public ... 公開
|
|
|
|
* home ... ホームタイムライン(ユーザーページのタイムライン含む)のみに流す
|
|
|
|
* followers ... フォロワーのみ
|
2018-04-28 22:30:51 +03:00
|
|
|
* specified ... visibleUserIds で指定したユーザーのみ
|
2018-04-28 11:25:56 +03:00
|
|
|
* private ... 自分のみ
|
|
|
|
*/
|
2018-04-28 22:30:51 +03:00
|
|
|
visibility: 'public' | 'home' | 'followers' | 'specified' | 'private';
|
|
|
|
|
|
|
|
visibleUserIds: mongo.ObjectID[];
|
2018-04-28 11:25:56 +03:00
|
|
|
|
2018-03-05 01:44:37 +02:00
|
|
|
geo: {
|
2018-03-29 09:23:15 +03:00
|
|
|
coordinates: number[];
|
2018-03-05 01:44:37 +02:00
|
|
|
altitude: number;
|
|
|
|
accuracy: number;
|
|
|
|
altitudeAccuracy: number;
|
|
|
|
heading: number;
|
|
|
|
speed: number;
|
|
|
|
};
|
2018-04-03 17:45:13 +03:00
|
|
|
uri: string;
|
2018-04-05 22:04:50 +03:00
|
|
|
|
2018-04-19 06:43:25 +03:00
|
|
|
// 非正規化
|
2018-04-05 22:04:50 +03:00
|
|
|
_reply?: {
|
|
|
|
userId: mongo.ObjectID;
|
|
|
|
};
|
2018-04-07 20:30:37 +03:00
|
|
|
_renote?: {
|
2018-04-05 22:04:50 +03:00
|
|
|
userId: mongo.ObjectID;
|
|
|
|
};
|
|
|
|
_user: {
|
|
|
|
host: string;
|
2018-04-19 06:43:25 +03:00
|
|
|
inbox?: string;
|
2018-04-05 22:04:50 +03:00
|
|
|
};
|
2017-09-07 22:13:01 +03:00
|
|
|
};
|
2018-02-02 01:06:01 +02:00
|
|
|
|
2018-04-11 21:46:32 +03:00
|
|
|
/**
|
|
|
|
* Noteを物理削除します
|
|
|
|
*/
|
|
|
|
export async function deleteNote(note: string | mongo.ObjectID | INote) {
|
2018-04-11 12:24:42 +03:00
|
|
|
let n: INote;
|
|
|
|
|
|
|
|
// Populate
|
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(note)) {
|
|
|
|
n = await Note.findOne({
|
|
|
|
_id: note
|
|
|
|
});
|
|
|
|
} else if (typeof note === 'string') {
|
|
|
|
n = await Note.findOne({
|
|
|
|
_id: new mongo.ObjectID(note)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
n = note as INote;
|
|
|
|
}
|
|
|
|
|
2018-04-15 05:57:33 +03:00
|
|
|
console.log(n == null ? `Note: delete skipped ${note}` : `Note: deleting ${n._id}`);
|
|
|
|
|
2018-04-11 12:24:42 +03:00
|
|
|
if (n == null) return;
|
|
|
|
|
2018-04-11 21:46:32 +03:00
|
|
|
// このNoteへの返信をすべて削除
|
|
|
|
await Promise.all((
|
|
|
|
await Note.find({ replyId: n._id })
|
|
|
|
).map(x => deleteNote(x)));
|
2018-04-11 12:24:42 +03:00
|
|
|
|
2018-04-11 21:46:32 +03:00
|
|
|
// このNoteのRenoteをすべて削除
|
|
|
|
await Promise.all((
|
|
|
|
await Note.find({ renoteId: n._id })
|
|
|
|
).map(x => deleteNote(x)));
|
2018-04-11 12:24:42 +03:00
|
|
|
|
2018-04-11 21:46:32 +03:00
|
|
|
// この投稿に対するNoteWatchingをすべて削除
|
|
|
|
await Promise.all((
|
|
|
|
await NoteWatching.find({ noteId: n._id })
|
|
|
|
).map(x => deleteNoteWatching(x)));
|
|
|
|
|
|
|
|
// この投稿に対するNoteReactionをすべて削除
|
|
|
|
await Promise.all((
|
|
|
|
await NoteReaction.find({ noteId: n._id })
|
|
|
|
).map(x => deleteNoteReaction(x)));
|
2018-04-11 12:24:42 +03:00
|
|
|
|
2018-04-12 01:19:28 +03:00
|
|
|
// この投稿に対するPollVoteをすべて削除
|
|
|
|
await Promise.all((
|
|
|
|
await PollVote.find({ noteId: n._id })
|
|
|
|
).map(x => deletePollVote(x)));
|
|
|
|
|
2018-04-11 12:24:42 +03:00
|
|
|
// この投稿に対するFavoriteをすべて削除
|
2018-04-11 21:46:32 +03:00
|
|
|
await Promise.all((
|
|
|
|
await Favorite.find({ noteId: n._id })
|
|
|
|
).map(x => deleteFavorite(x)));
|
|
|
|
|
2018-04-15 00:34:55 +03:00
|
|
|
// この投稿に対するNotificationをすべて削除
|
|
|
|
await Promise.all((
|
|
|
|
await Notification.find({ noteId: n._id })
|
|
|
|
).map(x => deleteNotification(x)));
|
|
|
|
|
2018-04-11 21:46:32 +03:00
|
|
|
// このNoteを削除
|
|
|
|
await Note.remove({
|
|
|
|
_id: n._id
|
|
|
|
});
|
2018-04-15 05:57:33 +03:00
|
|
|
|
|
|
|
console.log(`Note: deleted ${n._id}`);
|
2018-04-11 12:24:42 +03:00
|
|
|
}
|
|
|
|
|
2018-02-02 01:06:01 +02:00
|
|
|
/**
|
2018-04-07 20:30:37 +03:00
|
|
|
* Pack a note for API response
|
2018-02-02 01:06:01 +02:00
|
|
|
*
|
2018-04-07 20:30:37 +03:00
|
|
|
* @param note target
|
2018-02-02 01:06:01 +02:00
|
|
|
* @param me? serializee
|
|
|
|
* @param options? serialize options
|
|
|
|
* @return response
|
|
|
|
*/
|
|
|
|
export const pack = async (
|
2018-04-07 20:30:37 +03:00
|
|
|
note: string | mongo.ObjectID | INote,
|
2018-02-02 01:06:01 +02:00
|
|
|
me?: string | mongo.ObjectID | IUser,
|
|
|
|
options?: {
|
|
|
|
detail: boolean
|
|
|
|
}
|
|
|
|
) => {
|
|
|
|
const opts = options || {
|
|
|
|
detail: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
let _note: any;
|
2018-02-02 01:06:01 +02:00
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
// Populate the note if 'note' is ID
|
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(note)) {
|
|
|
|
_note = await Note.findOne({
|
|
|
|
_id: note
|
2018-02-02 01:06:01 +02:00
|
|
|
});
|
2018-04-07 20:30:37 +03:00
|
|
|
} else if (typeof note === 'string') {
|
|
|
|
_note = await Note.findOne({
|
|
|
|
_id: new mongo.ObjectID(note)
|
2018-02-02 01:06:01 +02:00
|
|
|
});
|
|
|
|
} else {
|
2018-04-07 20:30:37 +03:00
|
|
|
_note = deepcopy(note);
|
2018-02-02 01:06:01 +02:00
|
|
|
}
|
|
|
|
|
2018-04-08 00:55:26 +03:00
|
|
|
if (!_note) throw `invalid note arg ${note}`;
|
2018-02-02 01:06:01 +02:00
|
|
|
|
2018-04-28 22:30:51 +03:00
|
|
|
let hide = false;
|
|
|
|
|
|
|
|
// visibility が private かつ投稿者のIDが自分のIDではなかったら非表示
|
|
|
|
if (_note.visibility == 'private' && (meId == null || !meId.equals(_note.userId))) {
|
|
|
|
hide = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// visibility が specified かつ自分が指定されていなかったら非表示
|
|
|
|
if (_note.visibility == 'specified') {
|
|
|
|
if (meId == null) {
|
|
|
|
hide = true;
|
|
|
|
} else if (meId.equals(_note.userId)) {
|
|
|
|
hide = false;
|
|
|
|
} else {
|
|
|
|
// 指定されているかどうか
|
|
|
|
const specified = _note.visibleUserIds.test(id => id.equals(meId));
|
|
|
|
|
|
|
|
if (specified) {
|
|
|
|
hide = false;
|
|
|
|
} else {
|
|
|
|
hide = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// visibility が followers かつ自分が投稿者のフォロワーでなかったら非表示
|
|
|
|
if (_note.visibility == 'followers') {
|
|
|
|
if (meId == null) {
|
|
|
|
hide = true;
|
|
|
|
} else if (meId.equals(_note.userId)) {
|
|
|
|
hide = false;
|
|
|
|
} else {
|
|
|
|
// フォロワーかどうか
|
|
|
|
const following = await Following.findOne({
|
|
|
|
followeeId: _note.userId,
|
|
|
|
followerId: meId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (following == null) {
|
|
|
|
hide = true;
|
|
|
|
} else {
|
|
|
|
hide = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
const id = _note._id;
|
2018-02-02 01:06:01 +02:00
|
|
|
|
|
|
|
// Rename _id to id
|
2018-04-07 20:30:37 +03:00
|
|
|
_note.id = _note._id;
|
|
|
|
delete _note._id;
|
2018-02-02 01:06:01 +02:00
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
delete _note.mentions;
|
|
|
|
if (_note.geo) delete _note.geo.type;
|
2018-02-02 01:06:01 +02:00
|
|
|
|
|
|
|
// Populate user
|
2018-04-07 20:30:37 +03:00
|
|
|
_note.user = packUser(_note.userId, meId);
|
2018-02-02 01:06:01 +02:00
|
|
|
|
|
|
|
// Populate app
|
2018-04-07 20:30:37 +03:00
|
|
|
if (_note.appId) {
|
|
|
|
_note.app = packApp(_note.appId);
|
2018-02-02 01:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate channel
|
2018-04-07 20:30:37 +03:00
|
|
|
if (_note.channelId) {
|
|
|
|
_note.channel = packChannel(_note.channelId);
|
2018-02-02 01:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate media
|
2018-04-07 20:30:37 +03:00
|
|
|
if (_note.mediaIds) {
|
|
|
|
_note.media = Promise.all(_note.mediaIds.map(fileId =>
|
2018-02-02 01:06:01 +02:00
|
|
|
packFile(fileId)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
// When requested a detailed note data
|
2018-02-02 01:06:01 +02:00
|
|
|
if (opts.detail) {
|
2018-04-07 20:30:37 +03:00
|
|
|
// Get previous note info
|
|
|
|
_note.prev = (async () => {
|
|
|
|
const prev = await Note.findOne({
|
|
|
|
userId: _note.userId,
|
2018-02-02 01:06:01 +02:00
|
|
|
_id: {
|
|
|
|
$lt: id
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: true
|
|
|
|
},
|
|
|
|
sort: {
|
|
|
|
_id: -1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return prev ? prev._id : null;
|
|
|
|
})();
|
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
// Get next note info
|
|
|
|
_note.next = (async () => {
|
|
|
|
const next = await Note.findOne({
|
|
|
|
userId: _note.userId,
|
2018-02-02 01:06:01 +02:00
|
|
|
_id: {
|
|
|
|
$gt: id
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
fields: {
|
|
|
|
_id: true
|
|
|
|
},
|
|
|
|
sort: {
|
|
|
|
_id: 1
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return next ? next._id : null;
|
|
|
|
})();
|
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
if (_note.replyId) {
|
|
|
|
// Populate reply to note
|
|
|
|
_note.reply = pack(_note.replyId, meId, {
|
2018-02-02 01:06:01 +02:00
|
|
|
detail: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
if (_note.renoteId) {
|
|
|
|
// Populate renote
|
|
|
|
_note.renote = pack(_note.renoteId, meId, {
|
|
|
|
detail: _note.text == null
|
2018-02-02 01:06:01 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Poll
|
2018-04-07 20:30:37 +03:00
|
|
|
if (meId && _note.poll) {
|
|
|
|
_note.poll = (async (poll) => {
|
2018-04-15 00:34:55 +03:00
|
|
|
const vote = await PollVote
|
2018-02-02 01:06:01 +02:00
|
|
|
.findOne({
|
2018-03-29 08:48:47 +03:00
|
|
|
userId: meId,
|
2018-04-07 20:30:37 +03:00
|
|
|
noteId: id
|
2018-02-02 01:06:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (vote != null) {
|
|
|
|
const myChoice = poll.choices
|
|
|
|
.filter(c => c.id == vote.choice)[0];
|
|
|
|
|
2018-03-29 08:48:47 +03:00
|
|
|
myChoice.isVoted = true;
|
2018-02-02 01:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return poll;
|
2018-04-07 20:30:37 +03:00
|
|
|
})(_note.poll);
|
2018-02-02 01:06:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch my reaction
|
|
|
|
if (meId) {
|
2018-04-07 20:30:37 +03:00
|
|
|
_note.myReaction = (async () => {
|
2018-02-02 01:06:01 +02:00
|
|
|
const reaction = await Reaction
|
|
|
|
.findOne({
|
2018-03-29 08:48:47 +03:00
|
|
|
userId: meId,
|
2018-04-07 20:30:37 +03:00
|
|
|
noteId: id,
|
2018-03-29 08:48:47 +03:00
|
|
|
deletedAt: { $exists: false }
|
2018-02-02 01:06:01 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (reaction) {
|
|
|
|
return reaction.reaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
// resolve promises in _note object
|
|
|
|
_note = await rap(_note);
|
2018-02-02 01:06:01 +02:00
|
|
|
|
2018-04-07 20:30:37 +03:00
|
|
|
return _note;
|
2018-02-02 01:06:01 +02:00
|
|
|
};
|