restfulness is not working. Must implement rememberer.
Some checks failed
gitea.arg.rip/vassago/pipeline/head There was a failure building this commit

This commit is contained in:
adam 2025-01-30 17:43:48 -05:00
parent 203c6af3cf
commit c3a9ac3c54
7 changed files with 108 additions and 13 deletions

View File

@ -31,6 +31,8 @@ public class Behaver
private static readonly Behaver _instance = new Behaver(); private static readonly Behaver _instance = new Behaver();
//TODO: you know why I didn't make this a static class? lifecycle issues with the dbcontext. but now that we don't have a stored instance,
//no need to have a... *checks over shoulder*... *whispers*: singleton
public static Behaver Instance public static Behaver Instance
{ {
get { return _instance; } get { return _instance; }
@ -80,6 +82,7 @@ public class Behaver
CollapseUsers(SelfUser, selfAccount.IsUser, db); CollapseUsers(SelfUser, selfAccount.IsUser, db);
} }
SelfAccounts = db.Accounts.Where(a => a.IsUser == SelfUser).ToList(); SelfAccounts = db.Accounts.Where(a => a.IsUser == SelfUser).ToList();
db.SaveChanges();
} }
public bool CollapseUsers(User primary, User secondary, ChattingContext db) public bool CollapseUsers(User primary, User secondary, ChattingContext db)

View File

@ -16,7 +16,7 @@ public class ChattingContext : DbContext
public ChattingContext() : base() { } public ChattingContext() : base() { }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
optionsBuilder.UseNpgsql(Shared.DBConnectionString) optionsBuilder.UseNpgsql(Shared.DBConnectionString);
.EnableSensitiveDataLogging(true); //who the fuck is looking at log output but not allowed to see it? this should be on by default. //.EnableSensitiveDataLogging(true); //"sensitive" is one thing. writing "did something" every time you think a thought is a different thing.
} }
} }

View File

@ -198,7 +198,7 @@ public class DiscordInterface
a.Filename = dAttachment.Filename; a.Filename = dAttachment.Filename;
a.Size = dAttachment.Size; a.Size = dAttachment.Size;
a.Source = new Uri(dAttachment.Url); a.Source = new Uri(dAttachment.Url);
db.SaveChanges();
return a; return a;
} }
internal Message UpsertMessage(IUserMessage dMessage) internal Message UpsertMessage(IUserMessage dMessage)
@ -234,6 +234,7 @@ public class DiscordInterface
m.Reply = (t) => { return dMessage.ReplyAsync(t); }; m.Reply = (t) => { return dMessage.ReplyAsync(t); };
m.React = (e) => { return attemptReact(dMessage, e); }; m.React = (e) => { return attemptReact(dMessage, e); };
db.SaveChangesAsync();
return m; return m;
} }
internal Channel UpsertChannel(IMessageChannel channel) internal Channel UpsertChannel(IMessageChannel channel)
@ -276,6 +277,7 @@ public class DiscordInterface
c.DisplayName = "DM: " + (channel as IPrivateChannel).Recipients?.FirstOrDefault(u => u.Id != client.CurrentUser.Id).Username; c.DisplayName = "DM: " + (channel as IPrivateChannel).Recipients?.FirstOrDefault(u => u.Id != client.CurrentUser.Id).Username;
break; break;
} }
db.SaveChangesAsync();
return c; return c;
} }
internal Channel UpsertChannel(IGuild channel) internal Channel UpsertChannel(IGuild channel)
@ -286,6 +288,7 @@ public class DiscordInterface
{ {
c = new Channel(); c = new Channel();
db.Channels.Add(c); db.Channels.Add(c);
Console.WriteLine($"upserting channel {channel.Name} from discord, have to create a new one in the DB");
} }
c.DisplayName = channel.Name; c.DisplayName = channel.Name;
@ -299,6 +302,7 @@ public class DiscordInterface
c.SendMessage = (t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; cannot accept text"); }; c.SendMessage = (t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; cannot accept text"); };
c.SendFile = (f, t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; send file"); }; c.SendFile = (f, t) => { throw new InvalidOperationException($"channel {channel.Name} is guild; send file"); };
db.SaveChanges();
return c; return c;
} }
internal Account UpsertAccount(IUser user, Channel inChannel) internal Account UpsertAccount(IUser user, Channel inChannel)
@ -323,6 +327,7 @@ public class DiscordInterface
acc.IsUser = new User() { Accounts = new List<Account>() { acc } }; acc.IsUser = new User() { Accounts = new List<Account>() { acc } };
db.Users.Add(acc.IsUser); db.Users.Add(acc.IsUser);
} }
db.SaveChanges();
return acc; return acc;
} }

View File

