Skip to content

Commit

Permalink
Syncing files from cloud/release/2024.004 to release/2024.004
Browse files Browse the repository at this point in the history
  • Loading branch information
maneeshchiba-hl committed Mar 5, 2024
1 parent 6951617 commit e54cdf7
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 149 deletions.
115 changes: 1 addition & 114 deletions applications/vanilla/controllers/class.discussionscontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,114 +257,6 @@ public function index($Page = false)
$this->render();
}

/**
* @deprecated since 2.3
*/
public function unread($page = "0")
{
deprecated(__METHOD__);

if (!Gdn::session()->isValid()) {
redirectTo("/discussions/index");
}

// Figure out which discussions layout to choose (Defined on "Homepage" settings page).
$layout = c("Vanilla.Discussions.Layout");
switch ($layout) {
case "table":
if ($this->SyndicationMethod == SYNDICATION_NONE) {
$this->View = "table";
}
break;
default:
// $this->View = 'index';
break;
}
Gdn_Theme::section("DiscussionList");

// Determine offset from $Page
[$page, $limit] = offsetLimit($page, c("Vanilla.Discussions.PerPage", 30));
$this->canonicalUrl(url(concatSep("/", "discussions", "unread", pageNumber($page, $limit, true, false)), true));

// Validate $Page
if (!is_numeric($page) || $page < 0) {
$page = 0;
}

// Setup head.
if (!$this->data("Title")) {
$title = Gdn::formatService()->renderPlainText(c("Garden.HomepageTitle"), HtmlFormat::FORMAT_KEY);
if ($title) {
$this->title($title, "");
} else {
$this->title(t("Unread Discussions"));
}
}
if (!$this->description()) {
$this->description(
Gdn::formatService()->renderPlainText(c("Garden.Description", ""), HtmlFormat::FORMAT_KEY)
);
}
if ($this->Head) {
$this->Head->addRss(url("/discussions/unread/feed.rss", true), $this->Head->title());
}

// Add modules
$this->addModule("DiscussionFilterModule");
$this->addModule("NewDiscussionModule");
$this->addModule("CategoriesModule");
$this->addModule("BookmarkedModule");
$this->addModule("TagModule");

$this->setData("Breadcrumbs", [
["Name" => t("Discussions"), "Url" => "/discussions"],
["Name" => t("Unread"), "Url" => "/discussions/unread"],
]);

// Set criteria & get discussions data
$this->setData("Category", false, true);
$discussionModel = new DiscussionModel();
$discussionModel->setSort(Gdn::request()->get());
$discussionModel->setFilters(Gdn::request()->get());
$this->setData("Sort", $discussionModel->getSort());
$this->setData("Filters", $discussionModel->getFilters());

// Get Discussion Count
$countDiscussions = $discussionModel->getUnreadCount();
$this->setData("CountDiscussions", $countDiscussions);

// Get Discussions
$this->DiscussionData = $discussionModel->getUnread($page, $limit, [
"d.CategoryID" => CategoryModel::instance()->getVisibleCategoryIDs(["filterHideDiscussions" => true]),
]);

$this->setData("Discussions", $this->DiscussionData, true);
$this->setJson("Loading", $page . " to " . $limit);

// Build a pager
$pagerFactory = new Gdn_PagerFactory();
$this->EventArguments["PagerType"] = "Pager";
$this->fireEvent("BeforeBuildPager");
$this->Pager = $pagerFactory->getPager($this->EventArguments["PagerType"], $this);
$this->Pager->ClientID = "Pager";
$this->Pager->configure($page, $limit, $countDiscussions, 'discussions/unread/%1$s');
if (!$this->data("_PagerUrl")) {
$this->setData("_PagerUrl", "discussions/unread/{Page}");
}
$this->setData("_Page", $page);
$this->setData("_Limit", $limit);
$this->fireEvent("AfterBuildPager");

// Deliver JSON data if necessary
if ($this->_DeliveryType != DELIVERY_TYPE_ALL) {
$this->setJson("LessRow", $this->Pager->toString("less"));
$this->setJson("MoreRow", $this->Pager->toString("more"));
$this->View = "discussions";
}

$this->render();
}

