Skip to content

Commit

Permalink
Added "Attachments" property to the "FacebookPost" class
Browse files Browse the repository at this point in the history
  • Loading branch information
abjerner committed Apr 29, 2018
1 parent 258146f commit 07dc3dd
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 1 deletion.
@@ -0,0 +1,55 @@
using Newtonsoft.Json.Linq;
using Skybrud.Essentials.Json.Extensions;

namespace Skybrud.Social.Facebook.Models.Attachments {

/// <summary>
/// Class representing a Facebook attachment.
/// </summary>
/// <see>
/// <cref>https://developers.facebook.com/docs/graph-api/reference/v2.12/attachment</cref>
/// </see>
public class FacebookAttachment : FacebookAttachmentBase {

#region Properties

/// <summary>
/// Gets an array of all sub attachments within the attachment. Not all attachments have sub attachments.
/// </summary>
public FacebookAttachmentBase[] SubAttachments { get; }

/// <summary>
/// Gets whether the attachment has any sub attachments.
/// </summary>
public bool HasSubAttachments => SubAttachments.Length > 0;

#endregion

#region Constructors

/// <summary>
/// Initializes a new instance based on the specified <paramref name="obj"/>.
/// </summary>
/// <param name="obj">The instance of <see cref="JObject"/> representing the event.</param>
private FacebookAttachment(JObject obj) : base(obj) {
SubAttachments = obj.GetArrayItems("subattachments", FacebookAttachmentBase.Parse);
}

#endregion

#region Static methods

/// <summary>
/// Parses the specified <paramref name="obj"/> into an instance of <see cref="FacebookAttachment"/>.
/// </summary>
/// <param name="obj">The instance of <see cref="JObject"/> to be parsed.</param>
/// <returns>An instance of <see cref="FacebookAttachment"/>.</returns>
public new static FacebookAttachment Parse(JObject obj) {
return obj == null ? null : new FacebookAttachment(obj);
}

#endregion

}

}
@@ -0,0 +1,97 @@
using System;
using Newtonsoft.Json.Linq;
using Skybrud.Essentials.Json.Extensions;
using Skybrud.Social.Facebook.Models.Common.Tags;

namespace Skybrud.Social.Facebook.Models.Attachments {

/// <summary>
/// Class representing a Facebook attachment.
/// </summary>
/// <see>
/// <cref>https://developers.facebook.com/docs/graph-api/reference/v2.12/attachment</cref>
/// </see>
public class FacebookAttachmentBase : FacebookObject {

#region Properties

/// <summary>
/// Gets the text accompanying the attachment.
/// </summary>
public string Description { get; }

/// <summary>
/// Gets whether the <see cref="Description"/> property was included in the response.
/// </summary>
public bool HasDescription => !String.IsNullOrWhiteSpace(Description);

/// <summary>
/// Gets an array of the profiles tagged in the <see cref="Description"/> property.
/// </summary>
public FacebookProfileTag[] DescriptionTags { get; }

/// <summary>
/// Gets whether the <see cref="DescriptionTags"/> property was included in the response.
/// </summary>
public bool HasDescriptionTagsTags => DescriptionTags.Length > 0;

/// <summary>
/// Gets a reference to the media data of the attachment.
/// </summary>
public FacebookAttachmentMedia Media { get; }

/// <summary>
/// Gets whether the <see cref="Media"/> property was included in the response.
/// </summary>
public bool HasMedia => Media != null;

/// <summary>
/// Gets the title of the attachment.
/// </summary>
public string Title { get; }

/// <summary>
/// Gets the type of the attachment.
/// </summary>
public string Type { get; }

/// <summary>
/// Gets the URL of the attachment.
/// </summary>
public string Url { get; }

#endregion

#region Constructors

/// <summary>
/// Initializes a new instance based on the specified <paramref name="obj"/>.
/// </summary>
/// <param name="obj">The instance of <see cref="JObject"/> representing the event.</param>
protected FacebookAttachmentBase(JObject obj) : base(obj) {
Description = obj.GetString("id");
DescriptionTags = obj.GetArrayItems("description_tags", FacebookProfileTag.Parse);
Media = obj.GetObject("media", FacebookAttachmentMedia.Parse);
Title = obj.GetString("title");
Type = obj.GetString("type");
Url = obj.GetString("url");
}

#endregion

#region Static methods

/// <summary>
/// Parses the specified <paramref name="obj"/> into an instance of <see cref="FacebookAttachmentBase"/>.
/// </summary>
/// <param name="obj">The instance of <see cref="JObject"/> to be parsed.</param>
/// <returns>An instance of <see cref="FacebookAttachmentBase"/>.</returns>
public static FacebookAttachmentBase Parse(JObject obj) {
return obj == null ? null : new FacebookAttachmentBase(obj);
}

#endregion

}

}
@@ -0,0 +1,55 @@
using Newtonsoft.Json.Linq;
using Skybrud.Essentials.Json.Extensions;

