This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/models/post.js.coffee.erb
2013-02-15 15:17:57 -05:00

248 lines
8.2 KiB
Plaintext

window.Discourse.Post = Ember.Object.extend Discourse.Presence,
# Url to this post
url: (->
Discourse.Utilities.postUrl(@get('topic.slug') || @get('topic_slug'), @get('topic_id'), @get('post_number'))
).property('post_number', 'topic_id', 'topic.slug')
originalPostUrl: (->
"/t/#{@get('topic_id')}/#{@get('reply_to_post_number')}"
).property('reply_to_post_number')
showUserReplyTab: (->
@get('reply_to_user') and (@get('reply_to_post_number') < (@get('post_number') - 1))
).property('reply_to_user', 'reply_to_post_number', 'post_number')
firstPost: (->
return true if @get('bestOfFirst') == true
@get('post_number') == 1
).property('post_number')
hasHistory: (-> @get('version') > 1 ).property('version')
postElementId: (-> "post_#{@get('post_number')}").property()
# The class for the read icon of the post. It starts with read-icon then adds 'seen' or
# 'last-read' if the post has been seen or is the highest post number seen so far respectively.
bookmarkClass: (->
result = 'read-icon'
return result + ' bookmarked' if @get('bookmarked')
topic = @get('topic')
if topic and topic.get('last_read_post_number') == @get('post_number')
result += ' last-read'
else
result += ' seen' if @get('read')
result
).property('read', 'topic.last_read_post_number', 'bookmarked')
# Custom tooltips for the bookmark icons
bookmarkTooltip: (->
return Em.String.i18n('bookmarks.created') if @get('bookmarked')
return "" unless @get('read')
topic = @get('topic')
if topic and topic.get('last_read_post_number') == @get('post_number')
return Em.String.i18n('bookmarks.last_read')
return Em.String.i18n('bookmarks.not_bookmarked')
).property('read', 'topic.last_read_post_number', 'bookmarked')
bookmarkedChanged: (->
jQuery.ajax
url: "/posts/#{@get('id')}/bookmark",
type: 'PUT'
data:
bookmarked: if @get('bookmarked') then true else false
error: (error) =>
errors = jQuery.parseJSON(error.responseText).errors
bootbox.alert(errors[0])
@toggleProperty('bookmarked')
).observes('bookmarked')
internalLinks: (->
return null if @blank('link_counts')
@get('link_counts').filterProperty('internal').filterProperty('title')
).property('link_counts.@each.internal')
# Edits are the version - 1, so version 2 = 1 edit
editCount: (->
@get('version') - 1
).property('version')
historyHeat: (->
return unless updatedAt = @get('updated_at')
rightNow = new Date().getTime()
# Show heat on age
updatedAtDate = Date.create(updatedAt).getTime()
return 'heatmap-high' if updatedAtDate > (rightNow - 60 * 60 * 1000 * 12)
return 'heatmap-med' if updatedAtDate > (rightNow - 60 * 60 * 1000 * 24)
return 'heatmap-low' if updatedAtDate > (rightNow - 60 * 60 * 1000 * 48)
).property('updated_at')
flagsAvailable: (->
Discourse.get('site.flagTypes').filter (item) =>
@get("actionByName.#{item.get('name_key')}.can_act")
).property('Discourse.site.flagTypes', 'actions_summary.@each.can_act')
actionsHistory: (->
return null unless @present('actions_summary')
@get('actions_summary').filter (i) ->
return false if i.get('count') == 0
return true if i.get('users') and i.get('users').length > 0
return not i.get('hidden')
).property('actions_summary.@each.users', 'actions_summary.@each.count')
# Save a post and call the callback when done.
save: (complete, error) ->
unless @get('newPost')
# We're updating a post
$.ajax
url: "/posts/#{@get('id')}",
type: 'PUT'
data:
post: {raw: @get('raw')}
image_sizes: @get('imageSizes')
success: (result) ->
complete?(Discourse.Post.create(result))
error: (result) -> error?(result)
else
# We're saving a post
data =
post: @getProperties('raw', 'topic_id', 'reply_to_post_number', 'category')
archetype: @get('archetype')
title: @get('title')
image_sizes: @get('imageSizes')
target_usernames: @get('target_usernames')
# Put the metaData into the request
if metaData = @get('metaData')
data.meta_data = {}
Ember.keys(metaData).forEach (key) -> data.meta_data[key] = metaData.get(key)
$.ajax
type: 'POST'
url: "/posts",
data: data
success: (result) -> complete?(Discourse.Post.create(result))
error: (result) -> error?(result)
recover: ->
$.ajax "/posts/#{@get('id')}/recover", type: 'PUT', cache: false
delete: (complete) ->
$.ajax "/posts/#{@get('id')}", type: 'DELETE', success: (result) -> complete?()
# Update the properties of this post from an obj, ignoring cooked as we should already
# have that rendered.
updateFromSave: (obj) ->
return unless obj
Object.each obj, (key, val) =>
return false if key == 'actions_summary'
@set(key, val) if val
# Rebuild actions summary
@set('actions_summary', Em.A())
if obj.actions_summary
lookup = Em.Object.create()
obj.actions_summary.each (a) =>
a.post = @
a.actionType = Discourse.get("site").postActionTypeById(a.id)
actionSummary = Discourse.ActionSummary.create(a)
@get('actions_summary').pushObject(actionSummary)
lookup.set(a.actionType.get('name_key'), actionSummary)
@set('actionByName', lookup)
# Load replies to this post
loadReplies: ->
promise = new RSVP.Promise()
@set('loadingReplies', true)
@set('replies', [])
jQuery.getJSON "/posts/#{@get('id')}/replies", (loaded) =>
loaded.each (reply) =>
post = Discourse.Post.create(reply)
post.set('topic', @get('topic'))
@get('replies').pushObject post
@set('loadingReplies', false)
promise.resolve()
promise
loadVersions: (callback) ->
$.get "/posts/#{@get('id')}/versions.json", (result) -> callback(result)
# Whether to show replies directly below
showRepliesBelow: (->
reply_count = @get('reply_count')
# We don't show replies if there aren't any
return false if reply_count is 0
# Always show replies if the setting `supress_reply_directly_below` is false.
return true unless Discourse.SiteSettings.supress_reply_directly_below
# Always show replies if there's more than one
return true if reply_count > 1
# If we have *exactly* one reply, we have to consider if it's directly below us
return false if @get('topic')?.isReplyDirectlyBelow(@)
true
).property('reply_count')
window.Discourse.Post.reopenClass
REGULAR_TYPE: <%= Post::REGULAR %>
MODERATOR_ACTION_TYPE: <%= Post::MODERATOR_ACTION %>
createActionSummary: (result) ->
if (result.actions_summary)
lookup = Em.Object.create()
result.actions_summary = result.actions_summary.map (a) ->
a.post = result
a.actionType = Discourse.get("site").postActionTypeById(a.id)
actionSummary = Discourse.ActionSummary.create(a)
lookup.set(a.actionType.get('name_key'), actionSummary)
actionSummary
result.set('actionByName', lookup)
create: (obj, topic) ->
result = @_super(obj)
@createActionSummary(result)
result.set('reply_to_user', Discourse.User.create(obj.reply_to_user)) if obj.reply_to_user
result.set('topic', topic)
result
deleteMany: (posts) ->
$.ajax "/posts/destroy_many",
type: 'DELETE',
data:
post_ids: posts.map (p) -> p.get('id')
loadVersion: (postId, version, callback) ->
jQuery.getJSON "/posts/#{postId}.json?version=#{version}", (result) =>
callback(Discourse.Post.create(result))
loadByPostNumber: (topicId, postId, callback) ->
jQuery.getJSON "/posts/by_number/#{topicId}/#{postId}.json", (result) => callback(Discourse.Post.create(result))
loadQuote: (postId) ->
promise = new RSVP.Promise
jQuery.getJSON "/posts/#{postId}.json", (result) =>
post = Discourse.Post.create(result)
promise.resolve(Discourse.BBCode.buildQuoteBBCode(post, post.get('raw')))
promise
load: (postId, callback) ->
jQuery.getJSON "/posts/#{postId}.json", (result) => callback(Discourse.Post.create(result))