aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/user/user.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-24 19:41:09 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-26 09:11:38 +0200
commit72c7248b6fdcdb2175e726ff51b42e7555f2bd84 (patch)
tree1bfdee99dbe2392cc997edba8e314e2a8a401c72 /server/models/user/user.ts
parent8113a93a0d9f31aa9e23702bbc31b8a76275ae22 (diff)
downloadPeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.tar.gz
PeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.tar.zst
PeerTube-72c7248b6fdcdb2175e726ff51b42e7555f2bd84.zip
Add video channels
Diffstat (limited to 'server/models/user/user.ts')
-rw-r--r--server/models/user/user.ts69
1 files changed, 55 insertions, 14 deletions
diff --git a/server/models/user/user.ts b/server/models/user/user.ts
index 0dc52d3cf..f8598c40f 100644
--- a/server/models/user/user.ts
+++ b/server/models/user/user.ts
@@ -27,10 +27,10 @@ let toFormattedJSON: UserMethods.ToFormattedJSON
27let isAdmin: UserMethods.IsAdmin 27let isAdmin: UserMethods.IsAdmin
28let countTotal: UserMethods.CountTotal 28let countTotal: UserMethods.CountTotal
29let getByUsername: UserMethods.GetByUsername 29let getByUsername: UserMethods.GetByUsername
30let list: UserMethods.List
31let listForApi: UserMethods.ListForApi 30let listForApi: UserMethods.ListForApi
32let loadById: UserMethods.LoadById 31let loadById: UserMethods.LoadById
33let loadByUsername: UserMethods.LoadByUsername 32let loadByUsername: UserMethods.LoadByUsername
33let loadByUsernameAndPopulateChannels: UserMethods.LoadByUsernameAndPopulateChannels
34let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail 34let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail
35let isAbleToUploadVideo: UserMethods.IsAbleToUploadVideo 35let isAbleToUploadVideo: UserMethods.IsAbleToUploadVideo
36 36
@@ -113,10 +113,10 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da
113 113
114 countTotal, 114 countTotal,
115 getByUsername, 115 getByUsername,
116 list,
117 listForApi, 116 listForApi,
118 loadById, 117 loadById,
119 loadByUsername, 118 loadByUsername,
119 loadByUsernameAndPopulateChannels,
120 loadByUsernameOrEmail 120 loadByUsernameOrEmail
121 ] 121 ]
122 const instanceMethods = [ 122 const instanceMethods = [
@@ -144,15 +144,34 @@ isPasswordMatch = function (this: UserInstance, password: string) {
144} 144}
145 145
146toFormattedJSON = function (this: UserInstance) { 146toFormattedJSON = function (this: UserInstance) {
147 return { 147 const json = {
148 id: this.id, 148 id: this.id,
149 username: this.username, 149 username: this.username,
150 email: this.email, 150 email: this.email,
151 displayNSFW: this.displayNSFW, 151 displayNSFW: this.displayNSFW,
152 role: this.role, 152 role: this.role,
153 videoQuota: this.videoQuota, 153 videoQuota: this.videoQuota,
154 createdAt: this.createdAt 154 createdAt: this.createdAt,
155 author: {
156 id: this.Author.id,
157 uuid: this.Author.uuid
158 }
155 } 159 }
160
161 if (Array.isArray(this.Author.VideoChannels) === true) {
162 const videoChannels = this.Author.VideoChannels
163 .map(c => c.toFormattedJSON())
164 .sort((v1, v2) => {
165 if (v1.createdAt < v2.createdAt) return -1
166 if (v1.createdAt === v2.createdAt) return 0
167
168 return 1
169 })
170
171 json['videoChannels'] = videoChannels
172 }
173
174 return json
156} 175}
157 176
158isAdmin = function (this: UserInstance) { 177isAdmin = function (this: UserInstance) {
@@ -189,21 +208,19 @@ getByUsername = function (username: string) {
189 const query = { 208 const query = {
190 where: { 209 where: {
191 username: username 210 username: username
192 } 211 },
212 include: [ { model: User['sequelize'].models.Author, required: true } ]
193 } 213 }
194 214
195 return User.findOne(query) 215 return User.findOne(query)
196} 216}
197 217
198list = function () {
199 return User.findAll()
200}
201
202listForApi = function (start: number, count: number, sort: string) { 218listForApi = function (start: number, count: number, sort: string) {
203 const query = { 219 const query = {
204 offset: start, 220 offset: start,
205 limit: count, 221 limit: count,
206 order: [ getSort(sort) ] 222 order: [ getSort(sort) ],
223 include: [ { model: User['sequelize'].models.Author, required: true } ]
207 } 224 }
208 225
209 return User.findAndCountAll(query).then(({ rows, count }) => { 226 return User.findAndCountAll(query).then(({ rows, count }) => {
@@ -215,14 +232,36 @@ listForApi = function (start: number, count: number, sort: string) {
215} 232}
216 233
217loadById = function (id: number) { 234loadById = function (id: number) {
218 return User.findById(id) 235 const options = {
236 include: [ { model: User['sequelize'].models.Author, required: true } ]
237 }
238
239 return User.findById(id, options)
219} 240}
220 241
221loadByUsername = function (username: string) { 242loadByUsername = function (username: string) {
222 const query = { 243 const query = {
223 where: { 244 where: {
224 username 245 username
225 } 246 },
247 include: [ { model: User['sequelize'].models.Author, required: true } ]
248 }
249
250 return User.findOne(query)
251}
252
253loadByUsernameAndPopulateChannels = function (username: string) {
254 const query = {
255 where: {
256 username
257 },
258 include: [
259 {
260 model: User['sequelize'].models.Author,
261 required: true,
262 include: [ User['sequelize'].models.VideoChannel ]
263 }
264 ]
226 } 265 }
227 266
228 return User.findOne(query) 267 return User.findOne(query)
@@ -230,6 +269,7 @@ loadByUsername = function (username: string) {
230 269
231loadByUsernameOrEmail = function (username: string, email: string) { 270loadByUsernameOrEmail = function (username: string, email: string) {
232 const query = { 271 const query = {
272 include: [ { model: User['sequelize'].models.Author, required: true } ],
233 where: { 273 where: {
234 $or: [ { username }, { email } ] 274 $or: [ { username }, { email } ]
235 } 275 }
@@ -242,11 +282,12 @@ loadByUsernameOrEmail = function (username: string, email: string) {
242// --------------------------------------------------------------------------- 282// ---------------------------------------------------------------------------
243 283
244function getOriginalVideoFileTotalFromUser (user: UserInstance) { 284function getOriginalVideoFileTotalFromUser (user: UserInstance) {
245 // Don't use sequelize because we need to use a subquery 285 // Don't use sequelize because we need to use a sub query
246 const query = 'SELECT SUM("size") AS "total" FROM ' + 286 const query = 'SELECT SUM("size") AS "total" FROM ' +
247 '(SELECT MAX("VideoFiles"."size") AS "size" FROM "VideoFiles" ' + 287 '(SELECT MAX("VideoFiles"."size") AS "size" FROM "VideoFiles" ' +
248 'INNER JOIN "Videos" ON "VideoFiles"."videoId" = "Videos"."id" ' + 288 'INNER JOIN "Videos" ON "VideoFiles"."videoId" = "Videos"."id" ' +
249 'INNER JOIN "Authors" ON "Videos"."authorId" = "Authors"."id" ' + 289 'INNER JOIN "VideoChannels" ON "VideoChannels"."id" = "Videos"."channelId" ' +
290 'INNER JOIN "Authors" ON "VideoChannels"."authorId" = "Authors"."id" ' +
250 'INNER JOIN "Users" ON "Authors"."userId" = "Users"."id" ' + 291 'INNER JOIN "Users" ON "Authors"."userId" = "Users"."id" ' +
251 'WHERE "Users"."id" = $userId GROUP BY "Videos"."id") t' 292 'WHERE "Users"."id" = $userId GROUP BY "Videos"."id") t'
252 293