FEATURE: User Status API (#19149)

This adds API scope for the user status. This also adds a get method to the user status controller. We didn't need a dedicated method that returns status before because the server returns status with user objects, but I think we need to provide this method for API clients.
This commit is contained in:
Andrei Prigorshnev 2022-11-24 19:16:28 +04:00 committed by GitHub
parent 9e42473af4
commit 6357a3ce33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 56 additions and 0 deletions

View File

@ -3,6 +3,13 @@
class UserStatusController < ApplicationController
requires_login
def get
ensure_feature_enabled
respond_to do |format|
format.json { render json: UserStatusSerializer.new(current_user.user_status, root: false) }
end
end
def set
ensure_feature_enabled
description = params.require(:description)

View File

@ -63,6 +63,10 @@ class ApiKeyScope < ActiveRecord::Base
delete: { actions: %w[admin/users#destroy] },
list: { actions: %w[admin/users#index] },
},
user_status: {
read: { actions: %w[user_status#get] },
update: { actions: %w[user_status#set user_status#clear] },
},
email: {
receive_emails: { actions: %w[admin/email#handle_mail admin/email#smtp_should_reject] }
},

View File

@ -4518,6 +4518,9 @@ en:
anonymize: Anonymize user accounts.
delete: Delete user accounts.
list: Get a list of users.
user_status:
read: Read user status.
update: Update user status.
email:
receive_emails: Combine this scope with the mail-receiver to process incoming emails.
badges:

View File

@ -1037,6 +1037,7 @@ Discourse::Application.routes.draw do
post "/presence/update" => "presence#update"
get "/presence/get" => "presence#get"
get "user-status" => "user_status#get"
put "user-status" => "user_status#set"
delete "user-status" => "user_status#clear"

View File

@ -421,6 +421,7 @@ RSpec.describe Admin::ApiController do
"email",
"posts",
"uploads",
"user_status",
"global",
"badges",
"categories",

View File

@ -1,6 +1,46 @@
# frozen_string_literal: true
RSpec.describe UserStatusController do
describe '#get' do
it 'requires user to be logged in' do
get "/user-status.json"
expect(response.status).to eq(403)
end
it "returns 404 if the feature is disabled" do
user = Fabricate(:user)
sign_in(user)
SiteSetting.enable_user_status = false
get "/user-status.json"
expect(response.status).to eq(404)
end
describe 'when feature is enabled and a user is logged in' do
fab!(:user) { Fabricate(:user) }
before do
sign_in(user)
SiteSetting.enable_user_status = true
end
it "returns user status" do
status = "off to dentist"
status_emoji = "tooth"
ends_at = "2100-01-01T18:00:00.000Z"
user.set_status!(status, status_emoji, DateTime.parse(ends_at))
get "/user-status.json"
expect(response.status).to eq(200)
expect(response.parsed_body["description"]).to eq(status)
expect(response.parsed_body["emoji"]).to eq(status_emoji)
expect(response.parsed_body["ends_at"]).to eq(ends_at)
end
end
end
describe '#set' do
it 'requires user to be logged in' do
put "/user-status.json", params: { description: "off to dentist" }