From 0d91633f76f0b1080fdedb1a9ccb9327ea0f2989 Mon Sep 17 00:00:00 2001 From: Koray Koska Date: Sun, 16 Jul 2017 13:01:57 +0200 Subject: [PATCH] Add title and url to attachments --- .../Webhooks/FacebookAttachment.swift | 14 ++++++++++++++ .../Webhooks/FacebookAttachmentPayload.swift | 5 ++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Sources/VaporFacebookBot/Webhooks/FacebookAttachment.swift b/Sources/VaporFacebookBot/Webhooks/FacebookAttachment.swift index 9e3e527..578608e 100644 --- a/Sources/VaporFacebookBot/Webhooks/FacebookAttachment.swift +++ b/Sources/VaporFacebookBot/Webhooks/FacebookAttachment.swift @@ -14,18 +14,32 @@ public final class FacebookAttachment: JSONConvertible { public var type: FacebookAttachmentType public var payload: FacebookAttachmentPayload + public var title: String? + public var url: String? + public init(json: JSON) throws { guard let type = FacebookAttachmentType(rawValue: try json.get("type")) else { throw Abort(.badRequest, metadata: "type is not a valid FacebookAttachmentType in FacebookAttachement") } self.type = type self.payload = try FacebookAttachmentPayload(json: json.get("payload")) + + self.title = json["title"]?.string + self.url = json["url"]?.string } public func makeJSON() throws -> JSON { var json = JSON() try json.set("type", type.rawValue) try json.set("payload", try payload.makeJSON()) + + if let title = title { + try json.set("title", title) + } + if let url = url { + try json.set("url", url) + } + return json } } diff --git a/Sources/VaporFacebookBot/Webhooks/FacebookAttachmentPayload.swift b/Sources/VaporFacebookBot/Webhooks/FacebookAttachmentPayload.swift index ce0467d..a8bcb7d 100644 --- a/Sources/VaporFacebookBot/Webhooks/FacebookAttachmentPayload.swift +++ b/Sources/VaporFacebookBot/Webhooks/FacebookAttachmentPayload.swift @@ -19,10 +19,9 @@ public final class FacebookAttachmentPayload: JSONConvertible { self.url = url } - public init(coordinatesLat: Double, coordinatesLong: Double, url: String? = nil) { + public init(coordinatesLat: Double, coordinatesLong: Double) { self.coordinatesLat = coordinatesLat self.coordinatesLong = coordinatesLong - self.url = url } public convenience init(json: JSON) throws { @@ -31,7 +30,7 @@ public final class FacebookAttachmentPayload: JSONConvertible { let coordinatesLong = json["coordinates"]?["long"]?.double if let coordinatesLat = coordinatesLat, let coordinatesLong = coordinatesLong { - self.init(coordinatesLat: coordinatesLat, coordinatesLong: coordinatesLong, url: url) + self.init(coordinatesLat: coordinatesLat, coordinatesLong: coordinatesLong) } else if let url = url { self.init(url: url) } else {