mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-17 23:18:27 +02:00
MESSENGER-5203 add federation check for room
This commit is contained in:
79
bwi/Federation/MXRoom+Federation.swift
Normal file
79
bwi/Federation/MXRoom+Federation.swift
Normal file
@@ -0,0 +1,79 @@
|
||||
//
|
||||
/*
|
||||
* Copyright (c) 2023 BWI GmbH
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import Foundation
|
||||
import MatrixSDK
|
||||
|
||||
@objc extension MXRoom {
|
||||
|
||||
func isRoomFederated() -> Bool {
|
||||
var isFederated: Bool = true
|
||||
state { state in
|
||||
// Search for federate flag. If the flag is not found, default value is true
|
||||
if let events = state?.stateEvents(with: .roomCreate) {
|
||||
for event in events {
|
||||
if let federateContentValue = event.wireContent["m.federate"] {
|
||||
isFederated = federateContentValue as? Bool ?? true
|
||||
}
|
||||
}
|
||||
}
|
||||
if isFederated {
|
||||
// Check Server ACLs
|
||||
if let events = state?.stateEvents(with: .roomServerACL) {
|
||||
for event in events {
|
||||
if let aclContentValue = event.wireContent["m.room.server_acl"] {
|
||||
if let aclEvent = aclContentValue as? MXEvent {
|
||||
if aclEvent.wireContent["deny"] != nil {
|
||||
isFederated = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return isFederated
|
||||
}
|
||||
|
||||
func isDMFederated() -> Bool {
|
||||
var isFederated: Bool = true
|
||||
state { state in
|
||||
// Search for federate flag. If the flag is not found, default value is true
|
||||
if let events = state?.stateEvents(with: .roomCreate) {
|
||||
for event in events {
|
||||
if let federateContentValue = event.wireContent["m.federate"] {
|
||||
isFederated = federateContentValue as? Bool ?? true
|
||||
}
|
||||
}
|
||||
}
|
||||
if isFederated {
|
||||
// Check if the user is from the same homeserver
|
||||
if let memberUserId = self.directUserId {
|
||||
if let myUserId = self.mxSession.myUser.userId {
|
||||
var memberUserIdComponents = memberUserId.components(separatedBy: ":")
|
||||
var myUserIdComponents = myUserId.components(separatedBy: ":")
|
||||
if memberUserIdComponents.count == 2 && myUserIdComponents.count == 2
|
||||
&& memberUserIdComponents.last == myUserIdComponents.last {
|
||||
isFederated = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return isFederated
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user