2019-02-09 04:01:21 +00:00
|
|
|
|
import * as promiseLimit from 'promise-limit';
|
2018-04-08 19:08:56 +00:00
|
|
|
|
|
2021-03-23 08:43:07 +00:00
|
|
|
|
import config from '@/config';
|
2018-04-08 19:08:56 +00:00
|
|
|
|
import Resolver from '../resolver';
|
|
|
|
|
import post from '../../../services/note/create';
|
2018-04-17 06:30:58 +00:00
|
|
|
|
import { resolvePerson, updatePerson } from './person';
|
2018-04-08 19:08:56 +00:00
|
|
|
|
import { resolveImage } from './image';
|
2020-01-30 09:58:13 +00:00
|
|
|
|
import { IRemoteUser } from '../../../models/entities/user';
|
2020-04-03 13:51:38 +00:00
|
|
|
|
import { htmlToMfm } from '../misc/html-to-mfm';
|
|
|
|
|
import { extractApHashtags } from './tag';
|
|
|
|
|
import { unique, toArray, toSingle } from '../../../prelude/array';
|
2019-01-21 04:27:19 +00:00
|
|
|
|
import { extractPollFromQuestion } from './question';
|
|
|
|
|
import vote from '../../../services/note/polls/vote';
|
2019-02-03 07:45:13 +00:00
|
|
|
|
import { apLogger } from '../logger';
|
2019-04-07 12:50:36 +00:00
|
|
|
|
import { DriveFile } from '../../../models/entities/drive-file';
|
2019-03-07 12:19:32 +00:00
|
|
|
|
import { deliverQuestionUpdate } from '../../../services/note/polls/update';
|
2021-03-23 08:43:07 +00:00
|
|
|
|
import { extractDbHost, toPuny } from '@/misc/convert-host';
|
2020-05-08 23:21:42 +00:00
|
|
|
|
import { Emojis, Polls, MessagingMessages } from '../../../models';
|
2019-04-07 12:50:36 +00:00
|
|
|
|
import { Note } from '../../../models/entities/note';
|
2020-04-11 09:27:58 +00:00
|
|
|
|
import { IObject, getOneApId, getApId, getOneApHrefNullable, validPost, IPost, isEmoji } from '../type';
|
2019-04-07 12:50:36 +00:00
|
|
|
|
import { Emoji } from '../../../models/entities/emoji';
|
2021-03-23 08:43:07 +00:00
|
|
|
|
import { genId } from '@/misc/gen-id';
|
|
|
|
|
import { fetchMeta } from '@/misc/fetch-meta';
|
|
|
|
|
import { getApLock } from '@/misc/app-lock';
|
2019-10-28 21:01:14 +00:00
|
|
|
|
import { createMessage } from '../../../services/messages/create';
|
2020-01-30 09:58:13 +00:00
|
|
|
|
import { parseAudience } from '../audience';
|
2020-04-03 13:51:38 +00:00
|
|
|
|
import { extractApMentions } from './mention';
|
2020-05-08 23:21:42 +00:00
|
|
|
|
import DbResolver from '../db-resolver';
|
2018-04-08 19:08:56 +00:00
|
|
|
|
|
2019-02-03 07:45:13 +00:00
|
|
|
|
const logger = apLogger;
|
2018-04-08 19:08:56 +00:00
|
|
|
|
|
2019-05-09 06:43:31 +00:00
|
|
|
|
export function validateNote(object: any, uri: string) {
|
|
|
|
|
const expectHost = extractDbHost(uri);
|
|
|
|
|
|
|
|
|
|
if (object == null) {
|
|
|
|
|
return new Error('invalid Note: object is null');
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 09:54:10 +00:00
|
|
|
|
if (!validPost.includes(object.type)) {
|
2019-09-15 14:27:33 +00:00
|
|
|
|
return new Error(`invalid Note: invalid object type ${object.type}`);
|
2019-05-09 06:43:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (object.id && extractDbHost(object.id) !== expectHost) {
|
|
|
|
|
return new Error(`invalid Note: id has different host. expected: ${expectHost}, actual: ${extractDbHost(object.id)}`);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 09:54:10 +00:00
|
|
|
|
if (object.attributedTo && extractDbHost(getOneApId(object.attributedTo)) !== expectHost) {
|
2019-05-09 06:43:31 +00:00
|
|
|
|
return new Error(`invalid Note: attributedTo has different host. expected: ${expectHost}, actual: ${extractDbHost(object.attributedTo)}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-08 19:08:56 +00:00
|
|
|
|
/**
|
|
|
|
|
* Noteをフェッチします。
|
|
|
|
|
*
|
|
|
|
|
* Misskeyに対象のNoteが登録されていればそれを返します。
|
|
|
|
|
*/
|
2020-05-08 23:21:42 +00:00
|
|
|
|
export async function fetchNote(object: string | IObject): Promise<Note | null> {
|
|
|
|
|
const dbResolver = new DbResolver();
|
|
|
|
|
return await dbResolver.getNoteFromApId(object);
|
2018-04-08 19:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Noteを作成します。
|
|
|
|
|
*/
|
2020-02-08 12:40:06 +00:00
|
|
|
|
export async function createNote(value: string | IObject, resolver?: Resolver, silent = false): Promise<Note | null> {
|
2018-04-08 19:08:56 +00:00
|
|
|
|
if (resolver == null) resolver = new Resolver();
|
|
|
|
|
|
2019-03-06 13:55:47 +00:00
|
|
|
|
const object: any = await resolver.resolve(value);
|
2018-04-08 19:08:56 +00:00
|
|
|
|
|
2019-06-28 09:54:10 +00:00
|
|
|
|
const entryUri = getApId(value);
|
2019-05-09 06:43:31 +00:00
|
|
|
|
const err = validateNote(object, entryUri);
|
|
|
|
|
if (err) {
|
|
|
|
|
logger.error(`${err.message}`, {
|
2019-03-04 05:02:42 +00:00
|
|
|
|
resolver: {
|
|
|
|
|
history: resolver.getHistory()
|
|
|
|
|
},
|
|
|
|
|
value: value,
|
|
|
|
|
object: object
|
|
|
|
|
});
|
2019-04-13 19:17:24 +00:00
|
|
|
|
throw new Error('invalid note');
|
2018-04-08 19:08:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-29 16:38:05 +00:00
|
|
|
|
const note: IPost = object;
|
2018-04-08 19:08:56 +00:00
|
|
|
|
|
2019-03-06 13:55:47 +00:00
|
|
|
|
logger.debug(`Note fetched: ${JSON.stringify(note, null, 2)}`);
|
|
|
|
|
|
2019-02-03 07:45:13 +00:00
|
|
|
|
logger.info(`Creating the Note: ${note.id}`);
|
2018-04-08 19:08:56 +00:00
|
|
|
|
|
|
|
|
|
// 投稿者をフェッチ
|
2019-06-28 09:54:10 +00:00
|
|
|
|
const actor = await resolvePerson(getOneApId(note.attributedTo), resolver) as IRemoteUser;
|
2018-04-08 19:08:56 +00:00
|
|
|
|
|
2018-04-19 09:54:34 +00:00
|
|
|
|
// 投稿者が凍結されていたらスキップ
|
|
|
|
|
if (actor.isSuspended) {
|
2019-04-13 19:17:24 +00:00
|
|
|
|
throw new Error('actor has been suspended');
|
2018-04-19 09:54:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-30 09:58:13 +00:00
|
|
|
|
const noteAudience = await parseAudience(actor, note.to, note.cc);
|
|
|
|
|
let visibility = noteAudience.visibility;
|
2020-02-08 12:40:06 +00:00
|
|
|
|
const visibleUsers = noteAudience.visibleUsers;
|
2020-01-30 09:58:13 +00:00
|
|
|
|
|
|
|
|
|
// Audience (to, cc) が指定されてなかった場合
|
|
|
|
|
if (visibility === 'specified' && visibleUsers.length === 0) {
|
2020-02-08 12:40:06 +00:00
|
|
|
|
if (typeof value === 'string') { // 入力がstringならばresolverでGETが発生している
|
2020-01-30 09:58:13 +00:00
|
|
|
|
// こちらから匿名GET出来たものならばpublic
|
|
|
|
|
visibility = 'public';
|
2018-04-28 19:44:58 +00:00
|
|
|
|
}
|
2019-04-12 16:43:22 +00:00
|
|
|
|
}
|
2018-11-08 23:44:19 +00:00
|
|
|
|
|
2020-02-06 08:11:02 +00:00
|
|
|
|
let isTalk = note._misskey_talk && visibility === 'specified';
|
|
|
|
|
|
2020-04-03 13:51:38 +00:00
|
|
|
|
const apMentions = await extractApMentions(note.tag);
|
|
|
|
|
const apHashtags = await extractApHashtags(note.tag);
|
2018-12-02 09:05:33 +00:00
|
|
|
|
|
2018-09-05 10:32:46 +00:00
|
|
|
|
// 添付ファイル
|
2018-04-08 19:08:56 +00:00
|
|
|
|
// TODO: attachmentは必ずしもImageではない
|
|
|
|
|
// TODO: attachmentは必ずしも配列ではない
|
2018-08-19 09:08:29 +00:00
|
|
|
|
// Noteがsensitiveなら添付もsensitiveにする
|
2019-02-09 04:01:21 +00:00
|
|
|
|
const limit = promiseLimit(2);
|
2019-03-06 13:55:47 +00:00
|
|
|
|
|
|
|
|
|
note.attachment = Array.isArray(note.attachment) ? note.attachment : note.attachment ? [note.attachment] : [];
|
2018-09-05 10:32:46 +00:00
|
|
|
|
const files = note.attachment
|
2018-08-19 09:08:29 +00:00
|
|
|
|
.map(attach => attach.sensitive = note.sensitive)
|
2019-04-07 12:50:36 +00:00
|
|
|
|
? (await Promise.all(note.attachment.map(x => limit(() => resolveImage(actor, x)) as Promise<DriveFile>)))
|
2019-03-10 13:27:25 +00:00
|
|
|
|
.filter(image => image != null)
|
2018-04-08 19:08:56 +00:00
|
|
|
|
: [];
|
|
|
|
|
|
|
|
|
|
// リプライ
|
2019-04-13 09:45:07 +00:00
|
|
|
|
const reply: Note | null = note.inReplyTo
|
|
|
|
|
? await resolveNote(note.inReplyTo, resolver).then(x => {
|
|
|
|
|
if (x == null) {
|
|
|
|
|
logger.warn(`Specified inReplyTo, but nout found`);
|
2019-04-13 19:17:24 +00:00
|
|
|
|
throw new Error('inReplyTo not found');
|
2019-04-13 09:45:07 +00:00
|
|
|
|
} else {
|
|
|
|
|
return x;
|
2019-03-10 13:27:25 +00:00
|
|
|
|
}
|
2020-02-06 08:11:02 +00:00
|
|
|
|
}).catch(async e => {
|
|
|
|
|
// トークだったらinReplyToのエラーは無視
|
|
|
|
|
const uri = getApId(note.inReplyTo);
|
|
|
|
|
if (uri.startsWith(config.url + '/')) {
|
|
|
|
|
const id = uri.split('/').pop();
|
|
|
|
|
const talk = await MessagingMessages.findOne(id);
|
|
|
|
|
if (talk) {
|
|
|
|
|
isTalk = true;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-10 13:27:25 +00:00
|
|
|
|
logger.warn(`Error in inReplyTo ${note.inReplyTo} - ${e.statusCode || e}`);
|
|
|
|
|
throw e;
|
|
|
|
|
})
|
|
|
|
|
: null;
|
2018-04-08 19:08:56 +00:00
|
|
|
|
|
2018-11-22 17:10:07 +00:00
|
|
|
|
// 引用
|
2019-04-12 16:43:22 +00:00
|
|
|
|
let quote: Note | undefined | null;
|
2018-11-22 17:10:07 +00:00
|
|
|
|
|
2019-12-14 18:37:54 +00:00
|
|
|
|
if (note._misskey_quote || note.quoteUrl) {
|
|
|
|
|
const tryResolveNote = async (uri: string): Promise<{
|
|
|
|
|
status: 'ok';
|
|
|
|
|
res: Note | null;
|
|
|
|
|
} | {
|
|
|
|
|
status: 'permerror' | 'temperror';
|
|
|
|
|
}> => {
|
|
|
|
|
if (typeof uri !== 'string' || !uri.match(/^https?:/)) return { status: 'permerror' };
|
|
|
|
|
try {
|
|
|
|
|
const res = await resolveNote(uri);
|
|
|
|
|
if (res) {
|
|
|
|
|
return {
|
|
|
|
|
status: 'ok',
|
|
|
|
|
res
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
return {
|
|
|
|
|
status: 'permerror'
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return {
|
|
|
|
|
status: e.statusCode >= 400 && e.statusCode < 500 ? 'permerror' : 'temperror'
|
|
|
|
|
};
|
2019-03-13 02:21:16 +00:00
|
|
|
|
}
|
2019-12-14 18:37:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const uris = unique([note._misskey_quote, note.quoteUrl].filter((x): x is string => typeof x === 'string'));
|
|
|
|
|
const results = await Promise.all(uris.map(uri => tryResolveNote(uri)));
|
|
|
|
|
|
|
|
|
|
quote = results.filter((x): x is { status: 'ok', res: Note | null } => x.status === 'ok').map(x => x.res).find(x => x);
|
|
|
|
|
if (!quote) {
|
|
|
|
|
if (results.some(x => x.status === 'temperror')) {
|
|
|
|
|
throw 'quote resolve failed';
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-22 17:10:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-30 22:15:10 +00:00
|
|
|
|
const cw = note.summary === '' ? null : note.summary;
|
|
|
|
|
|
2018-05-06 17:54:14 +00:00
|
|
|
|
// テキストのパース
|
2020-04-03 13:51:38 +00:00
|
|
|
|
const text = note._misskey_content || (note.content ? htmlToMfm(note.content, note.tag) : null);
|
2018-04-08 19:08:56 +00:00
|
|
|
|
|
2019-01-21 04:27:19 +00:00
|
|
|
|
// vote
|
2019-04-07 12:50:36 +00:00
|
|
|
|
if (reply && reply.hasPoll) {
|
2021-02-13 06:33:38 +00:00
|
|
|
|
const poll = await Polls.findOneOrFail(reply.id);
|
2019-04-12 16:43:22 +00:00
|
|
|
|
|
2019-03-06 13:55:47 +00:00
|
|
|
|
const tryCreateVote = async (name: string, index: number): Promise<null> => {
|
2019-04-07 12:50:36 +00:00
|
|
|
|
if (poll.expiresAt && Date.now() > new Date(poll.expiresAt).getTime()) {
|
2019-03-06 13:55:47 +00:00
|
|
|
|
logger.warn(`vote to expired poll from AP: actor=${actor.username}@${actor.host}, note=${note.id}, choice=${name}`);
|
|
|
|
|
} else if (index >= 0) {
|
|
|
|
|
logger.info(`vote from AP: actor=${actor.username}@${actor.host}, note=${note.id}, choice=${name}`);
|
|
|
|
|
await vote(actor, reply, index);
|
2019-03-07 12:19:32 +00:00
|
|
|
|
|
|
|
|
|
// リモートフォロワーにUpdate配信
|
2019-04-07 12:50:36 +00:00
|
|
|
|
deliverQuestionUpdate(reply.id);
|
2019-03-06 13:55:47 +00:00
|
|
|
|
}
|
2019-01-21 04:27:19 +00:00
|
|
|
|
return null;
|
2019-03-06 13:55:47 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (note.name) {
|
2019-04-07 12:50:36 +00:00
|
|
|
|
return await tryCreateVote(note.name, poll.choices.findIndex(x => x === note.name));
|
2019-03-06 13:55:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 後方互換性のため
|
|
|
|
|
if (text) {
|
|
|
|
|
const m = text.match(/(\d+)$/);
|
|
|
|
|
|
|
|
|
|
if (m) {
|
|
|
|
|
return await tryCreateVote(m[0], Number(m[1]));
|
|
|
|
|
}
|
2019-01-21 04:27:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
|
const emojis = await extractEmojis(note.tag || [], actor.host).catch(e => {
|
2019-02-03 07:45:13 +00:00
|
|
|
|
logger.info(`extractEmojis: ${e}`);
|
2019-04-07 12:50:36 +00:00
|
|
|
|
return [] as Emoji[];
|
2018-11-01 23:59:40 +00:00
|
|
|
|
});
|
|
|
|
|
|
2018-12-02 09:05:33 +00:00
|
|
|
|
const apEmojis = emojis.map(emoji => emoji.name);
|
|
|
|
|
|
2019-07-19 18:20:06 +00:00
|
|
|
|
const poll = await extractPollFromQuestion(note, resolver).catch(() => undefined);
|
2019-01-21 04:27:19 +00:00
|
|
|
|
|
2018-04-17 06:30:58 +00:00
|
|
|
|
// ユーザーの情報が古かったらついでに更新しておく
|
2018-11-22 23:01:14 +00:00
|
|
|
|
if (actor.lastFetchedAt == null || Date.now() - actor.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24) {
|
2019-06-28 09:54:10 +00:00
|
|
|
|
if (actor.uri) updatePerson(actor.uri);
|
2018-04-17 06:30:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-06 08:11:02 +00:00
|
|
|
|
if (isTalk) {
|
2019-10-28 21:01:14 +00:00
|
|
|
|
for (const recipient of visibleUsers) {
|
2019-12-14 18:37:19 +00:00
|
|
|
|
await createMessage(actor, recipient, undefined, text || undefined, (files && files.length > 0) ? files[0] : null, object.id);
|
2019-10-28 21:01:14 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-08 19:08:56 +00:00
|
|
|
|
return await post(actor, {
|
2019-04-12 16:43:22 +00:00
|
|
|
|
createdAt: note.published ? new Date(note.published) : null,
|
2019-03-06 13:55:47 +00:00
|
|
|
|
files,
|
2018-04-08 19:08:56 +00:00
|
|
|
|
reply,
|
2018-11-22 17:10:07 +00:00
|
|
|
|
renote: quote,
|
2019-03-14 15:23:24 +00:00
|
|
|
|
name: note.name,
|
2019-03-06 13:55:47 +00:00
|
|
|
|
cw,
|
|
|
|
|
text,
|
2018-04-08 19:08:56 +00:00
|
|
|
|
viaMobile: false,
|
2018-11-15 20:47:29 +00:00
|
|
|
|
localOnly: false,
|
2018-04-08 19:08:56 +00:00
|
|
|
|
visibility,
|
2018-04-28 19:44:58 +00:00
|
|
|
|
visibleUsers,
|
2018-11-08 23:44:19 +00:00
|
|
|
|
apMentions,
|
2018-12-02 09:05:33 +00:00
|
|
|
|
apHashtags,
|
|
|
|
|
apEmojis,
|
2019-01-21 04:27:19 +00:00
|
|
|
|
poll,
|
2020-04-02 12:59:14 +00:00
|
|
|
|
uri: note.id,
|
2020-04-11 09:27:58 +00:00
|
|
|
|
url: getOneApHrefNullable(note.url),
|
2018-04-08 19:08:56 +00:00
|
|
|
|
}, silent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Noteを解決します。
|
|
|
|
|
*
|
|
|
|
|
* Misskeyに対象のNoteが登録されていればそれを返し、そうでなければ
|
|
|
|
|
* リモートサーバーからフェッチしてMisskeyに登録しそれを返します。
|
|
|
|
|
*/
|
2019-04-12 16:43:22 +00:00
|
|
|
|
export async function resolveNote(value: string | IObject, resolver?: Resolver): Promise<Note | null> {
|
2020-04-03 23:46:54 +00:00
|
|
|
|
const uri = typeof value === 'string' ? value : value.id;
|
2019-04-13 19:17:24 +00:00
|
|
|
|
if (uri == null) throw new Error('missing uri');
|
2018-04-08 19:08:56 +00:00
|
|
|
|
|
2019-03-13 02:21:16 +00:00
|
|
|
|
// ブロックしてたら中断
|
2019-04-07 12:50:36 +00:00
|
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
if (meta.blockedHosts.includes(extractDbHost(uri))) throw { statusCode: 451 };
|
2019-03-13 02:21:16 +00:00
|
|
|
|
|
2019-09-09 13:46:45 +00:00
|
|
|
|
const unlock = await getApLock(uri);
|
2018-04-08 19:08:56 +00:00
|
|
|
|
|
2019-09-09 13:46:45 +00:00
|
|
|
|
try {
|
|
|
|
|
//#region このサーバーに既に登録されていたらそれを返す
|
|
|
|
|
const exist = await fetchNote(uri);
|
2018-04-08 19:08:56 +00:00
|
|
|
|
|
2019-09-09 13:46:45 +00:00
|
|
|
|
if (exist) {
|
|
|
|
|
return exist;
|
2019-04-12 04:07:56 +00:00
|
|
|
|
}
|
2019-09-09 13:46:45 +00:00
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
|
|
// リモートサーバーからフェッチしてきて登録
|
|
|
|
|
// ここでuriの代わりに添付されてきたNote Objectが指定されていると、サーバーフェッチを経ずにノートが生成されるが
|
|
|
|
|
// 添付されてきたNote Objectは偽装されている可能性があるため、常にuriを指定してサーバーフェッチを行う。
|
|
|
|
|
return await createNote(uri, resolver, true);
|
|
|
|
|
} finally {
|
|
|
|
|
unlock();
|
|
|
|
|
}
|
2018-04-08 19:08:56 +00:00
|
|
|
|
}
|
2018-11-01 23:59:40 +00:00
|
|
|
|
|
2020-04-03 13:51:38 +00:00
|
|
|
|
export async function extractEmojis(tags: IObject | IObject[], host: string): Promise<Emoji[]> {
|
2019-04-09 15:59:41 +00:00
|
|
|
|
host = toPuny(host);
|
2018-11-01 23:59:40 +00:00
|
|
|
|
|
|
|
|
|
if (!tags) return [];
|
|
|
|
|
|
2020-04-03 13:51:38 +00:00
|
|
|
|
const eomjiTags = toArray(tags).filter(isEmoji);
|
2018-11-01 23:59:40 +00:00
|
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
|
return await Promise.all(eomjiTags.map(async tag => {
|
|
|
|
|
const name = tag.name!.replace(/^:/, '').replace(/:$/, '');
|
2020-04-03 13:51:38 +00:00
|
|
|
|
tag.icon = toSingle(tag.icon);
|
2018-11-01 23:59:40 +00:00
|
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
|
const exists = await Emojis.findOne({
|
|
|
|
|
host,
|
|
|
|
|
name
|
|
|
|
|
});
|
2019-04-07 12:50:36 +00:00
|
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
|
if (exists) {
|
|
|
|
|
if ((tag.updated != null && exists.updatedAt == null)
|
|
|
|
|
|| (tag.id != null && exists.uri == null)
|
|
|
|
|
|| (tag.updated != null && exists.updatedAt != null && new Date(tag.updated) > exists.updatedAt)
|
2019-07-27 20:32:40 +00:00
|
|
|
|
|| (tag.icon!.url !== exists.url)
|
2019-04-12 16:43:22 +00:00
|
|
|
|
) {
|
|
|
|
|
await Emojis.update({
|
|
|
|
|
host,
|
|
|
|
|
name,
|
|
|
|
|
}, {
|
|
|
|
|
uri: tag.id,
|
|
|
|
|
url: tag.icon!.url,
|
2019-07-27 20:32:40 +00:00
|
|
|
|
updatedAt: new Date(),
|
2019-04-12 16:43:22 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return await Emojis.findOne({
|
|
|
|
|
host,
|
|
|
|
|
name
|
|
|
|
|
}) as Emoji;
|
2018-11-01 23:59:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
|
return exists;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info(`register emoji host=${host}, name=${name}`);
|
|
|
|
|
|
|
|
|
|
return await Emojis.save({
|
|
|
|
|
id: genId(),
|
|
|
|
|
host,
|
|
|
|
|
name,
|
|
|
|
|
uri: tag.id,
|
|
|
|
|
url: tag.icon!.url,
|
2019-07-27 20:32:40 +00:00
|
|
|
|
updatedAt: new Date(),
|
2019-04-12 16:43:22 +00:00
|
|
|
|
aliases: []
|
|
|
|
|
} as Partial<Emoji>);
|
|
|
|
|
}));
|
2018-11-01 23:59:40 +00:00
|
|
|
|
}
|