@ -47,7 +47,7 @@ public class ChannelsController : Controller
} }
var sb = new StringBuilder(); var sb = new StringBuilder();
sb.Append("["); sb.Append("[");
sb.Append("{text: \"channels\", nodes: ["); sb.Append($"{{text: \"{channel.SubChannels?.Count}\", nodes: [");
var first=true; var first=true;
foreach(var subChannel in channel.SubChannels) foreach(var subChannel in channel.SubChannels)
{ {
@ -59,9 +59,9 @@ public class ChannelsController : Controller
{ {
first = false; first = false;
} }
sb.Append($"{{\"text\": \"<a href=\\\"{Url.ActionLink(action: "Details", controller: "Channels", values: new {id = subChannel.Id})}\\\">{subChannel.DisplayName}</a>\""); sb.Append($"{{\"text\": \"<a href=\\\"{Url.ActionLink(action: "Details", controller: "Channels", values: new {id = subChannel.Id})}\\\">{subChannel.DisplayName}</a>\"}}");
} }
sb.Append("]"); sb.Append("]}]");
ViewData.Add("channelsTree", sb.ToString()); ViewData.Add("channelsTree", sb.ToString());
return View( return View(

View File

@ -41,4 +41,53 @@ public class ChannelsController : ControllerBase
_db.SaveChanges(); _db.SaveChanges();
return Ok(fromDb); return Ok(fromDb);
} }
[HttpDelete]
[Produces("application/json")]
public IActionResult Delete([FromBody] Channel channel)
{
var fromDb = _db.Channels.Find(channel.Id);
if (fromDb == null)
{
_logger.LogError($"attempt to delete channel {channel.Id}, not found");
return NotFound();
}
deleteChannel(fromDb);
_db.SaveChanges();
return Ok();
}
private void deleteChannel(Channel channel)
{
if (channel.SubChannels?.Count > 0)
{
foreach (var childChannel in channel.SubChannels)
{
deleteChannel(childChannel);
}
}
if(channel.Users?.Count > 0)
{
foreach(var account in channel.Users)
{
deleteAccount(account);
}
}
if(channel.Messages?.Count > 0)
{
_db.Remove(channel.Messages);
}
_db.Remove(channel);
}
private void deleteAccount(Account account)
{
var user = account.IsUser;
var usersOnlyAccount = user.Accounts?.Count == 1;
_db.Remove(account);
if(usersOnlyAccount)
_db.Users.Remove(user);
}
} }

View File

@ -93,6 +93,11 @@
<th scope="row">Accounts</th> <th scope="row">Accounts</th>
<td>@(ThisChannel.Users?.Count ?? 0)</td> <td>@(ThisChannel.Users?.Count ?? 0)</td>
</tr> </tr>
<tr>
<td colspan="2">
<button onclick="forget()">forget</button>
</td>
</tr>
</tbody> </tbody>
</table> </table>
@ -117,15 +122,18 @@
console.log(channelNow); console.log(channelNow);
return channelNow; return channelNow;
} }
@if((ThisChannel.SubChannels?.Count ?? 0) > 0) function forget(){
{ console.log("here we go");
/* if(window.confirm("delete? really really?") == true){
deleteModel(jsonifyChannel(), '/api/Channels/');
}
}
function channelsTree() { function channelsTree() {
var tree = @Html.Raw(ViewData["treeString"]); var tree = @Html.Raw(ViewData["channelsTree"]);
return tree; return tree;
} }
$('#channelsTree').bstreeview({ data: channelsTree() }); $('#channelsTree').bstreeview({ data: channelsTree() });
*/
}
</script> </script>
} }

View File

@ -20,7 +20,6 @@ function patchModel(model, apiUrl)
if(components[2] !== "Details") if(components[2] !== "Details")
{ {
console.log("wtf are you doing? " + components[2] + " is something other than Details"); console.log("wtf are you doing? " + components[2] + " is something other than Details");
//add different endpoings here, if you like
} }
var type=components[1]; var type=components[1];
var id=components[3]; var id=components[3];
@ -48,3 +47,34 @@ function patchModel(model, apiUrl)
console.error('Error:', error); console.error('Error:', error);
}); });
} }
function deleteModel(model, apiUrl)
{
var components = window.location.pathname.split('/');
if(components[2] !== "Details")
{
console.log("wtf are you doing? " + components[2] + " is something other than Details");
}
var type=components[1];
var id=components[3];
fetch(apiUrl, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(model),
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not "ok". which is not ok.');
}
return response.json();
})
.then(returnedSuccessdata => {
// perhaps a success callback
console.log('returnedSuccessdata:', returnedSuccessdata);
})
.catch(error => {
console.error('Error:', error);
});
}