Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Pleroma
pleroma
Commits
6cf7c132
Commit
6cf7c132
authored
Apr 27, 2017
by
dtluna
Browse files
Refactor code to comply with credo suggestions
parent
fb5cebc1
Changes
21
Hide whitespace changes
Inline
Side-by-side
.credo.exs
0 → 100644
View file @
6cf7c132
# This file contains the configuration for Credo and you are probably reading
# this after creating it with `mix credo.gen.config`.
#
# If you find anything wrong or unclear in this file, please report an
# issue on GitHub: https://github.com/rrrene/credo/issues
#
%{
#
# You can have as many configs as you like in the `configs:` field.
configs:
[
%{
#
# Run any config using `mix credo -C <name>`. If no config name is given
# "default" is used.
name:
"default"
,
#
# These are the files included in the analysis:
files:
%{
#
# You can give explicit globs or simply directories.
# In the latter case `**/*.{ex,exs}` will be used.
included:
[
"lib/"
,
"src/"
,
"web/"
,
"apps/"
],
excluded:
[
~r"/_build/"
,
~r"/deps/"
]
},
#
# If you create your own checks, you must specify the source files for
# them here, so they can be loaded by Credo before running the analysis.
requires:
[],
#
# Credo automatically checks for updates, like e.g. Hex does.
# You can disable this behaviour below:
check_for_updates:
true
,
#
# If you want to enforce a style guide and need a more traditional linting
# experience, you can change `strict` to `true` below:
strict:
false
,
#
# If you want to use uncolored output by default, you can change `color`
# to `false` below:
color:
true
,
#
# You can customize the parameters of any check by adding a second element
# to the tuple.
#
# To disable a check put `false` as second element:
#
# {Credo.Check.Design.DuplicatedCode, false}
#
checks:
[
{
Credo
.
Check
.
Consistency
.
ExceptionNames
},
{
Credo
.
Check
.
Consistency
.
LineEndings
},
{
Credo
.
Check
.
Consistency
.
MultiAliasImportRequireUse
},
{
Credo
.
Check
.
Consistency
.
ParameterPatternMatching
},
{
Credo
.
Check
.
Consistency
.
SpaceAroundOperators
},
{
Credo
.
Check
.
Consistency
.
SpaceInParentheses
},
{
Credo
.
Check
.
Consistency
.
TabsOrSpaces
},
# For some checks, like AliasUsage, you can only customize the priority
# Priority values are: `low, normal, high, higher`
{
Credo
.
Check
.
Design
.
AliasUsage
,
priority:
:low
},
# For others you can set parameters
# If you don't want the `setup` and `test` macro calls in ExUnit tests
# or the `schema` macro in Ecto schemas to trigger DuplicatedCode, just
# set the `excluded_macros` parameter to `[:schema, :setup, :test]`.
{
Credo
.
Check
.
Design
.
DuplicatedCode
,
excluded_macros:
[]},
# You can also customize the exit_status of each check.
# If you don't want TODO comments to cause `mix credo` to fail, just
# set this value to 0 (zero).
{
Credo
.
Check
.
Design
.
TagTODO
,
exit_status:
2
},
{
Credo
.
Check
.
Design
.
TagFIXME
},
{
Credo
.
Check
.
Readability
.
FunctionNames
},
{
Credo
.
Check
.
Readability
.
LargeNumbers
},
{
Credo
.
Check
.
Readability
.
MaxLineLength
,
priority:
:low
,
max_length:
100
},
{
Credo
.
Check
.
Readability
.
ModuleAttributeNames
},
{
Credo
.
Check
.
Readability
.
ModuleDoc
,
false
},
{
Credo
.
Check
.
Readability
.
ModuleNames
},
{
Credo
.
Check
.
Readability
.
ParenthesesOnZeroArityDefs
},
{
Credo
.
Check
.
Readability
.
ParenthesesInCondition
},
{
Credo
.
Check
.
Readability
.
PredicateFunctionNames
},
{
Credo
.
Check
.
Readability
.
PreferImplicitTry
},
{
Credo
.
Check
.
Readability
.
RedundantBlankLines
},
{
Credo
.
Check
.
Readability
.
StringSigils
},
{
Credo
.
Check
.
Readability
.
TrailingBlankLine
},
{
Credo
.
Check
.
Readability
.
TrailingWhiteSpace
},
{
Credo
.
Check
.
Readability
.
VariableNames
},
{
Credo
.
Check
.
Readability
.
Semicolons
},
{
Credo
.
Check
.
Readability
.
SpaceAfterCommas
},
{
Credo
.
Check
.
Refactor
.
DoubleBooleanNegation
},
{
Credo
.
Check
.
Refactor
.
CondStatements
},
{
Credo
.
Check
.
Refactor
.
CyclomaticComplexity
},
{
Credo
.
Check
.
Refactor
.
FunctionArity
},
{
Credo
.
Check
.
Refactor
.
MatchInCondition
},
{
Credo
.
Check
.
Refactor
.
NegatedConditionsInUnless
},
{
Credo
.
Check
.
Refactor
.
NegatedConditionsWithElse
},
{
Credo
.
Check
.
Refactor
.
Nesting
},
{
Credo
.
Check
.
Refactor
.
PipeChainStart
},
{
Credo
.
Check
.
Refactor
.
UnlessWithElse
},
{
Credo
.
Check
.
Warning
.
BoolOperationOnSameValues
},
{
Credo
.
Check
.
Warning
.
IExPry
},
{
Credo
.
Check
.
Warning
.
IoInspect
},
{
Credo
.
Check
.
Warning
.
LazyLogging
},
{
Credo
.
Check
.
Warning
.
OperationOnSameValues
},
{
Credo
.
Check
.
Warning
.
OperationWithConstantResult
},
{
Credo
.
Check
.
Warning
.
UnusedEnumOperation
},
{
Credo
.
Check
.
Warning
.
UnusedFileOperation
},
{
Credo
.
Check
.
Warning
.
UnusedKeywordOperation
},
{
Credo
.
Check
.
Warning
.
UnusedListOperation
},
{
Credo
.
Check
.
Warning
.
UnusedPathOperation
},
{
Credo
.
Check
.
Warning
.
UnusedRegexOperation
},
{
Credo
.
Check
.
Warning
.
UnusedStringOperation
},
{
Credo
.
Check
.
Warning
.
UnusedTupleOperation
},
# Controversial and experimental checks (opt-in, just remove `, false`)
#
{
Credo
.
Check
.
Refactor
.
ABCSize
,
false
},
{
Credo
.
Check
.
Refactor
.
AppendSingleItem
,
false
},
{
Credo
.
Check
.
Refactor
.
VariableRebinding
,
false
},
{
Credo
.
Check
.
Warning
.
MapGetUnsafePass
,
false
},
# Deprecated checks (these will be deleted after a grace period)
{
Credo
.
Check
.
Readability
.
Specs
,
false
},
{
Credo
.
Check
.
Warning
.
NameRedeclarationByAssignment
,
false
},
{
Credo
.
Check
.
Warning
.
NameRedeclarationByCase
,
false
},
{
Credo
.
Check
.
Warning
.
NameRedeclarationByDef
,
false
},
{
Credo
.
Check
.
Warning
.
NameRedeclarationByFn
,
false
},
# Custom checks can be created using `mix credo.gen.check`.
#
]
}
]
}
lib/pleroma/plugs/authentication_plug.ex
View file @
6cf7c132
defmodule
Pleroma
.
Plugs
.
AuthenticationPlug
do
alias
Comeonin
.
Pbkdf2
import
Plug
.
Conn
def
init
(
options
)
do
...
...
@@ -25,12 +26,12 @@ defp verify(%{id: id} = user, _password, id) do
end
defp
verify
(
nil
,
_password
,
_user_id
)
do
Comeonin
.
Pbkdf2
.
dummy_checkpw
Pbkdf2
.
dummy_checkpw
:error
end
defp
verify
(
user
,
password
,
_user_id
)
do
if
Comeonin
.
Pbkdf2
.
checkpw
(
password
,
user
.
password_hash
)
do
if
Pbkdf2
.
checkpw
(
password
,
user
.
password_hash
)
do
{
:ok
,
user
}
else
:error
...
...
@@ -42,7 +43,7 @@ defp decode_header(conn) do
{
:ok
,
userinfo
}
<-
Base
.
decode64
(
header
),
[
username
,
password
]
<-
String
.
split
(
userinfo
,
":"
)
do
{
:ok
,
username
,
password
}
{
:ok
,
username
,
password
}
end
end
...
...
lib/pleroma/upload.ex
View file @
6cf7c132
defmodule
Pleroma
.
Upload
do
alias
Ecto
.
UUID
alias
Pleroma
.
Web
def
store
(%
Plug
.
Upload
{}
=
file
)
do
uuid
=
Ecto
.
UUID
.
generate
uuid
=
UUID
.
generate
upload_folder
=
Path
.
join
(
upload_path
(),
uuid
)
File
.
mkdir_p!
(
upload_folder
)
result_file
=
Path
.
join
(
upload_folder
,
file
.
filename
)
...
...
@@ -21,7 +23,7 @@ def store(%Plug.Upload{} = file) do
def
store
(%{
"img"
=>
"data:image/"
<>
image_data
})
do
parsed
=
Regex
.
named_captures
(
~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/
,
image_data
)
data
=
Base
.
decode64!
(
parsed
[
"data"
])
uuid
=
Ecto
.
UUID
.
generate
uuid
=
UUID
.
generate
upload_folder
=
Path
.
join
(
upload_path
(),
uuid
)
File
.
mkdir_p!
(
upload_folder
)
filename
=
Base
.
encode16
(
:crypto
.
hash
(
:sha256
,
data
))
<>
".
#{
parsed
[
"filetype"
]
}
"
...
...
@@ -44,11 +46,11 @@ def store(%{"img" => "data:image/" <> image_data}) do
end
defp
upload_path
do
Application
.
get_env
(
:pleroma
,
Pleroma
.
Upload
)
|>
Keyword
.
fetch!
(
:uploads
)
settings
=
Application
.
get_env
(
:pleroma
,
Pleroma
.
Upload
)
Keyword
.
fetch!
(
settings
,
:uploads
)
end
defp
url_for
(
file
)
do
"
#{
Pleroma
.
Web
.
base_url
()
}
/media/
#{
file
}
"
"
#{
Web
.
base_url
()
}
/media/
#{
file
}
"
end
end
lib/pleroma/user.ex
View file @
6cf7c132
defmodule
Pleroma
.
User
do
use
Ecto
.
Schema
import
Ecto
.
Changeset
import
Ecto
.
Query
alias
Pleroma
.
{
Repo
,
User
,
Activity
,
Object
}
import
Ecto
.
{
Changeset
,
Query
}
alias
Pleroma
.
{
Repo
,
User
,
Object
,
Web
}
alias
Comeonin
.
Pbkdf2
schema
"users"
do
field
:bio
,
:string
...
...
@@ -12,7 +12,7 @@ defmodule Pleroma.User do
field
:password_hash
,
:string
field
:password
,
:string
,
virtual:
true
field
:password_confirmation
,
:string
,
virtual:
true
field
:following
,
{
:array
,
:string
},
default:
[]
field
:following
,
{
:array
,
:string
},
default:
[]
field
:ap_id
,
:string
field
:avatar
,
:map
...
...
@@ -27,7 +27,7 @@ def avatar_url(user) do
end
def
ap_id
(%
User
{
nickname:
nickname
})
do
"
#{
Pleroma
.
Web
.
base_url
}
/users/
#{
nickname
}
"
"
#{
Web
.
base_url
}
/users/
#{
nickname
}
"
end
def
ap_followers
(%
User
{}
=
user
)
do
...
...
@@ -66,7 +66,7 @@ def register_changeset(struct, params \\ %{}) do
|>
validate_format
(
:nickname
,
~r/^[a-zA-Z\d]+$/
)
if
changeset
.
valid?
do
hashed
=
Comeonin
.
Pbkdf2
.
hashpwsalt
(
changeset
.
changes
[
:password
])
hashed
=
Pbkdf2
.
hashpwsalt
(
changeset
.
changes
[
:password
])
ap_id
=
User
.
ap_id
(%
User
{
nickname:
changeset
.
changes
[
:nickname
]})
followers
=
User
.
ap_followers
(%
User
{
nickname:
changeset
.
changes
[
:nickname
]})
changeset
...
...
@@ -81,8 +81,8 @@ def register_changeset(struct, params \\ %{}) do
def
follow
(%
User
{}
=
follower
,
%
User
{}
=
followed
)
do
ap_followers
=
User
.
ap_followers
(
followed
)
if
following?
(
follower
,
followed
)
do
{
:error
,
"Could not follow user:
#{
followed
.
nickname
}
is already on your list."
}
{
:error
,
"Could not follow user:
#{
followed
.
nickname
}
is already on your list."
}
else
following
=
[
ap_followers
|
follower
.
following
]
|>
Enum
.
uniq
...
...
@@ -103,7 +103,7 @@ def unfollow(%User{} = follower, %User{} = followed) do
|>
follow_changeset
(%{
following:
following
})
|>
Repo
.
update
else
{
:error
,
"Not subscribed!"
}
{
:error
,
"Not subscribed!"
}
end
end
...
...
lib/pleroma/web/activity_pub/activity_pub.ex
View file @
6cf7c132
defmodule
Pleroma
.
Web
.
ActivityPub
.
ActivityPub
do
alias
Pleroma
.
Repo
alias
Pleroma
.
{
Activity
,
Object
,
Upload
,
User
}
alias
Pleroma
.
{
Activity
,
Repo
,
Object
,
Upload
,
User
,
Web
}
alias
Ecto
.
{
Changeset
,
UUID
}
import
Ecto
.
Query
def
insert
(
map
)
when
is_map
(
map
)
do
...
...
@@ -19,7 +19,7 @@ def insert(map) when is_map(map) do
Repo
.
insert
(%
Activity
{
data:
map
})
end
def
like
(%
User
{
ap_id:
ap_id
}
=
user
,
%
Object
{
data:
%{
"id"
=>
id
}}
=
object
)
do
def
like
(%
User
{
ap_id:
ap_id
}
=
user
,
%
Object
{
data:
%{
"id"
=>
id
}}
=
object
)
do
cond
do
# There's already a like here, so return the original activity.
ap_id
in
(
object
.
data
[
"likes"
]
||
[])
->
...
...
@@ -44,7 +44,7 @@ def like(%User{ap_id: ap_id} = user, %Object{data: %{ "id" => id}} = object) do
|>
Map
.
put
(
"like_count"
,
length
(
likes
))
|>
Map
.
put
(
"likes"
,
likes
)
changeset
=
Ecto
.
Changeset
.
change
(
object
,
data:
new_data
)
changeset
=
Changeset
.
change
(
object
,
data:
new_data
)
{
:ok
,
object
}
=
Repo
.
update
(
changeset
)
update_object_in_activities
(
object
)
...
...
@@ -58,7 +58,7 @@ defp update_object_in_activities(%{data: %{"id" => id}} = object) do
relevant_activities
=
Activity
.
all_by_object_ap_id
(
id
)
Enum
.
map
(
relevant_activities
,
fn
(
activity
)
->
new_activity_data
=
activity
.
data
|>
Map
.
put
(
"object"
,
object
.
data
)
changeset
=
Ecto
.
Changeset
.
change
(
activity
,
data:
new_activity_data
)
changeset
=
Changeset
.
change
(
activity
,
data:
new_activity_data
)
Repo
.
update
(
changeset
)
end
)
end
...
...
@@ -79,7 +79,7 @@ def unlike(%User{ap_id: ap_id}, %Object{data: %{ "id" => id}} = object) do
|>
Map
.
put
(
"like_count"
,
length
(
likes
))
|>
Map
.
put
(
"likes"
,
likes
)
changeset
=
Ecto
.
Changeset
.
change
(
object
,
data:
new_data
)
changeset
=
Changeset
.
change
(
object
,
data:
new_data
)
{
:ok
,
object
}
=
Repo
.
update
(
changeset
)
update_object_in_activities
(
object
)
...
...
@@ -103,7 +103,7 @@ def generate_object_id do
end
def
generate_id
(
type
)
do
"
#{
Pleroma
.
Web
.
base_url
()
}
/
#{
type
}
/
#{
Ecto
.
UUID
.
generate
}
"
"
#{
Web
.
base_url
()
}
/
#{
type
}
/
#{
UUID
.
generate
}
"
end
def
fetch_public_activities
(
opts
\\
%{})
do
...
...
@@ -140,8 +140,7 @@ def fetch_activities(recipients, opts \\ %{}) do
query
end
Repo
.
all
(
query
)
|>
Enum
.
reverse
Enum
.
reverse
(
Repo
.
all
(
query
))
end
def
announce
(%
User
{
ap_id:
ap_id
}
=
user
,
%
Object
{
data:
%{
"id"
=>
id
}}
=
object
)
do
...
...
@@ -160,7 +159,7 @@ def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object)
|>
Map
.
put
(
"announcement_count"
,
length
(
announcements
))
|>
Map
.
put
(
"announcements"
,
announcements
)
changeset
=
Ecto
.
Changeset
.
change
(
object
,
data:
new_data
)
changeset
=
Changeset
.
change
(
object
,
data:
new_data
)
{
:ok
,
object
}
=
Repo
.
update
(
changeset
)
update_object_in_activities
(
object
)
...
...
lib/pleroma/web/ostatus/activity_representer.ex
View file @
6cf7c132
...
...
@@ -23,5 +23,5 @@ def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user)
]
++
attachments
end
def
to_simple_form
(
_
,
_
),
do
:
nil
def
to_simple_form
(
_
,
_
),
do
:
nil
end
lib/pleroma/web/ostatus/feed_representer.ex
View file @
6cf7c132
...
...
@@ -8,7 +8,8 @@ def to_simple_form(user, activities, users) do
h
=
fn
(
str
)
->
[
to_charlist
(
str
)]
end
entries
=
Enum
.
map
(
activities
,
fn
(
activity
)
->
entries
=
activities
|>
Enum
.
map
(
fn
(
activity
)
->
{
:entry
,
ActivityRepresenter
.
to_simple_form
(
activity
,
user
)}
end
)
|>
Enum
.
filter
(
fn
({
_
,
form
})
->
form
end
)
...
...
lib/pleroma/web/ostatus/ostatus_controller.ex
View file @
6cf7c132
...
...
@@ -16,7 +16,8 @@ def feed(conn, %{"nickname" => nickname}) do
activities
=
query
|>
Repo
.
all
response
=
FeedRepresenter
.
to_simple_form
(
user
,
activities
,
[
user
])
response
=
user
|>
FeedRepresenter
.
to_simple_form
(
activities
,
[
user
])
|>
:xmerl
.
export_simple
(
:xmerl_xml
)
|>
to_string
...
...
@@ -25,7 +26,7 @@ def feed(conn, %{"nickname" => nickname}) do
|>
send_resp
(
200
,
response
)
end
def
temp
(
conn
,
params
)
do
def
temp
(
_
conn
,
params
)
do
IO
.
inspect
(
params
)
end
end
lib/pleroma/web/ostatus/user_representer.ex
View file @
6cf7c132
...
...
@@ -7,14 +7,14 @@ def to_simple_form(user) do
bio
=
to_charlist
(
user
.
bio
)
avatar_url
=
to_charlist
(
User
.
avatar_url
(
user
))
[
{
:id
,
[
ap_id
]
},
{
:"activity:object"
,
[
'http://activitystrea.ms/schema/1.0/person'
]
},
{
:uri
,
[
ap_id
]
},
{
:"poco:preferredUsername"
,
[
nickname
]
},
{
:"poco:displayName"
,
[
name
]
},
{
:"poco:note"
,
[
bio
]
},
{
:name
,
[
nickname
]
},
{
:link
,
[
rel:
'avatar'
,
href:
avatar_url
],
[]}
{
:id
,
[
ap_id
]},
{
:"activity:object"
,
[
'http://activitystrea.ms/schema/1.0/person'
]},
{
:uri
,
[
ap_id
]},
{
:"poco:preferredUsername"
,
[
nickname
]},
{
:"poco:displayName"
,
[
name
]},
{
:"poco:note"
,
[
bio
]},
{
:name
,
[
nickname
]},
{
:link
,
[
rel:
'avatar'
,
href:
avatar_url
],
[]}
]
end
end
lib/pleroma/web/router.ex
View file @
6cf7c132
defmodule
Pleroma
.
Web
.
Router
do
use
Pleroma
.
Web
,
:router
alias
Pleroma
.
{
Repo
,
User
}
alias
Pleroma
.
{
Repo
,
User
,
Web
.
Router
}
def
user_fetcher
(
username
)
do
{
:ok
,
Repo
.
get_by
(
User
,
%{
nickname:
username
})}
...
...
@@ -10,13 +10,13 @@ def user_fetcher(username) do
pipeline
:api
do
plug
:accepts
,
[
"json"
]
plug
:fetch_session
plug
Pleroma
.
Plugs
.
AuthenticationPlug
,
%{
fetcher:
&
Pleroma
.
Web
.
Router
.
user_fetcher
/
1
,
optional:
true
}
plug
Pleroma
.
Plugs
.
AuthenticationPlug
,
%{
fetcher:
&
Router
.
user_fetcher
/
1
,
optional:
true
}
end
pipeline
:authenticated_api
do
plug
:accepts
,
[
"json"
]
plug
:fetch_session
plug
Pleroma
.
Plugs
.
AuthenticationPlug
,
%{
fetcher:
&
Pleroma
.
Web
.
Router
.
user_fetcher
/
1
}
plug
Pleroma
.
Plugs
.
AuthenticationPlug
,
%{
fetcher:
&
Router
.
user_fetcher
/
1
}
end
pipeline
:well_known
do
...
...
lib/pleroma/web/salmon/salmon.ex
View file @
6cf7c132
...
...
@@ -10,7 +10,6 @@ def decode(salmon) do
{
:xmlObj
,
:string
,
encoding
}
=
:xmerl_xpath
.
string
(
'string(//me:encoding[1])'
,
doc
)
{
:xmlObj
,
:string
,
type
}
=
:xmerl_xpath
.
string
(
'string(//me:data[1]/@type)'
,
doc
)
{
:ok
,
data
}
=
Base
.
url_decode64
(
to_string
(
data
),
ignore:
:whitespace
)
{
:ok
,
sig
}
=
Base
.
url_decode64
(
to_string
(
sig
),
ignore:
:whitespace
)
alg
=
to_string
(
alg
)
...
...
lib/pleroma/web/twitter_api/representers/activity_representer.ex
View file @
6cf7c132
defmodule
Pleroma
.
Web
.
TwitterAPI
.
Representers
.
ActivityRepresenter
do
use
Pleroma
.
Web
.
TwitterAPI
.
Representers
.
BaseRepresenter
alias
Pleroma
.
Web
.
TwitterAPI
.
Representers
.
{
UserRepresenter
,
ObjectRepresenter
}
alias
Pleroma
.
Activity
alias
Pleroma
.
{
Activity
,
User
}
alias
Calendar
.
Strftime
defp
user_by_ap_id
(
user_list
,
ap_id
)
do
Enum
.
find
(
user_list
,
fn
(%{
ap_id:
user_id
})
->
ap_id
==
user_id
end
)
end
def
to_map
(%
Activity
{
data:
%{
"type"
=>
"Announce"
,
"actor"
=>
actor
}}
=
activity
,
%{
users:
users
,
announced_activity:
announced_activity
}
=
opts
)
do
def
to_map
(%
Activity
{
data:
%{
"type"
=>
"Announce"
,
"actor"
=>
actor
,
"published"
=>
created_at
}}
=
activity
,
%{
users:
users
,
announced_activity:
announced_activity
}
=
opts
)
do
user
=
user_by_ap_id
(
users
,
actor
)
created_at
=
get_in
(
activity
.
data
,
[
"published"
])
|>
date_to_asctime
created_at
=
created_at
|>
date_to_asctime
text
=
"
#{
user
.
nickname
}
retweeted a status."
...
...
@@ -30,16 +30,16 @@ def to_map(%Activity{data: %{"type" => "Announce", "actor" => actor}} = activity
}
end
def
to_map
(%
Activity
{
data:
%{
"type"
=>
"Like"
}}
=
activity
,
%{
user:
user
,
liked_activity:
liked_activity
}
=
opts
)
do
created_at
=
get_in
(
activity
.
data
,
[
"published"
])
|>
date_to_asctime
def
to_map
(%
Activity
{
data:
%{
"type"
=>
"Like"
,
"published"
=>
created_at
}}
=
activity
,
%{
user:
user
,
liked_activity:
liked_activity
}
=
opts
)
do
created_at
=
created_at
|>
date_to_asctime
text
=
"
#{
user
.
nickname
}
favorited a status."
%{
"id"
=>
activity
.
id
,
"user"
=>
UserRepresenter
.
to_map
(
user
,
opts
),
"statusnet_html"
=>
text
,
# TODO: add summary
"statusnet_html"
=>
text
,
"text"
=>
text
,
"is_local"
=>
true
,
"is_post_verb"
=>
false
,
...
...
@@ -49,16 +49,17 @@ def to_map(%Activity{data: %{"type" => "Like"}} = activity, %{user: user, liked_
}
end
def
to_map
(%
Activity
{
data:
%{
"type"
=>
"Follow"
}}
=
activity
,
%{
user:
user
}
=
opts
)
do
created_at
=
get_in
(
activity
.
data
,
[
"published"
])
|>
date_to_asctime
def
to_map
(%
Activity
{
data:
%{
"type"
=>
"Follow"
,
"published"
=>
created_at
,
"object"
=>
followed_id
}}
=
activity
,
%{
user:
user
}
=
opts
)
do
created_at
=
created_at
|>
date_to_asctime
followed
=
User
.
get_cached_by_ap_id
(
followed_id
)
text
=
"
#{
user
.
nickname
}
started following
#{
followed
.
nickname
}
"
%{
"id"
=>
activity
.
id
,
"user"
=>
UserRepresenter
.
to_map
(
user
,
opts
),
"attentions"
=>
[],
"statusnet_html"
=>
""
,
# TODO: add summary
"text"
=>
""
,
"statusnet_html"
=>
text
,
"text"
=>
text
,
"is_local"
=>
true
,
"is_post_verb"
=>
false
,
"created_at"
=>
created_at
,
...
...
@@ -66,14 +67,12 @@ def to_map(%Activity{data: %{"type" => "Follow"}} = activity, %{user: user} = op
}
end
def
to_map
(%
Activity
{}
=
activity
,
%{
user:
user
}
=
opts
)
do
content
=
get_in
(
activity
.
data
,
[
"object"
,
"content"
])
created_at
=
get_in
(
activity
.
data
,
[
"object"
,
"published"
])
|>
date_to_asctime
like_count
=
get_in
(
activity
.
data
,
[
"object"
,
"like_count"
])
||
0
announcement_count
=
get_in
(
activity
.
data
,
[
"object"
,
"announcement_count"
])
||
0
favorited
=
opts
[
:for
]
&&
opts
[
:for
]
.
ap_id
in
(
activity
.
data
[
"object"
][
"likes"
]
||
[])
repeated
=
opts
[
:for
]
&&
opts
[
:for
]
.
ap_id
in
(
activity
.
data
[
"object"
][
"announcements"
]
||
[])
def
to_map
(%
Activity
{
data:
%{
"object"
=>
%{
"content"
=>
content
}
=
object
}}
=
activity
,
%{
user:
user
}
=
opts
)
do
created_at
=
object
[
"published"
]
|>
date_to_asctime
like_count
=
object
[
"like_count"
]
||
0
announcement_count
=
object
[
"announcement_count"
]
||
0
favorited
=
opts
[
:for
]
&&
opts
[
:for
]
.
ap_id
in
(
object
[
"likes"
]
||
[])
repeated
=
opts
[
:for
]
&&
opts
[
:for
]
.
ap_id
in
(
object
[
"announcements"
]
||
[])
mentions
=
opts
[
:mentioned
]
||
[]
...
...
@@ -91,22 +90,34 @@ def to_map(%Activity{} = activity, %{user: user} = opts) do
"is_local"
=>
true
,
"is_post_verb"
=>
true
,
"created_at"
=>
created_at
,
"in_reply_to_status_id"
=>
activity
.
data
[
"
object
"
]
[
"inReplyToStatusId"
],
"statusnet_conversation_id"
=>
activity
.
data
[
"
object
"
]
[
"statusnetConversationId"
],
"attachments"
=>
(
activity
.
data
[
"
object
"
]
[
"attachment"
]
||
[])
|>
ObjectRepresenter
.
enum_to_list
(
opts
),
"in_reply_to_status_id"
=>
object
[
"inReplyToStatusId"
],
"statusnet_conversation_id"
=>
object
[
"statusnetConversationId"
],
"attachments"
=>
(
object
[
"attachment"
]
||
[])
|>
ObjectRepresenter
.
enum_to_list
(
opts
),
"attentions"
=>
attentions
,
"fave_num"
=>
like_count
,
"repeat_num"
=>
announcement_count
,
"favorited"
=>
!!
favorited
,
"repeated"
=>
!!
repeated
,
"favorited"
=>
to_boolean
(
favorited
)
,
"repeated"
=>
to_boolean
(
repeated
)
,
}
end
defp
date_to_asctime
(
date
)
do
with
{
:ok
,
date
,
_offset
}
<-
date
|>
DateTime
.
from_iso8601
do
Calendar
.
Strftime
.
strftime!
(
date
,
"%a %b %d %H:%M:%S %z %Y"
)
Strftime
.
strftime!
(
date
,
"%a %b %d %H:%M:%S %z %Y"
)
else
_e
->
""
end
end
defp
to_boolean
(
false
)
do
false
end
defp
to_boolean
(
nil
)
do
false
end
defp
to_boolean
(
_
)
do
true
end
end
lib/pleroma/web/twitter_api/twitter_api.ex
View file @
6cf7c132
defmodule
Pleroma
.
Web
.
TwitterAPI
.
TwitterAPI
do
alias
Ecto
.
Changeset
alias
Pleroma
.
{
User
,
Activity
,
Repo
,
Object
}
alias
Pleroma
.
Web
.
ActivityPub
.
ActivityPub
alias
Pleroma
.
Web
.
{
ActivityPub
.
ActivityPub
,
Websub
,
OStatus
}
alias
Pleroma
.
Web
.
TwitterAPI
.
Representers
.
{
ActivityRepresenter
,
UserRepresenter
}
import
Ecto
.
Query
def
create_status
(
u
ser
=
%
U
ser
{},
data
=
%{}
)
do
def
create_status
(
%
U
ser
{}
=
u
ser
,
%{}
=
data
)
do
attachments
=
Enum
.
map
(
data
[
"media_ids"
]
||
[],
fn
(
media_id
)
->
Repo
.
get
(
Object
,
media_id
)
.
data
end
)
context
=
ActivityPub
.
generate_context_id
content
=
HtmlSanitizeEx
.
strip_tags
(
data
[
"status"
])
|>
String
.
replace
(
"
\n
"
,
"<br>"
)
content
=
data
[
"status"
]
|>
HtmlSanitizeEx
.
strip_tags
|>
String
.
replace
(
"
\n
"
,
"<br>"
)
mentions
=
parse_mentions
(
content
)
...
...
@@ -40,10 +40,10 @@ def create_status(user = %User{}, data = %{}) do
"context"
=>
context
,
"attachment"
=>
attachments
,
"actor"
=>
user
.
ap_id
},
},
"published"
=>
date
,
"context"
=>
context
}
}
# Wire up reply info.
activity
=
with
inReplyToId
when
not
is_nil
(
inReplyToId
)
<-
data
[
"in_reply_to_status_id"
],
...
...
@@ -67,34 +67,34 @@ def create_status(user = %User{}, data = %{}) do