-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from wizguin/develop
1.5.0-beta
- Loading branch information
Showing
53 changed files
with
3,361 additions
and
1,529 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Sequelize from 'sequelize' | ||
|
||
|
||
export default class BaseModel extends Sequelize.Model { | ||
|
||
protectedAttributes = [] | ||
|
||
toJSON() { | ||
let attributes = this.get() | ||
|
||
for (let attribute of this.protectedAttributes) { | ||
delete attributes[attribute] | ||
} | ||
|
||
return attributes | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
export default class Collection { | ||
|
||
constructor(user, models, model, indexKey) { | ||
this.user = user | ||
this.indexKey = indexKey | ||
|
||
this.db = user.db | ||
this.model = user.db[model] | ||
this.handler = user.handler | ||
|
||
this.collection = {} | ||
|
||
this.collect(models) | ||
} | ||
|
||
collect(models) { | ||
for (let model of models) { | ||
this.collection[model[this.indexKey]] = model | ||
} | ||
} | ||
|
||
add(record) { | ||
this.model.create(record) | ||
.then((model) => { | ||
this.collection[model[this.indexKey]] = model | ||
|
||
}) | ||
.catch((error) => { | ||
this.handler.error(error) | ||
}) | ||
} | ||
|
||
remove(key) { | ||
this.collection[key].destroy() | ||
|
||
delete this.collection[key] | ||
} | ||
|
||
includes(key) { | ||
return key in this.collection | ||
} | ||
|
||
toJSON() { | ||
return Object.keys(this.collection).map(key => parseInt(key)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Collection from '../Collection' | ||
|
||
|
||
export default class BuddyCollection extends Collection { | ||
|
||
constructor(user, models) { | ||
super(user, models, 'buddies', 'buddyId') | ||
|
||
this.usersById = this.handler.usersById | ||
} | ||
|
||
add(id) { | ||
super.add({ userId: this.user.id, buddyId: id }) | ||
} | ||
|
||
isOnline(id) { | ||
return id in this.usersById | ||
} | ||
|
||
sendOnline(id) { | ||
let user = this.usersById[id] | ||
|
||
user.send('buddy_online', { id: this.user.id }) | ||
} | ||
|
||
sendOffline() { | ||
for (let buddy in this.collection) { | ||
if (this.isOnline(buddy)) { | ||
let user = this.usersById[buddy] | ||
|
||
user.send('buddy_offline', { id: this.user.id }) | ||
} | ||
} | ||
} | ||
|
||
toJSON() { | ||
let buddies = [] | ||
|
||
for (let buddy in this.collection) { | ||
let online = this.isOnline(buddy) | ||
let username = this.collection[buddy].user.username | ||
|
||
buddies.push({ id: parseInt(buddy), username: username, online: online }) | ||
|
||
if (online) { | ||
this.sendOnline(buddy) | ||
} | ||
} | ||
|
||
return buddies | ||
} | ||
|
||
} |
Oops, something went wrong.