Skip to content

Commit

Permalink
1.3 (#1053)
Browse files Browse the repository at this point in the history
Co-authored-by: Bosco Ho <[email protected]>
Co-authored-by: Sjmarf <[email protected]>
Co-authored-by: Sumeet <[email protected]>
Co-authored-by: Zach McGaughey <[email protected]>
  • Loading branch information
5 people authored May 5, 2024
1 parent ceec486 commit e506ba1
Show file tree
Hide file tree
Showing 380 changed files with 17,235 additions and 3,621 deletions.
1,017 changes: 915 additions & 102 deletions Mlem.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@
"version" : "2.1.0"
}
},
{
"identity" : "swiftui-introspect",
"kind" : "remoteSourceControl",
"location" : "https://github.com/siteline/SwiftUI-Introspect",
"state" : {
"revision" : "0cd2a5a5895306bc21d54a2254302d24a9a571e4",
"version" : "1.1.3"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
Expand Down
24 changes: 24 additions & 0 deletions Mlem/API/APIClient/APIClient+Comment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,28 @@ extension APIClient {
let request = try CreateCommentReportRequest(session: session, commentId: id, reason: reason)
return try await perform(request: request)
}

func removeComment(id: Int, shouldRemove: Bool, reason: String?) async throws -> CommentResponse {
let request = try RemoveCommentRequest(session: session, commentId: id, removed: shouldRemove, reason: reason)
return try await perform(request: request)
}

func purgeComment(id: Int, reason: String?) async throws -> SuccessResponse {
let request = try PurgeCommentRequest(session: session, commentId: id, reason: reason)
return try await perform(request: request)
}

func getCommentLikes(
id: Int,
page: Int,
limit: Int?
) async throws -> APIListCommentLikesResponse {
let request = try ListCommentLikesRequest(
session: session,
commentId: id,
page: page,
limit: limit
)
return try await perform(request: request)
}
}
70 changes: 70 additions & 0 deletions Mlem/API/APIClient/APIClient+Community.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,74 @@ extension APIClient {

return try await perform(request: request)
}

/// Bans the given user from the given community, provided the current user has permissions to do so
/// - Parameters:
/// - userId: id of the user to ban
/// - communityId: id of the community to ban the user from
/// - ban: true if user should be banned, false if unbanned
/// - removeData: true if user data should be removed from community, false or nil otherwise
/// - reason: reason for ban
/// - expires: expiration date of ban (unit???)
/// - Returns: updated ban status of user (true if banned, false otherwise)
func banFromCommunity(
userId: Int,
communityId: Int,
ban: Bool,
removeData: Bool? = nil,
reason: String? = nil,
expires: Int? = nil
) async throws -> Bool {
let request = try BanFromCommunityRequest(
session: session,
communityId: communityId,
personId: userId,
ban: ban,
removeData: removeData,
reason: reason,
expires: expires
)

let response = try await perform(request: request)

return response.banned
}

func removeCommunity(id: Int, shouldRemove: Bool, reason: String?) async throws -> CommunityResponse {
let request = try RemoveCommunityRequest(session: session, communityId: id, removed: shouldRemove, reason: reason)
return try await perform(request: request)
}

func purgeCommunity(id: Int, reason: String?) async throws -> SuccessResponse {
let request = try PurgeCommunityRequest(session: session, communityId: id, reason: reason)
return try await perform(request: request)
}

/// Adds or removes the given user from the mod list of the given community
/// - Parameters:
/// - of: id of user to add/remove
/// - in: id of the community to add/remove to/from
/// - status: whether to add (true) or remove (false)
/// - Returns: new list of moderators
/// - Throws: error upon failed update
func updateModStatus(of userId: Int, in communityId: Int, status: Bool) async throws -> [UserModel] {
// perform update
let request = try AddModToCommunityRequest(
session: session,
communityId: communityId,
personId: userId,
added: status
)
print(request)
let response = try await perform(request: request)

// validate response
let isMod = response.moderators.contains(where: { $0.moderator.id == userId })
if isMod != status {
throw ContextualError(title: "Failed to add mod", underlyingError: APIClientError.unexpectedResponse)
}

// return new mod list
return response.moderators.map { UserModel(from: $0.moderator) }
}
}
93 changes: 93 additions & 0 deletions Mlem/API/APIClient/APIClient+Instance.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// APIClient+Instance.swift
// Mlem
//
// Created by Eric Andrews on 2024-03-11.
//

