AP.NET/Repositories/ActivityPubContext.cs
Gordon Pedersen 2f152a656e Added dbcontext and reference model deps
Reference KristofferStrube.ActivityStreams for models.
His models are very similar to mine, if I put the work in I was planning to for the JSON converters and stuff. Why duplicate efforts when I can just submit PRs to the existing nuget package?
2024-04-22 09:40:14 +10:00

74 lines
3.2 KiB
C#

using KristofferStrube.ActivityStreams;
using Microsoft.EntityFrameworkCore;
using System.Net.Mail;
using Object = KristofferStrube.ActivityStreams.Object;
namespace ActivityPub.Repositories;
/// <summary>
/// Database Context for ActivityPub objects (using EF and SQLite)
/// </summary>
public class ActivityPubContext : DbContext {
/// <summary>
/// The table to store Activities
/// </summary>
public DbSet<Activity> Activities { get; set; }
/// <summary>
/// The table to store Objects
/// </summary>
public DbSet<ObjectOrLink> Objects { get; set; }
/// <summary>
/// The path to the sqlite database file
/// </summary>
public string DbPath { get; }
/// <summary>
/// Default constructor
/// </summary>
public ActivityPubContext() {
var folder = Environment.SpecialFolder.LocalApplicationData;
var path = Environment.GetFolderPath(folder);
DbPath = System.IO.Path.Join(path, "activitypub.db");
}
/// <summary>
/// The following configures EF to create a Sqlite database file in the
/// special "local" folder for your platform.
/// </summary>
/// <param name="options"></param>
protected override void OnConfiguring(DbContextOptionsBuilder options) {
options.UseSqlite($"Data Source={DbPath}");
}
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<Activity>().HasMany(_ => _.Actor as IEnumerable<Actor>);
modelBuilder.Entity<Activity>().HasMany(_ => _.Object as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Activity>().HasMany(_ => _.Target as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Activity>().HasMany(_ => _.Result as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Activity>().HasMany(_ => _.Origin as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Activity>().HasMany(_ => _.Instrument as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Object>().HasMany(_ => _.Attachment as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Object>().HasMany(_ => _.AttributedTo as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Object>().HasMany(_ => _.Audience as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Object>().HasMany(_ => _.Bcc as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Object>().HasMany(_ => _.Bto as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Object>().HasMany(_ => _.Cc as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Object>().HasMany(_ => _.Context as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Object>().HasMany(_ => _.Generator as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Object>().HasMany(_ => _.Icon as IEnumerable<Image>);
modelBuilder.Entity<Object>().HasMany(_ => _.Image as IEnumerable<Image>);
modelBuilder.Entity<Object>().HasMany(_ => _.InReplyTo as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Object>().HasMany(_ => _.Location as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Object>().HasMany(_ => _.Tag as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Object>().HasMany(_ => _.To as IEnumerable<ObjectOrLink>);
modelBuilder.Entity<Object>().HasMany(_ => _.Url as IEnumerable<Link>);
modelBuilder.Entity<Object>().Ignore(_ => _.ExtensionData);
//modelBuilder.Entity<Object>().HasMany(_ => _.Content as IList<string>);
}
}