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

Add optional filtering of help posts #140

Merged
merged 6 commits into from
Jan 7, 2025
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
16 changes: 15 additions & 1 deletion server/activity_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@ type jsonResponse struct {

func ActivityHandler(db Database) echo.HandlerFunc {
return func(c echo.Context) error {
activity, next, err := db.Activity(LocaleForRequest(c.Request()), c.QueryParam("next"), 50)
locale := LocaleForRequest(c.Request())
filter := func(a Activity) bool {
return true
}
if c.QueryParam("nohelp") == "true" {
filter = func(a Activity) bool {
if fp, ok := a.(*ForumPost); ok {
if fp.ForumId == locale.HelpForumId {
return false
}
}
return true
}
}
activity, next, err := db.Activity(locale, c.QueryParam("next"), 50, filter)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions server/bolt_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (db *BoltDatabase) AddActivity(activity []Activity) error {
})
}

func (db *BoltDatabase) Activity(locale *Locale, start string, count int) ([]Activity, string, error) {
func (db *BoltDatabase) Activity(locale *Locale, start string, count int, filter func(a Activity) bool) ([]Activity, string, error) {
ret := []Activity(nil)
next := ""
if err := db.db.View(func(tx *bolt.Tx) error {
Expand All @@ -66,7 +66,7 @@ func (db *BoltDatabase) Activity(locale *Locale, start string, count int) ([]Act
activity, err := unmarshalActivity(k, v)
if err != nil {
return err
} else if activity != nil && locale.ActivityFilter(activity) {
} else if activity != nil && locale.ActivityFilter(activity) && filter(activity) {
ret = append(ret, activity)
next = base64.RawURLEncoding.EncodeToString(k)
}
Expand Down
2 changes: 1 addition & 1 deletion server/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (

type Database interface {
AddActivity(activity []Activity) error
Activity(locale *Locale, start string, count int) ([]Activity, string, error)
Activity(locale *Locale, start string, count int, filter func(a Activity) bool) ([]Activity, string, error)
Close() error
}

Expand Down
8 changes: 5 additions & 3 deletions server/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,23 @@ func testDatabase_ForumPosts(t *testing.T, db Database) {

db.AddActivity([]Activity{post1, post2})

posts, next, err := db.Activity(locale, "", 1)
all := func(a Activity) bool { return true }

posts, next, err := db.Activity(locale, "", 1, all)
require.NoError(t, err)
require.Equal(t, 1, len(posts))
assert.Equal(t, post1.Id, posts[0].(*ForumPost).Id)
assert.Equal(t, post1.Poster, posts[0].(*ForumPost).Poster)
assert.Equal(t, post1.Time.Unix(), posts[0].(*ForumPost).Time.Unix())

posts, next, err = db.Activity(locale, next, 1)
posts, next, err = db.Activity(locale, next, 1, all)
require.NoError(t, err)
require.Equal(t, 1, len(posts))
assert.Equal(t, post2.Id, posts[0].(*ForumPost).Id)
assert.Equal(t, post2.Poster, posts[0].(*ForumPost).Poster)
assert.Equal(t, post2.Time.Unix(), posts[0].(*ForumPost).Time.Unix())

posts, _, err = db.Activity(locale, next, 1)
posts, _, err = db.Activity(locale, next, 1, all)
require.NoError(t, err)
require.Equal(t, 0, len(posts))
}
4 changes: 2 additions & 2 deletions server/dynamodb_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (db *DynamoDBDatabase) AddActivity(activity []Activity) error {
return nil
}

func (db *DynamoDBDatabase) Activity(locale *Locale, start string, count int) ([]Activity, string, error) {
func (db *DynamoDBDatabase) Activity(locale *Locale, start string, count int, filter func(a Activity) bool) ([]Activity, string, error) {
var activity []Activity

var startKey map[string]dynamodb.AttributeValue
Expand Down Expand Up @@ -120,7 +120,7 @@ func (db *DynamoDBDatabase) Activity(locale *Locale, start string, count int) ([
for _, item := range result.Items {
if a, err := unmarshalActivity(item["rk"].B, item["v"].B); err != nil {
return nil, "", err
} else if a != nil {
} else if a != nil && filter(a) {
activity = append(activity, a)
}
}
Expand Down
1 change: 1 addition & 0 deletions server/index_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var index = `<!DOCTYPE html><html>
</div>
<div class="content-box">
<h1>{{call $.Translate "Activity"}}</h1>
<div id="help-toggle"></div>
<a href="rss"><img src="static/images/rss-icon-28.png" class="rss-icon" /></a>
<table id="activity-table" class="list">
<thead>
Expand Down
11 changes: 9 additions & 2 deletions server/localization.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Locale struct {
IncludeReddit bool
Translations map[string]string
ParseTime func(s string, tz *time.Location) (time.Time, error)
HelpForumId int

forumIds atomic.Value
}
Expand Down Expand Up @@ -95,6 +96,7 @@ var Locales = []*Locale{
{
IncludeReddit: true,
Image: "static/images/locales/gb.png",
HelpForumId: 584,
},
{
Subdomain: "br",
Expand All @@ -106,6 +108,7 @@ var Locales = []*Locale{
"Time": "Hora",
"Forum": "Fórum",
},
HelpForumId: 774,
},
{
Subdomain: "ru",
Expand All @@ -119,8 +122,9 @@ var Locales = []*Locale{
},
},
{
Subdomain: "th",
Image: "static/images/locales/th.png",
Subdomain: "th",
Image: "static/images/locales/th.png",
HelpForumId: 1011,
},
{
Subdomain: "de",
Expand All @@ -132,6 +136,7 @@ var Locales = []*Locale{
"Time": "Datum",
"Forum": "Forum",
},
HelpForumId: 1123,
},
{
Subdomain: "fr",
Expand All @@ -143,6 +148,7 @@ var Locales = []*Locale{
"Time": "Date",
"Forum": "Forum",
},
HelpForumId: 1051,
},
{
Subdomain: "es",
Expand All @@ -154,6 +160,7 @@ var Locales = []*Locale{
"Time": "Fecha",
"Forum": "Foro",
},
HelpForumId: 1193,
},
{
Subdomain: "jp",
Expand Down
2 changes: 1 addition & 1 deletion server/rss_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type rssResponse struct {

func RSSHandler(db Database) echo.HandlerFunc {
return func(c echo.Context) error {
activity, _, err := db.Activity(LocaleForRequest(c.Request()), c.QueryParam("next"), 50)
activity, _, err := db.Activity(LocaleForRequest(c.Request()), c.QueryParam("next"), 50, func(a Activity) bool { return true })
if err != nil {
return err
}
Expand Down
26 changes: 23 additions & 3 deletions server/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ var POE = {
};

function loadActivity() {
var page = location.hash.replace(/^#page=/, '');
var params = new URLSearchParams(location.hash.replace(/^#/, ''));
var page = params.get('page') || '';
var nohelp = params.get('nohelp') || '';
if (currentPage !== undefined && page == currentPage) {
return;
}
var previousPage = currentPage;
currentPage = page;

$.get('activity.json?next=' + page, function(data) {
if (nohelp == 'true') {
$('#activity-table tbody').empty().append($('<tr>').append($('<td>').attr('colspan', 6).text('Loading...')))
}

$.get('activity.json?next=' + page + '&nohelp=' + nohelp, function(data) {
var $tbody = $('#activity-table tbody');
$tbody.empty();

Expand Down Expand Up @@ -135,7 +141,21 @@ function loadActivity() {
$tbody.append($tr);
}

$('#activity-nav').empty().append($('<a>').text('Next Page').attr('href', '#page=' + data.next).click(function() {
var nohelpText;
var nohelpHref;
if (nohelp != 'true') {
nohelpText = 'Hide Help Forum';
nohelpHref = '#page=' + page + '&nohelp=true';
} else {
nohelpText = 'Show Help Forum';
nohelpHref = '#page=' + page + '&nohelp=false';
}
$('#help-toggle').empty().append($('<a>').text(nohelpText).attr('href', nohelpHref).click(function() {
currentPage = undefined;
window.scrollTo(0, 0);
}));

$('#activity-nav').empty().append($('<a>').text('Next Page').attr('href', '#page=' + data.next + '&nohelp=' + nohelp).click(function() {
window.scrollTo(0, 0);
}));
}).fail(function() {
Expand Down
6 changes: 6 additions & 0 deletions server/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ div.container {
opacity: 1.0;
}

#help-toggle {
position: absolute;
top: 24px;
right: 50px;
}

div.content-box h1 {
padding: 6px;
padding-top: 0px;
Expand Down