Skip to content

Commit

Permalink
Merge pull request #23 from rejectedsoftware/fix_deprecations
Browse files Browse the repository at this point in the history
Fix deprecation warnings
  • Loading branch information
s-ludwig authored Sep 8, 2024
2 parents 6d4117f + a8af0df commit ce477de
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
52 changes: 30 additions & 22 deletions source/vibelog/db/mongo.d
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ final class MongoDBController : DBController {
enforce(createdefault, "Configuration does not exist.");
auto cfg = new Config;
cfg.name = name;
m_configs.insert(cfg.toBson());
m_configs.insertOne(cfg.toBson());
return cfg;
}

void setConfig(Config cfg)
{
Bson update = cfg.toBson();
m_configs.update(["name": Bson(cfg.name)], update);
m_configs.replaceOne(["name": Bson(cfg.name)], update);
foreach (d; m_onConfigChange) d();
}

Expand All @@ -66,7 +66,7 @@ final class MongoDBController : DBController {

void deleteConfig(string name)
{
m_configs.remove(["name": Bson(name)]);
m_configs.deleteOne(["name": Bson(name)]);
}

Config[] getAllConfigs()
Expand Down Expand Up @@ -96,7 +96,7 @@ final class MongoDBController : DBController {
initial_admin.password = generatePasswordHash("admin");
initial_admin.name = "Default Administrator";
initial_admin.groups ~= "admin";
m_users.insert(initial_admin);
m_users.insertOne(initial_admin);
ret["admin"] = initial_admin;
}
return ret;
Expand Down Expand Up @@ -136,37 +136,40 @@ final class MongoDBController : DBController {
auto id = BsonObjectID.generate();
Bson userbson = user.toBson();
userbson["_id"] = Bson(id);
m_users.insert(userbson);
m_users.insertOne(userbson);
return id;
}

void modifyUser(User user)
{
assert(user._id.valid);
Bson update = user.toBson();
m_users.update(["_id": Bson(user._id)], update);
m_users.replaceOne(["_id": Bson(user._id)], update);
}

void deleteUser(BsonObjectID id)
{
assert(id.valid);
m_users.remove(["_id": Bson(id)]);
m_users.deleteOne(["_id": Bson(id)]);
}

int countPostsForCategory(string[] categories)
{
static struct CQ { @name("$in") string[] categories; }
static struct Q { CQ category; bool isPublic; }
return cast(int)m_posts.count(Q(CQ(categories), true));
return cast(int)m_posts.countDocuments(Q(CQ(categories), true));
}

void getPostsForCategory(string[] categories, int nskip, bool delegate(size_t idx, Post post) del)
{
auto cats = new Bson[categories.length];
foreach( i; 0 .. categories.length ) cats[i] = Bson(categories[i]);
Bson category = Bson(["$in" : Bson(cats)]);
Bson[string] query = ["query" : Bson(["category" : category]), "orderby" : Bson(["date" : Bson(-1)])];
foreach (idx, post; m_posts.find(query, null, QueryFlags.None, nskip).byPair) {
Bson[string] query = ["category" : category];
FindOptions opts;
opts.skip = nskip;
opts.sort = Bson(["date" : Bson(-1)]);
foreach (idx, post; m_posts.find(query, opts).byPair) {
if (!del(idx, Post.fromBson(post)))
break;
}
Expand All @@ -177,8 +180,11 @@ final class MongoDBController : DBController {
auto cats = new Bson[categories.length];
foreach( i; 0 .. categories.length ) cats[i] = Bson(categories[i]);
Bson category = Bson(["$in" : Bson(cats)]);
Bson[string] query = ["query" : Bson(["category" : category, "isPublic": Bson(true)]), "orderby" : Bson(["date" : Bson(-1)])];
foreach (idx, post; m_posts.find(query, null, QueryFlags.None, nskip).byPair) {
Bson[string] query = ["category" : category, "isPublic": Bson(true)];
FindOptions opts;
opts.skip = nskip;
opts.sort = Bson(["date" : Bson(-1)]);
foreach (idx, post; m_posts.find(query, opts).byPair) {
if (!del(idx, Post.fromBson(post)))
break;
}
Expand All @@ -187,8 +193,10 @@ final class MongoDBController : DBController {
void getAllPosts(int nskip, bool delegate(size_t idx, Post post) del)
{
Bson[string] query;
Bson[string] extquery = ["query" : Bson(query), "orderby" : Bson(["date" : Bson(-1)])];
foreach (idx, post; m_posts.find(extquery, null, QueryFlags.None, nskip).byPair) {
FindOptions opts;
opts.skip = nskip;
opts.sort = Bson(["date" : Bson(-1)]);
foreach (idx, post; m_posts.find(query, opts).byPair) {
if (!del(idx, Post.fromBson(post)))
break;
}
Expand Down Expand Up @@ -220,21 +228,21 @@ final class MongoDBController : DBController {
auto id = BsonObjectID.generate();
Bson postbson = post.toBson();
postbson["_id"] = Bson(id);
m_posts.insert(postbson);
m_posts.insertOne(postbson);
return id;
}

void modifyPost(Post post)
{
assert(post.id.valid);
Bson update = post.toBson();
m_posts.update(["_id": Bson(post.id)], update);
m_posts.replaceOne(["_id": Bson(post.id)], update);
}

void deletePost(BsonObjectID id)
{
assert(id.valid);
m_posts.remove(["_id": Bson(id)]);
m_posts.deleteOne(["_id": Bson(id)]);
}

void addFile(string post_name, string file_name, in ubyte[] contents)
Expand All @@ -244,7 +252,7 @@ final class MongoDBController : DBController {
string postName;
string fileName;
}
m_postFiles.insert(PostFile(post_name, file_name, cast(ubyte[])contents));
m_postFiles.insertOne(PostFile(post_name, file_name, cast(ubyte[])contents));
}

string[] getFiles(string post_name)
Expand All @@ -263,7 +271,7 @@ final class MongoDBController : DBController {

void removeFile(string post_name, string file_name)
{
m_postFiles.remove(["postName": post_name, "fileName": file_name]);
m_postFiles.deleteOne(["postName": post_name, "fileName": file_name]);
}

private void upgradeComments(MongoDatabase db)
Expand All @@ -278,9 +286,9 @@ final class MongoDBController : DBController {
foreach( c; p["comments"] ){
c["_id"] = BsonObjectID.generate();
c["postId"] = p["_id"];
comments.insert(c);
comments.insertOne(c);
}
m_posts.update(["_id": p["_id"]], ["$unset": ["comments": 1]]);
m_posts.updateOne(["_id": p["_id"]], ["$unset": ["comments": 1]]);
}

// Upgrade old comments to Diskuto format
Expand All @@ -297,7 +305,7 @@ final class MongoDBController : DBController {
newc.website = oldc.authorHomepage;
newc.text = oldc.content;
newc.time = oldc.date;
comments.update(["_id": c["_id"]], MongoStruct!StoredComment(newc));
comments.replaceOne(["_id": c["_id"]], MongoStruct!StoredComment(newc));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/vibelog/webadmin.d
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ logInfo("FILE %s", f.filename.name);
User[string] users = m_ctrl.db.getAllUsers();
auto pu = uname in users;
if (pu is null) {
redirect(m_subPath ~ "login?"~formEncode(["redirect": req.path]));
redirect(m_subPath ~ "login?"~formEncode(["redirect": (cast(PosixPath)req.requestPath).toString()]));
return AuthInfo.init;
}
enforceHTTP(pu !is null, HTTPStatus.forbidden, "Not authorized to access this page.");
Expand Down

0 comments on commit ce477de

Please sign in to comment.