A platform for community discussion. Free, open, simple.
FEATURE: Share Rewind reports publicly by default and allow for making them private (#36587)
Martin Brennanc4d4ed890fbb
diff --git a/plugins/discourse-rewind/app/controllers/discourse_rewind/rewinds_controller.rb b/plugins/discourse-rewind/app/controllers/discourse_rewind/rewinds_controller.rbindex e072a1718b4..aab6bc9feee 100644--- a/plugins/discourse-rewind/app/controllers/discourse_rewind/rewinds_controller.rb+++ b/plugins/discourse-rewind/app/controllers/discourse_rewind/rewinds_controller.rb@@ -2,9 +2,8 @@module ::DiscourseRewindclass RewindsController < ::ApplicationController- requires_plugin PLUGIN_NAME-requires_login+ requires_plugin PLUGIN_NAMEdef dismissDiscourseRewind::Dismiss.call(service_params) do@@ -15,6 +14,9 @@ module ::DiscourseRewinddef indexDiscourseRewind::FetchReports.call(service_params) do+ on_model_not_found(:for_user) do+ raise Discourse::NotFound.new(nil, custom_message: "discourse_rewind.user_not_found")+ endon_model_not_found(:year) doraise Discourse::NotFound.new(nil, custom_message: "discourse_rewind.invalid_year")end@@ -29,6 +31,9 @@ module ::DiscourseRewinddef showDiscourseRewind::FetchReport.call(service_params) do+ on_model_not_found(:for_user) do+ raise Discourse::NotFound.new(nil, custom_message: "discourse_rewind.user_not_found")+ endon_model_not_found(:year) doraise Discourse::NotFound.new(nil, custom_message: "discourse_rewind.invalid_year")end@@ -46,5 +51,23 @@ module ::DiscourseRewindon_success { |report:| render json: { report: }, status: :ok }endend++ def toggle_share+ DiscourseRewind::ToggleShare.call(service_params) do+ on_success do |shared:|+ render json: {+ shared: guardian.user.reload.user_option.discourse_rewind_share_publicly,+ },+ status: :ok+ end+ on_failure { render(json: failed_json, status: :unprocessable_entity) }+ on_failed_policy(:user_not_hiding_profile) do+ render_json_error(+ I18n.t("discourse_rewind.cannot_share_when_profile_hidden"),+ status: 400,+ )+ end+ end+ endendenddiff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/best_posts.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/best_posts.rbindex aaa02fc9529..0eb56c31f37 100644--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/best_posts.rb+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/best_posts.rb@@ -36,11 +36,12 @@ module DiscourseRewindreturn FakeData if should_use_fake_data?best_posts =Post+ .joins(:topic).where(user_id: user.id)- .where(created_at: date)- .where(deleted_at: nil)+ .where(posts: { created_at: date, deleted_at: nil }).where("post_number > 1")- .order("like_count DESC NULLS LAST, created_at ASC")+ .where.not(topics: { archetype: Archetype.private_message })+ .order("like_count DESC NULLS LAST, posts.created_at ASC").limit(3).select(:post_number, :topic_id, :like_count, :reply_count, :raw, :cooked).map do |post|diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/best_topics.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/best_topics.rbindex 78b3697fae7..632c402fa69 100644--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/best_topics.rb+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/best_topics.rb@@ -34,6 +34,7 @@ module DiscourseRewind.includes(:topic).references(:topic).where(topic: { deleted_at: nil, created_at: date, user_id: user.id })+ .where.not(topic: { archetype: Archetype.private_message }).order("yearly_score DESC NULLS LAST").limit(3).pluck(:topic_id, :title, :excerpt, :yearly_score)diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rbindex 312c033ec68..94a151fbfbf 100644--- a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb+++ b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb@@ -16,20 +16,38 @@ module DiscourseRewind# @param [Guardian] guardian# @param [Hash] params# @option params [Integer] :index of the report+ # @option params [String] :for_user_username (optional) username of the user to see the rewind for, otherwise the guardian user is used# @return [Service::Base::Context]params doattribute :index, :integer+ attribute :for_user_username, :stringvalidates :index, presence: true, numericality: { greater_than_or_equal_to: 0 }end+ model :for_usermodel :yearmodel :all_reportsmodel :reportprivate+ def fetch_for_user(params:, guardian:)+ return guardian.user if params.for_user_username.blank?++ user = User.find_by(username: params.for_user_username)+ return if user.nil?++ if guardian.user.id != user.id+ if !user.discourse_rewind_and_profile_public?+ return if !guardian.user.admin?+ end+ end++ user+ end+def fetch_yearcurrent_date = Time.zone.nowcurrent_month = current_date.month@@ -49,8 +67,8 @@ module DiscourseRewindendend- def fetch_all_reports(guardian:, year:)- key = "rewind:#{guardian.user.username}:#{year}"+ def fetch_all_reports(for_user:, year:)+ key = "rewind:#{for_user.username}:#{year}"reports = Discourse.redis.get(key)return nil unless reportsdiff --git a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rbindex bb4b6585b33..8a367ba8f80 100644--- a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb+++ b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb@@ -6,7 +6,7 @@ module DiscourseRewind# @example# ::DiscourseRewind::Rewind::Fetch.call(# guardian: guardian,- # params: { year: 2023, username: 'codinghorror' }+ # params: { user: User, for_user_username: 'codinghorror' }# )#class FetchReports@@ -15,8 +15,7 @@ module DiscourseRewind# @!method self.call(guardian:, params:)# @param [Guardian] guardian# @param [Hash] params- # @option params [Integer] :year of the rewind- # @option params [Integer] :username of the rewind+ # @option params [String] :for_user_username (optional) username of the user to see the rewind for, otherwise the guardian user is used# @return [Service::Base::Context]CACHE_DURATION = Rails.env.development? ? 10.seconds : 3.days@@ -44,6 +43,12 @@ module DiscourseRewindAction::Invites,]+ params do+ attribute :user+ attribute :for_user_username, :string+ end++ model :for_usermodel :yearmodel :datemodel :all_reports@@ -52,6 +57,21 @@ module DiscourseRewindprivate+ def fetch_for_user(params:, guardian:)+ return guardian.user if params.for_user_username.blank?++ user = User.find_by(username: params.for_user_username)+ return if user.nil?++ if guardian.user.id != user.id+ if !user.discourse_rewind_and_profile_public?+ return if !guardian.user.admin?+ end+ end++ user+ end+def fetch_yearcurrent_date = Time.zone.nowcurrent_month = current_date.month@@ -77,14 +97,14 @@ module DiscourseRewindDate.new(year).all_yearend- def fetch_all_reports(date:, guardian:, year:)- key = cache_key(guardian.user.username, year)+ def fetch_all_reports(date:, for_user:, year:)+ key = cache_key(for_user.username, year)reports = Discourse.redis.get(key)if !reportsreports =REPORTS.filter_map do |report|- report.call(date:, user: guardian.user, guardian:)+ report.call(date:, user: for_user)rescue StandardErrornilenddiff --git a/plugins/discourse-rewind/app/services/discourse_rewind/toggle_share.rb b/plugins/discourse-rewind/app/services/discourse_rewind/toggle_share.rbnew file mode 100644index 00000000000..3a970a1b117--- /dev/null+++ b/plugins/discourse-rewind/app/services/discourse_rewind/toggle_share.rb@@ -0,0 +1,38 @@+# frozen_string_literal: true++module DiscourseRewind+ # Service responsible for toggling the user's public sharing preference for Rewind.+ #+ # @example+ # ::DiscourseRewind::ToggleShare.call(guardian: guardian)+ #+ class ToggleShare+ include Service::Base++ # @!method self.call(guardian:)+ # @param [Guardian] guardian+ # @return [Service::Base::Context]++ policy :user_not_hiding_profile+ step :toggle_share_preference++ private++ def user_not_hiding_profile(guardian:)+ if guardian.user.user_option.hide_profile &&+ !guardian.user.user_option.discourse_rewind_share_publicly+ false+ else+ # Even if hiding profile, it's fine to turn off rewind public sharing.+ true+ end+ end++ def toggle_share_preference(guardian:)+ guardian.user.user_option.update!(+ discourse_rewind_share_publicly: !guardian.user.user_option.discourse_rewind_share_publicly,+ )+ context[:shared] = guardian.user.user_option.discourse_rewind_share_publicly+ end+ end+enddiff --git a/plugins/discourse-rewind/assets/javascripts/discourse/components/rewind.gjs b/plugins/discourse-rewind/assets/javascripts/discourse/components/rewind.gjsindex e3c781db035..ccb005f06bd 100644--- a/plugins/discourse-rewind/assets/javascripts/discourse/components/rewind.gjs+++ b/plugins/discourse-rewind/assets/javascripts/discourse/components/rewind.gjs@@ -4,10 +4,16 @@ import { on } from "@ember/modifier";import { action } from "@ember/object";import didInsert from "@ember/render-modifiers/modifiers/did-insert";import willDestroy from "@ember/render-modifiers/modifiers/will-destroy";+import { service } from "@ember/service";+import { htmlSafe } from "@ember/template";import DButton from "discourse/components/d-button";+import DToggleSwitch from "discourse/components/d-toggle-switch";import concatClass from "discourse/helpers/concat-class";import { ajax } from "discourse/lib/ajax";import { popupAjaxError } from "discourse/lib/ajax-error";+import { getAbsoluteURL } from "discourse/lib/get-url";+import { clipboardCopy } from "discourse/lib/utilities";+import { eq } from "discourse/truth-helpers";import { i18n } from "discourse-i18n";import ActivityCalendar from "discourse/plugins/discourse-rewind/discourse/components/reports/activity-calendar";import AiUsage from "discourse/plugins/discourse-rewind/discourse/components/reports/ai-usage";@@ -32,13 +38,22 @@ const BUFFER_SIZE = 3;const SCROLL_THRESHOLD = 0.7;export default class Rewind extends Component {+ @service dialog;+ @service currentUser;+ @service toasts;+@tracked rewind = [];- @tracked fullScreen = true;+ @tracked fullScreen = this.currentUser !== null;@tracked loadingRewind = false;@tracked totalAvailable = 0;@tracked isLoadingMore = false;+ @tracked cannotViewRewind = false;nextReportIndex = 0;+ get canShare() {+ return this.currentUser.id === this.args.user.id;+ }+@actionregisterScrollWrapper(element) {this.scrollWrapper = element;@@ -52,14 +67,23 @@ export default class Rewind extends Component {@actionasync loadRewind() {+ let url = "/rewinds.json";+ if (this.args.user.id !== this.currentUser.id) {+ url += `?for_user_username=${this.args.user.username}`;+ }+try {this.loadingRewind = true;- const response = await ajax("/rewinds.json");+ const response = await ajax(url);this.rewind = response.reports;this.totalAvailable = response.total_available;this.nextReportIndex = response.reports.length;- } catch (e) {- popupAjaxError(e);+ } catch (err) {+ if (err.jqXHR.status === 404 || err.jqXHR.status === 403) {+ this.cannotViewRewind = true;+ } else {+ popupAjaxError(err);+ }} finally {this.loadingRewind = false;}@@ -107,8 +131,13 @@ export default class Rewind extends Component {try {while (this.nextReportIndex < targetIndex) {+ let url = `/rewinds/${this.nextReportIndex}.json`;+ if (this.args.user.id !== this.currentUser.id) {+ url += `?for_user_username=${this.args.user.username}`;+ }+try {- const response = await ajax(`/rewinds/${this.nextReportIndex}.json`, {+ const response = await ajax(url, {ignoreUnsent: false,});if (response.report) {@@ -132,6 +161,73 @@ export default class Rewind extends Component {this.fullScreen = !this.fullScreen;}+ @action+ async copyRewindLink() {+ await clipboardCopy(+ getAbsoluteURL(`/u/${this.currentUser.username}/activity/rewind`)+ );+ this.toasts.success({+ duration: "short",+ data: {+ message: i18n("post.controls.link_copied"),+ },+ });+ }++ @action+ async toggleShareRewind() {+ if (this.currentUser.user_option.discourse_rewind_share_publicly) {+ try {+ const response = await ajax("/rewinds/toggle-share", {+ type: "PUT",+ });++ this.currentUser.set(+ "user_option.discourse_rewind_share_publicly",+ response.shared+ );++ this.toasts.success({+ duration: "short",+ data: {+ message: i18n("discourse_rewind.share.disabled_success"),+ },+ });+ } catch (err) {+ popupAjaxError(err);+ return;+ }++ return;+ }++ await this.dialog.confirm({+ message: i18n("discourse_rewind.share.confirm"),+ confirmButtonLabel: "discourse_rewind.share.confirm_button.enable",+ cancelButtonLabel: "discourse_rewind.share.confirm_button.disable",+ didConfirm: async () => {+ try {+ const response = await ajax("/rewinds/toggle-share", {+ type: "PUT",+ });+ this.currentUser.set(+ "user_option.discourse_rewind_share_publicly",+ response.shared+ );++ this.toasts.success({+ duration: "short",+ data: {+ message: i18n("discourse_rewind.share.enabled_success"),+ },+ });+ } catch (err) {+ popupAjaxError(err);+ }+ },+ });+ }+@actionhandleEscape(event) {if (this.fullScreen && event.key === "Escape") {@@ -214,16 +310,64 @@ export default class Rewind extends Component {</div></div>{{else}}- <DButton- class="btn-default rewind__exit-fullscreen-btn --special-kbd"- @icon={{if this.fullScreen "discourse-compress" "discourse-expand"}}- @action={{this.toggleFullScreen}}- />+ <div class="rewind__header-buttons">+ {{#if this.canShare}}+ <div class="rewind__share-toggle-wrapper">+ {{i18n "discourse_rewind.share.toggle_label.private"}}++ <DToggleSwitch+ @state={{this.currentUser.user_option.discourse_rewind_share_publicly}}+ class="rewind__share-toggle"+ {{on "click" this.toggleShareRewind}}+ />+ {{i18n "discourse_rewind.share.toggle_label.public"}}+ </div>++ {{#if+ this.currentUser.user_option.discourse_rewind_share_publicly+ }}+ <DButton+ class="btn-default rewind__copy-link-btn --special-kbd"+ @title="composer.link_toolbar.copy"+ @icon="link"+ @action={{this.copyRewindLink}}+ />+ {{/if}}+ {{/if}}+ <DButton+ class="btn-default rewind__exit-fullscreen-btn --special-kbd"+ @icon={{if+ this.fullScreen+ "discourse-compress"+ "discourse-expand"+ }}+ @action={{this.toggleFullScreen}}+ />++ </div>+<divclass="rewind__scroll-wrapper"{{didInsert this.registerScrollWrapper}}{{willDestroy this.cleanup}}>+ {{#unless (eq this.currentUser.id @user.id)}}+ <p class="rewind-other-user">{{htmlSafe+ (i18n+ "discourse_rewind.viewing_other_user"+ )+ }}</p>+ {{/unless}}++ {{#if this.cannotViewRewind}}+ <div class="rewind-error">+ {{htmlSafe+ (i18n "discourse_rewind.cannot_view_rewind_gibberish")+ }}+ {{htmlSafe (i18n "discourse_rewind.cannot_view_rewind")}}+ </div>+ {{/if}}{{#each this.rewind as |report|}}{{#letdiff --git a/plugins/discourse-rewind/assets/javascripts/discourse/controllers/preferences/rewind.js b/plugins/discourse-rewind/assets/javascripts/discourse/controllers/preferences/rewind.jsindex 431bd954702..e0418e1dd95 100644--- a/plugins/discourse-rewind/assets/javascripts/discourse/controllers/preferences/rewind.js+++ b/plugins/discourse-rewind/assets/javascripts/discourse/controllers/preferences/rewind.js@@ -4,7 +4,10 @@ import { service } from "@ember/service";import { popupAjaxError } from "discourse/lib/ajax-error";import { i18n } from "discourse-i18n";-const REWIND_ATTRS = ["discourse_rewind_disabled"];+const REWIND_ATTRS = [+ "discourse_rewind_disabled",+ "discourse_rewind_share_publicly",+];export default class PreferencesRewindController extends Controller {@service rewind;diff --git a/plugins/discourse-rewind/assets/javascripts/discourse/pre-initializers/rewind-user-options.js b/plugins/discourse-rewind/assets/javascripts/discourse/pre-initializers/rewind-user-options.jsindex 0f7f72ac72c..e77ea30fdc4 100644--- a/plugins/discourse-rewind/assets/javascripts/discourse/pre-initializers/rewind-user-options.js+++ b/plugins/discourse-rewind/assets/javascripts/discourse/pre-initializers/rewind-user-options.js@@ -1,6 +1,7 @@import { withPluginApi } from "discourse/lib/plugin-api";-const FIELD_NAME = "discourse_rewind_disabled";+const DISABLED_FIELD_NAME = "discourse_rewind_disabled";+const SHARE_FIELD_NAME = "discourse_rewind_share_publicly";export default {name: "rewind-user-options",@@ -9,7 +10,8 @@ export default {withPluginApi((api) => {const siteSettings = container.lookup("service:site-settings");if (siteSettings.discourse_rewind_enabled) {- api.addSaveableUserOptionField(FIELD_NAME);+ api.addSaveableUserOptionField(DISABLED_FIELD_NAME);+ api.addSaveableUserOptionField(SHARE_FIELD_NAME);}});},diff --git a/plugins/discourse-rewind/assets/javascripts/discourse/routes/user-activity/rewind.js b/plugins/discourse-rewind/assets/javascripts/discourse/routes/user-activity/rewind.jsindex 4e82daeb19d..dd54957a268 100644--- a/plugins/discourse-rewind/assets/javascripts/discourse/routes/user-activity/rewind.js+++ b/plugins/discourse-rewind/assets/javascripts/discourse/routes/user-activity/rewind.js@@ -1,5 +1,15 @@+import { service } from "@ember/service";import DiscourseRoute from "discourse/routes/discourse";export default class UserActivityRewind extends DiscourseRoute {+ @service currentUser;+ @service router;+templateName = "user/rewind";++ beforeModel() {+ if (!this.currentUser) {+ return this.router.transitionTo("discovery.latest");+ }+ }}diff --git a/plugins/discourse-rewind/assets/javascripts/discourse/templates/preferences/rewind.gjs b/plugins/discourse-rewind/assets/javascripts/discourse/templates/preferences/rewind.gjsindex 0a276dd6551..f83373ed370 100644--- a/plugins/discourse-rewind/assets/javascripts/discourse/templates/preferences/rewind.gjs+++ b/plugins/discourse-rewind/assets/javascripts/discourse/templates/preferences/rewind.gjs@@ -19,6 +19,24 @@ export default <template></label></div>+ <div+ class="control-group rewind-setting"+ data-setting-name="user_discourse_rewind_share_publicly"+ >+ <label class="controls">+ <Input+ id="user_discourse_rewind_share_publicly"+ disabled={{@controller.model.user_option.hide_profile}}+ title={{i18n+ "discourse_rewind.preferences.cannot_share_when_profile_hidden"+ }}+ @type="checkbox"+ @checked={{@controller.model.user_option.discourse_rewind_share_publicly}}+ />+ {{i18n "discourse_rewind.preferences.share_publicly"}}+ </label>+ </div>+<SaveControls@model={{@controller.model}}@action={{@controller.save}}diff --git a/plugins/discourse-rewind/assets/javascripts/discourse/templates/user/rewind.gjs b/plugins/discourse-rewind/assets/javascripts/discourse/templates/user/rewind.gjsindex 72478d30414..ed30c90505a 100644--- a/plugins/discourse-rewind/assets/javascripts/discourse/templates/user/rewind.gjs+++ b/plugins/discourse-rewind/assets/javascripts/discourse/templates/user/rewind.gjs@@ -1,3 +1,3 @@import Rewind from "../../components/rewind";-export default <template><Rewind /></template>+export default <template><Rewind @user={{@controller.model}} /></template>diff --git a/plugins/discourse-rewind/assets/stylesheets/common/rewind-callout.scss b/plugins/discourse-rewind/assets/stylesheets/common/rewind-callout.scssindex 4eac9acf59b..0146e37542e 100644--- a/plugins/discourse-rewind/assets/stylesheets/common/rewind-callout.scss+++ b/plugins/discourse-rewind/assets/stylesheets/common/rewind-callout.scss@@ -43,12 +43,13 @@.rewind-callout {position: relative;- display: inline-block;+ display: flex;transition: transform 200ms ease;.btn.no-text.--special-kbd {- top: 0.75em;+ top: 0.5em;right: 1em;+ position: absolute;font-size: var(--font-down-3);@media screen and (width <= 639px) {diff --git a/plugins/discourse-rewind/assets/stylesheets/common/rewind-header.scss b/plugins/discourse-rewind/assets/stylesheets/common/rewind-header.scssindex d7784e4d201..0a73af7a3ef 100644--- a/plugins/discourse-rewind/assets/stylesheets/common/rewind-header.scss+++ b/plugins/discourse-rewind/assets/stylesheets/common/rewind-header.scss@@ -40,6 +40,7 @@position: sticky;z-index: 3;color: var(--rewind-black);+ display: flex;.rewind-logo {margin-bottom: 5px;@@ -58,7 +59,7 @@svg {grid-area: logo;- width: 2.5em;+ width: 2.15em;height: 2.5em;}}diff --git a/plugins/discourse-rewind/assets/stylesheets/common/rewind.scss b/plugins/discourse-rewind/assets/stylesheets/common/rewind.scssindex 852d6c68229..9087804ab48 100644--- a/plugins/discourse-rewind/assets/stylesheets/common/rewind.scss+++ b/plugins/discourse-rewind/assets/stylesheets/common/rewind.scss@@ -1,3 +1,5 @@+@use "lib/viewport";+.rewind-container {border-radius: var(--d-border-radius);box-sizing: border-box;@@ -27,6 +29,10 @@}}+#dialog-holder {+ z-index: 10000;+}+.rewind-report:last-of-type {margin-bottom: 10em;}@@ -106,14 +112,108 @@flex-direction: column;padding-bottom: var(--safe-area-inset-bottom);padding-top: 2em;++ .rewind-error,+ .rewind-other-user {+ color: var(--rewind-green);+ font-family: var(--pixel-text);+ text-align: left;+ }++ .rewind-other-user {+ font-size: var(--font-down-1);+ text-align: center;+ }++ .rewind-error {+ line-height: 1em;+ padding: 2em;+ }++ @include viewport.until(sm) {+ padding-top: 3.5em;+ }+}++.rewind__header-buttons {+ display: flex;+ flex-direction: row;+ position: absolute;+ top: 0;+ right: 0;+ z-index: 3;+ padding: 1em;++ .--fullscreen & {+ right: 1em;+ }++ @media (width <= 650px) {+ display: grid;+ grid-template-columns: 1fr auto auto;+ grid-template-rows: auto auto;+ }++ .rewind__share-toggle-wrapper {+ margin-left: auto;+ display: flex;+ align-items: center;+ background-color: var(--rewind-beige);+ padding: var(--space-2) var(--space-4);+ border-radius: 4px;+ font-family: var(--pixel-text);+ color: #5b5848;++ @media (width <= 650px) {+ border-radius: 0 0 4px 4px;+ grid-column-start: 1;+ grid-column-end: 3;+ margin-top: 1.15em;+ }++ .d-toggle-switch__label {+ &:hover {+ .d-toggle-switch__checkbox-slider {+ background: #b0a68a !important; // override core specificity+ }+ }+ }++ .d-toggle-switch__checkbox-slider {+ margin-inline: var(--space-2);+ border-radius: 2px;+ background: #c5bfa0;++ &:hover {+ background: #b0a68a !important; // override core specificity+ }++ .d-icon-check {+ display: none;+ }++ &::before {+ height: 17px;+ border-radius: 2px;+ color: #d7d1b0;+ box-shadow: -1px 1px 0 2px #afa88d;+ background: #ede9d4;+ }+ }+ }++ @media (width <= 650px) {+ .btn.no-text.--special-kbd {+ grid-row-start: 1;+ justify-self: end;+ }+ }}// specific to override core.btn.no-text.--special-kbd {- position: absolute;- top: 15px;- right: 24px;background-color: #ede9d4;+ margin-left: 0.5em;border-color: #d7d1b0 #c5bfa0 #d7d1b0 #c5bfa0;border-width: 1px 5px 7px 4px;box-shadow: -1px 1px 0 1px #c0b99b;@@ -125,6 +225,14 @@transform: skew(3deg);font-size: var(--font-down-1);+ &.rewind__share-btn {+ right: 78px;+ }++ &.rewind__exit-fullscreen-btn {+ right: 24px;+ }+.d-icon {color: var(--rewind-blackdiff --git a/plugins/discourse-rewind/config/locales/client.en.yml b/plugins/discourse-rewind/config/locales/client.en.ymlindex 192516c2d16..b416404976a 100644--- a/plugins/discourse-rewind/config/locales/client.en.yml+++ b/plugins/discourse-rewind/config/locales/client.en.yml@@ -9,9 +9,35 @@ en:placeholder: Discourse Rewindtitle: Rewindloading: "Reticulating splines..."+ viewing_other_user: "You are viewing <strong>%{username}'s</strong> Discourse Rewind report."+ cannot_view_rewind_gibberish: >+ [ACCESS DENIED::REWIND MAINFRAME SUBROUTINE FAILURE]<br><br>++ Bootstrapping anomaly log...<br><br>++ >> quantum buffer underrun ⧛<br>+ >> cross-node parity drift (Δ=004.33)<br>+ >> packet ϟ fragment at offset ∇47<br>+ >> flux conduit handshake TIMED OUT<br><br>+ cannot_view_rewind: >+ You cannot view this Discourse Rewind report. Maybe the owner has not shared it publicly?<br><br>++ $> _<br><br>preferences:disable_rewind: "Disable Discourse Rewind. This hides the year-end activity summary in your profile and on your avatar."+ share_publicly: "Share Discourse Rewind summary with other forum members. Any forum member will be able to view your Discourse Rewind summary."+ cannot_share_when_profile_hidden: "In order to share Rewind you must allow others to view your public profile."profile_link: "View your %{rewindYear} Discourse Rewind"+ share:+ toggle_label:+ private: "Private"+ public: "Public"+ confirm: "Be sure to review your Rewind before sharing, as it may include data from personal messages. You can make it private again at any time."+ confirm_button:+ enable: "Make public"+ disable: "Keep private"+ enabled_success: "Your Rewind is now sharable with other forum members."+ disabled_success: "Your Rewind is now private."reports:activity_calendar:title: Activity Calendardiff --git a/plugins/discourse-rewind/config/locales/server.en.yml b/plugins/discourse-rewind/config/locales/server.en.ymlindex 1294cd525a1..77f3195af9e 100644--- a/plugins/discourse-rewind/config/locales/server.en.yml+++ b/plugins/discourse-rewind/config/locales/server.en.yml@@ -4,5 +4,7 @@ en:discourse_rewind:report_failed: "Failed to generate Discourse Rewind report."invalid_year: "Rewind can only be generated in January or December."+ user_not_found: "User not found or cannot be viewed."reports_not_cached: "Reports have not been generated yet. Please refresh the page."report_not_found: "Report not found."+ cannot_share_when_profile_hidden: "In order to share Rewind you must allow others to view your public profile."diff --git a/plugins/discourse-rewind/config/routes.rb b/plugins/discourse-rewind/config/routes.rbindex 9c52106e708..1acc14fa87b 100644--- a/plugins/discourse-rewind/config/routes.rb+++ b/plugins/discourse-rewind/config/routes.rb@@ -3,6 +3,7 @@DiscourseRewind::Engine.routes.draw doget "/rewinds" => "rewinds#index"get "/rewinds/:index" => "rewinds#show"+ put "/rewinds/toggle-share" => "rewinds#toggle_share"post "/rewinds/dismiss" => "rewinds#dismiss"enddiff --git a/plugins/discourse-rewind/db/migrate/20251210065956_add_discourse_rewind_share_publicly_to_user_options.rb b/plugins/discourse-rewind/db/migrate/20251210065956_add_discourse_rewind_share_publicly_to_user_options.rbnew file mode 100644index 00000000000..b2db3c99ac2--- /dev/null+++ b/plugins/discourse-rewind/db/migrate/20251210065956_add_discourse_rewind_share_publicly_to_user_options.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++class AddDiscourseRewindSharePubliclyToUserOptions < ActiveRecord::Migration[7.2]+ def change+ add_column :user_options, :discourse_rewind_share_publicly, :boolean, default: true, null: false+ end+enddiff --git a/plugins/discourse-rewind/plugin.rb b/plugins/discourse-rewind/plugin.rbindex 4390d1a9dd2..5be452e52c6 100644--- a/plugins/discourse-rewind/plugin.rb+++ b/plugins/discourse-rewind/plugin.rb@@ -43,15 +43,25 @@ endrequire_relative "lib/discourse_rewind/engine"after_initialize do- UserUpdater::OPTION_ATTR.push(:discourse_rewind_disabled)+ UserUpdater::OPTION_ATTR.push(:discourse_rewind_disabled, :discourse_rewind_share_publicly)+ add_to_class(:user, :discourse_rewind_and_profile_public?) do+ self.user_option.discourse_rewind_share_publicly && !self.user_option.hide_profile+ end++ # add_to_serializer(:current_user) / add_to_serializer(:current_user_option)%i[user_option current_user_option].each do |serializer|add_to_serializer(serializer, :discourse_rewind_disabled) { object.discourse_rewind_disabled }+add_to_serializer(serializer, :discourse_rewind_dismissed) dodismissed_at = object.discourse_rewind_dismissed_atdismissed_at.present? &&DiscourseRewind.rewind_year(dismissed_at) >= DiscourseRewind.rewind_yearend++ add_to_serializer(serializer, :discourse_rewind_share_publicly) do+ scope.user.discourse_rewind_and_profile_public?+ endendadd_to_serializer(:current_user, :is_rewind_active) dodiff --git a/plugins/discourse-rewind/spec/actions/best_posts_spec.rb b/plugins/discourse-rewind/spec/actions/best_posts_spec.rbindex 9c9d4e9e847..5bc0999ed46 100644--- a/plugins/discourse-rewind/spec/actions/best_posts_spec.rb+++ b/plugins/discourse-rewind/spec/actions/best_posts_spec.rb@@ -62,5 +62,23 @@ RSpec.describe DiscourseRewind::Action::BestPosts doexpect(call_report[:data].map { |d| d[:post_number] }).not_to include(post_1.post_number)endend++ context "when a post is in a private message" do+ fab!(:pm_topic) { Fabricate(:private_message_topic, user: user) }+ fab!(:pm_post) do+ Fabricate(+ :post,+ created_at: random_datetime,+ user: user,+ post_number: 2,+ topic: pm_topic,+ like_count: 99,+ )+ end++ it "is not included" do+ expect(call_report[:data].map { |d| d[:topic_id] }).not_to include(pm_topic.id)+ end+ endendenddiff --git a/plugins/discourse-rewind/spec/actions/best_topics_spec.rb b/plugins/discourse-rewind/spec/actions/best_topics_spec.rbindex 410433219f0..5243b8f6cb5 100644--- a/plugins/discourse-rewind/spec/actions/best_topics_spec.rb+++ b/plugins/discourse-rewind/spec/actions/best_topics_spec.rb@@ -62,5 +62,18 @@ RSpec.describe DiscourseRewind::Action::BestTopics doexpect(call_report[:data].map { |d| d[:topic_id] }).not_to include(topic_1.id)endend++ context "when a topic is a private message" do+ fab!(:pm_topic) { Fabricate(:private_message_topic, user: user, created_at: random_datetime) }++ before do+ TopTopic.refresh!+ TopTopic.find_by(topic_id: pm_topic.id)&.update!(yearly_score: 99)+ end++ it "is not included" do+ expect(call_report[:data].map { |d| d[:topic_id] }).not_to include(pm_topic.id)+ end+ endendenddiff --git a/plugins/discourse-rewind/spec/requests/rewinds_controller_spec.rb b/plugins/discourse-rewind/spec/requests/rewinds_controller_spec.rbindex 2ae146c6d99..89ec10e8af9 100644--- a/plugins/discourse-rewind/spec/requests/rewinds_controller_spec.rb+++ b/plugins/discourse-rewind/spec/requests/rewinds_controller_spec.rb@@ -135,4 +135,62 @@ RSpec.describe DiscourseRewind::RewindsController doendendend++ describe "#toggle_share" do+ it "requires login" do+ put "/rewinds/toggle-share.json"+ expect(response.status).to eq(403)+ end++ context "when logged in" do+ fab!(:user)+ before { sign_in(user) }++ it "toggles share preference from false to true" do+ user.user_option.update!(discourse_rewind_share_publicly: false)++ put "/rewinds/toggle-share.json"++ expect(response.status).to eq(200)+ expect(response.parsed_body["shared"]).to eq(true)+ expect(user.user_option.reload.discourse_rewind_share_publicly).to eq(true)+ end++ it "toggles share preference from true to false" do+ user.user_option.update!(discourse_rewind_share_publicly: true)++ put "/rewinds/toggle-share.json"++ expect(response.status).to eq(200)+ expect(response.parsed_body["shared"]).to eq(false)+ expect(user.user_option.reload.discourse_rewind_share_publicly).to eq(false)+ end++ context "when user has hidden profile" do+ before { user.user_option.update!(hide_profile: true) }++ it "prevents enabling share when profile is hidden" do+ user.user_option.update!(discourse_rewind_share_publicly: false)++ put "/rewinds/toggle-share.json"++ expect(response.status).to eq(400)+ expect(response.parsed_body["errors"].first).to eq(+ I18n.t("discourse_rewind.cannot_share_when_profile_hidden"),+ )+ expect(user.user_option.reload.discourse_rewind_share_publicly).to eq(false)+ end++ it "allows disabling share even when profile is hidden" do+ user.user_option.update!(discourse_rewind_share_publicly: true)++ put "/rewinds/toggle-share.json"++ expect(response.status).to eq(200)+ expect(response.parsed_body["shared"]).to eq(false)+ expect(user.user_option.reload.discourse_rewind_share_publicly).to eq(false)+ end+ end+ end+ endenddiff --git a/plugins/discourse-rewind/spec/services/fetch_report_spec.rb b/plugins/discourse-rewind/spec/services/fetch_report_spec.rbnew file mode 100644index 00000000000..0f32181c519--- /dev/null+++ b/plugins/discourse-rewind/spec/services/fetch_report_spec.rb@@ -0,0 +1,186 @@+# frozen_string_literal: true++RSpec.describe(DiscourseRewind::FetchReport) do+ describe ".call" do+ subject(:result) { described_class.call(**dependencies) }++ fab!(:current_user, :user)++ let(:guardian) { Guardian.new(current_user) }+ let(:params) { { index: 0 } }+ let(:dependencies) { { guardian:, params: } }++ before do+ SiteSetting.discourse_rewind_enabled = true+ freeze_time DateTime.parse("2021-12-22")+ key = "rewind:#{current_user.username}:2021"+ reports = [+ { type: "top_words", data: "report 1" },+ { type: "reading_time", data: "report 2" },+ { type: "writing_analysis", data: "report 3" },+ ]+ Discourse.redis.setex(key, 3600, MultiJson.dump(reports))+ end++ context "when index is not provided" do+ let(:params) { {} }++ it { is_expected.to fail_a_contract }+ end++ context "when index is negative" do+ let(:params) { { index: -1 } }++ it { is_expected.to fail_a_contract }+ end++ context "when fetching a report by index" do+ context "when index is 0" do+ let(:params) { { index: 0 } }++ it "returns the first report" do+ expect(result).to be_success+ expect(result.report[:type]).to eq("top_words")+ end+ end++ context "when index is 1" do+ let(:params) { { index: 1 } }++ it "returns the second report" do+ expect(result).to be_success+ expect(result.report[:type]).to eq("reading_time")+ end+ end++ context "when index is out of bounds" do+ let(:params) { { index: 10 } }++ it { is_expected.to fail_to_find_a_model(:report) }+ end+ end++ context "when in january" do+ before do+ freeze_time DateTime.parse("2021-01-22")+ key = "rewind:#{current_user.username}:2020"+ reports = [{ type: "top_words", data: "report 1" }]+ Discourse.redis.setex(key, 3600, MultiJson.dump(reports))+ end++ it "fetches reports from the previous year" do+ expect(result.year).to eq(2020)+ expect(result).to be_success+ end+ end++ context "when out of valid months" do+ before { freeze_time DateTime.parse("2021-02-22") }++ it { is_expected.to fail_to_find_a_model(:year) }+ end++ context "in development mode" do+ before do+ Rails.env.stubs(:development?).returns(true)+ freeze_time DateTime.parse("2021-06-22")+ key = "rewind:#{current_user.username}:2021"+ reports = [{ type: "top_words", data: "report 1" }]+ Discourse.redis.setex(key, 3600, MultiJson.dump(reports))+ end++ it "finds the year no matter what month" do+ expect(result.year).to eq(2021)+ expect(result).to be_success+ end+ end++ context "when reports are not cached" do+ before { Discourse.redis.del("rewind:#{current_user.username}:2021") }++ it { is_expected.to fail_to_find_a_model(:all_reports) }+ end++ context "with for_user_username parameter" do+ fab!(:other_user, :user)+ fab!(:admin, :admin)++ before do+ freeze_time DateTime.parse("2021-12-22")+ key = "rewind:#{other_user.username}:2021"+ reports = [+ { type: "top_words", data: "other user report 1" },+ { type: "reading_time", data: "other user report 2" },+ ]+ Discourse.redis.setex(key, 3600, MultiJson.dump(reports))+ end++ context "when for_user_username is blank" do+ let(:params) { { index: 0, for_user_username: "" } }++ it "uses the guardian user" do+ expect(result).to be_success+ expect(result.for_user).to eq(current_user)+ end+ end++ context "when for_user_username is provided but user does not exist" do+ let(:params) { { index: 0, for_user_username: "nonexistent" } }++ it { is_expected.to fail_to_find_a_model(:for_user) }+ end++ context "when viewing own rewind" do+ let(:params) { { index: 0, for_user_username: current_user.username } }++ it "returns the user's report" do+ expect(result).to be_success+ expect(result.for_user).to eq(current_user)+ end+ end++ context "when viewing another user's rewind" do+ context "when the other user has sharing enabled" do+ before { other_user.user_option.update!(discourse_rewind_share_publicly: true) }++ let(:params) { { index: 0, for_user_username: other_user.username } }++ it "allows access to the report" do+ expect(result).to be_success+ expect(result.for_user).to eq(other_user)+ expect(result.report[:type]).to eq("top_words")+ expect(result.report[:data]).to eq("other user report 1")+ end++ context "when the other user has hide_profile enabled" do+ before { other_user.user_option.update!(hide_profile: true) }++ it { is_expected.to fail_to_find_a_model(:for_user) }+ end+ end++ context "when the other user has sharing disabled" do+ before { other_user.user_option.update!(discourse_rewind_share_publicly: false) }++ context "when guardian is an admin" do+ let(:guardian) { Guardian.new(admin) }+ let(:params) { { index: 0, for_user_username: other_user.username } }++ it "allows access to the report" do+ expect(result).to be_success+ expect(result.for_user).to eq(other_user)+ expect(result.report[:type]).to eq("top_words")+ expect(result.report[:data]).to eq("other user report 1")+ end+ end++ context "when guardian is not an admin" do+ let(:params) { { index: 0, for_user_username: other_user.username } }++ it { is_expected.to fail_to_find_a_model(:for_user) }+ end+ end+ end+ end+ end+enddiff --git a/plugins/discourse-rewind/spec/services/fetch_reports_spec.rb b/plugins/discourse-rewind/spec/services/fetch_reports_spec.rbindex beeb2dcddfe..1b569ded348 100644--- a/plugins/discourse-rewind/spec/services/fetch_reports_spec.rb+++ b/plugins/discourse-rewind/spec/services/fetch_reports_spec.rb@@ -9,6 +9,8 @@ RSpec.describe(DiscourseRewind::FetchReports) dolet(:guardian) { Guardian.new(current_user) }let(:dependencies) { { guardian: } }+ before { SiteSetting.discourse_rewind_enabled = true }+context "when in january" dobefore { freeze_time DateTime.parse("2021-01-22") }@@ -67,5 +69,75 @@ RSpec.describe(DiscourseRewind::FetchReports) doexpect(DiscourseRewind::Action::TopWords).to have_received(:call)endend++ context "with for_user_username parameter" do+ fab!(:other_user, :user)+ fab!(:admin, :admin)++ before { freeze_time DateTime.parse("2021-12-22") }++ context "when for_user_username is blank" do+ let(:dependencies) { { guardian:, params: { for_user_username: "" } } }++ it "uses the guardian user" do+ expect(result).to be_success+ expect(result.for_user).to eq(current_user)+ end+ end++ context "when for_user_username is provided but user does not exist" do+ let(:dependencies) { { guardian:, params: { for_user_username: "nonexistent" } } }++ it { is_expected.to fail_to_find_a_model(:for_user) }+ end++ context "when viewing own rewind" do+ let(:dependencies) { { guardian:, params: { for_user_username: current_user.username } } }++ it "returns the user's reports" do+ expect(result).to be_success+ expect(result.for_user).to eq(current_user)+ end+ end++ context "when viewing another user's rewind" do+ context "when the other user has sharing enabled" do+ before { other_user.user_option.update!(discourse_rewind_share_publicly: true) }++ let(:dependencies) { { guardian:, params: { for_user_username: other_user.username } } }++ it "allows access to the reports" do+ expect(result).to be_success+ expect(result.for_user).to eq(other_user)+ end++ context "when the other user has hide_profile enabled" do+ before { other_user.user_option.update!(hide_profile: true) }++ it { is_expected.to fail_to_find_a_model(:for_user) }+ end+ end++ context "when the other user has sharing disabled" do+ before { other_user.user_option.update!(discourse_rewind_share_publicly: false) }++ context "when guardian is an admin" do+ let(:guardian) { Guardian.new(admin) }+ let(:dependencies) { { guardian:, params: { for_user_username: other_user.username } } }++ it "allows access to the reports" do+ expect(result).to be_success+ expect(result.for_user).to eq(other_user)+ end+ end++ context "when guardian is not an admin" do+ let(:dependencies) { { guardian:, params: { for_user_username: other_user.username } } }++ it { is_expected.to fail_to_find_a_model(:for_user) }+ end+ end+ end+ endendenddiff --git a/plugins/discourse-rewind/spec/services/toggle_share_spec.rb b/plugins/discourse-rewind/spec/services/toggle_share_spec.rbnew file mode 100644index 00000000000..db306221b14--- /dev/null+++ b/plugins/discourse-rewind/spec/services/toggle_share_spec.rb@@ -0,0 +1,48 @@+# frozen_string_literal: true++RSpec.describe(DiscourseRewind::ToggleShare) do+ describe ".call" do+ subject(:result) { described_class.call(**dependencies) }++ fab!(:user)++ let(:guardian) { Guardian.new(user) }+ let(:dependencies) { { guardian: } }++ context "when discourse_rewind_share_publicly is false" do+ before { user.user_option.update!(discourse_rewind_share_publicly: false) }++ it "toggles to true" do+ expect { result }.to change {+ user.user_option.reload.discourse_rewind_share_publicly+ }.from(false).to(true)+ end++ context "when user has hide_profile set to true" do+ before { user.user_option.update!(hide_profile: true) }++ it { is_expected.to fail_a_policy(:user_not_hiding_profile) }+ end+ end++ context "when discourse_rewind_share_publicly is true" do+ before { user.user_option.update!(discourse_rewind_share_publicly: true) }++ it "toggles to false" do+ expect { result }.to change {+ user.user_option.reload.discourse_rewind_share_publicly+ }.from(true).to(false)+ end++ context "when user has hide_profile set to true" do+ before { user.user_option.update!(hide_profile: true) }++ it "toggles to false" do+ expect { result }.to change {+ user.user_option.reload.discourse_rewind_share_publicly+ }.from(true).to(false)+ end+ end+ end+ end+enddiff --git a/plugins/discourse-rewind/spec/system/page_objects/pages/rewind.rb b/plugins/discourse-rewind/spec/system/page_objects/pages/rewind.rbindex 1f441e19cf5..efe3f1fd1d6 100644--- a/plugins/discourse-rewind/spec/system/page_objects/pages/rewind.rb+++ b/plugins/discourse-rewind/spec/system/page_objects/pages/rewind.rb@@ -84,6 +84,52 @@ module PageObjectsdef has_no_rewind_profile_link?has_no_css?("#quick-access-profile a[href*='/activity/rewind']")end++ def visit_rewind(username)+ page.visit("/u/#{username}/activity/rewind")+ self+ end++ def share_toggle+ PageObjects::Components::DToggleSwitch.new(".rewind__share-toggle")+ end++ def has_share_toggle?+ has_css?(".rewind__share-toggle")+ end++ def has_no_share_toggle?+ has_no_css?(".rewind__share-toggle")+ end++ def has_copy_link_button?+ has_css?(".rewind__copy-link-btn")+ end++ def has_no_copy_link_button?+ has_no_css?(".rewind__copy-link-btn")+ end++ def click_copy_link_button+ find(".rewind__copy-link-btn").click+ self+ end++ def has_viewing_other_user_message?(username)+ has_css?(".rewind-other-user", text: username)+ end++ def has_cannot_view_rewind_error?+ has_css?(".rewind-error")+ end++ def has_no_cannot_view_rewind_error?+ has_no_css?(".rewind-error")+ end++ def has_rewind_loaded?+ has_css?(".rewind__scroll-wrapper")+ endendendenddiff --git a/plugins/discourse-rewind/spec/system/rewind_sharing_spec.rb b/plugins/discourse-rewind/spec/system/rewind_sharing_spec.rbnew file mode 100644index 00000000000..0081f4e9f82--- /dev/null+++ b/plugins/discourse-rewind/spec/system/rewind_sharing_spec.rb@@ -0,0 +1,222 @@+# frozen_string_literal: true++describe "DiscourseRewind | rewind sharing", type: :system do+ fab!(:current_user) { Fabricate(:user, refresh_auto_groups: true, trust_level: 2) }+ fab!(:other_user) { Fabricate(:user, refresh_auto_groups: true, trust_level: 2) }+ fab!(:admin)++ let(:rewind_page) { PageObjects::Pages::Rewind.new }+ let(:dialog) { PageObjects::Components::Dialog.new }+ let(:cdp) { PageObjects::CDP.new }++ before { SiteSetting.discourse_rewind_enabled = true }++ describe "sharing functionality" do+ context "when user shares their rewind if it's not already shared publicly" do+ before do+ sign_in(current_user)+ current_user.user_option.update!(discourse_rewind_share_publicly: false)+ freeze_time DateTime.parse("2022-12-22")+ end++ it "shows confirmation dialog and enables sharing" do+ rewind_page.visit_rewind(current_user.username)++ expect(rewind_page).to have_share_toggle+ expect(rewind_page.share_toggle).to be_unchecked+ expect(rewind_page).to have_no_copy_link_button+ expect(current_user.user_option.discourse_rewind_share_publicly).to eq(false)++ rewind_page.share_toggle.toggle++ expect(dialog).to be_open+ expect(dialog).to have_content(I18n.t("js.discourse_rewind.share.confirm"))++ dialog.click_yes++ current_user.reload+ expect(current_user.user_option.discourse_rewind_share_publicly).to eq(true)+ expect(rewind_page.share_toggle).to be_checked+ expect(rewind_page).to have_copy_link_button+ end++ it "does not enable sharing if user cancels confirmation" do+ rewind_page.visit_rewind(current_user.username)++ expect(rewind_page).to have_share_toggle+ expect(rewind_page.share_toggle).to be_unchecked+ expect(rewind_page).to have_no_copy_link_button+ expect(current_user.user_option.discourse_rewind_share_publicly).to eq(false)++ rewind_page.share_toggle.toggle++ expect(dialog).to be_open++ dialog.click_no++ current_user.reload+ expect(current_user.user_option.discourse_rewind_share_publicly).to eq(false)+ expect(rewind_page.share_toggle).to be_unchecked+ expect(rewind_page).to have_no_copy_link_button+ end++ it "unshares without confirmation when already shared" do+ current_user.user_option.update!(discourse_rewind_share_publicly: true)++ rewind_page.visit_rewind(current_user.username)++ expect(rewind_page).to have_share_toggle+ expect(rewind_page.share_toggle).to be_checked+ expect(rewind_page).to have_copy_link_button++ rewind_page.share_toggle.toggle++ current_user.reload+ expect(current_user.user_option.discourse_rewind_share_publicly).to eq(false)+ expect(rewind_page.share_toggle).to be_unchecked+ expect(rewind_page).to have_no_copy_link_button+ end++ context "when copying rewind link" do+ before { cdp.allow_clipboard }++ it "copies rewind link to clipboard when link button is clicked" do+ current_user.user_option.update!(discourse_rewind_share_publicly: true)++ rewind_page.visit_rewind(current_user.username)++ expect(rewind_page).to have_copy_link_button++ rewind_page.click_copy_link_button++ cdp.clipboard_has_text?("/u/#{current_user.username}/activity/rewind", strict: false)+ expect(PageObjects::Components::Toasts.new).to have_success(+ I18n.t("js.post.controls.link_copied"),+ )+ end++ it "does not show copy link button when sharing is disabled" do+ current_user.user_option.update!(discourse_rewind_share_publicly: false)++ rewind_page.visit_rewind(current_user.username)++ expect(rewind_page).to have_no_copy_link_button+ end+ end+ end++ context "when viewing another user's shared rewind" do+ before do+ sign_in(other_user)+ current_user.user_option.update!(discourse_rewind_share_publicly: true)+ freeze_time DateTime.parse("2022-12-22")+ end++ it "displays the rewind with a message indicating viewing other user" do+ rewind_page.visit_rewind(current_user.username)++ expect(rewind_page).to have_viewing_other_user_message(current_user.username)+ expect(rewind_page).to have_no_share_toggle+ expect(rewind_page).to have_no_copy_link_button+ expect(rewind_page).to have_no_cannot_view_rewind_error+ expect(rewind_page).to have_rewind_loaded+ end+ end++ context "when viewing another user's unshared rewind" do+ before do+ sign_in(other_user)+ current_user.user_option.update!(discourse_rewind_share_publicly: false)+ freeze_time DateTime.parse("2022-12-22")+ end++ it "shows error message and cannot view the rewind" do+ rewind_page.visit_rewind(current_user.username)++ expect(rewind_page).to have_cannot_view_rewind_error+ expect(rewind_page).to have_no_share_toggle+ expect(rewind_page).to have_no_copy_link_button+ end++ it "can view after current_user enables sharing" do+ rewind_page.visit_rewind(current_user.username)++ expect(rewind_page).to have_cannot_view_rewind_error++ current_user.user_option.update!(discourse_rewind_share_publicly: true)++ page.refresh++ expect(rewind_page).to have_no_cannot_view_rewind_error+ expect(rewind_page).to have_viewing_other_user_message(current_user.username)+ expect(rewind_page).to have_rewind_loaded+ end++ it "cannot view after current_user disables sharing" do+ current_user.user_option.update!(discourse_rewind_share_publicly: true)++ rewind_page.visit_rewind(current_user.username)++ expect(rewind_page).to have_no_cannot_view_rewind_error+ expect(rewind_page).to have_rewind_loaded++ current_user.user_option.update!(discourse_rewind_share_publicly: false)++ page.refresh++ expect(rewind_page).to have_cannot_view_rewind_error+ end+ end++ context "when admin views any user's rewind" do+ before do+ sign_in(admin)+ freeze_time DateTime.parse("2022-12-22")+ end++ it "can view rewind even when sharing is disabled" do+ current_user.user_option.update!(discourse_rewind_share_publicly: false)++ rewind_page.visit_rewind(current_user.username)++ expect(rewind_page).to have_viewing_other_user_message(current_user.username)+ expect(rewind_page).to have_no_share_toggle+ expect(rewind_page).to have_no_copy_link_button+ expect(rewind_page).to have_no_cannot_view_rewind_error+ expect(rewind_page).to have_rewind_loaded+ end++ it "can view rewind when sharing is enabled" do+ current_user.user_option.update!(discourse_rewind_share_publicly: true)++ rewind_page.visit_rewind(current_user.username)++ expect(rewind_page).to have_viewing_other_user_message(current_user.username)+ expect(rewind_page).to have_no_share_toggle+ expect(rewind_page).to have_no_copy_link_button+ expect(rewind_page).to have_no_cannot_view_rewind_error+ expect(rewind_page).to have_rewind_loaded+ end+ end++ context "when anonymous user attempts to view rewind" do+ before { freeze_time DateTime.parse("2022-12-22") }++ it "redirects to /latest when user's rewind is shared" do+ current_user.user_option.update!(discourse_rewind_share_publicly: true)++ rewind_page.visit_rewind(current_user.username)++ expect(page).to have_current_path("/latest")+ end++ it "redirects to /latest when user's rewind is not shared" do+ current_user.user_option.update!(discourse_rewind_share_publicly: false)++ rewind_page.visit_rewind(current_user.username)++ expect(page).to have_current_path("/latest")+ end+ end+ end+end