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:8540/api/v1'),
56 export let beta: API = {
57 client: new LemmyHttp('http://localhost:8550/api/v1'),
60 export let gamma: API = {
61 client: new LemmyHttp('http://localhost:8560/api/v1'),
64 export let delta: API = {
65 client: new LemmyHttp('http://localhost:8570/api/v1'),
68 export let epsilon: API = {
69 client: new LemmyHttp('http://localhost:8580/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 form: PostForm = {
129 return api.client.createPost(form);
132 export async function updatePost(api: API, post: Post): Promise<PostResponse> {
133 let name = 'A jest test federated post, updated';
134 let form: PostForm = {
140 return api.client.editPost(form);
143 export async function deletePost(
147 ): Promise<PostResponse> {
148 let form: DeletePostForm = {
153 return api.client.deletePost(form);
156 export async function removePost(
160 ): Promise<PostResponse> {
161 let form: RemovePostForm = {
166 return api.client.removePost(form);
169 export async function stickyPost(
173 ): Promise<PostResponse> {
174 let form: StickyPostForm = {
179 return api.client.stickyPost(form);
182 export async function lockPost(
186 ): Promise<PostResponse> {
187 let form: LockPostForm = {
192 return api.client.lockPost(form);
195 export async function searchPost(
198 ): Promise<SearchResponse> {
199 let form: SearchForm = {
201 type_: SearchType.Posts,
202 sort: SortType.TopAll,
204 return api.client.search(form);
207 export async function getPost(
210 ): Promise<GetPostResponse> {
211 let form: GetPostForm = {
214 return api.client.getPost(form);
217 export async function searchComment(
220 ): Promise<SearchResponse> {
221 let form: SearchForm = {
223 type_: SearchType.Comments,
224 sort: SortType.TopAll,
226 return api.client.search(form);
229 export async function searchForBetaCommunity(
231 ): Promise<SearchResponse> {
232 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
233 // Use short-hand search url
234 let form: SearchForm = {
235 q: '!main@lemmy-beta:8550',
236 type_: SearchType.Communities,
237 sort: SortType.TopAll,
239 return api.client.search(form);
242 export async function searchForUser(
245 ): Promise<SearchResponse> {
246 // Make sure lemmy-beta/c/main is cached on lemmy_alpha
247 // Use short-hand search url
248 let form: SearchForm = {
250 type_: SearchType.Users,
251 sort: SortType.TopAll,
253 return api.client.search(form);
256 export async function followCommunity(
260 ): Promise<CommunityResponse> {
261 let form: FollowCommunityForm = {
266 return api.client.followCommunity(form);
269 export async function checkFollowedCommunities(
271 ): Promise<GetFollowedCommunitiesResponse> {
272 let form: GetFollowedCommunitiesForm = {
275 return api.client.getFollowedCommunities(form);
278 export async function likePost(
282 ): Promise<PostResponse> {
283 let form: CreatePostLikeForm = {
289 return api.client.likePost(form);
292 export async function createComment(
296 content = 'a jest test comment'
297 ): Promise<CommentResponse> {
298 let form: CommentForm = {
304 return api.client.createComment(form);
307 export async function updateComment(
310 content = 'A jest test federated comment update'
311 ): Promise<CommentResponse> {
312 let form: CommentForm = {
317 return api.client.editComment(form);
320 export async function deleteComment(
324 ): Promise<CommentResponse> {
325 let form: DeleteCommentForm = {
330 return api.client.deleteComment(form);
333 export async function removeComment(
337 ): Promise<CommentResponse> {
338 let form: RemoveCommentForm = {
343 return api.client.removeComment(form);
346 export async function getMentions(api: API): Promise<GetUserMentionsResponse> {
347 let form: GetUserMentionsForm = {
352 return api.client.getUserMentions(form);
355 export async function likeComment(
359 ): Promise<CommentResponse> {
360 let form: CommentLikeForm = {
361 comment_id: comment.id,
365 return api.client.likeComment(form);
368 export async function createCommunity(
370 name_: string = randomString(5)
371 ): Promise<CommunityResponse> {
372 let form: CommunityForm = {
379 return api.client.createCommunity(form);
382 export async function deleteCommunity(
386 ): Promise<CommunityResponse> {
387 let form: DeleteCommunityForm = {
392 return api.client.deleteCommunity(form);
395 export async function removeCommunity(
399 ): Promise<CommunityResponse> {
400 let form: RemoveCommunityForm = {
405 return api.client.removeCommunity(form);
408 export async function createPrivateMessage(
411 ): Promise<PrivateMessageResponse> {
412 let content = 'A jest test federated private message';
413 let form: PrivateMessageForm = {
418 return api.client.createPrivateMessage(form);
421 export async function updatePrivateMessage(
424 ): Promise<PrivateMessageResponse> {
425 let updatedContent = 'A jest test federated private message edited';
426 let form: EditPrivateMessageForm = {
427 content: updatedContent,
431 return api.client.editPrivateMessage(form);
434 export async function deletePrivateMessage(
438 ): Promise<PrivateMessageResponse> {
439 let form: DeletePrivateMessageForm = {
444 return api.client.deletePrivateMessage(form);
447 export async function registerUser(
449 username: string = randomString(5)
450 ): Promise<LoginResponse> {
451 let form: RegisterForm = {
454 password_verify: 'test',
458 return api.client.register(form);
461 export async function saveUserSettingsBio(
464 ): Promise<LoginResponse> {
465 let form: UserSettingsForm = {
468 default_sort_type: Object.keys(SortType).indexOf(SortType.Active),
469 default_listing_type: Object.keys(ListingType).indexOf(ListingType.All),
472 send_notifications_to_email: false,
473 bio: 'a changed bio',
476 return api.client.saveUserSettings(form);
479 export async function getSite(
482 ): Promise<GetSiteResponse> {
483 let form: GetSiteForm = {
486 return api.client.getSite(form);
489 export async function listPrivateMessages(
491 ): Promise<PrivateMessagesResponse> {
492 let form: GetPrivateMessagesForm = {
497 return api.client.getPrivateMessages(form);
500 export async function unfollowRemotes(
502 ): Promise<GetFollowedCommunitiesResponse> {
503 // Unfollow all remote communities
504 let followed = await checkFollowedCommunities(api);
505 let remoteFollowed = followed.communities.filter(
506 c => c.community_local == false
508 for (let cu of remoteFollowed) {
509 await followCommunity(api, false, cu.community_id);
511 let followed2 = await checkFollowedCommunities(api);
515 export async function followBeta(api: API): Promise<CommunityResponse> {
516 await unfollowRemotes(api);
519 let search = await searchForBetaCommunity(api);
520 let com = search.communities.filter(c => c.local == false);
522 let follow = await followCommunity(api, true, com[0].id);
527 export const delay = (millis: number = 1500) =>
528 new Promise((resolve, _reject) => {
529 setTimeout(_ => resolve(), millis);
532 export function wrapper(form: any): string {
533 return JSON.stringify(form);
536 function randomString(length: number): string {
538 var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_';
539 var charactersLength = characters.length;
540 for (var i = 0; i < length; i++) {
541 result += characters.charAt(Math.floor(Math.random() * charactersLength));