Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

De-hardcode bot index #202

Merged
merged 4 commits into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion app/controllers/api/bots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,23 @@ def bot_api_json(bot)
id: bot.id,
name: bot.name,
published: bot.published?,
channel_setup: bot.channels.first.setup?,
channel_setup: bot.channels.any?{|c| c.setup? },
uuid: bot.uuid,
channels: bot.channels.select{|c| c.setup? }.map{|c| c.name.capitalize },
updated_at: [
bot.updated_at,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's only use bot.updated_at for now. I've opened instedd/aida#253 in order to properly address this behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 15aa773. Thanks, @NEKRON!

bot.channels.any? ? bot.channels.maximum(:updated_at) : Time.at(0),
bot.behaviours.any? ? bot.behaviours.maximum(:updated_at) : Time.at(0),
bot.translations.any? ? bot.translations.maximum(:updated_at) : Time.at(0),
bot.variable_assignments.any? ? bot.variable_assignments.maximum(:updated_at) : Time.at(0),
bot.data_tables.any? ? bot.data_tables.maximum(:updated_at) : Time.at(0),
bot.collaborators.any? ? bot.collaborators.maximum(:updated_at) : Time.at(0),
bot.invitations.any? ? bot.invitations.maximum(:updated_at) : Time.at(0),
bot.sessions.any? ? bot.sessions.maximum(:updated_at) : Time.at(0),
bot.users.any? ? bot.users.maximum(:updated_at) : Time.at(0),
bot.notifications.any? ? bot.notifications.maximum(:updated_at) : Time.at(0)
].max,
active_users: bot.published? ? GatherBotStats.run(bot, 'this_month')[:active_users] : 0,
permissions: bot_permissions(bot),
collaborator_id: bot.collaborators.find { |c| c.user_id == current_user.id }.try(:id)
}
Expand Down
15 changes: 12 additions & 3 deletions app/javascript/components/BotIndex.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,23 @@ export class BotIndexComponent extends Component {
)
} else {
const title = botList.length == 1 ? '1 bot' : `${botList.length} bots`

const commaSeparatedChannels = channels => (
channels.sort().map((channel, ix) => (
ix > 0 ? `, ${channel}` : channel
))
)

const dateOptions = { year: 'numeric', month: 'long', day: 'numeric' }

content = (
<MainWhite>
<Listing items={botList} title={title}
onItemClick={b => history.push(routes.bot(b.id))}>
<Column title="Name" render={b => b.name} />
<Column title="Type" render={b => "Facebook"} />
<Column title="Uses" render={b => null} />
<Column title="Last activity date" render={d => null} />
<Column title="Type" render={b => commaSeparatedChannels(b.channels)} />
<Column title="Uses" render={b => b.active_users} />
<Column title="Last activity date" render={b => new Date(b.updated_at).toLocaleDateString('en-US', dateOptions)} />
</Listing>
</MainWhite>
)
Expand Down
3 changes: 3 additions & 0 deletions app/javascript/utils/types-generated-decl.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ export type Bot = {
name: string;
published: boolean;
channel_setup: boolean;
channels?: Array<string>;
uuid?: string | null;
updated_at?: string;
active_users?: number;
permissions: Permissions;
collaborator_id?: number | null;
};
Expand Down
8 changes: 8 additions & 0 deletions app/schemas/types.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,20 @@
"name": { "type": "string" },
"published": { "type": "boolean" },
"channel_setup": { "type": "boolean" },
"channels": {
"type": "array",
"items": {
"type": "string"
}
},
"uuid": {
"oneOf": [
{ "type": "string" },
{ "type": "null" }
]
},
"updated_at": { "type": "string" },
"active_users": {"type": "integer" },
"permissions": { "$ref": "#/definitions/permissions" },
"collaborator_id": {
"oneOf": [
Expand Down
2 changes: 2 additions & 0 deletions spec/controllers/api/bots_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

describe "index" do
it "returns a list of bots" do
allow(GatherBotStats).to receive(:run) { { :active_users => 0 } }
get :index

expect(response).to be_success
Expand All @@ -20,6 +21,7 @@
it "lists only the user accessible bots" do
shared_bot = create(:bot, shared_with: user)

allow(GatherBotStats).to receive(:run) { { :active_users => 0 } }
get :index

bot_ids = json_pluck(json_body, :id)
Expand Down