Helpers.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import Foundation
  2. import CryptoKit
  3. func topicUrl(baseUrl: String, topic: String) -> String {
  4. return "\(baseUrl)/\(topic)"
  5. }
  6. func topicShortUrl(baseUrl: String, topic: String) -> String {
  7. return shortUrl(url: topicUrl(baseUrl: baseUrl, topic: topic))
  8. }
  9. func topicAuthUrl(baseUrl: String, topic: String) -> String {
  10. return "\(baseUrl)/\(topic)/auth"
  11. }
  12. func topicHash(baseUrl: String, topic: String) -> String {
  13. let data = Data(topicUrl(baseUrl: baseUrl, topic: topic).utf8)
  14. let digest = SHA256.hash(data: data)
  15. return digest.compactMap { String(format: "%02x", $0)}.joined()
  16. }
  17. func shortUrl(url: String) -> String {
  18. return url
  19. .replacingOccurrences(of: "http://", with: "")
  20. .replacingOccurrences(of: "https://", with: "")
  21. }
  22. func parseAllTags(_ tags: String?) -> [String] {
  23. return (tags?.components(separatedBy: ",") ?? [])
  24. .filter { !$0.trimmingCharacters(in: .whitespaces).isEmpty }
  25. }
  26. func parseEmojiTags(_ tags: String?) -> [String] {
  27. return parseEmojiTags(parseAllTags(tags))
  28. }
  29. func parseEmojiTags(_ tags: [String]?) -> [String] {
  30. guard let tags = tags else { return [] }
  31. var emojiTags: [String] = []
  32. for tag in tags {
  33. if let emoji = EmojiManager.shared.getEmojiByAlias(alias: tag) {
  34. emojiTags.append(emoji.getUnicode())
  35. }
  36. }
  37. return emojiTags
  38. }
  39. func parseNonEmojiTags(_ tags: String?) -> [String] {
  40. return parseAllTags(tags)
  41. .filter { EmojiManager.shared.getEmojiByAlias(alias: $0) == nil }
  42. }