AP.NET/Types.cs

228 lines
8 KiB
C#

namespace ActivityPub;
///// <summary>
///// A class representing a link (either as a Uri or Link object)
///// </summary>
//public class LinkOrUri : OneOfBase<Uri, Link> {
// protected LinkOrUri(OneOf<Uri, Link> _) : 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<T>() 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
// );
// }
//}
///// <summary>
///// A class representing either an object or link
///// </summary>
//public class GenericObjectOrLink<T> : OneOfBase<Uri, Link, T> where T : Object, new() {
// protected GenericObjectOrLink(OneOf<Uri, Link, T> _) : base(_) { }
// public static implicit operator GenericObjectOrLink<T>(Uri _) => new GenericObjectOrLink<T>(_);
// public static implicit operator GenericObjectOrLink<T>(Link _) => new GenericObjectOrLink<T>(_);
// public static implicit operator GenericObjectOrLink<T>(T _) => new GenericObjectOrLink<T>(_);
// public static implicit operator GenericObjectOrLink<T>(string _) => new GenericObjectOrLink<T>(new Uri(_));
// public static explicit operator Uri(GenericObjectOrLink<T> _) => _.Match(uri => uri, link => link.Href, obj => obj.Id);
// public static explicit operator Link(GenericObjectOrLink<T> _) => _.Match(
// uri => new Link { Href = uri },
// link => link,
// obj => new Link { Href = obj.Id, Name = obj.Name }
// );
// public static explicit operator T?(GenericObjectOrLink<T> _) => _.FetchObject();
// public static explicit operator string?(GenericObjectOrLink<T> _) => ((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<Object> {
// protected ObjectOrLink(OneOf<Uri, Link, Object> _) : base(_) { }
// public bool IsUri => this.IsT0;
// public bool IsLink => this.IsT1;
// public bool IsObject => this.IsT2;
//}
///// <summary>
///// A class representing either a collection or link
///// </summary>
//public class CollectionOrLink : GenericObjectOrLink<Collection> {
// protected CollectionOrLink(OneOf<Uri, Link, Collection> _) : base(_) { }
//}
///// <summary>
///// A class representing either a collection page or link
///// </summary>
//public class CollectionPageOrLink : GenericObjectOrLink<CollectionPage> {
// protected CollectionPageOrLink(OneOf<Uri, Link, CollectionPage> _) : base(_) { }
//}
///// <summary>
///// A class representing a list of ObjectOrLinks
///// </summary>
//public class ListOrLink<T> : List<GenericObjectOrLink<T>> where T : Object, new() {
// protected ListOrLink(List<GenericObjectOrLink<T>> _) : base(_) { }
// public static implicit operator ListOrLink<T>(Uri _) => new ListOrLink<T>(new List<GenericObjectOrLink<T>> { _ });
// public static implicit operator ListOrLink<T>(Link _) => new ListOrLink<T>(new List<GenericObjectOrLink<T>> { _ });
// public static implicit operator ListOrLink<T>(T _) => new ListOrLink<T>(new List<GenericObjectOrLink<T>> { _ });
// public static implicit operator ListOrLink<T>(string _) => new ListOrLink<T>(new List<GenericObjectOrLink<T>> { new Uri(_) });
// public static explicit operator List<Uri>(ListOrLink<T> _) => _.Cast<Uri>().ToList();
//}
/// <summary>
/// Static functions for working with ActivityPub/ActivityStreams types
/// </summary>
public static class Types {
/// <summary>
/// Normalizes the type string to title case (all types in ActivityStreams start with a capital letter)
/// </summary>
/// <param name="type">The type string to normalize</param>
/// <returns>The normalized type string (capital first letter)</returns>
public static string Normalize(string type) {
return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(type.ToLower());
}
/// <summary>
/// https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
/// </summary>
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"
];
/// <summary>
/// https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
/// </summary>
public static readonly string[] Actor = [
"Application",
"Group",
"Organization",
"Person",
"Service"
];
/// <summary>
/// https://www.w3.org/TR/activitystreams-vocabulary/#object-types
/// </summary>
public static readonly string[] Object = [
"Article",
"Audio",
"Document",
"Event",
"Image",
"Note",
"Page",
"Place",
"Profile",
"Relationship",
"Tombstone",
"Video"
];
/// <summary>
/// https://www.w3.org/TR/activitystreams-vocabulary/#object-types
/// </summary>
public static readonly string[] Link = [
"Mention"
];
/// <summary>
/// Checks if the object type is an activity type
/// </summary>
/// <param name="type">The type of the object</param>
/// <returns>A boolean indicating whether the object is an activity type</returns>
public static bool IsActivity(string type) => Activity.Contains(type);
//public static bool IsActivity(this Object obj) => obj.Type == null ? false : IsActivity(obj.Type);
/// <summary>
/// Checks if the object type is an actor type
/// </summary>
/// <param name="type">The type of the object</param>
/// <returns>A boolean indicating whether the object is an actor type</returns>
public static bool IsActor(string type) => Actor.Contains(type);
//public static bool IsActor(this Object obj) => obj.Type == null ? false : IsActor(obj.Type);
/// <summary>
/// Checks if the object type is an object type
/// </summary>
/// <param name="type">The type of the object</param>
/// <returns>A boolean indicating whether the object is an object type</returns>
public static bool IsObject(string type) => Object.Contains(type);
//public static bool IsObject(this Object obj) => obj.Type == null ? false : IsObject(obj.Type);
/// <summary>
/// Checks if the object type is a link type
/// </summary>
/// <param name="type">The type of the object</param>
/// <returns>A boolean indicating whether the object is a link type</returns>
public static bool IsLink(string type) => Link.Contains(type);
//public static bool IsLink(this Object obj) => obj.Type == null ? false : IsLink(obj.Type);
/// <summary>
/// Checks if the object type is an object or link type
/// </summary>
/// <param name="type">The type of the object</param>
/// <returns>A boolean indicating whether the object is an object or link type</returns>
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);
}