|
| 1 | +package social.bigbone.rx.admin |
| 2 | + |
| 3 | +import io.reactivex.rxjava3.core.Completable |
| 4 | +import io.reactivex.rxjava3.core.Single |
| 5 | +import social.bigbone.MastodonClient |
| 6 | +import social.bigbone.api.Pageable |
| 7 | +import social.bigbone.api.Range |
| 8 | +import social.bigbone.api.entity.admin.AccountOrigin |
| 9 | +import social.bigbone.api.entity.admin.AccountStatus |
| 10 | +import social.bigbone.api.entity.admin.ActionAgainstAccount |
| 11 | +import social.bigbone.api.entity.admin.AdminAccount |
| 12 | +import social.bigbone.api.method.admin.AdminAccountMethods |
| 13 | + |
| 14 | +/** |
| 15 | + * Reactive implementation of [AdminAccountMethods]. |
| 16 | + * |
| 17 | + * Perform moderation actions with accounts. |
| 18 | + * |
| 19 | + * @see <a href="https://docs.joinmastodon.org/methods/admin/accounts/">Mastodon admin/accounts API methods</a> |
| 20 | + */ |
| 21 | +class RxAdminAccountMethods(client: MastodonClient) { |
| 22 | + |
| 23 | + private val adminAccountMethods = AdminAccountMethods(client) |
| 24 | + |
| 25 | + /** |
| 26 | + * View all accounts, optionally matching certain criteria for filtering, up to 100 at a time. |
| 27 | + * |
| 28 | + * @param range optional Range for the pageable return value |
| 29 | + * @param origin Filter for [AccountOrigin.Local] or [AccountOrigin.Remote] |
| 30 | + * @param status Filter for [AccountStatus] accounts |
| 31 | + * @param permissions Filter for accounts with <code>staff</code> permissions (users that can manage reports) |
| 32 | + * @param roleIds Filter for users with these roles |
| 33 | + * @param invitedById Lookup users invited by the account with this ID |
| 34 | + * @param username Search for the given username |
| 35 | + * @param displayName Search for the given display name |
| 36 | + * @param byDomain Filter by the given domain |
| 37 | + * @param emailAddress Lookup a user with this email |
| 38 | + * @param ipAddress Lookup users with this IP address |
| 39 | + * |
| 40 | + * @see <a href="https://docs.joinmastodon.org/methods/admin/accounts/#v2">Mastodon API documentation: admin/accounts/#v2</a> |
| 41 | + */ |
| 42 | + @JvmOverloads |
| 43 | + fun viewAccounts( |
| 44 | + range: Range = Range(), |
| 45 | + origin: AccountOrigin? = null, |
| 46 | + status: AccountStatus? = null, |
| 47 | + permissions: String? = null, |
| 48 | + roleIds: List<String>? = null, |
| 49 | + invitedById: String? = null, |
| 50 | + username: String? = null, |
| 51 | + displayName: String? = null, |
| 52 | + byDomain: String? = null, |
| 53 | + emailAddress: String? = null, |
| 54 | + ipAddress: String? = null |
| 55 | + ): Single<Pageable<AdminAccount>> = Single.fromCallable { |
| 56 | + adminAccountMethods.viewAccounts( |
| 57 | + range = range, |
| 58 | + origin = origin, |
| 59 | + status = status, |
| 60 | + permissions = permissions, |
| 61 | + roleIds = roleIds, |
| 62 | + invitedById = invitedById, |
| 63 | + username = username, |
| 64 | + displayName = displayName, |
| 65 | + byDomain = byDomain, |
| 66 | + emailAddress = emailAddress, |
| 67 | + ipAddress = ipAddress |
| 68 | + ).execute() |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * View admin-level information about the given account. |
| 73 | + * |
| 74 | + * @param withId The ID of the account in the database. |
| 75 | + * |
| 76 | + * @see <a href="https://docs.joinmastodon.org/methods/admin/accounts/#get-one">Mastodon API documentation: admin/accounts/#get-one</a> |
| 77 | + */ |
| 78 | + fun viewAccount(withId: String): Single<AdminAccount> = Single.fromCallable { |
| 79 | + adminAccountMethods.viewAccount(withId = withId).execute() |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Approve the given local account if it is currently pending approval. |
| 84 | + * |
| 85 | + * @param withId The ID of the account in the database. |
| 86 | + * |
| 87 | + * @see <a href="https://docs.joinmastodon.org/methods/admin/accounts/#approve">Mastodon API documentation: admin/accounts/#approve</a> |
| 88 | + */ |
| 89 | + fun approvePendingAccount(withId: String): Single<AdminAccount> = Single.fromCallable { |
| 90 | + adminAccountMethods.approvePendingAccount(withId = withId).execute() |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Reject the given local account if it is currently pending approval. |
| 95 | + * |
| 96 | + * @param withId The ID of the account in the database. |
| 97 | + * |
| 98 | + * @see <a href="https://docs.joinmastodon.org/methods/admin/accounts/#reject">Mastodon API documentation: admin/accounts/#reject</a> |
| 99 | + */ |
| 100 | + fun rejectPendingAccount(withId: String): Single<AdminAccount> = Single.fromCallable { |
| 101 | + adminAccountMethods.rejectPendingAccount(withId = withId).execute() |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Permanently delete data for a suspended account. |
| 106 | + * |
| 107 | + * @param withId The ID of the account in the database. |
| 108 | + * |
| 109 | + * @see <a href="https://docs.joinmastodon.org/methods/admin/accounts/#delete">Mastodon API documentation: admin/accounts/#delete</a> |
| 110 | + */ |
| 111 | + fun deleteAccount(withId: String): Single<AdminAccount> = Single.fromCallable { |
| 112 | + adminAccountMethods.deleteAccount(withId = withId).execute() |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Perform an action against an account and log this action in the moderation history. |
| 117 | + * Also resolves any open reports against this account. |
| 118 | + * |
| 119 | + * @param withId The ID of the account in the database. |
| 120 | + * @param type The type of action to be taken. One of [ActionAgainstAccount]. |
| 121 | + * @param text Additional clarification for why this action was taken. |
| 122 | + * @param reportId The ID of an associated report that caused this action to be taken. |
| 123 | + * @param warningPresetId The ID of a preset warning. |
| 124 | + * @param sendEmailNotification Whether an email should be sent to the user with the above information. |
| 125 | + * |
| 126 | + * @see <a href="https://docs.joinmastodon.org/methods/admin/accounts/#action">Mastodon API documentation: admin/accounts/#action</a> |
| 127 | + */ |
| 128 | + @JvmOverloads |
| 129 | + fun performActionAgainstAccount( |
| 130 | + withId: String, |
| 131 | + type: ActionAgainstAccount, |
| 132 | + text: String? = null, |
| 133 | + reportId: String? = null, |
| 134 | + warningPresetId: String? = null, |
| 135 | + sendEmailNotification: Boolean? = null |
| 136 | + ): Completable { |
| 137 | + return Completable.fromAction { |
| 138 | + adminAccountMethods.performActionAgainstAccount( |
| 139 | + withId = withId, |
| 140 | + type = type, |
| 141 | + text = text, |
| 142 | + reportId = reportId, |
| 143 | + warningPresetId = warningPresetId, |
| 144 | + sendEmailNotification = sendEmailNotification |
| 145 | + ) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Re-enable a local account whose login is currently disabled. |
| 151 | + * |
| 152 | + * @see <a href="https://docs.joinmastodon.org/methods/admin/accounts/#enable">Mastodon API documentation: admin/accounts/#enable</a> |
| 153 | + */ |
| 154 | + fun enableDisabledAccount(withId: String): Single<AdminAccount> = Single.fromCallable { |
| 155 | + adminAccountMethods.enableDisabledAccount(withId = withId).execute() |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Unsilence an account if it is currently silenced. |
| 160 | + * |
| 161 | + * @see <a href="https://docs.joinmastodon.org/methods/admin/accounts/#unsilence">Mastodon API documentation: admin/accounts/#unsilence</a> |
| 162 | + */ |
| 163 | + fun unsilenceAccount(withId: String): Single<AdminAccount> = Single.fromCallable { |
| 164 | + adminAccountMethods.unsilenceAccount(withId = withId).execute() |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Unsuspend a currently suspended account. |
| 169 | + * |
| 170 | + * @see <a href="https://docs.joinmastodon.org/methods/admin/accounts/#unsuspend">Mastodon API documentation: admin/accounts/#unsuspend</a> |
| 171 | + */ |
| 172 | + fun unsuspendAccount(withId: String): Single<AdminAccount> = Single.fromCallable { |
| 173 | + adminAccountMethods.unsuspendAccount(withId = withId).execute() |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Stops marking an account's posts as sensitive, if it was previously flagged as sensitive. |
| 178 | + * |
| 179 | + * @see <a href="https://docs.joinmastodon.org/methods/admin/accounts/#unsensitive">Mastodon API documentation: admin/accounts/#unsensitive</a> |
| 180 | + */ |
| 181 | + fun unmarkAccountAsSensitive(withId: String): Single<AdminAccount> = Single.fromCallable { |
| 182 | + adminAccountMethods.unmarkAccountAsSensitive(withId = withId).execute() |
| 183 | + } |
| 184 | +} |
0 commit comments