-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlists.go
268 lines (241 loc) · 8.59 KB
/
playlists.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package models
import (
"fmt"
u "github.com/PierreBougon/Bym-BackEnd/app/utils"
"github.com/jinzhu/gorm"
)
type Playlist struct {
Model
Name string `json:"name"`
UserId uint `json:"user_id"`
SongsNumber int `json:"songs_number"`
Songs []Song `gorm:"ForeignKey:PlaylistId"`
Follower []*Account `gorm:"many2many:account_playlist;"`
FollowerCount int `json:"follower_count"`
Acl []PlaylistAccessControl `gorm:"ForeignKey:PlaylistId"`
}
type PlaylistOwned struct {
Playlist
Owned bool `json:"owned"`
}
func (playlist *Playlist) Validate() (map[string]interface{}, bool) {
if len(playlist.Name) < 3 {
return u.Message(false, "Playlist name must be at least 3 characters long"), false
}
if playlist.UserId <= 0 {
return u.Message(false, "Invalid user"), false
}
return u.Message(false, "Requirement passed"), true
}
func (playlist *Playlist) Create(user uint) map[string]interface{} {
playlist.UserId = user
playlist.SongsNumber = 0
if resp, ok := playlist.Validate(); !ok {
return resp
}
GetDB().Create(playlist)
if playlist.ID <= 0 {
return u.Message(false, "Failed to create playlist, connection error.")
}
response := u.Message(true, "Playlist has been created")
response["playlist"] = playlist
return response
}
// TODO(variable POST optionel pour choisir le ROLE de l'user (BYMER ou FOllOWER), BYMER by default)
func (playlist *Playlist) Join(user uint, playlistId uint) map[string]interface{} {
account := &Account{}
retPlaylist := &Playlist{}
err := GetDB().Table("playlists").Where("id = ?", playlistId).Find(&retPlaylist).Error
if err != nil {
return u.Message(false, "This playlist does not exist")
}
if retPlaylist.UserId == user {
return u.Message(false, "Author does not need to follow his playlist")
}
res := make([]*Account, 0)
GetDB().Model(retPlaylist).Association("Follower").Find(&res)
for _, follower := range res {
if follower.ID == user {
return u.Message(false, "User already joined the playlist")
}
}
GetDB().Table("accounts").Where("id = ?", user).Find(&account)
GetDB().Model(retPlaylist).Association("Follower").Append(account)
GetDB().Model(retPlaylist).UpdateColumn("follower_count", gorm.Expr("follower_count + ?", 1))
// TODO(do the append only if Bymer ROLE is asked in the post parameter)
GetDB().Model(retPlaylist).Association("Acl").Append(&PlaylistAccessControl{
UserId: user,
PlaylistId: retPlaylist.ID,
RoleId: ROLE_BYMER,
})
return u.Message(true, "User has joined the playlist")
}
/*
TODO(Cas où l'utilisateur dont l'acl est changé n'appartient pas à la playlist ????)
TODO(Cas où le front ne peut envoyer qu'un email et pas un id)
TODO(Roles sont rentré à la main dans la db, dans l'ordre croissant d'authorité (1 > 4))
TODO(Le front peut-il directement utilisé ces 'valeurs' d'autorité en dure, sans avoir besoin de les fetch dans la db ?)
*/
func ChangeAclOnPlaylist(user uint, userToPromote uint, playlistId uint, role uint) map[string]interface{} {
var (
userAcl uint
retPlaylist Playlist
oldAcl PlaylistAccessControl
)
db.Table("playlist_access_controls").
Where(&PlaylistAccessControl{UserId: user, PlaylistId: playlistId}).Pluck("role", userAcl)
db.Table("playlists").Where("id = ?", playlistId).Find(&retPlaylist)
if userAcl != ROLE_ADMIN && retPlaylist.UserId != user {
return u.Message(false, "User has no right upon the members of this playlist")
}
// At this point, if userAcl == 0 then he is Author
exist := !db.Table("playlist_access_controls").
Where("user_id = ? AND playlist_id = ?", userToPromote, playlistId).Find(&oldAcl).
RecordNotFound()
if oldAcl.RoleId == role {
return u.Message(false, "This user already have this role")
}
if oldAcl.RoleId == ROLE_ADMIN && role > ROLE_ADMIN && userAcl != 0 {
return u.Message(false, "To demote an Admin needs Author rights")
}
if exist {
db.Table("playlist_access_controls").
Where("playlist_id = ? AND user_id = ?", playlistId, userToPromote).
UpdateColumn("role_id", role)
} else {
return u.Message(false, "Can not find this user")
}
return u.Message(true, "New role successfully given")
}
func (playlist *Playlist) LeavePlaylist(user uint, playlistId uint) map[string]interface{} {
retPlaylist := &Playlist{}
err := db.Where("id = ?", playlistId).First(&retPlaylist, playlistId).Error
if err != nil {
return u.Message(false, "Playlist does not exist")
}
accounts := make([]*Account, 0)
GetDB().Model(retPlaylist).Association("Follower").Find(&accounts)
isFollowed := false
for _, account := range accounts {
if account.ID == user {
isFollowed = true
}
}
if !isFollowed {
return u.Message(false, "Playlist is not followed by user")
}
account := &Account{}
GetDB().Table("accounts").Find(&account, "id = ?", user)
GetDB().Model(retPlaylist).Association("Follower").Delete(&account)
GetDB().Model(retPlaylist).UpdateColumn("follower_count", gorm.Expr("follower_count - ?", 1))
return u.Message(true, "Playlist successfully left")
}
type PlaylistFilter struct {
ShowSongs bool
ShowFollower bool
ShowAcl bool
}
func GetPlaylistById(u uint, filter *PlaylistFilter) *Playlist {
retPlaylist := &Playlist{}
if filter == nil {
filter = &PlaylistFilter{}
}
req := GetDB()
if filter.ShowSongs {
req = req.Preload("Songs")
}
if filter.ShowFollower {
req = req.Preload("Follower")
}
if filter.ShowAcl {
req = req.Preload("Acl")
}
req.Table("playlists").Where("id = ?", u).First(retPlaylist)
if retPlaylist.Name == "" {
return nil
}
return retPlaylist
}
func checkIfOwned(playlist []*Playlist, user uint) []*PlaylistOwned {
owned := make([]*PlaylistOwned, 0)
for k := 0; k < len(playlist); k++ {
owned = append(owned, &PlaylistOwned{
Playlist: *playlist[k],
Owned: playlist[k].UserId == user,
})
}
return owned
}
func GetPlaylistsByUser(user uint) []*PlaylistOwned {
playlists := make([]*Playlist, 0)
err := GetDB().Table("playlists").Where("user_id = ?", user).Find(&playlists).Error
if err != nil {
fmt.Println(err)
return nil
}
User := &Account{}
GetDB().Table("accounts").Find(User, user)
joined := make([]*Playlist, 0)
err = GetDB().Model(User).Related(&joined, "FollowedPlaylists").Error
if err != nil {
fmt.Println(err)
return nil
}
return checkIfOwned(append(playlists, joined...), user)
}
func (playlist *Playlist) UpdatePlaylist(user uint, playlistId uint, newPlaylist *Playlist) map[string]interface{} {
retPlaylist := &Playlist{}
err := db.Where(&Playlist{UserId: user}).First(&retPlaylist, playlistId).Error
if err != nil {
return u.Message(false, "Invalid playlist, you may not own this playlist")
}
retPlaylist.Name = newPlaylist.Name
db.Save(&retPlaylist)
return u.Message(true, "Playlist successfully updated")
}
func updatePlaylistSoundCount(user uint, playlistId uint, countModifier int) map[string]interface{} {
retPlaylist := &Playlist{}
err := db.Where(&Playlist{UserId: user}).First(&retPlaylist, playlistId).Error
if err != nil {
return u.Message(false, "Invalid playlist, you may not own this playlist")
}
retPlaylist.SongsNumber += countModifier
db.Save(&retPlaylist)
return u.Message(true, "Playlist successfully updated")
}
func (playlist *Playlist) DeletePlaylist(user uint, playlistId uint, notifyOnDelete func(userId uint, playlistId uint, message string), messageOnDelete string) map[string]interface{} {
retPlaylist := &Playlist{}
err := db.Where(&Playlist{UserId: user}).First(&retPlaylist, playlistId).Error
if err != nil {
return u.Message(false, "Invalid playlist, you may not own this playlist")
}
notifyOnDelete(user, playlistId, messageOnDelete)
db.Model(retPlaylist).Association("Follower").Clear()
db.Model(retPlaylist).Association("Acl").Clear()
db.Delete(retPlaylist)
return u.Message(true, "Playlist successfully deleted")
}
func GetRole(user uint, playlistId uint) (*Role, string) {
retPlaylist := &Playlist{}
err := GetDB().Table("playlists").Where("id = ?", playlistId).First(&retPlaylist).Error
if err != nil {
return nil, "Invalid playlist, it does not exist"
}
if retPlaylist.UserId == user {
return &Role{Name: RoleName[0], ID: 0}, ""
}
acl := &PlaylistAccessControl{}
notFound := db.Table("playlist_access_controls").Where(PlaylistAccessControl{
UserId: user,
PlaylistId: playlistId,
}).First(acl).RecordNotFound()
if notFound {
return &Role{Name: RoleName[ROLE_VISITOR], ID: ROLE_VISITOR}, ""
}
return &Role{ID: acl.RoleId, Name: RoleName[acl.RoleId]}, ""
}
func GetPlaylistFromSong(song *Song) *Playlist {
playlist := &Playlist{}
db.First(playlist, song.PlaylistId)
return playlist
}