2016-12-28 23:49:51 +01:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-08 19:50:09 +01:00
|
|
|
import $ from 'cafy';
|
2018-03-27 09:51:12 +02:00
|
|
|
import { JSDOM } from 'jsdom';
|
|
|
|
import { toUnicode, toASCII } from 'punycode';
|
|
|
|
import uploadFromUrl from '../../common/drive/upload_from_url';
|
2018-03-29 13:32:18 +02:00
|
|
|
import User, { pack, validateUsername, isValidName, isValidDescription } from '../../../../models/user';
|
2018-03-27 09:51:12 +02:00
|
|
|
const request = require('request-promise-native');
|
|
|
|
const WebFinger = require('webfinger.js');
|
|
|
|
|
|
|
|
const webFinger = new WebFinger({});
|
|
|
|
|
|
|
|
async function getCollectionCount(url) {
|
|
|
|
if (!url) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const collection = await request({ url, json: true });
|
|
|
|
return collection ? collection.totalItems : null;
|
|
|
|
} catch (exception) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function findUser(q) {
|
|
|
|
return User.findOne(q, {
|
|
|
|
fields: {
|
|
|
|
data: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function webFingerAndVerify(query, verifier) {
|
|
|
|
return new Promise((res, rej) => webFinger.lookup(query, (error, result) => {
|
|
|
|
if (error) {
|
|
|
|
return rej(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.object.subject.toLowerCase().replace(/^acct:/, '') !== verifier) {
|
|
|
|
return rej('WebFinger verfification failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
res(result.object);
|
|
|
|
}));
|
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a user
|
|
|
|
*
|
2017-03-01 09:37:01 +01:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} me
|
|
|
|
* @return {Promise<any>}
|
2016-12-28 23:49:51 +01:00
|
|
|
*/
|
2017-03-03 20:28:38 +01:00
|
|
|
module.exports = (params, me) => new Promise(async (res, rej) => {
|
2018-03-27 09:51:12 +02:00
|
|
|
let user;
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
// Get 'userId' parameter
|
|
|
|
const [userId, userIdErr] = $(params.userId).optional.id().$;
|
|
|
|
if (userIdErr) return rej('invalid userId param');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Get 'username' parameter
|
2017-03-08 19:50:09 +01:00
|
|
|
const [username, usernameErr] = $(params.username).optional.string().$;
|
2017-03-02 23:47:14 +01:00
|
|
|
if (usernameErr) return rej('invalid username param');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-03-27 09:51:12 +02:00
|
|
|
// Get 'host' parameter
|
2018-04-01 07:10:22 +02:00
|
|
|
const [host, hostErr] = $(params.host).nullable.optional.string().$;
|
2018-03-29 14:45:43 +02:00
|
|
|
if (hostErr) return rej('invalid host param');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-03-27 09:51:12 +02:00
|
|
|
if (userId === undefined && typeof username !== 'string') {
|
2018-03-29 07:48:47 +02:00
|
|
|
return rej('userId or pair of username and host is required');
|
2018-03-27 09:51:12 +02:00
|
|
|
}
|
2017-02-22 05:08:33 +01:00
|
|
|
|
2016-12-28 23:49:51 +01:00
|
|
|
// Lookup user
|
2018-03-27 09:51:12 +02:00
|
|
|
if (typeof host === 'string') {
|
2018-03-29 07:48:47 +02:00
|
|
|
const usernameLower = username.toLowerCase();
|
2018-03-29 14:44:39 +02:00
|
|
|
const hostLowerAscii = toASCII(host).toLowerCase();
|
|
|
|
const hostLower = toUnicode(hostLowerAscii);
|
2018-03-27 09:51:12 +02:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
user = await findUser({ usernameLower, hostLower });
|
2018-03-27 09:51:12 +02:00
|
|
|
|
|
|
|
if (user === null) {
|
2018-03-29 14:44:39 +02:00
|
|
|
const acctLower = `${usernameLower}@${hostLowerAscii}`;
|
2018-03-27 09:51:12 +02:00
|
|
|
let activityStreams;
|
|
|
|
let finger;
|
2018-03-29 07:48:47 +02:00
|
|
|
let followersCount;
|
|
|
|
let followingCount;
|
|
|
|
let postsCount;
|
2018-03-27 09:51:12 +02:00
|
|
|
|
|
|
|
if (!validateUsername(username)) {
|
|
|
|
return rej('username validation failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2018-03-29 14:44:39 +02:00
|
|
|
finger = await webFingerAndVerify(acctLower, acctLower);
|
2018-03-27 09:51:12 +02:00
|
|
|
} catch (exception) {
|
|
|
|
return rej('WebFinger lookup failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
const self = finger.links.find(link => link.rel && link.rel.toLowerCase() === 'self');
|
|
|
|
if (!self) {
|
|
|
|
return rej('WebFinger has no reference to self representation');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
activityStreams = await request({
|
|
|
|
url: self.href,
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/activity+json, application/ld+json'
|
|
|
|
},
|
|
|
|
json: true
|
|
|
|
});
|
|
|
|
} catch (exception) {
|
|
|
|
return rej('failed to retrieve ActivityStreams representation');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(activityStreams &&
|
|
|
|
(Array.isArray(activityStreams['@context']) ?
|
|
|
|
activityStreams['@context'].includes('https://www.w3.org/ns/activitystreams') :
|
|
|
|
activityStreams['@context'] === 'https://www.w3.org/ns/activitystreams') &&
|
|
|
|
activityStreams.type === 'Person' &&
|
|
|
|
typeof activityStreams.preferredUsername === 'string' &&
|
2018-03-29 07:48:47 +02:00
|
|
|
activityStreams.preferredUsername.toLowerCase() === usernameLower &&
|
2018-03-27 09:51:12 +02:00
|
|
|
isValidName(activityStreams.name) &&
|
|
|
|
isValidDescription(activityStreams.summary)
|
|
|
|
)) {
|
|
|
|
return rej('failed ActivityStreams validation');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2018-03-29 14:44:39 +02:00
|
|
|
[followersCount, followingCount, postsCount] = await Promise.all([
|
2018-03-27 09:51:12 +02:00
|
|
|
getCollectionCount(activityStreams.followers),
|
|
|
|
getCollectionCount(activityStreams.following),
|
|
|
|
getCollectionCount(activityStreams.outbox),
|
2018-03-29 14:44:39 +02:00
|
|
|
webFingerAndVerify(activityStreams.id, acctLower),
|
2018-03-27 09:51:12 +02:00
|
|
|
]);
|
|
|
|
} catch (exception) {
|
|
|
|
return rej('failed to fetch assets');
|
|
|
|
}
|
|
|
|
|
|
|
|
const summaryDOM = JSDOM.fragment(activityStreams.summary);
|
|
|
|
|
|
|
|
// Create user
|
|
|
|
user = await User.insert({
|
2018-03-29 07:48:47 +02:00
|
|
|
avatarId: null,
|
|
|
|
bannerId: null,
|
|
|
|
createdAt: new Date(),
|
2018-03-27 09:51:12 +02:00
|
|
|
description: summaryDOM.textContent,
|
2018-03-29 07:48:47 +02:00
|
|
|
followersCount,
|
|
|
|
followingCount,
|
2018-03-27 09:51:12 +02:00
|
|
|
name: activityStreams.name,
|
2018-03-29 07:48:47 +02:00
|
|
|
postsCount,
|
2018-03-29 14:36:27 +02:00
|
|
|
driveCapacity: 1024 * 1024 * 8, // 8MiB
|
2018-03-29 14:44:39 +02:00
|
|
|
username,
|
2018-03-29 07:48:47 +02:00
|
|
|
usernameLower,
|
2018-03-27 09:51:12 +02:00
|
|
|
host: toUnicode(finger.subject.replace(/^.*?@/, '')),
|
2018-03-29 07:48:47 +02:00
|
|
|
hostLower,
|
2018-03-27 09:51:12 +02:00
|
|
|
account: {
|
|
|
|
uri: activityStreams.id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const [icon, image] = await Promise.all([
|
|
|
|
activityStreams.icon,
|
|
|
|
activityStreams.image,
|
|
|
|
].map(async image => {
|
|
|
|
if (!image || image.type !== 'Image') {
|
|
|
|
return { _id: null };
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await uploadFromUrl(image.url, user);
|
|
|
|
} catch (exception) {
|
|
|
|
return { _id: null };
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
User.update({ _id: user._id }, {
|
|
|
|
$set: {
|
2018-03-29 07:48:47 +02:00
|
|
|
avatarId: icon._id,
|
|
|
|
bannerId: image._id,
|
2018-03-27 09:51:12 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
user.avatarId = icon._id;
|
|
|
|
user.bannerId = icon._id;
|
2017-02-22 05:08:33 +01:00
|
|
|
}
|
2018-03-27 09:51:12 +02:00
|
|
|
} else {
|
|
|
|
const q = userId !== undefined
|
|
|
|
? { _id: userId }
|
2018-03-29 07:48:47 +02:00
|
|
|
: { usernameLower: username.toLowerCase(), host: null };
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-03-27 09:51:12 +02:00
|
|
|
user = await findUser(q);
|
|
|
|
|
|
|
|
if (user === null) {
|
|
|
|
return rej('user not found');
|
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send response
|
2018-02-02 00:21:30 +01:00
|
|
|
res(await pack(user, me, {
|
2016-12-28 23:49:51 +01:00
|
|
|
detail: true
|
|
|
|
}));
|
|
|
|
});
|