Skip to content
This repository has been archived by the owner on Oct 3, 2022. It is now read-only.

Commit

Permalink
db: Fix image mod spoilering and deletion
Browse files Browse the repository at this point in the history
close #1034
  • Loading branch information
bakape committed Apr 15, 2019
1 parent 112b207 commit 37b68fe
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 21 deletions.
60 changes: 44 additions & 16 deletions db/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package db

import (
"database/sql"
"reflect"
"runtime"
"testing"
"time"

Expand Down Expand Up @@ -43,23 +41,53 @@ func writeAdminAccount(t *testing.T) {
}
}

func TestModeratePost(t *testing.T) {
func TestDeleteImages(t *testing.T) {
prepareForModeration(t)

for _, f := range []func([]uint64, string) error{
ModSpoilerImages,
DeleteImages,
DeletePosts,
} {
p := reflect.ValueOf(f).Pointer()
t.Run(runtime.FuncForPC(p).Name(), func(t *testing.T) {
t.Parallel()
p, err := GetPost(1)
if err != nil {
t.Fatal(err)
}
if p.Image == nil {
t.Fatal("no image")
}

err := f([]uint64{1}, "admin")
if err != nil {
t.Fatalf("%#v", err)
}
})
err = DeleteImages([]uint64{1}, "admin")
if err != nil {
t.Fatal(err)
}

p, err = GetPost(1)
if err != nil {
t.Fatal(err)
}
if p.Image != nil {
t.Fatal("image not deleted")
}
}

func TestSpoilerImages(t *testing.T) {
prepareForModeration(t)

p, err := GetPost(1)
if err != nil {
t.Fatal(err)
}
if p.Image.Spoiler {
t.Fatal("has spoiler")
}

err = ModSpoilerImages([]uint64{1}, "admin")
if err != nil {
t.Fatal(err)
}

p, err = GetPost(1)
if err != nil {
t.Fatal(err)
}
if !p.Image.Spoiler {
t.Fatal("no spoiler")
}
}

Expand Down
3 changes: 3 additions & 0 deletions db/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,9 @@ var migrations = []func(*sql.Tx) error{
func(tx *sql.Tx) (err error) {
return registerFunctions(tx, "delete_posts_by_ip")
},
func(tx *sql.Tx) (err error) {
return registerFunctions(tx, "delete_images", "spoiler_images")
},
}

func createIndex(table string, columns ...string) string {
Expand Down
8 changes: 4 additions & 4 deletions imager/assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package assets

import (
"io"
"os"
"path/filepath"

"github.com/bakape/meguca/common"
"github.com/bakape/meguca/config"
"github.com/bakape/meguca/util"
"os"
"path/filepath"
)

// Only used in tests, but we still need them exported
Expand All @@ -23,8 +24,7 @@ var (
MD5: "YOQQklgfezKbBXuEAsqopw",
Size: 300792,
},
Name: "sample.jpg",
Spoiler: true,
Name: "sample.jpg",
}

// StdDims contains esulting dimentions after thumbnailing sample images.
Expand Down
3 changes: 3 additions & 0 deletions static/src/sql/functions/delete_images.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ begin
checked_boards = checked_boards || jsonb_build_object(board, true);
end if;

update posts as p
set sha1 = null
where p.id = post_id;
insert into mod_log (type, board, post_id, "by")
values (3, board, post_id, account);
end loop;
Expand Down
3 changes: 3 additions & 0 deletions static/src/sql/functions/spoiler_images.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ begin
checked_boards = checked_boards || jsonb_build_object(board, true);
end if;

update posts as p
set spoiler = true
where p.id = post_id;
insert into mod_log (type, board, post_id, "by")
values (4, board, post_id, account);
end loop;
Expand Down
2 changes: 1 addition & 1 deletion static/statik/statik.go

Large diffs are not rendered by default.

0 comments on commit 37b68fe

Please sign in to comment.