35 PrivateMessageResponse,
36 PrivateMessagesResponse,
37 GetPersonMentionsResponse,
47 BanFromCommunityResponse,
50 ResolveObjectResponse,
56 ListPostReportsResponse,
58 CommentReportResponse,
60 ListCommentReportsResponse,
61 } from 'lemmy-js-client';
63 export interface API {
68 export let alpha: API = {
69 client: new LemmyHttp('http://localhost:8541'),
72 export let beta: API = {
73 client: new LemmyHttp('http://localhost:8551'),
76 export let gamma: API = {
77 client: new LemmyHttp('http://localhost:8561'),
80 export let delta: API = {
81 client: new LemmyHttp('http://localhost:8571'),
84 export let epsilon: API = {
85 client: new LemmyHttp('http://localhost:8581'),
88 const password = 'lemmylemmy'
90 export async function setupLogins() {
91 let formAlpha: Login = {
92 username_or_email: 'lemmy_alpha',
95 let resAlpha = alpha.client.login(formAlpha);
98 username_or_email: 'lemmy_beta',
101 let resBeta = beta.client.login(formBeta);
104 username_or_email: 'lemmy_gamma',
107 let resGamma = gamma.client.login(formGamma);
110 username_or_email: 'lemmy_delta',
113 let resDelta = delta.client.login(formDelta);
116 username_or_email: 'lemmy_epsilon',
119 let resEpsilon = epsilon.client.login(formEpsilon);
121 let res = await Promise.all([
129 alpha.auth = res[0].jwt;
130 beta.auth = res[1].jwt;
131 gamma.auth = res[2].jwt;
132 delta.auth = res[3].jwt;
133 epsilon.auth = res[4].jwt;
136 export async function createPost(
139 ): Promise<PostResponse> {
140 let name = randomString(5);
141 let body = randomString(10);
142 let url = 'https://google.com/';
143 let form: CreatePost = {
151 return api.client.createPost(form);
154 export async function editPost(api: API, post: Post): Promise<PostResponse> {
155 let name = 'A jest test federated post, updated';
156 let form: EditPost = {
162 return api.client.editPost(form);
165 export async function deletePost(
169 ): Promise<PostResponse> {
170 let form: DeletePost = {
175 return api.client.deletePost(form);
178 export async function removePost(
182 ): Promise<PostResponse> {
183 let form: RemovePost = {
188 return api.client.removePost(form);
191 export async function stickyPost(
195 ): Promise<PostResponse> {
196 let form: StickyPost = {
201 return api.client.stickyPost(form);
204 export async function lockPost(
208 ): Promise<PostResponse> {
209 let form: LockPost = {
214 return api.client.lockPost(form);
217 export async function resolvePost(
220 ): Promise<ResolveObjectResponse> {
221 let form: ResolveObject = {
224 return api.client.resolveObject(form);
227 export async function searchPostLocal(
230 ): Promise<SearchResponse> {
233 type_: SearchType.Posts,
234 sort: SortType.TopAll,
236 return api.client.search(form);
239 export async function getPost(
242 ): Promise<GetPostResponse> {
243 let form: GetPost = {
246 return api.client.getPost(form);
249 export async function resolveComment(
252 ): Promise<ResolveObjectResponse> {
253 let form: ResolveObject = {
256 return api.client.resolveObject(form);
259 export async function resolveBetaCommunity(
261 ): Promise<ResolveObjectResponse> {
262 // Use short-hand search url
263 let form: ResolveObject = {
264 q: '!main@lemmy-beta:8551',
266 return api.client.resolveObject(form);
269 export async function resolveCommunity(
272 ): Promise<ResolveObjectResponse> {
273 let form: ResolveObject = {
276 return api.client.resolveObject(form);
279 export async function resolvePerson(
282 ): Promise<ResolveObjectResponse> {
283 let form: ResolveObject = {
286 return api.client.resolveObject(form);
289 export async function banPersonFromSite(
293 ): Promise<BanPersonResponse> {
294 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
295 let form: BanPerson = {
301 return api.client.banPerson(form);
304 export async function banPersonFromCommunity(
307 community_id: number,
309 ): Promise<BanFromCommunityResponse> {
310 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
311 let form: BanFromCommunity = {
318 return api.client.banFromCommunity(form);
321 export async function followCommunity(
325 ): Promise<CommunityResponse> {
326 let form: FollowCommunity = {
331 return api.client.followCommunity(form);
334 export async function likePost(
338 ): Promise<PostResponse> {
339 let form: CreatePostLike = {
345 return api.client.likePost(form);
348 export async function createComment(
352 content = 'a jest test comment'
353 ): Promise<CommentResponse> {
354 let form: CreateComment = {
360 return api.client.createComment(form);
363 export async function editComment(
366 content = 'A jest test federated comment update'
367 ): Promise<CommentResponse> {
368 let form: EditComment = {
373 return api.client.editComment(form);
376 export async function deleteComment(
380 ): Promise<CommentResponse> {
381 let form: DeleteComment = {
386 return api.client.deleteComment(form);
389 export async function removeComment(
393 ): Promise<CommentResponse> {
394 let form: RemoveComment = {
399 return api.client.removeComment(form);
402 export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
403 let form: GetPersonMentions = {
408 return api.client.getPersonMentions(form);
411 export async function likeComment(
415 ): Promise<CommentResponse> {
416 let form: CreateCommentLike = {
417 comment_id: comment.id,
421 return api.client.likeComment(form);
424 export async function createCommunity(
426 name_: string = randomString(5)
427 ): Promise<CommunityResponse> {
428 let description = 'a sample description';
429 let icon = 'https://image.flaticon.com/icons/png/512/35/35896.png';
430 let banner = 'https://image.flaticon.com/icons/png/512/35/35896.png';
431 let form: CreateCommunity = {
440 return api.client.createCommunity(form);
443 export async function getCommunity(
446 ): Promise<CommunityResponse> {
447 let form: GetCommunity = {
450 return api.client.getCommunity(form);
453 export async function deleteCommunity(
457 ): Promise<CommunityResponse> {
458 let form: DeleteCommunity = {
463 return api.client.deleteCommunity(form);
466 export async function removeCommunity(
470 ): Promise<CommunityResponse> {
471 let form: RemoveCommunity = {
476 return api.client.removeCommunity(form);
479 export async function createPrivateMessage(
482 ): Promise<PrivateMessageResponse> {
483 let content = 'A jest test federated private message';
484 let form: CreatePrivateMessage = {
489 return api.client.createPrivateMessage(form);
492 export async function editPrivateMessage(
494 private_message_id: number
495 ): Promise<PrivateMessageResponse> {
496 let updatedContent = 'A jest test federated private message edited';
497 let form: EditPrivateMessage = {
498 content: updatedContent,
502 return api.client.editPrivateMessage(form);
505 export async function deletePrivateMessage(
508 private_message_id: number
509 ): Promise<PrivateMessageResponse> {
510 let form: DeletePrivateMessage = {
515 return api.client.deletePrivateMessage(form);
518 export async function registerUser(
520 username: string = randomString(5)
521 ): Promise<LoginResponse> {
522 let form: Register = {
525 password_verify: password,
528 return api.client.register(form);
531 export async function saveUserSettingsBio(
533 ): Promise<LoginResponse> {
534 let form: SaveUserSettings = {
537 default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
538 default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
541 send_notifications_to_email: false,
542 bio: 'a changed bio',
545 return saveUserSettings(api, form);
548 export async function saveUserSettings(
550 form: SaveUserSettings
551 ): Promise<LoginResponse> {
552 return api.client.saveUserSettings(form);
555 export async function getSite(
557 ): Promise<GetSiteResponse> {
558 let form: GetSite = {
561 return api.client.getSite(form);
564 export async function listPrivateMessages(
566 ): Promise<PrivateMessagesResponse> {
567 let form: GetPrivateMessages = {
572 return api.client.getPrivateMessages(form);
575 export async function unfollowRemotes(
577 ): Promise<GetSiteResponse> {
578 // Unfollow all remote communities
579 let site = await getSite(api);
580 let remoteFollowed = site.my_user.follows.filter(
581 c => c.community.local == false
583 for (let cu of remoteFollowed) {
584 await followCommunity(api, false, cu.community.id);
586 let siteRes = await getSite(api);
590 export async function followBeta(api: API): Promise<CommunityResponse> {
591 let betaCommunity = (await resolveBetaCommunity(api)).community;
593 let follow = await followCommunity(api, true, betaCommunity.community.id);
598 export async function reportPost(
602 ): Promise<PostReportResponse> {
603 let form: CreatePostReport = {
608 return api.client.createPostReport(form);
611 export async function listPostReports(api: API): Promise<ListPostReportsResponse> {
612 let form: ListPostReports = {
615 return api.client.listPostReports(form);
618 export async function reportComment(
622 ): Promise<CommentReportResponse> {
623 let form: CreateCommentReport = {
628 return api.client.createCommentReport(form);
631 export async function listCommentReports(api: API): Promise<ListCommentReportsResponse> {
632 let form: ListCommentReports = {
635 return api.client.listCommentReports(form);
638 export function delay(millis: number = 500) {
639 return new Promise(resolve => setTimeout(resolve, millis));
642 export function longDelay() {
646 export function wrapper(form: any): string {
647 return JSON.stringify(form);
650 export function randomString(length: number): string {
652 var characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
653 var charactersLength = characters.length;
654 for (var i = 0; i < length; i++) {
655 result += characters.charAt(Math.floor(Math.random() * charactersLength));