namespace ActivityPub; ///// ///// A class representing a link (either as a Uri or Link object) ///// //public class LinkOrUri : OneOfBase { // protected LinkOrUri(OneOf _) : base(_) { } // public static implicit operator LinkOrUri(Uri _) => new LinkOrUri(_); // public static implicit operator LinkOrUri(Link _) => new LinkOrUri(_); // public static implicit operator LinkOrUri(string _) => new LinkOrUri(new Uri(_)); // public static explicit operator Uri?(LinkOrUri _) => _.Match(uri => uri, link => link.Href); // public static explicit operator Link(LinkOrUri _) => _.Match( // uri => new Link { Href = uri }, // link => link // ); // public static explicit operator string?(LinkOrUri _) => ((Uri?)_)?.ToString(); // public T? FetchObject() where T : Object, new() { // Uri? uri = (Uri?)this; // if (uri == null) return default(T); // // TODO: Fetch the object from the uri instead of the below: // return this.Match( // uri => new T { Id = uri }, // TODO: Fetch the object // link => new T { Id = link.Href, Name = link.Name } // TODO: Fetch the object // ); // } //} ///// ///// A class representing either an object or link ///// //public class GenericObjectOrLink : OneOfBase where T : Object, new() { // protected GenericObjectOrLink(OneOf _) : base(_) { } // public static implicit operator GenericObjectOrLink(Uri _) => new GenericObjectOrLink(_); // public static implicit operator GenericObjectOrLink(Link _) => new GenericObjectOrLink(_); // public static implicit operator GenericObjectOrLink(T _) => new GenericObjectOrLink(_); // public static implicit operator GenericObjectOrLink(string _) => new GenericObjectOrLink(new Uri(_)); // public static explicit operator Uri(GenericObjectOrLink _) => _.Match(uri => uri, link => link.Href, obj => obj.Id); // public static explicit operator Link(GenericObjectOrLink _) => _.Match( // uri => new Link { Href = uri }, // link => link, // obj => new Link { Href = obj.Id, Name = obj.Name } // ); // public static explicit operator T?(GenericObjectOrLink _) => _.FetchObject(); // public static explicit operator string?(GenericObjectOrLink _) => ((Uri?)_)?.ToString(); // public T? FetchObject(bool force = false) { // if (this.IsT2 && !force) return this.AsT2; // Uri? uri = (Uri?)this; // if (uri == null) return null; // // TODO: Fetch the object from the uri instead of the below: // return this.Match( // uri => new T { Id = uri }, // TODO: Fetch the object // link => new T { Id = link.Href, Name = link.Name }, // TODO: Fetch the object // obj => obj // ); // } //} //public class ObjectOrLink : GenericObjectOrLink { // protected ObjectOrLink(OneOf _) : base(_) { } // public bool IsUri => this.IsT0; // public bool IsLink => this.IsT1; // public bool IsObject => this.IsT2; //} ///// ///// A class representing either a collection or link ///// //public class CollectionOrLink : GenericObjectOrLink { // protected CollectionOrLink(OneOf _) : base(_) { } //} ///// ///// A class representing either a collection page or link ///// //public class CollectionPageOrLink : GenericObjectOrLink { // protected CollectionPageOrLink(OneOf _) : base(_) { } //} ///// ///// A class representing a list of ObjectOrLinks ///// //public class ListOrLink : List> where T : Object, new() { // protected ListOrLink(List> _) : base(_) { } // public static implicit operator ListOrLink(Uri _) => new ListOrLink(new List> { _ }); // public static implicit operator ListOrLink(Link _) => new ListOrLink(new List> { _ }); // public static implicit operator ListOrLink(T _) => new ListOrLink(new List> { _ }); // public static implicit operator ListOrLink(string _) => new ListOrLink(new List> { new Uri(_) }); // public static explicit operator List(ListOrLink _) => _.Cast().ToList(); //} /// /// Static functions for working with ActivityPub/ActivityStreams types /// public static class Types { /// /// Normalizes the type string to title case (all types in ActivityStreams start with a capital letter) /// /// The type string to normalize /// The normalized type string (capital first letter) public static string Normalize(string type) { return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(type.ToLower()); } /// /// https://www.w3.org/TR/activitystreams-vocabulary/#activity-types /// public static readonly string[] Activity = [ "Accept", "Add", "Announce", "Arrive", "Block", "Create", "Delete", "Dislike", "Flag", "Follow", "Ignore", "Invite", "Join", "Leave", "Like", "Listen", "Move", "Offer", "Question", "Reject", "Read", "Remove", "TentativeReject", "TentativeAccept", "Travel", "Undo", "Update", "View" ]; /// /// https://www.w3.org/TR/activitystreams-vocabulary/#actor-types /// public static readonly string[] Actor = [ "Application", "Group", "Organization", "Person", "Service" ]; /// /// https://www.w3.org/TR/activitystreams-vocabulary/#object-types /// public static readonly string[] Object = [ "Article", "Audio", "Document", "Event", "Image", "Note", "Page", "Place", "Profile", "Relationship", "Tombstone", "Video" ]; /// /// https://www.w3.org/TR/activitystreams-vocabulary/#object-types /// public static readonly string[] Link = [ "Mention" ]; public static bool IsActivity(string type) => Activity.Contains(type); //public static bool IsActivity(this Object obj) => obj.Type == null ? false : IsActivity(obj.Type); public static bool IsActor(string type) => Actor.Contains(type); //public static bool IsActor(this Object obj) => obj.Type == null ? false : IsActor(obj.Type); public static bool IsObject(string type) => Object.Contains(type); //public static bool IsObject(this Object obj) => obj.Type == null ? false : IsObject(obj.Type); public static bool IsLink(string type) => Link.Contains(type); //public static bool IsLink(this Object obj) => obj.Type == null ? false : IsLink(obj.Type); public static bool IsObjectOrLink(string type) => Object.Concat(Link).Contains(type); //public static bool IsObjectOrLink(this Object obj) => obj.Type == null ? false : IsObjectOrLink(obj.Type); }