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 tag blacklist for automatic filtering #31

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
40 changes: 40 additions & 0 deletions DataManager/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@ func CachedUser(u *User) *User {
log.Println("not a *User in cache")
return u
}
fmt.Println("Got cached user", cu.ID)
return cu
} else {
C.Cache.Set("USR", strconv.Itoa(u.ID), u)
fmt.Println("Set user in cache", u.ID)
}
return u
}

type User struct {
ID int
Name string

blacklist *string

passwordHash string
salt string
Joined time.Time
Expand Down Expand Up @@ -157,6 +162,41 @@ func (u *User) SetName(name string) {
u.Name = name
}

func (u *User) QBlacklist(q querier)string {
if u.ID == 0 {
return ""
}

if u.blacklist != nil {
return *u.blacklist
}

var s string

err := q.QueryRow("SELECT blacklist FROM users WHERE id = $1", u.ID).Scan(&s)
if err != nil {
log.Println(err)
return ""
}

u.blacklist = &s
return *u.blacklist
}

func (u *User) SetBlacklist(q querier, blacklist string) error {
if u.ID == 0 {
return errors.New("User is not logged in!")
}

if _, err := q.Exec("UPDATE users SET blacklist = $1 WHERE id = $2", blacklist, u.ID); err != nil {
return err
}

u.blacklist = &blacklist

return nil
}

func (u *User) Flag() flag {
if u.flag != nil {
return *u.flag
Expand Down
1 change: 1 addition & 0 deletions handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func init() {

Handlers["/user/"] = makeStatHandler(UserHandler)
Handlers["/user/taghistory/"] = makeStatHandler(UserTagHistoryHandler)
Handlers["/user/blacklist/"] = makeStatHandler(userBlacklistHandler)
Handlers["/user/pool/"] = makeStatHandler(UserPoolHandler)
Handlers["/user/pool/remove/"] = makeStatHandler(editUserPoolHandler)
Handlers["/user/pools/"] = makeStatHandler(UserPoolsHandler)
Expand Down
10 changes: 7 additions & 3 deletions handlers/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type Postpage struct {
Base base
Post *DM.Post
Voted bool
Voted bool
Comments []*DM.PostComment
Dups *DM.Duplicate
Comics []*DM.Comic
Expand Down Expand Up @@ -250,7 +250,9 @@ func PostsHandler(w http.ResponseWriter, r *http.Request) {

bm := benchmark.Begin()

p.User = userCookies(w, r)
var user *DM.User
user, p.User = getUser(w, r)
user = DM.CachedUser(user)

pageLimit := p.User.Limit

Expand Down Expand Up @@ -315,8 +317,10 @@ func PostsHandler(w http.ResponseWriter, r *http.Request) {

bm.Split("Before posts")

blacklist := user.QBlacklist(DM.DB)

pc := &DM.PostCollector{}
err = pc.Get(tagString, p.Sidebar.Filter, p.Sidebar.Unless, order)
err = pc.Get(tagString, p.Sidebar.Filter+","+blacklist, p.Sidebar.Unless, order)
if err != nil {
//log.Println(err)
// notFoundHandler(w, r)
Expand Down
25 changes: 25 additions & 0 deletions handlers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type UserInfo struct {

func UserHandler(w http.ResponseWriter, r *http.Request) {
u, ui := getUser(w, r)
u = DM.CachedUser(u)
profile := u

paths := splitURI(r.URL.Path)
Expand All @@ -43,9 +44,12 @@ func UserHandler(w http.ResponseWriter, r *http.Request) {
Profile *DM.User
RecentPosts []*DM.Post
RecentVotes []*DM.Post
Blacklist string
}

var p = page{User: u, UserInfo: ui, Profile: profile}
u.QName(DM.DB)
p.Blacklist = u.QBlacklist(DM.DB)
profile.QName(DM.DB)
profile.QFlag(DM.DB)

Expand Down Expand Up @@ -119,6 +123,27 @@ func UserTagHistoryHandler(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "taghistory", p)
}

func userBlacklistHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
notFoundHandler(w, r)
return
}

u, _ := getUser(w, r)

if u.QID(DM.DB) == 0 {
http.Error(w, "Not logged in error", http.StatusBadRequest)
return
}

if err := u.SetBlacklist(DM.DB, r.FormValue("blacklist")); err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}

http.Redirect(w, r, r.Referer(), http.StatusSeeOther)
}

func LoginHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
username := r.FormValue("username")
Expand Down
1 change: 1 addition & 0 deletions out/sql/up10.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE users ADD COLUMN blacklist TEXT NOT NULL DEFAULT '';
4 changes: 4 additions & 0 deletions out/templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ <h4>Recent votes</h4>

{{end}}
{{if .User.ID}}
<form action="/user/blacklist/" method="POST">
<textarea name="blacklist">{{.Blacklist}}</textarea>
<input type="submit">
</form>
<h2><a href="/logout/">Logout</a></h2>
<h3><a href="/login/">Sessions</a></h3>
{{else}}
Expand Down