summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/gopackage/ddp/messages.go
blob: fc127ceeb02f2ef36eee6c19ad3ebcb5d6a6aeb1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package ddp

import (
	"crypto/sha256"
	"encoding/hex"
	"io"
)

// ------------------------------------------------------------
// DDP Messages
//
// Go structs representing common DDP raw messages ready for JSON
// encoding.
// ------------------------------------------------------------

// Message contains the common fields that all DDP messages use.
type Message struct {
	Type string `json:"msg"`
	ID   string `json:"id,omitempty"`
}

// Connect represents a DDP connect message.
type Connect struct {
	Message
	Version string   `json:"version"`
	Support []string `json:"support"`
	Session string   `json:"session,omitempty"`
}

// NewConnect creates a new connect message
func NewConnect() *Connect {
	return &Connect{Message: Message{Type: "connect"}, Version: "1", Support: []string{"1"}}
}

// NewReconnect creates a new connect message with a session ID to resume.
func NewReconnect(session string) *Connect {
	c := NewConnect()
	c.Session = session
	return c
}

// Ping represents a DDP ping message.
type Ping Message

// NewPing creates a new ping message with optional ID.
func NewPing(id string) *Ping {
	return &Ping{Type: "ping", ID: id}
}

// Pong represents a DDP pong message.
type Pong Message

// NewPong creates a new pong message with optional ID.
func NewPong(id string) *Pong {
	return &Pong{Type: "pong", ID: id}
}

// Method is used to send a remote procedure call to the server.
type Method struct {
	Message
	ServiceMethod string        `json:"method"`
	Args          []interface{} `json:"params"`
}

// NewMethod creates a new method invocation object.
func NewMethod(id, serviceMethod string, args []interface{}) *Method {
	return &Method{
		Message:       Message{Type: "method", ID: id},
		ServiceMethod: serviceMethod,
		Args:          args,
	}
}

// Sub is used to send a subscription request to the server.
type Sub struct {
	Message
	SubName string        `json:"name"`
	Args    []interface{} `json:"params"`
}

// NewSub creates a new sub object.
func NewSub(id, subName string, args []interface{}) *Sub {
	return &Sub{
		Message: Message{Type: "sub", ID: id},
		SubName: subName,
		Args:    args,
	}
}


// Login provides a Meteor.Accounts password login support
type Login struct {
	User     *User     `json:"user"`
	Password *Password `json:"password"`
}

func NewEmailLogin(email, pass string) *Login {
	return &Login{User: &User{Email: email}, Password: NewPassword(pass)}
}

func NewUsernameLogin(user, pass string) *Login {
	return &Login{User: &User{Username: user}, Password: NewPassword(pass)}
}

type LoginResume struct {
	Token string `json:"resume"`
}

func NewLoginResume(token string) *LoginResume {
	return &LoginResume{Token: token}
}

type User struct {
	Email    string `json:"email,omitempty"`
	Username string `json:"username,omitempty"`
}

type Password struct {
	Digest    string `json:"digest"`
	Algorithm string `json:"algorithm"`
}

func NewPassword(pass string) *Password {
	sha := sha256.New()
	io.WriteString(sha, pass)
	digest := sha.Sum(nil)
	return &Password{Digest: hex.EncodeToString(digest), Algorithm: "sha-256"}
}