TalkActor.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import Foundation
  6. import SwiftyAttributes
  7. @objcMembers public class TalkActor: NSObject {
  8. public var id: String?
  9. public var type: String?
  10. /// Contains the raw displayName that was used to create the TalkActor
  11. public var rawDisplayName: String
  12. /// Takes deleted users and guests into account and returns the correct displayName
  13. /// Does **not** append a potential `cloudId`
  14. public var displayName: String {
  15. if rawDisplayName.isEmpty {
  16. if isDeleted {
  17. return NSLocalizedString("Deleted user", comment: "")
  18. } else {
  19. return NSLocalizedString("Guest", comment: "")
  20. }
  21. }
  22. return rawDisplayName
  23. }
  24. /// Takes deleted users and guests into account and returns it as `secondaryLabel`
  25. /// This also appends a potential `cloudId` as `tertiaryLabel` in parentheses
  26. public var attributedDisplayName: NSMutableAttributedString {
  27. let titleLabel = displayName.withTextColor(.secondaryLabel)
  28. if let remoteServer = cloudId {
  29. let remoteServerString = " (\(String(remoteServer)))"
  30. titleLabel.append(remoteServerString.withTextColor(.tertiaryLabel))
  31. }
  32. return titleLabel
  33. }
  34. init(actorId: String? = nil, actorType: String? = nil, actorDisplayName: String? = nil) {
  35. self.id = actorId
  36. self.type = actorType
  37. self.rawDisplayName = actorDisplayName ?? ""
  38. }
  39. public var isDeleted: Bool {
  40. return id == "deleted_users" && type == "deleted_users"
  41. }
  42. public var isFederated: Bool {
  43. return type == "federated_users"
  44. }
  45. public var cloudId: String? {
  46. guard isFederated, let remoteServer = id?.split(separator: "@").last else { return nil }
  47. return String(remoteServer)
  48. }
  49. }