Control socket: add chat messages

このコミットが含まれているのは:
n9k 2022-08-02 04:50:45 +00:00
コミット f3d613de3b
2個のファイルの変更31行の追加6行の削除

ファイルの表示

@ -4,9 +4,11 @@
import itertools import itertools
from anonstream.chat import delete_chat_messages from anonstream.chat import delete_chat_messages
from anonstream.control.exceptions import CommandFailed
from anonstream.control.spec import NoParse from anonstream.control.spec import NoParse
from anonstream.control.spec.common import Str, End, Args from anonstream.control.spec.common import Str, End, Args, ArgsJsonString, ArgsUser
from anonstream.control.spec.utils import get_item, json_dumps_contiguous from anonstream.control.spec.utils import get_item, json_dumps_contiguous
from anonstream.chat import add_chat_message, Rejected
class ArgsSeqs(Args): class ArgsSeqs(Args):
def consume(self, words, index): def consume(self, words, index):
@ -33,12 +35,18 @@ async def cmd_chat_help():
response = ( response = (
'Usage: chat delete SEQS\n' 'Usage: chat delete SEQS\n'
'Commands:\n' 'Commands:\n'
#' chat show [MESSAGES]......show chat messages\n' #' chat show [MESSAGES]...........show chat messages\n'
' chat delete SEQS..........delete chat messages\n' ' chat delete SEQS...............delete chat messages\n'
' chat add USER NONCE COMMENT....add chat message\n'
'Definitions:\n' 'Definitions:\n'
#' MESSAGES..................undefined\n' #' MESSAGES...................undefined\n'
' SEQS......................=SEQ [SEQ...]\n' ' SEQS.......................=SEQ [SEQ...]\n'
' SEQ.......................a chat message\'s seq, base-10 integer\n' ' SEQ........................a chat message\'s seq, base-10 integer\n'
' USER.......................={token TOKEN | hash HASH}\n'
' TOKEN......................a user\'s token, json string\n'
' HASH.......................a user\'s token hash\n'
' NONCE......................a chat message\'s nonce, json string\n'
' COMMENT....................json string\n'
) )
return normal, response return normal, response
@ -48,8 +56,24 @@ async def cmd_chat_delete(*seqs):
response = '' response = ''
return normal, response return normal, response
async def cmd_chat_add(user, nonce, comment):
try:
seq = add_chat_message(user, nonce, comment)
except Rejected as e:
raise CommandFailed(f'rejected: {e}') from e
else:
assert seq is not None
normal = [
'chat', 'add',
'token', json_dumps_contiguous(user['token']),
json_dumps_contiguous(nonce), json_dumps_contiguous(comment),
]
response = str(seq) + '\n'
return normal, response
SPEC = Str({ SPEC = Str({
None: End(cmd_chat_help), None: End(cmd_chat_help),
'help': End(cmd_chat_help), 'help': End(cmd_chat_help),
'delete': ArgsSeqs(End(cmd_chat_delete)), 'delete': ArgsSeqs(End(cmd_chat_delete)),
'add': ArgsUser(ArgsJsonString(ArgsJsonString(End(cmd_chat_add)))),
}) })

ファイルの表示

@ -21,6 +21,7 @@ async def cmd_help():
' user add VERIFIED TOKEN........add new user\n' ' user add VERIFIED TOKEN........add new user\n'
#' chat show MESSAGES.............show a list of messages\n' #' chat show MESSAGES.............show a list of messages\n'
' chat delete SEQS...............delete a set of messages\n' ' chat delete SEQS...............delete a set of messages\n'
' chat add USER NONCE COMMENT....add chat message\n'
' allowedness [show].............show the current allowedness\n' ' allowedness [show].............show the current allowedness\n'
' allowedness setdefault BOOLEAN.set the default allowedness\n' ' allowedness setdefault BOOLEAN.set the default allowedness\n'
' allowedness add SET STRING.....add to the blacklist/whitelist\n' ' allowedness add SET STRING.....add to the blacklist/whitelist\n'