~phw/discourse-listenbrainz

44c174c1bf30ae264826ee2c4a1fc58703239323 — Philipp Wolfer 1 year, 4 months ago a2c03f1
Remove ListenBrainz onebox implementation

The feature never worked as intended. Yet it duplicates a lot
of functionality and needed extra maintenance and testing whenever
styling got changed.
10 files changed, 6 insertions(+), 214 deletions(-)

M CHANGES.md
M README.md
M assets/stylesheets/listenbrainz.scss
M config/locales/server.de.yml
M config/locales/server.en.yml
M config/settings.yml
D lib/onebox/listenbrainz_profile_onebox.rb
D lib/settings/ranges_site_setting.rb
M plugin.rb
D templates/listenbrainzprofile.mustache
M CHANGES.md => CHANGES.md +4 -0
@@ 1,5 1,9 @@
# Discourse ListenBrainz Changelog

## 0.14 - unreleased
- Remove ListenBrainz onebox implementation


## 0.13.2 - 2023-05-15
- Truncate the displayed pin blurb after 280 characters
- Various layout tweaks for pinned recording card

M README.md => README.md +0 -4
@@ 18,10 18,6 @@ You can configure the ListenBrainz plugin in the Discourse admin interface. The 

- **listenbrainz poster icon**: Allow users to show a link to their ListenBrainz profile next to their username in each post. Default: enabled.

- **listenbrainz_onebox_enabled**: Enables Onebox support for URLs to ListenBrainz profile pages. This is an experimental feature. Default: disabled.

- **listenbrainz_onebox_range**: Statistic time range for ListenBrainz Oneboxes. Default: This month.


## User settings


M assets/stylesheets/listenbrainz.scss => assets/stylesheets/listenbrainz.scss +2 -49
@@ 1,7 1,6 @@
$border-radius: 8px;

