Refactor
This commit is contained in:
5
src/remote/activitypub/renderer/context.ts
Normal file
5
src/remote/activitypub/renderer/context.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export default [
|
||||
'https://www.w3.org/ns/activitystreams',
|
||||
'https://w3id.org/security/v1',
|
||||
{ Hashtag: 'as:Hashtag' }
|
||||
];
|
7
src/remote/activitypub/renderer/document.ts
Normal file
7
src/remote/activitypub/renderer/document.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import config from '../../../conf';
|
||||
|
||||
export default ({ _id, contentType }) => ({
|
||||
type: 'Document',
|
||||
mediaType: contentType,
|
||||
url: `${config.drive_url}/${_id}`
|
||||
});
|
8
src/remote/activitypub/renderer/follow.ts
Normal file
8
src/remote/activitypub/renderer/follow.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import config from '../../../conf';
|
||||
import { IRemoteUser } from '../../../models/user';
|
||||
|
||||
export default ({ username }, followee: IRemoteUser) => ({
|
||||
type: 'Follow',
|
||||
actor: `${config.url}/@${username}`,
|
||||
object: followee.account.uri
|
||||
});
|
7
src/remote/activitypub/renderer/hashtag.ts
Normal file
7
src/remote/activitypub/renderer/hashtag.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import config from '../../../conf';
|
||||
|
||||
export default tag => ({
|
||||
type: 'Hashtag',
|
||||
href: `${config.url}/search?q=#${encodeURIComponent(tag)}`,
|
||||
name: '#' + tag
|
||||
});
|
6
src/remote/activitypub/renderer/image.ts
Normal file
6
src/remote/activitypub/renderer/image.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import config from '../../../conf';
|
||||
|
||||
export default ({ _id }) => ({
|
||||
type: 'Image',
|
||||
url: `${config.drive_url}/${_id}`
|
||||
});
|
10
src/remote/activitypub/renderer/key.ts
Normal file
10
src/remote/activitypub/renderer/key.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import config from '../../../conf';
|
||||
import { extractPublic } from '../../../crypto_key';
|
||||
import { ILocalUser } from '../../../models/user';
|
||||
|
||||
export default (user: ILocalUser) => ({
|
||||
id: `${config.url}/@${user.username}/publickey`,
|
||||
type: 'Key',
|
||||
owner: `${config.url}/@${user.username}`,
|
||||
publicKeyPem: extractPublic(user.account.keypair)
|
||||
});
|
44
src/remote/activitypub/renderer/note.ts
Normal file
44
src/remote/activitypub/renderer/note.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import renderDocument from './document';
|
||||
import renderHashtag from './hashtag';
|
||||
import config from '../../../conf';
|
||||
import DriveFile from '../../../models/drive-file';
|
||||
import Post from '../../../models/post';
|
||||
import User from '../../../models/user';
|
||||
|
||||
export default async (user, post) => {
|
||||
const promisedFiles = DriveFile.find({ _id: { $in: post.mediaIds } });
|
||||
let inReplyTo;
|
||||
|
||||
if (post.replyId) {
|
||||
const inReplyToPost = await Post.findOne({
|
||||
_id: post.replyId,
|
||||
});
|
||||
|
||||
if (inReplyToPost !== null) {
|
||||
const inReplyToUser = await User.findOne({
|
||||
_id: post.userId,
|
||||
});
|
||||
|
||||
if (inReplyToUser !== null) {
|
||||
inReplyTo = `${config.url}@${inReplyToUser.username}/${inReplyToPost._id}`;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
inReplyTo = null;
|
||||
}
|
||||
|
||||
const attributedTo = `${config.url}/@${user.username}`;
|
||||
|
||||
return {
|
||||
id: `${attributedTo}/${post._id}`,
|
||||
type: 'Note',
|
||||
attributedTo,
|
||||
content: post.textHtml,
|
||||
published: post.createdAt.toISOString(),
|
||||
to: 'https://www.w3.org/ns/activitystreams#Public',
|
||||
cc: `${attributedTo}/followers`,
|
||||
inReplyTo,
|
||||
attachment: (await promisedFiles).map(renderDocument),
|
||||
tag: post.tags.map(renderHashtag)
|
||||
};
|
||||
};
|
6
src/remote/activitypub/renderer/ordered-collection.ts
Normal file
6
src/remote/activitypub/renderer/ordered-collection.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export default (id, totalItems, orderedItems) => ({
|
||||
id,
|
||||
type: 'OrderedCollection',
|
||||
totalItems,
|
||||
orderedItems
|
||||
});
|
20
src/remote/activitypub/renderer/person.ts
Normal file
20
src/remote/activitypub/renderer/person.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import renderImage from './image';
|
||||
import renderKey from './key';
|
||||
import config from '../../../conf';
|
||||
|
||||
export default user => {
|
||||
const id = `${config.url}/@${user.username}`;
|
||||
|
||||
return {
|
||||
type: 'Person',
|
||||
id,
|
||||
inbox: `${id}/inbox`,
|
||||
outbox: `${id}/outbox`,
|
||||
preferredUsername: user.username,
|
||||
name: user.name,
|
||||
summary: user.description,
|
||||
icon: user.avatarId && renderImage({ _id: user.avatarId }),
|
||||
image: user.bannerId && renderImage({ _id: user.bannerId }),
|
||||
publicKey: renderKey(user)
|
||||
};
|
||||
};
|
Reference in New Issue
Block a user