Merge pull request #1666 from LemmyNet/resolve-endpoint

Move resolving of activitypub objects to separate api endpoint
reindex_non_concurrently_on_startup 0.11.4-rc.16
Nutomic 2021-08-23 17:06:51 +00:00 committed by GitHub
commit e413350be3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 339 additions and 266 deletions

4
Cargo.lock generated
View File

@ -1736,10 +1736,13 @@ name = "lemmy_apub_lib"
version = "0.11.3" version = "0.11.3"
dependencies = [ dependencies = [
"activitystreams", "activitystreams",
"anyhow",
"async-trait", "async-trait",
"lemmy_apub_lib_derive", "lemmy_apub_lib_derive",
"lemmy_utils", "lemmy_utils",
"lemmy_websocket", "lemmy_websocket",
"log",
"reqwest",
"serde", "serde",
"serde_json", "serde_json",
"url", "url",
@ -1836,6 +1839,7 @@ dependencies = [
"diesel", "diesel",
"lazy_static", "lazy_static",
"lemmy_api_common", "lemmy_api_common",
"lemmy_apub_lib",
"lemmy_db_queries", "lemmy_db_queries",
"lemmy_db_schema", "lemmy_db_schema",
"lemmy_db_views", "lemmy_db_views",

View File

@ -16,7 +16,7 @@
"eslint": "^7.30.0", "eslint": "^7.30.0",
"eslint-plugin-jane": "^9.0.3", "eslint-plugin-jane": "^9.0.3",
"jest": "^27.0.6", "jest": "^27.0.6",
"lemmy-js-client": "0.11.4-rc.9", "lemmy-js-client": "0.11.4-rc.14",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"prettier": "^2.3.2", "prettier": "^2.3.2",
"ts-jest": "^27.0.3", "ts-jest": "^27.0.3",

View File

@ -6,16 +6,16 @@ import {
setupLogins, setupLogins,
createPost, createPost,
getPost, getPost,
searchComment, resolveComment,
likeComment, likeComment,
followBeta, followBeta,
searchForBetaCommunity, resolveBetaCommunity,
createComment, createComment,
editComment, editComment,
deleteComment, deleteComment,
removeComment, removeComment,
getMentions, getMentions,
searchPost, resolvePost,
unfollowRemotes, unfollowRemotes,
createCommunity, createCommunity,
registerUser, registerUser,
@ -31,10 +31,10 @@ beforeAll(async () => {
await setupLogins(); await setupLogins();
await followBeta(alpha); await followBeta(alpha);
await followBeta(gamma); await followBeta(gamma);
let search = await searchForBetaCommunity(alpha); let betaCommunity = (await resolveBetaCommunity(alpha)).community;
postRes = await createPost( postRes = await createPost(
alpha, alpha,
search.communities.find(c => c.community.local == false).community.id betaCommunity.community.id
); );
}); });
@ -65,8 +65,7 @@ test('Create a comment', async () => {
expect(commentRes.comment_view.counts.score).toBe(1); expect(commentRes.comment_view.counts.score).toBe(1);
// Make sure that comment is liked on beta // Make sure that comment is liked on beta
let searchBeta = await searchComment(beta, commentRes.comment_view.comment); let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment;
let betaComment = searchBeta.comments[0];
expect(betaComment).toBeDefined(); expect(betaComment).toBeDefined();
expect(betaComment.community.local).toBe(true); expect(betaComment.community.local).toBe(true);
expect(betaComment.creator.local).toBe(false); expect(betaComment.creator.local).toBe(false);
@ -82,8 +81,8 @@ test('Create a comment in a non-existent post', async () => {
test('Update a comment', async () => { test('Update a comment', async () => {
let commentRes = await createComment(alpha, postRes.post_view.post.id); let commentRes = await createComment(alpha, postRes.post_view.post.id);
// Federate the comment first // Federate the comment first
let searchBeta = await searchComment(beta, commentRes.comment_view.comment); let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment;
assertCommentFederation(searchBeta.comments[0], commentRes.comment_view); assertCommentFederation(betaComment, commentRes.comment_view);
let updateCommentRes = await editComment( let updateCommentRes = await editComment(
alpha, alpha,
@ -96,12 +95,12 @@ test('Update a comment', async () => {
expect(updateCommentRes.comment_view.creator.local).toBe(true); expect(updateCommentRes.comment_view.creator.local).toBe(true);
// Make sure that post is updated on beta // Make sure that post is updated on beta
let searchBetaUpdated = await searchComment( let betaCommentUpdated = (await resolveComment(
beta, beta,
commentRes.comment_view.comment commentRes.comment_view.comment
); )).comment;
assertCommentFederation( assertCommentFederation(
searchBetaUpdated.comments[0], betaCommentUpdated,
updateCommentRes.comment_view updateCommentRes.comment_view
); );
}); });
@ -118,9 +117,8 @@ test('Delete a comment', async () => {
expect(deleteCommentRes.comment_view.comment.content).toBe(""); expect(deleteCommentRes.comment_view.comment.content).toBe("");
// Make sure that comment is undefined on beta // Make sure that comment is undefined on beta
let searchBeta = await searchComment(beta, commentRes.comment_view.comment); let betaCommentRes: any = await resolveComment(beta, commentRes.comment_view.comment);
let betaComment = searchBeta.comments[0]; expect(betaCommentRes).toStrictEqual({ error: 'couldnt_find_object' });
expect(betaComment).toBeUndefined();
let undeleteCommentRes = await deleteComment( let undeleteCommentRes = await deleteComment(
alpha, alpha,
@ -130,11 +128,10 @@ test('Delete a comment', async () => {
expect(undeleteCommentRes.comment_view.comment.deleted).toBe(false); expect(undeleteCommentRes.comment_view.comment.deleted).toBe(false);
// Make sure that comment is undeleted on beta // Make sure that comment is undeleted on beta
let searchBeta2 = await searchComment(beta, commentRes.comment_view.comment); let betaComment2 = (await resolveComment(beta, commentRes.comment_view.comment)).comment;
let betaComment2 = searchBeta2.comments[0];
expect(betaComment2.comment.deleted).toBe(false); expect(betaComment2.comment.deleted).toBe(false);
assertCommentFederation( assertCommentFederation(
searchBeta2.comments[0], betaComment2,
undeleteCommentRes.comment_view undeleteCommentRes.comment_view
); );
}); });
@ -144,8 +141,8 @@ test('Remove a comment from admin and community on the same instance', async ()
// Get the id for beta // Get the id for beta
let betaCommentId = ( let betaCommentId = (
await searchComment(beta, commentRes.comment_view.comment) await resolveComment(beta, commentRes.comment_view.comment)
).comments[0].comment.id; ).comment.comment.id;
// The beta admin removes it (the community lives on beta) // The beta admin removes it (the community lives on beta)
let removeCommentRes = await removeComment(beta, true, betaCommentId); let removeCommentRes = await removeComment(beta, true, betaCommentId);
@ -185,8 +182,7 @@ test('Remove a comment from admin and community on different instance', async ()
expect(commentRes.comment_view.comment.content).toBeDefined(); expect(commentRes.comment_view.comment.content).toBeDefined();
// Beta searches that to cache it, then removes it // Beta searches that to cache it, then removes it
let searchBeta = await searchComment(beta, commentRes.comment_view.comment); let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment;
let betaComment = searchBeta.comments[0];
let removeCommentRes = await removeComment( let removeCommentRes = await removeComment(
beta, beta,
true, true,
@ -206,8 +202,7 @@ test('Unlike a comment', async () => {
expect(unlike.comment_view.counts.score).toBe(0); expect(unlike.comment_view.counts.score).toBe(0);
// Make sure that post is unliked on beta // Make sure that post is unliked on beta
let searchBeta = await searchComment(beta, commentRes.comment_view.comment); let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment;
let betaComment = searchBeta.comments[0];
expect(betaComment).toBeDefined(); expect(betaComment).toBeDefined();
expect(betaComment.community.local).toBe(true); expect(betaComment.community.local).toBe(true);
expect(betaComment.creator.local).toBe(false); expect(betaComment.creator.local).toBe(false);
@ -218,8 +213,7 @@ test('Federated comment like', async () => {
let commentRes = await createComment(alpha, postRes.post_view.post.id); let commentRes = await createComment(alpha, postRes.post_view.post.id);
// Find the comment on beta // Find the comment on beta
let searchBeta = await searchComment(beta, commentRes.comment_view.comment); let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment;
let betaComment = searchBeta.comments[0];
let like = await likeComment(beta, 1, betaComment.comment); let like = await likeComment(beta, 1, betaComment.comment);
expect(like.comment_view.counts.score).toBe(2); expect(like.comment_view.counts.score).toBe(2);
@ -232,8 +226,7 @@ test('Federated comment like', async () => {
test('Reply to a comment', async () => { test('Reply to a comment', async () => {
// Create a comment on alpha, find it on beta // Create a comment on alpha, find it on beta
let commentRes = await createComment(alpha, postRes.post_view.post.id); let commentRes = await createComment(alpha, postRes.post_view.post.id);
let searchBeta = await searchComment(beta, commentRes.comment_view.comment); let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment;
let betaComment = searchBeta.comments[0];
// find that comment id on beta // find that comment id on beta
@ -287,8 +280,8 @@ test('Mention beta', async () => {
test('Comment Search', async () => { test('Comment Search', async () => {
let commentRes = await createComment(alpha, postRes.post_view.post.id); let commentRes = await createComment(alpha, postRes.post_view.post.id);
let searchBeta = await searchComment(beta, commentRes.comment_view.comment); let betaComment = (await resolveComment(beta, commentRes.comment_view.comment)).comment;
assertCommentFederation(searchBeta.comments[0], commentRes.comment_view); assertCommentFederation(betaComment, commentRes.comment_view);
}); });
test('A and G subscribe to B (center) A posts, G mentions B, it gets announced to A', async () => { test('A and G subscribe to B (center) A posts, G mentions B, it gets announced to A', async () => {
@ -297,8 +290,7 @@ test('A and G subscribe to B (center) A posts, G mentions B, it gets announced t
expect(alphaPost.post_view.community.local).toBe(true); expect(alphaPost.post_view.community.local).toBe(true);
// Make sure gamma sees it // Make sure gamma sees it
let search = await searchPost(gamma, alphaPost.post_view.post); let gammaPost = (await resolvePost(gamma, alphaPost.post_view.post)).post;
let gammaPost = search.posts[0];
let commentContent = let commentContent =
'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551'; 'A jest test federated comment announce, lets mention @lemmy_beta@lemmy-beta:8551';
@ -379,8 +371,7 @@ test('Fetch in_reply_tos: A is unsubbed from B, B makes a post, and some embedde
expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent); expect(updateRes.comment_view.comment.content).toBe(updatedCommentContent);
// Get the post from alpha // Get the post from alpha
let search = await searchPost(alpha, postRes.post_view.post); let alphaPostB = (await resolvePost(alpha, postRes.post_view.post)).post;
let alphaPostB = search.posts[0];
let alphaPost = await getPost(alpha, alphaPostB.post.id); let alphaPost = await getPost(alpha, alphaPostB.post.id);
expect(alphaPost.post_view.post.name).toBeDefined(); expect(alphaPost.post_view.post.name).toBeDefined();

View File

@ -3,7 +3,7 @@ import {
alpha, alpha,
beta, beta,
setupLogins, setupLogins,
searchForCommunity, resolveCommunity,
createCommunity, createCommunity,
deleteCommunity, deleteCommunity,
removeCommunity, removeCommunity,
@ -47,9 +47,8 @@ test('Create community', async () => {
// Cache the community on beta, make sure it has the other fields // Cache the community on beta, make sure it has the other fields
let searchShort = `!${prevName}@lemmy-alpha:8541`; let searchShort = `!${prevName}@lemmy-alpha:8541`;
let search = await searchForCommunity(beta, searchShort); let betaCommunity = (await resolveCommunity(beta, searchShort)).community;
let communityOnBeta = search.communities[0]; assertCommunityFederation(betaCommunity, communityRes.community_view);
assertCommunityFederation(communityOnBeta, communityRes.community_view);
}); });
test('Delete community', async () => { test('Delete community', async () => {
@ -57,15 +56,14 @@ test('Delete community', async () => {
// Cache the community on Alpha // Cache the community on Alpha
let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`; let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
let search = await searchForCommunity(alpha, searchShort); let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community;
let communityOnAlpha = search.communities[0]; assertCommunityFederation(alphaCommunity, communityRes.community_view);
assertCommunityFederation(communityOnAlpha, communityRes.community_view);
// Follow the community from alpha // Follow the community from alpha
let follow = await followCommunity( let follow = await followCommunity(
alpha, alpha,
true, true,
communityOnAlpha.community.id alphaCommunity.community.id
); );
// Make sure the follow response went through // Make sure the follow response went through
@ -82,7 +80,7 @@ test('Delete community', async () => {
// Make sure it got deleted on A // Make sure it got deleted on A
let communityOnAlphaDeleted = await getCommunity( let communityOnAlphaDeleted = await getCommunity(
alpha, alpha,
communityOnAlpha.community.id alphaCommunity.community.id
); );
expect(communityOnAlphaDeleted.community_view.community.deleted).toBe(true); expect(communityOnAlphaDeleted.community_view.community.deleted).toBe(true);
@ -97,7 +95,7 @@ test('Delete community', async () => {
// Make sure it got undeleted on A // Make sure it got undeleted on A
let communityOnAlphaUnDeleted = await getCommunity( let communityOnAlphaUnDeleted = await getCommunity(
alpha, alpha,
communityOnAlpha.community.id alphaCommunity.community.id
); );
expect(communityOnAlphaUnDeleted.community_view.community.deleted).toBe( expect(communityOnAlphaUnDeleted.community_view.community.deleted).toBe(
false false
@ -109,15 +107,14 @@ test('Remove community', async () => {
// Cache the community on Alpha // Cache the community on Alpha
let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`; let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
let search = await searchForCommunity(alpha, searchShort); let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community;
let communityOnAlpha = search.communities[0]; assertCommunityFederation(alphaCommunity, communityRes.community_view);
assertCommunityFederation(communityOnAlpha, communityRes.community_view);
// Follow the community from alpha // Follow the community from alpha
let follow = await followCommunity( let follow = await followCommunity(
alpha, alpha,
true, true,
communityOnAlpha.community.id alphaCommunity.community.id
); );
// Make sure the follow response went through // Make sure the follow response went through
@ -134,7 +131,7 @@ test('Remove community', async () => {
// Make sure it got Removed on A // Make sure it got Removed on A
let communityOnAlphaRemoved = await getCommunity( let communityOnAlphaRemoved = await getCommunity(
alpha, alpha,
communityOnAlpha.community.id alphaCommunity.community.id
); );
expect(communityOnAlphaRemoved.community_view.community.removed).toBe(true); expect(communityOnAlphaRemoved.community_view.community.removed).toBe(true);
@ -149,7 +146,7 @@ test('Remove community', async () => {
// Make sure it got undeleted on A // Make sure it got undeleted on A
let communityOnAlphaUnRemoved = await getCommunity( let communityOnAlphaUnRemoved = await getCommunity(
alpha, alpha,
communityOnAlpha.community.id alphaCommunity.community.id
); );
expect(communityOnAlphaUnRemoved.community_view.community.removed).toBe( expect(communityOnAlphaUnRemoved.community_view.community.removed).toBe(
false false
@ -161,7 +158,6 @@ test('Search for beta community', async () => {
expect(communityRes.community_view.community.name).toBeDefined(); expect(communityRes.community_view.community.name).toBeDefined();
let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`; let searchShort = `!${communityRes.community_view.community.name}@lemmy-beta:8551`;
let search = await searchForCommunity(alpha, searchShort); let alphaCommunity = (await resolveCommunity(alpha, searchShort)).community;
let communityOnAlpha = search.communities[0]; assertCommunityFederation(alphaCommunity, communityRes.community_view);
assertCommunityFederation(communityOnAlpha, communityRes.community_view);
}); });

View File

@ -2,7 +2,7 @@ jest.setTimeout(120000);
import { import {
alpha, alpha,
setupLogins, setupLogins,
searchForBetaCommunity, resolveBetaCommunity,
followCommunity, followCommunity,
unfollowRemotes, unfollowRemotes,
getSite, getSite,
@ -17,11 +17,11 @@ afterAll(async () => {
}); });
test('Follow federated community', async () => { test('Follow federated community', async () => {
let search = await searchForBetaCommunity(alpha); // TODO sometimes this is returning null? let betaCommunity = (await resolveBetaCommunity(alpha)).community;
let follow = await followCommunity( let follow = await followCommunity(
alpha, alpha,
true, true,
search.communities[0].community.id betaCommunity.community.id
); );
// Make sure the follow response went through // Make sure the follow response went through

View File

@ -10,16 +10,16 @@ import {
editPost, editPost,
stickyPost, stickyPost,
lockPost, lockPost,
searchPost, resolvePost,
likePost, likePost,
followBeta, followBeta,
searchForBetaCommunity, resolveBetaCommunity,
createComment, createComment,
deletePost, deletePost,
removePost, removePost,
getPost, getPost,
unfollowRemotes, unfollowRemotes,
searchForUser, resolvePerson,
banPersonFromSite, banPersonFromSite,
searchPostLocal, searchPostLocal,
followCommunity, followCommunity,
@ -31,8 +31,8 @@ let betaCommunity: CommunityView;
beforeAll(async () => { beforeAll(async () => {
await setupLogins(); await setupLogins();
let search = await searchForBetaCommunity(alpha); betaCommunity = (await resolveBetaCommunity(alpha)).community;
betaCommunity = search.communities[0]; expect(betaCommunity).toBeDefined();
await unfollows(); await unfollows();
}); });
@ -71,8 +71,7 @@ test('Create a post', async () => {
expect(postRes.post_view.counts.score).toBe(1); expect(postRes.post_view.counts.score).toBe(1);
// Make sure that post is liked on beta // Make sure that post is liked on beta
let searchBeta = await searchPost(beta, postRes.post_view.post); let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
let betaPost = searchBeta.posts[0];
expect(betaPost).toBeDefined(); expect(betaPost).toBeDefined();
expect(betaPost.community.local).toBe(true); expect(betaPost.community.local).toBe(true);
@ -81,12 +80,12 @@ test('Create a post', async () => {
assertPostFederation(betaPost, postRes.post_view); assertPostFederation(betaPost, postRes.post_view);
// Delta only follows beta, so it should not see an alpha ap_id // Delta only follows beta, so it should not see an alpha ap_id
let searchDelta = await searchPost(delta, postRes.post_view.post); let deltaPost = (await resolvePost(delta, postRes.post_view.post)).post;
expect(searchDelta.posts[0]).toBeUndefined(); expect(deltaPost).toBeUndefined();
// Epsilon has alpha blocked, it should not see the alpha post // Epsilon has alpha blocked, it should not see the alpha post
let searchEpsilon = await searchPost(epsilon, postRes.post_view.post); let epsilonPost = (await resolvePost(epsilon, postRes.post_view.post)).post;
expect(searchEpsilon.posts[0]).toBeUndefined(); expect(epsilonPost).toBeUndefined();
}); });
test('Create a post in a non-existent community', async () => { test('Create a post in a non-existent community', async () => {
@ -104,8 +103,7 @@ test('Unlike a post', async () => {
expect(unlike2.post_view.counts.score).toBe(0); expect(unlike2.post_view.counts.score).toBe(0);
// Make sure that post is unliked on beta // Make sure that post is unliked on beta
let searchBeta = await searchPost(beta, postRes.post_view.post); let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
let betaPost = searchBeta.posts[0];
expect(betaPost).toBeDefined(); expect(betaPost).toBeDefined();
expect(betaPost.community.local).toBe(true); expect(betaPost.community.local).toBe(true);
expect(betaPost.creator.local).toBe(false); expect(betaPost.creator.local).toBe(false);
@ -123,8 +121,7 @@ test('Update a post', async () => {
expect(updatedPost.post_view.creator.local).toBe(true); expect(updatedPost.post_view.creator.local).toBe(true);
// Make sure that post is updated on beta // Make sure that post is updated on beta
let searchBeta = await searchPost(beta, postRes.post_view.post); let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
let betaPost = searchBeta.posts[0];
expect(betaPost.community.local).toBe(true); expect(betaPost.community.local).toBe(true);
expect(betaPost.creator.local).toBe(false); expect(betaPost.creator.local).toBe(false);
expect(betaPost.post.name).toBe(updatedName); expect(betaPost.post.name).toBe(updatedName);
@ -142,8 +139,7 @@ test('Sticky a post', async () => {
expect(stickiedPostRes.post_view.post.stickied).toBe(true); expect(stickiedPostRes.post_view.post.stickied).toBe(true);
// Make sure that post is stickied on beta // Make sure that post is stickied on beta
let searchBeta = await searchPost(beta, postRes.post_view.post); let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
let betaPost = searchBeta.posts[0];
expect(betaPost.community.local).toBe(true); expect(betaPost.community.local).toBe(true);
expect(betaPost.creator.local).toBe(false); expect(betaPost.creator.local).toBe(false);
expect(betaPost.post.stickied).toBe(true); expect(betaPost.post.stickied).toBe(true);
@ -153,18 +149,15 @@ test('Sticky a post', async () => {
expect(unstickiedPost.post_view.post.stickied).toBe(false); expect(unstickiedPost.post_view.post.stickied).toBe(false);
// Make sure that post is unstickied on beta // Make sure that post is unstickied on beta
let searchBeta2 = await searchPost(beta, postRes.post_view.post); let betaPost2 = (await resolvePost(beta, postRes.post_view.post)).post;
let betaPost2 = searchBeta2.posts[0];
expect(betaPost2.community.local).toBe(true); expect(betaPost2.community.local).toBe(true);
expect(betaPost2.creator.local).toBe(false); expect(betaPost2.creator.local).toBe(false);
expect(betaPost2.post.stickied).toBe(false); expect(betaPost2.post.stickied).toBe(false);
// Make sure that gamma cannot sticky the post on beta // Make sure that gamma cannot sticky the post on beta
let searchGamma = await searchPost(gamma, postRes.post_view.post); let gammaPost = (await resolvePost(gamma, postRes.post_view.post)).post;
let gammaPost = searchGamma.posts[0];
let gammaTrySticky = await stickyPost(gamma, true, gammaPost.post); let gammaTrySticky = await stickyPost(gamma, true, gammaPost.post);
let searchBeta3 = await searchPost(beta, postRes.post_view.post); let betaPost3 = (await resolvePost(beta, postRes.post_view.post)).post;
let betaPost3 = searchBeta3.posts[0];
expect(gammaTrySticky.post_view.post.stickied).toBe(true); expect(gammaTrySticky.post_view.post.stickied).toBe(true);
expect(betaPost3.post.stickied).toBe(false); expect(betaPost3.post.stickied).toBe(false);
}); });
@ -174,8 +167,7 @@ test('Lock a post', async () => {
let postRes = await createPost(alpha, betaCommunity.community.id); let postRes = await createPost(alpha, betaCommunity.community.id);
// Lock the post // Lock the post
let searchBeta = await searchPost(beta, postRes.post_view.post); let betaPost1 = (await resolvePost(beta, postRes.post_view.post)).post;
let betaPost1 = searchBeta.posts[0];
let lockedPostRes = await lockPost(beta, true, betaPost1.post); let lockedPostRes = await lockPost(beta, true, betaPost1.post);
expect(lockedPostRes.post_view.post.locked).toBe(true); expect(lockedPostRes.post_view.post.locked).toBe(true);
@ -213,8 +205,7 @@ test('Delete a post', async () => {
expect(deletedPost.post_view.post.name).toBe(""); expect(deletedPost.post_view.post.name).toBe("");
// Make sure lemmy beta sees post is deleted // Make sure lemmy beta sees post is deleted
let searchBeta = await searchPost(beta, postRes.post_view.post); let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
let betaPost = searchBeta.posts[0];
// This will be undefined because of the tombstone // This will be undefined because of the tombstone
expect(betaPost).toBeUndefined(); expect(betaPost).toBeUndefined();
@ -223,8 +214,7 @@ test('Delete a post', async () => {
expect(undeletedPost.post_view.post.deleted).toBe(false); expect(undeletedPost.post_view.post.deleted).toBe(false);
// Make sure lemmy beta sees post is undeleted // Make sure lemmy beta sees post is undeleted
let searchBeta2 = await searchPost(beta, postRes.post_view.post); let betaPost2 = (await resolvePost(beta, postRes.post_view.post)).post;
let betaPost2 = searchBeta2.posts[0];
expect(betaPost2.post.deleted).toBe(false); expect(betaPost2.post.deleted).toBe(false);
assertPostFederation(betaPost2, undeletedPost.post_view); assertPostFederation(betaPost2, undeletedPost.post_view);
@ -241,8 +231,7 @@ test('Remove a post from admin and community on different instance', async () =>
expect(removedPost.post_view.post.name).toBe(""); expect(removedPost.post_view.post.name).toBe("");
// Make sure lemmy beta sees post is NOT removed // Make sure lemmy beta sees post is NOT removed
let searchBeta = await searchPost(beta, postRes.post_view.post); let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
let betaPost = searchBeta.posts[0];
expect(betaPost.post.removed).toBe(false); expect(betaPost.post.removed).toBe(false);
// Undelete // Undelete
@ -250,8 +239,7 @@ test('Remove a post from admin and community on different instance', async () =>
expect(undeletedPost.post_view.post.removed).toBe(false); expect(undeletedPost.post_view.post.removed).toBe(false);
// Make sure lemmy beta sees post is undeleted // Make sure lemmy beta sees post is undeleted
let searchBeta2 = await searchPost(beta, postRes.post_view.post); let betaPost2 = (await resolvePost(beta, postRes.post_view.post)).post;
let betaPost2 = searchBeta2.posts[0];
expect(betaPost2.post.removed).toBe(false); expect(betaPost2.post.removed).toBe(false);
assertPostFederation(betaPost2, undeletedPost.post_view); assertPostFederation(betaPost2, undeletedPost.post_view);
}); });
@ -291,27 +279,26 @@ test('Search for a post', async () => {
let postRes = await createPost(alpha, betaCommunity.community.id); let postRes = await createPost(alpha, betaCommunity.community.id);
expect(postRes.post_view.post).toBeDefined(); expect(postRes.post_view.post).toBeDefined();
let searchBeta = await searchPost(beta, postRes.post_view.post); let betaPost = (await resolvePost(beta, postRes.post_view.post)).post;
expect(searchBeta.posts[0].post.name).toBeDefined(); expect(betaPost.post.name).toBeDefined();
}); });
test('A and G subscribe to B (center) A posts, it gets announced to G', async () => { test('A and G subscribe to B (center) A posts, it gets announced to G', async () => {
let postRes = await createPost(alpha, betaCommunity.community.id); let postRes = await createPost(alpha, betaCommunity.community.id);
expect(postRes.post_view.post).toBeDefined(); expect(postRes.post_view.post).toBeDefined();
let search2 = await searchPost(gamma, postRes.post_view.post); let betaPost = (await resolvePost(gamma, postRes.post_view.post)).post;
expect(search2.posts[0].post.name).toBeDefined(); expect(betaPost.post.name).toBeDefined();
}); });
test('Enforce site ban for federated user', async () => { test('Enforce site ban for federated user', async () => {
let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`; let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
let userSearch = await searchForUser(beta, alphaShortname); let alphaPerson = (await resolvePerson(beta, alphaShortname)).person;
let alphaUser = userSearch.users[0]; expect(alphaPerson).toBeDefined();
expect(alphaUser).toBeDefined();
// ban alpha from beta site // ban alpha from beta site
let banAlpha = await banPersonFromSite(beta, alphaUser.person.id, true); let banAlpha = await banPersonFromSite(beta, alphaPerson.person.id, true);
expect(banAlpha.banned).toBe(true); expect(banAlpha.banned).toBe(true);
// Alpha makes post on beta // Alpha makes post on beta
@ -327,19 +314,18 @@ test('Enforce site ban for federated user', async () => {
expect(betaPost).toBeUndefined(); expect(betaPost).toBeUndefined();
// Unban alpha // Unban alpha
let unBanAlpha = await banPersonFromSite(beta, alphaUser.person.id, false); let unBanAlpha = await banPersonFromSite(beta, alphaPerson.person.id, false);
expect(unBanAlpha.banned).toBe(false); expect(unBanAlpha.banned).toBe(false);
}); });
test('Enforce community ban for federated user', async () => { test('Enforce community ban for federated user', async () => {
let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`; let alphaShortname = `@lemmy_alpha@lemmy-alpha:8541`;
let userSearch = await searchForUser(beta, alphaShortname); let alphaPerson = (await resolvePerson(beta, alphaShortname)).person;
let alphaUser = userSearch.users[0]; expect(alphaPerson).toBeDefined();
expect(alphaUser).toBeDefined();
// ban alpha from beta site // ban alpha from beta site
await banPersonFromCommunity(beta, alphaUser.person.id, 2, false); await banPersonFromCommunity(beta, alphaPerson.person.id, 2, false);
let banAlpha = await banPersonFromCommunity(beta, alphaUser.person.id, 2, true); let banAlpha = await banPersonFromCommunity(beta, alphaPerson.person.id, 2, true);
expect(banAlpha.banned).toBe(true); expect(banAlpha.banned).toBe(true);
// Alpha tries to make post on beta, but it fails because of ban // Alpha tries to make post on beta, but it fails because of ban
@ -349,7 +335,7 @@ test('Enforce community ban for federated user', async () => {
// Unban alpha // Unban alpha
let unBanAlpha = await banPersonFromCommunity( let unBanAlpha = await banPersonFromCommunity(
beta, beta,
alphaUser.person.id, alphaPerson.person.id,
2, 2,
false false
); );

View File

@ -47,6 +47,8 @@ import {
BanFromCommunityResponse, BanFromCommunityResponse,
Post, Post,
CreatePrivateMessage, CreatePrivateMessage,
ResolveObjectResponse,
ResolveObject,
} from 'lemmy-js-client'; } from 'lemmy-js-client';
export interface API { export interface API {
@ -201,16 +203,14 @@ export async function lockPost(
return api.client.lockPost(form); return api.client.lockPost(form);
} }
export async function searchPost( export async function resolvePost(
api: API, api: API,
post: Post post: Post
): Promise<SearchResponse> { ): Promise<ResolveObjectResponse> {
let form: Search = { let form: ResolveObject = {
q: post.ap_id, q: post.ap_id,
type_: SearchType.Posts,
sort: SortType.TopAll,
}; };
return api.client.search(form); return api.client.resolveObject(form);
} }
export async function searchPostLocal( export async function searchPostLocal(
@ -235,56 +235,44 @@ export async function getPost(
return api.client.getPost(form); return api.client.getPost(form);
} }
export async function searchComment( export async function resolveComment(
api: API, api: API,
comment: Comment comment: Comment
): Promise<SearchResponse> { ): Promise<ResolveObjectResponse> {
let form: Search = { let form: ResolveObject = {
q: comment.ap_id, q: comment.ap_id,
type_: SearchType.Comments,
sort: SortType.TopAll,
}; };
return api.client.search(form); return api.client.resolveObject(form);
} }
export async function searchForBetaCommunity( export async function resolveBetaCommunity(
api: API api: API
): Promise<SearchResponse> { ): Promise<ResolveObjectResponse> {
// Make sure lemmy-beta/c/main is cached on lemmy_alpha
// Use short-hand search url // Use short-hand search url
let form: Search = { let form: ResolveObject = {
q: '!main@lemmy-beta:8551', q: '!main@lemmy-beta:8551',
type_: SearchType.Communities,
sort: SortType.TopAll,
}; };
return api.client.search(form); return api.client.resolveObject(form);
} }
export async function searchForCommunity( export async function resolveCommunity(
api: API, api: API,
q: string q: string
): Promise<SearchResponse> { ): Promise<ResolveObjectResponse> {
// Use short-hand search url let form: ResolveObject = {
let form: Search = {
q, q,
type_: SearchType.Communities,
sort: SortType.TopAll,
}; };
return api.client.search(form); return api.client.resolveObject(form);
} }
export async function searchForUser( export async function resolvePerson(
api: API, api: API,
apShortname: string apShortname: string
): Promise<SearchResponse> { ): Promise<ResolveObjectResponse> {
// Make sure lemmy-beta/c/main is cached on lemmy_alpha let form: ResolveObject = {
// Use short-hand search url
let form: Search = {
q: apShortname, q: apShortname,
type_: SearchType.Users,
sort: SortType.TopAll,
}; };
return api.client.search(form); return api.client.resolveObject(form);
} }
export async function banPersonFromSite( export async function banPersonFromSite(
@ -293,7 +281,6 @@ export async function banPersonFromSite(
ban: boolean ban: boolean
): Promise<BanPersonResponse> { ): Promise<BanPersonResponse> {
// Make sure lemmy-beta/c/main is cached on lemmy_alpha // Make sure lemmy-beta/c/main is cached on lemmy_alpha
// Use short-hand search url
let form: BanPerson = { let form: BanPerson = {
person_id, person_id,
ban, ban,
@ -310,7 +297,6 @@ export async function banPersonFromCommunity(
ban: boolean ban: boolean
): Promise<BanFromCommunityResponse> { ): Promise<BanFromCommunityResponse> {
// Make sure lemmy-beta/c/main is cached on lemmy_alpha // Make sure lemmy-beta/c/main is cached on lemmy_alpha
// Use short-hand search url
let form: BanFromCommunity = { let form: BanFromCommunity = {
person_id, person_id,
community_id, community_id,
@ -591,11 +577,9 @@ export async function unfollowRemotes(
} }
export async function followBeta(api: API): Promise<CommunityResponse> { export async function followBeta(api: API): Promise<CommunityResponse> {
// Cache it let betaCommunity = (await resolveBetaCommunity(api)).community;
let search = await searchForBetaCommunity(api); if (betaCommunity) {
let com = search.communities.find(c => c.community.local == false); let follow = await followCommunity(api, true, betaCommunity.community.id);
if (com) {
let follow = await followCommunity(api, true, com.community.id);
return follow; return follow;
} }
} }

View File

@ -3,7 +3,7 @@ import {
alpha, alpha,
beta, beta,
registerUser, registerUser,
searchForUser, resolvePerson,
saveUserSettings, saveUserSettings,
getSite, getSite,
} from './shared'; } from './shared';
@ -56,9 +56,7 @@ test('Set some user settings, check that they are federated', async () => {
}; };
await saveUserSettings(alpha, form); await saveUserSettings(alpha, form);
let searchAlpha = await searchForUser(alpha, apShortname); let alphaPerson = (await resolvePerson(alpha, apShortname)).person;
let userOnAlpha = searchAlpha.users[0]; let betaPerson = (await resolvePerson(beta, apShortname)).person;
let searchBeta = await searchForUser(beta, apShortname); assertUserFederation(alphaPerson, betaPerson);
let userOnBeta = searchBeta.users[0];
assertUserFederation(userOnAlpha, userOnBeta);
}); });

View File

@ -3076,10 +3076,10 @@ language-tags@^1.0.5:
dependencies: dependencies:
language-subtag-registry "~0.3.2" language-subtag-registry "~0.3.2"
lemmy-js-client@0.11.4-rc.9: lemmy-js-client@0.11.4-rc.14:
version "0.11.4-rc.9" version "0.11.4-rc.14"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.11.4-rc.9.tgz#f7b3c73691e4c1600daf3840d22d9cfbddc5f363" resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.11.4-rc.14.tgz#dcac5b8dc78c3b04e6b3630ff9351a94aa73e109"
integrity sha512-zP8JxWzQU+yuyE8cMG0GzR8aR3lJ++G5zzbynsXwDevzAZXhOm0ObNNtJiA3Q5msStFVKVYa3GwZxBv4XiYshw== integrity sha512-R8M+myyriNQljQlTweVqtUKGBpgmaM7RI4ebYb7N7sYr5Bk5Ip6v2qTNvKAV6BlsDOCTWANOonfeoz/cIerLEg==
leven@^3.1.0: leven@^3.1.0:
version "3.1.0" version "3.1.0"

View File

@ -87,6 +87,9 @@ pub async fn match_websocket_operation(
do_websocket_operation::<SaveSiteConfig>(context, id, op, data).await do_websocket_operation::<SaveSiteConfig>(context, id, op, data).await
} }
UserOperation::Search => do_websocket_operation::<Search>(context, id, op, data).await, UserOperation::Search => do_websocket_operation::<Search>(context, id, op, data).await,
UserOperation::ResolveObject => {
do_websocket_operation::<ResolveObject>(context, id, op, data).await
}
UserOperation::TransferCommunity => { UserOperation::TransferCommunity => {
do_websocket_operation::<TransferCommunity>(context, id, op, data).await do_websocket_operation::<TransferCommunity>(context, id, op, data).await
} }

View File

@ -50,7 +50,6 @@ use lemmy_utils::{
LemmyError, LemmyError,
}; };
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use log::debug;
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for GetModlog { impl Perform for GetModlog {
@ -143,11 +142,6 @@ impl Perform for Search {
) -> Result<SearchResponse, LemmyError> { ) -> Result<SearchResponse, LemmyError> {
let data: &Search = self; let data: &Search = self;
match search_by_apub_id(&data.q, context).await {
Ok(r) => return Ok(r),
Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
}
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?; let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw); let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
@ -372,6 +366,23 @@ impl Perform for Search {
} }
} }
#[async_trait::async_trait(?Send)]
impl Perform for ResolveObject {
type Response = ResolveObjectResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<ResolveObjectResponse, LemmyError> {
let local_user_view = get_local_user_view_from_jwt_opt(&self.auth, context.pool()).await?;
let res = search_by_apub_id(&self.q, local_user_view, context)
.await
.map_err(|_| ApiError::err("couldnt_find_object"))?;
Ok(res)
}
}
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl Perform for TransferSite { impl Perform for TransferSite {
type Response = GetSiteResponse; type Response = GetSiteResponse;

View File

@ -46,26 +46,8 @@ use lemmy_utils::{
LemmyError, LemmyError,
}; };
use log::error; use log::error;
use serde::{Deserialize, Serialize};
use url::Url; use url::Url;
#[derive(Serialize, Deserialize, Debug)]
pub struct WebFingerLink {
pub rel: Option<String>,
#[serde(rename(serialize = "type", deserialize = "type"))]
pub type_: Option<String>,
pub href: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub template: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct WebFingerResponse {
pub subject: String,
pub aliases: Vec<Url>,
pub links: Vec<WebFingerLink>,
}
pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError> pub async fn blocking<F, T>(pool: &DbPool, f: F) -> Result<T, LemmyError>
where where
F: FnOnce(&diesel::PgConnection) -> T + Send + 'static, F: FnOnce(&diesel::PgConnection) -> T + Send + 'static,

View File

@ -50,6 +50,20 @@ pub struct SearchResponse {
pub users: Vec<PersonViewSafe>, pub users: Vec<PersonViewSafe>,
} }
#[derive(Deserialize, Debug)]
pub struct ResolveObject {
pub q: String,
pub auth: Option<String>,
}
#[derive(Serialize, Default)]
pub struct ResolveObjectResponse {
pub comment: Option<CommentView>,
pub post: Option<PostView>,
pub community: Option<CommunityView>,
pub person: Option<PersonViewSafe>,
}
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct GetModlog { pub struct GetModlog {
pub mod_person_id: Option<PersonId>, pub mod_person_id: Option<PersonId>,

View File

@ -5,7 +5,8 @@ use activitystreams::{
}; };
use anyhow::anyhow; use anyhow::anyhow;
use itertools::Itertools; use itertools::Itertools;
use lemmy_api_common::{blocking, send_local_notifs, WebFingerResponse}; use lemmy_api_common::{blocking, send_local_notifs};
use lemmy_apub_lib::webfinger::WebfingerResponse;
use lemmy_db_queries::{Crud, DbPool}; use lemmy_db_queries::{Crud, DbPool};
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{comment::Comment, community::Community, person::Person, post::Post}, source::{comment::Comment, community::Community, person::Person, post::Post},
@ -128,7 +129,7 @@ async fn fetch_webfinger_url(mention: &MentionData, client: &Client) -> Result<U
let response = retry(|| client.get(&fetch_url).send()).await?; let response = retry(|| client.get(&fetch_url).send()).await?;
let res: WebFingerResponse = response let res: WebfingerResponse = response
.json() .json()
.await .await
.map_err(|e| RecvError(e.to_string()))?; .map_err(|e| RecvError(e.to_string()))?;

View File

@ -1,25 +1,24 @@
use crate::{ use crate::{
fetcher::{ fetcher::{
community::get_or_fetch_and_upsert_community,
fetch::fetch_remote_object, fetch::fetch_remote_object,
get_or_fetch_and_upsert_community,
get_or_fetch_and_upsert_person,
is_deleted, is_deleted,
person::get_or_fetch_and_upsert_person,
}, },
find_object_by_id, find_object_by_id,
objects::{comment::Note, community::Group, person::Person as ApubPerson, post::Page, FromApub}, objects::{comment::Note, community::Group, person::Person as ApubPerson, post::Page, FromApub},
Object, Object,
}; };
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_api_common::{blocking, site::SearchResponse}; use itertools::Itertools;
use lemmy_db_queries::{ use lemmy_api_common::{blocking, site::ResolveObjectResponse};
source::{ use lemmy_apub_lib::webfinger::{webfinger_resolve_actor, WebfingerType};
comment::Comment_, use lemmy_db_queries::source::{
community::Community_, comment::Comment_,
person::Person_, community::Community_,
post::Post_, person::Person_,
private_message::PrivateMessage_, post::Post_,
}, private_message::PrivateMessage_,
SearchType,
}; };
use lemmy_db_schema::source::{ use lemmy_db_schema::source::{
comment::Comment, comment::Comment,
@ -28,11 +27,14 @@ use lemmy_db_schema::source::{
post::Post, post::Post,
private_message::PrivateMessage, private_message::PrivateMessage,
}; };
use lemmy_db_views::{comment_view::CommentView, post_view::PostView}; use lemmy_db_views::{
comment_view::CommentView,
local_user_view::LocalUserView,
post_view::PostView,
};
use lemmy_db_views_actor::{community_view::CommunityView, person_view::PersonViewSafe}; use lemmy_db_views_actor::{community_view::CommunityView, person_view::PersonViewSafe};
use lemmy_utils::{settings::structs::Settings, LemmyError}; use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use log::debug;
use url::Url; use url::Url;
/// The types of ActivityPub objects that can be fetched directly by searching for their ID. /// The types of ActivityPub objects that can be fetched directly by searching for their ID.
@ -54,50 +56,68 @@ enum SearchAcceptedObjects {
/// http://lemmy_delta:8571/comment/2 /// http://lemmy_delta:8571/comment/2
pub async fn search_by_apub_id( pub async fn search_by_apub_id(
query: &str, query: &str,
local_user_view: Option<LocalUserView>,
context: &LemmyContext, context: &LemmyContext,
) -> Result<SearchResponse, LemmyError> { ) -> Result<ResolveObjectResponse, LemmyError> {
// Parse the shorthand query url let query_url = match Url::parse(query) {
let query_url = if query.contains('@') { Ok(u) => u,
debug!("Search for {}", query); Err(_) => {
let split = query.split('@').collect::<Vec<&str>>(); let (kind, name) = query.split_at(1);
let kind = match kind {
// Person type will look like ['', username, instance] "@" => WebfingerType::Person,
// Community will look like [!community, instance] "!" => WebfingerType::Group,
let (name, instance) = if split.len() == 3 { _ => return Err(anyhow!("invalid query").into()),
(format!("/u/{}", split[1]), split[2]) };
} else if split.len() == 2 { // remote actor, use webfinger to resolve url
if split[0].contains('!') { if name.contains('@') {
let split2 = split[0].split('!').collect::<Vec<&str>>(); let (name, domain) = name.splitn(2, '@').collect_tuple().expect("invalid query");
(format!("/c/{}", split2[1]), split[1]) webfinger_resolve_actor(name, domain, kind, context.client()).await?
} else {
return Err(anyhow!("Invalid search query: {}", query).into());
} }
} else { // local actor, read from database and return
return Err(anyhow!("Invalid search query: {}", query).into()); else {
}; let name: String = name.into();
return match kind {
let url = format!( WebfingerType::Group => {
"{}://{}{}", let res = blocking(context.pool(), move |conn| {
Settings::get().get_protocol_string(), let community = Community::read_from_name(conn, &name)?;
instance, CommunityView::read(conn, community.id, local_user_view.map(|l| l.person.id))
name })
); .await??;
Url::parse(&url)? Ok(ResolveObjectResponse {
} else { community: Some(res),
Url::parse(query)? ..ResolveObjectResponse::default()
})
}
WebfingerType::Person => {
let res = blocking(context.pool(), move |conn| {
let person = Person::find_by_name(conn, &name)?;
PersonViewSafe::read(conn, person.id)
})
.await??;
Ok(ResolveObjectResponse {
person: Some(res),
..ResolveObjectResponse::default()
})
}
};
}
}
}; };
let recursion_counter = &mut 0; let request_counter = &mut 0;
// this does a fetch (even for local objects), just to determine its type and fetch it again
// below. we need to fix this when rewriting the fetcher.
let fetch_response = let fetch_response =
fetch_remote_object::<SearchAcceptedObjects>(context.client(), &query_url, recursion_counter) fetch_remote_object::<SearchAcceptedObjects>(context.client(), &query_url, request_counter)
.await; .await;
if is_deleted(&fetch_response) { if is_deleted(&fetch_response) {
delete_object_locally(&query_url, context).await?; delete_object_locally(&query_url, context).await?;
return Err(anyhow!("Object was deleted").into());
} }
// Necessary because we get a stack overflow using FetchError // Necessary because we get a stack overflow using FetchError
let fet_res = fetch_response.map_err(|e| LemmyError::from(e.inner))?; let fet_res = fetch_response.map_err(|e| LemmyError::from(e.inner))?;
build_response(fet_res, query_url, recursion_counter, context).await build_response(fet_res, query_url, request_counter, context).await
} }
async fn build_response( async fn build_response(
@ -105,58 +125,56 @@ async fn build_response(
query_url: Url, query_url: Url,
recursion_counter: &mut i32, recursion_counter: &mut i32,
context: &LemmyContext, context: &LemmyContext,
) -> Result<SearchResponse, LemmyError> { ) -> Result<ResolveObjectResponse, LemmyError> {
let mut response = SearchResponse { use ResolveObjectResponse as ROR;
type_: SearchType::All.to_string(), Ok(match fetch_response {
comments: vec![],
posts: vec![],
communities: vec![],
users: vec![],
};
match fetch_response {
SearchAcceptedObjects::Person(p) => { SearchAcceptedObjects::Person(p) => {
let person_id = p.id(&query_url)?; let person_uri = p.id(&query_url)?;
let person = get_or_fetch_and_upsert_person(person_id, context, recursion_counter).await?;
response.users = vec![ let person = get_or_fetch_and_upsert_person(person_uri, context, recursion_counter).await?;
blocking(context.pool(), move |conn| { ROR {
person: blocking(context.pool(), move |conn| {
PersonViewSafe::read(conn, person.id) PersonViewSafe::read(conn, person.id)
}) })
.await??, .await?
]; .ok(),
..ROR::default()
}
} }
SearchAcceptedObjects::Group(g) => { SearchAcceptedObjects::Group(g) => {
let community_uri = g.id(&query_url)?; let community_uri = g.id(&query_url)?;
let community = let community =
get_or_fetch_and_upsert_community(community_uri, context, recursion_counter).await?; get_or_fetch_and_upsert_community(community_uri, context, recursion_counter).await?;
ROR {
response.communities = vec![ community: blocking(context.pool(), move |conn| {
blocking(context.pool(), move |conn| {
CommunityView::read(conn, community.id, None) CommunityView::read(conn, community.id, None)
}) })
.await??, .await?
]; .ok(),
..ROR::default()
}
} }
SearchAcceptedObjects::Page(p) => { SearchAcceptedObjects::Page(p) => {
let p = Post::from_apub(&p, context, &query_url, recursion_counter).await?; let p = Post::from_apub(&p, context, &query_url, recursion_counter).await?;
ROR {
response.posts = post: blocking(context.pool(), move |conn| PostView::read(conn, p.id, None))
vec![blocking(context.pool(), move |conn| PostView::read(conn, p.id, None)).await??]; .await?
.ok(),
..ROR::default()
}
} }
SearchAcceptedObjects::Comment(c) => { SearchAcceptedObjects::Comment(c) => {
let c = Comment::from_apub(&c, context, &query_url, recursion_counter).await?; let c = Comment::from_apub(&c, context, &query_url, recursion_counter).await?;
ROR {
response.comments = vec![ comment: blocking(context.pool(), move |conn| {
blocking(context.pool(), move |conn| {
CommentView::read(conn, c.id, None) CommentView::read(conn, c.id, None)
}) })
.await??, .await?
]; .ok(),
..ROR::default()
}
} }
}; })
Ok(response)
} }
async fn delete_object_locally(query_url: &Url, context: &LemmyContext) -> Result<(), LemmyError> { async fn delete_object_locally(query_url: &Url, context: &LemmyContext) -> Result<(), LemmyError> {
@ -194,5 +212,5 @@ async fn delete_object_locally(query_url: &Url, context: &LemmyContext) -> Resul
.await??; .await??;
} }
} }
Err(anyhow!("Object was deleted").into()) Ok(())
} }

View File

@ -14,3 +14,6 @@ serde = { version = "1.0.127", features = ["derive"] }
async-trait = "0.1.51" async-trait = "0.1.51"
url = { version = "2.2.2", features = ["serde"] } url = { version = "2.2.2", features = ["serde"] }
serde_json = { version = "1.0.66", features = ["preserve_order"] } serde_json = { version = "1.0.66", features = ["preserve_order"] }
anyhow = "1.0.41"
reqwest = { version = "0.11.4", features = ["json"] }
log = "0.4.14"

View File

@ -6,6 +6,8 @@ use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use url::Url; use url::Url;
pub mod webfinger;
pub trait ActivityFields { pub trait ActivityFields {
fn id_unchecked(&self) -> &Url; fn id_unchecked(&self) -> &Url;
fn actor(&self) -> &Url; fn actor(&self) -> &Url;

View File

@ -0,0 +1,72 @@
use anyhow::anyhow;
use lemmy_utils::{
request::{retry, RecvError},
settings::structs::Settings,
LemmyError,
};
use log::debug;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use url::Url;
#[derive(Serialize, Deserialize, Debug)]
pub struct WebfingerLink {
pub rel: Option<String>,
#[serde(rename(serialize = "type", deserialize = "type"))]
pub type_: Option<String>,
pub href: Option<Url>,
#[serde(skip_serializing_if = "Option::is_none")]
pub template: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct WebfingerResponse {
pub subject: String,
pub aliases: Vec<Url>,
pub links: Vec<WebfingerLink>,
}
pub enum WebfingerType {
Person,
Group,
}
/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
/// using webfinger.
pub async fn webfinger_resolve_actor(
name: &str,
domain: &str,
webfinger_type: WebfingerType,
client: &Client,
) -> Result<Url, LemmyError> {
let webfinger_type = match webfinger_type {
WebfingerType::Person => "acct",
WebfingerType::Group => "group",
};
let fetch_url = format!(
"{}://{}/.well-known/webfinger?resource={}:{}@{}",
Settings::get().get_protocol_string(),
domain,
webfinger_type,
name,
domain
);
debug!("Fetching webfinger url: {}", &fetch_url);
let response = retry(|| client.get(&fetch_url).send()).await?;
let res: WebfingerResponse = response
.json()
.await
.map_err(|e| RecvError(e.to_string()))?;
let link = res
.links
.iter()
.find(|l| l.type_.eq(&Some("application/activity+json".to_string())))
.ok_or_else(|| anyhow!("No application/activity+json link found."))?;
link
.href
.to_owned()
.ok_or_else(|| anyhow!("No href found.").into())
}

View File

@ -16,6 +16,7 @@ lemmy_db_views = { version = "=0.11.3", path = "../db_views" }
lemmy_db_views_actor = { version = "=0.11.3", path = "../db_views_actor" } lemmy_db_views_actor = { version = "=0.11.3", path = "../db_views_actor" }
lemmy_db_schema = { version = "=0.11.3", path = "../db_schema" } lemmy_db_schema = { version = "=0.11.3", path = "../db_schema" }
lemmy_api_common = { version = "=0.11.3", path = "../api_common" } lemmy_api_common = { version = "=0.11.3", path = "../api_common" }
lemmy_apub_lib = { version = "=0.11.3", path = "../apub_lib" }
diesel = "1.4.7" diesel = "1.4.7"
actix = "0.12.0" actix = "0.12.0"
actix-web = { version = "4.0.0-beta.8", default-features = false, features = ["rustls"] } actix-web = { version = "4.0.0-beta.8", default-features = false, features = ["rustls"] }

View File

@ -1,6 +1,7 @@
use actix_web::{error::ErrorBadRequest, web::Query, *}; use actix_web::{error::ErrorBadRequest, web::Query, *};
use anyhow::anyhow; use anyhow::anyhow;
use lemmy_api_common::{blocking, WebFingerLink, WebFingerResponse}; use lemmy_api_common::blocking;
use lemmy_apub_lib::webfinger::{WebfingerLink, WebfingerResponse};
use lemmy_db_queries::source::{community::Community_, person::Person_}; use lemmy_db_queries::source::{community::Community_, person::Person_};
use lemmy_db_schema::source::{community::Community, person::Person}; use lemmy_db_schema::source::{community::Community, person::Person};
use lemmy_utils::{ use lemmy_utils::{
@ -68,17 +69,17 @@ async fn get_webfinger_response(
return Err(ErrorBadRequest(LemmyError::from(anyhow!("not_found")))); return Err(ErrorBadRequest(LemmyError::from(anyhow!("not_found"))));
}; };
let json = WebFingerResponse { let json = WebfingerResponse {
subject: info.resource.to_owned(), subject: info.resource.to_owned(),
aliases: vec![url.to_owned().into()], aliases: vec![url.to_owned().into()],
links: vec![ links: vec![
WebFingerLink { WebfingerLink {
rel: Some("http://webfinger.net/rel/profile-page".to_string()), rel: Some("http://webfinger.net/rel/profile-page".to_string()),
type_: Some("text/html".to_string()), type_: Some("text/html".to_string()),
href: Some(url.to_owned().into()), href: Some(url.to_owned().into()),
template: None, template: None,
}, },
WebFingerLink { WebfingerLink {
rel: Some("self".to_string()), rel: Some("self".to_string()),
type_: Some("application/activity+json".to_string()), type_: Some("application/activity+json".to_string()),
href: Some(url.into()), href: Some(url.into()),

View File

@ -110,6 +110,7 @@ pub enum UserOperation {
AddAdmin, AddAdmin,
BanPerson, BanPerson,
Search, Search,
ResolveObject,
MarkAllAsRead, MarkAllAsRead,
SaveUserSettings, SaveUserSettings,
TransferCommunity, TransferCommunity,

View File

@ -33,6 +33,11 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimit) {
.wrap(rate_limit.message()) .wrap(rate_limit.message())
.route(web::get().to(route_get::<Search>)), .route(web::get().to(route_get::<Search>)),
) )
.service(
web::resource("/resolve_object")
.wrap(rate_limit.message())
.route(web::get().to(route_get::<ResolveObject>)),
)
// Community // Community
.service( .service(
web::resource("/community") web::resource("/community")