-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(boards2): add core flagging logic (#3451)
Add flagging support for boards2 realm. Closes #3146
- Loading branch information
Showing
11 changed files
with
268 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package boards2 | ||
|
||
import ( | ||
"std" | ||
"strconv" | ||
) | ||
|
||
const flagThreshold = 1 | ||
|
||
type Flag struct { | ||
User std.Address | ||
Reason string | ||
} | ||
|
||
func NewFlag(creator std.Address, reason string) Flag { | ||
return Flag{ | ||
User: creator, | ||
Reason: reason, | ||
} | ||
} | ||
|
||
type Flaggable interface { | ||
// AddFlag adds a new flag to an item. | ||
// | ||
// Returns false if item was already flagged by user. | ||
AddFlag(flag Flag) bool | ||
|
||
// FlagsCount returns number of times item was flagged. | ||
FlagsCount() int | ||
} | ||
|
||
// flagItem adds a flag to a flaggable item (post, thread, etc). | ||
// | ||
// Returns whether flag count threshold is reached and item can be hidden. | ||
// | ||
// Panics if flag count threshold was already reached. | ||
func flagItem(item Flaggable, flag Flag) bool { | ||
if item.FlagsCount() >= flagThreshold { | ||
panic("item flag count threshold exceeded: " + strconv.Itoa(flagThreshold)) | ||
} | ||
|
||
if !item.AddFlag(flag) { | ||
panic("item has been already flagged by a current user") | ||
} | ||
|
||
return item.FlagsCount() == flagThreshold | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"std" | ||
|
||
"gno.land/r/demo/boards2" | ||
) | ||
|
||
const owner = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // @test1 | ||
|
||
func init() { | ||
std.TestSetOrigCaller(owner) | ||
} | ||
|
||
func main() { | ||
bid := boards2.CreateBoard("test1") | ||
pid := boards2.CreateThread(bid, "thread", "thread") | ||
boards2.FlagThread(bid, pid, "reason") | ||
_ = boards2.CreateReply(bid, pid, pid, "reply") | ||
} | ||
|
||
// Error: | ||
// thread with ID: 1 was hidden |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"std" | ||
|
||
"gno.land/r/demo/boards2" | ||
) | ||
|
||
const owner = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // @test1 | ||
|
||
func init() { | ||
std.TestSetOrigCaller(owner) | ||
} | ||
|
||
func main() { | ||
// ensure that nested replies denied if root thread is hidden. | ||
bid := boards2.CreateBoard("test1") | ||
pid := boards2.CreateThread(bid, "thread", "thread") | ||
rid := boards2.CreateReply(bid, pid, pid, "reply1") | ||
|
||
boards2.FlagThread(bid, pid, "reason") | ||
_ = boards2.CreateReply(bid, pid, rid, "reply1.1") | ||
} | ||
|
||
// Error: | ||
// thread with ID: 1 was hidden |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"std" | ||
|
||
"gno.land/r/demo/boards2" | ||
) | ||
|
||
const owner = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // @test1 | ||
|
||
func init() { | ||
std.TestSetOrigCaller(owner) | ||
} | ||
|
||
func main() { | ||
// ensure that nested replies denied if root thread is hidden. | ||
bid := boards2.CreateBoard("test1") | ||
pid := boards2.CreateThread(bid, "thread", "thread") | ||
rid := boards2.CreateReply(bid, pid, pid, "reply1") | ||
|
||
boards2.FlagReply(bid, pid, rid, "reason") | ||
_ = boards2.CreateReply(bid, pid, rid, "reply1.1") | ||
} | ||
|
||
// Error: | ||
// reply with ID: 2 was hidden |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
import ( | ||
"std" | ||
|
||
"gno.land/r/demo/boards2" | ||
) | ||
|
||
const owner = std.Address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // @test1 | ||
|
||
func init() { | ||
std.TestSetOrigCaller(owner) | ||
} | ||
|
||
func main() { | ||
// Only single user per flag can't be tested atm, as flagThreshold = 1. | ||
bid := boards2.CreateBoard("test1") | ||
pid := boards2.CreateThread(bid, "thread", "thread") | ||
|
||
boards2.FlagThread(bid, pid, "reason1") | ||
boards2.FlagThread(bid, pid, "reason2") | ||
} | ||
|
||
// Error: | ||
// item flag count threshold exceeded: 1 |