MentionSuggestion.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import Foundation
  6. @objcMembers public class MentionSuggestion: NSObject {
  7. public var id: String
  8. public var label: String
  9. public var source: String
  10. public var mentionId: String?
  11. public var userStatus: String?
  12. public var details: String?
  13. init(dictionary: [String: Any]) {
  14. self.id = dictionary["id"] as? String ?? ""
  15. self.label = dictionary["label"] as? String ?? ""
  16. self.source = dictionary["source"] as? String ?? ""
  17. self.mentionId = dictionary["mentionId"] as? String
  18. self.userStatus = dictionary["status"] as? String
  19. self.details = dictionary["details"] as? String
  20. super.init()
  21. }
  22. func getIdForChat() -> String {
  23. // When we support a mentionId serverside, we use that
  24. var id = self.mentionId ?? self.id
  25. if id.contains("/") || id.rangeOfCharacter(from: .whitespaces) != nil {
  26. id = "\"\(id)\""
  27. }
  28. return id
  29. }
  30. func getIdForAvatar() -> String {
  31. // For avatars we always want to use the actorId, so ignore a potential serverside mentionId here
  32. return self.id
  33. }
  34. func asMessageParameter() -> NCMessageParameter {
  35. let messageParameter = NCMessageParameter()
  36. messageParameter.parameterId = self.getIdForAvatar()
  37. messageParameter.name = self.label
  38. messageParameter.mentionDisplayName = "@\(self.label)"
  39. // Note: The mentionId on NCMessageParameter is different than the one on MentionSuggestion!
  40. messageParameter.mentionId = "@\(self.getIdForChat())"
  41. // Set parameter type
  42. if self.source == "calls" {
  43. messageParameter.type = "call"
  44. } else if self.source == "users" || self.source == "federated_users" {
  45. messageParameter.type = "user"
  46. } else if self.source == "guests" {
  47. messageParameter.type = "guest"
  48. } else if self.source == "groups" {
  49. messageParameter.type = "user-group"
  50. }
  51. return messageParameter
  52. }
  53. }