mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-26 18:45:22 +00:00
Require review before importing shared content (#1430)
Replaces the share-extension handoff that auto-sent shared text/URLs into the current channel with a validated, single-slot 24h envelope that shows the destination and a bounded preview and requires an explicit tap to copy into the composer — it never sends. Rejects malformed/oversized/control-character/non-HTTP(S)/expired payloads (including bidi-override U+202E), and clears on consume/cancel/expiry/panic. Rebased onto main with Persian strings added for all new keys (30-locale coverage verified), plus a panic-recovery clear so an envelope staged during an interrupted wipe cannot survive relaunch.
This commit is contained in:
@@ -2,11 +2,6 @@ import BitFoundation
|
||||
import Combine
|
||||
import Foundation
|
||||
|
||||
enum SharedContentKind: String, Sendable, Equatable {
|
||||
case text
|
||||
case url
|
||||
}
|
||||
|
||||
enum RuntimeScenePhase: String, Sendable, Equatable {
|
||||
case active
|
||||
case inactive
|
||||
@@ -25,7 +20,7 @@ enum AppEvent: Sendable, Equatable {
|
||||
case startupCompleted
|
||||
case scenePhaseChanged(RuntimeScenePhase)
|
||||
case openedURL(String)
|
||||
case sharedContentAccepted(SharedContentKind)
|
||||
case sharedContentReadyForReview(SharedContentKind)
|
||||
case notificationOpened(peerID: PeerID?)
|
||||
case deepLinkOpened(String)
|
||||
case torLifecycleChanged(TorLifecycleEvent)
|
||||
|
||||
@@ -20,6 +20,7 @@ final class AppChromeModel: ObservableObject {
|
||||
@Published var showScreenshotPrivacyWarning = false
|
||||
|
||||
private let chatViewModel: ChatViewModel
|
||||
private let onPanicWipe: () -> Void
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
/// The composer owns capture state above ChatViewModel. ContentView
|
||||
/// installs this hook so both panic entry points synchronously stop it.
|
||||
@@ -28,8 +29,13 @@ final class AppChromeModel: ObservableObject {
|
||||
/// Bulletin-board coordinator, created on first use of the board sheet.
|
||||
private(set) lazy var boardManager = BoardManager(transport: chatViewModel.meshService)
|
||||
|
||||
init(chatViewModel: ChatViewModel, privateInboxModel: PrivateInboxModel) {
|
||||
init(
|
||||
chatViewModel: ChatViewModel,
|
||||
privateInboxModel: PrivateInboxModel,
|
||||
onPanicWipe: @escaping () -> Void = {}
|
||||
) {
|
||||
self.chatViewModel = chatViewModel
|
||||
self.onPanicWipe = onPanicWipe
|
||||
self.nickname = chatViewModel.nickname
|
||||
|
||||
bind(privateInboxModel: privateInboxModel)
|
||||
@@ -106,6 +112,7 @@ final class AppChromeModel: ObservableObject {
|
||||
|
||||
func panicClearAllData() {
|
||||
prepareForPanic?()
|
||||
onPanicWipe()
|
||||
chatViewModel.panicClearAllData()
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ final class AppRuntime: ObservableObject {
|
||||
let peerListModel: PeerListModel
|
||||
let appChromeModel: AppChromeModel
|
||||
let boardAlertsModel: BoardAlertsModel
|
||||
let sharedContentImportModel: SharedContentImportModel
|
||||
|
||||
private let idBridge: NostrIdentityBridge
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
@@ -41,7 +42,8 @@ final class AppRuntime: ObservableObject {
|
||||
|
||||
init(
|
||||
keychain: KeychainManagerProtocol = KeychainManager.makeDefault(),
|
||||
idBridge: NostrIdentityBridge = NostrIdentityBridge()
|
||||
idBridge: NostrIdentityBridge = NostrIdentityBridge(),
|
||||
sharedContentStore: SharedContentStore? = nil
|
||||
) {
|
||||
self.idBridge = idBridge
|
||||
let conversations = ConversationStore()
|
||||
@@ -84,9 +86,20 @@ final class AppRuntime: ObservableObject {
|
||||
peerIdentityStore: peerIdentityStore,
|
||||
locationPresenceStore: locationPresenceStore
|
||||
)
|
||||
let resolvedSharedContentStore: SharedContentStore?
|
||||
if let sharedContentStore {
|
||||
resolvedSharedContentStore = sharedContentStore
|
||||
} else if let sharedDefaults = UserDefaults(suiteName: BitchatApp.groupID) {
|
||||
resolvedSharedContentStore = SharedContentStore(defaults: sharedDefaults)
|
||||
} else {
|
||||
resolvedSharedContentStore = nil
|
||||
}
|
||||
let sharedContentImportModel = SharedContentImportModel(store: resolvedSharedContentStore)
|
||||
self.sharedContentImportModel = sharedContentImportModel
|
||||
self.appChromeModel = AppChromeModel(
|
||||
chatViewModel: self.chatViewModel,
|
||||
privateInboxModel: self.privateInboxModel
|
||||
privateInboxModel: self.privateInboxModel,
|
||||
onPanicWipe: { sharedContentImportModel.discardAll() }
|
||||
)
|
||||
let chatViewModel = self.chatViewModel
|
||||
self.boardAlertsModel = BoardAlertsModel(
|
||||
@@ -106,7 +119,6 @@ final class AppRuntime: ObservableObject {
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
if chatViewModel.networkActivationAllowed {
|
||||
GeoRelayDirectory.shared.prefetchIfNeeded()
|
||||
}
|
||||
@@ -328,45 +340,22 @@ private extension AppRuntime {
|
||||
}
|
||||
|
||||
func checkForSharedContent() {
|
||||
guard chatViewModel.networkActivationAllowed else { return }
|
||||
guard let userDefaults = UserDefaults(suiteName: BitchatApp.groupID) else { return }
|
||||
let clearSharedContent = {
|
||||
userDefaults.removeObject(forKey: "sharedContent")
|
||||
userDefaults.removeObject(forKey: "sharedContentType")
|
||||
userDefaults.removeObject(forKey: "sharedContentDate")
|
||||
let previousID = sharedContentImportModel.offer?.id
|
||||
guard let payload = sharedContentImportModel.refresh(
|
||||
destination: currentSharedContentDestination
|
||||
) else { return }
|
||||
|
||||
if previousID != payload.id {
|
||||
record(.sharedContentReadyForReview(payload.kind))
|
||||
}
|
||||
}
|
||||
|
||||
guard let sharedContent = userDefaults.string(forKey: "sharedContent"),
|
||||
let sharedDate = userDefaults.object(forKey: "sharedContentDate") as? Date else {
|
||||
// A partial or malformed handoff must not linger in the shared
|
||||
// app-group container indefinitely.
|
||||
clearSharedContent()
|
||||
return
|
||||
}
|
||||
|
||||
guard Date().timeIntervalSince(sharedDate) < TransportConfig.uiShareAcceptWindowSeconds else {
|
||||
clearSharedContent()
|
||||
return
|
||||
}
|
||||
|
||||
let contentKind = SharedContentKind(rawValue: userDefaults.string(forKey: "sharedContentType") ?? "") ?? .text
|
||||
|
||||
clearSharedContent()
|
||||
|
||||
switch contentKind {
|
||||
case .url:
|
||||
if let data = sharedContent.data(using: .utf8),
|
||||
let urlData = try? JSONSerialization.jsonObject(with: data) as? [String: String],
|
||||
let url = urlData["url"] {
|
||||
chatViewModel.sendMessage(url)
|
||||
} else {
|
||||
chatViewModel.sendMessage(sharedContent)
|
||||
}
|
||||
case .text:
|
||||
chatViewModel.sendMessage(sharedContent)
|
||||
}
|
||||
|
||||
record(.sharedContentAccepted(contentKind))
|
||||
var currentSharedContentDestination: SharedContentDestination {
|
||||
SharedContentDestination.resolve(
|
||||
selectedPrivatePeerID: privateConversationModel.selectedPeerID,
|
||||
privateDisplayName: privateConversationModel.selectedHeaderState?.displayName,
|
||||
activeChannel: locationChannelsModel.selectedChannel
|
||||
)
|
||||
}
|
||||
|
||||
func handleNostrRelayConnectionChanged(_ isConnected: Bool) {
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
import BitFoundation
|
||||
import Combine
|
||||
import Foundation
|
||||
|
||||
enum SharedContentDestination: Sendable, Equatable {
|
||||
case mesh
|
||||
case geohash(String)
|
||||
case privateConversation(peerID: PeerID, displayName: String)
|
||||
|
||||
static func resolve(
|
||||
selectedPrivatePeerID: PeerID?,
|
||||
privateDisplayName: String?,
|
||||
activeChannel: ChannelID
|
||||
) -> SharedContentDestination {
|
||||
if let selectedPrivatePeerID {
|
||||
let fallback = String(selectedPrivatePeerID.id.prefix(12))
|
||||
return .privateConversation(
|
||||
peerID: selectedPrivatePeerID,
|
||||
displayName: privateDisplayName?.trimmedOrNilIfEmpty ?? fallback
|
||||
)
|
||||
}
|
||||
|
||||
switch activeChannel {
|
||||
case .mesh:
|
||||
return .mesh
|
||||
case .location(let channel):
|
||||
return .geohash(channel.geohash.lowercased())
|
||||
}
|
||||
}
|
||||
|
||||
var displayName: String {
|
||||
switch self {
|
||||
case .mesh:
|
||||
return "#mesh"
|
||||
case .geohash(let geohash):
|
||||
return "#\(geohash)"
|
||||
case .privateConversation(_, let displayName):
|
||||
return displayName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct SharedContentOffer: Identifiable, Sendable, Equatable {
|
||||
let payload: SharedContentPayload
|
||||
let destination: SharedContentDestination
|
||||
|
||||
var id: UUID { payload.id }
|
||||
}
|
||||
|
||||
/// Holds a pending extension handoff until the user chooses a destination and
|
||||
/// explicitly adds it to the composer. This type has no send dependency by
|
||||
/// design: confirming an import can never transmit a message.
|
||||
@MainActor
|
||||
final class SharedContentImportModel: ObservableObject {
|
||||
@Published private(set) var offer: SharedContentOffer?
|
||||
|
||||
private let store: SharedContentStore?
|
||||
|
||||
init(store: SharedContentStore?) {
|
||||
self.store = store
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
func refresh(
|
||||
destination: SharedContentDestination,
|
||||
now: Date = Date()
|
||||
) -> SharedContentPayload? {
|
||||
guard let payload = store?.pending(now: now) else {
|
||||
offer = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
let nextOffer = SharedContentOffer(payload: payload, destination: destination)
|
||||
if offer != nextOffer {
|
||||
offer = nextOffer
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func updateDestination(_ destination: SharedContentDestination) {
|
||||
guard let offer, offer.destination != destination else { return }
|
||||
self.offer = SharedContentOffer(payload: offer.payload, destination: destination)
|
||||
}
|
||||
|
||||
/// Returns composer text only when the currently displayed destination is
|
||||
/// still current and the reviewed envelope is still the stored envelope.
|
||||
/// A destination change updates the prompt and requires another tap.
|
||||
func confirm(
|
||||
destination: SharedContentDestination,
|
||||
now: Date = Date()
|
||||
) -> String? {
|
||||
guard let offer else { return nil }
|
||||
guard offer.destination == destination else {
|
||||
updateDestination(destination)
|
||||
return nil
|
||||
}
|
||||
guard let payload = store?.consume(id: offer.id, now: now) else {
|
||||
_ = refresh(destination: destination, now: now)
|
||||
return nil
|
||||
}
|
||||
|
||||
self.offer = nil
|
||||
return payload.composerText
|
||||
}
|
||||
|
||||
func cancel(destination: SharedContentDestination, now: Date = Date()) {
|
||||
guard let offer else { return }
|
||||
store?.discard(id: offer.id)
|
||||
self.offer = nil
|
||||
// If a newer share replaced the reviewed envelope, surface it rather
|
||||
// than losing it with the older cancellation.
|
||||
_ = refresh(destination: destination, now: now)
|
||||
}
|
||||
|
||||
func discardAll() {
|
||||
store?.discardAll()
|
||||
offer = nil
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,7 @@ struct BitchatApp: App {
|
||||
.environmentObject(runtime.peerListModel)
|
||||
.environmentObject(runtime.appChromeModel)
|
||||
.environmentObject(runtime.boardAlertsModel)
|
||||
.environmentObject(runtime.sharedContentImportModel)
|
||||
.onAppear {
|
||||
appDelegate.runtime = runtime
|
||||
runtime.start()
|
||||
|
||||
@@ -72662,6 +72662,114 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"share_import.review.message" : {
|
||||
"comment" : "Explains that shared content replaces the named destination's composer and is not sent automatically",
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
"ar" : { "stringUnit" : { "state" : "needs_review", "value" : "هل تريد استبدال مسودة %@ بهذا المحتوى؟ لن يتم إرسال أي شيء تلقائيًا." } },
|
||||
"bn" : { "stringUnit" : { "state" : "needs_review", "value" : "%@-এর খসড়াটি এই কনটেন্ট দিয়ে বদলাবেন? কিছুই স্বয়ংক্রিয়ভাবে পাঠানো হবে না।" } },
|
||||
"de" : { "stringUnit" : { "state" : "needs_review", "value" : "Entwurf für %@ durch diesen Inhalt ersetzen? Es wird nichts automatisch gesendet." } },
|
||||
"en" : { "stringUnit" : { "state" : "translated", "value" : "Replace the draft for %@ with this content? Nothing will be sent automatically." } },
|
||||
"es" : { "stringUnit" : { "state" : "needs_review", "value" : "¿Reemplazar el borrador de %@ con este contenido? No se enviará nada automáticamente." } },
|
||||
"fa" : { "stringUnit" : { "state" : "needs_review", "value" : "پیشنویس %@ با این محتوا جایگزین شود؟ چیزی بهطور خودکار ارسال نمیشود." } },
|
||||
"fil" : { "stringUnit" : { "state" : "needs_review", "value" : "Palitan ng content na ito ang draft para sa %@? Walang awtomatikong ipapadala." } },
|
||||
"fr" : { "stringUnit" : { "state" : "needs_review", "value" : "Remplacer le brouillon pour %@ par ce contenu ? Rien ne sera envoyé automatiquement." } },
|
||||
"he" : { "stringUnit" : { "state" : "needs_review", "value" : "להחליף את הטיוטה עבור %@ בתוכן הזה? שום דבר לא יישלח אוטומטית." } },
|
||||
"hi" : { "stringUnit" : { "state" : "needs_review", "value" : "%@ का ड्राफ़्ट इस सामग्री से बदलें? कुछ भी अपने आप नहीं भेजा जाएगा।" } },
|
||||
"id" : { "stringUnit" : { "state" : "needs_review", "value" : "Ganti draf untuk %@ dengan konten ini? Tidak ada yang akan dikirim otomatis." } },
|
||||
"it" : { "stringUnit" : { "state" : "needs_review", "value" : "Sostituire la bozza per %@ con questo contenuto? Nulla verrà inviato automaticamente." } },
|
||||
"ja" : { "stringUnit" : { "state" : "needs_review", "value" : "%@ の下書きをこの内容で置き換えますか?自動的には送信されません。" } },
|
||||
"ko" : { "stringUnit" : { "state" : "needs_review", "value" : "%@의 초안을 이 콘텐츠로 바꾸시겠습니까? 자동으로 전송되지 않습니다." } },
|
||||
"ms" : { "stringUnit" : { "state" : "needs_review", "value" : "Gantikan draf untuk %@ dengan kandungan ini? Tiada apa-apa akan dihantar secara automatik." } },
|
||||
"ne" : { "stringUnit" : { "state" : "needs_review", "value" : "%@ को मस्यौदा यो सामग्रीले बदल्ने? केही पनि स्वचालित रूपमा पठाइने छैन।" } },
|
||||
"nl" : { "stringUnit" : { "state" : "needs_review", "value" : "Concept voor %@ vervangen door deze inhoud? Er wordt niets automatisch verzonden." } },
|
||||
"pl" : { "stringUnit" : { "state" : "needs_review", "value" : "Zastąpić szkic dla %@ tą treścią? Nic nie zostanie wysłane automatycznie." } },
|
||||
"pt" : { "stringUnit" : { "state" : "needs_review", "value" : "Substituir o rascunho para %@ por este conteúdo? Nada será enviado automaticamente." } },
|
||||
"pt-BR" : { "stringUnit" : { "state" : "needs_review", "value" : "Substituir o rascunho de %@ por este conteúdo? Nada será enviado automaticamente." } },
|
||||
"ru" : { "stringUnit" : { "state" : "needs_review", "value" : "Заменить черновик для %@ этим содержимым? Ничего не будет отправлено автоматически." } },
|
||||
"sv" : { "stringUnit" : { "state" : "needs_review", "value" : "Ersätta utkastet för %@ med detta innehåll? Inget skickas automatiskt." } },
|
||||
"ta" : { "stringUnit" : { "state" : "needs_review", "value" : "%@ க்கான வரைவைக் இந்த உள்ளடக்கத்தால் மாற்றவா? எதுவும் தானாக அனுப்பப்படாது." } },
|
||||
"th" : { "stringUnit" : { "state" : "needs_review", "value" : "แทนที่ฉบับร่างสำหรับ %@ ด้วยเนื้อหานี้หรือไม่? จะไม่มีการส่งอัตโนมัติ" } },
|
||||
"tr" : { "stringUnit" : { "state" : "needs_review", "value" : "%@ taslağı bu içerikle değiştirilsin mi? Hiçbir şey otomatik olarak gönderilmez." } },
|
||||
"uk" : { "stringUnit" : { "state" : "needs_review", "value" : "Замінити чернетку для %@ цим вмістом? Нічого не буде надіслано автоматично." } },
|
||||
"ur" : { "stringUnit" : { "state" : "needs_review", "value" : "%@ کا مسودہ اس مواد سے بدلیں؟ کچھ بھی خودکار طور پر نہیں بھیجا جائے گا۔" } },
|
||||
"vi" : { "stringUnit" : { "state" : "needs_review", "value" : "Thay bản nháp cho %@ bằng nội dung này? Không có gì được tự động gửi." } },
|
||||
"zh-Hans" : { "stringUnit" : { "state" : "needs_review", "value" : "要用此内容替换 %@ 的草稿吗?内容不会自动发送。" } },
|
||||
"zh-Hant" : { "stringUnit" : { "state" : "needs_review", "value" : "要用此內容取代 %@ 的草稿嗎?內容不會自動傳送。" } }
|
||||
}
|
||||
},
|
||||
"share_import.review.title" : {
|
||||
"comment" : "Title for reviewing content received from the share extension",
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
"ar" : { "stringUnit" : { "state" : "needs_review", "value" : "مراجعة المحتوى المشترك" } },
|
||||
"bn" : { "stringUnit" : { "state" : "needs_review", "value" : "শেয়ার করা কনটেন্ট পর্যালোচনা করুন" } },
|
||||
"de" : { "stringUnit" : { "state" : "needs_review", "value" : "Geteilte Inhalte prüfen" } },
|
||||
"en" : { "stringUnit" : { "state" : "translated", "value" : "Review shared content" } },
|
||||
"es" : { "stringUnit" : { "state" : "needs_review", "value" : "Revisar contenido compartido" } },
|
||||
"fa" : { "stringUnit" : { "state" : "needs_review", "value" : "بازبینی محتوای همرسانیشده" } },
|
||||
"fil" : { "stringUnit" : { "state" : "needs_review", "value" : "Suriin ang ibinahaging content" } },
|
||||
"fr" : { "stringUnit" : { "state" : "needs_review", "value" : "Vérifier le contenu partagé" } },
|
||||
"he" : { "stringUnit" : { "state" : "needs_review", "value" : "בדיקת תוכן משותף" } },
|
||||
"hi" : { "stringUnit" : { "state" : "needs_review", "value" : "शेयर की गई सामग्री की समीक्षा करें" } },
|
||||
"id" : { "stringUnit" : { "state" : "needs_review", "value" : "Tinjau konten yang dibagikan" } },
|
||||
"it" : { "stringUnit" : { "state" : "needs_review", "value" : "Controlla il contenuto condiviso" } },
|
||||
"ja" : { "stringUnit" : { "state" : "needs_review", "value" : "共有コンテンツを確認" } },
|
||||
"ko" : { "stringUnit" : { "state" : "needs_review", "value" : "공유 콘텐츠 검토" } },
|
||||
"ms" : { "stringUnit" : { "state" : "needs_review", "value" : "Semak kandungan yang dikongsi" } },
|
||||
"ne" : { "stringUnit" : { "state" : "needs_review", "value" : "साझा सामग्री समीक्षा गर्नुहोस्" } },
|
||||
"nl" : { "stringUnit" : { "state" : "needs_review", "value" : "Gedeelde inhoud bekijken" } },
|
||||
"pl" : { "stringUnit" : { "state" : "needs_review", "value" : "Sprawdź udostępnioną treść" } },
|
||||
"pt" : { "stringUnit" : { "state" : "needs_review", "value" : "Rever conteúdo partilhado" } },
|
||||
"pt-BR" : { "stringUnit" : { "state" : "needs_review", "value" : "Revisar conteúdo compartilhado" } },
|
||||
"ru" : { "stringUnit" : { "state" : "needs_review", "value" : "Проверить общий контент" } },
|
||||
"sv" : { "stringUnit" : { "state" : "needs_review", "value" : "Granska delat innehåll" } },
|
||||
"ta" : { "stringUnit" : { "state" : "needs_review", "value" : "பகிரப்பட்ட உள்ளடக்கத்தை மதிப்பாய்வு செய்" } },
|
||||
"th" : { "stringUnit" : { "state" : "needs_review", "value" : "ตรวจสอบเนื้อหาที่แชร์" } },
|
||||
"tr" : { "stringUnit" : { "state" : "needs_review", "value" : "Paylaşılan içeriği gözden geçir" } },
|
||||
"uk" : { "stringUnit" : { "state" : "needs_review", "value" : "Переглянути спільний вміст" } },
|
||||
"ur" : { "stringUnit" : { "state" : "needs_review", "value" : "شیئر کردہ مواد کا جائزہ لیں" } },
|
||||
"vi" : { "stringUnit" : { "state" : "needs_review", "value" : "Xem lại nội dung được chia sẻ" } },
|
||||
"zh-Hans" : { "stringUnit" : { "state" : "needs_review", "value" : "查看共享内容" } },
|
||||
"zh-Hant" : { "stringUnit" : { "state" : "needs_review", "value" : "查看分享內容" } }
|
||||
}
|
||||
},
|
||||
"share_import.review.use_in_composer" : {
|
||||
"comment" : "Action that places reviewed shared content in the composer without sending it",
|
||||
"extractionState" : "manual",
|
||||
"localizations" : {
|
||||
"ar" : { "stringUnit" : { "state" : "needs_review", "value" : "استخدام في مسودة الرسالة" } },
|
||||
"bn" : { "stringUnit" : { "state" : "needs_review", "value" : "বার্তার খসড়ায় ব্যবহার করুন" } },
|
||||
"de" : { "stringUnit" : { "state" : "needs_review", "value" : "Im Nachrichtenentwurf verwenden" } },
|
||||
"en" : { "stringUnit" : { "state" : "translated", "value" : "Use in composer" } },
|
||||
"es" : { "stringUnit" : { "state" : "needs_review", "value" : "Usar en el borrador" } },
|
||||
"fa" : { "stringUnit" : { "state" : "needs_review", "value" : "استفاده در پیشنویس پیام" } },
|
||||
"fil" : { "stringUnit" : { "state" : "needs_review", "value" : "Gamitin sa draft" } },
|
||||
"fr" : { "stringUnit" : { "state" : "needs_review", "value" : "Utiliser dans le brouillon" } },
|
||||
"he" : { "stringUnit" : { "state" : "needs_review", "value" : "שימוש בטיוטה" } },
|
||||
"hi" : { "stringUnit" : { "state" : "needs_review", "value" : "संदेश के ड्राफ़्ट में उपयोग करें" } },
|
||||
"id" : { "stringUnit" : { "state" : "needs_review", "value" : "Gunakan di draf" } },
|
||||
"it" : { "stringUnit" : { "state" : "needs_review", "value" : "Usa nella bozza" } },
|
||||
"ja" : { "stringUnit" : { "state" : "needs_review", "value" : "下書きで使用" } },
|
||||
"ko" : { "stringUnit" : { "state" : "needs_review", "value" : "초안에 사용" } },
|
||||
"ms" : { "stringUnit" : { "state" : "needs_review", "value" : "Gunakan dalam draf" } },
|
||||
"ne" : { "stringUnit" : { "state" : "needs_review", "value" : "सन्देशको मस्यौदामा प्रयोग गर्नुहोस्" } },
|
||||
"nl" : { "stringUnit" : { "state" : "needs_review", "value" : "In concept gebruiken" } },
|
||||
"pl" : { "stringUnit" : { "state" : "needs_review", "value" : "Użyj w szkicu" } },
|
||||
"pt" : { "stringUnit" : { "state" : "needs_review", "value" : "Usar no rascunho" } },
|
||||
"pt-BR" : { "stringUnit" : { "state" : "needs_review", "value" : "Usar no rascunho" } },
|
||||
"ru" : { "stringUnit" : { "state" : "needs_review", "value" : "Использовать в черновике" } },
|
||||
"sv" : { "stringUnit" : { "state" : "needs_review", "value" : "Använd i utkast" } },
|
||||
"ta" : { "stringUnit" : { "state" : "needs_review", "value" : "செய்தி வரைவில் பயன்படுத்து" } },
|
||||
"th" : { "stringUnit" : { "state" : "needs_review", "value" : "ใช้ในฉบับร่าง" } },
|
||||
"tr" : { "stringUnit" : { "state" : "needs_review", "value" : "Taslakta kullan" } },
|
||||
"uk" : { "stringUnit" : { "state" : "needs_review", "value" : "Використати в чернетці" } },
|
||||
"ur" : { "stringUnit" : { "state" : "needs_review", "value" : "پیغام کے مسودے میں استعمال کریں" } },
|
||||
"vi" : { "stringUnit" : { "state" : "needs_review", "value" : "Dùng trong bản nháp" } },
|
||||
"zh-Hans" : { "stringUnit" : { "state" : "needs_review", "value" : "用于草稿" } },
|
||||
"zh-Hant" : { "stringUnit" : { "state" : "needs_review", "value" : "用於草稿" } }
|
||||
}
|
||||
}
|
||||
},
|
||||
"version" : "1.1"
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
import Foundation
|
||||
|
||||
enum SharedContentKind: String, Codable, Sendable, Equatable {
|
||||
case text
|
||||
case url
|
||||
}
|
||||
|
||||
/// The single, bounded payload handed from the share extension to the app.
|
||||
///
|
||||
/// The app-group store intentionally contains at most one envelope. A newer
|
||||
/// share replaces an older one, which prevents unbounded shared-container
|
||||
/// growth while still surviving suspension and a later app launch.
|
||||
struct SharedContentPayload: Codable, Sendable, Equatable, Identifiable {
|
||||
static let currentVersion = 1
|
||||
static let maxContentBytes = 16_000
|
||||
static let maxTitleBytes = 512
|
||||
static let maxEnvelopeBytes = 24_000
|
||||
static let retentionSeconds: TimeInterval = 24 * 60 * 60
|
||||
static let allowedFutureSkewSeconds: TimeInterval = 5 * 60
|
||||
|
||||
let version: Int
|
||||
let id: UUID
|
||||
let kind: SharedContentKind
|
||||
let content: String
|
||||
let title: String?
|
||||
let createdAt: Date
|
||||
|
||||
init(
|
||||
version: Int = Self.currentVersion,
|
||||
id: UUID = UUID(),
|
||||
kind: SharedContentKind,
|
||||
content: String,
|
||||
title: String? = nil,
|
||||
createdAt: Date = Date()
|
||||
) {
|
||||
self.version = version
|
||||
self.id = id
|
||||
self.kind = kind
|
||||
self.content = content
|
||||
self.title = title
|
||||
self.createdAt = createdAt
|
||||
}
|
||||
|
||||
static func text(_ content: String, createdAt: Date = Date()) -> SharedContentPayload {
|
||||
SharedContentPayload(kind: .text, content: content, createdAt: createdAt)
|
||||
}
|
||||
|
||||
var composerText: String { content }
|
||||
|
||||
var preview: String {
|
||||
let normalized = content
|
||||
.replacingOccurrences(of: "\r\n", with: "\n")
|
||||
.replacingOccurrences(of: "\r", with: "\n")
|
||||
guard normalized.count > 240 else { return normalized }
|
||||
return String(normalized.prefix(240)) + "…"
|
||||
}
|
||||
|
||||
func validate(now: Date = Date()) throws {
|
||||
guard version == Self.currentVersion else {
|
||||
throw SharedContentHandoffError.unsupportedVersion
|
||||
}
|
||||
|
||||
let trimmed = content.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !trimmed.isEmpty else {
|
||||
throw SharedContentHandoffError.emptyContent
|
||||
}
|
||||
guard content.utf8.count <= Self.maxContentBytes else {
|
||||
throw SharedContentHandoffError.contentTooLarge
|
||||
}
|
||||
if let title {
|
||||
guard title.utf8.count <= Self.maxTitleBytes else {
|
||||
throw SharedContentHandoffError.titleTooLarge
|
||||
}
|
||||
guard !Self.containsDisallowedControl(in: title, allowsTextLayout: false) else {
|
||||
throw SharedContentHandoffError.invalidCharacters
|
||||
}
|
||||
}
|
||||
|
||||
let age = now.timeIntervalSince(createdAt)
|
||||
guard age >= -Self.allowedFutureSkewSeconds,
|
||||
age <= Self.retentionSeconds else {
|
||||
throw SharedContentHandoffError.expired
|
||||
}
|
||||
|
||||
switch kind {
|
||||
case .text:
|
||||
guard !Self.containsDisallowedControl(in: content, allowsTextLayout: true) else {
|
||||
throw SharedContentHandoffError.invalidCharacters
|
||||
}
|
||||
case .url:
|
||||
guard !Self.containsDisallowedControl(in: content, allowsTextLayout: false),
|
||||
let components = URLComponents(string: content),
|
||||
let scheme = components.scheme?.lowercased(),
|
||||
scheme == "http" || scheme == "https",
|
||||
components.host?.isEmpty == false else {
|
||||
throw SharedContentHandoffError.unsupportedURL
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static func containsDisallowedControl(
|
||||
in value: String,
|
||||
allowsTextLayout: Bool
|
||||
) -> Bool {
|
||||
value.unicodeScalars.contains { scalar in
|
||||
guard CharacterSet.controlCharacters.contains(scalar) else { return false }
|
||||
if allowsTextLayout, scalar == "\n" || scalar == "\r" || scalar == "\t" {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum SharedContentHandoffError: Error, Equatable {
|
||||
case unsupportedVersion
|
||||
case emptyContent
|
||||
case contentTooLarge
|
||||
case titleTooLarge
|
||||
case invalidCharacters
|
||||
case expired
|
||||
case unsupportedURL
|
||||
case envelopeTooLarge
|
||||
case encodingFailed
|
||||
}
|
||||
|
||||
/// Durable, single-item app-group storage used by both the extension and app.
|
||||
final class SharedContentStore {
|
||||
static let storageKey = "sharedContentEnvelopeV1"
|
||||
|
||||
private static let legacyKeys = [
|
||||
"sharedContent",
|
||||
"sharedContentType",
|
||||
"sharedContentDate"
|
||||
]
|
||||
|
||||
private let defaults: UserDefaults
|
||||
private let encoder: JSONEncoder
|
||||
private let decoder: JSONDecoder
|
||||
|
||||
init(defaults: UserDefaults) {
|
||||
self.defaults = defaults
|
||||
self.encoder = JSONEncoder()
|
||||
self.decoder = JSONDecoder()
|
||||
}
|
||||
|
||||
/// Replaces any older pending share with a validated, bounded envelope.
|
||||
func stage(_ payload: SharedContentPayload, now: Date = Date()) throws {
|
||||
try payload.validate(now: now)
|
||||
guard let encoded = try? encoder.encode(payload) else {
|
||||
throw SharedContentHandoffError.encodingFailed
|
||||
}
|
||||
guard encoded.count <= SharedContentPayload.maxEnvelopeBytes else {
|
||||
throw SharedContentHandoffError.envelopeTooLarge
|
||||
}
|
||||
|
||||
defaults.set(encoded, forKey: Self.storageKey)
|
||||
clearLegacyKeys()
|
||||
}
|
||||
|
||||
/// Reads the pending share without consuming it. Invalid and expired data
|
||||
/// is removed immediately so malformed app-group state cannot linger.
|
||||
func pending(now: Date = Date()) -> SharedContentPayload? {
|
||||
clearLegacyKeys()
|
||||
|
||||
guard let encoded = defaults.data(forKey: Self.storageKey) else { return nil }
|
||||
guard encoded.count <= SharedContentPayload.maxEnvelopeBytes,
|
||||
let payload = try? decoder.decode(SharedContentPayload.self, from: encoded) else {
|
||||
defaults.removeObject(forKey: Self.storageKey)
|
||||
return nil
|
||||
}
|
||||
|
||||
do {
|
||||
try payload.validate(now: now)
|
||||
return payload
|
||||
} catch {
|
||||
defaults.removeObject(forKey: Self.storageKey)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
/// Consumes only the envelope the user actually reviewed. If a newer share
|
||||
/// already replaced it, the newer content remains pending.
|
||||
func consume(id: UUID, now: Date = Date()) -> SharedContentPayload? {
|
||||
guard let payload = pending(now: now), payload.id == id else { return nil }
|
||||
defaults.removeObject(forKey: Self.storageKey)
|
||||
return payload
|
||||
}
|
||||
|
||||
/// Explicit cancellation has the same identity guard as consumption so it
|
||||
/// can never discard a newer share that arrived while a prompt was open.
|
||||
func discard(id: UUID) {
|
||||
guard let encoded = defaults.data(forKey: Self.storageKey),
|
||||
encoded.count <= SharedContentPayload.maxEnvelopeBytes,
|
||||
let payload = try? decoder.decode(SharedContentPayload.self, from: encoded),
|
||||
payload.id == id else {
|
||||
return
|
||||
}
|
||||
defaults.removeObject(forKey: Self.storageKey)
|
||||
}
|
||||
|
||||
func discardAll() {
|
||||
defaults.removeObject(forKey: Self.storageKey)
|
||||
clearLegacyKeys()
|
||||
}
|
||||
|
||||
private func clearLegacyKeys() {
|
||||
for key in Self.legacyKeys {
|
||||
defaults.removeObject(forKey: key)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -345,7 +345,6 @@ enum TransportConfig {
|
||||
|
||||
// Share extension
|
||||
static let uiShareExtensionDismissDelaySeconds: TimeInterval = 2.0
|
||||
static let uiShareAcceptWindowSeconds: TimeInterval = 30.0
|
||||
static let uiMigrationCutoffSeconds: TimeInterval = 24 * 60 * 60
|
||||
|
||||
// Gossip Sync Configuration
|
||||
|
||||
@@ -1331,6 +1331,15 @@ final class ChatViewModel: ObservableObject, BitchatDelegate, TransportEventDele
|
||||
// posts are signed with our identity key and persist for days.
|
||||
BoardStore.shared.wipe()
|
||||
|
||||
// Drop any share-extension handoff staged in the app group. The normal
|
||||
// panic path clears this through AppChromeModel.onPanicWipe, but the
|
||||
// crash-recovery replay calls this method directly and would otherwise
|
||||
// let a staged envelope survive the wipe. Clearing here is idempotent
|
||||
// (it only removes the app-group key), so the double-clear is harmless.
|
||||
if let sharedDefaults = UserDefaults(suiteName: BitchatApp.groupID) {
|
||||
SharedContentStore(defaults: sharedDefaults).discardAll()
|
||||
}
|
||||
|
||||
// Identity manager has cleared persisted identity data above
|
||||
|
||||
// Clear autocomplete state
|
||||
|
||||
@@ -36,6 +36,7 @@ struct ContentView: View {
|
||||
@EnvironmentObject private var verificationModel: VerificationModel
|
||||
@EnvironmentObject private var conversationUIModel: ConversationUIModel
|
||||
@EnvironmentObject private var locationChannelsModel: LocationChannelsModel
|
||||
@EnvironmentObject private var sharedContentImportModel: SharedContentImportModel
|
||||
|
||||
@StateObject private var voiceRecordingVM = VoiceRecordingViewModel()
|
||||
@State private var messageText = ""
|
||||
@@ -69,6 +70,14 @@ struct ContentView: View {
|
||||
privateConversationModel.selectedPeerID
|
||||
}
|
||||
|
||||
private var sharedContentDestination: SharedContentDestination {
|
||||
SharedContentDestination.resolve(
|
||||
selectedPrivatePeerID: selectedPrivatePeerID,
|
||||
privateDisplayName: privateConversationModel.selectedHeaderState?.displayName,
|
||||
activeChannel: locationChannelsModel.selectedChannel
|
||||
)
|
||||
}
|
||||
|
||||
private var usesGlassLayout: Bool { appTheme.usesGlassChrome }
|
||||
|
||||
var body: some View {
|
||||
@@ -88,6 +97,7 @@ struct ContentView: View {
|
||||
isTextFieldFocused = true
|
||||
}
|
||||
#endif
|
||||
sharedContentImportModel.updateDestination(sharedContentDestination)
|
||||
}
|
||||
.onChange(of: colorScheme) { newValue in
|
||||
conversationUIModel.setCurrentColorScheme(newValue)
|
||||
@@ -104,6 +114,10 @@ struct ContentView: View {
|
||||
if newValue != nil {
|
||||
showSidebar = true
|
||||
}
|
||||
sharedContentImportModel.updateDestination(sharedContentDestination)
|
||||
}
|
||||
.onChange(of: locationChannelsModel.selectedChannel) { _ in
|
||||
sharedContentImportModel.updateDestination(sharedContentDestination)
|
||||
}
|
||||
.sheet(
|
||||
isPresented: Binding(
|
||||
@@ -230,6 +244,34 @@ struct ContentView: View {
|
||||
} message: {
|
||||
Text(appChromeModel.bluetoothAlertMessage)
|
||||
}
|
||||
.alert(
|
||||
String(localized: "share_import.review.title", comment: "Title for reviewing content received from the share extension"),
|
||||
isPresented: Binding(
|
||||
get: { sharedContentImportModel.offer != nil },
|
||||
set: { _ in }
|
||||
),
|
||||
presenting: sharedContentImportModel.offer
|
||||
) { _ in
|
||||
Button("common.cancel", role: .cancel) {
|
||||
sharedContentImportModel.cancel(destination: sharedContentDestination)
|
||||
}
|
||||
Button("share_import.review.use_in_composer") {
|
||||
guard let importedText = sharedContentImportModel.confirm(
|
||||
destination: sharedContentDestination
|
||||
) else { return }
|
||||
// Replacing is deliberate and called out in the prompt. It
|
||||
// avoids combining a stale draft from another conversation
|
||||
// with newly shared content.
|
||||
messageText = importedText
|
||||
isTextFieldFocused = true
|
||||
}
|
||||
} message: { offer in
|
||||
let format = String(
|
||||
localized: "share_import.review.message",
|
||||
comment: "Explains that shared content will replace the named destination's composer and will not be sent automatically"
|
||||
)
|
||||
Text(String(format: format, offer.destination.displayName) + "\n\n" + offer.payload.preview)
|
||||
}
|
||||
.onDisappear {
|
||||
autocompleteDebounceTimer?.invalidate()
|
||||
appChromeModel.setPanicPreparation(nil)
|
||||
|
||||
Reference in New Issue
Block a user