/**
* Highlight route and include JS, CSS, and modules used by all methods.
*
Expand Down Expand Up @@ -431,14 +323,9 @@ public function bookmarked($page = "0")
// Determine offset from $Page
[$offset, $limit] = offsetLimit($page, c("Vanilla.Discussions.PerPage", 30));
$this->canonicalUrl(
url(concatSep("/", "discussions", "bookmarked", pageNumber($page, $limit, true, false)), true)
url(concatSep("/", "discussions", "bookmarked", pageNumber($offset, $limit, true, false)), true)
);

// Validate $Page
if (!is_numeric($page) || $page < 0) {
$page = 0;
}

$discussionModel = new DiscussionModel();
$discussionModel->setSort(Gdn::request()->get());
$discussionModel->setFilters(Gdn::request()->get());
Expand Down
2 changes: 1 addition & 1 deletion applications/vanilla/controllers/class.postcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function discussion($categoryID = "")
$categoryData = $this->ShowCategorySelector ? CategoryModel::categories() : false;

// Check permission
if (isset($this->Discussion)) {
if (isset($this->Discussion) and $draftID == 0) {
// Make sure that content can (still) be edited.
$canEdit = DiscussionModel::canEdit($this->Discussion);
if (!$canEdit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@
namespace Vanilla\Forum\Tests\Controllers;

use Garden\Schema\ValidationException;
use Garden\Web\Exception\ForbiddenException;
use PHPUnit\Framework\TestCase;
use Vanilla\CurrentTimeStamp;
use Vanilla\EmbeddedContent\Embeds\FileEmbed;
use Vanilla\EmbeddedContent\Embeds\ImageEmbed;
use Vanilla\EmbeddedContent\Embeds\QuoteEmbed;
use Vanilla\EmbeddedContent\EmbedService;
use VanillaTests\Forum\Utils\CommunityApiTestTrait;
use VanillaTests\Models\TestCategoryModelTrait;
use VanillaTests\Models\TestCommentModelTrait;
use VanillaTests\Models\TestDiscussionModelTrait;
use VanillaTests\SetupTraitsTrait;
use VanillaTests\SiteTestTrait;
use VanillaTests\VanillaTestCase;

/**
* Tests for the `DraftsController` class.
Expand Down Expand Up @@ -718,4 +719,50 @@ public function testDiscussionCommentFormFormat()
$this->assertSame($this->commentDraft["Format"], $r->Form->getValue("Format"));
});
}

/**
* Make sure we can edit a draft even without Discussion.Edit.
*
* @return void
*/
public function testEditingPermission(): void
{
// Disable editing of posts.
$this->config->saveToConfig("Garden.EditContentTimeout", "0");
$this->api()->setUserID($this->memberID);
$draftID = $this->draftModel->save([
"Name" => __FUNCTION__,
"Body" => __FUNCTION__,
"Format" => "Text",
"Type" => "Discussion",
"CategoryID" => -1,
]);

/** @var \PostController $response */
$response = $this->bessy()->get("/post/editdiscussion/0/$draftID");
$this->assertEquals($draftID, $response->Draft->DraftID);
}

/**
* Test fetching the draft from another user.
*
* @return void
*/
public function testEditingAnotherUserDraft(): void
{
$this->expectException(ForbiddenException::class);
$user = $this->createUserFixture(VanillaTestCase::ROLE_MEMBER);
$this->api()->setUserID($user["UserID"]);
$draftID = $this->draftModel->save([
"Name" => __FUNCTION__,
"Body" => __FUNCTION__,
"Format" => "Text",
"Type" => "Discussion",
"CategoryID" => -1,
]);

$this->api()->setUserID($this->memberID);
/** @var \PostController $response */
$this->bessy()->get("/post/editdiscussion/0/$draftID");
}
}
31 changes: 0 additions & 31 deletions applications/vanilla/views/discussions/unread.php

This file was deleted.

5 changes: 4 additions & 1 deletion packages/vanilla-utils/src/logicUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import chai, { expect } from "chai";
import asPromised from "chai-as-promised";
import { flattenObject, spaceshipCompare, unflattenObject } from "./logicUtils";
import { ColorHelper } from "csx";
chai.use(asPromised);

describe("spaceshipCompare()", () => {
Expand All @@ -30,13 +31,15 @@ describe("flattenObject()", () => {
nestedKey: "val2",
array1: ["one", "two", "three"],
},
color: new ColorHelper("rgb", 0, 0, 0, 1, false),
};

const flattened = {
key1: "val1",
isNull: null,
"nested.nestedKey": "val2",
"nested.array1": ["one", "two", "three"],
color: "rgb(0,0,0)",
};

const things = ["one", "two"];
Expand All @@ -46,6 +49,6 @@ describe("flattenObject()", () => {
});

it("unflattens objects", () => {
expect(unflattenObject(flattened)).deep.equal(nested);
expect(unflattenObject(flattened)).deep.equal({ ...nested, color: "rgb(0,0,0)" });
});
});
7 changes: 6 additions & 1 deletion packages/vanilla-utils/src/logicUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import set from "lodash/set";
import { ColorHelper } from "csx";

export function spaceshipCompare(a, b): number {
if (a > b) {
Expand All @@ -20,7 +21,11 @@ export function flattenObject(obj: Record<any, any>, delimiter = "") {
return Object.keys(obj).reduce((acc, key) => {
const prefix = delimiter.length ? delimiter + "." : "";
if (typeof obj[key] === "object" && !Array.isArray(obj[key]) && obj[key] !== null) {
Object.assign(acc, flattenObject(obj[key], prefix + key));
if (obj[key] instanceof ColorHelper) {
acc[prefix + key] = obj[key].toString();
} else {
Object.assign(acc, flattenObject(obj[key], prefix + key));
}
} else {
acc[prefix + key] = obj[key];
}
Expand Down

0 comments on commit e54cdf7

Please sign in to comment.