forked from snap-cloud/snapCloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.lua
140 lines (130 loc) · 4.39 KB
/
models.lua
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
-- Database abstractions
-- =====================
--
-- A cloud backend for Snap!
-- Written by Bernat Romagosa and Michael Ball
--
-- Copyright (C) 2019 by Bernat Romagosa and Michael Ball
--
-- This file is part of Snap Cloud.
--
-- Snap Cloud is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of
-- the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.-
local db = package.loaded.db
local Model = package.loaded.Model
package.loaded.Users = Model:extend('active_users', {
relations = {
{'collections', has_many = 'Collections'}
},
isadmin = function (self)
return self.role == 'admin'
end,
isbanned = function (self)
return self.role == 'banned'
end,
has_one_of_roles = function (self, roles)
for _, role in pairs(roles) do
if self.role == role then
return true
end
end
return false
end,
rollbar_params = function (self)
-- just the info necessary for error tracking
return {id = self.id, username = self.username}
end
})
package.loaded.DeletedUsers = Model:extend('deleted_users')
package.loaded.Projects = Model:extend('active_projects', {
primary_key = {'username', 'projectname'}
})
package.loaded.DeletedProjects = Model:extend('deleted_projects', {
primary_key = {'username', 'projectname'}
})
package.loaded.Tokens = Model:extend('tokens', {
primary_key = {'value'}
})
package.loaded.Remixes = Model:extend('remixes', {
primary_key = {'original_project_id', 'remixed_project_id'}
})
package.loaded.Collections = Model:extend('collections', {
primary_key = {'creator_id', 'name'},
timestamp = true,
relations = {
-- creates Collection:get_creator()
{'creator', belongs_to = 'Users', key = 'creator_id'},
{'memberships', has_many = 'CollectionMemberships'},
{'projects',
fetch = function (self)
local query = db.interpolate_query(
[[ WHERE id IN (
SELECT project_id
FROM collection_memberships
WHERE collection_id = ?
)]],
self.id)
return package.loaded.Projects:paginated(query)
end
},
{'shared_and_published_projects',
fetch = function (self)
local query = db.interpolate_query(
[[ WHERE id IN (
SELECT project_id
FROM collection_memberships
WHERE collection_id = ?
)
AND (ispublished OR ispublic) ]],
self.id)
return package.loaded.Projects:paginated(query)
end
},
{'published_projects',
fetch = function (self)
local query = db.interpolate_query(
[[ WHERE id IN (
SELECT project_id
FROM collection_memberships
WHERE collection_id = ?
)
AND ispublished ]],
self.id)
return package.loaded.Projects:paginated(query)
end
}
},
constraints = {
name = function(self, value)
if not value then
return 'A name must be present'
end
end
},
count_projects = function (self)
return package.loaded.CollectionMemberships:count('collection_id = ?',
self.id)
end
})
package.loaded.CollectionMemberships = Model:extend(
'collection_memberships', {
primary_key = {'collection_id', 'project_id'},
timestamp = true
}
)
package.loaded.BannedIPs = Model:extend(
'banned_ips', {
primary_key = 'ip',
timestamp = true
}
)