35 PrivateMessageResponse,
36 PrivateMessagesResponse,
37 GetPersonMentionsResponse,
47 BanFromCommunityResponse,
50 } from 'lemmy-js-client';
52 export interface API {
57 export let alpha: API = {
58 client: new LemmyHttp('http://localhost:8541'),
61 export let beta: API = {
62 client: new LemmyHttp('http://localhost:8551'),
65 export let gamma: API = {
66 client: new LemmyHttp('http://localhost:8561'),
69 export let delta: API = {
70 client: new LemmyHttp('http://localhost:8571'),
73 export let epsilon: API = {
74 client: new LemmyHttp('http://localhost:8581'),
77 export async function setupLogins() {
78 let formAlpha: Login = {
79 username_or_email: 'lemmy_alpha',
82 let resAlpha = alpha.client.login(formAlpha);
85 username_or_email: 'lemmy_beta',
88 let resBeta = beta.client.login(formBeta);
91 username_or_email: 'lemmy_gamma',
94 let resGamma = gamma.client.login(formGamma);
97 username_or_email: 'lemmy_delta',
100 let resDelta = delta.client.login(formDelta);
103 username_or_email: 'lemmy_epsilon',
106 let resEpsilon = epsilon.client.login(formEpsilon);
108 let res = await Promise.all([
116 alpha.auth = res[0].jwt;
117 beta.auth = res[1].jwt;
118 gamma.auth = res[2].jwt;
119 delta.auth = res[3].jwt;
120 epsilon.auth = res[4].jwt;
123 export async function createPost(
126 ): Promise<PostResponse> {
127 let name = randomString(5);
128 let body = randomString(10);
129 let url = 'https://google.com/';
130 let form: CreatePost = {
138 return api.client.createPost(form);
141 export async function editPost(api: API, post: Post): Promise<PostResponse> {
142 let name = 'A jest test federated post, updated';
143 let form: EditPost = {
149 return api.client.editPost(form);
152 export async function deletePost(
156 ): Promise<PostResponse> {
157 let form: DeletePost = {
162 return api.client.deletePost(form);
165 export async function removePost(
169 ): Promise<PostResponse> {
170 let form: RemovePost = {
175 return api.client.removePost(form);
178 export async function stickyPost(
182 ): Promise<PostResponse> {
183 let form: StickyPost = {
188 return api.client.stickyPost(form);
191 export async function lockPost(
195 ): Promise<PostResponse> {
196 let form: LockPost = {
201 return api.client.lockPost(form);
204 export async function searchPost(
207 ): Promise<SearchResponse> {
210 type_: SearchType.Posts,
211 sort: SortType.TopAll,
213 return api.client.search(form);
216 export async function searchPostLocal(
219 ): Promise<SearchResponse> {
222 type_: SearchType.Posts,
223 sort: SortType.TopAll,
225 return api.client.search(form);
228 export async function getPost(
231 ): Promise<GetPostResponse> {
232 let form: GetPost = {
235 return api.client.getPost(form);
238 export async function searchComment(
241 ): Promise<SearchResponse> {
244 type_: SearchType.Comments,
245 sort: SortType.TopAll,
247 return api.client.search(form);
250 export async function searchForBetaCommunity(
252 ): Promise<SearchResponse> {
253 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
254 // Use short-hand search url
256 q: '!main@lemmy-beta:8551',
257 type_: SearchType.Communities,
258 sort: SortType.TopAll,
260 return api.client.search(form);
263 export async function searchForCommunity(
266 ): Promise<SearchResponse> {
267 // Use short-hand search url
270 type_: SearchType.Communities,
271 sort: SortType.TopAll,
273 return api.client.search(form);
276 export async function searchForUser(
279 ): Promise<SearchResponse> {
280 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
281 // Use short-hand search url
284 type_: SearchType.Users,
285 sort: SortType.TopAll,
287 return api.client.search(form);
290 export async function banPersonFromSite(
294 ): Promise<BanPersonResponse> {
295 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
296 // Use short-hand search url
297 let form: BanPerson = {
303 return api.client.banPerson(form);
306 export async function banPersonFromCommunity(
309 community_id: number,
311 ): Promise<BanFromCommunityResponse> {
312 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
313 // Use short-hand search url
314 let form: BanFromCommunity = {
321 return api.client.banFromCommunity(form);
324 export async function followCommunity(
328 ): Promise<CommunityResponse> {
329 let form: FollowCommunity = {
334 return api.client.followCommunity(form);
337 export async function likePost(
341 ): Promise<PostResponse> {
342 let form: CreatePostLike = {
348 return api.client.likePost(form);
351 export async function createComment(
355 content = 'a jest test comment'
356 ): Promise<CommentResponse> {
357 let form: CreateComment = {
363 return api.client.createComment(form);
366 export async function editComment(
369 content = 'A jest test federated comment update'
370 ): Promise<CommentResponse> {
371 let form: EditComment = {
376 return api.client.editComment(form);
379 export async function deleteComment(
383 ): Promise<CommentResponse> {
384 let form: DeleteComment = {
389 return api.client.deleteComment(form);
392 export async function removeComment(
396 ): Promise<CommentResponse> {
397 let form: RemoveComment = {
402 return api.client.removeComment(form);
405 export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
406 let form: GetPersonMentions = {
411 return api.client.getPersonMentions(form);
414 export async function likeComment(
418 ): Promise<CommentResponse> {
419 let form: CreateCommentLike = {
420 comment_id: comment.id,
424 return api.client.likeComment(form);
427 export async function createCommunity(
429 name_: string = randomString(5)
430 ): Promise<CommunityResponse> {
431 let description = 'a sample description';
432 let icon = 'https://image.flaticon.com/icons/png/512/35/35896.png';
433 let banner = 'https://image.flaticon.com/icons/png/512/35/35896.png';
434 let form: CreateCommunity = {
443 return api.client.createCommunity(form);
446 export async function getCommunity(
449 ): Promise<CommunityResponse> {
450 let form: GetCommunity = {
453 return api.client.getCommunity(form);
456 export async function deleteCommunity(
460 ): Promise<CommunityResponse> {
461 let form: DeleteCommunity = {
466 return api.client.deleteCommunity(form);
469 export async function removeCommunity(
473 ): Promise<CommunityResponse> {
474 let form: RemoveCommunity = {
479 return api.client.removeCommunity(form);
482 export async function createPrivateMessage(
485 ): Promise<PrivateMessageResponse> {
486 let content = 'A jest test federated private message';
487 let form: CreatePrivateMessage = {
492 return api.client.createPrivateMessage(form);
495 export async function editPrivateMessage(
497 private_message_id: number
498 ): Promise<PrivateMessageResponse> {
499 let updatedContent = 'A jest test federated private message edited';
500 let form: EditPrivateMessage = {
501 content: updatedContent,
505 return api.client.editPrivateMessage(form);
508 export async function deletePrivateMessage(
511 private_message_id: number
512 ): Promise<PrivateMessageResponse> {
513 let form: DeletePrivateMessage = {
518 return api.client.deletePrivateMessage(form);
521 export async function registerUser(
523 username: string = randomString(5)
524 ): Promise<LoginResponse> {
525 let form: Register = {
528 password_verify: 'test',
531 return api.client.register(form);
534 export async function saveUserSettingsBio(
536 ): Promise<LoginResponse> {
537 let form: SaveUserSettings = {
540 default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
541 default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
544 send_notifications_to_email: false,
545 bio: 'a changed bio',
548 return saveUserSettings(api, form);
551 export async function saveUserSettings(
553 form: SaveUserSettings
554 ): Promise<LoginResponse> {
555 return api.client.saveUserSettings(form);
558 export async function getSite(
560 ): Promise<GetSiteResponse> {
561 let form: GetSite = {
564 return api.client.getSite(form);
567 export async function listPrivateMessages(
569 ): Promise<PrivateMessagesResponse> {
570 let form: GetPrivateMessages = {
575 return api.client.getPrivateMessages(form);
578 export async function unfollowRemotes(
580 ): Promise<GetSiteResponse> {
581 // Unfollow all remote communities
582 let site = await getSite(api);
583 let remoteFollowed = site.my_user.follows.filter(
584 c => c.community.local == false
586 for (let cu of remoteFollowed) {
587 await followCommunity(api, false, cu.community.id);
589 let siteRes = await getSite(api);
593 export async function followBeta(api: API): Promise<CommunityResponse> {
595 let search = await searchForBetaCommunity(api);
596 let com = search.communities.find(c => c.community.local == false);
598 let follow = await followCommunity(api, true, com.community.id);
603 export function delay(millis: number = 500) {
604 return new Promise(resolve => setTimeout(resolve, millis));
607 export function longDelay() {
611 export function wrapper(form: any): string {
612 return JSON.stringify(form);
615 function randomString(length: number): string {
617 var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
618 var charactersLength = characters.length;
619 for (var i = 0; i < length; i++) {
620 result += characters.charAt(Math.floor(Math.random() * charactersLength));