.listenbrainz-card,
aside.onebox.listenbrainzprofile {
.listenbrainz-card {
    --text-color: var(--primary);
    --secondary-text-color: var(--primary-800);
    --background-color: var(--secondary);


@@ 17,8 16,7 @@ aside.onebox.listenbrainzprofile {
    height: 100%;
    font-size: 14px;

    .listenbrainz-card-main-content,
    .onebox-body {
    .listenbrainz-card-main-content {
        display: flex;
        flex-wrap: wrap;
        color: var(--text-color);


@@ 234,51 232,6 @@ svg.listenbrainz-statistics-background {
    margin-top: .5em;
}

aside.onebox.listenbrainzprofile {
    padding: 0;

    .source {
        color: var(--secondary);
        background-color: var(--tertiary);
        margin: 0;
        padding: 4px 14px 4px 14px;
        border-top-left-radius: $border-radius;
        border-top-right-radius: $border-radius;

        a {
            color: var(--secondary);
        }

        .site-icon {
            height: 16px;
            width: 16px;
            padding-right: 6px;
        }
    }

    .onebox-body {
        .listenbrainz-card-thumbnail {
            img, obj {
                border-top-left-radius: 0;
            }
        }

        .listenbrainz-card-background {
            position: absolute;
            bottom: 0;
            left: 66px;
            width: calc(100% - 66px - 1em);
            margin-left: .5em;
            height: 80%;
            z-index: 0;
        }

        .listenbrainz-card-details {
            z-index: 5;
        }
    }
}

.mobile-view .user-main .listenbrainz-card {
    margin-bottom: 16px;
}

M config/locales/server.de.yml => config/locales/server.de.yml +0 -16
@@ 3,19 3,3 @@ en:
    listenbrainz_enabled: Benutzern erlauben, ihren ListenBrainz-Status in ihren Profilen anzuzeigen
    listenbrainz_username_matches_discourse: Legt fest, ob der ListenBrainz-Benutzername standardmäßig auf den Discourse-Benutzernamen gesetzt werden soll
    listenbrainz_poster_icon: Zeige einen Icon-Link auf das ListenBrainz-Profil des Benutzer neben dem Benutzernamen
    listenbrainz_onebox_enabled: ListenBrainz Oneboxes aktivieren (experimentell)
    listenbrainz_onebox_range: "Statistik-Zeitraum für ListenBrainz Oneboxes"
  listenbrainz:
    profile:
      ranges:
        this_week: '%{count} Listens diese Woche'
        this_month: '%{count} Listens diesen Monat'
        this_year: '%{count} Listens dieses Jahr'
        all_time: '%{count} Listens bislang'
      top_recording: „%{title}“ von %{artist}
      top_recording_tooltip: '%{username}s meist gehörter Titel'
    range_options:
      this_week: Diese Woche
      this_month: Diesen Monat
      this_year: Dieses Jahr
      all_time: Insgesamt

M config/locales/server.en.yml => config/locales/server.en.yml +0 -16
@@ 3,19 3,3 @@ en:
    listenbrainz_enabled: "Allow users to show their ListenBrainz status in their profiles"
    listenbrainz_username_matches_discourse: "Whether the ListenBrainz username should default to the Discourse username"
    listenbrainz_poster_icon: "Show an icon link to the user’s ListenBrainz profile next to their poster name"
    listenbrainz_onebox_enabled: "Enable ListenBrainz Oneboxes (experimental)"
    listenbrainz_onebox_range: "Statistic time range for ListenBrainz Oneboxes"
  listenbrainz:
    profile:
      ranges:
        this_week: "%{count} listens this week"
        this_month: "%{count} listens this month"
        this_year: "%{count} listens this year"
        all_time: "%{count} listens so far"
      top_recording: "“%{title}” by %{artist}"
      top_recording_tooltip: "%{username}’s most listened to track"
    range_options:
      this_week: "This week"
      this_month: "This month"
      this_year: "This year"
      all_time: "All time"

M config/settings.yml => config/settings.yml +0 -7
@@ 8,10 8,3 @@ plugins:
  listenbrainz_poster_icon:
    default: true
    client: true
  listenbrainz_onebox_enabled:
    default: false
    client: false
  listenbrainz_onebox_range:
    default: 'this_month'
    enum: 'ListenBrainz::RangesSiteSetting'
    client: false

D lib/onebox/listenbrainz_profile_onebox.rb => lib/onebox/listenbrainz_profile_onebox.rb +0 -76
@@ 1,76 0,0 @@
require_relative '../helpers/profile_helper'
require_relative '../helpers/listen_helper'

module Onebox
    module Engine
        class ListenBrainzProfileOnebox
            include Engine
            include LayoutSupport

            COVERART_PLACEHOLDER_URL = '/plugins/listenbrainz/images/cover-art-placeholder.png'

            matches_regexp /^https?:\/\/(?<domain>listenbrainz\.org)\/user\/(?<username>[^\/]+)/i

            def self.===(other)
                return false if !enabled
                super
            end

            def self.enabled
                SiteSetting.listenbrainz_enabled && SiteSetting.listenbrainz_onebox_enabled
            end

            def match
                @match ||= @url.match(@@matcher)
            end

            def data
                profile = {
                    favicon: '/plugins/listenbrainz/images/listenbrainz.svg',
                    link: @url,
                    username: match[:username],
                    range: SiteSetting.listenbrainz_onebox_range || 'this_month',
                    thumbnail_url: COVERART_PLACEHOLDER_URL
                }

                # ListenBrainz::ProfileHelper::add_thumbnail(profile)
                ListenBrainz::ProfileHelper::load_stats(profile)
                ListenBrainz::ProfileHelper::precalculate_stats(profile)
                ListenBrainz::ProfileHelper::load_top_recording(profile)

                # Always use site's main locale for onebox texts
                I18n.with_locale SiteSetting.default_locale do
                    profile[:listen_count_label] = I18n.t(
                        "listenbrainz.profile.ranges.#{profile[:range]}",
                        count: format_number(profile[:listen_count]),
                    )

                    if profile[:top_recording]
                        top_recording = profile[:top_recording]
                        # ListenBrainz::ListenHelper::enhance_metadata(top_recording)
                        caa_release_mbid = top_recording['caa_release_mbid'] || top_recording['release_mbid']
                        cover_url = ListenBrainz::ListenHelper::coverart_url(caa_release_mbid)
                        profile[:thumbnail_url] = cover_url if cover_url

                        profile[:top_recording_label] = I18n.t(
                            "listenbrainz.profile.top_recording",
                            title: top_recording['track_name'],
                            artist: top_recording['artist_name'],
                        )

                        profile[:top_recording_tooltip] = I18n.t(
                            "listenbrainz.profile.top_recording_tooltip",
                            username: profile[:username],
                        )
                    end
                end

                profile
            end

            def format_number(number, thousands_separator="\u202F")
                number.to_s.reverse.scan(/\d{1,3}/).join(thousands_separator).reverse
            end
        end
    end
end

D lib/settings/ranges_site_setting.rb => lib/settings/ranges_site_setting.rb +0 -19
@@ 1,19 0,0 @@
require_relative '../const'
require_dependency 'enum_site_setting'

module ListenBrainz
    class RangesSiteSetting < EnumSiteSetting
        def self.valid_value?(val)
            values.any? { |v| v[:value].to_s == val.to_s }
        end

        def self.values
            @values ||= ListenBrainz::RANGES.map do |v|
                {
                    name: I18n.t("listenbrainz.range_options.#{v}"),
                    value: v
                }
            end
        end
    end
end

M plugin.rb => plugin.rb +0 -7
@@ 4,8 4,6 @@
# authors: Philipp Wolfer <ph.wolfer@gmail.com>
# url: https://git.sr.ht/~phw/discourse-listenbrainz

require_relative 'lib/settings/ranges_site_setting'

enabled_site_setting :listenbrainz_enabled

register_asset "stylesheets/listenbrainz.scss"


@@ 65,8 63,3 @@ after_initialize do
        get "listenbrainz/profile/:user_id" => "listenbrainz_profile#profile"
    end
end

# Load oneboxes
require 'onebox'
require_relative 'lib/onebox/listenbrainz_profile_onebox'
Onebox.options.load_paths.push(File.join(File.dirname(__FILE__), "templates"))

D templates/listenbrainzprofile.mustache => templates/listenbrainzprofile.mustache +0 -20
@@ 1,20 0,0 @@
<div class="listenbrainz-card-thumbnail">
    <img src="{{thumbnail_url}}">
</div>
<div class="listenbrainz-card-details">
    <div class="listenbrainz-card-primary">
        <div class="listenbrainz-card-title" title="{{username}}">
            <a href="{{link}}" target="_blank">
                {{username}}
            </a>
        </div>
        <div class="listenbrainz-card-duration">
            {{listen_count_label}}
        </div>
    </div>
    <div class="listenbrainz-card-secondary">
        <span title="{{top_recording_tooltip}}">
        ❤️ {{top_recording_label}}
        </span>
    </div>
</div>