-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmodels.js
More file actions
91 lines (81 loc) · 2.29 KB
/
models.js
File metadata and controls
91 lines (81 loc) · 2.29 KB
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
const AV = require('leanengine')
const { push } = require('./utils')
const ChannelObject = AV.Object.extend('Channel')
const UserToChannelObject = AV.Object.extend('UserToChannel')
class User {
constructor (openId) {
this.openId = openId
}
async login () {
// openId 就是用来识别用户的唯一标识
try {
this.Object = await AV.User.signUpOrlogInWithAuthData({
uid: this.openId
}, 'mp')
} catch (e) {
console.error(e)
}
return this
}
async findChannels () {
let userToChannelQuery = new AV.Query('UserToChannel')
let results = await userToChannelQuery.equalTo('user', this.Object).find()
return results.map(userToChannel => {
return userToChannel.get('channelName')
})
}
async linkChannel (channel) {
if (!channel.exist) {
channel.owner = this
await channel.create()
}
let userToChannelQuery = new AV.Query('UserToChannel')
let result = await userToChannelQuery.equalTo('user', this.Object).equalTo('channel', channel.Object).first()
if (result) return
let userToChannelObject = new UserToChannelObject()
userToChannelObject.set({
user: this.Object,
openId: this.openId,
channel: channel.Object,
channelName: channel.name
})
await userToChannelObject.save()
}
}
class Channel {
constructor (channelName) {
this.name = channelName
this.exist = false
}
async init () {
if (this.Object) return
let channelQuery = new AV.Query('Channel')
let result = await channelQuery.equalTo('name', this.name).first()
if (result) {
this.Object = result
this.exist = true
} else {
let channelObject = new ChannelObject()
this.Object = channelObject
}
}
async create () {
if (this.exist) return
let channelObject = this.Object
channelObject.set({name: this.name, owner: this.owner.Object})
this.Object = await channelObject.save()
this.exist = true
}
async push (text) {
let userToChannelQuery = new AV.Query('UserToChannel')
let results = await userToChannelQuery.equalTo('channel', this.Object).find()
return Promise.all(results.map(userToChannel => {
let openId = userToChannel.get('openId')
return push(openId, text)
}))
}
}
module.exports = {
User,
Channel
}