Skip to content

Commit

Permalink
Fix activation and deactivation of articles. See also #21.
Browse files Browse the repository at this point in the history
Strange that the same code has worked before, as the logic for handling the first and last articles of a group/thread was definitely flawed.
  • Loading branch information
s-ludwig committed May 5, 2014
1 parent 8a81239 commit dace18a
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions source/vibenews/controller.d
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ class Controller {

// update the group counters
foreach( string gname, grp; oldart.groups ){
// update the group
string numfield = "groups."~gname~".articleNumber";
auto groupname = Bson(unescapeGroup(gname));
auto articlequery = Bson([numfield: Bson(["$exists": Bson(true)]), "active": Bson(true)]);
Expand All @@ -582,22 +583,27 @@ class Controller {
if( g.minArticleNumber == num ){
auto minorder = serializeToBson([numfield: 1]);
auto minart = m_articles.findOne(["query": articlequery, "orderby": minorder]);
long newnum = minart.groups[gname].articleNumber.get!long;
long newnum;
if (minart.isNull()) newnum = long.max;
else newnum = minart.groups[gname].articleNumber.get!long;
m_groups.update(["name": groupname, "minArticleNumber": num], ["$set": ["minArticleNumber": newnum]]);
}
if( g.maxArticleNumber == num ){
auto maxorder = serializeToBson([numfield: -1]);
auto maxart = m_articles.findOne(["query": articlequery, "orderby": maxorder]);
long newnum = maxart.groups[gname].articleNumber.get!long;
long newnum;
if (!maxart.isNull()) newnum = maxart.groups[gname].articleNumber.get!long;
else newnum = -1;
m_groups.update(["name": groupname, "maxArticleNumber": num], ["$set": ["maxArticleNumber": newnum]]);
}

// update the matching thread
auto threadid = grp.threadId;
auto newfirstart = m_articles.findOne(["query": ["groups."~gname~".threadId": threadid, "active": Bson(true)], "orderby": ["_id": Bson(1)]], ["_id": true]);
auto newfirstid = newfirstart.isNull() ? BsonObjectID() : newfirstart._id.get!BsonObjectID;
m_threads.update(["_id": threadid, "firstArticleId": oldart._id], ["$set": ["firstArticleId": newfirstid]]);
auto newlastart = m_articles.findOne(["query": ["groups."~gname~".threadId": threadid, "active": Bson(true)], "orderby": ["_id": Bson(-1)]], ["_id": true]);
auto newlastid = newfirstart.isNull() ? BsonObjectID() : newlastart._id.get!BsonObjectID;
m_threads.update(["_id": threadid, "firstArticleId": oldart._id], ["$set": ["firstArticleId": newfirstid]]);
m_threads.update(["_id": threadid, "lastArticleId": oldart._id], ["$set": ["lastArticleId": newlastid]]);
}
}
Expand All @@ -617,7 +623,8 @@ class Controller {
m_groups.update(["name": groupname, "maxArticleNumber": Bson(["$lt": num])], ["$set": ["maxArticleNumber": num]]);
m_groups.update(["name": groupname, "minArticleNumber": Bson(["$gt": num])], ["$set": ["minArticleNumber": num]]);

m_threads.update(["_id": threadid, "firstArticleId": Bson(["$gt": oldart._id])], ["$set": ["firstArticleId": oldart._id]]);
auto first_matches = serializeToBson([["firstArticleId": Bson(["$gt": oldart._id])], ["firstArticleId": Bson(BsonObjectID())]]);
m_threads.update(["_id": threadid, "$or": first_matches], ["$set": ["firstArticleId": oldart._id]]);
m_threads.update(["_id": threadid, "lastArticleId": Bson(["$lt": oldart._id])], ["$set": ["lastArticleId": oldart._id]]);
}
}
Expand Down

0 comments on commit dace18a

Please sign in to comment.