forked from adam/discord-bot-shtik
Compare commits
No commits in common. "224a3c5a622ab50febc2758d08323800fcc72d70" and "0ff3902fd0da156a02752ead2d49f7c453b50e3f" have entirely different histories.
224a3c5a62
...
0ff3902fd0
11
Behaver.cs
11
Behaver.cs
@ -224,15 +224,4 @@ public class Behaver
|
||||
|
||||
return await iprotocol.SendFile(channel, path, accompanyingText);
|
||||
}
|
||||
public async Task<int> SendFile(Guid channelId, string base64dData, string filename, string accompanyingText)
|
||||
{
|
||||
var channel = Rememberer.ChannelDetail(channelId);
|
||||
if (channel == null)
|
||||
return 404;
|
||||
var iprotocol = fetchInterface(channel);
|
||||
if (iprotocol == null)
|
||||
return 404;
|
||||
|
||||
return await iprotocol.SendFile(channel, base64dData, filename, accompanyingText);
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ public class Channel
|
||||
public List<Channel> SubChannels { get; set; }
|
||||
[JsonIgnore]
|
||||
public Channel ParentChannel { get; set; }
|
||||
public Guid? ParentChannelId { get; set; }
|
||||
public string Protocol { get; set; }
|
||||
[DeleteBehavior(DeleteBehavior.Cascade)]
|
||||
public List<Message> Messages { get; set; }
|
||||
|
@ -434,7 +434,7 @@ public class DiscordInterface : ProtocolInterface
|
||||
return 503;
|
||||
}
|
||||
}
|
||||
public override async Task<int> SendFile(Channel channel, string base64dData, string filename, string accompanyingText)
|
||||
public override async Task<int> SendFile(Channel channel, string path, string accompanyingText)
|
||||
{
|
||||
var dcCh = await client.GetChannelAsync(ulong.Parse(channel.ExternalId));
|
||||
if (dcCh == null)
|
||||
@ -444,10 +444,7 @@ public class DiscordInterface : ProtocolInterface
|
||||
|
||||
if (dcCh is IMessageChannel msgChannel)
|
||||
{
|
||||
using (var ms = new MemoryStream(Convert.FromBase64String(base64dData)))
|
||||
{
|
||||
await msgChannel.SendFileAsync(ms, filename, TruncateText(accompanyingText, channel.MaxTextChars));
|
||||
}
|
||||
await msgChannel.SendFileAsync(path, TruncateText(accompanyingText, channel.MaxTextChars));
|
||||
return 200;
|
||||
}
|
||||
else
|
||||
|
@ -7,16 +7,7 @@ public abstract class ProtocolInterface
|
||||
public static string Protocol { get; }
|
||||
public abstract Channel SelfChannel { get; }
|
||||
public abstract Task<int> SendMessage(Channel channel, string text);
|
||||
public virtual async Task<int> SendFile(Channel channel, string path, string accompanyingText)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
return 404;
|
||||
}
|
||||
var fstring = Convert.ToBase64String(File.ReadAllBytes(path));
|
||||
return await SendFile(channel, fstring, Path.GetFileName(path), accompanyingText);
|
||||
}
|
||||
public abstract Task<int> SendFile(Channel channel, string base64dData, string filename, string accompanyingText);
|
||||
public abstract Task<int> SendFile(Channel channel, string path, string accompanyingText);
|
||||
public abstract Task<int> React(Message message, string reaction);
|
||||
public abstract Task<int> Reply(Message message, string text);
|
||||
}
|
||||
|
@ -295,7 +295,7 @@ public class TwitchInterface : ProtocolInterface
|
||||
Task.Run(() => { client.SendMessage(channel.ExternalId, text); });
|
||||
return 200;
|
||||
}
|
||||
public override async Task<int> SendFile(Channel channel, string base64dData, string filename, string accompanyingText)
|
||||
public override async Task<int> SendFile(Channel channel, string path, string accompanyingText)
|
||||
{
|
||||
return 405;
|
||||
}
|
||||
|
@ -8,25 +8,6 @@ public static class Rememberer
|
||||
{
|
||||
private static readonly SemaphoreSlim dbAccessSemaphore = new(1, 1);
|
||||
private static readonly ChattingContext db = new();
|
||||
private static List<Channel> channels;
|
||||
private static bool channelCacheDirty = true;
|
||||
|
||||
private static void cacheChannels()
|
||||
{
|
||||
dbAccessSemaphore.Wait();
|
||||
channels = db.Channels.ToList();
|
||||
foreach (Channel ch in channels)
|
||||
{
|
||||
if (ch.ParentChannelId != null)
|
||||
{
|
||||
ch.ParentChannel = channels.FirstOrDefault(c => c.Id == ch.ParentChannelId);
|
||||
ch.ParentChannel.SubChannels ??= [];
|
||||
ch.ParentChannel.SubChannels.Add(ch);
|
||||
}
|
||||
}
|
||||
channelCacheDirty = false;
|
||||
dbAccessSemaphore.Release();
|
||||
}
|
||||
|
||||
public static Account SearchAccount(Expression<Func<Account, bool>> predicate)
|
||||
{
|
||||
@ -52,11 +33,13 @@ public static class Rememberer
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static Channel SearchChannel(Func<Channel, bool> predicate)
|
||||
public static Channel SearchChannel(Expression<Func<Channel, bool>> predicate)
|
||||
{
|
||||
if(channelCacheDirty)
|
||||
Task.Run(() => cacheChannels()).Wait();
|
||||
return channels.FirstOrDefault(predicate);
|
||||
Channel toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
toReturn = db.Channels.FirstOrDefault(predicate);
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
}
|
||||
public static Message SearchMessage(Expression<Func<Message, bool>> predicate)
|
||||
{
|
||||
@ -92,14 +75,10 @@ public static class Rememberer
|
||||
}
|
||||
public static Channel RememberChannel(Channel toRemember)
|
||||
{
|
||||
if(channelCacheDirty)
|
||||
Task.Run(() => cacheChannels()).Wait(); //so we always do 2 db trips?
|
||||
dbAccessSemaphore.Wait();
|
||||
db.Update(toRemember);
|
||||
db.SaveChanges();
|
||||
dbAccessSemaphore.Release();
|
||||
channelCacheDirty = true;
|
||||
cacheChannels();
|
||||
return toRemember;
|
||||
}
|
||||
public static void RememberMessage(Message toRemember)
|
||||
@ -223,9 +202,14 @@ public static class Rememberer
|
||||
}
|
||||
public static Channel ChannelDetail(Guid Id)
|
||||
{
|
||||
if(channelCacheDirty)
|
||||
Task.Run(() => cacheChannels()).Wait();
|
||||
return channels.Find(c => c.Id == Id);
|
||||
Channel toReturn;
|
||||
dbAccessSemaphore.Wait();
|
||||
toReturn = db.Channels.Find(Id);
|
||||
dbAccessSemaphore.Release();
|
||||
return toReturn;
|
||||
// .Include(u => u.SubChannels)
|
||||
// .Include(u => u.Users)
|
||||
// .Include(u => u.ParentChannel);
|
||||
}
|
||||
public static Message MessageDetail(Guid Id)
|
||||
{
|
||||
|
@ -17,37 +17,56 @@ public class InternalAPIProtocolController : ControllerBase
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public class extraSpecialObjectReadGlorifiedTupleFor_PostMessage
|
||||
{
|
||||
public string messageText;
|
||||
public Guid channelId;
|
||||
}
|
||||
[HttpPost]
|
||||
[Route("PostMessage")]
|
||||
[Produces("application/json")]
|
||||
public IActionResult PostMessage(string messageText, Guid channelId)
|
||||
public IActionResult PostMessage([FromBody] extraSpecialObjectReadGlorifiedTupleFor_PostMessage param)
|
||||
{
|
||||
return StatusCode(Behaver.Instance.SendMessage(channelId, messageText).Result);
|
||||
return StatusCode(Behaver.Instance.SendMessage(param.channelId, param.messageText).Result);
|
||||
}
|
||||
public class extraSpecialObjectReadGlorifiedTupleFor_ReplyToMessage
|
||||
{
|
||||
public string messageText;
|
||||
public Guid repliedMessageId;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("ReplyToMessage")]
|
||||
[Produces("application/json")]
|
||||
public IActionResult ReplyToMessage(string messageText, Guid repliedMessageId)
|
||||
public IActionResult ReplyToMessage([FromBody] extraSpecialObjectReadGlorifiedTupleFor_ReplyToMessage param)
|
||||
{
|
||||
Console.WriteLine($"ReplyToMessage - {repliedMessageId}, {messageText}");
|
||||
return StatusCode(Behaver.Instance.Reply(repliedMessageId, messageText).Result);
|
||||
Console.WriteLine($"ReplyToMessage - {param.repliedMessageId}, {param.messageText}");
|
||||
return StatusCode(Behaver.Instance.Reply(param.repliedMessageId, param.messageText).Result);
|
||||
}
|
||||
|
||||
public class extraSpecialObjectReadGlorifiedTupleFor_SendFile
|
||||
{
|
||||
public Guid channelId; public string path; public string accompanyingText;
|
||||
}
|
||||
[HttpPost]
|
||||
[Route("SendFile")]
|
||||
[Produces("application/json")]
|
||||
public IActionResult SendFile(Guid channelId, string accompanyingText, string base64dData, string filename)
|
||||
public IActionResult SendFile([FromBody] extraSpecialObjectReadGlorifiedTupleFor_SendFile param)
|
||||
{
|
||||
Console.WriteLine($"SendFile- {channelId}, {filename} (base64'd, {base64dData?.Length} chars), {accompanyingText}");
|
||||
return StatusCode(Behaver.Instance.SendFile(channelId, base64dData, filename, accompanyingText).Result);
|
||||
Console.WriteLine($"SendFile- {param.channelId}, {param.path}, {param.accompanyingText}");
|
||||
return StatusCode(Behaver.Instance.SendFile(param.channelId, param.path, param.accompanyingText).Result);
|
||||
}
|
||||
|
||||
public class extraSpecialObjectReadGlorifiedTupleFor_ReactToMessage
|
||||
{
|
||||
public string reactionString;
|
||||
public Guid reactedMessageId;
|
||||
}
|
||||
[HttpPost]
|
||||
[Route("ReactToMessage")]
|
||||
[Produces("application/json")]
|
||||
public IActionResult ReactToMessage(string reactionString, Guid reactedMessageId)
|
||||
public IActionResult ReactToMessage([FromBody] extraSpecialObjectReadGlorifiedTupleFor_ReactToMessage param)
|
||||
{
|
||||
Console.WriteLine($"ReactToMessage- {reactedMessageId}, {reactionString}");
|
||||
return StatusCode(Behaver.Instance.React(reactedMessageId, reactionString).Result);
|
||||
Console.WriteLine($"ReactToMessage- {param.reactedMessageId}, {param.reactionString}");
|
||||
return StatusCode(Behaver.Instance.React(param.reactedMessageId, param.reactionString).Result);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user