-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MAM-2669-breadcrumbs-and-transparency-foryou-posts (#193)
* save origin reason when adding status to a user's for you feed * fetch status origin api GET `https://staging.moth.social/api/v3/timelines/for_you/statuses/11129536085876024` * Trigger rebuild of FY when unsubscribing from smartlist
- Loading branch information
Showing
10 changed files
with
135 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::V3::Timelines::StatusesController < Api::BaseController | ||
before_action :require_mammoth! | ||
|
||
rescue_from Mammoth::StatusOrigin::NotFound do |e| | ||
render json: { error: e.to_s }, status: 404 | ||
end | ||
|
||
def show | ||
origin = Mammoth::StatusOrigin.instance | ||
@origins = origin.find(status_id_param) | ||
render json: @origins, each_serializer: StatusOriginSerializer | ||
end | ||
|
||
private | ||
|
||
def status_id_param | ||
params.require(:id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
# rubocop:disable all | ||
require 'singleton' | ||
|
||
module Mammoth | ||
|
||
class StatusOrigin | ||
include Singleton | ||
include Redisable | ||
class NotFound < StandardError; end | ||
|
||
|
||
# Add Status and Reason to list | ||
def add_channel(status, channel) | ||
list_key = key(status[:id]) | ||
reason = channel_reason(status, channel) | ||
|
||
add_reason(list_key, reason) | ||
end | ||
|
||
def add_mammoth_pick(status) | ||
list_key = key(status[:id]) | ||
reason = mammoth_pick_reason(status) | ||
|
||
add_reason(list_key, reason) | ||
end | ||
|
||
# Add reason by key id | ||
# Expire Reason in 7 days | ||
def add_reason(key, reason) | ||
redis.sadd(key, reason) | ||
redis.expire(key, 7.day.seconds) | ||
end | ||
|
||
def find(status_id) | ||
list_key = key(status_id) | ||
results = redis.smembers(list_key).map { |o| | ||
payload = Oj.load(o, symbol_keys: true) | ||
originating_account = Account.create(payload[:originating_account]) | ||
# StatusOrigin Active Model for serialization | ||
::StatusOrigin.new(source: payload[:source], channel_id: payload[:channel_id], title: payload[:title], originating_account:originating_account ) | ||
} | ||
# Throw Error if array find is empty | ||
raise NotFound, 'status not found' unless results.length > 0 | ||
return results | ||
end | ||
|
||
private | ||
|
||
# Redis key of a status | ||
# @param [Integer] status id | ||
# @param [Symbol] subtype | ||
# @return [String] | ||
def key(id, subtype = nil) | ||
return "origin:for_you:#{id}" unless subtype | ||
"origin:for_you:#{id}:#{subtype}" | ||
end | ||
|
||
def channel_reason(status, channel) | ||
Oj.dump({source: "SmartList", channel_id: channel[:id], title: channel[:title], originating_account: status.account}) | ||
end | ||
|
||
def mammoth_pick_reason(status) | ||
Oj.dump({source: "MammothPick", originating_account: status.account }) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# ActiveModel Only for Serialization | ||
class StatusOrigin | ||
include ActiveModel::Model | ||
include ActiveModel::Serialization | ||
|
||
attr_accessor :source, :channel_id, :title, :originating_account | ||
|
||
def initialize(attributes = {}) | ||
super | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Required Source & Originating Account | ||
# Channel_id & Title maybe be null | ||
class StatusOriginSerializer < ActiveModel::Serializer | ||
attributes :source, :title, :channel_id | ||
|
||
belongs_to :originating_account, serializer: REST::AccountSerializer | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Fabricator(:status_origin) do | ||
source "MyString" | ||
title "MyString" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe StatusOrigin, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |