d664dde798
Replaces the wrong approach (stage + try to bounce into the app, which iOS blocks) with the one Teams/WhatsApp actually use: the picker and the send happen INSIDE the share extension, so there's no app-open at all. Tap Share → Biz Connect → pick a chat → it uploads and sends, right there in the sheet. How the extension can send without the app: it's a separate process that can't see the web app's HttpOnly cookie, so: - server: GET /api/share/token mints a bearer token for the logged-in user. - web: on every launch the app fetches that token and hands it to the extension via the App Group (ShareInbox.setAuth writes token+base to the shared UserDefaults). - extension: reads the token and calls the SAME API the native client uses — GET /api/messages/conversations to list chats, POST /api/messages/upload for each file, POST /api/messages to send. Native UITableView picker with search. Robustness: it still stages the files + writes a manifest first, so if there's no token yet (user never signed in) or the send fails, the file isn't lost — the app collects it on next open, exactly as before. On success the manifest is cleared so the app doesn't re-offer it. Server + web are live now; the token endpoint is harmless until a build ships the extension. NEEDS A NEW iOS BUILD for the picker itself. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
83 lines
3.9 KiB
Swift
83 lines
3.9 KiB
Swift
import Foundation
|
|
import Capacitor
|
|
|
|
// Reads the files the Share Extension staged into the App Group container, so the web app can pick a
|
|
// conversation and send them. The extension and the app are separate processes; the App Group's shared
|
|
// container is the only place both can read/write, and it is NOT one of @capacitor/filesystem's known
|
|
// directories — hence this small bridge.
|
|
//
|
|
// getPending() → { items: [ {kind, name, path, uri, mime, size} | {kind:"text", text} ] }
|
|
// `uri` is a file:// URL the web layer turns into a fetchable source with Capacitor.convertFileSrc,
|
|
// so the existing upload path can read the bytes without base64 marshalling.
|
|
// clear() removes the manifest and every staged file, once the app has taken them.
|
|
@objc(ShareInboxPlugin)
|
|
public class ShareInboxPlugin: CAPPlugin, CAPBridgedPlugin {
|
|
public let identifier = "ShareInboxPlugin"
|
|
public let jsName = "ShareInbox"
|
|
public let pluginMethods: [CAPPluginMethod] = [
|
|
CAPPluginMethod(name: "getPending", returnType: CAPPluginReturnPromise),
|
|
CAPPluginMethod(name: "clear", returnType: CAPPluginReturnPromise),
|
|
CAPPluginMethod(name: "setAuth", returnType: CAPPluginReturnPromise)
|
|
]
|
|
|
|
private let appGroup = "group.com.bizgaze.connect"
|
|
|
|
// The web app hands the extension a bearer token + API base (via /api/share/token) so the extension can
|
|
// list chats, upload and send on its own — no app-open needed. Stored in the App Group's shared
|
|
// UserDefaults, which the extension reads directly. Passing an empty token clears it (e.g. on logout).
|
|
@objc func setAuth(_ call: CAPPluginCall) {
|
|
let token = call.getString("token") ?? ""
|
|
let base = call.getString("base") ?? ""
|
|
if let d = UserDefaults(suiteName: appGroup) {
|
|
if token.isEmpty { d.removeObject(forKey: "bzc_token"); d.removeObject(forKey: "bzc_base") }
|
|
else { d.set(token, forKey: "bzc_token"); d.set(base, forKey: "bzc_base") }
|
|
}
|
|
call.resolve(["ok": true])
|
|
}
|
|
|
|
@objc func getPending(_ call: CAPPluginCall) {
|
|
guard let dir = sharedDir() else { return call.resolve(["items": []]) }
|
|
let manifest = dir.appendingPathComponent("manifest.json")
|
|
guard let data = try? Data(contentsOf: manifest),
|
|
let records = try? JSONSerialization.jsonObject(with: data) as? [[String: Any]] else {
|
|
return call.resolve(["items": []])
|
|
}
|
|
var items: [[String: Any]] = []
|
|
for r in records {
|
|
let kind = r["kind"] as? String ?? "file"
|
|
if kind == "text" {
|
|
if let text = r["text"] as? String { items.append(["kind": "text", "text": text]) }
|
|
continue
|
|
}
|
|
// A file record is only usable if its staged copy is still on disk.
|
|
guard let path = r["path"] as? String,
|
|
FileManager.default.fileExists(atPath: path) else { continue }
|
|
let url = URL(fileURLWithPath: path)
|
|
items.append([
|
|
"kind": "file",
|
|
"name": r["name"] as? String ?? url.lastPathComponent,
|
|
"path": path,
|
|
"uri": url.absoluteString,
|
|
"mime": r["mime"] as? String ?? "application/octet-stream",
|
|
"size": r["size"] as? Int64 ?? (r["size"] as? Int ?? 0)
|
|
])
|
|
}
|
|
call.resolve(["items": items])
|
|
}
|
|
|
|
@objc func clear(_ call: CAPPluginCall) {
|
|
if let dir = sharedDir() {
|
|
let fm = FileManager.default
|
|
if let entries = try? fm.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil) {
|
|
for e in entries { try? fm.removeItem(at: e) }
|
|
}
|
|
}
|
|
call.resolve()
|
|
}
|
|
|
|
private func sharedDir() -> URL? {
|
|
guard let base = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroup) else { return nil }
|
|
return base.appendingPathComponent("Shared", isDirectory: true)
|
|
}
|
|
}
|