This commit is contained in:
syuilo 2018-05-07 02:54:14 +09:00
parent 0159b0bbd8
commit cab573c288
3 changed files with 72 additions and 4 deletions

View File

@ -69,6 +69,7 @@
"@types/ms": "0.7.30", "@types/ms": "0.7.30",
"@types/node": "9.6.6", "@types/node": "9.6.6",
"@types/nopt": "3.0.29", "@types/nopt": "3.0.29",
"@types/parse5": "^3.0.0",
"@types/pug": "2.0.4", "@types/pug": "2.0.4",
"@types/qrcode": "0.8.1", "@types/qrcode": "0.8.1",
"@types/ratelimiter": "2.1.28", "@types/ratelimiter": "2.1.28",
@ -166,6 +167,7 @@
"object-assign-deep": "0.4.0", "object-assign-deep": "0.4.0",
"on-build-webpack": "0.1.0", "on-build-webpack": "0.1.0",
"os-utils": "0.0.14", "os-utils": "0.0.14",
"parse5": "^4.0.0",
"progress-bar-webpack-plugin": "1.11.0", "progress-bar-webpack-plugin": "1.11.0",
"prominence": "0.2.0", "prominence": "0.2.0",
"promise-sequential": "^1.1.1", "promise-sequential": "^1.1.1",

View File

@ -1,5 +1,5 @@
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import { JSDOM } from 'jsdom'; import * as parse5 from 'parse5';
import * as debug from 'debug'; import * as debug from 'debug';
import config from '../../../config'; import config from '../../../config';
@ -13,6 +13,72 @@ import { IRemoteUser } from '../../../models/user';
const log = debug('misskey:activitypub'); const log = debug('misskey:activitypub');
function parse(tag, html: string): string {
const dom = parse5.parseFragment(html) as parse5.AST.Default.Document;
let text = '';
dom.childNodes.forEach(n => analyze(n));
return text.trim();
function analyze(node) {
switch (node.nodeName) {
case '#text':
text += node.value;
break;
case 'br':
text += '\n';
break;
case 'a':
const cls = node.attrs
? (node.attrs.find(x => x.name == 'class') || { value: '' }).value.split(' ')
: [];
// for Mastodon
if (cls.includes('mention')) {
//#region ホスト名部分が省略されているので復元する
// Activityのtag情報に次のような形で省略前の情報が添付されているのでそこから持ってくる
// {
// "type": "Mention",
// "href": "https://misskey.xyz/users/57d01a501fdf2d07be417afe",
// "name": "@syuilo@misskey.xyz"
// }
const href = node.attrs.find(x => x.name == 'href').value;
const acct = tag.find(t => t.type == 'Mention' && t.href == href).name;
text += acct;
break;
//#endregion
}
if (node.childNodes) {
node.childNodes.forEach(n => analyze(n));
}
break;
case 'p':
text += '\n';
if (node.childNodes) {
node.childNodes.forEach(n => analyze(n));
}
break;
default:
if (node.childNodes) {
node.childNodes.forEach(n => analyze(n));
}
break;
}
}
}
/** /**
* Noteをフェッチします * Noteをフェッチします
* *
@ -87,9 +153,8 @@ export async function createNote(value: any, resolver?: Resolver, silent = false
// リプライ // リプライ
const reply = note.inReplyTo ? await resolveNote(note.inReplyTo, resolver) : null; const reply = note.inReplyTo ? await resolveNote(note.inReplyTo, resolver) : null;
// MastodonはHTMLを送り付けてくる // テキストのパース
// そして改行は<br />で表現されている const text = parse(note.tag, note.content);
const { window } = new JSDOM(note.content.replace(/<br \/>/g, '\n'));
// ユーザーの情報が古かったらついでに更新しておく // ユーザーの情報が古かったらついでに更新しておく
if (actor.updatedAt == null || Date.now() - actor.updatedAt.getTime() > 1000 * 60 * 60 * 24) { if (actor.updatedAt == null || Date.now() - actor.updatedAt.getTime() > 1000 * 60 * 60 * 24) {

View File

@ -15,6 +15,7 @@ export interface IObject {
icon?: any; icon?: any;
image?: any; image?: any;
url?: string; url?: string;
tag?: any[];
} }
export interface IActivity extends IObject { export interface IActivity extends IObject {