FEATURE: Add draft count to quick access profile panel (PR #13758)

Example when user has 2 drafts

GitHub

Using .length in ActiveRecord will cause all of the objects to be loaded into memory. What you want here is .count which will do an SQL count query.

Few changes but looks good overall; it would be good if you add a test to current_user_serializer as well.

Maybe this widget already takes this into account, but does this break if this.currentUser is null (like in the case of anon?)

Also just took a look at draft.stream and in this case you don’t care about limit and offset, that is just used for pagination. Looking at the stream method:

def self.stream(opts = nil)
  opts ||= {}

  user_id = opts[:user].id
  offset = (opts[:offset] || 0).to_i
  limit = (opts[:limit] || 30).to_i

  stream = Draft.where(user_id: user_id)
    .order(updated_at: :desc)
    .offset(offset)
    .limit(limit)

  # Preload posts and topics to avoid N+1 queries
  Draft.preload_data(stream, opts[:user])

  stream
end

I think what you should do is add a new option here called :all or :no_limit, and then you could use that and don’t do the limit and offset. So the code becomes like this:

stream = Draft.where(user_id: user_id).order(updated_at: :desc)

if !opts[:all]
   stream = stream.offset(offset).limit(limit)
end

And your call becomes:

Draft.stream({ user: object, all: true }).count

Actually looks like current_user_serializer test is broken from this change too, so when you add the test for the new attribute there you will need to run locally to diagnose + fix

It would also be good to not output (0) when the user has no drafts.

Ok I added a check to not have (0) when no drafts are present.

@martin-brennan I do not think this menu is even visible if this.currentUser is null.

I do however see an issue that the site needs a refresh before displaying the correct amount of drafts after drafts are deleted/created.

Is there a way to update this in the widget upon draft creation or deletion?

I don’t think you need this else clause, this panel is only shown to users.

I think we might need a different translation as this won’t work for RTL locales.

This is doing too much work just for a count.

Draft.where(user: object).count

Should be enough and much efficient :+1:

Since both include_has_topic_draft? and include_draft_count? do the same request, any chances this could be cached so we do the request (in Draft.has_topic_draft) once?