github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/r/demo/boards/public.gno (about)

     1  package boards
     2  
     3  import (
     4  	"std"
     5  	"strconv"
     6  )
     7  
     8  //----------------------------------------
     9  // Public facing functions
    10  
    11  func GetBoardIDFromName(name string) (BoardID, bool) {
    12  	boardI, exists := gBoardsByName.Get(name)
    13  	if !exists {
    14  		return 0, false
    15  	}
    16  	return boardI.(*Board).id, true
    17  }
    18  
    19  func CreateBoard(name string) BoardID {
    20  	std.AssertOriginCall()
    21  	bid := incGetBoardID()
    22  	caller := std.GetOrigCaller()
    23  	if usernameOf(caller) == "" {
    24  		panic("unauthorized")
    25  	}
    26  	url := "/r/demo/boards:" + name
    27  	board := newBoard(bid, url, name, caller)
    28  	bidkey := boardIDKey(bid)
    29  	gBoards.Set(bidkey, board)
    30  	gBoardsByName.Set(name, board)
    31  	return board.id
    32  }
    33  
    34  func checkAnonFee() bool {
    35  	sent := std.GetOrigSend()
    36  	anonFeeCoin := std.Coin{"ugnot", int64(gDefaultAnonFee)}
    37  	if len(sent) == 1 && sent[0].IsGTE(anonFeeCoin) {
    38  		return true
    39  	}
    40  	return false
    41  }
    42  
    43  func CreateThread(bid BoardID, title string, body string) PostID {
    44  	std.AssertOriginCall()
    45  	caller := std.GetOrigCaller()
    46  	if usernameOf(caller) == "" {
    47  		if !checkAnonFee() {
    48  			panic("please register, otherwise minimum fee " + strconv.Itoa(gDefaultAnonFee) + " is required if anonymous")
    49  		}
    50  	}
    51  	board := getBoard(bid)
    52  	if board == nil {
    53  		panic("board not exist")
    54  	}
    55  	thread := board.AddThread(caller, title, body)
    56  	return thread.id
    57  }
    58  
    59  func CreateReply(bid BoardID, threadid, postid PostID, body string) PostID {
    60  	std.AssertOriginCall()
    61  	caller := std.GetOrigCaller()
    62  	if usernameOf(caller) == "" {
    63  		if !checkAnonFee() {
    64  			panic("please register, otherwise minimum fee " + strconv.Itoa(gDefaultAnonFee) + " is required if anonymous")
    65  		}
    66  	}
    67  	board := getBoard(bid)
    68  	if board == nil {
    69  		panic("board not exist")
    70  	}
    71  	thread := board.GetThread(threadid)
    72  	if thread == nil {
    73  		panic("thread not exist")
    74  	}
    75  	if postid == threadid {
    76  		reply := thread.AddReply(caller, body)
    77  		return reply.id
    78  	} else {
    79  		post := thread.GetReply(postid)
    80  		reply := post.AddReply(caller, body)
    81  		return reply.id
    82  	}
    83  }
    84  
    85  // If dstBoard is private, does not ping back.
    86  // If board specified by bid is private, panics.
    87  func CreateRepost(bid BoardID, postid PostID, title string, body string, dstBoardID BoardID) PostID {
    88  	std.AssertOriginCall()
    89  	caller := std.GetOrigCaller()
    90  	if usernameOf(caller) == "" {
    91  		// TODO: allow with gDefaultAnonFee payment.
    92  		if !checkAnonFee() {
    93  			panic("please register, otherwise minimum fee " + strconv.Itoa(gDefaultAnonFee) + " is required if anonymous")
    94  		}
    95  	}
    96  	board := getBoard(bid)
    97  	if board == nil {
    98  		panic("src board not exist")
    99  	}
   100  	if board.IsPrivate() {
   101  		panic("cannot repost from a private board")
   102  	}
   103  	dst := getBoard(dstBoardID)
   104  	if dst == nil {
   105  		panic("dst board not exist")
   106  	}
   107  	thread := board.GetThread(postid)
   108  	if thread == nil {
   109  		panic("thread not exist")
   110  	}
   111  	repost := thread.AddRepostTo(caller, title, body, dst)
   112  	return repost.id
   113  }
   114  
   115  func DeletePost(bid BoardID, threadid, postid PostID, reason string) {
   116  	std.AssertOriginCall()
   117  	caller := std.GetOrigCaller()
   118  	board := getBoard(bid)
   119  	if board == nil {
   120  		panic("board not exist")
   121  	}
   122  	thread := board.GetThread(threadid)
   123  	if thread == nil {
   124  		panic("thread not exist")
   125  	}
   126  	if postid == threadid {
   127  		// delete thread
   128  		if !thread.HasPermission(caller, DeletePermission) {
   129  			panic("unauthorized")
   130  		}
   131  		board.DeleteThread(threadid)
   132  	} else {
   133  		// delete thread's post
   134  		post := thread.GetReply(postid)
   135  		if post == nil {
   136  			panic("post not exist")
   137  		}
   138  		if !post.HasPermission(caller, DeletePermission) {
   139  			panic("unauthorized")
   140  		}
   141  		thread.DeletePost(postid)
   142  	}
   143  }
   144  
   145  func EditPost(bid BoardID, threadid, postid PostID, title, body string) {
   146  	std.AssertOriginCall()
   147  	caller := std.GetOrigCaller()
   148  	board := getBoard(bid)
   149  	if board == nil {
   150  		panic("board not exist")
   151  	}
   152  	thread := board.GetThread(threadid)
   153  	if thread == nil {
   154  		panic("thread not exist")
   155  	}
   156  	if postid == threadid {
   157  		// edit thread
   158  		if !thread.HasPermission(caller, EditPermission) {
   159  			panic("unauthorized")
   160  		}
   161  		thread.Update(title, body)
   162  	} else {
   163  		// edit thread's post
   164  		post := thread.GetReply(postid)
   165  		if post == nil {
   166  			panic("post not exist")
   167  		}
   168  		if !post.HasPermission(caller, EditPermission) {
   169  			panic("unauthorized")
   170  		}
   171  		post.Update(title, body)
   172  	}
   173  }