Exclude reblogs from `GET /api/pleroma/admin/statuses` by default
[akkoma] / lib / pleroma / bbs / handler.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.BBS.Handler do
6 use Sshd.ShellHandler
7 alias Pleroma.Activity
8 alias Pleroma.HTML
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.Web.CommonAPI
11
12 def on_shell(username, _pubkey, _ip, _port) do
13 :ok = IO.puts("Welcome to #{Pleroma.Config.get([:instance, :name])}!")
14 user = Pleroma.User.get_cached_by_nickname(to_string(username))
15 Logger.debug("#{inspect(user)}")
16 loop(run_state(user: user))
17 end
18
19 def on_connect(username, ip, port, method) do
20 Logger.debug(fn ->
21 """
22 Incoming SSH shell #{inspect(self())} requested for #{username} from #{inspect(ip)}:#{
23 inspect(port)
24 } using #{inspect(method)}
25 """
26 end)
27 end
28
29 def on_disconnect(username, ip, port) do
30 Logger.debug(fn ->
31 "Disconnecting SSH shell for #{username} from #{inspect(ip)}:#{inspect(port)}"
32 end)
33 end
34
35 defp loop(state) do
36 self_pid = self()
37 counter = state.counter
38 prefix = state.prefix
39 user = state.user
40
41 input = spawn(fn -> io_get(self_pid, prefix, counter, user.nickname) end)
42 wait_input(state, input)
43 end
44
45 def puts_activity(activity) do
46 status = Pleroma.Web.MastodonAPI.StatusView.render("show.json", %{activity: activity})
47 IO.puts("-- #{status.id} by #{status.account.display_name} (#{status.account.acct})")
48 IO.puts(HTML.strip_tags(status.content))
49 IO.puts("")
50 end
51
52 def handle_command(state, "help") do
53 IO.puts("Available commands:")
54 IO.puts("help - This help")
55 IO.puts("home - Show the home timeline")
56 IO.puts("p <text> - Post the given text")
57 IO.puts("r <id> <text> - Reply to the post with the given id")
58 IO.puts("quit - Quit")
59
60 state
61 end
62
63 def handle_command(%{user: user} = state, "r " <> text) do
64 text = String.trim(text)
65 [activity_id, rest] = String.split(text, " ", parts: 2)
66
67 with %Activity{} <- Activity.get_by_id(activity_id),
68 {:ok, _activity} <-
69 CommonAPI.post(user, %{"status" => rest, "in_reply_to_status_id" => activity_id}) do
70 IO.puts("Replied!")
71 else
72 _e -> IO.puts("Could not reply...")
73 end
74
75 state
76 end
77
78 def handle_command(%{user: user} = state, "p " <> text) do
79 text = String.trim(text)
80
81 with {:ok, _activity} <- CommonAPI.post(user, %{"status" => text}) do
82 IO.puts("Posted!")
83 else
84 _e -> IO.puts("Could not post...")
85 end
86
87 state
88 end
89
90 def handle_command(state, "home") do
91 user = state.user
92
93 params =
94 %{}
95 |> Map.put("type", ["Create"])
96 |> Map.put("blocking_user", user)
97 |> Map.put("muting_user", user)
98 |> Map.put("user", user)
99
100 activities =
101 [user.ap_id | Pleroma.User.following(user)]
102 |> ActivityPub.fetch_activities(params)
103
104 Enum.each(activities, fn activity ->
105 puts_activity(activity)
106 end)
107
108 state
109 end
110
111 def handle_command(state, command) do
112 IO.puts("Unknown command '#{command}'")
113 state
114 end
115
116 defp wait_input(state, input) do
117 receive do
118 {:input, ^input, "quit\n"} ->
119 IO.puts("Exiting...")
120
121 {:input, ^input, code} when is_binary(code) ->
122 code = String.trim(code)
123
124 state = handle_command(state, code)
125
126 loop(%{state | counter: state.counter + 1})
127
128 {:error, :interrupted} ->
129 IO.puts("Caught Ctrl+C...")
130 loop(%{state | counter: state.counter + 1})
131
132 {:input, ^input, msg} ->
133 :ok = Logger.warn("received unknown message: #{inspect(msg)}")
134 loop(%{state | counter: state.counter + 1})
135 end
136 end
137
138 defp run_state(opts) do
139 %{prefix: "pleroma", counter: 1, user: opts[:user]}
140 end
141
142 defp io_get(pid, prefix, counter, username) do
143 prompt = prompt(prefix, counter, username)
144 send(pid, {:input, self(), IO.gets(:stdio, prompt)})
145 end
146
147 defp prompt(prefix, counter, username) do
148 prompt = "#{username}@#{prefix}:#{counter}>"
149 prompt <> " "
150 end
151 end