X-Git-Url: http://these/git/?a=blobdiff_plain;f=api_tests%2Fsrc%2Fshared.ts;h=e4306c94c0de6d94b22b83987aa0ffea7e1d64a8;hb=f9351b651217cd437801e5f579891f5ad815e05d;hp=6aa5cfc4e3a2504651ca90dea269d1eaee1d0826;hpb=6c8a5723f8eeb0c8de1887c732e20ceaf81f7574;p=lemmy.git diff --git a/api_tests/src/shared.ts b/api_tests/src/shared.ts index 6aa5cfc4..e4306c94 100644 --- a/api_tests/src/shared.ts +++ b/api_tests/src/shared.ts @@ -1,102 +1,126 @@ -import { - LoginForm, - LoginResponse, - Post, - PostForm, - Comment, - DeletePostForm, - RemovePostForm, - StickyPostForm, - LockPostForm, - PostResponse, - SearchResponse, - FollowCommunityForm, - CommunityResponse, - GetFollowedCommunitiesResponse, - GetPostResponse, - RegisterForm, - CommentForm, - DeleteCommentForm, - RemoveCommentForm, - SearchForm, - CommentResponse, - CommunityForm, - DeleteCommunityForm, - RemoveCommunityForm, - GetUserMentionsForm, - CommentLikeForm, - CreatePostLikeForm, - PrivateMessageForm, - EditPrivateMessageForm, - DeletePrivateMessageForm, - GetFollowedCommunitiesForm, - GetPrivateMessagesForm, - GetSiteForm, - GetPostForm, - PrivateMessageResponse, - PrivateMessagesResponse, - GetUserMentionsResponse, - UserSettingsForm, - SortType, - ListingType, - GetSiteResponse, - SearchType, - LemmyHttp, -} from 'lemmy-js-client'; +import { LemmyHttp } from "lemmy-js-client"; +import { CreatePost } from "lemmy-js-client/dist/types/CreatePost"; +import { DeletePost } from "lemmy-js-client/dist/types/DeletePost"; +import { EditPost } from "lemmy-js-client/dist/types/EditPost"; +import { EditSite } from "lemmy-js-client/dist/types/EditSite"; +import { FeaturePost } from "lemmy-js-client/dist/types/FeaturePost"; +import { GetComments } from "lemmy-js-client/dist/types/GetComments"; +import { GetCommentsResponse } from "lemmy-js-client/dist/types/GetCommentsResponse"; +import { GetPost } from "lemmy-js-client/dist/types/GetPost"; +import { GetPostResponse } from "lemmy-js-client/dist/types/GetPostResponse"; +import { LockPost } from "lemmy-js-client/dist/types/LockPost"; +import { Login } from "lemmy-js-client/dist/types/Login"; +import { Post } from "lemmy-js-client/dist/types/Post"; +import { PostResponse } from "lemmy-js-client/dist/types/PostResponse"; +import { RemovePost } from "lemmy-js-client/dist/types/RemovePost"; +import { ResolveObject } from "lemmy-js-client/dist/types/ResolveObject"; +import { ResolveObjectResponse } from "lemmy-js-client/dist/types/ResolveObjectResponse"; +import { Search } from "lemmy-js-client/dist/types/Search"; +import { SearchResponse } from "lemmy-js-client/dist/types/SearchResponse"; +import { Comment } from "lemmy-js-client/dist/types/Comment"; +import { BanPersonResponse } from "lemmy-js-client/dist/types/BanPersonResponse"; +import { BanPerson } from "lemmy-js-client/dist/types/BanPerson"; +import { BanFromCommunityResponse } from "lemmy-js-client/dist/types/BanFromCommunityResponse"; +import { BanFromCommunity } from "lemmy-js-client/dist/types/BanFromCommunity"; +import { CommunityResponse } from "lemmy-js-client/dist/types/CommunityResponse"; +import { FollowCommunity } from "lemmy-js-client/dist/types/FollowCommunity"; +import { CreatePostLike } from "lemmy-js-client/dist/types/CreatePostLike"; +import { CommentResponse } from "lemmy-js-client/dist/types/CommentResponse"; +import { CreateComment } from "lemmy-js-client/dist/types/CreateComment"; +import { EditComment } from "lemmy-js-client/dist/types/EditComment"; +import { DeleteComment } from "lemmy-js-client/dist/types/DeleteComment"; +import { RemoveComment } from "lemmy-js-client/dist/types/RemoveComment"; +import { GetPersonMentionsResponse } from "lemmy-js-client/dist/types/GetPersonMentionsResponse"; +import { GetPersonMentions } from "lemmy-js-client/dist/types/GetPersonMentions"; +import { CreateCommentLike } from "lemmy-js-client/dist/types/CreateCommentLike"; +import { CreateCommunity } from "lemmy-js-client/dist/types/CreateCommunity"; +import { GetCommunity } from "lemmy-js-client/dist/types/GetCommunity"; +import { DeleteCommunity } from "lemmy-js-client/dist/types/DeleteCommunity"; +import { RemoveCommunity } from "lemmy-js-client/dist/types/RemoveCommunity"; +import { PrivateMessageResponse } from "lemmy-js-client/dist/types/PrivateMessageResponse"; +import { CreatePrivateMessage } from "lemmy-js-client/dist/types/CreatePrivateMessage"; +import { EditPrivateMessage } from "lemmy-js-client/dist/types/EditPrivateMessage"; +import { DeletePrivateMessage } from "lemmy-js-client/dist/types/DeletePrivateMessage"; +import { LoginResponse } from "lemmy-js-client/dist/types/LoginResponse"; +import { Register } from "lemmy-js-client/dist/types/Register"; +import { SaveUserSettings } from "lemmy-js-client/dist/types/SaveUserSettings"; +import { DeleteAccount } from "lemmy-js-client/dist/types/DeleteAccount"; +import { GetSiteResponse } from "lemmy-js-client/dist/types/GetSiteResponse"; +import { DeleteAccountResponse } from "lemmy-js-client/dist/types/DeleteAccountResponse"; +import { GetSite } from "lemmy-js-client/dist/types/GetSite"; +import { PrivateMessagesResponse } from "lemmy-js-client/dist/types/PrivateMessagesResponse"; +import { GetPrivateMessages } from "lemmy-js-client/dist/types/GetPrivateMessages"; +import { PostReportResponse } from "lemmy-js-client/dist/types/PostReportResponse"; +import { CreatePostReport } from "lemmy-js-client/dist/types/CreatePostReport"; +import { ListPostReportsResponse } from "lemmy-js-client/dist/types/ListPostReportsResponse"; +import { ListPostReports } from "lemmy-js-client/dist/types/ListPostReports"; +import { CommentReportResponse } from "lemmy-js-client/dist/types/CommentReportResponse"; +import { CreateCommentReport } from "lemmy-js-client/dist/types/CreateCommentReport"; +import { ListCommentReportsResponse } from "lemmy-js-client/dist/types/ListCommentReportsResponse"; +import { ListCommentReports } from "lemmy-js-client/dist/types/ListCommentReports"; +import { GetPersonDetailsResponse } from "lemmy-js-client/dist/types/GetPersonDetailsResponse"; +import { GetPersonDetails } from "lemmy-js-client/dist/types/GetPersonDetails"; export interface API { client: LemmyHttp; - auth?: string; + auth: string; } export let alpha: API = { - client: new LemmyHttp('http://localhost:8541/api/v1'), + client: new LemmyHttp("http://127.0.0.1:8541"), + auth: "", }; export let beta: API = { - client: new LemmyHttp('http://localhost:8551/api/v1'), + client: new LemmyHttp("http://127.0.0.1:8551"), + auth: "", }; export let gamma: API = { - client: new LemmyHttp('http://localhost:8561/api/v1'), + client: new LemmyHttp("http://127.0.0.1:8561"), + auth: "", }; export let delta: API = { - client: new LemmyHttp('http://localhost:8571/api/v1'), + client: new LemmyHttp("http://127.0.0.1:8571"), + auth: "", }; export let epsilon: API = { - client: new LemmyHttp('http://localhost:8581/api/v1'), + client: new LemmyHttp("http://127.0.0.1:8581"), + auth: "", }; +const password = "lemmylemmy"; + export async function setupLogins() { - let formAlpha: LoginForm = { - username_or_email: 'lemmy_alpha', - password: 'lemmy', + let formAlpha: Login = { + username_or_email: "lemmy_alpha", + password, }; let resAlpha = alpha.client.login(formAlpha); - let formBeta = { - username_or_email: 'lemmy_beta', - password: 'lemmy', + let formBeta: Login = { + username_or_email: "lemmy_beta", + password, }; let resBeta = beta.client.login(formBeta); - let formGamma = { - username_or_email: 'lemmy_gamma', - password: 'lemmy', + let formGamma: Login = { + username_or_email: "lemmy_gamma", + password, }; let resGamma = gamma.client.login(formGamma); - let formDelta = { - username_or_email: 'lemmy_delta', - password: 'lemmy', + let formDelta: Login = { + username_or_email: "lemmy_delta", + password, }; let resDelta = delta.client.login(formDelta); - let formEpsilon = { - username_or_email: 'lemmy_epsilon', - password: 'lemmy', + let formEpsilon: Login = { + username_or_email: "lemmy_epsilon", + password, }; let resEpsilon = epsilon.client.login(formEpsilon); @@ -108,38 +132,92 @@ export async function setupLogins() { resEpsilon, ]); - alpha.auth = res[0].jwt; - beta.auth = res[1].jwt; - gamma.auth = res[2].jwt; - delta.auth = res[3].jwt; - epsilon.auth = res[4].jwt; + alpha.auth = res[0].jwt ?? ""; + beta.auth = res[1].jwt ?? ""; + gamma.auth = res[2].jwt ?? ""; + delta.auth = res[3].jwt ?? ""; + epsilon.auth = res[4].jwt ?? ""; + + // Registration applications are now enabled by default, need to disable them + let editSiteForm: EditSite = { + registration_mode: "Open", + rate_limit_message: 999, + rate_limit_post: 999, + rate_limit_register: 999, + rate_limit_image: 999, + rate_limit_comment: 999, + rate_limit_search: 999, + auth: "", + }; + + // Set the blocks and auths for each + editSiteForm.auth = alpha.auth; + editSiteForm.allowed_instances = [ + "lemmy-beta", + "lemmy-gamma", + "lemmy-delta", + "lemmy-epsilon", + ]; + await alpha.client.editSite(editSiteForm); + + editSiteForm.auth = beta.auth; + editSiteForm.allowed_instances = [ + "lemmy-alpha", + "lemmy-gamma", + "lemmy-delta", + "lemmy-epsilon", + ]; + await beta.client.editSite(editSiteForm); + + editSiteForm.auth = gamma.auth; + editSiteForm.allowed_instances = [ + "lemmy-alpha", + "lemmy-beta", + "lemmy-delta", + "lemmy-epsilon", + ]; + await gamma.client.editSite(editSiteForm); + + editSiteForm.allowed_instances = ["lemmy-beta"]; + editSiteForm.auth = delta.auth; + await delta.client.editSite(editSiteForm); + + editSiteForm.auth = epsilon.auth; + editSiteForm.allowed_instances = []; + editSiteForm.blocked_instances = ["lemmy-alpha"]; + await epsilon.client.editSite(editSiteForm); + + // Create the main alpha/beta communities + // Ignore thrown errors of duplicates + try { + await createCommunity(alpha, "main"); + await createCommunity(beta, "main"); + } catch (_) {} } export async function createPost( api: API, - community_id: number + community_id: number, ): Promise { - let name = 'A jest test post'; - let body = 'Some body'; - let url = 'https://google.com/'; - let form: PostForm = { + let name = randomString(5); + let body = randomString(10); + let url = "https://google.com/"; + let form: CreatePost = { name, url, body, auth: api.auth, community_id, - nsfw: false, }; return api.client.createPost(form); } -export async function updatePost(api: API, post: Post): Promise { - let name = 'A jest test federated post, updated'; - let form: PostForm = { +export async function editPost(api: API, post: Post): Promise { + let name = "A jest test federated post, updated"; + let form: EditPost = { name, - edit_id: post.id, + post_id: post.id, auth: api.auth, - nsfw: false, }; return api.client.editPost(form); } @@ -147,10 +225,10 @@ export async function updatePost(api: API, post: Post): Promise { export async function deletePost( api: API, deleted: boolean, - post: Post + post: Post, ): Promise { - let form: DeletePostForm = { - edit_id: post.id, + let form: DeletePost = { + post_id: post.id, deleted: deleted, auth: api.auth, }; @@ -160,122 +238,174 @@ export async function deletePost( export async function removePost( api: API, removed: boolean, - post: Post + post: Post, ): Promise { - let form: RemovePostForm = { - edit_id: post.id, + let form: RemovePost = { + post_id: post.id, removed, auth: api.auth, }; return api.client.removePost(form); } -export async function stickyPost( +export async function featurePost( api: API, - stickied: boolean, - post: Post + featured: boolean, + post: Post, ): Promise { - let form: StickyPostForm = { - edit_id: post.id, - stickied, + let form: FeaturePost = { + post_id: post.id, + featured, + feature_type: "Community", auth: api.auth, }; - return api.client.stickyPost(form); + return api.client.featurePost(form); } export async function lockPost( api: API, locked: boolean, - post: Post + post: Post, ): Promise { - let form: LockPostForm = { - edit_id: post.id, + let form: LockPost = { + post_id: post.id, locked, auth: api.auth, }; return api.client.lockPost(form); } -export async function searchPost( +export async function resolvePost( api: API, - post: Post -): Promise { - let form: SearchForm = { + post: Post, +): Promise { + let form: ResolveObject = { q: post.ap_id, - type_: SearchType.Posts, - sort: SortType.TopAll, + auth: api.auth, + }; + return api.client.resolveObject(form); +} + +export async function searchPostLocal( + api: API, + post: Post, +): Promise { + let form: Search = { + q: post.name, + type_: "Posts", + sort: "TopAll", + auth: api.auth, }; return api.client.search(form); } export async function getPost( api: API, - post_id: number + post_id: number, ): Promise { - let form: GetPostForm = { + let form: GetPost = { id: post_id, + auth: api.auth, }; return api.client.getPost(form); } -export async function searchComment( +export async function getComments( api: API, - comment: Comment -): Promise { - let form: SearchForm = { + post_id: number, +): Promise { + let form: GetComments = { + post_id: post_id, + type_: "All", + sort: "New", + auth: api.auth, + }; + return api.client.getComments(form); +} + +export async function resolveComment( + api: API, + comment: Comment, +): Promise { + let form: ResolveObject = { q: comment.ap_id, - type_: SearchType.Comments, - sort: SortType.TopAll, + auth: api.auth, }; - return api.client.search(form); + return api.client.resolveObject(form); } -export async function searchForBetaCommunity( - api: API -): Promise { - // Make sure lemmy-beta/c/main is cached on lemmy_alpha +export async function resolveBetaCommunity( + api: API, +): Promise { // Use short-hand search url - let form: SearchForm = { - q: '!main@lemmy-beta:8551', - type_: SearchType.Communities, - sort: SortType.TopAll, + let form: ResolveObject = { + q: "!main@lemmy-beta:8551", + auth: api.auth, }; - return api.client.search(form); + return api.client.resolveObject(form); } -export async function searchForCommunity( +export async function resolveCommunity( api: API, q: string, -): Promise { - // Use short-hand search url - let form: SearchForm = { +): Promise { + let form: ResolveObject = { q, - type_: SearchType.Communities, - sort: SortType.TopAll, + auth: api.auth, }; - return api.client.search(form); + return api.client.resolveObject(form); } -export async function searchForUser( +export async function resolvePerson( api: API, - apShortname: string -): Promise { - // Make sure lemmy-beta/c/main is cached on lemmy_alpha - // Use short-hand search url - let form: SearchForm = { + apShortname: string, +): Promise { + let form: ResolveObject = { q: apShortname, - type_: SearchType.Users, - sort: SortType.TopAll, + auth: api.auth, }; - return api.client.search(form); + return api.client.resolveObject(form); +} + +export async function banPersonFromSite( + api: API, + person_id: number, + ban: boolean, + remove_data: boolean, +): Promise { + // Make sure lemmy-beta/c/main is cached on lemmy_alpha + let form: BanPerson = { + person_id, + ban, + remove_data: remove_data, + auth: api.auth, + }; + return api.client.banPerson(form); +} + +export async function banPersonFromCommunity( + api: API, + person_id: number, + community_id: number, + remove_data: boolean, + ban: boolean, +): Promise { + let form: BanFromCommunity = { + person_id, + community_id, + remove_data: remove_data, + ban, + auth: api.auth, + }; + return api.client.banFromCommunity(form); } export async function followCommunity( api: API, follow: boolean, - community_id: number + community_id: number, ): Promise { - let form: FollowCommunityForm = { + let form: FollowCommunity = { community_id, follow, auth: api.auth, @@ -283,21 +413,12 @@ export async function followCommunity( return api.client.followCommunity(form); } -export async function checkFollowedCommunities( - api: API -): Promise { - let form: GetFollowedCommunitiesForm = { - auth: api.auth, - }; - return api.client.getFollowedCommunities(form); -} - export async function likePost( api: API, score: number, - post: Post + post: Post, ): Promise { - let form: CreatePostLikeForm = { + let form: CreatePostLike = { post_id: post.id, score: score, auth: api.auth, @@ -310,9 +431,9 @@ export async function createComment( api: API, post_id: number, parent_id?: number, - content = 'a jest test comment' + content = "a jest test comment", ): Promise { - let form: CommentForm = { + let form: CreateComment = { content, post_id, parent_id, @@ -321,14 +442,14 @@ export async function createComment( return api.client.createComment(form); } -export async function updateComment( +export async function editComment( api: API, - edit_id: number, - content = 'A jest test federated comment update' + comment_id: number, + content = "A jest test federated comment update", ): Promise { - let form: CommentForm = { + let form: EditComment = { content, - edit_id, + comment_id, auth: api.auth, }; return api.client.editComment(form); @@ -337,10 +458,10 @@ export async function updateComment( export async function deleteComment( api: API, deleted: boolean, - edit_id: number + comment_id: number, ): Promise { - let form: DeleteCommentForm = { - edit_id, + let form: DeleteComment = { + comment_id, deleted, auth: api.auth, }; @@ -350,31 +471,33 @@ export async function deleteComment( export async function removeComment( api: API, removed: boolean, - edit_id: number + comment_id: number, ): Promise { - let form: RemoveCommentForm = { - edit_id, + let form: RemoveComment = { + comment_id, removed, auth: api.auth, }; return api.client.removeComment(form); } -export async function getMentions(api: API): Promise { - let form: GetUserMentionsForm = { - sort: SortType.New, +export async function getMentions( + api: API, +): Promise { + let form: GetPersonMentions = { + sort: "New", unread_only: false, auth: api.auth, }; - return api.client.getUserMentions(form); + return api.client.getPersonMentions(form); } export async function likeComment( api: API, score: number, - comment: Comment + comment: Comment, ): Promise { - let form: CommentLikeForm = { + let form: CreateCommentLike = { comment_id: comment.id, score, auth: api.auth, @@ -384,31 +507,36 @@ export async function likeComment( export async function createCommunity( api: API, - name_: string = randomString(5) + name_: string = randomString(5), ): Promise { - let description = 'a sample description'; - let icon = 'https://image.flaticon.com/icons/png/512/35/35896.png'; - let banner = 'https://image.flaticon.com/icons/png/512/35/35896.png'; - let form: CommunityForm = { + let description = "a sample description"; + let form: CreateCommunity = { name: name_, title: name_, description, - icon, - banner, - category_id: 1, - nsfw: false, auth: api.auth, }; return api.client.createCommunity(form); } +export async function getCommunity( + api: API, + id: number, +): Promise { + let form: GetCommunity = { + id, + auth: api.auth, + }; + return api.client.getCommunity(form); +} + export async function deleteCommunity( api: API, deleted: boolean, - edit_id: number + community_id: number, ): Promise { - let form: DeleteCommunityForm = { - edit_id, + let form: DeleteCommunity = { + community_id, deleted, auth: api.auth, }; @@ -418,10 +546,10 @@ export async function deleteCommunity( export async function removeCommunity( api: API, removed: boolean, - edit_id: number + community_id: number, ): Promise { - let form: RemoveCommunityForm = { - edit_id, + let form: RemoveCommunity = { + community_id, removed, auth: api.auth, }; @@ -430,10 +558,10 @@ export async function removeCommunity( export async function createPrivateMessage( api: API, - recipient_id: number + recipient_id: number, ): Promise { - let content = 'A jest test federated private message'; - let form: PrivateMessageForm = { + let content = "A jest test federated private message"; + let form: CreatePrivateMessage = { content, recipient_id, auth: api.auth, @@ -441,14 +569,14 @@ export async function createPrivateMessage( return api.client.createPrivateMessage(form); } -export async function updatePrivateMessage( +export async function editPrivateMessage( api: API, - edit_id: number + private_message_id: number, ): Promise { - let updatedContent = 'A jest test federated private message edited'; - let form: EditPrivateMessageForm = { + let updatedContent = "A jest test federated private message edited"; + let form: EditPrivateMessage = { content: updatedContent, - edit_id, + private_message_id, auth: api.auth, }; return api.client.editPrivateMessage(form); @@ -457,11 +585,11 @@ export async function updatePrivateMessage( export async function deletePrivateMessage( api: API, deleted: boolean, - edit_id: number + private_message_id: number, ): Promise { - let form: DeletePrivateMessageForm = { + let form: DeletePrivateMessage = { deleted, - edit_id, + private_message_id, auth: api.auth, }; return api.client.deletePrivateMessage(form); @@ -469,95 +597,168 @@ export async function deletePrivateMessage( export async function registerUser( api: API, - username: string = randomString(5) + username: string = randomString(5), ): Promise { - let form: RegisterForm = { + let form: Register = { username, - password: 'test', - password_verify: 'test', - admin: false, + password, + password_verify: password, show_nsfw: true, }; return api.client.register(form); } -export async function saveUserSettingsBio( - api: API, - auth: string -): Promise { - let form: UserSettingsForm = { +export async function saveUserSettingsBio(api: API): Promise { + let form: SaveUserSettings = { show_nsfw: true, - theme: 'darkly', - default_sort_type: Object.keys(SortType).indexOf(SortType.Active), - default_listing_type: Object.keys(ListingType).indexOf(ListingType.All), - lang: 'en', + blur_nsfw: false, + auto_expand: true, + theme: "darkly", + default_sort_type: "Active", + default_listing_type: "All", + interface_language: "en", show_avatars: true, send_notifications_to_email: false, - bio: 'a changed bio', - auth, + bio: "a changed bio", + auth: api.auth, }; return saveUserSettings(api, form); } +export async function saveUserSettingsFederated( + api: API, +): Promise { + let avatar = "https://image.flaticon.com/icons/png/512/35/35896.png"; + let banner = "https://image.flaticon.com/icons/png/512/36/35896.png"; + let bio = "a changed bio"; + let form: SaveUserSettings = { + show_nsfw: false, + blur_nsfw: true, + auto_expand: false, + default_sort_type: "Hot", + default_listing_type: "All", + interface_language: "", + avatar, + banner, + display_name: "user321", + show_avatars: false, + send_notifications_to_email: false, + bio, + auth: api.auth, + }; + return await saveUserSettings(alpha, form); +} + export async function saveUserSettings( api: API, - form: UserSettingsForm + form: SaveUserSettings, ): Promise { return api.client.saveUserSettings(form); } - -export async function getSite( +export async function getPersonDetails( api: API, - auth: string -): Promise { - let form: GetSiteForm = { - auth, + person_id: number, +): Promise { + let form: GetPersonDetails = { + auth: api.auth, + person_id: person_id, + }; + return api.client.getPersonDetails(form); +} + +export async function deleteUser(api: API): Promise { + let form: DeleteAccount = { + auth: api.auth, + password, + }; + return api.client.deleteAccount(form); +} + +export async function getSite(api: API): Promise { + let form: GetSite = { + auth: api.auth, }; return api.client.getSite(form); } export async function listPrivateMessages( - api: API + api: API, ): Promise { - let form: GetPrivateMessagesForm = { + let form: GetPrivateMessages = { auth: api.auth, unread_only: false, - limit: 999, }; return api.client.getPrivateMessages(form); } -export async function unfollowRemotes( - api: API -): Promise { +export async function unfollowRemotes(api: API): Promise { // Unfollow all remote communities - let followed = await checkFollowedCommunities(api); - let remoteFollowed = followed.communities.filter( - c => c.community_local == false - ); + let site = await getSite(api); + let remoteFollowed = + site.my_user?.follows.filter(c => c.community.local == false) ?? []; for (let cu of remoteFollowed) { - await followCommunity(api, false, cu.community_id); + await followCommunity(api, false, cu.community.id); } - let followed2 = await checkFollowedCommunities(api); - return followed2; + let siteRes = await getSite(api); + return siteRes; } export async function followBeta(api: API): Promise { - await unfollowRemotes(api); - - // Cache it - let search = await searchForBetaCommunity(api); - let com = search.communities.filter(c => c.local == false); - if (com[0]) { - let follow = await followCommunity(api, true, com[0].id); + let betaCommunity = (await resolveBetaCommunity(api)).community; + if (betaCommunity) { + let follow = await followCommunity(api, true, betaCommunity.community.id); return follow; + } else { + return Promise.reject("no community worked"); } } -export function delay(millis: number = 500) { - return new Promise((resolve, _reject) => { - setTimeout(_ => resolve(), millis); - }); +export async function reportPost( + api: API, + post_id: number, + reason: string, +): Promise { + let form: CreatePostReport = { + post_id, + reason, + auth: api.auth, + }; + return api.client.createPostReport(form); +} + +export async function listPostReports( + api: API, +): Promise { + let form: ListPostReports = { + auth: api.auth, + }; + return api.client.listPostReports(form); +} + +export async function reportComment( + api: API, + comment_id: number, + reason: string, +): Promise { + let form: CreateCommentReport = { + comment_id, + reason, + auth: api.auth, + }; + return api.client.createCommentReport(form); +} + +export async function listCommentReports( + api: API, +): Promise { + let form: ListCommentReports = { + auth: api.auth, + }; + return api.client.listCommentReports(form); +} + +export function delay(millis = 500) { + return new Promise(resolve => setTimeout(resolve, millis)); } export function longDelay() { @@ -568,12 +769,32 @@ export function wrapper(form: any): string { return JSON.stringify(form); } -function randomString(length: number): string { - var result = ''; - var characters = 'abcdefghijklmnopqrstuvwxyz0123456789_'; +export function randomString(length: number): string { + var result = ""; + var characters = + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; var charactersLength = characters.length; for (var i = 0; i < length; i++) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } + +export async function unfollows() { + await unfollowRemotes(alpha); + await unfollowRemotes(gamma); + await unfollowRemotes(delta); + await unfollowRemotes(epsilon); +} + +export function getCommentParentId(comment: Comment): number | undefined { + let split = comment.path.split("."); + // remove the 0 + split.shift(); + + if (split.length > 1) { + return Number(split[split.length - 2]); + } else { + return undefined; + } +}