Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize api endpoints (fixes #2022) #5216

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api_tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"eslint": "^9.14.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.5.0",
"lemmy-js-client": "0.20.0-instance-blocks.5",
"lemmy-js-client": "0.20.0-api-v4.16",
"prettier": "^3.2.5",
"ts-jest": "^29.1.0",
"typescript": "^5.5.4",
Expand Down
10 changes: 5 additions & 5 deletions api_tests/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions api_tests/prepare-drone-federation-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_epsilon.hjson \
target/lemmy_server >$LOG_DIR/lemmy_epsilon.out 2>&1 &

echo "wait for all instances to start"
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-alpha:8541/api/v3/site')" != "200" ]]; do sleep 1; done
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-alpha:8541/api/v4/site')" != "200" ]]; do sleep 1; done
echo "alpha started"
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-beta:8551/api/v3/site')" != "200" ]]; do sleep 1; done
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-beta:8551/api/v4/site')" != "200" ]]; do sleep 1; done
echo "beta started"
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-gamma:8561/api/v3/site')" != "200" ]]; do sleep 1; done
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-gamma:8561/api/v4/site')" != "200" ]]; do sleep 1; done
echo "gamma started"
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-delta:8571/api/v3/site')" != "200" ]]; do sleep 1; done
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-delta:8571/api/v4/site')" != "200" ]]; do sleep 1; done
echo "delta started"
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-epsilon:8581/api/v3/site')" != "200" ]]; do sleep 1; done
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'lemmy-epsilon:8581/api/v4/site')" != "200" ]]; do sleep 1; done
echo "epsilon started. All started"
8 changes: 4 additions & 4 deletions api_tests/src/comment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,10 @@ test("Check that activity from another instance is sent to third instance", asyn

test("Fetch in_reply_tos: A is unsubbed from B, B makes a post, and some embedded comments, A subs to B, B updates the lowest level comment, A fetches both the post and all the inreplyto comments for that post.", async () => {
// Unfollow all remote communities
let site = await unfollowRemotes(alpha);
expect(
site.my_user?.follows.filter(c => c.community.local == false).length,
).toBe(0);
let my_user = await unfollowRemotes(alpha);
expect(my_user.follows.filter(c => c.community.local == false).length).toBe(
0,
);

// B creates a post, and two comments, should be invisible to A
let postOnBetaRes = await createPost(beta, 2);
Expand Down
3 changes: 2 additions & 1 deletion api_tests/src/community.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
longDelay,
editCommunity,
unfollows,
getMyUser,
userBlockInstance,
} from "./shared";
import { AdminAllowInstanceParams } from "lemmy-js-client/dist/types/AdminAllowInstanceParams";
Expand Down Expand Up @@ -226,7 +227,7 @@ test("Admin actions in remote community are not federated to origin", async () =
if (!betaCommunity) {
throw "Missing beta community";
}
let bannedUserInfo1 = (await getSite(gamma)).my_user?.local_user_view.person;
let bannedUserInfo1 = (await getMyUser(gamma)).local_user_view.person;
if (!bannedUserInfo1) {
throw "Missing banned user 1";
}
Expand Down
9 changes: 5 additions & 4 deletions api_tests/src/follow.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
registerUser,
unfollows,
delay,
getMyUser,
} from "./shared";

beforeAll(setupLogins);
Expand Down Expand Up @@ -85,8 +86,8 @@ test("Follow federated community", async () => {
);

// Check it from local
let site = await getSite(alpha);
let remoteCommunityId = site.my_user?.follows.find(
let my_user = await getMyUser(alpha);
let remoteCommunityId = my_user?.follows.find(
c =>
c.community.local == false &&
c.community.id === betaCommunityInitial.community.id,
Expand All @@ -102,9 +103,9 @@ test("Follow federated community", async () => {
expect(unfollow.community_view.subscribed).toBe("NotSubscribed");

// Make sure you are unsubbed locally
let siteUnfollowCheck = await getSite(alpha);
let siteUnfollowCheck = await getMyUser(alpha);
expect(
siteUnfollowCheck.my_user?.follows.find(
siteUnfollowCheck.follows.find(
c => c.community.id === betaCommunityInitial.community.id,
),
).toBe(undefined);
Expand Down
17 changes: 9 additions & 8 deletions api_tests/src/image.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
createPostWithThumbnail,
sampleImage,
sampleSite,
getMyUser,
} from "./shared";

beforeAll(setupLogins);
Expand Down Expand Up @@ -129,9 +130,9 @@ test("Purge user, uploaded image removed", async () => {
expect(content.length).toBeGreaterThan(0);

// purge user
let site = await getSite(user);
let my_user = await getMyUser(user);
const purgeForm: PurgePerson = {
person_id: site.my_user!.local_user_view.person.id,
person_id: my_user.local_user_view.person.id,
};
const delete_ = await alphaImage.purgePerson(purgeForm);
expect(delete_.success).toBe(true);
Expand Down Expand Up @@ -199,11 +200,11 @@ test("Images in remote image post are proxied if setting enabled", async () => {
// remote image gets proxied after upload
expect(
post.thumbnail_url?.startsWith(
"http://lemmy-gamma:8561/api/v3/image_proxy?url",
"http://lemmy-gamma:8561/api/v4/image_proxy?url",
),
).toBeTruthy();
expect(
post.body?.startsWith("![](http://lemmy-gamma:8561/api/v3/image_proxy?url"),
post.body?.startsWith("![](http://lemmy-gamma:8561/api/v4/image_proxy?url"),
).toBeTruthy();

// Make sure that it ends with jpg, to be sure its an image
Expand All @@ -222,12 +223,12 @@ test("Images in remote image post are proxied if setting enabled", async () => {

expect(
epsilonPost.thumbnail_url?.startsWith(
"http://lemmy-epsilon:8581/api/v3/image_proxy?url",
"http://lemmy-epsilon:8581/api/v4/image_proxy?url",
),
).toBeTruthy();
expect(
epsilonPost.body?.startsWith(
"![](http://lemmy-epsilon:8581/api/v3/image_proxy?url",
"![](http://lemmy-epsilon:8581/api/v4/image_proxy?url",
),
).toBeTruthy();

Expand All @@ -249,7 +250,7 @@ test("Thumbnail of remote image link is proxied if setting enabled", async () =>
// remote image gets proxied after upload
expect(
post.thumbnail_url?.startsWith(
"http://lemmy-gamma:8561/api/v3/image_proxy?url",
"http://lemmy-gamma:8561/api/v4/image_proxy?url",
),
).toBeTruthy();

Expand All @@ -267,7 +268,7 @@ test("Thumbnail of remote image link is proxied if setting enabled", async () =>

expect(
epsilonPost.thumbnail_url?.startsWith(
"http://lemmy-epsilon:8581/api/v3/image_proxy?url",
"http://lemmy-epsilon:8581/api/v4/image_proxy?url",
),
).toBeTruthy();

Expand Down
10 changes: 4 additions & 6 deletions api_tests/src/post.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
alphaUrl,
loginUser,
createCommunity,
getMyUser,
} from "./shared";
import { PostView } from "lemmy-js-client/dist/types/PostView";
import { AdminBlockInstanceParams } from "lemmy-js-client/dist/types/AdminBlockInstanceParams";
Expand Down Expand Up @@ -451,8 +452,7 @@ test("Enforce site ban federation for local user", async () => {

// create a test user
let alphaUserHttp = await registerUser(alpha, alphaUrl);
let alphaUserPerson = (await getSite(alphaUserHttp)).my_user?.local_user_view
.person;
let alphaUserPerson = (await getMyUser(alphaUserHttp)).local_user_view.person;
let alphaUserActorId = alphaUserPerson?.actor_id;
if (!alphaUserActorId) {
throw "Missing alpha user actor id";
Expand Down Expand Up @@ -532,8 +532,7 @@ test("Enforce site ban federation for federated user", async () => {

// create a test user
let alphaUserHttp = await registerUser(alpha, alphaUrl);
let alphaUserPerson = (await getSite(alphaUserHttp)).my_user?.local_user_view
.person;
let alphaUserPerson = (await getMyUser(alphaUserHttp)).local_user_view.person;
let alphaUserActorId = alphaUserPerson?.actor_id;
if (!alphaUserActorId) {
throw "Missing alpha user actor id";
Expand Down Expand Up @@ -563,8 +562,7 @@ test("Enforce site ban federation for federated user", async () => {
expect(banAlphaOnBeta.banned).toBe(true);

// The beta site ban should NOT be federated to alpha
let alphaPerson2 = (await getSite(alphaUserHttp)).my_user!.local_user_view
.person;
let alphaPerson2 = (await getMyUser(alphaUserHttp)).local_user_view.person;
expect(alphaPerson2.banned).toBe(false);

// existing alpha post should be removed on beta
Expand Down
16 changes: 9 additions & 7 deletions api_tests/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
LemmyHttp,
ListCommunityPendingFollows,
ListCommunityPendingFollowsResponse,
MyUserInfo,
PersonId,
PostView,
PrivateMessageReportResponse,
Expand Down Expand Up @@ -761,6 +762,10 @@ export async function getSite(api: LemmyHttp): Promise<GetSiteResponse> {
return api.getSite();
}

export async function getMyUser(api: LemmyHttp): Promise<MyUserInfo> {
return api.getMyUser();
}

export async function listPrivateMessages(
api: LemmyHttp,
): Promise<PrivateMessagesResponse> {
Expand All @@ -770,19 +775,16 @@ export async function listPrivateMessages(
return api.getPrivateMessages(form);
}

export async function unfollowRemotes(
api: LemmyHttp,
): Promise<GetSiteResponse> {
export async function unfollowRemotes(api: LemmyHttp): Promise<MyUserInfo> {
// Unfollow all remote communities
let site = await getSite(api);
let my_user = await getMyUser(api);
let remoteFollowed =
site.my_user?.follows.filter(c => c.community.local == false) ?? [];
my_user.follows.filter(c => c.community.local == false) ?? [];
await Promise.all(
remoteFollowed.map(cu => followCommunity(api, false, cu.community.id)),
);

let siteRes = await getSite(api);
return siteRes;
return await getMyUser(api);
}

export async function followBeta(api: LemmyHttp): Promise<CommunityResponse> {
Expand Down
40 changes: 18 additions & 22 deletions api_tests/src/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
alphaImage,
unfollows,
saveUserSettingsBio,
getMyUser,
} from "./shared";
import {
EditSite,
Expand Down Expand Up @@ -49,12 +50,9 @@ function assertUserFederation(userOne?: PersonView, userTwo?: PersonView) {
test("Create user", async () => {
let user = await registerUser(alpha, alphaUrl);

let site = await getSite(user);
expect(site.my_user).toBeDefined();
if (!site.my_user) {
throw "Missing site user";
}
apShortname = `${site.my_user.local_user_view.person.name}@lemmy-alpha:8541`;
let my_user = await getMyUser(user);
expect(my_user).toBeDefined();
apShortname = `${my_user.local_user_view.person.name}@lemmy-alpha:8541`;
});

test("Set some user settings, check that they are federated", async () => {
Expand All @@ -69,8 +67,8 @@ test("Set some user settings, check that they are federated", async () => {
};
await saveUserSettings(beta, form);

let site = await getSite(beta);
expect(site.my_user?.local_user_view.local_user.theme).toBe("test");
let my_user = await getMyUser(beta);
expect(my_user.local_user_view.local_user.theme).toBe("test");
});

test("Delete user", async () => {
Expand Down Expand Up @@ -126,8 +124,10 @@ test("Requests with invalid auth should be treated as unauthenticated", async ()
headers: { Authorization: "Bearer foobar" },
fetchFunction,
});
await expect(getMyUser(invalid_auth)).rejects.toStrictEqual(
Error("incorrect_login"),
);
let site = await getSite(invalid_auth);
expect(site.my_user).toBeUndefined();
expect(site.site_view).toBeDefined();

let form: GetPosts = {};
Expand All @@ -142,12 +142,9 @@ test("Create user with Arabic name", async () => {
"تجريب" + Math.random().toString().slice(2, 10), // less than actor_name_max_length
);

let site = await getSite(user);
expect(site.my_user).toBeDefined();
if (!site.my_user) {
throw "Missing site user";
}
apShortname = `${site.my_user.local_user_view.person.name}@lemmy-alpha:8541`;
let my_user = await getMyUser(user);
expect(my_user).toBeDefined();
apShortname = `${my_user.local_user_view.person.name}@lemmy-alpha:8541`;

let alphaPerson = (await resolvePerson(alpha, apShortname)).person;
expect(alphaPerson).toBeDefined();
Expand All @@ -165,13 +162,12 @@ test("Create user with accept-language", async () => {
});
let user = await registerUser(lemmy_http, alphaUrl);

let my_user = await getMyUser(user);
expect(my_user).toBeDefined();
expect(my_user?.local_user_view.local_user.interface_language).toBe("fr");
let site = await getSite(user);
expect(site.my_user).toBeDefined();
expect(site.my_user?.local_user_view.local_user.interface_language).toBe(
"fr",
);
let langs = site.all_languages
.filter(a => site.my_user?.discussion_languages.includes(a.id))
.filter(a => my_user.discussion_languages.includes(a.id))
.map(l => l.code);
// should have languages from accept header, as well as "undetermined"
// which is automatically enabled by backend
Expand Down Expand Up @@ -217,8 +213,8 @@ test("Set a new avatar, old avatar is deleted", async () => {
// Now try to save a user settings, with the icon missing,
// and make sure it doesn't clear the data, or delete the image
await saveUserSettingsBio(alpha);
let site = await getSite(alpha);
expect(site.my_user?.local_user_view.person.avatar).toBe(upload2.url);
let my_user = await getMyUser(alpha);
expect(my_user.local_user_view.person.avatar).toBe(upload2.url);

// make sure only the new avatar is kept
const listMediaRes4 = await alphaImage.listMedia();
Expand Down
2 changes: 1 addition & 1 deletion config/defaults.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
# or

# If enabled, all images from remote domains are rewritten to pass through
# `/api/v3/image_proxy`, including embedded images in markdown. Images are stored temporarily
# `/api/v4/image_proxy`, including embedded images in markdown. Images are stored temporarily
# in pict-rs for caching. This improves privacy as users don't expose their IP to untrusted
# servers, and decreases load on other servers. However it increases bandwidth use for the
# local server.
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/community/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use lemmy_db_views_actor::structs::CommunityView;
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult};

#[tracing::instrument(skip(context))]
pub async fn block_community(
pub async fn user_block_community(
data: Json<BlockCommunity>,
context: Data<LemmyContext>,
local_user_view: LocalUserView,
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/local_user/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult};

#[tracing::instrument(skip(context))]
pub async fn block_person(
pub async fn user_block_person(
data: Json<BlockPerson>,
context: Data<LemmyContext>,
local_user_view: LocalUserView,
Expand Down
1 change: 1 addition & 0 deletions crates/api/src/local_user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ pub mod report_count;
pub mod reset_password;
pub mod save_settings;
pub mod update_totp;
pub mod user_block_instance;
pub mod validate_auth;
pub mod verify_email;
Loading