mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: Drafts view in user profile
* add drafts.json endpoint, user profile tab with drafts stream * improve drafts stream display in user profile * truncate excerpts in drafts list, better handling for resume draft action * improve draft stream SQL query, add rspec tests * if composer is open, quietly close it when user opens another draft from drafts stream; load PM draft only when user is in /u/username/messages (instead of /u/username) * cleanup * linting fixes * apply prettier styling to modified files * add client tests for drafts, includes a fixture for drafts.json * improvements to code following review * refresh drafts route when user deletes a draft open in the composer while being in the drafts route; minor prettier scss fix * added more spec tests, deleted an acceptance test for removing drafts that was too finicky, formatting and code style fixes, added appEvent for draft:destroyed * prettier, eslint fixes * use "username_lower" from users table, added error handling for rejected promises * adds guardian spec for can_see_drafts, adds improvements following code review * move DraftsController spec to its own file * fix failing drafts qunit test, use getOwner instead of deprecated this.container * limit test fixture for draft.json testing to new_topic request only
This commit is contained in:
@@ -43,6 +43,43 @@ class Draft < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def self.stream(opts = nil)
|
||||
opts ||= {}
|
||||
|
||||
user_id = opts[:user].id
|
||||
offset = (opts[:offset] || 0).to_i
|
||||
limit = (opts[:limit] || 30).to_i
|
||||
|
||||
# JOIN of topics table based on manipulating draft_key seems imperfect
|
||||
builder = DB.build <<~SQL
|
||||
SELECT
|
||||
d.*, t.title, t.id topic_id, t.archetype,
|
||||
t.category_id, t.closed topic_closed, t.archived topic_archived,
|
||||
pu.username, pu.name, pu.id user_id, pu.uploaded_avatar_id, pu.username_lower,
|
||||
du.username draft_username, NULL as raw, NULL as cooked, NULL as post_number
|
||||
FROM drafts d
|
||||
LEFT JOIN topics t ON
|
||||
CASE
|
||||
WHEN d.draft_key LIKE '%' || '#{EXISTING_TOPIC}' || '%'
|
||||
THEN CAST(replace(d.draft_key, '#{EXISTING_TOPIC}', '') AS INT)
|
||||
ELSE 0
|
||||
END = t.id
|
||||
JOIN users pu on pu.id = COALESCE(t.user_id, d.user_id)
|
||||
JOIN users du on du.id = #{user_id}
|
||||
/*where*/
|
||||
/*order_by*/
|
||||
/*offset*/
|
||||
/*limit*/
|
||||
SQL
|
||||
|
||||
builder
|
||||
.where('d.user_id = :user_id', user_id: user_id.to_i)
|
||||
.order_by('d.updated_at desc')
|
||||
.offset(offset)
|
||||
.limit(limit)
|
||||
.query
|
||||
end
|
||||
|
||||
def self.cleanup!
|
||||
DB.exec("DELETE FROM drafts where sequence < (
|
||||
SELECT max(s.sequence) from draft_sequences s
|
||||
|
||||
Reference in New Issue
Block a user