|
| 1 | +// |
| 2 | +// YTComment+actions.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Antoine Bollengier on 03.07.2024. |
| 6 | +// Copyright © 2024 Antoine Bollengier (github.com/b5i). All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +public extension YTComment { |
| 10 | + /// Do one of the ``YTComment/CommentAction`` (like, dislike, removeLike, removeDislike, delete). |
| 11 | + func commentAction(youtubeModel: YouTubeModel, action: YTComment.CommentAction, result: @escaping @Sendable (Error?) -> Void) { |
| 12 | + switch action { |
| 13 | + case .like: |
| 14 | + guard let param = self.actionsParams[.like] else { result("self.actionsParams[.like] is not present."); return } |
| 15 | + LikeCommentResponse.sendNonThrowingRequest(youtubeModel: youtubeModel, data: [.params: param], result: { res in |
| 16 | + switch res { |
| 17 | + case .success(_): |
| 18 | + result(nil) |
| 19 | + case .failure(let failure): |
| 20 | + result(failure) |
| 21 | + } |
| 22 | + }) |
| 23 | + case .dislike: |
| 24 | + guard let param = self.actionsParams[.dislike] else { result("self.actionsParams[.dislike] is not present."); return } |
| 25 | + DislikeCommentResponse.sendNonThrowingRequest(youtubeModel: youtubeModel, data: [.params: param], result: { res in |
| 26 | + switch res { |
| 27 | + case .success(_): |
| 28 | + result(nil) |
| 29 | + case .failure(let failure): |
| 30 | + result(failure) |
| 31 | + } |
| 32 | + }) |
| 33 | + case .removeLike: |
| 34 | + guard let param = self.actionsParams[.removeLike] else { result("self.actionsParams[.removeLike] is not present."); return } |
| 35 | + RemoveLikeCommentResponse.sendNonThrowingRequest(youtubeModel: youtubeModel, data: [.params: param], result: { res in |
| 36 | + switch res { |
| 37 | + case .success(_): |
| 38 | + result(nil) |
| 39 | + case .failure(let failure): |
| 40 | + result(failure) |
| 41 | + } |
| 42 | + }) |
| 43 | + case .removeDislike: |
| 44 | + guard let param = self.actionsParams[.removeDislike] else { result("self.actionsParams[.removeDislike] is not present."); return } |
| 45 | + RemoveDislikeCommentResponse.sendNonThrowingRequest(youtubeModel: youtubeModel, data: [.params: param], result: { res in |
| 46 | + switch res { |
| 47 | + case .success(_): |
| 48 | + result(nil) |
| 49 | + case .failure(let failure): |
| 50 | + result(failure) |
| 51 | + } |
| 52 | + }) |
| 53 | + case .delete: |
| 54 | + guard let param = self.actionsParams[.delete] else { result("self.actionsParams[.delete] is not present."); return } |
| 55 | + DeleteCommentResponse.sendNonThrowingRequest(youtubeModel: youtubeModel, data: [.params: param], result: { res in |
| 56 | + switch res { |
| 57 | + case .success(_): |
| 58 | + result(nil) |
| 59 | + case .failure(let failure): |
| 60 | + result(failure) |
| 61 | + } |
| 62 | + }) |
| 63 | + case .edit: |
| 64 | + result("edit is not supported via commentAction(_:_:_:), please use editComment(_:_:).") |
| 65 | + case .reply: |
| 66 | + result("reply is not supported via commentAction(_:_:_:), please use replyToComment(_:_:).") |
| 67 | + case .repliesContinuation: |
| 68 | + result("repliesContinuation is not supported via commentAction(_:_:_:), please use fetchRepliesContinuation(_:_:).") |
| 69 | + case .translate: |
| 70 | + result("translate is not supported via commentAction(_:_:_:), please use translateText(_:_:).") |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) |
| 75 | + func commentAction(youtubeModel: YouTubeModel, action: YTComment.CommentAction) async throws { |
| 76 | + return try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<Void, Error>) in |
| 77 | + self.commentAction(youtubeModel: youtubeModel, action: action, result: { error in |
| 78 | + if let error = error { |
| 79 | + continuation.resume(throwing: error) |
| 80 | + } else { |
| 81 | + continuation.resume() |
| 82 | + } |
| 83 | + }) |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + /// Edit the text of a comment (the cookies from the used ``YouTubeModel`` must be the owner of the comment). |
| 88 | + func editComment(withNewText text: String, youtubeModel: YouTubeModel, result: @escaping @Sendable (Error?) -> Void) { |
| 89 | + guard let param = self.actionsParams[.edit] else { result("self.actionsParams[.edit] is not present."); return } |
| 90 | + if (self.replyLevel ?? 0) == 0 { |
| 91 | + EditCommentResponse.sendNonThrowingRequest(youtubeModel: youtubeModel, data: [.params: param, .text: text], result: { res in |
| 92 | + switch res { |
| 93 | + case .success(_): |
| 94 | + result(nil) |
| 95 | + case .failure(let failure): |
| 96 | + result(failure) |
| 97 | + } |
| 98 | + }) |
| 99 | + } else { |
| 100 | + EditReplyCommandResponse.sendNonThrowingRequest(youtubeModel: youtubeModel, data: [.params: param, .text: text], result: { res in |
| 101 | + switch res { |
| 102 | + case .success(_): |
| 103 | + result(nil) |
| 104 | + case .failure(let failure): |
| 105 | + result(failure) |
| 106 | + } |
| 107 | + }) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) |
| 112 | + /// Edit the text of a comment (the cookies from the used ``YouTubeModel`` must be the owner of the comment). |
| 113 | + func editComment(withNewText text: String, youtubeModel: YouTubeModel) async throws { |
| 114 | + return try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<Void, Error>) in |
| 115 | + self.editComment(withNewText: text, youtubeModel: youtubeModel, result: { error in |
| 116 | + if let error = error { |
| 117 | + continuation.resume(throwing: error) |
| 118 | + } else { |
| 119 | + continuation.resume() |
| 120 | + } |
| 121 | + }) |
| 122 | + }) |
| 123 | + } |
| 124 | + |
| 125 | + /// Reply to a comment. |
| 126 | + func replyToComment(youtubeModel: YouTubeModel, text: String, result: @escaping @Sendable (Result<ReplyCommentResponse, Error>) -> Void) { |
| 127 | + guard let replyToken = self.actionsParams[.reply] else { result(.failure("self.actionsParams[.reply] is not present.")); return } |
| 128 | + ReplyCommentResponse.sendNonThrowingRequest(youtubeModel: youtubeModel, data: [.params: replyToken, .text: text], result: { res in |
| 129 | + switch res { |
| 130 | + case .success(let success): |
| 131 | + result(.success(success)) |
| 132 | + case .failure(let failure): |
| 133 | + result(.failure(failure)) |
| 134 | + } |
| 135 | + }) |
| 136 | + } |
| 137 | + |
| 138 | + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) |
| 139 | + /// Reply to a comment. |
| 140 | + func replyToComment(youtubeModel: YouTubeModel, text: String) async throws -> ReplyCommentResponse { |
| 141 | + return try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<ReplyCommentResponse, Error>) in |
| 142 | + self.replyToComment(youtubeModel: youtubeModel, text: text, result: { response in |
| 143 | + continuation.resume(with: response) |
| 144 | + }) |
| 145 | + }) |
| 146 | + } |
| 147 | + |
| 148 | + /// Get the replies of a comment, can also be used to get the continuation of the replies. |
| 149 | + func fetchRepliesContinuation(youtubeModel: YouTubeModel, useCookies: Bool? = nil, result: @escaping @Sendable (Result<VideoCommentsResponse.Continuation, Error>) -> Void) { |
| 150 | + guard let repliesContinuationToken = self.actionsParams[.repliesContinuation] else { result(.failure("self.actionsParams[.repliesContinuation] is not present.")); return } |
| 151 | + VideoCommentsResponse.Continuation.sendNonThrowingRequest(youtubeModel: youtubeModel, data: [.continuation: repliesContinuationToken], useCookies: useCookies, result: { res in |
| 152 | + switch res { |
| 153 | + case .success(let success): |
| 154 | + result(.success(success)) |
| 155 | + case .failure(let failure): |
| 156 | + result(.failure(failure)) |
| 157 | + } |
| 158 | + }) |
| 159 | + } |
| 160 | + |
| 161 | + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) |
| 162 | + /// Get the replies of a comment, can also be used to get the continuation of the replies. |
| 163 | + func fetchRepliesContinuation(youtubeModel: YouTubeModel, useCookies: Bool? = nil) async throws -> VideoCommentsResponse.Continuation { |
| 164 | + return try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<VideoCommentsResponse.Continuation, Error>) in |
| 165 | + self.fetchRepliesContinuation(youtubeModel: youtubeModel, useCookies: useCookies, result: { response in |
| 166 | + continuation.resume(with: response) |
| 167 | + }) |
| 168 | + }) |
| 169 | + } |
| 170 | + |
| 171 | + /// Translate the text of a comment, is available if YouTube thinks that the cookies' user or the language of the user agent (?) might need a translation. |
| 172 | + func translateText(youtubeModel: YouTubeModel, useCookies: Bool? = nil, result: @escaping @Sendable (Result<CommentTranslationResponse, Error>) -> Void) { |
| 173 | + guard let translationToken = self.actionsParams[.translate] else { result(.failure("self.actionsParams[.translate] is not present.")); return } |
| 174 | + CommentTranslationResponse.sendNonThrowingRequest(youtubeModel: youtubeModel, data: [.params: translationToken], useCookies: useCookies, result: { res in |
| 175 | + switch res { |
| 176 | + case .success(let success): |
| 177 | + result(.success(success)) |
| 178 | + case .failure(let failure): |
| 179 | + result(.failure(failure)) |
| 180 | + } |
| 181 | + }) |
| 182 | + } |
| 183 | + |
| 184 | + @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) |
| 185 | + /// Translate the text of a comment, is available if YouTube thinks that the cookies' user or the language of the user agent (?) might need a translation. |
| 186 | + func translateText(youtubeModel: YouTubeModel, useCookies: Bool? = nil) async throws -> CommentTranslationResponse { |
| 187 | + return try await withCheckedThrowingContinuation({ (continuation: CheckedContinuation<CommentTranslationResponse, Error>) in |
| 188 | + self.translateText(youtubeModel: youtubeModel, useCookies: useCookies, result: { response in |
| 189 | + continuation.resume(with: response) |
| 190 | + }) |
| 191 | + }) |
| 192 | + } |
| 193 | +} |
0 commit comments