import Foundation

extension APIClient {
// swiftlint:disable:next function_body_length
func getModlog(
for instanceUrl: URL? = nil,
modPersonId: Int? = nil,
communityId: Int? = nil,
page: Int,
limit: Int,
type: APIModlogActionType? = nil,
otherPersonId: Int? = nil
) async throws -> [ModlogEntry] {
var useSession: APISession

if let instanceUrl {
useSession = .unauthenticated(instanceUrl.appending(path: "/api/v3"))
} else {
useSession = session
}

#if DEBUG
if let host = instanceUrl?.host(), ["lemmy-alpha", "lemmy-beta", "lemmy-delta"].contains(host) {
useSession = .unauthenticated(.init(string: "http://localhost:8536/api/v3")!)
}
#endif

let request = try GetModlogRequest(
session: useSession,
modPersonId: nil,
communityId: communityId,
page: page,
limit: limit,
type_: type,
otherPersonId: otherPersonId
)

let response = try await perform(request: request)

func isAdmin(for actorId: URL?) -> Bool {
// can only view admin actions if the logged in user is an admin and the modlog is sourced from their instance
siteInformation.isAdmin && actorId?.host() == siteInformation.instance?.url.host()
}

func canViewRemovedPost(in community: APICommunity) -> Bool {
siteInformation.isMod(communityActorId: community.actorId) ||
isAdmin(for: community.actorId)
}

var ret: [ModlogEntry] = .init()
ret.append(contentsOf: response.removedPosts.map { ModlogEntry(
from: $0,
canViewRemovedPost: canViewRemovedPost(in: $0.community)
) })
ret.append(contentsOf: response.lockedPosts.map { ModlogEntry(from: $0) })
ret.append(contentsOf: response.featuredPosts.map { ModlogEntry(from: $0) })
ret.append(contentsOf: response.removedComments.map { ModlogEntry(from: $0) })
ret.append(contentsOf: response.removedCommunities.map { ModlogEntry(
from: $0,
canViewRemovedCommunity: isAdmin(for: $0.community.actorId)
) })
ret.append(contentsOf: response.bannedFromCommunity.map { ModlogEntry(from: $0) })
ret.append(contentsOf: response.banned.map { ModlogEntry(from: $0) })
ret.append(contentsOf: response.addedToCommunity.map { ModlogEntry(from: $0) })
ret.append(contentsOf: response.transferredToCommunity.map { ModlogEntry(from: $0) })
ret.append(contentsOf: response.added.map { ModlogEntry(from: $0) })
ret.append(contentsOf: response.adminPurgedPersons.map { ModlogEntry(from: $0) })
ret.append(contentsOf: response.adminPurgedCommunities.map { ModlogEntry(from: $0) })
ret.append(contentsOf: response.adminPurgedPosts.map { ModlogEntry(from: $0) })
ret.append(contentsOf: response.adminPurgedComments.map { ModlogEntry(from: $0) })
ret.append(contentsOf: response.hiddenCommunities.map { ModlogEntry(from: $0) })

return ret.sorted(by: { $0.date > $1.date })
}

@discardableResult
func blockSite(id: Int, shouldBlock: Bool) async throws -> BlockInstanceResponse {
let request = try BlockInstanceRequest(
session: session,
instanceId: id,
block: shouldBlock
)

return try await perform(request: request)
}
}
Loading

0 comments on commit e506ba1

Please sign in to comment.