14 FollowCommunityResponse,
36 PrivateMessageResponse,
37 PrivateMessagesResponse,
38 GetPersonMentionsResponse,
48 BanFromCommunityResponse,
51 ResolveObjectResponse,
56 ListPostReportsResponse,
58 CommentReportResponse,
60 ListCommentReportsResponse,
63 } from 'lemmy-js-client';
65 export interface API {
70 export let alpha: API = {
71 client: new LemmyHttp('http://127.0.0.1:8541'),
74 export let beta: API = {
75 client: new LemmyHttp('http://127.0.0.1:8551'),
78 export let gamma: API = {
79 client: new LemmyHttp('http://127.0.0.1:8561'),
82 export let delta: API = {
83 client: new LemmyHttp('http://127.0.0.1:8571'),
86 export let epsilon: API = {
87 client: new LemmyHttp('http://127.0.0.1:8581'),
90 const password = 'lemmylemmy'
92 export async function setupLogins() {
93 let formAlpha: Login = {
94 username_or_email: 'lemmy_alpha',
97 let resAlpha = alpha.client.login(formAlpha);
100 username_or_email: 'lemmy_beta',
103 let resBeta = beta.client.login(formBeta);
106 username_or_email: 'lemmy_gamma',
109 let resGamma = gamma.client.login(formGamma);
112 username_or_email: 'lemmy_delta',
115 let resDelta = delta.client.login(formDelta);
118 username_or_email: 'lemmy_epsilon',
121 let resEpsilon = epsilon.client.login(formEpsilon);
123 let res = await Promise.all([
131 alpha.auth = res[0].jwt;
132 beta.auth = res[1].jwt;
133 gamma.auth = res[2].jwt;
134 delta.auth = res[3].jwt;
135 epsilon.auth = res[4].jwt;
137 // regstration applications are now enabled by default, need to disable them
138 await alpha.client.editSite({ require_application: false, auth: alpha.auth});
139 await beta.client.editSite({ require_application: false, auth: beta.auth});
140 await gamma.client.editSite({ require_application: false, auth: gamma.auth});
141 await delta.client.editSite({ require_application: false, auth: delta.auth});
142 await epsilon.client.editSite({ require_application: false, auth: epsilon.auth});
145 export async function createPost(
148 ): Promise<PostResponse> {
149 let name = randomString(5);
150 let body = randomString(10);
151 let url = 'https://google.com/';
152 let form: CreatePost = {
160 return api.client.createPost(form);
163 export async function editPost(api: API, post: Post): Promise<PostResponse> {
164 let name = 'A jest test federated post, updated';
165 let form: EditPost = {
171 return api.client.editPost(form);
174 export async function deletePost(
178 ): Promise<PostResponse> {
179 let form: DeletePost = {
184 return api.client.deletePost(form);
187 export async function removePost(
191 ): Promise<PostResponse> {
192 let form: RemovePost = {
197 return api.client.removePost(form);
200 export async function stickyPost(
204 ): Promise<PostResponse> {
205 let form: StickyPost = {
210 return api.client.stickyPost(form);
213 export async function lockPost(
217 ): Promise<PostResponse> {
218 let form: LockPost = {
223 return api.client.lockPost(form);
226 export async function resolvePost(
229 ): Promise<ResolveObjectResponse> {
230 let form: ResolveObject = {
233 return api.client.resolveObject(form);
236 export async function searchPostLocal(
239 ): Promise<SearchResponse> {
242 type_: SearchType.Posts,
243 sort: SortType.TopAll,
245 return api.client.search(form);
248 export async function getPost(
251 ): Promise<GetPostResponse> {
252 let form: GetPost = {
255 return api.client.getPost(form);
258 export async function resolveComment(
261 ): Promise<ResolveObjectResponse> {
262 let form: ResolveObject = {
265 return api.client.resolveObject(form);
268 export async function resolveBetaCommunity(
270 ): Promise<ResolveObjectResponse> {
271 // Use short-hand search url
272 let form: ResolveObject = {
273 q: '!main@lemmy-beta:8551',
275 return api.client.resolveObject(form);
278 export async function resolveCommunity(
281 ): Promise<ResolveObjectResponse> {
282 let form: ResolveObject = {
285 return api.client.resolveObject(form);
288 export async function resolvePerson(
291 ): Promise<ResolveObjectResponse> {
292 let form: ResolveObject = {
295 return api.client.resolveObject(form);
298 export async function banPersonFromSite(
303 ): Promise<BanPersonResponse> {
304 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
305 let form: BanPerson = {
311 return api.client.banPerson(form);
314 export async function banPersonFromCommunity(
317 community_id: number,
318 remove_data: boolean,
320 ): Promise<BanFromCommunityResponse> {
321 let form: BanFromCommunity = {
328 return api.client.banFromCommunity(form);
331 export async function followCommunity(
335 ): Promise<FollowCommunityResponse> {
336 let form: FollowCommunity = {
341 return api.client.followCommunity(form);
344 export async function likePost(
348 ): Promise<PostResponse> {
349 let form: CreatePostLike = {
355 return api.client.likePost(form);
358 export async function createComment(
362 content = 'a jest test comment'
363 ): Promise<CommentResponse> {
364 let form: CreateComment = {
370 return api.client.createComment(form);
373 export async function editComment(
376 content = 'A jest test federated comment update'
377 ): Promise<CommentResponse> {
378 let form: EditComment = {
383 return api.client.editComment(form);
386 export async function deleteComment(
390 ): Promise<CommentResponse> {
391 let form: DeleteComment = {
396 return api.client.deleteComment(form);
399 export async function removeComment(
403 ): Promise<CommentResponse> {
404 let form: RemoveComment = {
409 return api.client.removeComment(form);
412 export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
413 let form: GetPersonMentions = {
418 return api.client.getPersonMentions(form);
421 export async function likeComment(
425 ): Promise<CommentResponse> {
426 let form: CreateCommentLike = {
427 comment_id: comment.id,
431 return api.client.likeComment(form);
434 export async function createCommunity(
436 name_: string = randomString(5)
437 ): Promise<CommunityResponse> {
438 let description = 'a sample description';
439 let form: CreateCommunity = {
446 return api.client.createCommunity(form);
449 export async function getCommunity(
452 ): Promise<CommunityResponse> {
453 let form: GetCommunity = {
456 return api.client.getCommunity(form);
459 export async function deleteCommunity(
463 ): Promise<CommunityResponse> {
464 let form: DeleteCommunity = {
469 return api.client.deleteCommunity(form);
472 export async function removeCommunity(
476 ): Promise<CommunityResponse> {
477 let form: RemoveCommunity = {
482 return api.client.removeCommunity(form);
485 export async function createPrivateMessage(
488 ): Promise<PrivateMessageResponse> {
489 let content = 'A jest test federated private message';
490 let form: CreatePrivateMessage = {
495 return api.client.createPrivateMessage(form);
498 export async function editPrivateMessage(
500 private_message_id: number
501 ): Promise<PrivateMessageResponse> {
502 let updatedContent = 'A jest test federated private message edited';
503 let form: EditPrivateMessage = {
504 content: updatedContent,
508 return api.client.editPrivateMessage(form);
511 export async function deletePrivateMessage(
514 private_message_id: number
515 ): Promise<PrivateMessageResponse> {
516 let form: DeletePrivateMessage = {
521 return api.client.deletePrivateMessage(form);
524 export async function registerUser(
526 username: string = randomString(5)
527 ): Promise<LoginResponse> {
528 let form: Register = {
531 password_verify: password,
534 return api.client.register(form);
537 export async function saveUserSettingsBio(
539 ): Promise<LoginResponse> {
540 let form: SaveUserSettings = {
543 default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
544 default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
547 send_notifications_to_email: false,
548 bio: 'a changed bio',
551 return saveUserSettings(api, form);
554 export async function saveUserSettings(
556 form: SaveUserSettings
557 ): Promise<LoginResponse> {
558 return api.client.saveUserSettings(form);
561 export async function deleteUser(
563 ): Promise<DeleteAccountResponse> {
564 let form: DeleteAccount = {
568 return api.client.deleteAccount(form);
571 export async function getSite(
573 ): Promise<GetSiteResponse> {
574 let form: GetSite = {
577 return api.client.getSite(form);
580 export async function listPrivateMessages(
582 ): Promise<PrivateMessagesResponse> {
583 let form: GetPrivateMessages = {
588 return api.client.getPrivateMessages(form);
591 export async function unfollowRemotes(
593 ): Promise<GetSiteResponse> {
594 // Unfollow all remote communities
595 let site = await getSite(api);
596 let remoteFollowed = site.my_user.follows.filter(
597 c => c.community.local == false
599 for (let cu of remoteFollowed) {
600 await followCommunity(api, false, cu.community.id);
602 let siteRes = await getSite(api);
606 export async function followBeta(api: API): Promise<FollowCommunityResponse> {
607 let betaCommunity = (await resolveBetaCommunity(api)).community;
609 let follow = await followCommunity(api, true, betaCommunity.community.id);
614 export async function reportPost(
618 ): Promise<PostReportResponse> {
619 let form: CreatePostReport = {
624 return api.client.createPostReport(form);
627 export async function listPostReports(api: API): Promise<ListPostReportsResponse> {
628 let form: ListPostReports = {
631 return api.client.listPostReports(form);
634 export async function reportComment(
638 ): Promise<CommentReportResponse> {
639 let form: CreateCommentReport = {
644 return api.client.createCommentReport(form);
647 export async function listCommentReports(api: API): Promise<ListCommentReportsResponse> {
648 let form: ListCommentReports = {
651 return api.client.listCommentReports(form);
654 export function delay(millis: number = 500) {
655 return new Promise(resolve => setTimeout(resolve, millis));
658 export function longDelay() {
662 export function wrapper(form: any): string {
663 return JSON.stringify(form);
666 export function randomString(length: number): string {
668 var characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
669 var charactersLength = characters.length;
670 for (var i = 0; i < length; i++) {
671 result += characters.charAt(Math.floor(Math.random() * charactersLength));
676 export async function unfollows() {
677 await unfollowRemotes(alpha);
678 await unfollowRemotes(gamma);
679 await unfollowRemotes(delta);
680 await unfollowRemotes(epsilon);