Skip to content

Commit

Permalink
chore: update example apps (#105)
Browse files Browse the repository at this point in the history
* chore: update example apps

* deps: update example project deps

* feat: finish social-network example updates
  • Loading branch information
zacharygolba committed May 19, 2016
1 parent 824caab commit a03cde5
Show file tree
Hide file tree
Showing 60 changed files with 789 additions and 322 deletions.
3 changes: 3 additions & 0 deletions examples/social-network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

## Running / Development

* `lux db:reset`
* `lux db:migrate`
* `lux db:seed`
* `lux serve`

## Testing
Expand Down
5 changes: 1 addition & 4 deletions examples/social-network/app/controllers/actions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Controller } from 'lux-framework';

class ActionsController extends Controller {
params = [
'trackableId',
'trackableType'
];

}

export default ActionsController;
3 changes: 1 addition & 2 deletions examples/social-network/app/controllers/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import setUser from '../middleware/set-user';
class CommentsController extends Controller {
params = [
'message',
'commentableId',
'commentableType'
'edited'
];

beforeAction = [
Expand Down
4 changes: 1 addition & 3 deletions examples/social-network/app/controllers/friendships.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Controller } from 'lux-framework';

class FriendshipsController extends Controller {
params = [
'userId'
];

}

export default FriendshipsController;
4 changes: 2 additions & 2 deletions examples/social-network/app/controllers/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import setUser from '../middleware/set-user';
class PostsController extends Controller {
params = [
'body',
'isPublic',
'userId'
'title',
'isPublic'
];

beforeAction = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { Controller } from 'lux-framework';

import setUser from '../middleware/set-user';

class LikesController extends Controller {
class ReactionsController extends Controller {
params = [
'likeableId',
'likeableType'
'type'
];

beforeAction = [
setUser
];
}

export default LikesController;
export default ReactionsController;
33 changes: 26 additions & 7 deletions examples/social-network/app/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Controller, action } from 'lux-framework';

import User from '../models/user';

class UsersController extends Controller {
params = [
'name',
Expand All @@ -9,22 +11,39 @@ class UsersController extends Controller {

@action
async login(req, res) {
const { store } = this;
const { params, session } = req;
const { email, password } = params;
const user = await store.modelFor('user').authenticate(email, password);
const {
session,

params: {
data: {
attributes: {
email,
password
}
}
}
} = req;

const user = await User.authenticate(email, password);

if (user) {
session.set('currentUserId', user.id);
}

return user;
return {
data: user
};
}

@action
logout(req, res) {
const { session } = req;
const { isAuthenticated } = session;
const {
session,

session: {
isAuthenticated
}
} = req;

if (isAuthenticated) {
session.delete('currentUserId');
Expand Down
23 changes: 12 additions & 11 deletions examples/social-network/app/middleware/authenticate.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
export default async function authenticate(req, res) {
const { modelName } = this;
const { url, method, record, session } = req;
const currentUserId = session.get('currentUserId');
let authenticated = true;

switch (this.modelName) {
case 'actions':
case 'comments':
case 'posts':
case 'likes':
switch (modelName) {
case 'action':
case 'comment':
case 'post':
case 'reaction':
switch (method) {
case 'DELETE':
case 'PATCH':
authenticated = record.user && record.user.id === currentUserId;
authenticated = record.userId === currentUserId;
break;

case 'POST':
Expand All @@ -20,7 +21,7 @@ export default async function authenticate(req, res) {
}
break;

case 'friendships':
case 'friendship':
switch (method) {
case 'DELETE':
case 'PATCH':
Expand All @@ -30,18 +31,18 @@ export default async function authenticate(req, res) {
}
break;

case 'users':
case 'user':
if (/^(DELETE|PATCH)$/g.test(method)) {
authenticated = url.pathname.includes('logout') ||
currentUserId === record.id;
}
break;

case 'notifications':
case 'notification':
if (record) {
authenticated = record.user && record.user.id === currentUserId;
authenticated = record.userId === currentUserId;
} else {
authenticated = !!currentUserId;
authenticated = false;
}
break;
}
Expand Down
17 changes: 12 additions & 5 deletions examples/social-network/app/middleware/set-user.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import User from '../models/user';

export default async function setUser(req, res) {
const { method } = req;

if (/^(PUT|POST)$/g.test(method)) {
const { session } = req;
const { attributes } = req.params.data;
const {
session,

if (attributes) {
const userId = session.get('currentUserId');
params: {
data: {
attributes
}
}
} = req;

if (attributes) {
req.params.data.attributes = {
...attributes,
user: await this.store.findRecord('user', userId)
user: await User.find(session.get('currentUserId'))
};
}
}
Expand Down
92 changes: 79 additions & 13 deletions examples/social-network/app/models/action.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,89 @@
import { Model } from 'lux-framework';

import Comment from './comment';
import Notification from './notification';
import Post from './post';
import Reaction from './reaction';
import User from './user';

/* TODO: Add support for polymorphic relationship to a 'trackable'.
* https://github.com/postlight/lux/issues/75
*/
class Action extends Model {
static attributes = {
trackableId: {
type: 'integer',
size: 4
},

trackableType: {
type: 'text'
static hooks = {
async afterCreate(action) {
await action.notifyOwner();
}
};

static hasOne = {
user: {
model: 'user',
reverse: 'actions'
async notifyOwner() {
const { trackableId, trackableType } = this;
let params;

if (trackableType === 'Comment') {
const {
postId,
userId
} = await Comment.find(trackableId, {
select: ['postId', 'userId']
});

const [
{ name: userName },
{ userId: recipientId }
] = await Promise.all([
User.find(userId, { select: ['name'] }),
Post.find(postId, { select: ['userId'] })
]);

params = {
recipientId,
message: `${userName} commented on your post!`
};
} else if (trackableType === 'Reaction') {
let reactableId;
let reactableModel = Post;

const {
commentId,
postId,
type,
userId
} = await Reaction.find(trackableId, {
select: [
'commentId',
'postId',
'type',
'userId'
]
});

if (!postId) {
reactableId = commentId;
reactableModel = Comment;
} else {
reactableId = postId;
}

const [
{ name: userName },
{ userId: recipientId }
] = await Promise.all([
User.find(userId, { select: ['name'] }),
reactableModel.find(reactableId, { select: ['userId'] })
]);

params = {
recipientId,
message: `${userName} reacted with ${type} to your ` +
`${reactableModel.name.toLowerCase()}!`
};
}
};

if (params) {
return await Notification.create(params);
}
}
}

export default Action;
33 changes: 15 additions & 18 deletions examples/social-network/app/models/comment.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
import { Model } from 'lux-framework';

class Comment extends Model {
static attributes = {
edited: {
type: 'boolean',
defaultValue: false
},
import track from '../utils/track';

message: {
type: 'text'
class Comment extends Model {
static belongsTo = {
post: {
inverse: 'comments'
},

commentableId: {
type: 'integer',
size: 4
},
user: {
inverse: 'comments'
}
};

commentableType: {
type: 'text'
static hasMany = {
reactions: {
inverse: 'comment'
}
};

static hasOne = {
user: {
model: 'user',
reverse: 'comments'
static hooks = {
async afterCreate(comment) {
await track(comment);
}
};
}
Expand Down
3 changes: 3 additions & 0 deletions examples/social-network/app/models/friendship.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Model } from 'lux-framework';

/* TODO: Add support for self-join on users through a join table.
* https://github.com/postlight/lux/issues/76
*/
class Friendship extends Model {

}
Expand Down
23 changes: 0 additions & 23 deletions examples/social-network/app/models/like.js

This file was deleted.

Loading

0 comments on commit a03cde5

Please sign in to comment.