Search Apps Documentation Source Content File Folder Download Copy Actions Download

public_freeze.gno

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