|
| 1 | +import Foundation |
| 2 | +import Logger |
| 3 | + |
| 4 | +extension CurrentUser { |
| 5 | + |
| 6 | + /// Builds a dictionary to be used as content when publishing a kind 0 |
| 7 | + /// event. |
| 8 | + private func buildMetadataJSONObject(author: Author) -> [String: String] { |
| 9 | + var metaEvent = MetadataEventJSON( |
| 10 | + displayName: author.displayName, |
| 11 | + name: author.name, |
| 12 | + nip05: author.nip05, |
| 13 | + uns: author.uns, |
| 14 | + about: author.about, |
| 15 | + website: author.website, |
| 16 | + picture: author.profilePhotoURL?.absoluteString |
| 17 | + ).dictionary |
| 18 | + if let rawData = author.rawMetadata { |
| 19 | + // Tack on any unsupported fields back onto the dictionary before |
| 20 | + // publish. |
| 21 | + do { |
| 22 | + let rawJson = try JSONSerialization.jsonObject(with: rawData) |
| 23 | + if let rawDictionary = rawJson as? [String: AnyObject] { |
| 24 | + for key in rawDictionary.keys { |
| 25 | + guard metaEvent[key] == nil else { |
| 26 | + continue |
| 27 | + } |
| 28 | + if let rawValue = rawDictionary[key] as? String { |
| 29 | + metaEvent[key] = rawValue |
| 30 | + Log.debug("Added \(key) : \(rawValue)") |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } catch { |
| 35 | + Log.debug("Couldn't parse a JSON from the user raw metadata") |
| 36 | + // Continue with the metaEvent object we built previously |
| 37 | + } |
| 38 | + } |
| 39 | + return metaEvent |
| 40 | + } |
| 41 | + |
| 42 | + @MainActor func publishMetadata() async throws { |
| 43 | + guard let pubKey = publicKeyHex else { |
| 44 | + Log.debug("Error: no publicKeyHex") |
| 45 | + throw CurrentUserError.authorNotFound |
| 46 | + } |
| 47 | + guard let pair = keyPair else { |
| 48 | + Log.debug("Error: no keyPair") |
| 49 | + throw CurrentUserError.authorNotFound |
| 50 | + } |
| 51 | + guard let context = viewContext else { |
| 52 | + Log.debug("Error: no context") |
| 53 | + throw CurrentUserError.authorNotFound |
| 54 | + } |
| 55 | + guard let author = try Author.find(by: pubKey, context: context) else { |
| 56 | + Log.debug("Error: no author in DB") |
| 57 | + throw CurrentUserError.authorNotFound |
| 58 | + } |
| 59 | + |
| 60 | + self.author = author |
| 61 | + |
| 62 | + let jsonObject = buildMetadataJSONObject(author: author) |
| 63 | + let data = try JSONSerialization.data(withJSONObject: jsonObject) |
| 64 | + let content = String(decoding: data, as: UTF8.self) |
| 65 | + |
| 66 | + let jsonEvent = JSONEvent( |
| 67 | + pubKey: pubKey, |
| 68 | + kind: .metaData, |
| 69 | + tags: [], |
| 70 | + content: content |
| 71 | + ) |
| 72 | + |
| 73 | + do { |
| 74 | + try await relayService.publishToAll( |
| 75 | + event: jsonEvent, |
| 76 | + signingKey: pair, |
| 77 | + context: viewContext |
| 78 | + ) |
| 79 | + } catch { |
| 80 | + Log.error(error.localizedDescription) |
| 81 | + throw CurrentUserError.errorWhilePublishingToRelays |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @MainActor func publishMuteList(keys: [String]) async { |
| 86 | + guard let pubKey = publicKeyHex else { |
| 87 | + Log.debug("Error: no pubKey") |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + let jsonEvent = JSONEvent(pubKey: pubKey, kind: .mute, tags: keys.pTags, content: "") |
| 92 | + |
| 93 | + if let pair = keyPair { |
| 94 | + do { |
| 95 | + try await relayService.publishToAll(event: jsonEvent, signingKey: pair, context: viewContext) |
| 96 | + } catch { |
| 97 | + Log.debug("Failed to update mute list \(error.localizedDescription)") |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @MainActor func publishDelete(for identifiers: [String], reason: String = "") async { |
| 103 | + guard let pubKey = publicKeyHex else { |
| 104 | + Log.debug("Error: no pubKey") |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + let tags = identifiers.eTags |
| 109 | + let jsonEvent = JSONEvent(pubKey: pubKey, kind: .delete, tags: tags, content: reason) |
| 110 | + |
| 111 | + if let pair = keyPair { |
| 112 | + do { |
| 113 | + try await relayService.publishToAll(event: jsonEvent, signingKey: pair, context: viewContext) |
| 114 | + } catch { |
| 115 | + Log.debug("Failed to delete events \(error.localizedDescription)") |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + @MainActor func publishContactList(tags: [[String]]) async { |
| 121 | + guard let pubKey = publicKeyHex else { |
| 122 | + Log.debug("Error: no pubKey") |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + guard let relays = author?.relays else { |
| 127 | + Log.debug("Error: No relay service") |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + var relayString = "{" |
| 132 | + for relay in relays { |
| 133 | + if let address = relay.address { |
| 134 | + relayString += "\"\(address)\":{\"write\":true,\"read\":true}," |
| 135 | + } |
| 136 | + } |
| 137 | + relayString.removeLast() |
| 138 | + relayString += "}" |
| 139 | + |
| 140 | + let jsonEvent = JSONEvent(pubKey: pubKey, kind: .contactList, tags: tags, content: relayString) |
| 141 | + |
| 142 | + if let pair = keyPair { |
| 143 | + do { |
| 144 | + try await relayService.publishToAll(event: jsonEvent, signingKey: pair, context: viewContext) |
| 145 | + } catch { |
| 146 | + Log.debug("failed to update Follows \(error.localizedDescription)") |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + /// Follow by public hex key |
| 152 | + @MainActor func follow(author toFollow: Author) async throws { |
| 153 | + guard let followKey = toFollow.hexadecimalPublicKey else { |
| 154 | + Log.debug("Error: followKey is nil") |
| 155 | + return |
| 156 | + } |
| 157 | + |
| 158 | + Log.debug("Following \(followKey)") |
| 159 | + |
| 160 | + var followKeys = await Array(socialGraph.followedKeys) |
| 161 | + followKeys.append(followKey) |
| 162 | + |
| 163 | + // Update author to add the new follow |
| 164 | + if let followedAuthor = try? Author.find(by: followKey, context: viewContext), let currentUser = author { |
| 165 | + let follow = try Follow.findOrCreate( |
| 166 | + source: currentUser, |
| 167 | + destination: followedAuthor, |
| 168 | + context: viewContext |
| 169 | + ) |
| 170 | + |
| 171 | + // Add to the current user's follows |
| 172 | + currentUser.follows.insert(follow) |
| 173 | + } |
| 174 | + |
| 175 | + try viewContext.save() |
| 176 | + await publishContactList(tags: followKeys.pTags) |
| 177 | + } |
| 178 | + |
| 179 | + /// Unfollow by public hex key |
| 180 | + @MainActor func unfollow(author toUnfollow: Author) async throws { |
| 181 | + guard let unfollowedKey = toUnfollow.hexadecimalPublicKey else { |
| 182 | + Log.debug("Error: unfollowedKey is nil") |
| 183 | + return |
| 184 | + } |
| 185 | + |
| 186 | + Log.debug("Unfollowing \(unfollowedKey)") |
| 187 | + |
| 188 | + let stillFollowingKeys = await Array(socialGraph.followedKeys) |
| 189 | + .filter { $0 != unfollowedKey } |
| 190 | + |
| 191 | + // Update author to only follow those still following |
| 192 | + if let unfollowedAuthor = try? Author.find(by: unfollowedKey, context: viewContext), let currentUser = author { |
| 193 | + // Remove from the current user's follows |
| 194 | + let unfollows = Follow.follows(source: currentUser, destination: unfollowedAuthor, context: viewContext) |
| 195 | + |
| 196 | + for unfollow in unfollows { |
| 197 | + // Remove current user's follows |
| 198 | + currentUser.follows.remove(unfollow) |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + try viewContext.save() |
| 203 | + await publishContactList(tags: stillFollowingKeys.pTags) |
| 204 | + } |
| 205 | + |
| 206 | + @MainActor func publishRequestToVanish(to relays: [URL]? = nil, reason: String? = nil) async throws { |
| 207 | + guard let keyPair else { |
| 208 | + Log.debug("Error: no key pair") |
| 209 | + return |
| 210 | + } |
| 211 | + |
| 212 | + let pubKey = keyPair.publicKey.hex |
| 213 | + let jsonEvent = JSONEvent.requestToVanish(pubKey: pubKey, relays: relays, reason: reason) |
| 214 | + |
| 215 | + do { |
| 216 | + if let relays, !relays.isEmpty { |
| 217 | + try await relayService.publish(event: jsonEvent, to: relays, signingKey: keyPair, context: viewContext) |
| 218 | + } else { |
| 219 | + try await relayService.publishToAll(event: jsonEvent, signingKey: keyPair, context: viewContext) |
| 220 | + } |
| 221 | + } catch { |
| 222 | + Log.debug("Failed to publish request to vanish \(error.localizedDescription)") |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments