Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Fix delete old articles logic
Browse files Browse the repository at this point in the history
- Fix double-negative in delete query
- Change logic to use days instead of weeks
- Use to_start_date() consistently
  • Loading branch information
brendanlong committed May 27, 2019
1 parent 9ea9b37 commit efe36f8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
4 changes: 1 addition & 3 deletions plugins/backend/decsync/decsyncInterface.vala
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,7 @@ public class FeedReader.decsyncInterface : FeedServerInterface {
var feeds = DataBase.readOnly().read_feeds();
var articles = new Gee.ArrayList<Article>();
GLib.Mutex mutex = GLib.Mutex();
var now = new GLib.DateTime.now_local();
int? weeks = ((DropArticles)Settings.general().get_enum("drop-articles-after")).to_weeks();
var dropDate = weeks == null ? null : now.add_weeks(-(int)weeks);
DateTime? dropDate = ((DropArticles)Settings.general().get_enum("drop-articles-after")).to_start_date();

try
{
Expand Down
6 changes: 3 additions & 3 deletions src/Backend/FeedServer.vala
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,10 @@ public class FeedReader.FeedServer : GLib.Object {
Notification.send(newArticles, new_and_unread);
}

var drop_weeks = drop_articles.to_weeks();
if(drop_weeks != null)
var article_max_age_days = drop_articles.to_days();
if(article_max_age_days != null)
{
db.dropOldArticles(-(int)drop_weeks);
db.dropOldArticles(article_max_age_days);
}

int last_modified = db.getLastModified();
Expand Down
8 changes: 4 additions & 4 deletions src/DataBaseWriteAccess.vala
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ public class FeedReader.DataBase : DataBaseReadOnly {
Settings.state().set_int("last-spring-cleaning", (int)now.to_unix());
}

public void dropOldArticles(int weeks)
public void dropOldArticles(int max_age_days)
{
var query = new QueryBuilder(QueryType.SELECT, "main.articles");
query.select_field("articleID");
query.select_field("feedID");
query.where("datetime(date, 'unixepoch', 'localtime') <= datetime('now', '-%i days')".printf(weeks*7));
query.where(@"datetime(date, 'unixepoch', 'localtime') <= datetime('now', '-$(max_age_days) days')");
query.where_equal_int("marked", ArticleStatus.UNMARKED.to_int());
if(FeedServer.get_default().useMaxArticles())
{
Expand Down Expand Up @@ -456,6 +456,7 @@ public class FeedReader.DataBase : DataBaseReadOnly {
assert (guidHash_position > 0);
assert (modified_position > 0);

DateTime? drop_date = ((DropArticles)Settings.general().get_enum("drop-articles-after")).to_start_date();
foreach(var article in articles)
{
// if article time is in the future
Expand All @@ -465,8 +466,7 @@ public class FeedReader.DataBase : DataBaseReadOnly {
article.SetDate(now);
}

int? weeks = ((DropArticles)Settings.general().get_enum("drop-articles-after")).to_weeks();
if(weeks != null && article.getDate().compare(now.add_weeks(-(int)weeks)) == -1)
if(drop_date != null && article.getDate().compare(drop_date) == -1)
{
Logger.info("Ignoring old article: %s".printf(article.getTitle()));
continue;
Expand Down
24 changes: 12 additions & 12 deletions src/Enums.vala
Original file line number Diff line number Diff line change
Expand Up @@ -177,32 +177,32 @@ namespace FeedReader {
ONE_MONTH,
SIX_MONTHS;

public int? to_weeks()
public int? to_days()
{
switch(this)
{
case NEVER:
case NEVER:
return null;
case ONE_WEEK:
return 1;
case ONE_MONTH:
return 4;
case SIX_MONTHS:
return 24;
default:
case ONE_WEEK:
return 7;
case ONE_MONTH:
return 31;
case SIX_MONTHS:
return 186;
default:
assert_not_reached();
}
}

public DateTime? to_start_date()
{
int? weeks = to_weeks();
if(weeks == null)
int? days = to_days();
if(days == null)
{
return null;
}

return new DateTime.now_utc().add_weeks(-(int)weeks);
return new DateTime.now_utc().add_days(-(int)days);
}
}

Expand Down

0 comments on commit efe36f8

Please sign in to comment.