15 GetFollowedCommunitiesResponse,
30 EditPrivateMessageForm,
31 DeletePrivateMessageForm,
32 GetFollowedCommunitiesForm,
33 GetPrivateMessagesForm,
36 PrivateMessageResponse,
37 PrivateMessagesResponse,
38 GetUserMentionsResponse,
45 } from 'lemmy-js-client';
47 export interface API {
52 export let alpha: API = {
53 client: new LemmyHttp('http://localhost:8541/api/v1'),
56 export let beta: API = {
57 client: new LemmyHttp('http://localhost:8551/api/v1'),
60 export let gamma: API = {
61 client: new LemmyHttp('http://localhost:8561/api/v1'),
64 export let delta: API = {
65 client: new LemmyHttp('http://localhost:8571/api/v1'),
68 export let epsilon: API = {
69 client: new LemmyHttp('http://localhost:8581/api/v1'),
72 export async function setupLogins() {
73 let formAlpha: LoginForm = {
74 username_or_email: 'lemmy_alpha',
77 let resAlpha = alpha.client.login(formAlpha);
80 username_or_email: 'lemmy_beta',
83 let resBeta = beta.client.login(formBeta);
86 username_or_email: 'lemmy_gamma',
89 let resGamma = gamma.client.login(formGamma);
92 username_or_email: 'lemmy_delta',
95 let resDelta = delta.client.login(formDelta);
98 username_or_email: 'lemmy_epsilon',
101 let resEpsilon = epsilon.client.login(formEpsilon);
103 let res = await Promise.all([
111 alpha.auth = res[0].jwt;
112 beta.auth = res[1].jwt;
113 gamma.auth = res[2].jwt;
114 delta.auth = res[3].jwt;
115 epsilon.auth = res[4].jwt;
118 export async function createPost(
121 ): Promise<PostResponse> {
122 let name = 'A jest test post';
123 let body = 'Some body';
124 let url = 'https://google.com/';
125 let form: PostForm = {
133 return api.client.createPost(form);
136 export async function updatePost(api: API, post: Post): Promise<PostResponse> {
137 let name = 'A jest test federated post, updated';
138 let form: PostForm = {
144 return api.client.editPost(form);
147 export async function deletePost(
151 ): Promise<PostResponse> {
152 let form: DeletePostForm = {
157 return api.client.deletePost(form);
160 export async function removePost(
164 ): Promise<PostResponse> {
165 let form: RemovePostForm = {
170 return api.client.removePost(form);
173 export async function stickyPost(
177 ): Promise<PostResponse> {
178 let form: StickyPostForm = {
183 return api.client.stickyPost(form);
186 export async function lockPost(
190 ): Promise<PostResponse> {
191 let form: LockPostForm = {
196 return api.client.lockPost(form);
199 export async function searchPost(
202 ): Promise<SearchResponse> {
203 let form: SearchForm = {
205 type_: SearchType.Posts,
206 sort: SortType.TopAll,
208 return api.client.search(form);
211 export async function getPost(
214 ): Promise<GetPostResponse> {
215 let form: GetPostForm = {
218 return api.client.getPost(form);
221 export async function searchComment(
224 ): Promise<SearchResponse> {
225 let form: SearchForm = {
227 type_: SearchType.Comments,
228 sort: SortType.TopAll,
230 return api.client.search(form);
233 export async function searchForBetaCommunity(
235 ): Promise<SearchResponse> {
236 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
237 // Use short-hand search url
238 let form: SearchForm = {
239 q: '!main@lemmy-beta:8551',
240 type_: SearchType.Communities,
241 sort: SortType.TopAll,
243 return api.client.search(form);
246 export async function searchForCommunity(
249 ): Promise<SearchResponse> {
250 // Use short-hand search url
251 let form: SearchForm = {
253 type_: SearchType.Communities,
254 sort: SortType.TopAll,
256 return api.client.search(form);
259 export async function searchForUser(
262 ): Promise<SearchResponse> {
263 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
264 // Use short-hand search url
265 let form: SearchForm = {
267 type_: SearchType.Users,
268 sort: SortType.TopAll,
270 return api.client.search(form);
273 export async function followCommunity(
277 ): Promise<CommunityResponse> {
278 let form: FollowCommunityForm = {
283 return api.client.followCommunity(form);
286 export async function checkFollowedCommunities(
288 ): Promise<GetFollowedCommunitiesResponse> {
289 let form: GetFollowedCommunitiesForm = {
292 return api.client.getFollowedCommunities(form);
295 export async function likePost(
299 ): Promise<PostResponse> {
300 let form: CreatePostLikeForm = {
306 return api.client.likePost(form);
309 export async function createComment(
313 content = 'a jest test comment'
314 ): Promise<CommentResponse> {
315 let form: CommentForm = {
321 return api.client.createComment(form);
324 export async function updateComment(
327 content = 'A jest test federated comment update'
328 ): Promise<CommentResponse> {
329 let form: CommentForm = {
334 return api.client.editComment(form);
337 export async function deleteComment(
341 ): Promise<CommentResponse> {
342 let form: DeleteCommentForm = {
347 return api.client.deleteComment(form);
350 export async function removeComment(
354 ): Promise<CommentResponse> {
355 let form: RemoveCommentForm = {
360 return api.client.removeComment(form);
363 export async function getMentions(api: API): Promise<GetUserMentionsResponse> {
364 let form: GetUserMentionsForm = {
369 return api.client.getUserMentions(form);
372 export async function likeComment(
376 ): Promise<CommentResponse> {
377 let form: CommentLikeForm = {
378 comment_id: comment.id,
382 return api.client.likeComment(form);
385 export async function createCommunity(
387 name_: string = randomString(5)
388 ): Promise<CommunityResponse> {
389 let description = 'a sample description';
390 let icon = 'https://image.flaticon.com/icons/png/512/35/35896.png';
391 let banner = 'https://image.flaticon.com/icons/png/512/35/35896.png';
392 let form: CommunityForm = {
402 return api.client.createCommunity(form);
405 export async function deleteCommunity(
409 ): Promise<CommunityResponse> {
410 let form: DeleteCommunityForm = {
415 return api.client.deleteCommunity(form);
418 export async function removeCommunity(
422 ): Promise<CommunityResponse> {
423 let form: RemoveCommunityForm = {
428 return api.client.removeCommunity(form);
431 export async function createPrivateMessage(
434 ): Promise<PrivateMessageResponse> {
435 let content = 'A jest test federated private message';
436 let form: PrivateMessageForm = {
441 return api.client.createPrivateMessage(form);
444 export async function updatePrivateMessage(
447 ): Promise<PrivateMessageResponse> {
448 let updatedContent = 'A jest test federated private message edited';
449 let form: EditPrivateMessageForm = {
450 content: updatedContent,
454 return api.client.editPrivateMessage(form);
457 export async function deletePrivateMessage(
461 ): Promise<PrivateMessageResponse> {
462 let form: DeletePrivateMessageForm = {
467 return api.client.deletePrivateMessage(form);
470 export async function registerUser(
472 username: string = randomString(5)
473 ): Promise<LoginResponse> {
474 let form: RegisterForm = {
477 password_verify: 'test',
481 return api.client.register(form);
484 export async function saveUserSettingsBio(
487 ): Promise<LoginResponse> {
488 let form: UserSettingsForm = {
491 default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
492 default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
495 send_notifications_to_email: false,
496 bio: 'a changed bio',
499 return api.client.saveUserSettings(form);
502 export async function getSite(
505 ): Promise<GetSiteResponse> {
506 let form: GetSiteForm = {
509 return api.client.getSite(form);
512 export async function listPrivateMessages(
514 ): Promise<PrivateMessagesResponse> {
515 let form: GetPrivateMessagesForm = {
520 return api.client.getPrivateMessages(form);
523 export async function unfollowRemotes(
525 ): Promise<GetFollowedCommunitiesResponse> {
526 // Unfollow all remote communities
527 let followed = await checkFollowedCommunities(api);
528 let remoteFollowed = followed.communities.filter(
529 c => c.community_local == false
531 for (let cu of remoteFollowed) {
532 await followCommunity(api, false, cu.community_id);
534 let followed2 = await checkFollowedCommunities(api);
538 export async function followBeta(api: API): Promise<CommunityResponse> {
539 await unfollowRemotes(api);
542 let search = await searchForBetaCommunity(api);
543 let com = search.communities.filter(c => c.local == false);
545 let follow = await followCommunity(api, true, com[0].id);
550 export const delay = (millis: number = 1500) =>
551 new Promise((resolve, _reject) => {
552 setTimeout(_ => resolve(), millis);
555 export function wrapper(form: any): string {
556 return JSON.stringify(form);
559 function randomString(length: number): string {
561 var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
562 var charactersLength = characters.length;
563 for (var i = 0; i < length; i++) {
564 result += characters.charAt(Math.floor(Math.random() * charactersLength));