|
| 1 | +import Cocoa |
| 2 | +import TGUIKit |
| 3 | +import TelegramCore |
| 4 | +import Postbox |
| 5 | +import SwiftSignalKit |
| 6 | +import TelegramTheme |
| 7 | +import TelegramContext |
| 8 | + |
| 9 | +private var spoilerEvidence: SpoilerEvidence? |
| 10 | +private final class SpoilerEvidenceButton: NSButton { |
| 11 | + var invoke: (() -> Void)? |
| 12 | + @objc func run() { invoke?() } |
| 13 | +} |
| 14 | +private final class SpoilerEvidence { |
| 15 | + let context: any AccountContextProtocol |
| 16 | + var closeObserver: NSObjectProtocol? |
| 17 | + let window: Window |
| 18 | + let status: NSTextField |
| 19 | + var tiles: [(ChatInteractiveContentView, TelegramMediaImage, Message)] = [] |
| 20 | + var revealed = false |
| 21 | + init(source: ChatInteraction) { |
| 22 | + context = source.interactionContext |
| 23 | + window = Window(contentRect: NSMakeRect(0, 0, 820, 420), styleMask: [.titled, .closable], backing: .buffered, defer: false) |
| 24 | + window.isReleasedWhenClosed = false |
| 25 | + window.title = "Media spoiler — isolated preview components" |
| 26 | + let host = View(frame: NSMakeRect(0, 0, 820, 420)) |
| 27 | + host.backgroundColor = theme.colors.chatBackground |
| 28 | + window.contentView = host |
| 29 | + status = NSTextField(labelWithString: "Hidden local checkerboard photos · no chat contents") |
| 30 | + status.frame = NSMakeRect(20, 15, 780, 25) |
| 31 | + status.textColor = theme.colors.text |
| 32 | + closeObserver = NotificationCenter.default.addObserver(forName: NSWindow.willCloseNotification, object: window, queue: .main) { [weak self] notification in |
| 33 | + self?.windowWillClose(notification) |
| 34 | + } |
| 35 | + host.addSubview(status) |
| 36 | + let button = SpoilerEvidenceButton(frame: NSMakeRect(20, 48, 250, 30)) |
| 37 | + button.title = "Reveal test previews" |
| 38 | + button.bezelStyle = .rounded |
| 39 | + button.target = button |
| 40 | + button.action = #selector(SpoilerEvidenceButton.run) |
| 41 | + button.invoke = { [weak self] in self?.reveal() } |
| 42 | + host.addSubview(button) |
| 43 | + for i in 0..<3 { |
| 44 | + let resource = LocalFileMediaResource(fileId: Int64.random(in: 1...Int64.max)) |
| 45 | + let thumb = LocalFileMediaResource(fileId: Int64.random(in: 1...Int64.max)) |
| 46 | + context.account.postbox.mediaBox.storeResourceData(resource.id, data: Self.checker(size: 256)) |
| 47 | + context.account.postbox.mediaBox.storeResourceData(thumb.id, data: Self.checker(size: 32)) |
| 48 | + let image = TelegramMediaImage(imageId: MediaId(namespace: 0, id: resource.fileId), representations: [TelegramMediaImageRepresentation(dimensions: PixelDimensions(width: 32, height: 32), resource: thumb, progressiveSizes: [], immediateThumbnailData: nil, hasVideo: false, isPersonal: false), TelegramMediaImageRepresentation(dimensions: PixelDimensions(width: 256, height: 256), resource: resource, progressiveSizes: [], immediateThumbnailData: nil, hasVideo: false, isPersonal: false)], immediateThumbnailData: nil, reference: nil, partialReference: nil, flags: []) |
| 49 | + let peer = source.peer! |
| 50 | + let message = Message(stableId: UInt32(10+i), stableVersion: 0, id: MessageId(peerId: peer.id, namespace: Namespaces.Message.Cloud, id: Int32(10+i)), globallyUniqueId: nil, groupingKey: 1, groupInfo: nil, threadId: nil, timestamp: 1782388800, flags: [.Incoming], tags: [], globalTags: [], localTags: [], customTags: [], forwardInfo: nil, author: peer, text: "", attributes: [], media: [image], peers: SimpleDictionary([peer.id: peer]), associatedMessages: .init(), associatedMessageIds: [], associatedMedia: [:], associatedThreadInfo: nil, associatedStories: [:]) |
| 51 | + let view = ChatInteractiveContentView(frame: NSMakeRect(20+CGFloat(i)*265, 110, 250, 250)) |
| 52 | + host.addSubview(view) |
| 53 | + tiles.append((view, image, message)) |
| 54 | + update(view, image, message, revealed: false) |
| 55 | + } |
| 56 | + window.center() |
| 57 | + window.makeKeyAndOrderFront(nil) |
| 58 | + } |
| 59 | + deinit { |
| 60 | + if let closeObserver { NotificationCenter.default.removeObserver(closeObserver) } |
| 61 | + } |
| 62 | + func windowWillClose(_ notification: Notification) { |
| 63 | + if spoilerEvidence === self { |
| 64 | + spoilerEvidence = nil |
| 65 | + } |
| 66 | + } |
| 67 | + static func checker(size: Int) -> Data { |
| 68 | + let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: size, pixelsHigh: size, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: size*4, bitsPerPixel: 32)! |
| 69 | + let square = max(1, size/16) |
| 70 | + let bytes = rep.bitmapData! |
| 71 | + for y in 0..<size { |
| 72 | + for x in 0..<size { |
| 73 | + let value: UInt8 = (x/square+y/square)%2 == 0 ? 0 : 255 |
| 74 | + let offset = (y*size+x)*4 |
| 75 | + bytes[offset] = value |
| 76 | + bytes[offset+1] = value |
| 77 | + bytes[offset+2] = value |
| 78 | + bytes[offset+3] = 255 |
| 79 | + } |
| 80 | + } |
| 81 | + return rep.representation(using: .png, properties: [:])! |
| 82 | + } |
| 83 | + func update(_ view: ChatInteractiveContentView, _ image: TelegramMediaImage, _ message: Message, revealed: Bool) { |
| 84 | + let parameters = ChatMediaLayoutParameters(presentation: .Empty, media: image, automaticDownload: false, isRevealed: revealed) |
| 85 | + parameters.forceSpoiler = true |
| 86 | + view.update(with: image, size: NSMakeSize(250, 250), context: context, parent: message, table: nil, parameters: parameters, animated: false, approximateSynchronousValue: true) |
| 87 | + } |
| 88 | + func reveal() { |
| 89 | + guard !revealed else { return } |
| 90 | + revealed = true |
| 91 | + for (view, image, message) in tiles { update(view, image, message, revealed: true) } |
| 92 | + DispatchQueue.main.asyncAfter(deadline: .now()+1) { [weak self] in self?.report() } |
| 93 | + } |
| 94 | + func report() { |
| 95 | + var checks: [[String: Any]] = [] |
| 96 | + for (i, tile) in tiles.enumerated() { |
| 97 | + let (image, hidden) = tile.0.codexSpoilerProbe() |
| 98 | + var values: [CGFloat] = [] |
| 99 | + if let image { |
| 100 | + let rep = NSBitmapImageRep(cgImage: image) |
| 101 | + for y in 0..<16 { |
| 102 | + for x in 0..<16 { |
| 103 | + if let color = rep.colorAt(x: (2*x+1)*rep.pixelsWide/32, y: (2*y+1)*rep.pixelsHigh/32)?.usingColorSpace(.deviceRGB) { values.append(color.redComponent) } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + let mean = values.reduce(0,+)/CGFloat(max(1,values.count)) |
| 108 | + let variance = values.map { ($0-mean)*($0-mean) }.reduce(0,+)/CGFloat(max(1,values.count)) |
| 109 | + checks.append(["name": "tile \(i+1) revealed without ink", "pass": !hidden]) |
| 110 | + checks.append(["name": "tile \(i+1) sharp after reveal", "pass": variance > 0.1, "pixelVariance": variance]) |
| 111 | + } |
| 112 | + let passed = checks.filter { $0["pass"] as? Bool == true }.count |
| 113 | + status.stringValue = "\(passed)/\(checks.count) checks passed · Revealed preview components" |
| 114 | + let file = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("spoiler-media.json") |
| 115 | + if let data = try? JSONSerialization.data(withJSONObject: checks, options: [.prettyPrinted, .sortedKeys]) { try? data.write(to: file, options: .atomic) } |
| 116 | + } |
| 117 | +} |
| 118 | +func codexInstallSpoilerEvidence(source: ChatInteraction, parent: NSView) { |
| 119 | + guard source.mode == .history else { return } |
| 120 | + let button = SpoilerEvidenceButton(frame: NSMakeRect(20, 130, 260, 30)) |
| 121 | + button.title = "Media spoiler validation" |
| 122 | + button.bezelStyle = .rounded |
| 123 | + button.target = button |
| 124 | + button.action = #selector(SpoilerEvidenceButton.run) |
| 125 | + button.invoke = { [weak source] in |
| 126 | + guard let source else { return } |
| 127 | + spoilerEvidence?.window.close() |
| 128 | + spoilerEvidence = SpoilerEvidence(source: source) |
| 129 | + } |
| 130 | + parent.addSubview(button) |
| 131 | +} |
0 commit comments