Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid deprecated MongoDB APIs #17

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dub.sdl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name "diskuto"
description "Embedded comment engine"
authors "Sönke Ludwig"
copyright "Copyright © 2017-2020, Sönke Ludwig"
copyright "Copyright © 2017-2023, Sönke Ludwig"
license "MIT"
dependency "vibe-d:mongodb" version=">=0.7.31 <0.10.0-0"
dependency "vibe-d:web" version=">=0.7.31 <0.10.0-0"
Expand Down
20 changes: 10 additions & 10 deletions source/diskuto/commentstores/mongodb.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class MongoDBCommentStore : DiskutoCommentStore {

// upgrade "author" field name
foreach (c; m_comments.find(["userID": ["$exists": true]], ["userID": true]))
m_comments.update(["_id": c["_id"]], ["$unset": ["userID": Bson(null)], "$set": ["author": c["userID"]]]);
m_comments.updateOne(["_id": c["_id"]], ["$unset": ["userID": Bson(null)], "$set": ["author": c["userID"]]]);
// upgrade missing "clientAddress" field name
m_comments.update(["clientAddress": ["$exists": false]], ["$set": ["clientAddress": ""]], UpdateFlags.multiUpdate);
m_comments.updateMany(["clientAddress": ["$exists": false]], ["$set": ["clientAddress": ""]]);
// upgrade old status field
m_comments.update(["status": cast(int)StoredComment.Status.active], ["$set": ["status": "active"]], UpdateFlags.multiUpdate);
m_comments.update(["status": cast(int)StoredComment.Status.disabled], ["$set": ["status": "disabled"]], UpdateFlags.multiUpdate);
m_comments.updateMany(["status": cast(int)StoredComment.Status.active], ["$set": ["status": "active"]]);
m_comments.updateMany(["status": cast(int)StoredComment.Status.disabled], ["$set": ["status": "disabled"]]);

m_comments.createIndex(["topic": 1, "status": 1]);
}
Expand All @@ -39,7 +39,7 @@ class MongoDBCommentStore : DiskutoCommentStore {
{
auto mc = MongoStruct!StoredComment(comment);
mc._id = BsonObjectID.generate();
m_comments.insert(mc);
m_comments.insertOne(mc);
return mc._id.toString();
}

Expand All @@ -53,17 +53,17 @@ class MongoDBCommentStore : DiskutoCommentStore {
void setCommentStatus(StoredComment.ID id, StoredComment.Status status)
{
import std.conv : to;
m_comments.update(["_id": BsonObjectID.fromString(id)], ["$set": ["status": status.to!string]]);
m_comments.updateOne(["_id": BsonObjectID.fromString(id)], ["$set": ["status": status.to!string]]);
}

void editComment(StoredComment.ID id, string new_text)
{
m_comments.update(["_id": BsonObjectID.fromString(id)], ["$set": ["text": new_text]]);
m_comments.updateOne(["_id": BsonObjectID.fromString(id)], ["$set": ["text": new_text]]);
}

void deleteComment(StoredComment.ID id)
{
m_comments.remove(["_id": BsonObjectID.fromString(id)]);
m_comments.deleteOne(["_id": BsonObjectID.fromString(id)]);
}

VoteDirection vote(StoredComment.ID id, StoredComment.UserID user, VoteDirection direction)
Expand Down Expand Up @@ -91,7 +91,7 @@ class MongoDBCommentStore : DiskutoCommentStore {
if (direction != VoteDirection.none) {
static struct DQ { @name("$ne") string ne; }
static struct Q { BsonObjectID _id; DQ downvotes; DQ author; }
m_comments.update(Q(BsonObjectID.fromString(id), DQ(user), DQ(user)), ["$addToSet": [direction == VoteDirection.up ? "upvotes" : "downvotes": user]]);
m_comments.updateOne(Q(BsonObjectID.fromString(id), DQ(user), DQ(user)), ["$addToSet": [direction == VoteDirection.up ? "upvotes" : "downvotes": user]]);
}

return direction;
Expand All @@ -100,7 +100,7 @@ class MongoDBCommentStore : DiskutoCommentStore {
uint getActiveCommentCount(string topic)
{
import std.conv : to;
return m_comments.count(["topic": topic, "status": "active"]).to!uint;
return m_comments.countDocuments(["topic": topic, "status": "active"]).to!uint;
}

void iterateAllComments(scope void delegate(ref StoredComment) del)
Expand Down
Loading