Skip to content

Commit

Permalink
Add babble topic author, don't display icon unless topic is available
Browse files Browse the repository at this point in the history
  • Loading branch information
gdpelican committed Aug 2, 2015
1 parent 9cb80d0 commit 11ef644
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 40 deletions.
11 changes: 8 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
- Notify when new post has come in (DONE)
- Handle reading posts when they're on the screen (DONE)
- Write README (DONE)
- Live update reading of posts (need to revisit)
- Live update reading of posts (DONE)
- Errors for non-admin users (DONE)
- Show 'no chat messages yet' message when no posts have been made

## Nice to have / next up
- New messages line (DONE)
- Smileys / emoticons (DONE)
- Generalize Babble::Topic to be able to allow for multiple chat channels. (DONE)
- Prevent 1 unread from showing up when posting a chat message
- Allow switching between chat topics
- Strengthen test coverage
- Deal with fallout from having weird topic (admin sees topic, user post counts, etc.) [need to write some test scenarios for this]
- Generalize Babble::Topic to be able to allow for multiple chat channels.
- Deal with fallout from having weird topic
- Deal with fallout from having weird user
- handle postStream window and loading previous messages
- Editing posts
22 changes: 16 additions & 6 deletions assets/javascripts/discourse/components/babble-icon.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,38 @@ export default Ember.Component.extend({
topic: Ember.computed('topicVersion', function() { return Discourse.Babble && Discourse.Babble.topic }),
unreadCount: Ember.computed('topicVersion', function() {
var topic = this.get('topic')
if (topic) { return topic.unread_count }
else { return 0 }
if (!topic) { return 0 }
return topic.highest_post_number - topic.last_read_post_number
}),

_init: function() {
const self = this
const messageBus = Discourse.__container__.lookup('message-bus:main')

if (!Discourse.Babble) {
Discourse.Babble = {}
Discourse.Babble.refresh = function(data) {
if (!data.id) {
self.set('noTopicAvailable', true)
return
}

var topic = Discourse.Topic.create(data)
var postStream = Discourse.PostStream.create(topic.post_stream)

topic.last_read_post_number = data.last_read_post_number || Discourse.Babble.topic.last_read_post_number
topic.unread_count = data.highest_post_number - topic.last_read_post_number

postStream.posts = topic.post_stream.posts
postStream.topic = topic

Discourse.Babble.topic = topic
Discourse.Babble.postStream = postStream

var updateIfGiven = function(field, data) {
Discourse.Babble.topic[field] = data[field] || Discourse.Babble.topic[field]
}

updateIfGiven('last_read_post_number', data)
updateIfGiven('highest_post_number', data)

self.set('topicVersion', self.get('topicVersion') + 1)
}
}
Expand Down
22 changes: 12 additions & 10 deletions assets/javascripts/discourse/templates/components/babble-icon.hbs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<li class='babble-icon'>
<a class='icon'
href
data-dropdown="babble-dropdown"
id='babble-notifications'
title='{{i18n 'babble.title'}}'>
{{fa-icon "bullhorn" label="babble.title"}}
</a>
{{#if unreadCount}}
<a href class='badge-notification unread-notifications'>{{unreadCount}}</a>
{{/if}}
{{#unless noTopicAvailable}}
<a class='icon'
href
data-dropdown="babble-dropdown"
id='babble-notifications'
title='{{i18n 'babble.title'}}'>
{{fa-icon "bullhorn" label="babble.title"}}
</a>
{{#if unreadCount}}
<a href class='badge-notification unread-notifications'>{{unreadCount}}</a>
{{/if}}
{{/unless}}
</li>
54 changes: 33 additions & 21 deletions plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
register_asset "stylesheets/babble.css"

BABBLE_PLUGIN_NAME ||= "babble".freeze
BABBLE_TOPIC_ID ||= -1
BABBLE_TOPIC_TITLE ||= "Title for Babble Topic"
BABBLE_UNIQUE_EMAIL ||= "noreply@chattykathy.com"
BABBLE_UNIQUE_USERNAME ||= "chattykathy"
BABBLE_USER_ID ||= -2

after_initialize do
module ::Babble
Expand Down Expand Up @@ -36,8 +37,12 @@ class ::Babble::TopicsController < ::ApplicationController
rescue_from 'StandardError' do |e| render_json_error e.message end

def show
TopicUser.find_or_create_by(user: current_user, topic: topic)
respond_with_topic_view
if topic
TopicUser.find_or_create_by(user: current_user, topic: topic)
respond_with_topic_view
else
render json: { errors: 'No chat topics are available!' }
end
end

def read
Expand All @@ -49,8 +54,8 @@ def read
end

def post
Babble::PostCreator.new(current_user, post_creator_params)
respond_with_topic_view
Babble::PostCreator.create(current_user, post_creator_params)
head :ok
end

private
Expand All @@ -62,7 +67,7 @@ def respond_with_topic_view
# should be able to replace this with Babble::Topic.find(id) of some kind
# once we make to move to multiple chat channels
def topic
@topic ||= Babble::Topic.ensure_existence
@topic ||= Babble::Topic.default_topic
end

def topic_view
Expand All @@ -73,8 +78,7 @@ def topic_view
def post_creator_params
{
raw: params[:raw],
skip_validations: true,
auto_track: false
skip_validations: true
}
end
end
Expand All @@ -87,11 +91,11 @@ def self.create(user, opts)

def valid?
setup_post
@topic = @post.topic = Babble::Topic.ensure_existence
@topic = @post.topic = Babble::Topic.default_topic
end

def enqueue_jobs
return "Stubbed for #{BABBLE_TOPIC_TITLE}"
return false
end

def trigger_after_events(post)
Expand All @@ -111,22 +115,30 @@ def serialized_post
end
end

class ::Babble::User
def self.find_or_create
User.find_or_initialize_by(id: BABBLE_USER_ID,
email: BABBLE_UNIQUE_EMAIL,
username: BABBLE_UNIQUE_USERNAME).tap(&:save)
end
end

class ::Babble::Topic
def self.ensure_existence
current_chat_topic ||
Topic.create!(id: BABBLE_TOPIC_ID,
user: Discourse.system_user,
title: BABBLE_TOPIC_TITLE,
visible: false)

def self.create_topic(title)
Topic.create! user: Babble::User.find_or_create,
title: title,
visible: false
end

def self.current_chat_topic
Topic.unscoped.find_by id: BABBLE_TOPIC_ID
def self.default_topic
available_topics.first
end

def self.recreate
current_chat_topic.destroy && ensure_existence
def self.available_topics
Babble::User.find_or_create.topics
end

end

end

0 comments on commit 11ef644

Please sign in to comment.