1
0
forked from mirror/misskey
This commit is contained in:
syuilo 2018-04-05 00:50:57 +09:00
parent 0009a371c8
commit fd73fad148
3 changed files with 31 additions and 84 deletions

View File

@ -15,6 +15,8 @@ import Mute from '../../models/mute';
import pushSw from '../../publishers/push-sw'; import pushSw from '../../publishers/push-sw';
import event from '../../publishers/stream'; import event from '../../publishers/stream';
import parse from '../../text/parse'; import parse from '../../text/parse';
import html from '../../text/html';
import { IApp } from '../../models/app';
export default async (user: IUser, content: { export default async (user: IUser, content: {
createdAt: Date; createdAt: Date;
@ -23,9 +25,14 @@ export default async (user: IUser, content: {
repost: IPost; repost: IPost;
media: IDriveFile[]; media: IDriveFile[];
geo: any; geo: any;
poll: any;
viaMobile: boolean; viaMobile: boolean;
tags: string[]; tags: string[];
}) => new Promise(async (res, rej) => { cw: string;
visibility: string;
uri?: string;
app?: IApp;
}) => new Promise<IPost>(async (res, rej) => {
const tags = content.tags || []; const tags = content.tags || [];
let tokens = null; let tokens = null;
@ -53,10 +60,16 @@ export default async (user: IUser, content: {
replyId: content.reply ? content.reply._id : null, replyId: content.reply ? content.reply._id : null,
repostId: content.repost ? content.repost._id : null, repostId: content.repost ? content.repost._id : null,
text: content.text, text: content.text,
textHtml: tokens === null ? null : html(tokens),
poll: content.poll,
cw: content.cw,
tags, tags,
userId: user._id, userId: user._id,
viaMobile: content.viaMobile, viaMobile: content.viaMobile,
geo: content.geo || null, geo: content.geo || null,
uri: content.uri,
appId: content.app ? content.app._id : null,
visibility: content.visibility,
// 以下非正規化データ // 以下非正規化データ
_reply: content.reply ? { userId: content.reply.userId } : null, _reply: content.reply ? { userId: content.reply.userId } : null,

View File

@ -63,32 +63,26 @@ export default async (actor, activity): Promise<void> => {
throw new Error('invalid note'); throw new Error('invalid note');
} }
const mediaIds = []; const media = [];
if ('attachment' in note) { if ('attachment' in note) {
note.attachment.forEach(async media => { note.attachment.forEach(async media => {
const created = await createImage(resolver, media); const created = await createImage(resolver, media);
mediaIds.push(created._id); media.push(created);
}); });
} }
const { window } = new JSDOM(note.content); const { window } = new JSDOM(note.content);
await createPost(actor, { await createPost(actor, {
channelId: undefined,
index: undefined,
createdAt: new Date(note.published), createdAt: new Date(note.published),
mediaIds, media,
replyId: undefined, reply: undefined,
repostId: undefined, repost: undefined,
poll: undefined,
text: window.document.body.textContent, text: window.document.body.textContent,
textHtml: note.content && createDOMPurify(window).sanitize(note.content),
userId: actor._id,
appId: null,
viaMobile: false, viaMobile: false,
geo: undefined, geo: undefined,
uri: note.id uri: note.id
}, null, null, []); });
} }
}; };

View File

@ -3,16 +3,12 @@
*/ */
import $ from 'cafy'; import $ from 'cafy';
import deepEqual = require('deep-equal'); import deepEqual = require('deep-equal');
import renderAcct from '../../../../acct/render'; import Post, { IPost, isValidText, isValidCw, pack } from '../../../../models/post';
import config from '../../../../config';
import html from '../../../../text/html';
import parse from '../../../../text/parse';
import Post, { IPost, isValidText, isValidCw } from '../../../../models/post';
import { ILocalUser } from '../../../../models/user'; import { ILocalUser } from '../../../../models/user';
import Channel, { IChannel } from '../../../../models/channel'; import Channel, { IChannel } from '../../../../models/channel';
import DriveFile from '../../../../models/drive-file'; import DriveFile from '../../../../models/drive-file';
import create from '../../../../post/create'; import create from '../../../../api/post/create';
import distribute from '../../../../post/distribute'; import { IApp } from '../../../../models/app';
/** /**
* Create a post * Create a post
@ -22,7 +18,7 @@ import distribute from '../../../../post/distribute';
* @param {any} app * @param {any} app
* @return {Promise<any>} * @return {Promise<any>}
*/ */
module.exports = (params, user: ILocalUser, app) => new Promise(async (res, rej) => { module.exports = (params, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => {
// Get 'visibility' parameter // Get 'visibility' parameter
const [visibility = 'public', visibilityErr] = $(params.visibility).optional.string().or(['public', 'unlisted', 'private', 'direct']).$; const [visibility = 'public', visibilityErr] = $(params.visibility).optional.string().or(['public', 'unlisted', 'private', 'direct']).$;
if (visibilityErr) return rej('invalid visibility'); if (visibilityErr) return rej('invalid visibility');
@ -230,82 +226,26 @@ module.exports = (params, user: ILocalUser, app) => new Promise(async (res, rej)
} }
} }
let tokens = null;
if (text) {
// Analyze
tokens = parse(text);
// Extract hashtags
const hashtags = tokens
.filter(t => t.type == 'hashtag')
.map(t => t.hashtag);
hashtags.forEach(tag => {
if (tags.indexOf(tag) == -1) {
tags.push(tag);
}
});
}
let atMentions = [];
// If has text content
if (text) {
/*
// Extract a hashtags
const hashtags = tokens
.filter(t => t.type == 'hashtag')
.map(t => t.hashtag)
// Drop dupulicates
.filter((v, i, s) => s.indexOf(v) == i);
// ハッシュタグをデータベースに登録
registerHashtags(user, hashtags);
*/
// Extract an '@' mentions
atMentions = tokens
.filter(t => t.type == 'mention')
.map(renderAcct)
// Drop dupulicates
.filter((v, i, s) => s.indexOf(v) == i);
}
// 投稿を作成 // 投稿を作成
const post = await create({ const post = await create(user, {
createdAt: new Date(), createdAt: new Date(),
channelId: channel ? channel._id : undefined, media: files,
index: channel ? channel.index + 1 : undefined,
mediaIds: files ? files.map(file => file._id) : [],
poll: poll, poll: poll,
text: text, text: text,
textHtml: tokens === null ? null : html(tokens), reply,
repost,
cw: cw, cw: cw,
tags: tags, tags: tags,
userId: user._id, app: app,
appId: app ? app._id : null,
viaMobile: viaMobile, viaMobile: viaMobile,
visibility, visibility,
geo geo
}, reply, repost, atMentions); });
const postObj = await distribute(user, post.mentions, post); const postObj = await pack(post, user);
// Reponse // Reponse
res({ res({
createdPost: postObj createdPost: postObj
}); });
// Register to search database
if (post.text && config.elasticsearch.enable) {
const es = require('../../../db/elasticsearch');
es.index({
index: 'misskey',
type: 'post',
id: post._id.toString(),
body: {
text: post.text
}
});
}
}); });