This repository has been archived by the owner on Oct 6, 2018. It is now read-only.
/
users.go
125 lines (106 loc) · 2.83 KB
/
users.go
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
// Copyright © 2018 MTRNord <info@nordgedanken.de>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sqlite
import (
"github.com/Nordgedanken/Morpheusv2/pkg/matrix"
"github.com/Nordgedanken/Morpheusv2/pkg/matrix/users"
"github.com/matrix-org/gomatrix"
"strings"
)
// SaveUser saves a matrix User to the User table
func (s *SQLite) SaveUser(user matrix.User) error {
if s.db == nil {
s.db = s.Open()
}
tx, err := s.db.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare("INSERT INTO users (id, display_name, avatar) VALUES (?, ?, ?)")
if err != nil {
return err
}
defer stmt.Close()
displayName, err := user.GetDisplayName("")
if err != nil {
return err
}
avatar, err := user.GetAvatar("")
if err != nil {
return err
}
mxid := user.GetMXID()
_, err = stmt.Exec(mxid, displayName, string(avatar))
if err != nil {
return err
}
return tx.Commit()
}
// GetCurrentUser returns the current user including a ready gomatrix client
func (s *SQLite) GetCurrentUser() (userR matrix.User, err error) {
if s.db == nil {
s.db = s.Open()
}
stmt, err := s.db.Prepare(`SELECT id, display_name, avatar, access_token FROM users WHERE own=1`)
if err != nil {
return
}
defer stmt.Close()
row := stmt.QueryRow()
var mxid string
var displayName string
var avatar string
var accessToken string
err = row.Scan(&mxid, &displayName, &avatar, &accessToken)
if err != nil {
return
}
userI := &users.User{}
userI.SetMXID(mxid)
userI.SetDisplayName("", displayName)
userI.SetAvatar("", []byte(avatar))
splitUser := strings.Split(mxid, ":")
domain := strings.TrimPrefix(mxid, splitUser[0]+":")
client, err := gomatrix.NewClient(domain, mxid, accessToken)
if err != nil {
return
}
userI.SetCli(client)
userR = userI
return
}
// GetUser returns a user matching the mxid of the argument
func (s *SQLite) GetUser(userID string) (userR matrix.User, err error) {
if s.db == nil {
s.db = s.Open()
}
stmt, err := s.db.Prepare(`SELECT display_name, avatar FROM users WHERE id=$1`)
if err != nil {
return
}
defer stmt.Close()
row := stmt.QueryRow(userID)
var displayName string
var avatar string
err = row.Scan(&displayName, &avatar)
if err != nil {
return
}
userI := &users.User{}
userI.SetMXID(userID)
userI.SetDisplayName("", displayName)
userI.SetAvatar("", []byte(avatar))
userR = userI
return
}