2016-12-28 15:49:51 -07:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-08 11:50:09 -07:00
|
|
|
import $ from 'cafy';
|
2018-03-27 01:51:12 -06:00
|
|
|
import { JSDOM } from 'jsdom';
|
|
|
|
import { toUnicode, toASCII } from 'punycode';
|
|
|
|
import uploadFromUrl from '../../common/drive/upload_from_url';
|
|
|
|
import User, { pack, validateUsername, isValidName, isValidDescription } from '../../models/user';
|
|
|
|
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 15:49:51 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a user
|
|
|
|
*
|
2017-03-01 01:37:01 -07:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} me
|
|
|
|
* @return {Promise<any>}
|
2016-12-28 15:49:51 -07:00
|
|
|
*/
|
2017-03-03 12:28:38 -07:00
|
|
|
module.exports = (params, me) => new Promise(async (res, rej) => {
|
2018-03-27 01:51:12 -06:00
|
|
|
let user;
|
|
|
|
|
2016-12-28 15:49:51 -07:00
|
|
|
// Get 'user_id' parameter
|
2017-03-08 11:50:09 -07:00
|
|
|
const [userId, userIdErr] = $(params.user_id).optional.id().$;
|
2017-03-02 15:47:14 -07:00
|
|
|
if (userIdErr) return rej('invalid user_id param');
|
2016-12-28 15:49:51 -07:00
|
|
|
|
|
|
|
// Get 'username' parameter
|
2017-03-08 11:50:09 -07:00
|
|
|
const [username, usernameErr] = $(params.username).optional.string().$;
|
2017-03-02 15:47:14 -07:00
|
|
|
if (usernameErr) return rej('invalid username param');
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2018-03-27 01:51:12 -06:00
|
|
|
// Get 'host' parameter
|
|
|
|
const [host, hostErr] = $(params.host).optional.string().$;
|
|
|
|
if (hostErr) return rej('invalid username param');
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2018-03-27 01:51:12 -06:00
|
|
|
if (userId === undefined && typeof username !== 'string') {
|
|
|
|
return rej('user_id or pair of username and host is required');
|
|
|
|
}
|
2017-02-21 21:08:33 -07:00
|
|
|
|
2016-12-28 15:49:51 -07:00
|
|
|
// Lookup user
|
2018-03-27 01:51:12 -06:00
|
|
|
if (typeof host === 'string') {
|
|
|
|
const username_lower = username.toLowerCase();
|
|
|
|
const host_lower_ascii = toASCII(host).toLowerCase();
|
|
|
|
const host_lower = toUnicode(host_lower_ascii);
|
|
|
|
|
|
|
|
user = await findUser({ username_lower, host_lower });
|
|
|
|
|
|
|
|
if (user === null) {
|
|
|
|
const acct_lower = `${username_lower}@${host_lower_ascii}`;
|
|
|
|
let activityStreams;
|
|
|
|
let finger;
|
|
|
|
let followers_count;
|
|
|
|
let following_count;
|
|
|
|
let likes_count;
|
|
|
|
let posts_count;
|
|
|
|
|
|
|
|
if (!validateUsername(username)) {
|
|
|
|
return rej('username validation failed');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
finger = await webFingerAndVerify(acct_lower, acct_lower);
|
|
|
|
} 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' &&
|
|
|
|
activityStreams.preferredUsername.toLowerCase() === username_lower &&
|
|
|
|
isValidName(activityStreams.name) &&
|
|
|
|
isValidDescription(activityStreams.summary)
|
|
|
|
)) {
|
|
|
|
return rej('failed ActivityStreams validation');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
[followers_count, following_count, likes_count, posts_count] = await Promise.all([
|
|
|
|
getCollectionCount(activityStreams.followers),
|
|
|
|
getCollectionCount(activityStreams.following),
|
|
|
|
getCollectionCount(activityStreams.liked),
|
|
|
|
getCollectionCount(activityStreams.outbox),
|
|
|
|
webFingerAndVerify(activityStreams.id, acct_lower),
|
|
|
|
]);
|
|
|
|
} catch (exception) {
|
|
|
|
return rej('failed to fetch assets');
|
|
|
|
}
|
|
|
|
|
|
|
|
const summaryDOM = JSDOM.fragment(activityStreams.summary);
|
|
|
|
|
|
|
|
// Create user
|
|
|
|
user = await User.insert({
|
|
|
|
avatar_id: null,
|
|
|
|
banner_id: null,
|
|
|
|
created_at: new Date(),
|
|
|
|
description: summaryDOM.textContent,
|
|
|
|
followers_count,
|
|
|
|
following_count,
|
|
|
|
name: activityStreams.name,
|
|
|
|
posts_count,
|
|
|
|
likes_count,
|
|
|
|
liked_count: 0,
|
|
|
|
drive_capacity: 1073741824, // 1GB
|
|
|
|
username: username,
|
|
|
|
username_lower,
|
|
|
|
host: toUnicode(finger.subject.replace(/^.*?@/, '')),
|
|
|
|
host_lower,
|
|
|
|
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: {
|
|
|
|
avatar_id: icon._id,
|
|
|
|
banner_id: image._id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
user.avatar_id = icon._id;
|
|
|
|
user.banner_id = icon._id;
|
2017-02-21 21:08:33 -07:00
|
|
|
}
|
2018-03-27 01:51:12 -06:00
|
|
|
} else {
|
|
|
|
const q = userId !== undefined
|
|
|
|
? { _id: userId }
|
|
|
|
: { username_lower: username.toLowerCase(), host: null };
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2018-03-27 01:51:12 -06:00
|
|
|
user = await findUser(q);
|
|
|
|
|
|
|
|
if (user === null) {
|
|
|
|
return rej('user not found');
|
|
|
|
}
|
2016-12-28 15:49:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send response
|
2018-02-01 16:21:30 -07:00
|
|
|
res(await pack(user, me, {
|
2016-12-28 15:49:51 -07:00
|
|
|
detail: true
|
|
|
|
}));
|
|
|
|
});
|