2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe TopicViewSerializer do
|
2018-03-27 05:04:55 +08:00
|
|
|
def serialize_topic(topic, user_arg)
|
|
|
|
topic_view = TopicView.new(topic.id, user_arg)
|
2019-04-12 21:55:27 +08:00
|
|
|
serializer =
|
|
|
|
TopicViewSerializer.new(topic_view, scope: Guardian.new(user_arg), root: false).as_json
|
|
|
|
JSON.parse(MultiJson.dump(serializer)).deep_symbolize_keys!
|
2018-05-23 06:39:15 +08:00
|
|
|
end
|
|
|
|
|
2023-06-16 10:44:35 +08:00
|
|
|
# Ensure no suggested ids are cached cause that can muck up suggested
|
|
|
|
use_redis_snapshotting
|
2018-02-14 04:46:25 +08:00
|
|
|
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:topic)
|
2023-12-13 17:18:42 +08:00
|
|
|
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
|
2022-05-10 23:02:28 +08:00
|
|
|
fab!(:user_2) { Fabricate(:user) }
|
2024-03-26 11:41:12 +08:00
|
|
|
fab!(:admin)
|
2017-09-26 14:42:27 +08:00
|
|
|
|
2017-11-29 21:52:41 +08:00
|
|
|
describe "#featured_link and #featured_link_root_domain" do
|
2020-06-29 11:11:06 +08:00
|
|
|
fab!(:featured_link) { "http://meta.discourse.org" }
|
2017-11-29 21:52:41 +08:00
|
|
|
|
|
|
|
describe "when topic featured link is disable" do
|
|
|
|
it "should return the right attributes" do
|
|
|
|
topic.update!(featured_link: featured_link)
|
|
|
|
SiteSetting.topic_featured_link_enabled = false
|
|
|
|
|
2018-02-14 04:46:25 +08:00
|
|
|
json = serialize_topic(topic, user)
|
2017-11-29 21:52:41 +08:00
|
|
|
|
|
|
|
expect(json[:featured_link]).to eq(nil)
|
|
|
|
expect(json[:featured_link_root_domain]).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when topic featured link is enabled" do
|
|
|
|
it "should return the right attributes" do
|
|
|
|
topic.update!(featured_link: featured_link)
|
|
|
|
|
2018-02-14 04:46:25 +08:00
|
|
|
json = serialize_topic(topic, user)
|
2017-11-29 21:52:41 +08:00
|
|
|
|
|
|
|
expect(json[:featured_link]).to eq(featured_link)
|
|
|
|
expect(json[:featured_link_root_domain]).to eq("discourse.org")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-02-09 11:55:32 +08:00
|
|
|
describe "#external_id" do
|
|
|
|
describe "when a topic has an external_id" do
|
|
|
|
before { topic.update!(external_id: "42-asdf") }
|
|
|
|
|
|
|
|
it "should return the external_id" do
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:external_id]).to eq("42-asdf")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-22 12:02:49 +08:00
|
|
|
describe "#image_url" do
|
2020-06-29 11:11:06 +08:00
|
|
|
fab!(:image_upload) { Fabricate(:image_upload, width: 5000, height: 5000) }
|
2019-10-22 12:02:49 +08:00
|
|
|
|
|
|
|
describe "when a topic has an image" do
|
FEATURE: Include optimized thumbnails for topics (#9215)
This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this:
- Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact
- A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake
- Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued
- Topic lists and topics now include a `thumbnails` key, which includes all the available images:
```
"thumbnails": [
{
"max_width": null,
"max_height": null,
"url": "//example.com/original-image.png",
"width": 1380,
"height": 1840
},
{
"max_width": 1024,
"max_height": 1024,
"url": "//example.com/optimized-image.png",
"width": 768,
"height": 1024
}
]
```
- Themes can request additional thumbnail sizes by using a modifier in their `about.json` file:
```
"modifiers": {
"topic_thumbnail_sizes": [
[200, 200],
[800, 800]
],
...
```
Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated
- Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link`
2020-05-05 16:07:50 +08:00
|
|
|
before { topic.update!(image_upload_id: image_upload.id) }
|
2019-10-22 12:02:49 +08:00
|
|
|
|
FEATURE: Include optimized thumbnails for topics (#9215)
This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this:
- Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact
- A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake
- Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued
- Topic lists and topics now include a `thumbnails` key, which includes all the available images:
```
"thumbnails": [
{
"max_width": null,
"max_height": null,
"url": "//example.com/original-image.png",
"width": 1380,
"height": 1840
},
{
"max_width": 1024,
"max_height": 1024,
"url": "//example.com/optimized-image.png",
"width": 768,
"height": 1024
}
]
```
- Themes can request additional thumbnail sizes by using a modifier in their `about.json` file:
```
"modifiers": {
"topic_thumbnail_sizes": [
[200, 200],
[800, 800]
],
...
```
Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated
- Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link`
2020-05-05 16:07:50 +08:00
|
|
|
it "should return the image url" do
|
2019-10-22 12:02:49 +08:00
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
|
2020-05-15 20:35:20 +08:00
|
|
|
expect(json[:image_url]).to end_with(image_upload.url)
|
FEATURE: Include optimized thumbnails for topics (#9215)
This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this:
- Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact
- A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake
- Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued
- Topic lists and topics now include a `thumbnails` key, which includes all the available images:
```
"thumbnails": [
{
"max_width": null,
"max_height": null,
"url": "//example.com/original-image.png",
"width": 1380,
"height": 1840
},
{
"max_width": 1024,
"max_height": 1024,
"url": "//example.com/optimized-image.png",
"width": 768,
"height": 1024
}
]
```
- Themes can request additional thumbnail sizes by using a modifier in their `about.json` file:
```
"modifiers": {
"topic_thumbnail_sizes": [
[200, 200],
[800, 800]
],
...
```
Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated
- Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link`
2020-05-05 16:07:50 +08:00
|
|
|
end
|
|
|
|
|
2021-07-01 10:52:40 +08:00
|
|
|
it "should have thumbnail jobs enqueued" do
|
FEATURE: Include optimized thumbnails for topics (#9215)
This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this:
- Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact
- A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake
- Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued
- Topic lists and topics now include a `thumbnails` key, which includes all the available images:
```
"thumbnails": [
{
"max_width": null,
"max_height": null,
"url": "//example.com/original-image.png",
"width": 1380,
"height": 1840
},
{
"max_width": 1024,
"max_height": 1024,
"url": "//example.com/optimized-image.png",
"width": 768,
"height": 1024
}
]
```
- Themes can request additional thumbnail sizes by using a modifier in their `about.json` file:
```
"modifiers": {
"topic_thumbnail_sizes": [
[200, 200],
[800, 800]
],
...
```
Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated
- Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link`
2020-05-05 16:07:50 +08:00
|
|
|
SiteSetting.create_thumbnails = true
|
|
|
|
|
2022-06-17 18:07:32 +08:00
|
|
|
Discourse.redis.del(topic.thumbnail_job_redis_key([]))
|
FEATURE: Include optimized thumbnails for topics (#9215)
This introduces new APIs for obtaining optimized thumbnails for topics. There are a few building blocks required for this:
- Introduces new `image_upload_id` columns on the `posts` and `topics` table. This replaces the old `image_url` column, which means that thumbnails are now restricted to uploads. Hotlinked thumbnails are no longer possible. In normal use (with pull_hotlinked_images enabled), this has no noticeable impact
- A migration attempts to match existing urls to upload records. If a match cannot be found then the posts will be queued for rebake
- Optimized thumbnails are generated during post_process_cooked. If thumbnails are missing when serializing a topic list, then a sidekiq job is queued
- Topic lists and topics now include a `thumbnails` key, which includes all the available images:
```
"thumbnails": [
{
"max_width": null,
"max_height": null,
"url": "//example.com/original-image.png",
"width": 1380,
"height": 1840
},
{
"max_width": 1024,
"max_height": 1024,
"url": "//example.com/optimized-image.png",
"width": 768,
"height": 1024
}
]
```
- Themes can request additional thumbnail sizes by using a modifier in their `about.json` file:
```
"modifiers": {
"topic_thumbnail_sizes": [
[200, 200],
[800, 800]
],
...
```
Remember that these are generated asynchronously, so your theme should include logic to fallback to other available thumbnails if your requested size has not yet been generated
- Two new raw plugin outlets are introduced, to improve the customisability of the topic list. `topic-list-before-columns` and `topic-list-before-link`
2020-05-05 16:07:50 +08:00
|
|
|
json = nil
|
|
|
|
|
|
|
|
expect do json = serialize_topic(topic, user) end.to change {
|
|
|
|
Jobs::GenerateTopicThumbnails.jobs.size
|
|
|
|
}.by(1)
|
|
|
|
|
|
|
|
topic.generate_thumbnails!
|
|
|
|
|
|
|
|
expect do json = serialize_topic(topic, user) end.not_to change {
|
2022-07-19 22:03:03 +08:00
|
|
|
Jobs::GenerateTopicThumbnails.jobs.size
|
|
|
|
}
|
2021-07-01 10:52:40 +08:00
|
|
|
end
|
2019-10-22 12:02:49 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "when a topic does not contain an image" do
|
|
|
|
it "should return a nil image url" do
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
|
|
|
|
expect(json.has_key? :image_url).to eq(true)
|
|
|
|
expect(json[:image_url]).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-26 14:42:27 +08:00
|
|
|
describe "#suggested_topics" do
|
2020-06-29 11:11:06 +08:00
|
|
|
fab!(:topic2) { Fabricate(:topic) }
|
2017-09-26 14:42:27 +08:00
|
|
|
|
2017-11-18 05:08:31 +08:00
|
|
|
before { TopicUser.update_last_read(user, topic2.id, 0, 0, 0) }
|
2017-09-26 14:42:27 +08:00
|
|
|
|
|
|
|
describe "when loading last chunk" do
|
|
|
|
it "should include suggested topics" do
|
2018-02-14 04:46:25 +08:00
|
|
|
json = serialize_topic(topic, user)
|
2017-09-26 14:42:27 +08:00
|
|
|
|
2019-04-12 21:55:27 +08:00
|
|
|
expect(json[:suggested_topics].first[:id]).to eq(topic2.id)
|
2017-09-26 14:42:27 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "when not loading last chunk" do
|
2020-06-29 11:11:06 +08:00
|
|
|
fab!(:post) { Fabricate(:post, topic: topic) }
|
|
|
|
fab!(:post2) { Fabricate(:post, topic: topic) }
|
2017-09-26 14:42:27 +08:00
|
|
|
|
|
|
|
it "should not include suggested topics" do
|
|
|
|
post
|
|
|
|
post2
|
|
|
|
topic_view = TopicView.new(topic.id, user, post_ids: [post.id])
|
|
|
|
topic_view.next_page
|
|
|
|
json = described_class.new(topic_view, scope: Guardian.new(user), root: false).as_json
|
|
|
|
|
|
|
|
expect(json[:suggested_topics]).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
2020-06-27 03:25:38 +08:00
|
|
|
|
|
|
|
describe "with private messages" do
|
2020-06-29 11:11:06 +08:00
|
|
|
fab!(:topic) do
|
2020-06-27 03:25:38 +08:00
|
|
|
Fabricate(
|
|
|
|
:private_message_topic,
|
|
|
|
highest_post_number: 1,
|
|
|
|
topic_allowed_users: [Fabricate.build(:topic_allowed_user, user: user)],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-06-29 11:11:06 +08:00
|
|
|
fab!(:topic2) do
|
2020-06-27 03:25:38 +08:00
|
|
|
Fabricate(
|
|
|
|
:private_message_topic,
|
|
|
|
highest_post_number: 1,
|
|
|
|
topic_allowed_users: [Fabricate.build(:topic_allowed_user, user: user)],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes suggested topics" do
|
|
|
|
TopicUser.change(
|
|
|
|
user,
|
|
|
|
topic2.id,
|
|
|
|
notification_level: TopicUser.notification_levels[:tracking],
|
|
|
|
)
|
|
|
|
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:suggested_topics].map { |t| t[:id] }).to contain_exactly(topic2.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not include suggested topics if all PMs are read" do
|
|
|
|
TopicUser.update_last_read(user, topic2.id, 1, 1, 0)
|
|
|
|
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:suggested_topics]).to eq([])
|
|
|
|
end
|
|
|
|
end
|
2017-09-26 14:42:27 +08:00
|
|
|
end
|
2018-02-14 04:46:25 +08:00
|
|
|
|
2021-09-07 12:30:40 +08:00
|
|
|
describe "#suggested_group_name" do
|
|
|
|
fab!(:pm) { Fabricate(:private_message_post).topic }
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:group)
|
2022-09-26 11:58:40 +08:00
|
|
|
|
2021-09-07 12:30:40 +08:00
|
|
|
it "is nil for a regular topic" do
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
|
|
|
|
expect(json[:suggested_group_name]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is nil if user is an allowed user of the private message" do
|
|
|
|
pm.allowed_users << user
|
|
|
|
|
|
|
|
json = serialize_topic(pm, user)
|
|
|
|
|
|
|
|
expect(json[:suggested_group_name]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the right group name if user is part of allowed group in the private message" do
|
|
|
|
pm.allowed_groups << group
|
|
|
|
group.add(user)
|
|
|
|
|
|
|
|
json = serialize_topic(pm, user)
|
|
|
|
|
|
|
|
expect(json[:suggested_group_name]).to eq(group.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-14 04:46:25 +08:00
|
|
|
describe "when tags added to private message topics" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:moderator)
|
|
|
|
fab!(:tag)
|
2020-06-29 11:11:06 +08:00
|
|
|
fab!(:pm) do
|
2018-02-22 22:57:02 +08:00
|
|
|
Fabricate(
|
|
|
|
:private_message_topic,
|
|
|
|
tags: [tag],
|
|
|
|
topic_allowed_users: [
|
|
|
|
Fabricate.build(:topic_allowed_user, user: moderator),
|
|
|
|
Fabricate.build(:topic_allowed_user, user: user),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:group)
|
2022-05-10 23:02:28 +08:00
|
|
|
fab!(:pm_between_reg_users) do
|
|
|
|
Fabricate(
|
|
|
|
:private_message_topic,
|
|
|
|
tags: [tag],
|
|
|
|
topic_allowed_users: [
|
|
|
|
Fabricate.build(:topic_allowed_user, user: user),
|
|
|
|
Fabricate.build(:topic_allowed_user, user: user_2),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-02-14 04:46:25 +08:00
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
2022-05-10 23:02:28 +08:00
|
|
|
SiteSetting.pm_tags_allowed_for_groups = "1|2|3|4"
|
2018-02-14 04:46:25 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not include the tag for normal users" do
|
|
|
|
json = serialize_topic(pm, user)
|
|
|
|
expect(json[:tags]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should include the tag for staff users" do
|
2018-02-22 22:57:02 +08:00
|
|
|
[moderator, admin].each do |user|
|
|
|
|
json = serialize_topic(pm, user)
|
|
|
|
expect(json[:tags]).to eq([tag.name])
|
|
|
|
end
|
2018-02-14 04:46:25 +08:00
|
|
|
end
|
2018-02-22 00:17:02 +08:00
|
|
|
|
2022-05-10 23:02:28 +08:00
|
|
|
it "should include the tag for users in allowed groups" do
|
|
|
|
SiteSetting.pm_tags_allowed_for_groups = "1|2|3|#{group.id}"
|
|
|
|
|
|
|
|
user.group_users << Fabricate(:group_user, group: group, user: user)
|
|
|
|
json = serialize_topic(pm_between_reg_users, user)
|
|
|
|
expect(json[:tags]).to eq([tag.name])
|
|
|
|
|
|
|
|
json = serialize_topic(pm_between_reg_users, user_2)
|
|
|
|
expect(json[:tags]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2018-02-22 00:17:02 +08:00
|
|
|
it "should not include the tag if pm tags disabled" do
|
2022-05-10 23:02:28 +08:00
|
|
|
SiteSetting.pm_tags_allowed_for_groups = ""
|
2018-02-22 00:17:02 +08:00
|
|
|
|
2018-02-22 22:57:02 +08:00
|
|
|
[moderator, admin].each do |user|
|
|
|
|
json = serialize_topic(pm, user)
|
|
|
|
expect(json[:tags]).to eq(nil)
|
|
|
|
end
|
2018-02-22 00:17:02 +08:00
|
|
|
end
|
2018-02-14 04:46:25 +08:00
|
|
|
end
|
2018-03-27 05:04:55 +08:00
|
|
|
|
|
|
|
describe "with hidden tags" do
|
2020-06-29 11:11:06 +08:00
|
|
|
fab!(:hidden_tag) { Fabricate(:tag, name: "hidden") }
|
|
|
|
fab!(:staff_tag_group) do
|
|
|
|
Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: [hidden_tag.name])
|
2018-03-27 05:04:55 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
before { topic.tags << hidden_tag }
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2018-03-27 05:04:55 +08:00
|
|
|
it "returns hidden tag to staff" do
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
expect(json[:tags]).to eq([hidden_tag.name])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not return hidden tag to non-staff" do
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:tags]).to eq([])
|
|
|
|
end
|
|
|
|
end
|
2019-04-12 21:55:27 +08:00
|
|
|
|
2020-10-13 05:23:04 +08:00
|
|
|
describe "tags order" do
|
2023-01-20 09:50:24 +08:00
|
|
|
fab!(:tag1) do
|
|
|
|
Fabricate(
|
|
|
|
:tag,
|
|
|
|
name: "ctag",
|
|
|
|
description: "c description",
|
|
|
|
staff_topic_count: 5,
|
|
|
|
public_topic_count: 5,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
fab!(:tag2) do
|
|
|
|
Fabricate(
|
|
|
|
:tag,
|
|
|
|
name: "btag",
|
|
|
|
description: "b description",
|
|
|
|
staff_topic_count: 9,
|
|
|
|
public_topic_count: 9,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
fab!(:tag3) do
|
|
|
|
Fabricate(
|
|
|
|
:tag,
|
|
|
|
name: "atag",
|
|
|
|
description: "a description",
|
|
|
|
staff_topic_count: 3,
|
|
|
|
public_topic_count: 3,
|
|
|
|
)
|
|
|
|
end
|
2020-10-13 05:23:04 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
topic.tags << tag1
|
|
|
|
topic.tags << tag2
|
|
|
|
topic.tags << tag3
|
|
|
|
end
|
|
|
|
|
|
|
|
it "tags are automatically sorted by tag popularity" do
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:tags]).to eq(%w[btag ctag atag])
|
2021-12-01 06:18:56 +08:00
|
|
|
expect(json[:tags_descriptions]).to eq(
|
|
|
|
{ btag: "b description", ctag: "c description", atag: "a description" },
|
|
|
|
)
|
2020-10-13 05:23:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "tags can be sorted alphabetically" do
|
|
|
|
SiteSetting.tags_sort_alphabetically = true
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:tags]).to eq(%w[atag btag ctag])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-04 02:26:37 +08:00
|
|
|
context "with flags" do
|
2020-06-29 11:11:06 +08:00
|
|
|
fab!(:post) { Fabricate(:post, topic: topic) }
|
|
|
|
fab!(:other_post) { Fabricate(:post, topic: topic) }
|
2023-12-13 17:18:42 +08:00
|
|
|
fab!(:flagger_1) { Fabricate(:user, refresh_auto_groups: true) }
|
|
|
|
fab!(:flagger_2) { Fabricate(:user, refresh_auto_groups: true) }
|
2019-05-04 02:26:37 +08:00
|
|
|
|
|
|
|
it "will return reviewable counts on posts" do
|
2023-12-13 17:18:42 +08:00
|
|
|
r = PostActionCreator.inappropriate(flagger_1, post).reviewable
|
2019-05-04 02:26:37 +08:00
|
|
|
r.perform(admin, :agree_and_keep)
|
2023-12-13 17:18:42 +08:00
|
|
|
PostActionCreator.spam(flagger_2, post)
|
2019-05-04 02:26:37 +08:00
|
|
|
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
p0 = json[:post_stream][:posts][0]
|
|
|
|
expect(p0[:id]).to eq(post.id)
|
|
|
|
expect(p0[:reviewable_score_count]).to eq(2)
|
|
|
|
expect(p0[:reviewable_score_pending_count]).to eq(1)
|
|
|
|
|
|
|
|
p1 = json[:post_stream][:posts][1]
|
|
|
|
expect(p1[:reviewable_score_count]).to eq(0)
|
|
|
|
expect(p1[:reviewable_score_pending_count]).to eq(0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-12 21:55:27 +08:00
|
|
|
describe "pending posts" do
|
|
|
|
context "when the queue is enabled" do
|
|
|
|
before { SiteSetting.approve_post_count = 1 }
|
|
|
|
|
2020-06-29 11:11:06 +08:00
|
|
|
fab!(:queued_post) do
|
2019-04-12 21:55:27 +08:00
|
|
|
ReviewableQueuedPost.needs_review!(
|
|
|
|
topic: topic,
|
|
|
|
payload: {
|
|
|
|
raw: "hello my raw contents",
|
|
|
|
},
|
2023-07-29 00:16:23 +08:00
|
|
|
created_by: Discourse.system_user,
|
|
|
|
target_created_by: user,
|
2019-04-12 21:55:27 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a pending_posts_count when the queue is enabled" do
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
expect(json[:queued_posts_count]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a user's pending posts" do
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:queued_posts_count]).to be_nil
|
|
|
|
|
|
|
|
post = json[:pending_posts].find { |p| p[:id] = queued_post.id }
|
|
|
|
expect(post[:raw]).to eq("hello my raw contents")
|
|
|
|
expect(post).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without an enabled queue" do
|
|
|
|
it "returns nil for the count" do
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
expect(json[:queued_posts_count]).to be_nil
|
|
|
|
expect(json[:pending_posts]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with details" do
|
2019-04-12 21:55:27 +08:00
|
|
|
it "returns the details object" do
|
|
|
|
PostCreator.create!(user, topic_id: topic.id, raw: "this is my post content")
|
|
|
|
topic.topic_links.create!(
|
|
|
|
user: user,
|
|
|
|
url: "https://discourse.org",
|
|
|
|
domain: "discourse.org",
|
|
|
|
clicks: 100,
|
|
|
|
)
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
|
|
|
|
details = json[:details]
|
|
|
|
expect(details).to be_present
|
|
|
|
expect(details[:created_by][:id]).to eq(topic.user_id)
|
|
|
|
expect(details[:last_poster][:id]).to eq(user.id)
|
|
|
|
expect(details[:notification_level]).to be_present
|
|
|
|
expect(details[:can_move_posts]).to eq(true)
|
|
|
|
expect(details[:can_flag_topic]).to eq(true)
|
2019-05-04 02:26:37 +08:00
|
|
|
expect(details[:can_review_topic]).to eq(true)
|
2019-04-12 21:55:27 +08:00
|
|
|
expect(details[:links][0][:clicks]).to eq(100)
|
|
|
|
|
|
|
|
participant = details[:participants].find { |p| p[:id] == user.id }
|
|
|
|
expect(participant[:post_count]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns extra fields for a personal message" do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
GroupUser.create(group: group, user: user)
|
|
|
|
GroupUser.create(group: group, user: admin)
|
|
|
|
|
|
|
|
group2 = Fabricate(:group)
|
|
|
|
GroupUser.create(group: group2, user: user)
|
|
|
|
|
|
|
|
pm = Fabricate(:private_message_topic)
|
|
|
|
pm.update(archetype: "private_message")
|
|
|
|
pm.topic_allowed_groups.create!(group: group)
|
|
|
|
pm.topic_allowed_groups.create!(group: group2)
|
|
|
|
|
|
|
|
json = serialize_topic(pm, admin)
|
|
|
|
|
|
|
|
details = json[:details]
|
|
|
|
expect(details[:can_remove_self_id]).to eq(admin.id)
|
|
|
|
expect(details[:allowed_users].find { |au| au[:id] == pm.user_id }).to be_present
|
|
|
|
expect(details[:allowed_groups].find { |ag| ag[:id] == group.id }).to be_present
|
|
|
|
end
|
2019-10-24 02:05:38 +08:00
|
|
|
|
2020-04-09 00:52:36 +08:00
|
|
|
it "has can_publish_page if possible" do
|
|
|
|
SiteSetting.enable_page_publishing = true
|
|
|
|
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:details][:can_publish_page]).to be_blank
|
|
|
|
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
expect(json[:details][:can_publish_page]).to eq(true)
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with can_edit_tags" do
|
2019-10-24 02:05:38 +08:00
|
|
|
before do
|
|
|
|
SiteSetting.tagging_enabled = true
|
2023-12-12 12:20:37 +08:00
|
|
|
SiteSetting.edit_wiki_post_allowed_groups = Group::AUTO_GROUPS[:trust_level_2]
|
2019-10-24 02:05:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true when user can edit a wiki topic" do
|
|
|
|
post = Fabricate(:post, wiki: true)
|
|
|
|
topic = Fabricate(:topic, first_post: post)
|
|
|
|
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:details][:can_edit_tags]).to be_nil
|
|
|
|
|
2024-01-25 14:28:26 +08:00
|
|
|
user.change_trust_level!(TrustLevel[2])
|
2019-10-24 02:05:38 +08:00
|
|
|
|
2023-12-12 12:20:37 +08:00
|
|
|
json = serialize_topic(topic, user.reload)
|
2019-10-24 02:05:38 +08:00
|
|
|
expect(json[:details][:can_edit_tags]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2020-12-03 06:21:59 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with can_edit" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:group_user)
|
2020-12-03 06:21:59 +08:00
|
|
|
fab!(:category) { Fabricate(:category, reviewable_by_group: group_user.group) }
|
|
|
|
fab!(:topic) { Fabricate(:topic, category: category) }
|
|
|
|
let(:user) { group_user.user }
|
|
|
|
|
|
|
|
before { SiteSetting.enable_category_group_moderation = true }
|
|
|
|
|
|
|
|
it "explicitly returns can_edit" do
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:details][:can_edit]).to eq(true)
|
|
|
|
|
|
|
|
topic.update!(category: nil)
|
|
|
|
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:details][:can_edit]).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2019-04-12 21:55:27 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with published_page" do
|
2020-04-09 00:52:36 +08:00
|
|
|
fab!(:published_page) { Fabricate(:published_page, topic: topic) }
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when page publishing is disabled" do
|
2020-06-17 21:10:07 +08:00
|
|
|
before { SiteSetting.enable_page_publishing = false }
|
2020-04-09 00:52:36 +08:00
|
|
|
|
2020-06-17 21:10:07 +08:00
|
|
|
it "doesn't return the published page if not enabled" do
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
expect(json[:published_page]).to be_blank
|
|
|
|
end
|
2020-04-09 00:52:36 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when page publishing is enabled" do
|
2020-06-17 21:10:07 +08:00
|
|
|
before { SiteSetting.enable_page_publishing = true }
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when not staff" do
|
2020-06-17 21:10:07 +08:00
|
|
|
it "doesn't return the published page" do
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
expect(json[:published_page]).to be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when staff" do
|
2020-06-17 21:10:07 +08:00
|
|
|
it "returns the published page" do
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
expect(json[:published_page]).to be_present
|
|
|
|
expect(json[:published_page][:slug]).to eq(published_page.slug)
|
|
|
|
end
|
2020-11-06 08:33:19 +08:00
|
|
|
|
2022-09-29 07:24:33 +08:00
|
|
|
context "when secure uploads is enabled" do
|
2020-11-06 08:33:19 +08:00
|
|
|
before do
|
|
|
|
setup_s3
|
2022-09-29 07:24:33 +08:00
|
|
|
SiteSetting.secure_uploads = true
|
2020-11-06 08:33:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't return the published page" do
|
|
|
|
json = serialize_topic(topic, admin)
|
|
|
|
expect(json[:published_page]).to be_blank
|
|
|
|
end
|
|
|
|
end
|
2020-06-17 21:10:07 +08:00
|
|
|
end
|
2020-04-09 00:52:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when viewing private messages when enable_category_group_moderation is enabled" do
|
2020-07-29 07:06:55 +08:00
|
|
|
fab!(:pm_topic) do
|
|
|
|
Fabricate(
|
|
|
|
:private_message_topic,
|
|
|
|
topic_allowed_users: [
|
|
|
|
Fabricate.build(:topic_allowed_user, user: user),
|
|
|
|
Fabricate.build(:topic_allowed_user, user: admin),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
fab!(:post) { Fabricate(:post, topic: pm_topic) }
|
|
|
|
|
|
|
|
before { SiteSetting.enable_category_group_moderation = true }
|
|
|
|
|
|
|
|
# Ensure having enable_category_group_moderation turned on doesn't break private messages
|
|
|
|
it "should return posts" do
|
|
|
|
json = serialize_topic(pm_topic, user)
|
|
|
|
expect(json[:post_stream][:posts]).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-23 22:31:59 +08:00
|
|
|
describe "#user_last_posted_at" do
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when the slow mode is disabled" do
|
2020-10-23 22:31:59 +08:00
|
|
|
it "returns nil" do
|
|
|
|
Fabricate(:topic_user, user: user, topic: topic, last_posted_at: 6.hours.ago)
|
|
|
|
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
|
|
|
|
expect(json[:user_last_posted_at]).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when the slow mode is enabled" do
|
2020-10-23 22:31:59 +08:00
|
|
|
before { topic.update!(slow_mode_seconds: 1000) }
|
|
|
|
|
|
|
|
it "returns nil if no user is given" do
|
|
|
|
json = serialize_topic(topic, nil)
|
|
|
|
|
|
|
|
expect(json[:user_last_posted_at]).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns nil if there's no topic_user association" do
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
|
|
|
|
expect(json[:user_last_posted_at]).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the last time the user posted" do
|
|
|
|
Fabricate(:topic_user, user: user, topic: topic, last_posted_at: 6.hours.ago)
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
|
|
|
|
expect(json[:user_last_posted_at]).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-08-31 12:05:32 +08:00
|
|
|
|
|
|
|
describe "#requested_group_name" do
|
|
|
|
fab!(:pm) { Fabricate(:private_message_post).topic }
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:group)
|
2021-08-31 12:05:32 +08:00
|
|
|
|
|
|
|
it "should return the right group name when PM is a group membership request" do
|
|
|
|
pm.custom_fields[:requested_group_id] = group.id
|
|
|
|
pm.save!
|
|
|
|
|
|
|
|
user = pm.first_post.user
|
|
|
|
group.add_owner(user)
|
|
|
|
json = serialize_topic(pm, user)
|
|
|
|
|
|
|
|
expect(json[:requested_group_name]).to eq(group.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not include the attribute for a non group membership request PM" do
|
|
|
|
json = serialize_topic(pm, pm.first_post.user)
|
|
|
|
|
|
|
|
expect(json[:requested_group_name]).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
2022-05-05 11:21:01 +08:00
|
|
|
|
|
|
|
describe "#topic_timer" do
|
|
|
|
it "does not include the attribute when topic does not have a topic timer" do
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
|
|
|
|
expect(json[:topic_timer]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes the attribute when topic has a public topic timer" do
|
|
|
|
topic_timer =
|
|
|
|
Fabricate(:topic_timer, topic: topic, execute_at: Time.utc(2022, 4, 6, 16, 23, 56))
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
|
|
|
|
expect(json[:topic_timer][:id]).to eq(topic_timer.id)
|
|
|
|
expect(json[:topic_timer][:based_on_last_post]).to eq(false)
|
|
|
|
expect(json[:topic_timer][:category_id]).to eq(nil)
|
|
|
|
expect(json[:topic_timer][:duration_minutes]).to eq(nil)
|
|
|
|
expect(json[:topic_timer][:execute_at]).to eq("2022-04-06T16:23:56.000Z")
|
|
|
|
expect(json[:topic_timer][:status_type]).to eq("close")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not include the attribute for category topic timer where category is restricted to user" do
|
|
|
|
category = Fabricate(:category, read_restricted: true)
|
|
|
|
|
|
|
|
Fabricate(
|
|
|
|
:topic_timer,
|
|
|
|
topic: topic,
|
|
|
|
category_id: category.id,
|
|
|
|
status_type: TopicTimer.types[:publish_to_category],
|
|
|
|
)
|
|
|
|
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
|
|
|
|
expect(json[:topic_timer]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "includes the attribute for category topic timer where category is not restricted to user" do
|
|
|
|
category = Fabricate(:category, read_restricted: false)
|
|
|
|
|
|
|
|
topic_timer =
|
|
|
|
Fabricate(
|
|
|
|
:topic_timer,
|
|
|
|
topic: topic,
|
|
|
|
category_id: category.id,
|
|
|
|
status_type: TopicTimer.types[:publish_to_category],
|
|
|
|
)
|
|
|
|
|
|
|
|
json = serialize_topic(topic, user)
|
|
|
|
|
|
|
|
expect(json[:topic_timer][:id]).to eq(topic_timer.id)
|
|
|
|
end
|
|
|
|
end
|
2017-09-26 14:42:27 +08:00
|
|
|
end
|