-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1170 from Automattic/charlie/1145/append-to-note-mk2
Shortcut: Append to note
- Loading branch information
Showing
15 changed files
with
524 additions
and
1 deletion.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
IntentsExtension/Extensions/NSURLSessionConfiguration+Extensions.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// NSURLSessionConfiguration+Extensions.swift | ||
// IntentsExtension | ||
// | ||
// Created by Charlie Scheer on 6/3/24. | ||
// Copyright © 2024 Simperium. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension URLSessionConfiguration { | ||
/// Returns a new Background Session Configuration, with a random identifier. | ||
/// | ||
class func backgroundSessionConfigurationWithRandomizedIdentifier() -> URLSessionConfiguration { | ||
let identifier = IntentsConstants.extensionGroupName + "." + UUID().uuidString | ||
let configuration = URLSessionConfiguration.background(withIdentifier: identifier) | ||
configuration.sharedContainerIdentifier = IntentsConstants.extensionGroupName | ||
|
||
return configuration | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
IntentsExtension/IntentHandlers/AppendNoteIntentHandler.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Intents | ||
|
||
class AppendNoteIntentHandler: NSObject, AppendNoteIntentHandling { | ||
let coreDataWrapper = ExtensionCoreDataWrapper() | ||
|
||
func resolveContent(for intent: AppendNoteIntent) async -> INStringResolutionResult { | ||
guard let content = intent.content else { | ||
return INStringResolutionResult.needsValue() | ||
} | ||
return INStringResolutionResult.success(with: content) | ||
} | ||
|
||
func resolveNote(for intent: AppendNoteIntent) async -> IntentNoteResolutionResult { | ||
IntentNoteResolutionResult.resolve(intent.note, in: coreDataWrapper) | ||
} | ||
|
||
func provideNoteOptionsCollection(for intent: AppendNoteIntent) async throws -> INObjectCollection<IntentNote> { | ||
let intentNotes = try IntentNote.allNotes(in: coreDataWrapper) | ||
return INObjectCollection(items: intentNotes) | ||
} | ||
|
||
func handle(intent: AppendNoteIntent) async -> AppendNoteIntentResponse { | ||
guard let identifier = intent.note?.identifier, | ||
let content = intent.content, | ||
let note = coreDataWrapper.resultsController?.note(forSimperiumKey: identifier), | ||
let token = KeychainManager.extensionToken else { | ||
return AppendNoteIntentResponse(code: .failure, userActivity: nil) | ||
} | ||
|
||
guard let existingContent = try? await Downloader(simperiumToken: token).getNoteContent(for: identifier) else { | ||
return AppendNoteIntentResponse(code: .failure, userActivity: nil) | ||
} | ||
|
||
note.content = existingContent + "\n\(content)" | ||
let uploader = Uploader(simperiumToken: token) | ||
uploader.send(note) | ||
|
||
return AppendNoteIntentResponse(code: .success, userActivity: nil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import Foundation | ||
|
||
class Downloader: NSObject { | ||
|
||
/// Simperium's Token | ||
/// | ||
private let token: String | ||
|
||
/// Designated Initializer | ||
/// | ||
init(simperiumToken: String) { | ||
token = simperiumToken | ||
} | ||
|
||
func getNoteContent(for simperiumKey: String) async throws -> String? { | ||
let endpoint = String(format: "%@/%@/%@/i/%@", IntentsConstants.simperiumBaseURL, SPCredentials.simperiumAppID, Settings.bucketName, simperiumKey) | ||
let targetURL = URL(string: endpoint.lowercased())! | ||
|
||
// Request | ||
var request = URLRequest(url: targetURL) | ||
request.httpMethod = Settings.httpMethodGet | ||
request.setValue(token, forHTTPHeaderField: Settings.authHeader) | ||
|
||
let session = Foundation.URLSession(configuration: .default, delegate: nil, delegateQueue: .main) | ||
|
||
let downloadedData = try await session.data(for: request) | ||
|
||
return try extractNoteContent(from: downloadedData.0) | ||
} | ||
|
||
private func extractNoteContent(from data: Data) throws -> String? { | ||
let jsonObject = try JSONSerialization.jsonObject(with: data) as? [String: Any] | ||
return jsonObject?["content"] as? String | ||
} | ||
} | ||
|
||
// MARK: - Settings | ||
// | ||
private struct Settings { | ||
static let authHeader = "X-Simperium-Token" | ||
static let bucketName = "note" | ||
static let httpMethodGet = "GET" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import Foundation | ||
|
||
/// The purpose of this class is to encapsulate NSURLSession's interaction code, required to upload | ||
/// a note to Simperium's REST endpoint. | ||
/// | ||
class Uploader: NSObject { | ||
|
||
/// Simperium's Token | ||
/// | ||
private let token: String | ||
|
||
/// Designated Initializer | ||
/// | ||
init(simperiumToken: String) { | ||
token = simperiumToken | ||
} | ||
|
||
// MARK: - Public Methods | ||
func send(_ note: Note) { | ||
// Build the targetURL | ||
let endpoint = String(format: "%@/%@/%@/i/%@", IntentsConstants.simperiumBaseURL, SPCredentials.simperiumAppID, Settings.bucketName, note.simperiumKey) | ||
let targetURL = URL(string: endpoint.lowercased())! | ||
|
||
// Request | ||
var request = URLRequest(url: targetURL) | ||
request.httpMethod = Settings.httpMethodPost | ||
request.httpBody = note.toJsonData() | ||
request.setValue(token, forHTTPHeaderField: Settings.authHeader) | ||
|
||
// Task! | ||
let sc = URLSessionConfiguration.backgroundSessionConfigurationWithRandomizedIdentifier() | ||
|
||
let session = Foundation.URLSession(configuration: sc, delegate: self, delegateQueue: .main) | ||
let task = session.downloadTask(with: request) | ||
task.resume() | ||
} | ||
} | ||
|
||
// MARK: - URLSessionDelegate | ||
// | ||
extension Uploader: URLSessionDelegate { | ||
|
||
@objc | ||
func urlSession(_ session: URLSession, didBecomeInvalidWithError error: Error?) { | ||
print("<> Uploader.didBecomeInvalidWithError: \(String(describing: error))") | ||
} | ||
|
||
@objc | ||
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) { | ||
print("<> Uploader.URLSessionDidFinishEventsForBackgroundURLSession") | ||
} | ||
} | ||
|
||
// MARK: - URLSessionTaskDelegate | ||
// | ||
extension Uploader: URLSessionTaskDelegate { | ||
|
||
@objc | ||
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { | ||
print("<> Uploader.didCompleteWithError: \(String(describing: error))") | ||
} | ||
} | ||
|
||
// MARK: - Settings | ||
// | ||
private struct Settings { | ||
static let authHeader = "X-Simperium-Token" | ||
static let bucketName = "note" | ||
static let httpMethodPost = "POST" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.