misskey-awawa/src/remote/activitypub/resolve-person.ts

108 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-03-31 10:55:00 +00:00
import { JSDOM } from 'jsdom';
import { toUnicode } from 'punycode';
2018-04-01 19:15:27 +00:00
import User, { validateUsername, isValidName, isValidDescription } from '../../models/user';
import queue from '../../queue';
2018-03-31 10:55:00 +00:00
import webFinger from '../webfinger';
import create from './create';
import Resolver from './resolver';
async function isCollection(collection) {
return ['Collection', 'OrderedCollection'].includes(collection.type);
}
2018-04-02 09:36:47 +00:00
export default async (value, verifier?: string) => {
2018-04-01 12:24:25 +00:00
const { resolver, object } = await new Resolver().resolveOne(value);
2018-03-31 10:55:00 +00:00
if (
object === null ||
object.type !== 'Person' ||
typeof object.preferredUsername !== 'string' ||
2018-04-02 09:36:47 +00:00
!validateUsername(object.preferredUsername) ||
2018-03-31 10:55:00 +00:00
!isValidName(object.name) ||
!isValidDescription(object.summary)
) {
2018-04-01 12:24:25 +00:00
throw new Error();
2018-03-31 10:55:00 +00:00
}
const [followers, following, outbox, finger] = await Promise.all([
resolver.resolveOne(object.followers).then(
resolved => isCollection(resolved.object) ? resolved.object : null,
() => null
),
resolver.resolveOne(object.following).then(
resolved => isCollection(resolved.object) ? resolved.object : null,
() => null
),
resolver.resolveOne(object.outbox).then(
resolved => isCollection(resolved.object) ? resolved.object : null,
() => null
),
2018-04-02 09:36:47 +00:00
webFinger(object.id, verifier),
2018-03-31 10:55:00 +00:00
]);
2018-04-02 09:36:47 +00:00
const host = toUnicode(finger.subject.replace(/^.*?@/, ''));
const hostLower = host.replace(/[A-Z]+/, matched => matched.toLowerCase());
2018-03-31 10:55:00 +00:00
const summaryDOM = JSDOM.fragment(object.summary);
// Create user
const user = await User.insert({
avatarId: null,
bannerId: null,
createdAt: Date.parse(object.published),
description: summaryDOM.textContent,
2018-04-02 08:11:14 +00:00
followersCount: followers ? followers.totalItem || 0 : 0,
followingCount: following ? following.totalItem || 0 : 0,
2018-03-31 10:55:00 +00:00
name: object.name,
2018-04-02 08:11:14 +00:00
postsCount: outbox ? outbox.totalItem || 0 : 0,
2018-03-31 10:55:00 +00:00
driveCapacity: 1024 * 1024 * 8, // 8MiB
username: object.preferredUsername,
2018-04-02 09:36:47 +00:00
usernameLower: object.preferredUsername.toLowerCase(),
host,
2018-03-31 10:55:00 +00:00
hostLower,
account: {
2018-04-01 06:58:49 +00:00
publicKey: {
id: object.publicKey.id,
publicKeyPem: object.publicKey.publicKeyPem
},
2018-04-01 10:43:26 +00:00
inbox: object.inbox,
2018-03-31 10:55:00 +00:00
uri: object.id,
},
});
queue.create('http', {
type: 'performActivityPub',
actor: user._id,
outbox
}).save();
const [avatarId, bannerId] = await Promise.all([
object.icon,
object.image
].map(async value => {
if (value === undefined) {
return null;
}
try {
const created = await create(resolver, user, value);
await Promise.all(created.map(asyncCreated => asyncCreated.then(created => {
if (created !== null && created.object.$ref === 'driveFiles.files') {
throw created.object.$id;
}
}, () => {})));
return null;
} catch (id) {
return id;
}
}));
User.update({ _id: user._id }, { $set: { avatarId, bannerId } });
user.avatarId = avatarId;
user.bannerId = bannerId;
return user;
};