-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: simplify
boards2
implementation (#3115)
Refactors the code copied from `gno.land/r/demo/boards` to simplify it and to have it ready before introducing the new features.
- Loading branch information
1 parent
1510a6f
commit 4c7d16b
Showing
8 changed files
with
470 additions
and
448 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 |
---|---|---|
@@ -1,22 +1,54 @@ | ||
package boards | ||
|
||
import ( | ||
"regexp" | ||
import "gno.land/p/demo/avl" | ||
|
||
"gno.land/p/demo/avl" | ||
) | ||
|
||
//---------------------------------------- | ||
// Realm (package) state | ||
// Default minimum fee in ugnot required for anonymous users | ||
const defaultAnonymousFee = 100_000_000 | ||
|
||
var ( | ||
gBoards avl.Tree // id -> *Board | ||
gBoardsCtr int // increments Board.id | ||
gBoardsByName avl.Tree // name -> *Board | ||
gDefaultAnonFee = 100000000 // minimum fee required if anonymous | ||
gLastBoardID BoardID | ||
gBoardsByID avl.Tree // string(id) -> *Board | ||
gBoardsByName avl.Tree // string(name) -> *Board | ||
) | ||
|
||
//---------------------------------------- | ||
// Constants | ||
// incGetBoardID returns a new board ID. | ||
func incGetBoardID() BoardID { | ||
gLastBoardID++ | ||
return gLastBoardID | ||
} | ||
|
||
// getBoard returns a board for a specific ID. | ||
func getBoard(id BoardID) (_ *Board, found bool) { | ||
v, exists := gBoardsByID.Get(id.Key()) | ||
if !exists { | ||
return nil, false | ||
} | ||
return v.(*Board), true | ||
} | ||
|
||
// mustGetBoard returns a board or panics when it's not found. | ||
func mustGetBoard(id BoardID) *Board { | ||
board, found := getBoard(id) | ||
if !found { | ||
panic("board does not exist with ID: " + id.String()) | ||
} | ||
return board | ||
} | ||
|
||
// mustGetThread returns a thread or panics when it's not found. | ||
func mustGetThread(board *Board, threadID PostID) *Post { | ||
thread, found := board.GetThread(threadID) | ||
if !found { | ||
panic("thread does not exist with ID: " + threadID.String()) | ||
} | ||
return thread | ||
} | ||
|
||
var reName = regexp.MustCompile(`^[a-z]+[_a-z0-9]{2,29}$`) | ||
// mustGetReply returns a reply or panics when it's not found. | ||
func mustGetReply(thread *Post, replyID PostID) *Post { | ||
reply, found := thread.GetReply(replyID) | ||
if !found { | ||
panic("reply does not exist with ID: " + replyID.String()) | ||
} | ||
return reply | ||
} |
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,65 @@ | ||
package boards | ||
|
||
import ( | ||
"std" | ||
"strconv" | ||
"strings" | ||
|
||
"gno.land/r/demo/users" | ||
) | ||
|
||
func padLeft(s string, length int) string { | ||
if len(s) >= length { | ||
return s | ||
} | ||
return strings.Repeat(" ", length-len(s)) + s | ||
} | ||
|
||
func padZero(u64 uint64, length int) string { | ||
s := strconv.Itoa(int(u64)) | ||
if len(s) >= length { | ||
return s | ||
} | ||
return strings.Repeat("0", length-len(s)) + s | ||
} | ||
|
||
func indentBody(indent string, body string) string { | ||
var ( | ||
res string | ||
lines = strings.Split(body, "\n") | ||
) | ||
for i, line := range lines { | ||
if i > 0 { | ||
res += "\n" | ||
} | ||
res += indent + line | ||
} | ||
return res | ||
} | ||
|
||
// NOTE: length must be greater than 3. | ||
func summaryOf(text string, length int) string { | ||
lines := strings.SplitN(text, "\n", 2) | ||
line := lines[0] | ||
if len(line) > length { | ||
line = line[:(length-3)] + "..." | ||
} else if len(lines) > 1 { | ||
// len(line) <= 80 | ||
line = line + "..." | ||
} | ||
return line | ||
} | ||
|
||
// newLink returns a Markdown link. | ||
func newLink(label, uri string) string { | ||
return "[" + label + "](" + uri + ")" | ||
} | ||
|
||
// newUserLink returns a Markdown link for an account to the users realm. | ||
func newUserLink(addr std.Address) string { | ||
user := users.GetUserByAddress(addr) | ||
if user == nil { | ||
return newLink(addr.String(), "/r/demo/users:"+addr.String()) | ||
} | ||
return newLink("@"+user.Name, "/r/demo/users:"+user.Name) | ||
} |
Oops, something went wrong.