public_freeze.gno

3.88 Kb ยท 154 lines
  1package boards2
  2
  3import (
  4	"std"
  5	"strconv"
  6)
  7
  8// FreezeBoard freezes a board so no more threads and comments can be created or modified.
  9func FreezeBoard(_ realm, boardID BoardID) {
 10	setBoardReadonly(boardID, true)
 11}
 12
 13// UnfreezeBoard removes frozen status from a board.
 14func UnfreezeBoard(_ realm, boardID BoardID) {
 15	setBoardReadonly(boardID, false)
 16}
 17
 18// IsBoardFrozen checks if a board has been frozen.
 19func IsBoardFrozen(boardID BoardID) bool {
 20	board := mustGetBoard(boardID)
 21	return board.Readonly
 22}
 23
 24// FreezeThread freezes a thread so thread cannot be replied, modified or deleted.
 25//
 26// Fails if board is frozen.
 27func FreezeThread(_ realm, boardID BoardID, threadID PostID) {
 28	setThreadReadonly(boardID, threadID, true)
 29}
 30
 31// UnfreezeThread removes frozen status from a thread.
 32//
 33// Fails if board is frozen.
 34func UnfreezeThread(_ realm, boardID BoardID, threadID PostID) {
 35	setThreadReadonly(boardID, threadID, false)
 36}
 37
 38// IsThreadFrozen checks if a thread has been frozen.
 39//
 40// Returns true if board is frozen.
 41func IsThreadFrozen(boardID BoardID, threadID PostID) bool {
 42	board := mustGetBoard(boardID)
 43	thread := mustGetThread(board, threadID)
 44	assertThreadIsVisible(thread)
 45
 46	return board.Readonly || thread.Readonly
 47}
 48
 49// UnfreezeReply removes frozen status from a reply.
 50//
 51// Fails when parent thread or board are frozen.
 52func UnfreezeReply(_ realm, boardID BoardID, threadID, replyID PostID) {
 53	// XXX: Is there a use case for also freezing replies?
 54	setReplyReadonly(boardID, threadID, replyID, false)
 55}
 56
 57// FreezeReply freezes a thread reply so it cannot be modified or deleted.
 58//
 59// Fails when parent thread or board are frozen.
 60func FreezeReply(_ realm, boardID BoardID, threadID, replyID PostID) {
 61	setReplyReadonly(boardID, threadID, replyID, true)
 62}
 63
 64// IsReplyFrozen checks if a thread reply has been frozen.
 65//
 66// Returns true when board or a parent thread is frozen.
 67func IsReplyFrozen(boardID BoardID, threadID, replyID PostID) bool {
 68	board := mustGetBoard(boardID)
 69	thread := mustGetThread(board, threadID)
 70	assertThreadIsVisible(thread)
 71
 72	reply := mustGetReply(thread, replyID)
 73	assertReplyIsVisible(reply)
 74
 75	return board.Readonly || thread.Readonly || reply.Readonly
 76}
 77
 78func setReplyReadonly(boardID BoardID, threadID, replyID PostID, isReadonly bool) {
 79	assertRealmIsNotLocked()
 80
 81	board := mustGetBoard(boardID)
 82	assertBoardIsNotFrozen(board)
 83
 84	caller := std.PreviousRealm().Address()
 85	assertHasPermission(board.perms, caller, PermissionReplyFreeze)
 86
 87	thread := mustGetThread(board, threadID)
 88	assertThreadIsVisible(thread)
 89	assertThreadIsNotFrozen(thread)
 90
 91	reply := mustGetReply(thread, replyID)
 92	assertReplyIsVisible(reply)
 93
 94	if isReadonly {
 95		assertReplyIsNotFrozen(reply)
 96	}
 97
 98	reply.Readonly = isReadonly
 99
100	std.Emit(
101		"ReplyFreeze",
102		"caller", caller.String(),
103		"boardID", boardID.String(),
104		"threadID", threadID.String(),
105		"replyID", replyID.String(),
106		"frozen", strconv.FormatBool(isReadonly),
107	)
108}
109
110func setThreadReadonly(boardID BoardID, threadID PostID, isReadonly bool) {
111	assertRealmIsNotLocked()
112
113	board := mustGetBoard(boardID)
114	assertBoardIsNotFrozen(board)
115
116	caller := std.PreviousRealm().Address()
117	assertHasPermission(board.perms, caller, PermissionThreadFreeze)
118
119	thread := mustGetThread(board, threadID)
120	if isReadonly {
121		assertThreadIsNotFrozen(thread)
122	}
123
124	thread.Readonly = isReadonly
125
126	std.Emit(
127		"ThreadFreeze",
128		"caller", caller.String(),
129		"boardID", boardID.String(),
130		"threadID", threadID.String(),
131		"frozen", strconv.FormatBool(isReadonly),
132	)
133}
134
135func setBoardReadonly(boardID BoardID, isReadonly bool) {
136	assertRealmIsNotLocked()
137
138	board := mustGetBoard(boardID)
139	if isReadonly {
140		assertBoardIsNotFrozen(board)
141	}
142
143	caller := std.PreviousRealm().Address()
144	assertHasPermission(board.perms, caller, PermissionBoardFreeze)
145
146	board.Readonly = isReadonly
147
148	std.Emit(
149		"BoardFreeze",
150		"caller", caller.String(),
151		"boardID", boardID.String(),
152		"frozen", strconv.FormatBool(isReadonly),
153	)
154}