-
Notifications
You must be signed in to change notification settings - Fork 399
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
- Loading branch information
Showing
4 changed files
with
106 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package boards2 | ||
|
||
import ( | ||
"std" | ||
"strconv" | ||
) | ||
|
||
var flagThreshold = 3 | ||
|
||
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 entry. | ||
// | ||
// Returns false if item was already flagged by user. | ||
AddFlag(flag Flag) bool | ||
|
||
// FlagsCount returns number of times entry 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 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