/
device.go
144 lines (130 loc) · 3.88 KB
/
device.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package dao
import (
"context"
"go.uber.org/zap"
"github.com/ZNotify/server/app/db/ent/generate"
"github.com/ZNotify/server/app/db/ent/generate/device"
"github.com/ZNotify/server/app/global"
"github.com/ZNotify/server/app/manager/push/enum"
)
type deviceDao struct{}
var Device = deviceDao{}
// EnsureDevice ensures that a device exists for the given user and identifier.
func (dd deviceDao) EnsureDevice(
ctx context.Context,
identifier string,
channel enum.Sender,
channelToken string,
deviceName string,
deviceMeta string,
u *generate.User) (d *generate.Device, isChannelChange bool, oldDevice *generate.Device, ok bool) {
oldDevice, ok = dd.GetUserDeviceByIdentifier(ctx, u, identifier)
if ok {
// Update device
_, err := global.App.DB.Device.UpdateOne(oldDevice).
SetIdentifier(identifier).
SetChannel(channel).
SetChannelToken(channelToken).
SetDeviceName(deviceName).
SetDeviceMeta(deviceMeta).
SetUser(u).
Save(ctx)
if err != nil {
zap.S().Errorw("failed to update device", "err", err)
return nil, false, oldDevice, false
}
if oldDevice.Channel != channel {
return oldDevice, true, oldDevice, true
} else {
return oldDevice, false, nil, true
}
} else {
// Create device
d, err := global.App.DB.Device.Create().
SetIdentifier(identifier).
SetChannel(channel).
SetChannelToken(channelToken).
SetDeviceName(deviceName).
SetDeviceMeta(deviceMeta).
SetChannelMeta("").
SetUser(u).
Save(ctx)
if err != nil {
zap.S().Errorw("failed to create device", "err", err)
return nil, false, nil, false
}
return d, false, nil, true
}
}
func (deviceDao) UpdateDeviceChannelMeta(ctx context.Context, d *generate.Device, channelMeta string) bool {
_, err := global.App.DB.Device.UpdateOne(d).SetChannelMeta(channelMeta).Save(ctx)
if err != nil {
zap.S().Errorw("failed to update device channel meta", "err", err)
return false
}
return true
}
func (deviceDao) GetDeviceByIdentifier(ctx context.Context, identifier string) (*generate.Device, bool) {
d, err := global.App.DB.Device.
Query().
Where(device.Identifier(identifier)).
Only(ctx)
if err != nil {
if !generate.IsNotFound(err) {
zap.S().Errorw("failed to get device by identifier", "err")
}
return nil, false
}
return d, true
}
func (deviceDao) GetUserDevices(ctx context.Context, u *generate.User) ([]*generate.Device, bool) {
ds, err := u.QueryDevices().All(ctx)
if err != nil {
zap.S().Errorw("failed to get user devices", "err", err)
return nil, false
}
return ds, true
}
func (deviceDao) GetUserDeviceByIdentifier(ctx context.Context, u *generate.User, identifier string) (*generate.Device, bool) {
d, err := u.QueryDevices().Where(device.Identifier(identifier)).WithUser().Only(ctx)
if err != nil {
if !generate.IsNotFound(err) {
zap.S().Errorw("failed to get user device by identifier", "err", err)
}
return nil, false
}
return d, true
}
func (deviceDao) GetUserDeviceChannelTokens(ctx context.Context, u *generate.User, channel enum.Sender) ([]string, bool) {
devices := make([]string, 0)
err := u.
QueryDevices().
Where(device.Channel(channel)).
Select(device.FieldChannelToken).
Scan(ctx, &devices)
if err != nil {
zap.S().Errorw("failed to get user device channel tokens", "err", err)
return nil, false
}
return devices, true
}
func (deviceDao) DeleteDeviceByIdentifier(ctx context.Context, identifier string) bool {
_, err := global.App.DB.Device.Delete().Where(device.Identifier(identifier)).Exec(ctx)
if err != nil {
if !generate.IsNotFound(err) {
zap.S().Errorw("failed to delete device by identifier", "err", err)
}
return false
}
return true
}
func (deviceDao) DeleteDevice(ctx context.Context, d *generate.Device) bool {
err := global.App.DB.Device.DeleteOne(d).Exec(ctx)
if err != nil {
if !generate.IsNotFound(err) {
zap.S().Errorw("failed to delete device", "err", err)
}
return false
}
return true
}