Implement remote status retrieval
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import { validateFileName, pack } from '../../../../../models/drive-file';
|
||||
import create from '../../../common/drive/add-file';
|
||||
import create from '../../../../../common/drive/add-file';
|
||||
|
||||
/**
|
||||
* Create a file
|
||||
|
@ -4,7 +4,7 @@
|
||||
import $ from 'cafy';
|
||||
import DriveFolder from '../../../../../models/drive-folder';
|
||||
import DriveFile, { validateFileName, pack } from '../../../../../models/drive-file';
|
||||
import { publishDriveStream } from '../../../event';
|
||||
import { publishDriveStream } from '../../../../../common/event';
|
||||
|
||||
/**
|
||||
* Update a file
|
||||
|
@ -3,7 +3,7 @@
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import { pack } from '../../../../../models/drive-file';
|
||||
import uploadFromUrl from '../../../common/drive/upload_from_url';
|
||||
import uploadFromUrl from '../../../../../common/drive/upload_from_url';
|
||||
|
||||
/**
|
||||
* Create a file from a URL
|
||||
|
@ -3,7 +3,7 @@
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import DriveFolder, { isValidFolderName, pack } from '../../../../../models/drive-folder';
|
||||
import { publishDriveStream } from '../../../event';
|
||||
import { publishDriveStream } from '../../../../../common/event';
|
||||
|
||||
/**
|
||||
* Create drive folder
|
||||
|
@ -3,7 +3,7 @@
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import DriveFolder, { isValidFolderName, pack } from '../../../../../models/drive-folder';
|
||||
import { publishDriveStream } from '../../../event';
|
||||
import { publishDriveStream } from '../../../../../common/event';
|
||||
|
||||
/**
|
||||
* Update a folder
|
||||
|
@ -5,7 +5,7 @@ import $ from 'cafy';
|
||||
import User, { pack as packUser } from '../../../../models/user';
|
||||
import Following from '../../../../models/following';
|
||||
import notify from '../../common/notify';
|
||||
import event from '../../event';
|
||||
import event from '../../../../common/event';
|
||||
|
||||
/**
|
||||
* Follow a user
|
||||
|
@ -4,7 +4,7 @@
|
||||
import $ from 'cafy';
|
||||
import User, { pack as packUser } from '../../../../models/user';
|
||||
import Following from '../../../../models/following';
|
||||
import event from '../../event';
|
||||
import event from '../../../../common/event';
|
||||
|
||||
/**
|
||||
* Unfollow a user
|
||||
|
@ -4,7 +4,7 @@
|
||||
import $ from 'cafy';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import User from '../../../../models/user';
|
||||
import event from '../../event';
|
||||
import event from '../../../../common/event';
|
||||
import generateUserToken from '../../common/generate-native-user-token';
|
||||
|
||||
/**
|
||||
|
@ -3,7 +3,7 @@
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import User, { isValidName, isValidDescription, isValidLocation, isValidBirthday, pack } from '../../../../models/user';
|
||||
import event from '../../event';
|
||||
import event from '../../../../common/event';
|
||||
import config from '../../../../conf';
|
||||
|
||||
/**
|
||||
|
@ -3,7 +3,7 @@
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import User, { pack } from '../../../../models/user';
|
||||
import event from '../../event';
|
||||
import event from '../../../../common/event';
|
||||
|
||||
/**
|
||||
* Update myself
|
||||
|
@ -3,7 +3,7 @@
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import User from '../../../../models/user';
|
||||
import event from '../../event';
|
||||
import event from '../../../../common/event';
|
||||
|
||||
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'home' parameter
|
||||
|
@ -3,7 +3,7 @@
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
import User from '../../../../models/user';
|
||||
import event from '../../event';
|
||||
import event from '../../../../common/event';
|
||||
|
||||
module.exports = async (params, user) => new Promise(async (res, rej) => {
|
||||
// Get 'home' parameter
|
||||
|
@ -9,8 +9,8 @@ import User from '../../../../../models/user';
|
||||
import Mute from '../../../../../models/mute';
|
||||
import DriveFile from '../../../../../models/drive-file';
|
||||
import { pack } from '../../../../../models/messaging-message';
|
||||
import publishUserStream from '../../../event';
|
||||
import { publishMessagingStream, publishMessagingIndexStream, pushSw } from '../../../event';
|
||||
import publishUserStream from '../../../../../common/event';
|
||||
import { publishMessagingStream, publishMessagingIndexStream, pushSw } from '../../../../../common/event';
|
||||
import html from '../../../../../common/text/html';
|
||||
import parse from '../../../../../common/text/parse';
|
||||
import config from '../../../../../conf';
|
||||
|
@ -2,7 +2,7 @@
|
||||
* Module dependencies
|
||||
*/
|
||||
import Notification from '../../../../models/notification';
|
||||
import event from '../../event';
|
||||
import event from '../../../../common/event';
|
||||
|
||||
/**
|
||||
* Mark as read all notifications
|
||||
|
@ -2,7 +2,7 @@ import $ from 'cafy';
|
||||
import Matching, { pack as packMatching } from '../../../../models/othello-matching';
|
||||
import OthelloGame, { pack as packGame } from '../../../../models/othello-game';
|
||||
import User from '../../../../models/user';
|
||||
import publishUserStream, { publishOthelloStream } from '../../event';
|
||||
import publishUserStream, { publishOthelloStream } from '../../../../common/event';
|
||||
import { eighteight } from '../../../../common/othello/maps';
|
||||
|
||||
module.exports = (params, user) => new Promise(async (res, rej) => {
|
||||
|
@ -16,7 +16,7 @@ import ChannelWatching from '../../../../models/channel-watching';
|
||||
import { pack } from '../../../../models/post';
|
||||
import notify from '../../common/notify';
|
||||
import watch from '../../common/watch-post';
|
||||
import event, { pushSw, publishChannelStream } from '../../event';
|
||||
import event, { pushSw, publishChannelStream } from '../../../../common/event';
|
||||
import getAcct from '../../../../common/user/get-acct';
|
||||
import parseAcct from '../../../../common/user/parse-acct';
|
||||
import config from '../../../../conf';
|
||||
|
@ -7,7 +7,7 @@ import Post from '../../../../../models/post';
|
||||
import Watching from '../../../../../models/post-watching';
|
||||
import notify from '../../../common/notify';
|
||||
import watch from '../../../common/watch-post';
|
||||
import { publishPostStream } from '../../../event';
|
||||
import { publishPostStream } from '../../../../../common/event';
|
||||
|
||||
/**
|
||||
* Vote poll of a post
|
||||
|
@ -8,7 +8,7 @@ import { pack as packUser } from '../../../../../models/user';
|
||||
import Watching from '../../../../../models/post-watching';
|
||||
import notify from '../../../common/notify';
|
||||
import watch from '../../../common/watch-post';
|
||||
import { publishPostStream, pushSw } from '../../../event';
|
||||
import { publishPostStream, pushSw } from '../../../../../common/event';
|
||||
|
||||
/**
|
||||
* React to a post
|
||||
|
@ -2,49 +2,10 @@
|
||||
* Module dependencies
|
||||
*/
|
||||
import $ from 'cafy';
|
||||
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');
|
||||
import User, { pack } from '../../../../models/user';
|
||||
import resolveRemoteUser from '../../../../common/remote/resolve-user';
|
||||
|
||||
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);
|
||||
}));
|
||||
}
|
||||
const cursorOption = { fields: { data: false } };
|
||||
|
||||
/**
|
||||
* Show a user
|
||||
@ -74,124 +35,17 @@ module.exports = (params, me) => new Promise(async (res, rej) => {
|
||||
|
||||
// Lookup user
|
||||
if (typeof host === 'string') {
|
||||
const usernameLower = username.toLowerCase();
|
||||
const hostLowerAscii = toASCII(host).toLowerCase();
|
||||
const hostLower = toUnicode(hostLowerAscii);
|
||||
|
||||
user = await findUser({ usernameLower, hostLower });
|
||||
|
||||
if (user === null) {
|
||||
const acctLower = `${usernameLower}@${hostLowerAscii}`;
|
||||
let activityStreams;
|
||||
let finger;
|
||||
let followersCount;
|
||||
let followingCount;
|
||||
let postsCount;
|
||||
|
||||
if (!validateUsername(username)) {
|
||||
return rej('username validation failed');
|
||||
}
|
||||
|
||||
try {
|
||||
finger = await webFingerAndVerify(acctLower, acctLower);
|
||||
} 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() === usernameLower &&
|
||||
isValidName(activityStreams.name) &&
|
||||
isValidDescription(activityStreams.summary)
|
||||
)) {
|
||||
return rej('failed ActivityStreams validation');
|
||||
}
|
||||
|
||||
try {
|
||||
[followersCount, followingCount, postsCount] = await Promise.all([
|
||||
getCollectionCount(activityStreams.followers),
|
||||
getCollectionCount(activityStreams.following),
|
||||
getCollectionCount(activityStreams.outbox),
|
||||
webFingerAndVerify(activityStreams.id, acctLower),
|
||||
]);
|
||||
} catch (exception) {
|
||||
return rej('failed to fetch assets');
|
||||
}
|
||||
|
||||
const summaryDOM = JSDOM.fragment(activityStreams.summary);
|
||||
|
||||
// Create user
|
||||
user = await User.insert({
|
||||
avatarId: null,
|
||||
bannerId: null,
|
||||
createdAt: new Date(),
|
||||
description: summaryDOM.textContent,
|
||||
followersCount,
|
||||
followingCount,
|
||||
name: activityStreams.name,
|
||||
postsCount,
|
||||
driveCapacity: 1024 * 1024 * 8, // 8MiB
|
||||
username,
|
||||
usernameLower,
|
||||
host: toUnicode(finger.subject.replace(/^.*?@/, '')),
|
||||
hostLower,
|
||||
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: {
|
||||
avatarId: icon._id,
|
||||
bannerId: image._id,
|
||||
},
|
||||
});
|
||||
|
||||
user.avatarId = icon._id;
|
||||
user.bannerId = icon._id;
|
||||
try {
|
||||
user = await resolveRemoteUser(username, host, cursorOption);
|
||||
} catch (exception) {
|
||||
return rej('failed to resolve remote user');
|
||||
}
|
||||
} else {
|
||||
const q = userId !== undefined
|
||||
? { _id: userId }
|
||||
: { usernameLower: username.toLowerCase(), host: null };
|
||||
|
||||
user = await findUser(q);
|
||||
user = await User.findOne(q, cursorOption);
|
||||
|
||||
if (user === null) {
|
||||
return rej('user not found');
|
||||
|
Reference in New Issue
Block a user