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

View file

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

View file

@ -1,5 +1,5 @@
import * as mongo from 'mongodb';
import { JSDOM } from 'jsdom';
import * as parse5 from 'parse5';
import * as debug from 'debug';
import config from '../../../config';
@ -13,6 +13,72 @@ import { IRemoteUser } from '../../../models/user';
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をフェッチします
*
@ -87,9 +153,8 @@ export async function createNote(value: any, resolver?: Resolver, silent = false
// リプライ
const reply = note.inReplyTo ? await resolveNote(note.inReplyTo, resolver) : null;
// MastodonはHTMLを送り付けてくる
// そして改行は<br />で表現されている
const { window } = new JSDOM(note.content.replace(/<br \/>/g, '\n'));
// テキストのパース
const text = parse(note.tag, note.content);
// ユーザーの情報が古かったらついでに更新しておく
if (actor.updatedAt == null || Date.now() - actor.updatedAt.getTime() > 1000 * 60 * 60 * 24) {

View file

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