lemmy/api_tests/src/shared.ts

637 lines
13 KiB
TypeScript
Raw Normal View History

import {
2020-12-20 21:16:57 +00:00
Login,
LoginResponse,
2020-12-20 21:16:57 +00:00
CreatePost,
EditPost,
CreateComment,
DeletePost,
RemovePost,
StickyPost,
LockPost,
PostResponse,
SearchResponse,
2020-12-20 21:16:57 +00:00
FollowCommunity,
CommunityResponse,
GetFollowedCommunitiesResponse,
GetPostResponse,
2020-12-20 21:16:57 +00:00
Register,
Comment,
EditComment,
DeleteComment,
RemoveComment,
Search,
CommentResponse,
2020-12-20 21:16:57 +00:00
GetCommunity,
CreateCommunity,
DeleteCommunity,
RemoveCommunity,
2021-03-12 19:09:03 +00:00
GetPersonMentions,
2020-12-20 21:16:57 +00:00
CreateCommentLike,
CreatePostLike,
EditPrivateMessage,
DeletePrivateMessage,
GetFollowedCommunities,
GetPrivateMessages,
GetSite,
GetPost,
PrivateMessageResponse,
PrivateMessagesResponse,
2021-03-12 19:09:03 +00:00
GetPersonMentionsResponse,
2020-12-20 21:16:57 +00:00
SaveUserSettings,
SortType,
ListingType,
GetSiteResponse,
SearchType,
LemmyHttp,
2021-03-12 19:09:03 +00:00
BanPersonResponse,
BanPerson,
2020-12-20 21:16:57 +00:00
BanFromCommunity,
BanFromCommunityResponse,
2020-12-20 21:16:57 +00:00
Post,
CreatePrivateMessage,
} from 'lemmy-js-client';
export interface API {
client: LemmyHttp;
auth?: string;
}
export let alpha: API = {
2020-12-20 21:16:57 +00:00
client: new LemmyHttp('http://localhost:8541/api/v2'),
};
export let beta: API = {
2020-12-20 21:16:57 +00:00
client: new LemmyHttp('http://localhost:8551/api/v2'),
};
export let gamma: API = {
2020-12-20 21:16:57 +00:00
client: new LemmyHttp('http://localhost:8561/api/v2'),
};
export let delta: API = {
2020-12-20 21:16:57 +00:00
client: new LemmyHttp('http://localhost:8571/api/v2'),
};
export let epsilon: API = {
2020-12-20 21:16:57 +00:00
client: new LemmyHttp('http://localhost:8581/api/v2'),
};
export async function setupLogins() {
2020-12-20 21:16:57 +00:00
let formAlpha: Login = {
username_or_email: 'lemmy_alpha',
password: 'lemmy',
};
let resAlpha = alpha.client.login(formAlpha);
let formBeta = {
username_or_email: 'lemmy_beta',
password: 'lemmy',
};
let resBeta = beta.client.login(formBeta);
let formGamma = {
username_or_email: 'lemmy_gamma',
password: 'lemmy',
};
let resGamma = gamma.client.login(formGamma);
let formDelta = {
username_or_email: 'lemmy_delta',
password: 'lemmy',
};
let resDelta = delta.client.login(formDelta);
let formEpsilon = {
username_or_email: 'lemmy_epsilon',
password: 'lemmy',
};
let resEpsilon = epsilon.client.login(formEpsilon);
let res = await Promise.all([
resAlpha,
resBeta,
resGamma,
resDelta,
resEpsilon,
]);
alpha.auth = res[0].jwt;
beta.auth = res[1].jwt;
gamma.auth = res[2].jwt;
delta.auth = res[3].jwt;
epsilon.auth = res[4].jwt;
}
export async function createPost(
api: API,
community_id: number
): Promise<PostResponse> {
let name = randomString(5);
let body = randomString(10);
let url = 'https://google.com/';
2020-12-20 21:16:57 +00:00
let form: CreatePost = {
name,
url,
body,
auth: api.auth,
community_id,
nsfw: false,
};
return api.client.createPost(form);
}
2020-12-20 21:16:57 +00:00
export async function editPost(api: API, post: Post): Promise<PostResponse> {
let name = 'A jest test federated post, updated';
2020-12-20 21:16:57 +00:00
let form: EditPost = {
name,
post_id: post.id,
auth: api.auth,
nsfw: false,
};
return api.client.editPost(form);
}
export async function deletePost(
api: API,
deleted: boolean,
post: Post
): Promise<PostResponse> {
2020-12-20 21:16:57 +00:00
let form: DeletePost = {
post_id: post.id,
deleted: deleted,
auth: api.auth,
};
return api.client.deletePost(form);
}
export async function removePost(
api: API,
removed: boolean,
post: Post
): Promise<PostResponse> {
2020-12-20 21:16:57 +00:00
let form: RemovePost = {
post_id: post.id,
removed,
auth: api.auth,
};
return api.client.removePost(form);
}
export async function stickyPost(
api: API,
stickied: boolean,
post: Post
): Promise<PostResponse> {
2020-12-20 21:16:57 +00:00
let form: StickyPost = {
post_id: post.id,
stickied,
auth: api.auth,
};
return api.client.stickyPost(form);
}
export async function lockPost(
api: API,
locked: boolean,
post: Post
): Promise<PostResponse> {
2020-12-20 21:16:57 +00:00
let form: LockPost = {
post_id: post.id,
locked,
auth: api.auth,
};
return api.client.lockPost(form);
}
export async function searchPost(
api: API,
post: Post
): Promise<SearchResponse> {
2020-12-20 21:16:57 +00:00
let form: Search = {
q: post.ap_id,
type_: SearchType.Posts,
sort: SortType.TopAll,
};
return api.client.search(form);
}
export async function searchPostLocal(
api: API,
post: Post
): Promise<SearchResponse> {
2020-12-20 21:16:57 +00:00
let form: Search = {
q: post.name,
type_: SearchType.Posts,
sort: SortType.TopAll,
};
return api.client.search(form);
}
export async function getPost(
api: API,
post_id: number
): Promise<GetPostResponse> {
2020-12-20 21:16:57 +00:00
let form: GetPost = {
id: post_id,
};
return api.client.getPost(form);
}
export async function searchComment(
api: API,
comment: Comment
): Promise<SearchResponse> {
2020-12-20 21:16:57 +00:00
let form: Search = {
q: comment.ap_id,
type_: SearchType.Comments,
sort: SortType.TopAll,
};
return api.client.search(form);
}
export async function searchForBetaCommunity(
api: API
): Promise<SearchResponse> {
// Make sure lemmy-beta/c/main is cached on lemmy_alpha
// Use short-hand search url
2020-12-20 21:16:57 +00:00
let form: Search = {
q: '!main@lemmy-beta:8551',
type_: SearchType.Communities,
sort: SortType.TopAll,
};
return api.client.search(form);
}
export async function searchForCommunity(
api: API,
2020-12-20 21:16:57 +00:00
q: string
): Promise<SearchResponse> {
// Use short-hand search url
2020-12-20 21:16:57 +00:00
let form: Search = {
q,
type_: SearchType.Communities,
sort: SortType.TopAll,
};
return api.client.search(form);
}
export async function searchForUser(
api: API,
apShortname: string
): Promise<SearchResponse> {
// Make sure lemmy-beta/c/main is cached on lemmy_alpha
// Use short-hand search url
2020-12-20 21:16:57 +00:00
let form: Search = {
q: apShortname,
type_: SearchType.Users,
sort: SortType.TopAll,
};
return api.client.search(form);
}
2021-03-12 19:09:03 +00:00
export async function banPersonFromSite(
api: API,
2021-03-12 19:09:03 +00:00
person_id: number,
2020-12-20 21:16:57 +00:00
ban: boolean
2021-03-12 19:09:03 +00:00
): Promise<BanPersonResponse> {
// Make sure lemmy-beta/c/main is cached on lemmy_alpha
// Use short-hand search url
2021-03-12 19:09:03 +00:00
let form: BanPerson = {
person_id,
ban,
2020-12-20 21:16:57 +00:00
remove_data: false,
auth: api.auth,
};
2021-03-12 19:09:03 +00:00
return api.client.banPerson(form);
}
2021-03-12 19:09:03 +00:00
export async function banPersonFromCommunity(
api: API,
2021-03-12 19:09:03 +00:00
person_id: number,
community_id: number,
2020-12-20 21:16:57 +00:00
ban: boolean
): Promise<BanFromCommunityResponse> {
// Make sure lemmy-beta/c/main is cached on lemmy_alpha
// Use short-hand search url
2020-12-20 21:16:57 +00:00
let form: BanFromCommunity = {
2021-03-12 19:09:03 +00:00
person_id,
community_id,
2020-12-20 21:16:57 +00:00
remove_data: false,
ban,
auth: api.auth,
};
return api.client.banFromCommunity(form);
}
export async function followCommunity(
api: API,
follow: boolean,
community_id: number
): Promise<CommunityResponse> {
2020-12-20 21:16:57 +00:00
let form: FollowCommunity = {
community_id,
follow,
auth: api.auth,
};
return api.client.followCommunity(form);
}
export async function checkFollowedCommunities(
api: API
): Promise<GetFollowedCommunitiesResponse> {
2020-12-20 21:16:57 +00:00
let form: GetFollowedCommunities = {
auth: api.auth,
};
return api.client.getFollowedCommunities(form);
}
export async function likePost(
api: API,
score: number,
post: Post
): Promise<PostResponse> {
2020-12-20 21:16:57 +00:00
let form: CreatePostLike = {
post_id: post.id,
score: score,
auth: api.auth,
};
return api.client.likePost(form);
}
export async function createComment(
api: API,
post_id: number,
parent_id?: number,
content = 'a jest test comment'
): Promise<CommentResponse> {
2020-12-20 21:16:57 +00:00
let form: CreateComment = {
content,
post_id,
parent_id,
auth: api.auth,
};
return api.client.createComment(form);
}
2020-12-20 21:16:57 +00:00
export async function editComment(
api: API,
comment_id: number,
content = 'A jest test federated comment update'
): Promise<CommentResponse> {
2020-12-20 21:16:57 +00:00
let form: EditComment = {
content,
comment_id,
auth: api.auth,
};
return api.client.editComment(form);
}
export async function deleteComment(
api: API,
deleted: boolean,
comment_id: number
): Promise<CommentResponse> {
2020-12-20 21:16:57 +00:00
let form: DeleteComment = {
comment_id,
deleted,
auth: api.auth,
};
return api.client.deleteComment(form);
}
export async function removeComment(
api: API,
removed: boolean,
comment_id: number
): Promise<CommentResponse> {
2020-12-20 21:16:57 +00:00
let form: RemoveComment = {
comment_id,
removed,
auth: api.auth,
};
return api.client.removeComment(form);
}
2021-03-12 19:09:03 +00:00
export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
let form: GetPersonMentions = {
sort: SortType.New,
unread_only: false,
auth: api.auth,
};
2021-03-12 19:09:03 +00:00
return api.client.getPersonMentions(form);
}
export async function likeComment(
api: API,
score: number,
comment: Comment
): Promise<CommentResponse> {
2020-12-20 21:16:57 +00:00
let form: CreateCommentLike = {
comment_id: comment.id,
score,
auth: api.auth,
};
return api.client.likeComment(form);
}
export async function createCommunity(
api: API,
name_: string = randomString(5)
): Promise<CommunityResponse> {
let description = 'a sample description';
let icon = 'https://image.flaticon.com/icons/png/512/35/35896.png';
let banner = 'https://image.flaticon.com/icons/png/512/35/35896.png';
2020-12-20 21:16:57 +00:00
let form: CreateCommunity = {
name: name_,
title: name_,
description,
icon,
banner,
nsfw: false,
auth: api.auth,
};
return api.client.createCommunity(form);
}
export async function getCommunity(
api: API,
2020-12-20 21:16:57 +00:00
id: number
): Promise<CommunityResponse> {
2020-12-20 21:16:57 +00:00
let form: GetCommunity = {
id,
};
return api.client.getCommunity(form);
}
export async function deleteCommunity(
api: API,
deleted: boolean,
community_id: number
): Promise<CommunityResponse> {
2020-12-20 21:16:57 +00:00
let form: DeleteCommunity = {
community_id,
deleted,
auth: api.auth,
};
return api.client.deleteCommunity(form);
}
export async function removeCommunity(
api: API,
removed: boolean,
community_id: number
): Promise<CommunityResponse> {
2020-12-20 21:16:57 +00:00
let form: RemoveCommunity = {
community_id,
removed,
auth: api.auth,
};
return api.client.removeCommunity(form);
}
export async function createPrivateMessage(
api: API,
recipient_id: number
): Promise<PrivateMessageResponse> {
let content = 'A jest test federated private message';
2020-12-20 21:16:57 +00:00
let form: CreatePrivateMessage = {
content,
recipient_id,
auth: api.auth,
};
return api.client.createPrivateMessage(form);
}
2020-12-20 21:16:57 +00:00
export async function editPrivateMessage(
api: API,
private_message_id: number
): Promise<PrivateMessageResponse> {
let updatedContent = 'A jest test federated private message edited';
2020-12-20 21:16:57 +00:00
let form: EditPrivateMessage = {
content: updatedContent,
private_message_id,
auth: api.auth,
};
return api.client.editPrivateMessage(form);
}
export async function deletePrivateMessage(
api: API,
deleted: boolean,
private_message_id: number
): Promise<PrivateMessageResponse> {
2020-12-20 21:16:57 +00:00
let form: DeletePrivateMessage = {
deleted,
private_message_id,
auth: api.auth,
};
return api.client.deletePrivateMessage(form);
}
export async function registerUser(
api: API,
username: string = randomString(5)
): Promise<LoginResponse> {
2020-12-20 21:16:57 +00:00
let form: Register = {
username,
password: 'test',
password_verify: 'test',
show_nsfw: true,
};
return api.client.register(form);
}
export async function saveUserSettingsBio(
api: API,
auth: string
): Promise<LoginResponse> {
2020-12-20 21:16:57 +00:00
let form: SaveUserSettings = {
show_nsfw: true,
theme: 'darkly',
2021-01-04 19:09:53 +00:00
default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
lang: 'en',
show_avatars: true,
send_notifications_to_email: false,
bio: 'a changed bio',
auth,
};
return saveUserSettings(api, form);
}
export async function saveUserSettings(
api: API,
2020-12-20 21:16:57 +00:00
form: SaveUserSettings
): Promise<LoginResponse> {
return api.client.saveUserSettings(form);
}
export async function getSite(
api: API,
auth: string
): Promise<GetSiteResponse> {
2020-12-20 21:16:57 +00:00
let form: GetSite = {
auth,
};
return api.client.getSite(form);
}
export async function listPrivateMessages(
api: API
): Promise<PrivateMessagesResponse> {
2020-12-20 21:16:57 +00:00
let form: GetPrivateMessages = {
auth: api.auth,
unread_only: false,
limit: 999,
};
return api.client.getPrivateMessages(form);
}
export async function unfollowRemotes(
api: API
): Promise<GetFollowedCommunitiesResponse> {
// Unfollow all remote communities
let followed = await checkFollowedCommunities(api);
let remoteFollowed = followed.communities.filter(
2020-12-20 21:16:57 +00:00
c => c.community.local == false
);
for (let cu of remoteFollowed) {
2020-12-20 21:16:57 +00:00
await followCommunity(api, false, cu.community.id);
}
let followed2 = await checkFollowedCommunities(api);
return followed2;
}
export async function followBeta(api: API): Promise<CommunityResponse> {
// Cache it
let search = await searchForBetaCommunity(api);
2020-12-20 21:16:57 +00:00
let com = search.communities.find(c => c.community.local == false);
if (com) {
let follow = await followCommunity(api, true, com.community.id);
return follow;
}
}
2020-10-01 13:32:01 +00:00
export function delay(millis: number = 500) {
2020-12-20 21:16:57 +00:00
return new Promise(resolve => setTimeout(resolve, millis));
2020-10-01 13:32:01 +00:00
}
export function longDelay() {
return delay(10000);
}
export function wrapper(form: any): string {
return JSON.stringify(form);
}
function randomString(length: number): string {
var result = '';
var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}