From 72c7248b6fdcdb2175e726ff51b42e7555f2bd84 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 24 Oct 2017 19:41:09 +0200 Subject: Add video channels --- server/lib/user.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 server/lib/user.ts (limited to 'server/lib/user.ts') diff --git a/server/lib/user.ts b/server/lib/user.ts new file mode 100644 index 000000000..8609e72d8 --- /dev/null +++ b/server/lib/user.ts @@ -0,0 +1,46 @@ +import { database as db } from '../initializers' +import { UserInstance } from '../models' +import { addVideoAuthorToFriends } from './friends' +import { createVideoChannel } from './video-channel' + +function createUserAuthorAndChannel (user: UserInstance, validateUser = true) { + return db.sequelize.transaction(t => { + const userOptions = { + transaction: t, + validate: validateUser + } + + return user.save(userOptions) + .then(user => { + const author = db.Author.build({ + name: user.username, + podId: null, // It is our pod + userId: user.id + }) + + return author.save({ transaction: t }) + .then(author => ({ author, user })) + }) + .then(({ author, user }) => { + const remoteVideoAuthor = author.toAddRemoteJSON() + + // Now we'll add the video channel's meta data to our friends + return addVideoAuthorToFriends(remoteVideoAuthor, t) + .then(() => ({ author, user })) + }) + .then(({ author, user }) => { + const videoChannelInfo = { + name: `Default ${user.username} channel` + } + + return createVideoChannel(videoChannelInfo, author, t) + .then(videoChannel => ({ author, user, videoChannel })) + }) + }) +} + +// --------------------------------------------------------------------------- + +export { + createUserAuthorAndChannel +} -- cgit v1.2.3