Skip to content

Commit

Permalink
feat(provider/user): Allow Uploading/setting of user avatar from with…
Browse files Browse the repository at this point in the history
…in sockbot.

useful for shenanigans
  • Loading branch information
AccaliaDeElementia committed Oct 13, 2017
1 parent 6c70a54 commit 0116708
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
8 changes: 1 addition & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,11 @@ build/Release
node_modules

#scratch ideas files
test.js
test.txt
test.*

#Cloud9 IDE
.c9


#test configuration files
test.json
test.yml

# Webstorm IDE project workspace folder
.idea/

Expand Down
21 changes: 21 additions & 0 deletions providers/nodebb/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,27 @@ exports.bindUser = function bindUser(forum) {
return forum.Chat.create(this.username, message, title);
}

/**
* Upload avatar and set as current avatar
*
* @public
*
* @param {!Buffer} imageData Image File Data to be uploaded
* @param {string} mimeType Mime type of the image data. E.g. 'image/jpeg'
* @returns {Promise<string>} Resolves to the url of the avatar that is uploaded
*
* @promise
* @fulfill {string} The url of the uploaded image
* @reject {Error} An Error that occured while processing
*/
uploadAvatar(imageData, mimeType) {
const data = `data:${mimeType || 'application/octet-stream'};base64,${imageData.toString('base64')}`;
return forum._emit('user.uploadCroppedPicture', {
uid: this.id,
imageData: data
}).then((result) => result.url);
}

/**
* Get User by Id
*
Expand Down
39 changes: 39 additions & 0 deletions test/providers/nodebb/userTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,45 @@ describe('providers/nodebb/user', () => {
});
});
});
describe('uploadAvatar()', () => {
let user = null, buffer, url;
beforeEach(() => {
user = new User({
uid: Math.random()
});
buffer = new Buffer('Hello World!', 'utf8');
url = `/path/to/upload/${Math.random()}`;
forum._emit.resolves({url: url});
});
it('should emit \'user.uploadCroppedPicture\' over websocket', () => {
return user.uploadAvatar(buffer).then(() => {
forum._emit.calledWith('user.uploadCroppedPicture').should.be.true;
});
});
it('should resolve to url of uploaded image', () => {
return user.uploadAvatar(buffer).should.become(url);
});
it('should upload avatar for correct user', () => {
return user.uploadAvatar(buffer).then(() => {
const payload = forum._emit.firstCall.args[1];
payload.uid.should.equal(user.id);
});
});
it('should use default mimetype when not specified', () => {
return user.uploadAvatar(buffer).then(() => {
const payload = forum._emit.firstCall.args[1];
payload.imageData.should.equal(`data:application/octet-stream;base64,${buffer.toString('base64')}`);
});
});
it('should use specified mimetype', () => {
const mime = `MIME${Math.random()}`;
return user.uploadAvatar(buffer, mime).then(() => {
const payload = forum._emit.firstCall.args[1];
payload.imageData.should.equal(`data:${mime};base64,${buffer.toString('base64')}`);
});
});

});
describe('static get()', () => {
it('should load via function `user.getUserByUID`', () => {
const expected = Math.random();
Expand Down

0 comments on commit 0116708

Please sign in to comment.