namespace Skybrud.Social.Facebook.Models.Attachments {

/// <summary>
/// Class representing an image of a Facebook attachment.
/// </summary>
public class FacebookAttachmentImage : FacebookObject {

#region Properties

/// <summary>
/// Gets the height of the image.
/// </summary>
public int Height { get; }

/// <summary>
/// Gets the source URL of the image.
/// </summary>
public string Src { get; }

/// <summary>
/// Gets the width of the image.
/// </summary>
public int Width { get; }

#endregion

#region Constructors

private FacebookAttachmentImage(JObject obj) : base(obj) {
Height = obj.GetInt32("height");
Src = obj.GetString("src");
Width = obj.GetInt32("width");
}

#endregion

#region Static methods

/// <summary>
/// Parses the specified <paramref name="obj"/> into an instance of <see cref="FacebookAttachmentImage"/>.
/// </summary>
/// <param name="obj">The instance of <see cref="JObject"/> to be parsed.</param>
/// <returns>An instance of <see cref="FacebookAttachmentImage"/>.</returns>
public static FacebookAttachmentImage Parse(JObject obj) {
return obj == null ? null : new FacebookAttachmentImage(obj);
}

#endregion

}

}
@@ -0,0 +1,43 @@
using Newtonsoft.Json.Linq;
using Skybrud.Essentials.Json.Extensions;

namespace Skybrud.Social.Facebook.Models.Attachments {

/// <summary>
/// Class with media data about a Facebook attachment.
/// </summary>
public class FacebookAttachmentMedia : FacebookObject {

#region Properties

/// <summary>
/// Gets a reference to the image of the attachment.
/// </summary>
public FacebookAttachmentImage Image { get; }

#endregion

#region Constructors

private FacebookAttachmentMedia(JObject obj) : base(obj) {
Image = obj.GetObject("image", FacebookAttachmentImage.Parse);
}

#endregion

#region Static methods

/// <summary>
/// Parses the specified <paramref name="obj"/> into an instance of <see cref="FacebookAttachmentMedia"/>.
/// </summary>
/// <param name="obj">The instance of <see cref="JObject"/> to be parsed.</param>
/// <returns>An instance of <see cref="FacebookAttachmentMedia"/>.</returns>
public static FacebookAttachmentMedia Parse(JObject obj) {
return obj == null ? null : new FacebookAttachmentMedia(obj);
}

#endregion

}

}
@@ -0,0 +1,55 @@
using System;
using Newtonsoft.Json.Linq;
using Skybrud.Essentials.Json.Extensions;

namespace Skybrud.Social.Facebook.Models.Attachments {

/// <summary>
/// Class representing the target object of a Facebook attachment.
/// </summary>
public class FacebookAttachmentTarget : FacebookObject {

#region Properties

/// <summary>
/// Gets the ID of the object in the attachment. An attachment may refer to an external URL, in which case an ID isn't available.
/// </summary>
public string Id { get; }

/// <summary>
/// Gets whether the <see cref="Id"/> property was included in the response.
/// </summary>
public bool HasId => !String.IsNullOrWhiteSpace(Id);

/// <summary>
/// Gets the URL to the attachment.
/// </summary>
public string Url { get; }

#endregion

#region Constructors

private FacebookAttachmentTarget(JObject obj) : base(obj) {
Id = obj.GetString("id");
Url = obj.GetString("url");
}

#endregion

#region Static methods

/// <summary>
/// Parses the specified <paramref name="obj"/> into an instance of <see cref="FacebookAttachmentTarget"/>.
/// </summary>
/// <param name="obj">The instance of <see cref="JObject"/> to be parsed.</param>
/// <returns>An instance of <see cref="FacebookAttachmentTarget"/>.</returns>
public static FacebookAttachmentTarget Parse(JObject obj) {
return obj == null ? null : new FacebookAttachmentTarget(obj);
}

#endregion

}

}
14 changes: 13 additions & 1 deletion src/Skybrud.Social.Facebook/Models/Posts/FacebookPost.cs
Expand Up @@ -51,7 +51,17 @@ public class FacebookPost : FacebookObject, ISocialTimelineEntry {
public bool HasApplication {
get { return Application != null; }
}


/// <summary>
/// Gets a collection of the attachments of the post. Not all posts have attachments.
/// </summary>
public FacebookPostAttachments Attachments { get; }

/// <summary>
/// Gets whether the response for the post included any attachments.
/// </summary>
public bool HasAttachments => Attachments != null && Attachments.Count > 0;

// TODO: Add support for the "call_to_action" property

/// <summary>
Expand Down Expand Up @@ -418,7 +428,9 @@ public class FacebookPost : FacebookObject, ISocialTimelineEntry {
Id = obj.GetString("id");
AdminCreator = obj.GetObject("admin_creator", FacebookEntity.Parse);
Application = obj.GetObject("application", FacebookApplication.Parse);
Attachments = obj.GetObject("attachments", FacebookPostAttachments.Parse);
// TODO: Add support for the "call_to_action" property
// TODO: Add support for the "can_reply_privately" property
Caption = obj.GetString("caption");
CreatedTime = obj.GetString("created_time", EssentialsDateTime.Parse);
Description = obj.GetString("description");
Expand Down

0 comments on commit 07dc3dd

Please sign in to comment.