Skip to content
This repository has been archived by the owner on Jun 7, 2020. It is now read-only.

[NEW] Allow user with required permissions to remove member from room #1637

Merged
merged 22 commits into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
523ae33
Rename Subscription*Request -> Room*Request
cardoso Apr 27, 2018
8915fb7
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat.i…
cardoso May 4, 2018
16abb59
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat.i…
cardoso May 16, 2018
ddc0276
Update tests
cardoso May 16, 2018
3afa3d2
Add remove user option to user action sheet
cardoso May 17, 2018
f8e015a
Fix not mapping subscription roles correctly
cardoso May 18, 2018
504d579
Add RoomRemoveUserRequest
cardoso May 18, 2018
68c4f28
Rename + Add RoomKickRequestSpec
cardoso May 18, 2018
c9039e0
Update tests
cardoso May 18, 2018
4534b4b
Show localized API errors when calling rooms.kick API
cardoso May 21, 2018
e87be00
Make roles fetching more testable
cardoso May 21, 2018
12195f2
Add test for role fetching
cardoso May 21, 2018
d207fb8
Localize error alert title
cardoso May 21, 2018
25378ca
Fix code style
cardoso May 21, 2018
a767090
Fix not refreshing screen after removing user
cardoso May 22, 2018
78f8ffb
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat.i…
cardoso May 22, 2018
81df919
Fix not refreshing members list title after removing
cardoso May 22, 2018
19c1e80
Don't allow user to open action sheet on himself
cardoso May 25, 2018
9e973b3
Merge branch 'develop' of https://github.com/RocketChat/Rocket.Chat.i…
cardoso May 25, 2018
dc2cdf9
Merge branch 'develop' into feat/kick_user.1319
rafaelks May 25, 2018
44f8862
Fixed a mistake on conflicts solution
rafaelks May 25, 2018
d3a7ff3
Fixed testing
rafaelks May 25, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 98 additions & 70 deletions Rocket.Chat.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Rocket.Chat/API/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ final class API: APIFetcher {

var authToken: String?
var userId: String?
var language: String?

static let userAgent: String = {
let info = Bundle.main.infoDictionary
Expand Down
11 changes: 6 additions & 5 deletions Rocket.Chat/API/APIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ extension APIRequest {
request.addValue(contentType, forHTTPHeaderField: "Content-Type")
request.addValue(API.userAgent, forHTTPHeaderField: "User-Agent")

if let token = api.authToken {
request.addValue(token, forHTTPHeaderField: "X-Auth-Token")
func addValueIfSome(_ value: String?, forHTTPHeaderField field: String) {
guard let value = value else { return }
request.addValue(value, forHTTPHeaderField: field)
}

if let userId = api.userId {
request.addValue(userId, forHTTPHeaderField: "X-User-Id")
}
addValueIfSome(api.authToken, forHTTPHeaderField: "X-Auth-Token")
addValueIfSome(api.userId, forHTTPHeaderField: "X-User-Id")
addValueIfSome(api.language, forHTTPHeaderField: "Accept-Language")

return request
}
Expand Down
31 changes: 31 additions & 0 deletions Rocket.Chat/API/Clients/SubscriptionsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,37 @@ struct SubscriptionsClient: APIClient {
}
}

func fetchRoles(subscription: Subscription, realm: Realm? = Realm.current, completion: (() -> Void)? = nil) {
let rid = subscription.rid
let rolesRequest = RoomRolesRequest(roomName: subscription.name, subscriptionType: subscription.type)

let currentRealm = realm

api.fetch(rolesRequest) { result in
switch result {
case .resource(let resource):
if let subscription = Subscription.find(rid: rid, realm: currentRealm) {
try? currentRealm?.write {
let subscriptionCopy = Subscription(value: subscription)

subscriptionCopy.usersRoles.removeAll()
resource.roomRoles?.forEach { role in
subscriptionCopy.usersRoles.append(role)
}

currentRealm?.add(subscriptionCopy, update: true)
}

completion?()
}

// Fail silently
case .error(let error):
print(error)
}
}
}

// fallback for servers < 0.60.0
func fetchSubscriptionsFallback(updatedSince: Date?, realm: Realm? = Realm.current, completion: (() -> Void)?) {
var params: [[String: Any]] = []
Expand Down
24 changes: 24 additions & 0 deletions Rocket.Chat/API/Requests/Base/ResourceWithError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// ResourceWithError.swift
// Rocket.Chat
//
// Created by Matheus Cardoso on 5/18/18.
// Copyright © 2018 Rocket.Chat. All rights reserved.
//

import Foundation

protocol ResourceWithError {
var error: String? { get }
var errorType: String? { get }
}

extension ResourceWithError where Self: APIResource {
var error: String? {
return raw?["error"].string
}

var errorType: String? {
return raw?["errorType"].string
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SubscriptionCreateRequest.swift
// RoomCreateRequest.swift
// Rocket.Chat
//
// Created by Bruno Macabeus Aquino on 28/09/17.
Expand All @@ -11,7 +11,7 @@

import Foundation

enum SubscriptionCreateType {
enum RoomCreateType {
case channel
case group

Expand All @@ -25,20 +25,20 @@ enum SubscriptionCreateType {
}
}

final class SubscriptionCreateRequest: APIRequest {
typealias APIResourceType = SubscriptionCreateResource
final class RoomCreateRequest: APIRequest {
typealias APIResourceType = RoomCreateResource

let method: HTTPMethod = .post
var path: String {
return type.path
}

let name: String
let type: SubscriptionCreateType
let type: RoomCreateType
let members: [String]
let readOnly: Bool

init(name: String, type: SubscriptionCreateType, members: [String] = [], readOnly: Bool = false) {
init(name: String, type: RoomCreateType, members: [String] = [], readOnly: Bool = false) {
self.name = name
self.type = type
self.members = members
Expand All @@ -56,7 +56,7 @@ final class SubscriptionCreateRequest: APIRequest {
}
}

class SubscriptionCreateResource: APIResource {
class RoomCreateResource: APIResource {
var success: Bool? {
return raw?["success"].boolValue
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SubscriptionFilesRequest.swift
// RoomFilesRequest.swift
// Rocket.Chat
//
// Created by Filipe Alvarenga on 11/04/18.
Expand All @@ -23,8 +23,8 @@ fileprivate extension SubscriptionType {
}
}

final class SubscriptionFilesRequest: APIRequest {
typealias APIResourceType = SubscriptionFilesResource
final class RoomFilesRequest: APIRequest {
typealias APIResourceType = RoomFilesResource

var path: String {
return type.path
Expand All @@ -41,7 +41,7 @@ final class SubscriptionFilesRequest: APIRequest {
}
}

final class SubscriptionFilesResource: APIResource {
final class RoomFilesResource: APIResource {
var files: [File]? {
let realm = Realm.current
return raw?["files"].arrayValue.map {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SubscriptionInfoRequest.swift
// RoomInfoRequest.swift
// Rocket.Chat
//
// Created by Matheus Cardoso on 9/19/17.
Expand All @@ -22,8 +22,8 @@ fileprivate extension SubscriptionType {
}
}

final class SubscriptionInfoRequest: APIRequest {
typealias APIResourceType = SubscriptionInfoResource
final class RoomInfoRequest: APIRequest {
typealias APIResourceType = RoomInfoResource

var path: String {
return type.path
Expand All @@ -50,7 +50,7 @@ final class SubscriptionInfoRequest: APIRequest {
}
}

final class SubscriptionInfoResource: APIResource {
final class RoomInfoResource: APIResource {
var channel: JSON? {
return raw?["channel"]
}
Expand Down
57 changes: 57 additions & 0 deletions Rocket.Chat/API/Requests/Room/RoomKickRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// RoomKickRequest.swift
// Rocket.Chat
//
// Created by Matheus Cardoso on 5/18/18.
// Copyright © 2018 Rocket.Chat. All rights reserved.
//

import SwiftyJSON

fileprivate extension RoomType {
var path: String {
switch self {
case .channel:
return "/api/v1/channels.kick"
case .group:
return "/api/v1/groups.kick"
case .directMessage:
return ""
}
}
}

final class RoomKickRequest: APIRequest {
typealias APIResourceType = RoomKickResource
let requiredVersion = Version(0, 48, 0)

let method: HTTPMethod = .post
var path: String {
return roomType.path
}

let roomId: String
let roomType: RoomType
let userId: String

init(roomId: String, roomType: RoomType, userId: String) {
self.roomId = roomId
self.roomType = roomType
self.userId = userId
}

func body() -> Data? {
let body = JSON([
"roomId": roomId,
"userId": userId
])

return body.rawString()?.data(using: .utf8)
}
}

final class RoomKickResource: APIResource, ResourceWithError {
var success: Bool? {
return raw?["success"].boolValue
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SubscriptionMembersRequest.swift
// RoomMembersRequest.swift
// Rocket.Chat
//
// Created by Matheus Cardoso on 9/21/17.
Expand All @@ -22,8 +22,8 @@ fileprivate extension SubscriptionType {
}
}

final class SubscriptionMembersRequest: APIRequest {
typealias APIResourceType = SubscriptionMembersResource
final class RoomMembersRequest: APIRequest {
typealias APIResourceType = RoomMembersResource

let requiredVersion = Version(0, 59, 0)

Expand Down Expand Up @@ -52,7 +52,7 @@ final class SubscriptionMembersRequest: APIRequest {
}
}

class SubscriptionMembersResource: APIResource {
class RoomMembersResource: APIResource {
var members: [User?]? {
return raw?["members"].arrayValue.map {
let user = User()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SubscriptionMentionsRequest.swift
// RoomMentionsRequest.swift
// Rocket.Chat
//
// Created by Filipe Alvarenga on 03/04/18.
Expand All @@ -9,8 +9,8 @@
import SwiftyJSON
import Foundation

final class SubscriptionMentionsRequest: APIRequest {
typealias APIResourceType = SubscriptionMentionsResource
final class RoomMentionsRequest: APIRequest {
typealias APIResourceType = RoomMentionsResource

let requiredVersion = Version(0, 63, 0)
let path = "/api/v1/channels.getAllUserMentionsByChannel"
Expand All @@ -24,7 +24,7 @@ final class SubscriptionMentionsRequest: APIRequest {
}
}

final class SubscriptionMentionsResource: APIResource {
final class RoomMentionsResource: APIResource {
var messages: [Message]? {
return raw?["mentions"].arrayValue.map {
let message = Message()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SubscriptionMessagesRequest.swift
// RoomMessagesRequest.swift
// Rocket.Chat
//
// Created by Matheus Cardoso on 9/21/17.
Expand All @@ -22,8 +22,8 @@ fileprivate extension SubscriptionType {
}
}

final class SubscriptionMessagesRequest: APIRequest {
typealias APIResourceType = SubscriptionMessagesResource
final class RoomMessagesRequest: APIRequest {
typealias APIResourceType = RoomMessagesResource

var path: String {
return type.path
Expand Down Expand Up @@ -60,7 +60,7 @@ final class SubscriptionMessagesRequest: APIRequest {
}
}

final class SubscriptionMessagesResource: APIResource {
final class RoomMessagesResource: APIResource {
var messages: [Message?]? {
return raw?["messages"].arrayValue.map {
let message = Message()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SubscriptionRolesRequest.swift
// RoomRolesRequest.swift
// Rocket.Chat
//
// Created by Rafael Kellermann Streit on 11/05/18.
Expand All @@ -22,8 +22,8 @@ fileprivate extension SubscriptionType {
}
}

final class SubscriptionRolesRequest: APIRequest {
typealias APIResourceType = SubscriptionRolesResource
final class RoomRolesRequest: APIRequest {
typealias APIResourceType = RoomRolesResource

let requiredVersion = Version(0, 64, 2)

Expand All @@ -42,11 +42,11 @@ final class SubscriptionRolesRequest: APIRequest {
}
}

final class SubscriptionRolesResource: APIResource {
var subscriptionRoles: [SubscriptionRoles]? {
final class RoomRolesResource: APIResource {
var roomRoles: [RoomRoles]? {
guard let realm = Realm.current else { return nil }
return raw?["roles"].arrayValue.map {
let object = SubscriptionRoles()
let object = RoomRoles()
object.user = User.getOrCreate(realm: realm, values: $0["u"], updates: nil)
object.roles.append(contentsOf: $0["roles"].arrayValue.compactMap({ $0.string }))
return object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ extension ChatViewController: ChatMessageCellProtocol, UserActionSheetPresenter

// on select reactor

controller.reactorListView.selectedReactor = { username, rect in
controller.reactorListView.selectedReactor = { [weak self] username, rect in
guard let user = User.find(username: username) else {
return
}

controller.presentActionSheetForUser(user, source: (controller.view, rect))
controller.presentActionSheetForUser(user, subscription: self?.subscription, source: (controller.view, rect))
}
}

Expand All @@ -74,7 +74,7 @@ extension ChatViewController: ChatMessageCellProtocol, UserActionSheetPresenter

func handleUsernameTapMessageCell(_ message: Message, view: UIView, recognizer: UIGestureRecognizer) {
guard let user = message.user else { return }
presentActionSheetForUser(user, source: (view, nil))
presentActionSheetForUser(user, subscription: subscription, source: (view, nil))
}

func openURL(url: URL) {
Expand Down
Loading