120 lines
2.5 KiB
Go
120 lines
2.5 KiB
Go
|
package dto
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
)
|
||
|
|
||
|
type PayloadAction uint8
|
||
|
|
||
|
const (
|
||
|
PayloadActionNext = iota
|
||
|
PayloadActionPrevious
|
||
|
PayloadActionAddMember
|
||
|
PayloadActionRemoveMember
|
||
|
PayloadActionYesNo
|
||
|
PayloadActionVote
|
||
|
)
|
||
|
|
||
|
type QuestionType uint8
|
||
|
|
||
|
const (
|
||
|
QuestionTypeChatMembers QuestionType = iota
|
||
|
QuestionTypeRedmine
|
||
|
QuestionTypeRedmineHours
|
||
|
)
|
||
|
|
||
|
type Payload struct {
|
||
|
User uint64 `json:"u,omitempty"`
|
||
|
Chat uint64 `json:"c,omitempty"`
|
||
|
Action PayloadAction `json:"a"`
|
||
|
Data json.RawMessage `json:"d,omitempty"`
|
||
|
}
|
||
|
|
||
|
type Member struct {
|
||
|
ID uint64
|
||
|
Name string
|
||
|
}
|
||
|
|
||
|
type Vote struct {
|
||
|
Vote float32 `json:"v"`
|
||
|
}
|
||
|
|
||
|
type Answer struct {
|
||
|
Type QuestionType `json:"t"`
|
||
|
Result bool `json:"r"`
|
||
|
}
|
||
|
|
||
|
func NewNextPageMembersPayload(userID, chatID uint64) *Payload {
|
||
|
return &Payload{
|
||
|
Action: PayloadActionNext,
|
||
|
User: userID,
|
||
|
Chat: chatID,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NewPrevPageMembersPayload(userID, chatID uint64) *Payload {
|
||
|
return &Payload{
|
||
|
Action: PayloadActionPrevious,
|
||
|
User: userID,
|
||
|
Chat: chatID,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NewAddMemberPayload(userID, chatID, memberID uint64, memberName string) *Payload {
|
||
|
return &Payload{
|
||
|
Action: PayloadActionAddMember,
|
||
|
User: userID,
|
||
|
Chat: chatID,
|
||
|
Data: marshal(Member{ID: memberID, Name: memberName}),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NewRemoveMemberPayload(userID, chatID, memberID uint64, memberName string) *Payload {
|
||
|
return &Payload{
|
||
|
Action: PayloadActionRemoveMember,
|
||
|
User: userID,
|
||
|
Chat: chatID,
|
||
|
Data: marshal(Member{ID: memberID, Name: memberName}),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NewVotePayload(chatID uint64, vote float32) *Payload {
|
||
|
return &Payload{
|
||
|
Action: PayloadActionVote,
|
||
|
Chat: chatID,
|
||
|
Data: marshal(Vote{Vote: vote}),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NewConfirmMembersPayload(userID uint64, chatID uint64) *Payload {
|
||
|
return &Payload{
|
||
|
Action: PayloadActionYesNo,
|
||
|
User: userID,
|
||
|
Chat: chatID,
|
||
|
Data: marshal(Answer{Type: QuestionTypeChatMembers, Result: true}),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NewRedmineQuestionPayload(userID uint64, chatID uint64, isYes bool) *Payload {
|
||
|
return &Payload{
|
||
|
Action: PayloadActionYesNo,
|
||
|
User: userID,
|
||
|
Chat: chatID,
|
||
|
Data: marshal(Answer{Type: QuestionTypeRedmine, Result: isYes}),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func NewRedmineHoursQuestionPayload(userID uint64, chatID uint64, isYes bool) *Payload {
|
||
|
return &Payload{
|
||
|
Action: PayloadActionYesNo,
|
||
|
User: userID,
|
||
|
Chat: chatID,
|
||
|
Data: marshal(Answer{Type: QuestionTypeRedmineHours, Result: isYes}),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func marshal(val interface{}) json.RawMessage {
|
||
|
data, _ := json.Marshal(val)
|
||
|
return data
|
||
|
}
|