mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-02 22:59:56 +08:00
feat(ios): improve share preview (#15538)
#### PR Dependency Tree * **PR #15538** 👈 This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added rich link previews to mobile and iOS sharing, including images, metadata, transcripts, and selected text. * Share imports can now create structured content blocks, embeds, bookmarks, and transcript callouts. * Added workspace-aware preview handling for cloud, self-hosted, and signed-out modes. * **Accessibility** * Improved collapse/expand controls with semantic buttons and ARIA relationships. * **Bug Fixes** * Enhanced URL and error sanitization in server logs. * Improved link-preview CORS support, validation, and request handling. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
import { app, clipboard, nativeImage, nativeTheme } from 'electron';
|
||||
import { getLinkPreview } from 'link-preview-js';
|
||||
import { map, shareReplay } from 'rxjs';
|
||||
|
||||
import { isMacOS } from '../../shared/utils';
|
||||
import { persistentConfig } from '../config-storage/persist';
|
||||
import { logger } from '../logger';
|
||||
import { openExternalSafely } from '../security/open-external';
|
||||
import { resolveAndValidateUrlForPreview } from '../security/url-safety';
|
||||
import type { WorkbenchViewMeta } from '../shared-state-schema';
|
||||
import { MenubarStateKey, MenubarStateSchema } from '../shared-state-schema';
|
||||
import { globalStateStorage } from '../shared-storage/storage';
|
||||
@@ -39,13 +37,6 @@ import { getOrCreateCustomThemeWindow } from '../windows-manager/custom-theme-wi
|
||||
import { getChallengeResponse } from './challenge';
|
||||
import { uiSubjects } from './subject';
|
||||
|
||||
const EMPTY_OBJECT = Object.freeze({
|
||||
title: undefined,
|
||||
description: undefined,
|
||||
icon: undefined,
|
||||
image: undefined,
|
||||
});
|
||||
|
||||
const TraySettingsState = {
|
||||
$: globalStateStorage.watch<MenubarStateSchema>(MenubarStateKey).pipe(
|
||||
map(v => MenubarStateSchema.parse(v ?? {})),
|
||||
@@ -134,83 +125,6 @@ export const uiHandlers = {
|
||||
logger.error('handleOpenMainApp', err);
|
||||
}
|
||||
},
|
||||
getBookmarkDataByLink: async (_, link: string) => {
|
||||
try {
|
||||
// Basic validation up-front to prevent SSRF (including redirects).
|
||||
await resolveAndValidateUrlForPreview(link);
|
||||
} catch {
|
||||
return EMPTY_OBJECT;
|
||||
}
|
||||
|
||||
if (
|
||||
(link.startsWith('https://x.com/') ||
|
||||
link.startsWith('https://www.x.com/') ||
|
||||
link.startsWith('https://www.twitter.com/') ||
|
||||
link.startsWith('https://twitter.com/')) &&
|
||||
link.includes('/status/')
|
||||
) {
|
||||
// use api.fxtwitter.com
|
||||
const statusId = /\/status\/(\d+)/.exec(link)?.[1];
|
||||
if (!statusId) return EMPTY_OBJECT;
|
||||
link = `https://api.fxtwitter.com/status/${statusId}`;
|
||||
try {
|
||||
const { tweet } = (await fetch(link).then(res => res.json())) as any;
|
||||
return {
|
||||
title: tweet.author.name,
|
||||
icon: tweet.author.avatar_url,
|
||||
description: tweet.text,
|
||||
image: tweet.media?.photos[0].url || tweet.author.banner_url,
|
||||
};
|
||||
} catch (err) {
|
||||
logger.error('getBookmarkDataByLink', err);
|
||||
return {
|
||||
title: undefined,
|
||||
description: undefined,
|
||||
icon: undefined,
|
||||
image: undefined,
|
||||
};
|
||||
}
|
||||
} else {
|
||||
const previewData = (await getLinkPreview(link, {
|
||||
timeout: 6000,
|
||||
headers: {
|
||||
'User-Agent':
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0 Safari/537.36 Edg/120.0.0',
|
||||
},
|
||||
followRedirects: 'manual',
|
||||
handleRedirects: (_baseUrl: string, forwardedUrl: string) => {
|
||||
try {
|
||||
// Only allow http(s) redirects and re-validate before following.
|
||||
const u = new URL(forwardedUrl);
|
||||
return u.protocol === 'http:' || u.protocol === 'https:';
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
resolveDNSHost: async (url: string) => {
|
||||
const { address } = await resolveAndValidateUrlForPreview(url);
|
||||
return address;
|
||||
},
|
||||
}).catch(() => {
|
||||
return {
|
||||
title: '',
|
||||
siteName: '',
|
||||
description: '',
|
||||
images: [],
|
||||
videos: [],
|
||||
contentType: `text/html`,
|
||||
favicons: [],
|
||||
};
|
||||
})) as any;
|
||||
|
||||
return {
|
||||
title: previewData.title,
|
||||
description: previewData.description,
|
||||
icon: previewData.favicons[0],
|
||||
image: previewData.images[0],
|
||||
};
|
||||
}
|
||||
},
|
||||
openExternal(_, url: string) {
|
||||
return openExternalSafely(url);
|
||||
},
|
||||
|
||||
@@ -51,6 +51,10 @@
|
||||
AA0000050000000000000000 /* AuthDateParserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA0000020000000000000000 /* AuthDateParserTests.swift */; };
|
||||
AB0000010000000000000000 /* ShareInboxSafety.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB0000030000000000000000 /* ShareInboxSafety.swift */; };
|
||||
AB0000020000000000000000 /* ShareInboxSafetyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB0000040000000000000000 /* ShareInboxSafetyTests.swift */; };
|
||||
AB0000060000000000000000 /* ShareInboxModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB0000050000000000000000 /* ShareInboxModels.swift */; };
|
||||
AB0000080000000000000000 /* ShareInboxConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB0000070000000000000000 /* ShareInboxConstants.swift */; };
|
||||
AB00000A0000000000000000 /* ShareLinkPreview.swift in Sources */ = {isa = PBXBuildFile; fileRef = AB0000090000000000000000 /* ShareLinkPreview.swift */; };
|
||||
AB00000B0000000000000000 /* capacitor.config.json in Resources */ = {isa = PBXBuildFile; fileRef = 9D90BE1E2CCB9876006677DB /* capacitor.config.json */; };
|
||||
C4C97C7C2D030BE000BC2AD1 /* affine_mobile_native.swift in Sources */ = {isa = PBXBuildFile; fileRef = C4C97C6F2D0307B700BC2AD1 /* affine_mobile_native.swift */; };
|
||||
C4C97C7D2D030BE000BC2AD1 /* affine_mobile_nativeFFI.h in Sources */ = {isa = PBXBuildFile; fileRef = C4C97C702D0307B700BC2AD1 /* affine_mobile_nativeFFI.h */; };
|
||||
C4C97C7E2D030BE000BC2AD1 /* affine_mobile_nativeFFI.modulemap in Sources */ = {isa = PBXBuildFile; fileRef = C4C97C712D0307B700BC2AD1 /* affine_mobile_nativeFFI.modulemap */; };
|
||||
@@ -139,6 +143,9 @@
|
||||
AA0000020000000000000000 /* AuthDateParserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthDateParserTests.swift; sourceTree = "<group>"; };
|
||||
AB0000030000000000000000 /* ShareInboxSafety.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ../Shared/ShareInbox/ShareInboxSafety.swift; sourceTree = "<group>"; };
|
||||
AB0000040000000000000000 /* ShareInboxSafetyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShareInboxSafetyTests.swift; sourceTree = "<group>"; };
|
||||
AB0000050000000000000000 /* ShareInboxModels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ../Shared/ShareInbox/ShareInboxModels.swift; sourceTree = "<group>"; };
|
||||
AB0000070000000000000000 /* ShareInboxConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ../Shared/ShareInbox/ShareInboxConstants.swift; sourceTree = "<group>"; };
|
||||
AB0000090000000000000000 /* ShareLinkPreview.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ../Shared/ShareInbox/ShareLinkPreview.swift; sourceTree = "<group>"; };
|
||||
AA0000030000000000000000 /* AFFiNETests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AFFiNETests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
AF51FD2D460BCFE21FA515B2 /* Pods-App.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-App.release.xcconfig"; path = "Pods/Target Support Files/Pods-App/Pods-App.release.xcconfig"; sourceTree = "<group>"; };
|
||||
BF48636D7DB5BEE00770FD9A /* Pods_AFFiNE.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_AFFiNE.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
@@ -344,6 +351,9 @@
|
||||
AA0000020000000000000000 /* AuthDateParserTests.swift */,
|
||||
AB0000030000000000000000 /* ShareInboxSafety.swift */,
|
||||
AB0000040000000000000000 /* ShareInboxSafetyTests.swift */,
|
||||
AB0000050000000000000000 /* ShareInboxModels.swift */,
|
||||
AB0000070000000000000000 /* ShareInboxConstants.swift */,
|
||||
AB0000090000000000000000 /* ShareLinkPreview.swift */,
|
||||
);
|
||||
path = AppTests;
|
||||
sourceTree = "<group>";
|
||||
@@ -510,6 +520,7 @@
|
||||
isa = PBXResourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
AB00000B0000000000000000 /* capacitor.config.json in Resources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
@@ -627,6 +638,9 @@
|
||||
AA0000050000000000000000 /* AuthDateParserTests.swift in Sources */,
|
||||
AB0000010000000000000000 /* ShareInboxSafety.swift in Sources */,
|
||||
AB0000020000000000000000 /* ShareInboxSafetyTests.swift in Sources */,
|
||||
AB0000060000000000000000 /* ShareInboxModels.swift in Sources */,
|
||||
AB0000080000000000000000 /* ShareInboxConstants.swift in Sources */,
|
||||
AB00000A0000000000000000 /* ShareLinkPreview.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
||||
@@ -7,6 +7,7 @@ public final class ShareInboxPlugin: CAPPlugin, CAPBridgedPlugin {
|
||||
public let jsName = "ShareInbox"
|
||||
public let pluginMethods: [CAPPluginMethod] = [
|
||||
CAPPluginMethod(name: "listPending", returnType: CAPPluginReturnPromise),
|
||||
CAPPluginMethod(name: "updateWorkspaceMode", returnType: CAPPluginReturnPromise),
|
||||
CAPPluginMethod(name: "updateTarget", returnType: CAPPluginReturnPromise),
|
||||
CAPPluginMethod(name: "resolveAttachment", returnType: CAPPluginReturnPromise),
|
||||
CAPPluginMethod(name: "complete", returnType: CAPPluginReturnPromise),
|
||||
@@ -36,6 +37,18 @@ public final class ShareInboxPlugin: CAPPlugin, CAPBridgedPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
@objc func updateWorkspaceMode(_ call: CAPPluginCall) {
|
||||
do {
|
||||
guard let value = call.getString("mode"), let mode = ShareWorkspaceMode(rawValue: value) else {
|
||||
throw ShareInboxError.invalidPayload
|
||||
}
|
||||
try store.updateWorkspaceMode(mode)
|
||||
call.resolve()
|
||||
} catch {
|
||||
call.reject("Failed to update share privacy mode.", nil, error)
|
||||
}
|
||||
}
|
||||
|
||||
@objc func updateTarget(_ call: CAPPluginCall) {
|
||||
do {
|
||||
var item = try item(from: call)
|
||||
|
||||
@@ -1,6 +1,91 @@
|
||||
import XCTest
|
||||
|
||||
private final class SharePreviewURLProtocol: URLProtocol {
|
||||
static var onStart: ((URLProtocol, URLRequest) -> Void)?
|
||||
static var onStop: (() -> Void)?
|
||||
|
||||
override class func canInit(with request: URLRequest) -> Bool { true }
|
||||
override class func canonicalRequest(for request: URLRequest) -> URLRequest { request }
|
||||
override func startLoading() { Self.onStart?(self, request) }
|
||||
override func stopLoading() { Self.onStop?() }
|
||||
}
|
||||
|
||||
final class ShareInboxSafetyTests: XCTestCase {
|
||||
func testManifestTitleIgnoresPreviewAndOnlyAcceptsExplicitEdits() {
|
||||
let originalTitle = "Original Safari title"
|
||||
let serverPreviewTitle = "Untrusted server preview title"
|
||||
|
||||
XCTAssertEqual(
|
||||
ShareInboxSafety.manifestTitle(original: originalTitle, userEdited: nil),
|
||||
originalTitle
|
||||
)
|
||||
XCTAssertNotEqual(
|
||||
ShareInboxSafety.manifestTitle(original: originalTitle, userEdited: nil),
|
||||
serverPreviewTitle
|
||||
)
|
||||
XCTAssertEqual(
|
||||
ShareInboxSafety.manifestTitle(
|
||||
original: originalTitle,
|
||||
userEdited: " My explicit title "
|
||||
),
|
||||
"My explicit title"
|
||||
)
|
||||
XCTAssertEqual(
|
||||
ShareInboxSafety.previewTitle(
|
||||
original: originalTitle, userEdited: nil, serverTitle: serverPreviewTitle),
|
||||
serverPreviewTitle
|
||||
)
|
||||
XCTAssertEqual(
|
||||
ShareInboxSafety.previewTitle(
|
||||
original: originalTitle, userEdited: "My explicit title", serverTitle: serverPreviewTitle),
|
||||
"My explicit title"
|
||||
)
|
||||
XCTAssertEqual(
|
||||
ShareInboxSafety.manifestTitle(original: originalTitle, userEdited: nil),
|
||||
originalTitle
|
||||
)
|
||||
}
|
||||
|
||||
func testShareExtensionActivationAcceptsSupportedRepresentationsAmongExtraAttachments() throws {
|
||||
let plistURL = URL(fileURLWithPath: #filePath)
|
||||
.deletingLastPathComponent()
|
||||
.deletingLastPathComponent()
|
||||
.appendingPathComponent("ShareExtension/Info.plist")
|
||||
let plist = try PropertyListSerialization.propertyList(
|
||||
from: Data(contentsOf: plistURL),
|
||||
format: nil
|
||||
) as? [String: Any]
|
||||
let extensionDictionary = plist?["NSExtension"] as? [String: Any]
|
||||
let attributes = extensionDictionary?["NSExtensionAttributes"] as? [String: Any]
|
||||
let rule = try XCTUnwrap(attributes?["NSExtensionActivationRule"] as? String)
|
||||
|
||||
XCTAssertTrue(rule.contains("public.url"))
|
||||
XCTAssertTrue(rule.contains("public.text"))
|
||||
XCTAssertTrue(rule.contains("public.image"))
|
||||
XCTAssertTrue(rule.contains("com.apple.property-list"))
|
||||
XCTAssertTrue(rule.contains(".@count > 0"))
|
||||
XCTAssertFalse(rule.contains("TRUEPREDICATE"))
|
||||
|
||||
let predicate = NSPredicate(format: rule)
|
||||
let youtubePayload: [String: Any] = [
|
||||
"extensionItems": [[
|
||||
"attachments": [
|
||||
["registeredTypeIdentifiers": ["public.url", "public.data"]],
|
||||
["registeredTypeIdentifiers": ["com.google.youtube.extra"]],
|
||||
]
|
||||
]]
|
||||
]
|
||||
let unsupportedPayload: [String: Any] = [
|
||||
"extensionItems": [[
|
||||
"attachments": [[
|
||||
"registeredTypeIdentifiers": ["com.adobe.pdf", "public.movie"]
|
||||
]]
|
||||
]]
|
||||
]
|
||||
XCTAssertTrue(predicate.evaluate(with: youtubePayload))
|
||||
XCTAssertFalse(predicate.evaluate(with: unsupportedPayload))
|
||||
}
|
||||
|
||||
func testManifestIDsMustBeUUIDs() {
|
||||
let id = UUID().uuidString
|
||||
XCTAssertEqual(ShareInboxSafety.normalizedManifestID(id.lowercased()), id)
|
||||
@@ -24,4 +109,201 @@ final class ShareInboxSafetyTests: XCTestCase {
|
||||
)
|
||||
XCTAssertNil(ShareInboxSafety.detectRasterImageMimeType(Data("<svg/>".utf8)))
|
||||
}
|
||||
|
||||
func testPreviewRouteMatrixAndAllowlistBypasses() {
|
||||
let publicURLs = [
|
||||
"https://x.com/affine/status/123",
|
||||
"https://www.twitter.com/affine/status/123",
|
||||
"https://youtu.be/video-id",
|
||||
"https://www.youtube.com/watch?v=video-id",
|
||||
"https://m.youtube.com/shorts/video-id",
|
||||
]
|
||||
for mode in [ShareWorkspaceMode.selfHostedPresent, .cloudOnly, .signedOut, .unknown] {
|
||||
for url in publicURLs {
|
||||
XCTAssertEqual(ShareInboxSafety.previewRoute(mode: mode, url: url), .official)
|
||||
}
|
||||
}
|
||||
|
||||
let genericURL = "https://example.com/private"
|
||||
XCTAssertEqual(ShareInboxSafety.previewRoute(mode: .selfHostedPresent, url: genericURL), .deferred)
|
||||
XCTAssertEqual(ShareInboxSafety.previewRoute(mode: .unknown, url: genericURL), .deferred)
|
||||
XCTAssertEqual(ShareInboxSafety.previewRoute(mode: .cloudOnly, url: genericURL), .official)
|
||||
XCTAssertEqual(ShareInboxSafety.previewRoute(mode: .signedOut, url: genericURL), .official)
|
||||
|
||||
for bypass in [
|
||||
"https://evil.x.com/affine/status/123",
|
||||
"https://x.com/affine/status/not-a-number",
|
||||
"https://x.com/affine/status/123/extra",
|
||||
"https://youtube.com.evil.example/watch?v=video-id",
|
||||
"https://www.youtube.com/channel/video-id",
|
||||
"https://youtu.be/video-id/extra",
|
||||
] {
|
||||
XCTAssertFalse(ShareInboxSafety.isOfficialPreviewURL(bypass), bypass)
|
||||
}
|
||||
}
|
||||
|
||||
func testWorkspaceModeSnapshotFailsClosed() throws {
|
||||
XCTAssertEqual(ShareInboxSafety.workspaceMode(from: nil), .unknown)
|
||||
XCTAssertEqual(ShareInboxSafety.workspaceMode(from: Data("invalid".utf8)), .unknown)
|
||||
let incompatible = Data(
|
||||
"{\"mode\":\"cloudOnly\",\"schemaVersion\":2,\"updatedAt\":\"2026-08-27T00:00:00Z\"}".utf8
|
||||
)
|
||||
XCTAssertEqual(ShareInboxSafety.workspaceMode(from: incompatible), .unknown)
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .iso8601
|
||||
let now = Date(timeIntervalSince1970: 1_800_000_000)
|
||||
let current = try encoder.encode(
|
||||
ShareWorkspaceModeSnapshot(mode: .selfHostedPresent, updatedAt: now)
|
||||
)
|
||||
XCTAssertEqual(ShareInboxSafety.workspaceMode(from: current, now: now), .selfHostedPresent)
|
||||
XCTAssertEqual(
|
||||
ShareInboxSafety.workspaceMode(from: current, now: now.addingTimeInterval(24 * 60 * 60 + 1)),
|
||||
.unknown
|
||||
)
|
||||
}
|
||||
|
||||
func testOldManifestDefaultsToConservativeRouteAndOriginalURLSurvives() throws {
|
||||
let id = UUID().uuidString
|
||||
let oldManifest = """
|
||||
{
|
||||
"id":"\(id)",
|
||||
"documentId":"\(UUID().uuidString)",
|
||||
"createdAt":"2026-08-27T00:00:00Z",
|
||||
"title":"Original",
|
||||
"content":{"kind":"url","url":"https://example.com/original?token=value"},
|
||||
"attachments":[]
|
||||
}
|
||||
"""
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .iso8601
|
||||
let item = try decoder.decode(ShareInboxItem.self, from: Data(oldManifest.utf8))
|
||||
XCTAssertNil(item.previewRoute)
|
||||
XCTAssertEqual(item.previewRoute ?? .deferred, .deferred)
|
||||
XCTAssertEqual(item.content.url, "https://example.com/original?token=value")
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.dateEncodingStrategy = .iso8601
|
||||
let encoded = try encoder.encode(item)
|
||||
XCTAssertEqual(try decoder.decode(ShareInboxItem.self, from: encoded).content.url, item.content.url)
|
||||
}
|
||||
|
||||
func testPreviewAndImageRequestsCarryHeadersAndCanBeCancelled() async throws {
|
||||
let family = "👨👩👧"
|
||||
let transcript = ShareLinkPreview.Transcript(
|
||||
language: nil,
|
||||
segments: [
|
||||
.init(text: " Hello\n\tworld ", startSeconds: nil, durationSeconds: nil, speaker: nil),
|
||||
.init(text: "again", startSeconds: nil, durationSeconds: nil, speaker: nil),
|
||||
],
|
||||
chapters: nil,
|
||||
truncated: nil
|
||||
)
|
||||
XCTAssertEqual(transcript.previewText, "Hello world again")
|
||||
let longTranscript = ShareLinkPreview.Transcript(
|
||||
language: nil,
|
||||
segments: [
|
||||
.init(
|
||||
text: String(repeating: family, count: 241), startSeconds: nil,
|
||||
durationSeconds: nil, speaker: nil)
|
||||
],
|
||||
chapters: nil,
|
||||
truncated: nil
|
||||
)
|
||||
XCTAssertEqual(longTranscript.previewText?.count, 241)
|
||||
XCTAssertTrue(longTranscript.previewText?.hasSuffix("…") == true)
|
||||
|
||||
let configuration = URLSessionConfiguration.ephemeral
|
||||
configuration.protocolClasses = [SharePreviewURLProtocol.self]
|
||||
let client = ShareLinkPreviewClient(
|
||||
session: URLSession(configuration: configuration), appVersion: "0.27.0")
|
||||
let started = expectation(description: "request started")
|
||||
let stopped = expectation(description: "request cancelled")
|
||||
SharePreviewURLProtocol.onStart = { _, request in
|
||||
XCTAssertEqual(request.value(forHTTPHeaderField: "User-Agent"), "AFFiNE/0.27.0")
|
||||
XCTAssertEqual(request.value(forHTTPHeaderField: "x-affine-version"), "0.27.0")
|
||||
started.fulfill()
|
||||
}
|
||||
SharePreviewURLProtocol.onStop = { stopped.fulfill() }
|
||||
defer {
|
||||
SharePreviewURLProtocol.onStart = nil
|
||||
SharePreviewURLProtocol.onStop = nil
|
||||
}
|
||||
|
||||
let task = Task {
|
||||
try await client.fetch(url: "https://www.youtube.com/watch?v=video-id")
|
||||
}
|
||||
await fulfillment(of: [started], timeout: 1)
|
||||
task.cancel()
|
||||
do {
|
||||
_ = try await task.value
|
||||
XCTFail("Cancelled preview unexpectedly completed")
|
||||
} catch {
|
||||
let urlError = error as? URLError
|
||||
XCTAssertTrue(error is CancellationError || urlError?.code == .cancelled)
|
||||
}
|
||||
await fulfillment(of: [stopped], timeout: 1)
|
||||
|
||||
let imageData = try XCTUnwrap(
|
||||
Data(
|
||||
base64Encoded:
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
|
||||
)
|
||||
)
|
||||
let imageLoaded = expectation(description: "image loaded")
|
||||
SharePreviewURLProtocol.onStart = { protocolInstance, request in
|
||||
XCTAssertEqual(request.httpMethod, "GET")
|
||||
XCTAssertEqual(request.url?.absoluteString, "https://app.affine.pro/api/worker/image-proxy")
|
||||
XCTAssertEqual(request.value(forHTTPHeaderField: "User-Agent"), "AFFiNE/0.27.0")
|
||||
XCTAssertEqual(request.value(forHTTPHeaderField: "x-affine-version"), "0.27.0")
|
||||
let response = HTTPURLResponse(
|
||||
url: request.url!, statusCode: 200, httpVersion: nil,
|
||||
headerFields: ["Content-Type": "image/png"]
|
||||
)!
|
||||
protocolInstance.client?.urlProtocol(
|
||||
protocolInstance, didReceive: response, cacheStoragePolicy: .notAllowed)
|
||||
protocolInstance.client?.urlProtocol(protocolInstance, didLoad: imageData)
|
||||
protocolInstance.client?.urlProtocolDidFinishLoading(protocolInstance)
|
||||
imageLoaded.fulfill()
|
||||
}
|
||||
SharePreviewURLProtocol.onStop = nil
|
||||
_ = try await client.fetchImage(url: "/api/worker/image-proxy")
|
||||
await fulfillment(of: [imageLoaded], timeout: 1)
|
||||
|
||||
SharePreviewURLProtocol.onStart = { protocolInstance, request in
|
||||
let response = HTTPURLResponse(
|
||||
url: request.url!, statusCode: 403, httpVersion: nil, headerFields: nil)!
|
||||
protocolInstance.client?.urlProtocol(
|
||||
protocolInstance, didReceive: response, cacheStoragePolicy: .notAllowed)
|
||||
protocolInstance.client?.urlProtocolDidFinishLoading(protocolInstance)
|
||||
}
|
||||
do {
|
||||
_ = try await client.fetchImage(url: "/api/worker/image-proxy")
|
||||
XCTFail("Failed image response unexpectedly decoded")
|
||||
} catch {
|
||||
XCTAssertEqual((error as? URLError)?.code, .badServerResponse)
|
||||
}
|
||||
|
||||
let imageStarted = expectation(description: "image request started")
|
||||
let imageStopped = expectation(description: "image request cancelled")
|
||||
SharePreviewURLProtocol.onStart = { _, request in
|
||||
XCTAssertEqual(request.value(forHTTPHeaderField: "User-Agent"), "AFFiNE/0.27.0")
|
||||
XCTAssertEqual(request.value(forHTTPHeaderField: "x-affine-version"), "0.27.0")
|
||||
imageStarted.fulfill()
|
||||
}
|
||||
SharePreviewURLProtocol.onStop = { imageStopped.fulfill() }
|
||||
let imageTask = Task {
|
||||
try await client.fetchImage(url: "/api/worker/image-proxy")
|
||||
}
|
||||
await fulfillment(of: [imageStarted], timeout: 1)
|
||||
imageTask.cancel()
|
||||
do {
|
||||
_ = try await imageTask.value
|
||||
XCTFail("Cancelled image request unexpectedly completed")
|
||||
} catch {
|
||||
let urlError = error as? URLError
|
||||
XCTAssertTrue(error is CancellationError || urlError?.code == .cancelled)
|
||||
}
|
||||
await fulfillment(of: [imageStopped], timeout: 1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,18 +23,7 @@
|
||||
<key>NSExtensionAttributes</key>
|
||||
<dict>
|
||||
<key>NSExtensionActivationRule</key>
|
||||
<dict>
|
||||
<key>NSExtensionActivationDictionaryVersion</key>
|
||||
<integer>2</integer>
|
||||
<key>NSExtensionActivationSupportsText</key>
|
||||
<true/>
|
||||
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
|
||||
<integer>1</integer>
|
||||
<key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
|
||||
<integer>1</integer>
|
||||
<key>NSExtensionActivationSupportsImageWithMaxCount</key>
|
||||
<integer>1</integer>
|
||||
</dict>
|
||||
<string>SUBQUERY(extensionItems, $extensionItem, SUBQUERY($extensionItem.attachments, $attachment, ANY $attachment.registeredTypeIdentifiers UTI-EQUALS "public.url" OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.text" OR ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image" OR ANY $attachment.registeredTypeIdentifiers UTI-EQUALS "com.apple.property-list").@count > 0).@count > 0</string>
|
||||
<key>NSExtensionJavaScriptPreprocessingFile</key>
|
||||
<string>SafariPageCapture</string>
|
||||
</dict>
|
||||
|
||||
@@ -36,44 +36,274 @@ struct ShareExtensionView: View {
|
||||
}
|
||||
|
||||
private var content: some View {
|
||||
Form {
|
||||
Section {
|
||||
ScrollView {
|
||||
VStack(alignment: .leading, spacing: 20) {
|
||||
Text("Choose a workspace in AFFiNE. This item will stay saved until then.")
|
||||
.font(.footnote)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
|
||||
Section {
|
||||
HStack(alignment: .top, spacing: 12) {
|
||||
Image(systemName: viewModel.previewImage == nil ? "doc.text" : "photo")
|
||||
.font(.title2)
|
||||
.frame(width: 32, height: 32)
|
||||
.foregroundStyle(.secondary)
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
TextField("Title", text: $viewModel.title)
|
||||
.font(.headline)
|
||||
if !viewModel.previewText.isEmpty {
|
||||
Text(viewModel.previewText)
|
||||
.lineLimit(3)
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
if viewModel.linkPreviewState != .idle {
|
||||
linkPreviewCard
|
||||
} else {
|
||||
attachmentCard
|
||||
}
|
||||
if let image = viewModel.previewImage {
|
||||
Image(uiImage: image)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
.frame(maxHeight: 180)
|
||||
}
|
||||
}
|
||||
|
||||
if let errorMessage = viewModel.errorMessage {
|
||||
Section {
|
||||
if let errorMessage = viewModel.errorMessage {
|
||||
Text(errorMessage)
|
||||
.font(.footnote)
|
||||
.foregroundStyle(.red)
|
||||
}
|
||||
}
|
||||
.padding(.horizontal, 16)
|
||||
.padding(.vertical, 20)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
.background(Color(uiColor: .systemGroupedBackground))
|
||||
}
|
||||
|
||||
private var titleField: some View {
|
||||
TextField(
|
||||
"Title",
|
||||
text: Binding(
|
||||
get: { viewModel.displayTitle },
|
||||
set: viewModel.updateTitle
|
||||
)
|
||||
)
|
||||
.font(.system(size: 17, weight: .semibold))
|
||||
}
|
||||
|
||||
private var linkPreviewCard: some View {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
switch viewModel.linkPreviewState {
|
||||
case .loading:
|
||||
previewSkeleton
|
||||
case let .loaded(preview):
|
||||
previewContent(preview)
|
||||
case .failed:
|
||||
fallbackLink(showFailure: true)
|
||||
case .deferred:
|
||||
fallbackLink(showFailure: false)
|
||||
case .idle:
|
||||
EmptyView()
|
||||
}
|
||||
|
||||
if let selectedText = viewModel.selectedText, !selectedText.isEmpty {
|
||||
Rectangle()
|
||||
.fill(Color(uiColor: .separator))
|
||||
.frame(height: 1)
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
Text("Selected text")
|
||||
.font(.footnote.weight(.semibold))
|
||||
Text(selectedText)
|
||||
.font(.footnote)
|
||||
.foregroundStyle(.secondary)
|
||||
.lineLimit(3)
|
||||
}
|
||||
.padding(.horizontal, 14)
|
||||
.padding(.top, 12)
|
||||
.padding(.bottom, 14)
|
||||
.accessibilityElement(children: .combine)
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.background(Color(uiColor: .secondarySystemGroupedBackground))
|
||||
.clipShape(RoundedRectangle(cornerRadius: 12))
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func previewContent(_ preview: ShareLinkPreview) -> some View {
|
||||
previewMedia(viewModel.linkPreviewMediaImage)
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
HStack(spacing: 6) {
|
||||
if let favicon = viewModel.linkPreviewFaviconImage {
|
||||
Image(uiImage: favicon)
|
||||
.resizable()
|
||||
.scaledToFit()
|
||||
.frame(width: 16, height: 16)
|
||||
.accessibilityHidden(true)
|
||||
} else {
|
||||
Image(systemName: "link")
|
||||
.frame(width: 16, height: 16)
|
||||
.accessibilityHidden(true)
|
||||
}
|
||||
Text(preview.siteName ?? previewHost)
|
||||
.font(.system(size: 13))
|
||||
.foregroundStyle(.secondary)
|
||||
.lineLimit(1)
|
||||
}
|
||||
.padding(.bottom, 2)
|
||||
titleField
|
||||
.lineLimit(2)
|
||||
if let description = preview.description,
|
||||
!description.isEmpty,
|
||||
description != viewModel.displayTitle
|
||||
{
|
||||
Text(description)
|
||||
.font(.system(size: 14))
|
||||
.foregroundStyle(.secondary)
|
||||
.lineLimit(2)
|
||||
}
|
||||
if let metadata = previewMetadata(preview) {
|
||||
Text(metadata)
|
||||
.font(.system(size: 13))
|
||||
.foregroundStyle(.secondary)
|
||||
.lineLimit(1)
|
||||
}
|
||||
}
|
||||
if let transcript = preview.transcript?.previewText {
|
||||
transcriptPreview(transcript)
|
||||
}
|
||||
}
|
||||
.padding(14)
|
||||
}
|
||||
|
||||
private func transcriptPreview(_ text: String) -> some View {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
Rectangle()
|
||||
.fill(Color(uiColor: .separator))
|
||||
.frame(height: 1)
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
HStack(spacing: 6) {
|
||||
Image(systemName: "waveform")
|
||||
.frame(width: 16, height: 16)
|
||||
.accessibilityHidden(true)
|
||||
Text("Transcript")
|
||||
.font(.footnote.weight(.semibold))
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
Text(text)
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(.secondary)
|
||||
.lineLimit(viewModel.selectedText?.isEmpty == false ? 2 : 3)
|
||||
}
|
||||
.padding(.top, 10)
|
||||
}
|
||||
.padding(.top, 8)
|
||||
.accessibilityElement(children: .ignore)
|
||||
.accessibilityLabel("Transcript preview: \(text)")
|
||||
}
|
||||
|
||||
private func fallbackLink(showFailure: Bool) -> some View {
|
||||
HStack(alignment: .top, spacing: 12) {
|
||||
Image(systemName: "link")
|
||||
.font(.title2)
|
||||
.foregroundStyle(.secondary)
|
||||
.accessibilityHidden(true)
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
titleField
|
||||
Text(previewHost)
|
||||
.font(.system(size: 13))
|
||||
.foregroundStyle(.secondary)
|
||||
if showFailure {
|
||||
Text("Preview unavailable")
|
||||
.font(.system(size: 13))
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(14)
|
||||
}
|
||||
|
||||
private var previewSkeleton: some View {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
Rectangle()
|
||||
.fill(.quaternary)
|
||||
.frame(maxWidth: .infinity)
|
||||
.frame(height: 180)
|
||||
GeometryReader { geometry in
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
RoundedRectangle(cornerRadius: 6)
|
||||
.fill(.quaternary)
|
||||
.frame(width: geometry.size.width * 0.6, height: 12)
|
||||
RoundedRectangle(cornerRadius: 6)
|
||||
.fill(.quaternary)
|
||||
.frame(width: geometry.size.width * 0.9, height: 16)
|
||||
RoundedRectangle(cornerRadius: 6)
|
||||
.fill(.quaternary)
|
||||
.frame(width: geometry.size.width * 0.55, height: 12)
|
||||
}
|
||||
}
|
||||
.frame(height: 56)
|
||||
.padding(14)
|
||||
}
|
||||
.accessibilityElement(children: .ignore)
|
||||
.accessibilityLabel("Loading link preview")
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private func previewMedia(_ image: UIImage?) -> some View {
|
||||
if let image {
|
||||
Image(uiImage: image)
|
||||
.resizable()
|
||||
.scaledToFill()
|
||||
.frame(maxWidth: .infinity)
|
||||
.frame(height: 180)
|
||||
.clipped()
|
||||
.accessibilityHidden(true)
|
||||
} else {
|
||||
mediaPlaceholder
|
||||
.frame(maxWidth: .infinity)
|
||||
.frame(height: 180)
|
||||
.accessibilityHidden(true)
|
||||
}
|
||||
}
|
||||
|
||||
private var mediaPlaceholder: some View {
|
||||
ZStack {
|
||||
Color(uiColor: .systemGroupedBackground)
|
||||
Image(systemName: "link")
|
||||
.font(.system(size: 24))
|
||||
.foregroundStyle(.tertiary)
|
||||
}
|
||||
}
|
||||
|
||||
private var attachmentCard: some View {
|
||||
VStack(alignment: .leading, spacing: 0) {
|
||||
if let image = viewModel.previewImage {
|
||||
Image(uiImage: image)
|
||||
.resizable()
|
||||
.scaledToFill()
|
||||
.frame(maxWidth: .infinity)
|
||||
.aspectRatio(16 / 9, contentMode: .fit)
|
||||
.frame(maxHeight: 180)
|
||||
.clipped()
|
||||
.accessibilityHidden(true)
|
||||
}
|
||||
HStack(alignment: .top, spacing: 12) {
|
||||
Image(systemName: viewModel.previewImage == nil ? "doc.text" : "photo")
|
||||
.font(.title2)
|
||||
.frame(width: 32, height: 32)
|
||||
.foregroundStyle(.secondary)
|
||||
.accessibilityHidden(true)
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
titleField
|
||||
if !viewModel.previewText.isEmpty {
|
||||
Text(viewModel.previewText)
|
||||
.lineLimit(3)
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(14)
|
||||
}
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
.background(Color(uiColor: .secondarySystemGroupedBackground))
|
||||
.clipShape(RoundedRectangle(cornerRadius: 12))
|
||||
}
|
||||
|
||||
private var previewHost: String {
|
||||
guard let value = viewModel.sharedURL, let url = URL(string: value) else { return "Link" }
|
||||
return url.host ?? value
|
||||
}
|
||||
|
||||
private func previewMetadata(_ preview: ShareLinkPreview) -> String? {
|
||||
var values: [String] = []
|
||||
if let author = preview.author?.name { values.append(author) }
|
||||
if let duration = preview.durationSeconds {
|
||||
values.append(String(format: "%d:%02d", Int(duration) / 60, Int(duration) % 60))
|
||||
}
|
||||
return values.prefix(2).isEmpty ? nil : values.prefix(2).joined(separator: " · ")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,9 @@ final class ShareViewModel: ObservableObject {
|
||||
@Published var isSaving = false
|
||||
@Published var hasSaved = false
|
||||
@Published var errorMessage: String?
|
||||
@Published var linkPreviewState: ShareLinkPreviewState = .idle
|
||||
@Published var linkPreviewMediaImage: UIImage?
|
||||
@Published var linkPreviewFaviconImage: UIImage?
|
||||
|
||||
var actionTitle: String {
|
||||
"Open AFFiNE"
|
||||
@@ -23,10 +26,43 @@ final class ShareViewModel: ObservableObject {
|
||||
}
|
||||
|
||||
private var draft: SharePayloadDraft?
|
||||
private var previewRoute: SharePreviewRoute = .deferred
|
||||
private var previewTask: Task<Void, Never>?
|
||||
private var userEditedTitle: String?
|
||||
private let store: ShareInboxStore
|
||||
private let previewClient: ShareLinkPreviewClient
|
||||
|
||||
init(store: ShareInboxStore = .shared) {
|
||||
init(
|
||||
store: ShareInboxStore = .shared,
|
||||
previewClient: ShareLinkPreviewClient = ShareLinkPreviewClient()
|
||||
) {
|
||||
self.store = store
|
||||
self.previewClient = previewClient
|
||||
}
|
||||
|
||||
var linkPreview: ShareLinkPreview? {
|
||||
guard case let .loaded(preview) = linkPreviewState else { return nil }
|
||||
return preview
|
||||
}
|
||||
|
||||
var displayTitle: String {
|
||||
ShareInboxSafety.previewTitle(
|
||||
original: title,
|
||||
userEdited: userEditedTitle,
|
||||
serverTitle: linkPreview?.title
|
||||
)
|
||||
}
|
||||
|
||||
var sharedURL: String? { draft?.content?.url }
|
||||
|
||||
var selectedText: String? {
|
||||
guard draft?.content?.kind == .url else { return nil }
|
||||
return draft?.content?.text
|
||||
}
|
||||
|
||||
func updateTitle(_ value: String) {
|
||||
userEditedTitle = value
|
||||
title = value
|
||||
}
|
||||
|
||||
func load(from extensionContext: NSExtensionContext?) async {
|
||||
@@ -36,21 +72,54 @@ final class ShareViewModel: ObservableObject {
|
||||
let items = extensionContext?.inputItems.compactMap { $0 as? NSExtensionItem } ?? []
|
||||
let built = await SharePayloadBuilder.build(from: items)
|
||||
draft = built
|
||||
userEditedTitle = nil
|
||||
title = built.title
|
||||
previewText = built.previewText
|
||||
errorMessage = built.errorMessage
|
||||
linkPreviewMediaImage = nil
|
||||
linkPreviewFaviconImage = nil
|
||||
if let file = built.file {
|
||||
previewImage = UIImage(data: file.data)?
|
||||
.preparingThumbnail(of: CGSize(width: 480, height: 480))
|
||||
}
|
||||
guard built.content?.kind == .url, let url = built.content?.url else { return }
|
||||
previewRoute = ShareInboxSafety.previewRoute(mode: store.workspaceMode(), url: url)
|
||||
guard previewRoute == .official else {
|
||||
linkPreviewState = .deferred
|
||||
return
|
||||
}
|
||||
linkPreviewState = .loading
|
||||
previewTask = Task { [weak self] in
|
||||
guard let self else { return }
|
||||
do {
|
||||
let preview = try await previewClient.fetch(url: url)
|
||||
guard !Task.isCancelled else { return }
|
||||
linkPreviewState = .loaded(preview)
|
||||
async let media = previewClient.fetchImageIfPresent(url: preview.images?.first)
|
||||
async let favicon = previewClient.fetchImageIfPresent(url: preview.favicons?.first)
|
||||
let images = await (media, favicon)
|
||||
guard !Task.isCancelled else { return }
|
||||
linkPreviewMediaImage = images.0
|
||||
linkPreviewFaviconImage = images.1
|
||||
} catch is CancellationError {
|
||||
return
|
||||
} catch {
|
||||
guard !Task.isCancelled else { return }
|
||||
linkPreviewState = .failed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func save() async -> Bool {
|
||||
guard !isSaving, !hasSaved else { return false }
|
||||
previewTask?.cancel()
|
||||
isSaving = true
|
||||
defer { isSaving = false }
|
||||
|
||||
let trimmedTitle = title.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
let trimmedTitle = ShareInboxSafety.manifestTitle(
|
||||
original: draft?.title ?? title,
|
||||
userEdited: userEditedTitle
|
||||
)
|
||||
guard !trimmedTitle.isEmpty else {
|
||||
errorMessage = "Title is required."
|
||||
return false
|
||||
@@ -77,6 +146,7 @@ final class ShareViewModel: ObservableObject {
|
||||
id: itemId,
|
||||
title: trimmedTitle,
|
||||
content: content,
|
||||
previewRoute: previewRoute,
|
||||
previewText: draft.previewText,
|
||||
attachments: attachments
|
||||
)
|
||||
|
||||
@@ -10,5 +10,9 @@ enum ShareInboxConstants {
|
||||
static let inboxDirectoryName = "ShareInbox"
|
||||
static let attachmentsDirectoryName = "Attachments"
|
||||
static let invalidDirectoryName = "Invalid"
|
||||
static let workspaceModeFileName = "ShareWorkspaceMode.json"
|
||||
static let officialLinkPreviewURL = URL(
|
||||
string: "https://app.affine.pro/api/worker/link-preview"
|
||||
)!
|
||||
static let openInboxURL = URL(string: "affine://share-inbox")!
|
||||
}
|
||||
|
||||
@@ -36,6 +36,7 @@ struct ShareInboxItem: Codable, Equatable, Identifiable {
|
||||
var createdAt: Date
|
||||
var title: String
|
||||
var content: ShareInboxContent
|
||||
var previewRoute: SharePreviewRoute?
|
||||
var target: ShareInboxTarget?
|
||||
var previewText: String?
|
||||
var attachments: [ShareInboxAttachment]
|
||||
@@ -48,6 +49,7 @@ struct ShareInboxItem: Codable, Equatable, Identifiable {
|
||||
createdAt: Date = Date(),
|
||||
title: String,
|
||||
content: ShareInboxContent,
|
||||
previewRoute: SharePreviewRoute? = nil,
|
||||
target: ShareInboxTarget? = nil,
|
||||
previewText: String? = nil,
|
||||
attachments: [ShareInboxAttachment] = [],
|
||||
@@ -59,6 +61,7 @@ struct ShareInboxItem: Codable, Equatable, Identifiable {
|
||||
self.createdAt = createdAt
|
||||
self.title = title
|
||||
self.content = content
|
||||
self.previewRoute = previewRoute
|
||||
self.target = target
|
||||
self.previewText = previewText
|
||||
self.attachments = attachments
|
||||
|
||||
@@ -1,6 +1,44 @@
|
||||
import Foundation
|
||||
|
||||
enum ShareWorkspaceMode: String, Codable {
|
||||
case selfHostedPresent
|
||||
case cloudOnly
|
||||
case signedOut
|
||||
case unknown
|
||||
}
|
||||
|
||||
enum SharePreviewRoute: String, Codable {
|
||||
case official
|
||||
case deferred
|
||||
}
|
||||
|
||||
struct ShareWorkspaceModeSnapshot: Codable, Equatable {
|
||||
static let schemaVersion = 1
|
||||
|
||||
var mode: ShareWorkspaceMode
|
||||
var schemaVersion: Int
|
||||
var updatedAt: Date
|
||||
|
||||
init(mode: ShareWorkspaceMode, updatedAt: Date = Date()) {
|
||||
self.mode = mode
|
||||
self.schemaVersion = Self.schemaVersion
|
||||
self.updatedAt = updatedAt
|
||||
}
|
||||
}
|
||||
|
||||
enum ShareInboxSafety {
|
||||
private static let workspaceModeMaxAge: TimeInterval = 24 * 60 * 60
|
||||
|
||||
static func manifestTitle(original: String, userEdited: String?) -> String {
|
||||
(userEdited ?? original).trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
}
|
||||
|
||||
static func previewTitle(original: String, userEdited: String?, serverTitle: String?) -> String {
|
||||
if let userEdited { return userEdited }
|
||||
guard let serverTitle, !serverTitle.isEmpty else { return original }
|
||||
return serverTitle
|
||||
}
|
||||
|
||||
static func normalizedManifestID(_ value: String) -> String? {
|
||||
UUID(uuidString: value)?.uuidString
|
||||
}
|
||||
@@ -22,6 +60,56 @@ enum ShareInboxSafety {
|
||||
return url.absoluteString
|
||||
}
|
||||
|
||||
static func isOfficialPreviewURL(_ value: String) -> Bool {
|
||||
guard let normalized = normalizedWebURL(value), let url = URL(string: normalized) else {
|
||||
return false
|
||||
}
|
||||
let host = url.host?.lowercased()
|
||||
let components = url.pathComponents.filter { $0 != "/" }
|
||||
if ["x.com", "www.x.com", "twitter.com", "www.twitter.com"].contains(host) {
|
||||
return components.count == 3
|
||||
&& components[1] == "status"
|
||||
&& !components[2].isEmpty
|
||||
&& components[2].allSatisfy(\.isNumber)
|
||||
}
|
||||
if host == "youtu.be" {
|
||||
return components.count == 1 && !components[0].isEmpty
|
||||
}
|
||||
if ["youtube.com", "www.youtube.com", "m.youtube.com"].contains(host) {
|
||||
if url.path == "/watch" {
|
||||
return !(URLComponents(url: url, resolvingAgainstBaseURL: false)?
|
||||
.queryItems?.first(where: { $0.name == "v" })?.value?.isEmpty ?? true)
|
||||
}
|
||||
return components.count == 2
|
||||
&& ["shorts", "live", "embed"].contains(components[0])
|
||||
&& !components[1].isEmpty
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
static func previewRoute(mode: ShareWorkspaceMode, url: String) -> SharePreviewRoute {
|
||||
if isOfficialPreviewURL(url) { return .official }
|
||||
switch mode {
|
||||
case .cloudOnly, .signedOut:
|
||||
return .official
|
||||
case .selfHostedPresent, .unknown:
|
||||
return .deferred
|
||||
}
|
||||
}
|
||||
|
||||
static func workspaceMode(from data: Data?, now: Date = Date()) -> ShareWorkspaceMode {
|
||||
guard let data else { return .unknown }
|
||||
let decoder = JSONDecoder()
|
||||
decoder.dateDecodingStrategy = .iso8601
|
||||
guard let snapshot = try? decoder.decode(ShareWorkspaceModeSnapshot.self, from: data),
|
||||
snapshot.schemaVersion == ShareWorkspaceModeSnapshot.schemaVersion,
|
||||
(0...workspaceModeMaxAge).contains(now.timeIntervalSince(snapshot.updatedAt))
|
||||
else {
|
||||
return .unknown
|
||||
}
|
||||
return snapshot.mode
|
||||
}
|
||||
|
||||
static func detectRasterImageMimeType(_ data: Data) -> String? {
|
||||
let bytes = [UInt8](data.prefix(12))
|
||||
if bytes.starts(with: [0xFF, 0xD8, 0xFF]) {
|
||||
|
||||
@@ -102,6 +102,21 @@ final class ShareInboxStore {
|
||||
try encoder.encode(item).write(to: fileURL, options: .atomic)
|
||||
}
|
||||
|
||||
func updateWorkspaceMode(_ mode: ShareWorkspaceMode) throws {
|
||||
guard let containerURL else { throw ShareInboxError.containerUnavailable }
|
||||
let url = containerURL.appendingPathComponent(ShareInboxConstants.workspaceModeFileName)
|
||||
try encoder.encode(ShareWorkspaceModeSnapshot(mode: mode)).write(to: url, options: .atomic)
|
||||
}
|
||||
|
||||
func workspaceMode() -> ShareWorkspaceMode {
|
||||
guard let containerURL,
|
||||
let data = try? Data(
|
||||
contentsOf: containerURL.appendingPathComponent(ShareInboxConstants.workspaceModeFileName)
|
||||
)
|
||||
else { return .unknown }
|
||||
return ShareInboxSafety.workspaceMode(from: data)
|
||||
}
|
||||
|
||||
func pendingItems() -> [ShareInboxItem] {
|
||||
guard ensureDirectories(), let inboxDirectoryURL else { return [] }
|
||||
guard let urls = try? fileManager.contentsOfDirectory(
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
struct ShareLinkPreview: Decodable, Equatable {
|
||||
struct Author: Decodable, Equatable {
|
||||
var name: String
|
||||
var handle: String?
|
||||
var avatar: String?
|
||||
}
|
||||
|
||||
struct Transcript: Decodable, Equatable {
|
||||
struct Segment: Decodable, Equatable {
|
||||
var text: String
|
||||
var startSeconds: Double?
|
||||
var durationSeconds: Double?
|
||||
var speaker: String?
|
||||
}
|
||||
|
||||
struct Chapter: Decodable, Equatable {
|
||||
var title: String
|
||||
var startSeconds: Double
|
||||
}
|
||||
|
||||
var language: String?
|
||||
var segments: [Segment]
|
||||
var chapters: [Chapter]?
|
||||
var truncated: Bool?
|
||||
}
|
||||
|
||||
var url: String
|
||||
var title: String?
|
||||
var siteName: String?
|
||||
var description: String?
|
||||
var images: [String]?
|
||||
var favicons: [String]?
|
||||
var mediaType: String?
|
||||
var provider: String?
|
||||
var author: Author?
|
||||
var publishedAt: String?
|
||||
var durationSeconds: Double?
|
||||
var transcript: Transcript?
|
||||
}
|
||||
|
||||
extension ShareLinkPreview.Transcript {
|
||||
var previewText: String? {
|
||||
let text = segments
|
||||
.map { $0.text.split(whereSeparator: \.isWhitespace).joined(separator: " ") }
|
||||
.filter { !$0.isEmpty }
|
||||
.joined(separator: " ")
|
||||
guard !text.isEmpty else { return nil }
|
||||
guard text.count > 240 else { return text }
|
||||
return String(text.prefix(240)) + "…"
|
||||
}
|
||||
}
|
||||
|
||||
enum ShareLinkPreviewState: Equatable {
|
||||
case idle
|
||||
case deferred
|
||||
case loading
|
||||
case loaded(ShareLinkPreview)
|
||||
case failed
|
||||
}
|
||||
|
||||
struct ShareLinkPreviewClient {
|
||||
private let session: URLSession
|
||||
private let appVersion: String
|
||||
|
||||
init(session: URLSession? = nil, appVersion: String? = nil) {
|
||||
self.appVersion = appVersion ?? Self.bundledAppVersion
|
||||
if let session {
|
||||
self.session = session
|
||||
} else {
|
||||
let configuration = URLSessionConfiguration.ephemeral
|
||||
configuration.timeoutIntervalForRequest = 4
|
||||
configuration.timeoutIntervalForResource = 6
|
||||
configuration.requestCachePolicy = .reloadIgnoringLocalCacheData
|
||||
configuration.urlCache = nil
|
||||
self.session = URLSession(configuration: configuration)
|
||||
}
|
||||
}
|
||||
|
||||
func fetch(url: String) async throws -> ShareLinkPreview {
|
||||
guard let normalized = ShareInboxSafety.normalizedWebURL(url) else {
|
||||
throw URLError(.badURL)
|
||||
}
|
||||
var request = URLRequest(url: ShareInboxConstants.officialLinkPreviewURL)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
addClientHeaders(to: &request)
|
||||
request.httpBody = try JSONEncoder().encode(
|
||||
Request(url: normalized, include: ["transcript"])
|
||||
)
|
||||
let (data, response) = try await session.data(for: request)
|
||||
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
|
||||
throw URLError(.badServerResponse)
|
||||
}
|
||||
return try JSONDecoder().decode(ShareLinkPreview.self, from: data)
|
||||
}
|
||||
|
||||
func fetchImage(url value: String) async throws -> UIImage {
|
||||
guard let candidate = URL(
|
||||
string: value,
|
||||
relativeTo: ShareInboxConstants.officialLinkPreviewURL
|
||||
) else {
|
||||
throw URLError(.badURL)
|
||||
}
|
||||
let resolved = candidate.absoluteURL
|
||||
guard
|
||||
let normalized = ShareInboxSafety.normalizedWebURL(resolved.absoluteString),
|
||||
let url = URL(string: normalized)
|
||||
else {
|
||||
throw URLError(.badURL)
|
||||
}
|
||||
var request = URLRequest(
|
||||
url: url,
|
||||
cachePolicy: .reloadIgnoringLocalCacheData,
|
||||
timeoutInterval: 3
|
||||
)
|
||||
addClientHeaders(to: &request)
|
||||
let (data, response) = try await session.data(for: request)
|
||||
guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {
|
||||
throw URLError(.badServerResponse)
|
||||
}
|
||||
guard let image = UIImage(data: data) else {
|
||||
throw URLError(.cannotDecodeContentData)
|
||||
}
|
||||
return image
|
||||
}
|
||||
|
||||
func fetchImageIfPresent(url: String?) async -> UIImage? {
|
||||
guard let url else { return nil }
|
||||
return try? await fetchImage(url: url)
|
||||
}
|
||||
|
||||
private func addClientHeaders(to request: inout URLRequest) {
|
||||
request.setValue("AFFiNE/\(appVersion)", forHTTPHeaderField: "User-Agent")
|
||||
request.setValue(appVersion, forHTTPHeaderField: "x-affine-version")
|
||||
}
|
||||
|
||||
private struct Request: Encodable {
|
||||
var url: String
|
||||
var include: [String]
|
||||
}
|
||||
|
||||
private struct AppConfig: Decodable {
|
||||
var affineVersion: String
|
||||
}
|
||||
|
||||
private static var bundledAppVersion: String {
|
||||
if let url = Bundle.main.url(forResource: "capacitor.config", withExtension: "json"),
|
||||
let data = try? Data(contentsOf: url),
|
||||
let version = try? JSONDecoder().decode(AppConfig.self, from: data).affineVersion,
|
||||
!version.isEmpty
|
||||
{
|
||||
return version
|
||||
}
|
||||
if let version = Bundle.main.object(
|
||||
forInfoDictionaryKey: "CFBundleShortVersionString"
|
||||
) as? String, !version.isEmpty {
|
||||
return version
|
||||
}
|
||||
return "0.2"
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ const config: CapacitorConfig & AppConfig = {
|
||||
ios: {
|
||||
scheme: 'AFFiNE',
|
||||
path: '.',
|
||||
appendUserAgent: `iOS AFFiNE/${packageJson.version}`,
|
||||
webContentsDebuggingEnabled: true,
|
||||
// Silence Capacitor's bridge logging (⚡️ TO JS / ⚡️ To Native -> / ⚡️ [log]).
|
||||
loggingBehavior: 'none',
|
||||
|
||||
@@ -4,6 +4,9 @@ import type {
|
||||
} from '@affine/core/mobile/components/share-import-controller/types';
|
||||
|
||||
export interface ShareInboxPlugin {
|
||||
updateWorkspaceMode(options: {
|
||||
mode: 'selfHostedPresent' | 'cloudOnly' | 'signedOut' | 'unknown';
|
||||
}): Promise<void>;
|
||||
listPending(): Promise<{ items: PendingShareItem[] }>;
|
||||
updateTarget(options: {
|
||||
itemId: string;
|
||||
|
||||
@@ -14,6 +14,9 @@ const blobToDataURL = (blob: Blob) =>
|
||||
});
|
||||
|
||||
export const shareInboxProvider: ShareInboxProvider = {
|
||||
async updateWorkspaceMode(mode) {
|
||||
await plugin.updateWorkspaceMode({ mode });
|
||||
},
|
||||
async listPending() {
|
||||
return (await plugin.listPending()).items;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user