Permissions associated with channel

This commit is contained in:
Adam R Grey 2023-06-19 11:03:06 -04:00
parent a1d2ec83b5
commit e433e56fec
24 changed files with 414 additions and 42 deletions

View File

@ -39,9 +39,9 @@ public class Behaver
foreach (var behavior in behaviors)
{
if (behavior.ShouldAct(permissions, message))
if (behavior.ShouldAct(message))
{
behavior.ActOn(permissions, message);
behavior.ActOn(message);
message.ActedOn = true;
}
}

View File

@ -11,10 +11,9 @@ using System.Collections.Generic;
//expect a behavior to be created per mesage
public abstract class Behavior
{
//TODO: message should have a channel, which should provide permissions. shouldn't have to pass it here.
public abstract Task<bool> ActOn(PermissionSettings permissions, Message message);
public abstract Task<bool> ActOn(Message message);
public virtual bool ShouldAct(PermissionSettings permissions, Message message)
public virtual bool ShouldAct(Message message)
{
return Regex.IsMatch(message.Content, $"{Trigger}\\b", RegexOptions.IgnoreCase);
}

View File

@ -16,7 +16,7 @@ public class ChatGPTSnark : Behavior
public override string Description => "snarkiness about the latest culty-fixation in ai";
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
await message.Channel.SendMessage("chatGPT is **weak**. also, are we done comparing every little if-then-else to skynet?");
return true;

View File

@ -16,7 +16,7 @@ public class DefinitionSnarkCogDiss : Behavior
public override string Description => "snarkiness about the rampant misuse of the term cognitive dissonance";
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
await message.Reply("that's not what cognitive dissonance means. Did you mean \"hypocrisy\"?");
return true;

View File

@ -16,7 +16,7 @@ public class DefinitionSnarkGaslight : Behavior
public override string Description => "snarkiness about the rampant misuse of the term gaslighting";
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
await message.Channel.SendMessage("that's not what gaslight means. Did you mean \"say something that (you believe) is wrong\"?");
return true;

View File

@ -22,9 +22,9 @@ public class Detiktokify : Behavior
ytdl.OutputFolder = "";
ytdl.OutputFileTemplate = "tiktokbad.%(ext)s";
}
public override bool ShouldAct(PermissionSettings permissions, Message message)
public override bool ShouldAct(Message message)
{
if(permissions.MaxAttachmentBytes == 0)
if(message.Channel.EffectivePermissions.MaxAttachmentBytes == 0)
return false;
var wordLikes = message.Content.Split(' ', StringSplitOptions.TrimEntries);
@ -41,7 +41,7 @@ public class Detiktokify : Behavior
}
return tiktokLinks.Any();
}
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
foreach(var link in tiktokLinks)
{
@ -64,7 +64,7 @@ public class Detiktokify : Behavior
if (File.Exists(path))
{
var bytesize = new System.IO.FileInfo(path).Length;
if (bytesize < permissions.MaxAttachmentBytes - 256)
if (bytesize < message.Channel.EffectivePermissions.MaxAttachmentBytes - 256)
{
try
{

View File

@ -18,7 +18,7 @@ public class FiximageHeic : Behavior
public override string Description => "convert heic images to jpg";
private List<Attachment> heics = new List<Attachment>();
public override bool ShouldAct(PermissionSettings permissions, Message message)
public override bool ShouldAct(Message message)
{
if (message.Attachments?.Count() > 0)
{
@ -33,7 +33,7 @@ public class FiximageHeic : Behavior
return heics.Any();
}
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
if (!Directory.Exists("tmp"))
{

View File

@ -15,13 +15,13 @@ public class GeneralSnarkCloudNative : Behavior
public override string Name => "general snarkiness: cloud native";
public override string Trigger => "certain tech buzzwords that no human uses in normal conversation";
public override bool ShouldAct(PermissionSettings permissions, Message message)
public override bool ShouldAct(Message message)
{
return Regex.IsMatch(message.Content, "\\bcloud( |-)?native\\b", RegexOptions.IgnoreCase) ||
Regex.IsMatch(message.Content, "\\benterprise( |-)?(level|solution)\\b", RegexOptions.IgnoreCase);
}
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
switch (Shared.r.Next(2))
{

View File

@ -17,11 +17,11 @@ public class GeneralSnarkPlaying : Behavior
public override string Description => "I didn't think you were playing, but now I do";
public override bool ShouldAct(PermissionSettings permissions, Message message)
public override bool ShouldAct(Message message)
{
return Regex.IsMatch(message.Content, "^(s?he|(yo)?u|y'?all|they) thinks? i'?m (playin|jokin|kiddin)g?$", RegexOptions.IgnoreCase);
}
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
await message.Channel.SendMessage("I believed you for a second, but then you assured me you's a \uD83C\uDDE7 \uD83C\uDDEE \uD83C\uDDF9 \uD83C\uDDE8 \uD83C\uDDED");
return true;

View File

@ -16,7 +16,7 @@ public class GeneralSnarkSkynet : Behavior
public override string Description => "snarkiness about the old AI fixation";
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
switch (Shared.r.Next(5))
{

View File

@ -15,11 +15,11 @@ public class Gratitude : Behavior
public override string Trigger => "thank me";
public override bool ShouldAct(PermissionSettings permissions, Message message)
public override bool ShouldAct(Message message)
{
return Regex.IsMatch(message.Content, "\\bthank (yo)?u\\b", RegexOptions.IgnoreCase) && message.MentionsMe;
}
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
switch (Shared.r.Next(4))

View File

@ -17,7 +17,7 @@ public class Joke : Behavior
public override string Description => "tell a joke";
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
Console.WriteLine("joking");
var jokes = File.ReadAllLines("assets/jokes.txt");

View File

@ -18,14 +18,14 @@ public class LaughAtOwnJoke : Behavior
public override string Description => Name;
public static List<string> punchlinesAwaitingReaction = new List<string>();
public override bool ShouldAct(PermissionSettings permissions, Message message)
public override bool ShouldAct(Message message)
{
//TODO: i need to keep track of myself from here somehow
return false;
//return message.Author == me && punchlinesAwaitingReaction.Contains(message.Content);
}
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
punchlinesAwaitingReaction.Remove(message.Content);
await message.React("\U0001F60E"); //smiling face with sunglasses

View File

@ -18,7 +18,7 @@ public class PepTalk : Behavior
public override string Description => "assembles a pep talk from a few pieces";
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{var piece1 = new List<string>{
"Champ, ",
"Fact: ",

View File

@ -14,7 +14,7 @@ public class PulseCheck : Behavior
public override string Trigger => "!pluse ?check";
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
await message.Channel.SendFile("assets/ekgblip.png", null);
return true;

View File

@ -18,7 +18,7 @@ public class QRify : Behavior
public override string Description => "generate text QR codes";
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
var qrContent = message.Content.Substring($"{Trigger} ".Length + message.Content.IndexOf(Trigger));
Console.WriteLine($"qring: {qrContent}");

View File

@ -10,7 +10,7 @@ public class UnitConvert : Behavior
public override string Trigger => "!freedomunits";
public override string Description => "convert between many units.";
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
var theseMatches = Regex.Matches(message.Content, "\\b([\\d]+\\.?\\d*) ?([^\\d\\s].*) (in|to|as) ([^\\d\\s].*)$", RegexOptions.IgnoreCase);

View File

@ -16,7 +16,7 @@ public class WishLuck : Behavior
public override string Description => "wishes you luck";
public override async Task<bool> ActOn(PermissionSettings permissions, Message message)
public override async Task<bool> ActOn(Message message)
{
if (Shared.r.Next(20) == 0)
{

View File

@ -18,6 +18,15 @@ public class DiscordInterface
internal DiscordSocketClient client;
private bool eventsSignedUp = false;
private ChattingContext _db;
private static PermissionSettings defaultPermissions = new PermissionSettings()
{
MeannessFilterLevel = 1,
LewdnessFilterLevel = 3,
MaxTextChars = 2000,
MaxAttachmentBytes = 8 * 1024 * 1024,
LinksAllowed = true,
ReactionsPossible = true
};
public DiscordInterface()
{
_db = Shared.dbContext;
@ -42,7 +51,7 @@ public class DiscordInterface
client.MessageReceived += MessageReceived;
// _client.MessageUpdated +=
client.UserJoined += UserJoined;
// client.UserJoined += UserJoined;
client.SlashCommandExecuted += SlashCommandHandler;
// _client.ChannelCreated +=
// _client.ChannelDestroyed +=
@ -100,13 +109,13 @@ public class DiscordInterface
_db.SaveChanges();
}
private Task UserJoined(SocketGuildUser arg)
private void UserJoined(SocketGuildUser arg)
{
var guild = UpsertChannel(arg.Guild);
var defaultChannel = UpsertChannel(arg.Guild.DefaultChannel);
defaultChannel.ParentChannel = guild;
var u = UpsertUser(arg);
return Behaver.Instance.OnJoin(u, defaultChannel);
}
private async Task ButtonHandler(SocketMessageComponent component)
{

View File

@ -0,0 +1,255 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using vassago.Models;
#nullable disable
namespace vassago.Migrations
{
[DbContext(typeof(ChattingContext))]
[Migration("20230619144448_permissionsmove")]
partial class permissionsmove
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.5")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("vassago.Models.Attachment", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<byte[]>("Content")
.HasColumnType("bytea");
b.Property<string>("ContentType")
.HasColumnType("text");
b.Property<string>("Description")
.HasColumnType("text");
b.Property<decimal?>("ExternalId")
.HasColumnType("numeric(20,0)");
b.Property<string>("Filename")
.HasColumnType("text");
b.Property<Guid?>("MessageId")
.HasColumnType("uuid");
b.Property<int>("Size")
.HasColumnType("integer");
b.Property<string>("Source")
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("MessageId");
b.ToTable("Attachments");
});
modelBuilder.Entity("vassago.Models.Channel", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<string>("DisplayName")
.HasColumnType("text");
b.Property<decimal?>("ExternalId")
.HasColumnType("numeric(20,0)");
b.Property<bool>("IsDM")
.HasColumnType("boolean");
b.Property<Guid?>("ParentChannelId")
.HasColumnType("uuid");
b.Property<int?>("PermissionsId")
.HasColumnType("integer");
b.Property<string>("Protocol")
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("ParentChannelId");
b.HasIndex("PermissionsId");
b.ToTable("Channels");
});
modelBuilder.Entity("vassago.Models.Message", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<bool>("ActedOn")
.HasColumnType("boolean");
b.Property<Guid?>("AuthorId")
.HasColumnType("uuid");
b.Property<Guid?>("ChannelId")
.HasColumnType("uuid");
b.Property<string>("Content")
.HasColumnType("text");
b.Property<decimal?>("ExternalId")
.HasColumnType("numeric(20,0)");
b.Property<bool>("MentionsMe")
.HasColumnType("boolean");
b.Property<DateTimeOffset>("Timestamp")
.HasColumnType("timestamp with time zone");
b.HasKey("Id");
b.HasIndex("AuthorId");
b.HasIndex("ChannelId");
b.ToTable("Messages");
});
modelBuilder.Entity("vassago.Models.PermissionSettings", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<int?>("LewdnessFilterLevel")
.HasColumnType("integer");
b.Property<bool?>("LinksAllowed")
.HasColumnType("boolean");
b.Property<long?>("MaxAttachmentBytes")
.HasColumnType("bigint");
b.Property<long?>("MaxTextChars")
.HasColumnType("bigint");
b.Property<int?>("MeannessFilterLevel")
.HasColumnType("integer");
b.Property<bool?>("ReactionsPossible")
.HasColumnType("boolean");
b.HasKey("Id");
b.ToTable("PermissionSettings");
});
modelBuilder.Entity("vassago.Models.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<decimal?>("ExternalId")
.HasColumnType("numeric(20,0)");
b.Property<bool>("IsBot")
.HasColumnType("boolean");
b.Property<string>("Protocol")
.HasColumnType("text");
b.Property<Guid?>("SeenInChannelId")
.HasColumnType("uuid");
b.Property<string>("Username")
.HasColumnType("text");
b.HasKey("Id");
b.HasIndex("SeenInChannelId");
b.ToTable("Users");
});
modelBuilder.Entity("vassago.Models.Attachment", b =>
{
b.HasOne("vassago.Models.Message", "Message")
.WithMany("Attachments")
.HasForeignKey("MessageId");
b.Navigation("Message");
});
modelBuilder.Entity("vassago.Models.Channel", b =>
{
b.HasOne("vassago.Models.Channel", "ParentChannel")
.WithMany("SubChannels")
.HasForeignKey("ParentChannelId");
b.HasOne("vassago.Models.PermissionSettings", "Permissions")
.WithMany()
.HasForeignKey("PermissionsId");
b.Navigation("ParentChannel");
b.Navigation("Permissions");
});
modelBuilder.Entity("vassago.Models.Message", b =>
{
b.HasOne("vassago.Models.User", "Author")
.WithMany()
.HasForeignKey("AuthorId");
b.HasOne("vassago.Models.Channel", "Channel")
.WithMany("Messages")
.HasForeignKey("ChannelId");
b.Navigation("Author");
b.Navigation("Channel");
});
modelBuilder.Entity("vassago.Models.User", b =>
{
b.HasOne("vassago.Models.Channel", "SeenInChannel")
.WithMany()
.HasForeignKey("SeenInChannelId");
b.Navigation("SeenInChannel");
});
modelBuilder.Entity("vassago.Models.Channel", b =>
{
b.Navigation("Messages");
b.Navigation("SubChannels");
});
modelBuilder.Entity("vassago.Models.Message", b =>
{
b.Navigation("Attachments");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,70 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace vassago.Migrations
{
/// <inheritdoc />
public partial class permissionsmove : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Channels_PermissionSettings_PermissionsOverridesId",
table: "Channels");
migrationBuilder.RenameColumn(
name: "PermissionsOverridesId",
table: "Channels",
newName: "PermissionsId");
migrationBuilder.RenameIndex(
name: "IX_Channels_PermissionsOverridesId",
table: "Channels",
newName: "IX_Channels_PermissionsId");
migrationBuilder.AddColumn<bool>(
name: "ReactionsPossible",
table: "PermissionSettings",
type: "boolean",
nullable: true);
migrationBuilder.AddForeignKey(
name: "FK_Channels_PermissionSettings_PermissionsId",
table: "Channels",
column: "PermissionsId",
principalTable: "PermissionSettings",
principalColumn: "Id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Channels_PermissionSettings_PermissionsId",
table: "Channels");
migrationBuilder.DropColumn(
name: "ReactionsPossible",
table: "PermissionSettings");
migrationBuilder.RenameColumn(
name: "PermissionsId",
table: "Channels",
newName: "PermissionsOverridesId");
migrationBuilder.RenameIndex(
name: "IX_Channels_PermissionsId",
table: "Channels",
newName: "IX_Channels_PermissionsOverridesId");
migrationBuilder.AddForeignKey(
name: "FK_Channels_PermissionSettings_PermissionsOverridesId",
table: "Channels",
column: "PermissionsOverridesId",
principalTable: "PermissionSettings",
principalColumn: "Id");
}
}
}

View File

@ -77,7 +77,7 @@ namespace vassago.Migrations
b.Property<Guid?>("ParentChannelId")
.HasColumnType("uuid");
b.Property<int?>("PermissionsOverridesId")
b.Property<int?>("PermissionsId")
.HasColumnType("integer");
b.Property<string>("Protocol")
@ -87,7 +87,7 @@ namespace vassago.Migrations
b.HasIndex("ParentChannelId");
b.HasIndex("PermissionsOverridesId");
b.HasIndex("PermissionsId");
b.ToTable("Channels");
});
@ -151,6 +151,9 @@ namespace vassago.Migrations
b.Property<int?>("MeannessFilterLevel")
.HasColumnType("integer");
b.Property<bool?>("ReactionsPossible")
.HasColumnType("boolean");
b.HasKey("Id");
b.ToTable("PermissionSettings");
@ -199,13 +202,13 @@ namespace vassago.Migrations
.WithMany("SubChannels")
.HasForeignKey("ParentChannelId");
b.HasOne("vassago.Models.PermissionSettings", "PermissionsOverrides")
b.HasOne("vassago.Models.PermissionSettings", "Permissions")
.WithMany()
.HasForeignKey("PermissionsOverridesId");
.HasForeignKey("PermissionsId");
b.Navigation("ParentChannel");
b.Navigation("PermissionsOverrides");
b.Navigation("Permissions");
});
modelBuilder.Entity("vassago.Models.Message", b =>

View File

@ -14,7 +14,7 @@ public class Channel
public ulong? ExternalId { get; set; }
public string DisplayName { get; set; }
public bool IsDM { get; set; }
public PermissionSettings PermissionsOverrides { get; set; }
public PermissionSettings Permissions { get; set; }
public List<Channel> SubChannels { get; set; }
public Channel ParentChannel { get; set; }
public string Protocol { get; set; }
@ -25,4 +25,39 @@ public class Channel
[NonSerialized]
public Func<string, Task> SendMessage;
public PermissionSettings EffectivePermissions
{
get
{
PermissionSettings toReturn = Permissions ?? new PermissionSettings();
return GetEffectivePermissions(ref toReturn);
}
}
private PermissionSettings GetEffectivePermissions(ref PermissionSettings settings)
{
if(settings == null) throw new ArgumentNullException();
settings.LewdnessFilterLevel = settings.LewdnessFilterLevel ?? Permissions?.LewdnessFilterLevel;
settings.MeannessFilterLevel = settings.MeannessFilterLevel ?? Permissions?.MeannessFilterLevel;
settings.LinksAllowed = settings.LinksAllowed ?? Permissions?.LinksAllowed;
settings.MaxAttachmentBytes = settings.MaxAttachmentBytes ?? Permissions?.MaxAttachmentBytes;
settings.MaxTextChars = settings.MaxTextChars ?? Permissions?.MaxTextChars;
settings.ReactionsPossible = settings.ReactionsPossible ?? Permissions?.ReactionsPossible;
if(this.ParentChannel != null &&
(settings.LewdnessFilterLevel == null ||
settings.MeannessFilterLevel == null ||
settings.LinksAllowed == null ||
settings.MaxAttachmentBytes == null ||
settings.MaxTextChars == null ||
settings.ReactionsPossible == null))
{
return this.ParentChannel.GetEffectivePermissions(ref settings);
}
else
{
return settings;
}
}
}

View File

@ -10,6 +10,7 @@ public class PermissionSettings
public uint? MaxAttachmentBytes { get; set; }
public uint? MaxTextChars { get; set; }
public bool? LinksAllowed { get; set; }
public bool? ReactionsPossible { get; set; }
public int? LewdnessFilterLevel { get; set; }
public int? MeannessFilterLevel { get; set; }
}