parent
9d08d35576
commit
3ec6694757
@ -0,0 +1,101 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"brainbaking.com/go-jamming/app/mf"
|
||||
"brainbaking.com/go-jamming/common"
|
||||
"os"
|
||||
)
|
||||
|
||||
type MentionRepo interface {
|
||||
InModeration(key mf.Mention, data *mf.IndiewebData) (string, error)
|
||||
Save(key mf.Mention, data *mf.IndiewebData) (string, error)
|
||||
Delete(key mf.Mention)
|
||||
Approve(key mf.Mention)
|
||||
Reject(key mf.Mention)
|
||||
|
||||
Get(key mf.Mention) *mf.IndiewebData
|
||||
GetAll(domain string) mf.IndiewebDataResult
|
||||
GetAllToModerate(domain string) mf.IndiewebDataResult
|
||||
|
||||
CleanupSpam(domain string, blacklist []string)
|
||||
|
||||
SavePicture(bytes string, domain string) (string, error)
|
||||
GetPicture(domain string) []byte
|
||||
LastSentMention(domain string) string
|
||||
UpdateLastSentMention(domain string, lastSent string)
|
||||
}
|
||||
|
||||
type MentionRepoWrapper struct {
|
||||
toApproveRepo *mentionRepoBunt
|
||||
approvedRepo *mentionRepoBunt
|
||||
}
|
||||
|
||||
func (m MentionRepoWrapper) Save(key mf.Mention, data *mf.IndiewebData) (string, error) {
|
||||
return m.approvedRepo.Save(key, data)
|
||||
}
|
||||
|
||||
func (m MentionRepoWrapper) InModeration(key mf.Mention, data *mf.IndiewebData) (string, error) {
|
||||
return m.toApproveRepo.Save(key, data)
|
||||
}
|
||||
|
||||
func (m MentionRepoWrapper) SavePicture(bytes string, domain string) (string, error) {
|
||||
return m.approvedRepo.SavePicture(bytes, domain)
|
||||
}
|
||||
|
||||
func (m MentionRepoWrapper) Delete(key mf.Mention) {
|
||||
m.approvedRepo.Delete(key)
|
||||
}
|
||||
|
||||
func (m MentionRepoWrapper) Approve(keyInModeration mf.Mention) {
|
||||
toApprove := m.toApproveRepo.Get(keyInModeration)
|
||||
m.Save(keyInModeration, toApprove)
|
||||
m.toApproveRepo.Delete(keyInModeration)
|
||||
}
|
||||
|
||||
func (m MentionRepoWrapper) Reject(keyInModeration mf.Mention) {
|
||||
m.toApproveRepo.Delete(keyInModeration)
|
||||
}
|
||||
|
||||
func (m MentionRepoWrapper) CleanupSpam(domain string, blacklist []string) {
|
||||
m.approvedRepo.CleanupSpam(domain, blacklist)
|
||||
}
|
||||
|
||||
func (m MentionRepoWrapper) LastSentMention(domain string) string {
|
||||
return m.approvedRepo.LastSentMention(domain)
|
||||
}
|
||||
|
||||
func (m MentionRepoWrapper) UpdateLastSentMention(domain string, lastSent string) {
|
||||
m.approvedRepo.UpdateLastSentMention(domain, lastSent)
|
||||
}
|
||||
|
||||
func (m MentionRepoWrapper) Get(key mf.Mention) *mf.IndiewebData {
|
||||
return m.approvedRepo.Get(key)
|
||||
}
|
||||
|
||||
func (m MentionRepoWrapper) GetPicture(domain string) []byte {
|
||||
return m.approvedRepo.GetPicture(domain)
|
||||
}
|
||||
|
||||
func (m MentionRepoWrapper) GetAll(domain string) mf.IndiewebDataResult {
|
||||
return m.approvedRepo.GetAll(domain)
|
||||
}
|
||||
|
||||
func (m MentionRepoWrapper) GetAllToModerate(domain string) mf.IndiewebDataResult {
|
||||
return m.toApproveRepo.GetAll(domain)
|
||||
}
|
||||
|
||||
// NewMentionRepo returns a wrapper to two different mentionRepoBunt instances
|
||||
// Depending on the to approve or approved mention, it will be saved in another file.
|
||||
func NewMentionRepo(c *common.Config) *MentionRepoWrapper {
|
||||
return &MentionRepoWrapper{
|
||||
toApproveRepo: newMentionRepoBunt("mentions_toapprove.db", c.AllowedWebmentionSources),
|
||||
approvedRepo: newMentionRepoBunt("mentions.db", c.AllowedWebmentionSources),
|
||||
}
|
||||
}
|
||||
|
||||
// Purge removes all database files from disk.
|
||||
// This is dangerous in production and should be used as a shorthand in tests!
|
||||
func Purge() {
|
||||
os.Remove("mentions_toapprove.db")
|
||||
os.Remove("mentions.db")
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"brainbaking.com/go-jamming/app/mf"
|
||||
"brainbaking.com/go-jamming/common"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
repoCnf = &common.Config{
|
||||
AllowedWebmentionSources: []string{
|
||||
"brainbaking.com",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
func TestApproveCases(t *testing.T) {
|
||||
cases := []struct {
|
||||
label string
|
||||
approve bool
|
||||
expectedInModerationDb int
|
||||
expectedInMentionDb int
|
||||
}{
|
||||
{
|
||||
"approve moves from the to moderate db to the mention db",
|
||||
true,
|
||||
0,
|
||||
1,
|
||||
},
|
||||
{
|
||||
"reject deletes from to moderate db and leaves mention db alone",
|
||||
false,
|
||||
0,
|
||||
0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.label, func(t *testing.T) {
|
||||
repo := NewMentionRepo(repoCnf)
|
||||
defer Purge()
|
||||
|
||||
wm := mf.Mention{
|
||||
Target: "https://brainbaking.com/sjiekedinges.html",
|
||||
}
|
||||
data := &mf.IndiewebData{
|
||||
Name: "lolz",
|
||||
}
|
||||
repo.InModeration(wm, data)
|
||||
|
||||
if tc.approve {
|
||||
repo.Approve(wm)
|
||||
} else {
|
||||
repo.Reject(wm)
|
||||
}
|
||||
|
||||
allWms := repo.GetAll("brainbaking.com")
|
||||
allWmsToModerate := repo.GetAllToModerate("brainbaking.com")
|
||||
assert.Equal(t, tc.expectedInMentionDb, len(allWms.Data), "mention db expectation failed")
|
||||
assert.Equal(t, tc.expectedInModerationDb, len(allWmsToModerate.Data), "in moderation db expectation failed")
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in new issue