Search Apps Documentation Source Content File Folder Download Copy Actions Download

public_freeze.gno

2.62 Kb · 105 lines
  1package boards2
  2
  3import (
  4	"chain"
  5	"strconv"
  6
  7	"gno.land/p/gnoland/boards"
  8)
  9
 10// FreezeBoard freezes a board so no more threads and comments can be created or modified.
 11func FreezeBoard(cur realm, boardID boards.ID) {
 12	setBoardReadonly(0, cur, boardID, true)
 13}
 14
 15// UnfreezeBoard removes frozen status from a board.
 16func UnfreezeBoard(cur realm, boardID boards.ID) {
 17	setBoardReadonly(0, cur, boardID, false)
 18}
 19
 20// IsBoardFrozen checks if a board has been frozen.
 21func IsBoardFrozen(boardID boards.ID) bool {
 22	board := mustGetBoard(boardID)
 23	return board.Readonly
 24}
 25
 26// FreezeThread freezes a thread so thread cannot be replied, modified or deleted.
 27//
 28// Fails if board is frozen.
 29func FreezeThread(cur realm, boardID, threadID boards.ID) {
 30	setThreadReadonly(0, cur, boardID, threadID, true)
 31}
 32
 33// UnfreezeThread removes frozen status from a thread.
 34//
 35// Fails if board is frozen.
 36func UnfreezeThread(cur realm, boardID, threadID boards.ID) {
 37	setThreadReadonly(0, cur, boardID, threadID, false)
 38}
 39
 40// IsThreadFrozen checks if a thread has been frozen.
 41//
 42// Returns true if board is frozen.
 43func IsThreadFrozen(boardID, threadID boards.ID) bool {
 44	board := mustGetBoard(boardID)
 45	thread := mustGetThread(board, threadID)
 46	return board.Readonly || thread.Readonly
 47}
 48
 49func setBoardReadonly(_ int, rlm realm, boardID boards.ID, readonly bool) {
 50	if !rlm.IsCurrent() {
 51		panic("unauthorized: rlm is not the caller's live cur")
 52	}
 53	assertRealmIsNotLocked()
 54
 55	board := mustGetBoard(boardID)
 56	if readonly {
 57		assertBoardIsNotFrozen(board)
 58	}
 59
 60	caller := rlm.Previous().Address()
 61	args := boards.Args{caller, board.ID, readonly}
 62	board.Permissions.WithPermission(caller, PermissionBoardFreeze, args, func() {
 63		assertRealmIsNotLocked()
 64
 65		board.Readonly = readonly
 66
 67		chain.Emit(
 68			"BoardFreeze",
 69			"caller", caller.String(),
 70			"boardID", board.ID.String(),
 71			"frozen", strconv.FormatBool(readonly),
 72		)
 73	})
 74}
 75
 76func setThreadReadonly(_ int, rlm realm, boardID, threadID boards.ID, readonly bool) {
 77	if !rlm.IsCurrent() {
 78		panic("unauthorized: rlm is not the caller's live cur")
 79	}
 80	assertRealmIsNotLocked()
 81
 82	board := mustGetBoard(boardID)
 83	assertBoardIsNotFrozen(board)
 84
 85	thread := mustGetThread(board, threadID)
 86	if readonly {
 87		assertThreadIsNotFrozen(thread)
 88	}
 89
 90	caller := rlm.Previous().Address()
 91	args := boards.Args{caller, board.ID, thread.ID, readonly}
 92	board.Permissions.WithPermission(caller, PermissionThreadFreeze, args, func() {
 93		assertRealmIsNotLocked()
 94
 95		thread.Readonly = readonly
 96
 97		chain.Emit(
 98			"ThreadFreeze",
 99			"caller", caller.String(),
100			"boardID", board.ID.String(),
101			"threadID", thread.ID.String(),
102			"frozen", strconv.FormatBool(readonly),
103		)
104	})
105}