AP.NET/DynamicObjectFactory.cs

655 lines
20 KiB
C#
Raw Normal View History

//namespace ActivityPub;
///// <summary>
///// A class to create ActivityPub Objects/Activities/etc from an incoming dynamic object
///// </summary>
//public static class DynamicObjectFactory {
// public static ObjectOrLink? NewObjectOrLink(dynamic that) {
// if (that is string) return (ObjectOrLink) new Uri((string)that);
// if (that is Uri) return (ObjectOrLink)that;
// string type = that.Type;
// if (type == null) return null; // TODO: throw exception?
// type = Types.Normalize(type);
// ObjectOrLink newObject;
// switch (type) {
// // https://www.w3.org/TR/activitystreams-vocabulary/#types
// case "Object": return (ObjectOrLink)NewObject(that);
// case "Link": return (ObjectOrLink)NewLink(that);
// case "Activity": return (ObjectOrLink)NewActivity(that);
// case "IntransitiveActivity": return (ObjectOrLink)NewIntransitiveActivity(that);
// case "Collection": return (ObjectOrLink)NewCollection(that);
// case "OrderedCollection": return (ObjectOrLink)NewOrderedCollection(that);
// case "CollectionPage": return (ObjectOrLink)NewCollectionPage(that);
// case "OrderedCollectionPage": return (ObjectOrLink)NewOrderedCollectionPage(that);
// // this is technically not an ActivityStreams type, but it's useful to exist
// case "Actor": return (ObjectOrLink)NewActor(that);
// // https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
// case "Accept": return (ObjectOrLink)NewAccept(that);
// case "Add": return (ObjectOrLink)NewAdd(that);
// case "Announce": return (ObjectOrLink)NewAnnounce(that);
// case "Arrive": return (ObjectOrLink)NewArrive(that);
// case "Block": return (ObjectOrLink)NewBlock(that);
// case "Create": return (ObjectOrLink)NewCreate(that);
// case "Delete": return (ObjectOrLink)NewDelete(that);
// case "Dislike": return (ObjectOrLink)NewDislike(that);
// case "Flag": return (ObjectOrLink)NewFlag(that);
// case "Follow": return (ObjectOrLink)NewFollow(that);
// case "Ignore": return (ObjectOrLink)NewIgnore(that);
// case "Invite": return (ObjectOrLink)NewInvite(that);
// case "Join": return (ObjectOrLink)NewJoin(that);
// case "Leave": return (ObjectOrLink)NewLeave(that);
// case "Like": return (ObjectOrLink)NewLike(that);
// case "Listen": return (ObjectOrLink)NewListen(that);
// case "Move": return (ObjectOrLink)NewMove(that);
// case "Offer": return (ObjectOrLink)NewOffer(that);
// case "Question": return (ObjectOrLink)NewQuestion(that);
// case "Reject": return (ObjectOrLink)NewReject(that);
// case "Read": return (ObjectOrLink)NewRead(that);
// case "Remove": return (ObjectOrLink)NewRemove(that);
// case "TentativeReject": return (ObjectOrLink)NewTentativeReject(that);
// case "TentativeAccept": return (ObjectOrLink)NewTentativeAccept(that);
// case "Travel": return (ObjectOrLink)NewTravel(that);
// case "Undo": return (ObjectOrLink)NewUndo(that);
// case "Update": return (ObjectOrLink)NewUpdate(that);
// case "View": return (ObjectOrLink)NewView(that);
// // https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
// case "Application": return (ObjectOrLink)NewApplication(that);
// case "Group": return (ObjectOrLink)NewGroup(that);
// case "Organization": return (ObjectOrLink)NewOrganization(that);
// case "Person": return (ObjectOrLink)NewPerson(that);
// case "Service": return (ObjectOrLink)NewService(that);
// // https://www.w3.org/TR/activitystreams-vocabulary/#object-types
// case "Article": return (ObjectOrLink)NewArticle(that);
// case "Audio": return (ObjectOrLink)NewAudio(that);
// case "Document": return (ObjectOrLink)NewDocument(that);
// case "Event": return (ObjectOrLink)NewEvent(that);
// case "Image": return (ObjectOrLink)NewImage(that);
// case "Note": return (ObjectOrLink)NewNote(that);
// case "Page": return (ObjectOrLink)NewPage(that);
// case "Place": return (ObjectOrLink)NewPlace(that);
// case "Profile": return (ObjectOrLink)NewProfile(that);
// case "Relationship": return (ObjectOrLink)NewRelationship(that);
// case "Tombstone": return (ObjectOrLink)NewTombstone(that);
// case "Video": return (ObjectOrLink)NewVideo(that);
// case "Mention": return (ObjectOrLink)NewMention(that);
// default: return null;
// }
// }
// public static ListOrLink<T>? NewListOrLink<T>(dynamic that) where T : KristofferStrube.ActivityStreams.Object, new() {
// if (that == null) return null;
// if (that is string) return (ListOrLink<T>)new Uri((string)that);
// if (that is Uri) return (ListOrLink<T>)that;
// if (that is IEnumerable<dynamic>)
// return (ListOrLink<T>)(that as IEnumerable<dynamic>).Select(x => NewObjectOrLink(x)).Cast<GenericObjectOrLink<T>>().ToList();
// return null;
// }
// public static DateTimeOffset? NewDate(dynamic that) {
// DateTimeOffset result;
// if(DateTimeOffset.TryParse(that, out result)) { return result; }
// return null;
// }
// public static Object NewObject(dynamic that, Object? self = null) {
// if(self == null) self = new Object();
// self.Id = new Uri(that.Id);
// // self.Type = that.Type; // not necessary
// self.Attachment = NewObjectOrLink(that.Attachment);
// self.AttributedTo = NewObjectOrLink(that.AttributedTo);
// self.Audience = NewObjectOrLink(that.Audience);
// self.Content = that.Content;
// self.Source = NewObjectOrLink(that.Source);
// self.Context = NewObjectOrLink(that.Id);
// self.Name = that.Name;
// self.EndTime = NewDate(that.EndTime);
// self.Generator = NewObjectOrLink(that.Generator);
// self.Icon = NewObjectOrLink(that.Icon);
// self.Image = NewObjectOrLink(that.Image);
// self.InReplyTo = NewObjectOrLink(that.InReplyTo);
// self.Location = NewObjectOrLink(that.Location);
// self.Preview = NewObjectOrLink(that.Preview);
// self.Published = NewDate(that.Published);
// self.Replies = NewObjectOrLink(that.Replies);
// self.StartTime = NewDate(that.StartTime);
// self.Summary = that.Summary;
// self.Tag = NewObjectOrLink(that.Tag);
// self.Updated = NewDate(that.Id);
// self.Url = (that.Url == null || that.Url is Uri) ? that.Url : new Uri(that.Url);
// self.To = NewListOrLink<Object>(that.To);
// self.Bto = NewListOrLink<Object>(that.Bto);
// self.Cc = NewListOrLink<Object>(that.Cc);
// self.Bcc = NewListOrLink<Object>(that.Bcc);
// self.MediaType = that.MediaType;
// self.Duration = that.Duration;
// self.Likes = NewListOrLink<Object>(that.Likes);
// self.Shares = NewListOrLink<Object>(that.Shares);
// return self;
// }
// public static Link NewLink(dynamic that, Link? self = null) {
// if (self == null) self = new Link();
// self.Id = new Uri(that.Id);
// // self.Type = that.Type; // not necessary
// self.Rel = that.Rel;
// self.MediaType = that.MediaType;
// self.Name = that.Name;
// self.Hreflang = that.Hreflang;
// self.Height = that.Height;
// self.Width = that.Width;
// self.Preview = NewObjectOrLink(that.Preview);
// return self;
// }
// public static Mention NewMention(dynamic that, Mention? self = null) {
// if(self == null) self = new Mention();
// self = NewLink(that, self);
// return self;
// }
// public static IntransitiveActivity NewIntransitiveActivity(dynamic that, IntransitiveActivity? self = null) {
// if (self == null) self = new IntransitiveActivity();
// self = NewObject(that, self);
// self.Actor = NewObjectOrLink(that.Actor);
// self.Target = NewObjectOrLink(that.Target);
// self.Result = NewObjectOrLink(that.Result);
// self.Origin = NewObjectOrLink(that.Origin);
// self.Instrument = NewObjectOrLink(that.Instrument);
// return self;
// }
// public static Activity NewActivity(dynamic that, Activity? self = null) {
// if (self == null) self = new Activity();
// self = NewIntransitiveActivity(that, self);
// self.Object = NewObjectOrLink(that.Object);
// return self;
// }
// public static Collection NewCollection(dynamic that, Collection? self = null) {
// if (self == null) self = new Collection();
// self = NewObject(that, self);
// self.TotalItems = that.TotalItems;
// self.Current = NewObjectOrLink(that.Current);
// self.First = NewObjectOrLink(that.First);
// self.Last = NewObjectOrLink(that.Last);
// self.Items = NewObjectOrLink(that.Items);
// return self;
// }
// public static Collection NewOrderedCollection(dynamic that, OrderedCollection? self = null) {
// if (self == null) self = new OrderedCollection();
// self = NewCollection(that, self);
// return self;
// }
// public static Collection NewCollectionPage(dynamic that, CollectionPage? self = null) {
// if (self == null) self = new CollectionPage();
// self = NewCollection(that, self);
// self.PartOf = NewObjectOrLink(that.PartOf);
// self.Next = NewObjectOrLink(that.Next);
// self.Prev = NewObjectOrLink(that.Prev);
// return self;
// }
// public static Collection NewOrderedCollectionPage(dynamic that, OrderedCollectionPage? self = null) {
// if (self == null) self = new OrderedCollectionPage();
// self = NewCollectionPage(that, self);
// return self;
// }
// public static OneOf<Uri, Endpoints>? NewEndpointsOrUri(dynamic that) {
// if (that == null) return null;
// if (that is string) return (OneOf<Uri, Endpoints>)new Uri((string)that);
// if (that is Uri) return (OneOf<Uri, Endpoints>)that;
// return NewEndpoints(that);
// }
// public static Endpoints NewEndpoints(dynamic that, Endpoints? self = null) {
// if (self == null) self = new Endpoints();
// self.ProxyUrl = new Uri(that.ProxyUrl);
// self.OauthAuthorizationEndpoint = new Uri(that.OauthAuthorizationEndpoint);
// self.OauthTokenEndpoint = new Uri(that.OauthTokenEndpoint);
// self.ProvideClientKey = new Uri(that.ProvideClientKey);
// self.SignClientKey = new Uri(that.SignClientKey);
// self.SharedInbox = new Uri(that.SharedInbox);
// return self;
// }
// public static Actor NewActor(dynamic that, Actor? self = null) {
// if (self == null) self = new Actor();
// self = NewObject(that, self);
// self.Inbox = NewObjectOrLink(that.Inbox);
// self.Outbox = NewObjectOrLink(that.Outbox);
// self.Following = NewObjectOrLink(that.Following);
// self.Followers = NewObjectOrLink(that.Followers);
// self.Liked = NewObjectOrLink(that.Liked);
// self.Streams = NewListOrLink<Collection>(that.Streams);
// self.PreferredUsername = that.PreferredUsername;
// self.Endpoints = NewEndpointsOrUri(that.Endpoints);
// return self;
// }
// public static Article NewArticle(dynamic that, Article? self = null) {
// if (self == null) self = new Article();
// self = NewObject(that, self);
// return self;
// }
// public static Document NewDocument(dynamic that, Document? self = null) {
// if (self == null) self = new Document();
// self = NewObject(that, self);
// return self;
// }
// public static Audio NewAudio(dynamic that, Audio? self = null) {
// if (self == null) self = new Audio();
// self = NewDocument(that, self);
// return self;
// }
// public static Event NewEvent(dynamic that, Event? self = null) {
// if (self == null) self = new Event();
// self = NewObject(that, self);
// return self;
// }
// public static Image NewImage(dynamic that, Image? self = null) {
// if (self == null) self = new Image();
// self = NewDocument(that, self);
// return self;
// }
// public static Note NewNote(dynamic that, Note? self = null) {
// if (self == null) self = new Note();
// self = NewObject(that, self);
// return self;
// }
// public static Page NewPage(dynamic that, Page? self = null) {
// if (self == null) self = new Page();
// self = NewDocument(that, self);
// return self;
// }
// public static Place NewPlace(dynamic that, Place? self = null) {
// if (self == null) self = new Place();
// self = NewObject(that, self);
// self.Accuracy = that.Accuracy;
// self.Altitude = that.Altitude;
// self.Latitude = that.Latitude;
// self.Longitude = that.Longitude;
// self.Radius = that.Radius;
// self.Units = that.Units;
// return self;
// }
// public static Profile NewProfile(dynamic that, Profile? self = null) {
// if (self == null) self = new Profile();
// self = NewObject(that, self);
// self.Describes = NewObjectOrLink(that.Describes);
// return self;
// }
// public static Relationship NewRelationship(dynamic that, Relationship? self = null) {
// if (self == null) self = new Relationship();
// self = NewObject(that, self);
// self.Subject = NewObjectOrLink(that.Subject);
// self.Object = NewObjectOrLink(that.Object);
// self.relationship = that.relationship;
// return self;
// }
// public static Tombstone NewTombstone(dynamic that, Tombstone? self = null) {
// if (self == null) self = new Tombstone();
// self = NewObject(that, self);
// self.FormerType = that.FormerType;
// self.Deleted = NewDate(that.Deleted);
// return self;
// }
// public static Video NewVideo(dynamic that, Video? self = null) {
// if (self == null) self = new Video();
// self = NewDocument(that, self);
// return self;
// }
// public static Application NewApplication(dynamic that, Application? self = null) {
// if (self == null) self = new Application();
// self = NewActor(that, self);
// return self;
// }
// public static Group NewGroup(dynamic that, Group? self = null) {
// if (self == null) self = new Group();
// self = NewActor(that, self);
// return self;
// }
// public static Organization NewOrganization(dynamic that, Organization? self = null) {
// if (self == null) self = new Organization();
// self = NewActor(that, self);
// return self;
// }
// public static Person NewPerson(dynamic that, Person? self = null) {
// if (self == null) self = new Person();
// self = NewActor(that, self);
// return self;
// }
// public static Service NewService(dynamic that, Service? self = null) {
// if (self == null) self = new Service();
// self = NewActor(that, self);
// return self;
// }
// public static Block NewBlock(dynamic that, Block? self = null) {
// if (self == null) self = new Block();
// self = NewIgnore(that, self);
// return self;
// }
// public static Invite NewInvite(dynamic that, Invite? self = null) {
// if (self == null) self = new Invite();
// self = NewOffer(that, self);
// return self;
// }
// public static Accept NewAccept(dynamic that, Accept? self = null) {
// if (self == null) self = new Accept();
// self = NewActivity(that, self);
// return self;
// }
// public static Add NewAdd(dynamic that, Add? self = null) {
// if (self == null) self = new Add();
// self = NewActivity(that, self);
// return self;
// }
// public static Announce NewAnnounce(dynamic that, Announce? self = null) {
// if (self == null) self = new Announce();
// self = NewActivity(that, self);
// return self;
// }
// public static Create NewCreate(dynamic that, Create? self = null) {
// if (self == null) self = new Create();
// self = NewActivity(that, self);
// return self;
// }
// public static Delete NewDelete(dynamic that, Delete? self = null) {
// if (self == null) self = new Delete();
// self = NewActivity(that, self);
// return self;
// }
// public static Dislike NewDislike(dynamic that, Dislike? self = null) {
// if (self == null) self = new Dislike();
// self = NewActivity(that, self);
// return self;
// }
// public static Flag NewFlag(dynamic that, Flag? self = null) {
// if (self == null) self = new Flag();
// self = NewActivity(that, self);
// return self;
// }
// public static Follow NewFollow(dynamic that, Follow? self = null) {
// if (self == null) self = new Follow();
// self = NewActivity(that, self);
// return self;
// }
// public static Ignore NewIgnore(dynamic that, Ignore? self = null) {
// if (self == null) self = new Ignore();
// self = NewActivity(that, self);
// return self;
// }
// public static Join NewJoin(dynamic that, Join? self = null) {
// if (self == null) self = new Join();
// self = NewActivity(that, self);
// return self;
// }
// public static Leave NewLeave(dynamic that, Leave? self = null) {
// if (self == null) self = new Leave();
// self = NewActivity(that, self);
// return self;
// }
// public static Like NewLike(dynamic that, Like? self = null) {
// if (self == null) self = new Like();
// self = NewActivity(that, self);
// return self;
// }
// public static Listen NewListen(dynamic that, Listen? self = null) {
// if (self == null) self = new Listen();
// self = NewActivity(that, self);
// return self;
// }
// public static Move NewMove(dynamic that, Move? self = null) {
// if (self == null) self = new Move();
// self = NewActivity(that, self);
// return self;
// }
// public static Offer NewOffer(dynamic that, Offer? self = null) {
// if (self == null) self = new Offer();
// self = NewActivity(that, self);
// return self;
// }
// public static Read NewRead(dynamic that, Read? self = null) {
// if (self == null) self = new Read();
// self = NewActivity(that, self);
// return self;
// }
// public static Reject NewReject(dynamic that, Reject? self = null) {
// if (self == null) self = new Reject();
// self = NewActivity(that, self);
// return self;
// }
// public static Remove NewRemove(dynamic that, Remove? self = null) {
// if (self == null) self = new Remove();
// self = NewActivity(that, self);
// return self;
// }
// public static Undo NewUndo(dynamic that, Undo? self = null) {
// if (self == null) self = new Undo();
// self = NewActivity(that, self);
// return self;
// }
// public static Update NewUpdate(dynamic that, Update? self = null) {
// if (self == null) self = new Update();
// self = NewActivity(that, self);
// return self;
// }
// public static View NewView(dynamic that, View? self = null) {
// if (self == null) self = new View();
// self = NewActivity(that, self);
// return self;
// }
// public static TentativeAccept NewTentativeAccept(dynamic that, TentativeAccept? self = null) {
// if (self == null) self = new TentativeAccept();
// self = NewAccept(that, self);
// return self;
// }
// public static TentativeReject NewTentativeReject(dynamic that, TentativeReject? self = null) {
// if (self == null) self = new TentativeReject();
// self = NewReject(that, self);
// return self;
// }
// public static Arrive NewArrive(dynamic that, Arrive? self = null) {
// if (self == null) self = new Arrive();
// self = NewIntransitiveActivity(that, self);
// return self;
// }
// public static Travel NewTravel(dynamic that, Travel? self = null) {
// if (self == null) self = new Travel();
// self = NewIntransitiveActivity(that, self);
// return self;
// }
// public static Question NewQuestion(dynamic that, Question? self = null) {
// if (self == null) self = new Question();
// self = NewIntransitiveActivity(that, self);
// self.OneOf = NewListOrLink<Object>(that.OneOf);
// self.AnyOf = NewListOrLink<Object>(that.AnyOf);
// ObjectOrLink? closed = NewObjectOrLink(that.Closed);
// if(closed != null) self.Closed = (Uri)closed;
// else {
// DateTimeOffset? dateClosed = NewDate(that.Closed);
// if (dateClosed.HasValue) self.Closed = dateClosed.Value;
// else self.Closed = !!that.closed;
// }
// return self;
// }
//}