-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannels.js
executable file
·83 lines (71 loc) · 1.92 KB
/
channels.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Copyright (C) 2012 Marco Jahn and Remko Plantenga
*/
var stories = require('./stories.js'),
channels = {};
Channel = function (name) {
this.name = name;
this.users = {};
this.stories = [];
};
Channel.prototype.join = function (username) {
this.users[username] = username;
};
Channel.prototype.getName = function () {
return this.name;
};
Channel.prototype.createStory = function (task, description) {
var story = stories.create(task, description);
this.stories.push(story);
return story;
};
Channel.prototype.getStoryById = function (id) {
var i;
for (i = this.stories.length - 1; i > -1; i--) {
if (this.stories[i].id == id) {
return this.stories[i];
}
}
}
Channel.prototype.vote = function (id, points, username) {
var story = this.users[username] ? this.getStoryById(id) : false;
if (story) {
story.vote(points, username);
}
return !!story;
};
Channel.prototype.listVotes = function (id) {
var votes, user,
story = this.getStoryById(id);
if (story) {
votes = story.listVotes();
for (user in this.users) {
if (!votes[user]) {
return;
}
}
return votes;
}
}
//
exports.create = function (name) {
channels[name] = new Channel(name);
};
exports.list = function () {
return channels;
};
exports.join = function (name, username) {
channels[name].join(username);
};
exports.createStory = function (name, task, description) {
return channels[name].createStory(task, description);
};
exports.listStories = function (name) {
return channels[name].stories;
};
exports.vote = function (name, id, points, username) {
return channels[name].vote(id, points, username);
}
exports.listVotes = function (name, id) {
return channels[name].listVotes(id);
}