WIP notification settings
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -69,7 +69,8 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Comment do
|
||||
mentions: mentions,
|
||||
local: is_nil(actor_domain),
|
||||
visibility: if(Visibility.is_public?(object), do: :public, else: :private),
|
||||
published_at: object["published"]
|
||||
published_at: object["published"],
|
||||
is_announcement: Map.get(object, "isAnnouncement", false)
|
||||
}
|
||||
|
||||
Logger.debug("Converted object before fetching parents")
|
||||
@@ -109,7 +110,8 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Comment do
|
||||
"uuid" => comment.uuid,
|
||||
"id" => comment.url,
|
||||
"tag" => build_mentions(comment.mentions) ++ build_tags(comment.tags),
|
||||
"published" => comment.published_at |> DateTime.to_iso8601()
|
||||
"published" => comment.published_at |> DateTime.to_iso8601(),
|
||||
"isAnnouncement" => comment.is_announcement
|
||||
}
|
||||
|
||||
object =
|
||||
|
||||
26
lib/graphql/resolvers/users/activity_settings.ex
Normal file
26
lib/graphql/resolvers/users/activity_settings.ex
Normal file
@@ -0,0 +1,26 @@
|
||||
defmodule Mobilizon.GraphQL.Resolvers.Users.ActivitySettings do
|
||||
@moduledoc """
|
||||
Handles the user activity settings-related GraphQL calls.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Users
|
||||
alias Mobilizon.Users.User
|
||||
|
||||
require Logger
|
||||
|
||||
def user_activity_settings(_parent, _args, %{context: %{current_user: %User{} = user}}) do
|
||||
{:ok, Users.activity_settings_for_user(user)}
|
||||
end
|
||||
|
||||
def user_activity_settings(_parent, _args, _context) do
|
||||
{:error, :unauthenticated}
|
||||
end
|
||||
|
||||
def upsert_user_activity_setting(_parent, args, %{context: %{current_user: %User{id: user_id}}}) do
|
||||
Users.create_activity_setting(Map.put(args, :user_id, user_id))
|
||||
end
|
||||
|
||||
def upsert_user_activity_setting(_parent, _args, _resolution) do
|
||||
{:error, :unauthenticated}
|
||||
end
|
||||
end
|
||||
@@ -48,6 +48,7 @@ defmodule Mobilizon.GraphQL.Schema do
|
||||
import_types(Schema.AdminType)
|
||||
import_types(Schema.StatisticsType)
|
||||
import_types(Schema.Users.PushSubscription)
|
||||
import_types(Schema.Users.ActivitySetting)
|
||||
|
||||
@desc "A struct containing the id of the deleted object"
|
||||
object :deleted_object do
|
||||
@@ -182,6 +183,7 @@ defmodule Mobilizon.GraphQL.Schema do
|
||||
import_fields(:actor_mutations)
|
||||
import_fields(:follower_mutations)
|
||||
import_fields(:push_mutations)
|
||||
import_fields(:activity_setting_mutations)
|
||||
end
|
||||
|
||||
@desc """
|
||||
|
||||
@@ -50,6 +50,10 @@ defmodule Mobilizon.GraphQL.Schema.Discussions.CommentType do
|
||||
field(:updated_at, :datetime, description: "When was the comment updated")
|
||||
field(:deleted_at, :datetime, description: "When was the comment deleted")
|
||||
field(:published_at, :datetime, description: "When was the comment published")
|
||||
|
||||
field(:is_announcement, non_null(:boolean),
|
||||
description: "Whether this comment needs to be announced to participants"
|
||||
)
|
||||
end
|
||||
|
||||
@desc "The list of visibility options for a comment"
|
||||
@@ -86,6 +90,8 @@ defmodule Mobilizon.GraphQL.Schema.Discussions.CommentType do
|
||||
arg(:event_id, non_null(:id), description: "The event under which this comment is")
|
||||
arg(:in_reply_to_comment_id, :id, description: "The comment ID this one replies to")
|
||||
|
||||
arg(:is_announcement, :boolean, description: "Should this comment be announced to everyone?")
|
||||
|
||||
resolve(&Comment.create_comment/3)
|
||||
end
|
||||
|
||||
@@ -94,6 +100,8 @@ defmodule Mobilizon.GraphQL.Schema.Discussions.CommentType do
|
||||
arg(:text, non_null(:string), description: "The comment updated body")
|
||||
arg(:comment_id, non_null(:id), description: "The comment ID")
|
||||
|
||||
arg(:is_announcement, :boolean, description: "Should this comment be announced to everyone?")
|
||||
|
||||
resolve(&Comment.update_comment/3)
|
||||
end
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ defmodule Mobilizon.GraphQL.Schema.UserType do
|
||||
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.GraphQL.Resolvers.{Media, User}
|
||||
alias Mobilizon.GraphQL.Resolvers.Users.ActivitySettings
|
||||
alias Mobilizon.GraphQL.Schema
|
||||
|
||||
import_types(Schema.SortType)
|
||||
@@ -131,6 +132,11 @@ defmodule Mobilizon.GraphQL.Schema.UserType do
|
||||
resolve: &Media.user_size/3,
|
||||
description: "The total size of all the media from this user (from all their actors)"
|
||||
)
|
||||
|
||||
field(:activity_settings, list_of(:activity_setting),
|
||||
resolve: &ActivitySettings.user_activity_settings/3,
|
||||
description: "The user's activity settings"
|
||||
)
|
||||
end
|
||||
|
||||
@desc "The list of roles an user can have"
|
||||
|
||||
23
lib/graphql/schema/users/activity_setting.ex
Normal file
23
lib/graphql/schema/users/activity_setting.ex
Normal file
@@ -0,0 +1,23 @@
|
||||
defmodule Mobilizon.GraphQL.Schema.Users.ActivitySetting do
|
||||
@moduledoc """
|
||||
Schema representation for PushSubscription
|
||||
"""
|
||||
use Absinthe.Schema.Notation
|
||||
alias Mobilizon.GraphQL.Resolvers.Users.ActivitySettings
|
||||
|
||||
object :activity_setting do
|
||||
field(:key, :string)
|
||||
field(:method, :string)
|
||||
field(:enabled, :boolean)
|
||||
field(:user, :user)
|
||||
end
|
||||
|
||||
object :activity_setting_mutations do
|
||||
field :update_activity_setting, :activity_setting do
|
||||
arg(:key, non_null(:string))
|
||||
arg(:method, non_null(:string))
|
||||
arg(:enabled, non_null(:boolean))
|
||||
resolve(&ActivitySettings.upsert_user_activity_setting/3)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -17,7 +17,7 @@ defmodule Mobilizon.Activities do
|
||||
very_high: 50
|
||||
)
|
||||
|
||||
@activity_types ["event", "post", "discussion", "resource", "group", "member"]
|
||||
@activity_types ["event", "post", "discussion", "resource", "group", "member", "comment"]
|
||||
@event_activity_subjects ["event_created", "event_updated", "event_deleted", "comment_posted"]
|
||||
@post_activity_subjects ["post_created", "post_updated", "post_deleted"]
|
||||
@discussion_activity_subjects [
|
||||
|
||||
@@ -45,6 +45,7 @@ defmodule Mobilizon.Discussions.Comment do
|
||||
:attributed_to_id,
|
||||
:deleted_at,
|
||||
:local,
|
||||
:is_announcement,
|
||||
:discussion_id
|
||||
]
|
||||
@attrs @required_attrs ++ @optional_attrs
|
||||
@@ -58,6 +59,7 @@ defmodule Mobilizon.Discussions.Comment do
|
||||
field(:total_replies, :integer, virtual: true, default: 0)
|
||||
field(:deleted_at, :utc_datetime)
|
||||
field(:published_at, :utc_datetime)
|
||||
field(:is_announcement, :boolean, default: false)
|
||||
|
||||
belongs_to(:actor, Actor, foreign_key: :actor_id)
|
||||
belongs_to(:attributed_to, Actor, foreign_key: :attributed_to_id)
|
||||
|
||||
34
lib/mobilizon/users/activity_setting.ex
Normal file
34
lib/mobilizon/users/activity_setting.ex
Normal file
@@ -0,0 +1,34 @@
|
||||
defmodule Mobilizon.Users.ActivitySetting do
|
||||
@moduledoc """
|
||||
Module to manage users settings
|
||||
"""
|
||||
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
alias Mobilizon.Users.User
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
key: String.t(),
|
||||
method: String.t(),
|
||||
enabled: boolean()
|
||||
}
|
||||
|
||||
@attrs [:key, :method, :enabled, :user_id]
|
||||
|
||||
@primary_key {:user_id, :id, autogenerate: false}
|
||||
schema "user_activity_settings" do
|
||||
field(:key, :string)
|
||||
field(:method, :string)
|
||||
field(:enabled, :boolean)
|
||||
|
||||
belongs_to(:user, User, primary_key: true, type: :id, foreign_key: :id, define_field: false)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(activity_setting, attrs) do
|
||||
activity_setting
|
||||
|> cast(attrs, @attrs)
|
||||
|> validate_required(@attrs)
|
||||
|> unique_constraint([:key, :method], name: :user_activity_settings_user_id_key_method_index)
|
||||
end
|
||||
end
|
||||
@@ -13,7 +13,7 @@ defmodule Mobilizon.Users do
|
||||
alias Mobilizon.{Crypto, Events}
|
||||
alias Mobilizon.Events.FeedToken
|
||||
alias Mobilizon.Storage.{Page, Repo}
|
||||
alias Mobilizon.Users.{PushSubscription, Setting, User}
|
||||
alias Mobilizon.Users.{ActivitySetting, PushSubscription, Setting, User}
|
||||
|
||||
defenum(UserRole, :user_role, [:administrator, :moderator, :user])
|
||||
|
||||
@@ -478,6 +478,48 @@ defmodule Mobilizon.Users do
|
||||
Repo.delete(push_subscription)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lists the activity settings for an user
|
||||
|
||||
## Examples
|
||||
|
||||
iex> activity_settings_for_user(user)
|
||||
[%ActivitySetting{}]
|
||||
|
||||
iex> activity_settings_for_user(user)
|
||||
[]
|
||||
|
||||
"""
|
||||
def activity_settings_for_user(%User{id: user_id}) do
|
||||
ActivitySetting
|
||||
|> where([a], a.user_id == ^user_id)
|
||||
|> Repo.all()
|
||||
end
|
||||
|
||||
def activity_setting(%User{id: user_id}, key, method) do
|
||||
ActivitySetting
|
||||
|> where([a], a.user_id == ^user_id and a.key == ^key and a.method == ^method)
|
||||
|> Repo.one()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates an activity setting. Overrides existing values if present
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_activity_setting(%{field: value})
|
||||
{:ok, %ActivitySetting{}}
|
||||
|
||||
iex> create_activity_setting(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_activity_setting(attrs \\ %{}) do
|
||||
%ActivitySetting{}
|
||||
|> ActivitySetting.changeset(attrs)
|
||||
|> Repo.insert(on_conflict: :replace_all, conflict_target: [:user_id, :key, :method])
|
||||
end
|
||||
|
||||
@spec user_by_email_query(String.t(), boolean | nil, boolean()) :: Ecto.Query.t()
|
||||
defp user_by_email_query(email, activated, unconfirmed) do
|
||||
User
|
||||
|
||||
@@ -2,12 +2,12 @@ defmodule Mobilizon.Service.Activity.Comment do
|
||||
@moduledoc """
|
||||
Insert a comment activity
|
||||
"""
|
||||
alias Mobilizon.{Actors, Discussions, Events}
|
||||
alias Mobilizon.{Discussions, Events}
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Discussions.Comment
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Service.Activity
|
||||
alias Mobilizon.Service.Workers.ActivityBuilder
|
||||
alias Mobilizon.Service.Workers.{ActivityBuilder, LegacyNotifierBuilder}
|
||||
|
||||
@behaviour Activity
|
||||
|
||||
@@ -17,33 +17,21 @@ defmodule Mobilizon.Service.Activity.Comment do
|
||||
def insert_activity(
|
||||
%Comment{
|
||||
actor_id: actor_id,
|
||||
event_id: event_id,
|
||||
in_reply_to_comment_id: in_reply_to_comment_id
|
||||
event_id: event_id
|
||||
} = comment,
|
||||
options
|
||||
)
|
||||
when not is_nil(actor_id) and not is_nil(event_id) do
|
||||
with {:ok, %Event{attributed_to: %Actor{type: :Group} = group} = event} <-
|
||||
Events.get_event_with_preload(event_id),
|
||||
%Actor{id: actor_id} <- Actors.get_actor(actor_id),
|
||||
subject <- Keyword.fetch!(options, :subject) do
|
||||
ActivityBuilder.enqueue(:build_activity, %{
|
||||
"type" => "event",
|
||||
"subject" => subject,
|
||||
"subject_params" => %{
|
||||
event_title: event.title,
|
||||
event_uuid: event.uuid,
|
||||
comment_reply_to: !is_nil(in_reply_to_comment_id)
|
||||
},
|
||||
"group_id" => group.id,
|
||||
"author_id" => actor_id,
|
||||
"object_type" => "comment",
|
||||
"object_id" => to_string(comment.id),
|
||||
"inserted_at" => DateTime.utc_now()
|
||||
})
|
||||
else
|
||||
# Event not from group
|
||||
{:ok, %Event{}} -> {:ok, nil}
|
||||
with {:ok, %Event{} = event} <-
|
||||
Events.get_event_with_preload(event_id) do
|
||||
# Notify the actors mentionned
|
||||
notify_mentionned(comment, event)
|
||||
|
||||
# Notify participants if there's a new announcement
|
||||
notify_announcement(comment, event)
|
||||
|
||||
# Notify event organizer or group that there's new comments
|
||||
notify_organizer(comment, event, options)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -53,4 +41,116 @@ defmodule Mobilizon.Service.Activity.Comment do
|
||||
def get_object(comment_id) do
|
||||
Discussions.get_comment(comment_id)
|
||||
end
|
||||
|
||||
defp notify_mentionned(%Comment{actor_id: actor_id, id: comment_id, mentions: mentions}, %Event{
|
||||
uuid: uuid,
|
||||
title: title
|
||||
})
|
||||
when length(mentions) > 0 do
|
||||
LegacyNotifierBuilder.enqueue(:legacy_notify, %{
|
||||
"type" => :comment,
|
||||
"subject" => :event_comment_mention,
|
||||
"subject_params" => %{
|
||||
event_uuid: uuid,
|
||||
event_title: title
|
||||
},
|
||||
"author_id" => actor_id,
|
||||
"object_type" => :comment,
|
||||
"object_id" => to_string(comment_id),
|
||||
"inserted_at" => DateTime.utc_now(),
|
||||
"mentions" => Enum.map(mentions, & &1.actor_id)
|
||||
})
|
||||
end
|
||||
|
||||
defp notify_mentionned(_, _), do: {:ok, :skipped}
|
||||
|
||||
defp notify_announcement(
|
||||
%Comment{actor_id: actor_id, is_announcement: true, id: comment_id},
|
||||
%Event{
|
||||
id: event_id,
|
||||
uuid: uuid,
|
||||
title: title
|
||||
}
|
||||
) do
|
||||
LegacyNotifierBuilder.enqueue(:legacy_notify, %{
|
||||
"type" => :comment,
|
||||
"subject" => :participation_event_comment,
|
||||
"subject_params" => %{
|
||||
event_id: event_id,
|
||||
event_uuid: uuid,
|
||||
event_title: title
|
||||
},
|
||||
"author_id" => actor_id,
|
||||
"object_type" => :comment,
|
||||
"object_id" => to_string(comment_id),
|
||||
"inserted_at" => DateTime.utc_now()
|
||||
})
|
||||
end
|
||||
|
||||
defp notify_announcement(_, _), do: {:ok, :skipped}
|
||||
|
||||
@spec notify_organizer(Comment.t(), Event.t(), Keyword.t()) ::
|
||||
{:ok, Oban.Job.t()} | {:ok, :skipped}
|
||||
defp notify_organizer(
|
||||
%Comment{
|
||||
actor_id: actor_id,
|
||||
is_announcement: true,
|
||||
in_reply_to_comment_id: in_reply_to_comment_id,
|
||||
id: comment_id
|
||||
},
|
||||
%Event{
|
||||
uuid: uuid,
|
||||
title: title,
|
||||
attributed_to: %Actor{type: :Group, id: group_id}
|
||||
},
|
||||
options
|
||||
) do
|
||||
ActivityBuilder.enqueue(:build_activity, %{
|
||||
"type" => "event",
|
||||
"subject" => Keyword.fetch!(options, :subject),
|
||||
"subject_params" => %{
|
||||
event_title: title,
|
||||
event_uuid: uuid,
|
||||
comment_reply_to: !is_nil(in_reply_to_comment_id)
|
||||
},
|
||||
"group_id" => group_id,
|
||||
"author_id" => actor_id,
|
||||
"object_type" => "comment",
|
||||
"object_id" => to_string(comment_id),
|
||||
"inserted_at" => DateTime.utc_now()
|
||||
})
|
||||
end
|
||||
|
||||
defp notify_organizer(
|
||||
%Comment{
|
||||
actor_id: actor_id,
|
||||
is_announcement: true,
|
||||
in_reply_to_comment_id: in_reply_to_comment_id,
|
||||
id: comment_id
|
||||
},
|
||||
%Event{
|
||||
uuid: uuid,
|
||||
title: title,
|
||||
attributed_to: nil,
|
||||
organizer_actor_id: organizer_actor_id
|
||||
},
|
||||
_options
|
||||
)
|
||||
when actor_id !== organizer_actor_id do
|
||||
LegacyNotifierBuilder.enqueue(:legacy_notify, %{
|
||||
"type" => :comment,
|
||||
"subject" => :event_new_comment,
|
||||
"subject_params" => %{
|
||||
event_title: title,
|
||||
event_uuid: uuid,
|
||||
comment_reply_to: !is_nil(in_reply_to_comment_id)
|
||||
},
|
||||
"author_id" => actor_id,
|
||||
"object_type" => :comment,
|
||||
"object_id" => to_string(comment_id),
|
||||
"inserted_at" => DateTime.utc_now()
|
||||
})
|
||||
end
|
||||
|
||||
defp notify_organizer(_, _, _), do: {:ok, :skipped}
|
||||
end
|
||||
|
||||
111
lib/service/activity/renderer/comment.ex
Normal file
111
lib/service/activity/renderer/comment.ex
Normal file
@@ -0,0 +1,111 @@
|
||||
defmodule Mobilizon.Service.Activity.Renderer.Comment do
|
||||
@moduledoc """
|
||||
Insert a comment activity
|
||||
"""
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.Activity.Renderer
|
||||
alias Mobilizon.Web.{Endpoint, Gettext}
|
||||
alias Mobilizon.Web.Router.Helpers, as: Routes
|
||||
import Mobilizon.Web.Gettext, only: [dgettext: 3]
|
||||
|
||||
@behaviour Renderer
|
||||
|
||||
@impl Renderer
|
||||
def render(%Activity{} = activity, options) do
|
||||
locale = Keyword.get(options, :locale, "en")
|
||||
Gettext.put_locale(locale)
|
||||
profile = profile(activity)
|
||||
|
||||
case activity.subject do
|
||||
:event_comment_mention ->
|
||||
%{
|
||||
body:
|
||||
dgettext(
|
||||
"activity",
|
||||
"%{profile} mentionned you in a comment under event %{event}.",
|
||||
%{
|
||||
profile: profile,
|
||||
event: event_title(activity)
|
||||
}
|
||||
),
|
||||
url: event_url(activity)
|
||||
}
|
||||
|
||||
:participation_event_comment ->
|
||||
%{
|
||||
body:
|
||||
dgettext(
|
||||
"activity",
|
||||
"%{profile} has posted an announcement under event %{event}.",
|
||||
%{
|
||||
profile: profile,
|
||||
event: event_title(activity)
|
||||
}
|
||||
),
|
||||
url: event_url(activity)
|
||||
}
|
||||
|
||||
:discussion_mention ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} mentionned you in the discussion %{discussion}.", %{
|
||||
profile: profile,
|
||||
discussion: title(activity)
|
||||
}),
|
||||
url: discussion_url(activity)
|
||||
}
|
||||
|
||||
:discussion_renamed ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} renamed the discussion %{discussion}.", %{
|
||||
profile: profile,
|
||||
discussion: title(activity)
|
||||
}),
|
||||
url: discussion_url(activity)
|
||||
}
|
||||
|
||||
:discussion_archived ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} archived the discussion %{discussion}.", %{
|
||||
profile: profile,
|
||||
discussion: title(activity)
|
||||
}),
|
||||
url: discussion_url(activity)
|
||||
}
|
||||
|
||||
:discussion_deleted ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} deleted the discussion %{discussion}.", %{
|
||||
profile: profile,
|
||||
discussion: title(activity)
|
||||
}),
|
||||
url: nil
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
defp discussion_url(activity) do
|
||||
Routes.page_url(
|
||||
Endpoint,
|
||||
:discussion,
|
||||
Actor.preferred_username_and_domain(activity.group),
|
||||
activity.subject_params["discussion_slug"]
|
||||
)
|
||||
end
|
||||
|
||||
defp event_url(activity) do
|
||||
Routes.page_url(
|
||||
Endpoint,
|
||||
:event,
|
||||
activity.subject_params["event_uuid"]
|
||||
)
|
||||
end
|
||||
|
||||
defp profile(activity), do: Actor.display_name_and_username(activity.author)
|
||||
defp event_title(activity), do: activity.subject_params["event_title"]
|
||||
defp title(activity), do: activity.subject_params["discussion_title"]
|
||||
end
|
||||
@@ -5,7 +5,17 @@ defmodule Mobilizon.Service.Activity.Renderer do
|
||||
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.Service.Activity.Renderer.{Discussion, Event, Group, Member, Post, Resource}
|
||||
|
||||
alias Mobilizon.Service.Activity.Renderer.{
|
||||
Comment,
|
||||
Discussion,
|
||||
Event,
|
||||
Group,
|
||||
Member,
|
||||
Post,
|
||||
Resource
|
||||
}
|
||||
|
||||
require Logger
|
||||
import Mobilizon.Web.Gettext, only: [dgettext: 3]
|
||||
|
||||
@@ -41,6 +51,7 @@ defmodule Mobilizon.Service.Activity.Renderer do
|
||||
:member -> Member.render(activity, options)
|
||||
:post -> Post.render(activity, options)
|
||||
:resource -> Resource.render(activity, options)
|
||||
:comment -> Comment.render(activity, options)
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,7 +5,7 @@ defmodule Mobilizon.Service.Notifier.Email do
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.{Config, Users}
|
||||
alias Mobilizon.Service.Notifier
|
||||
alias Mobilizon.Service.Notifier.Email
|
||||
alias Mobilizon.Service.Notifier.{Email, Filter}
|
||||
alias Mobilizon.Users.{NotificationPendingNotificationDelay, Setting, User}
|
||||
alias Mobilizon.Web.Email.Activity, as: EmailActivity
|
||||
alias Mobilizon.Web.Email.Mailer
|
||||
@@ -17,6 +17,8 @@ defmodule Mobilizon.Service.Notifier.Email do
|
||||
Config.get(__MODULE__, :enabled)
|
||||
end
|
||||
|
||||
def send(user, activity, options \\ [])
|
||||
|
||||
@impl Notifier
|
||||
def send(%User{} = user, %Activity{} = activity, options) do
|
||||
Email.send(user, [activity], options)
|
||||
@@ -25,7 +27,9 @@ defmodule Mobilizon.Service.Notifier.Email do
|
||||
@impl Notifier
|
||||
def send(%User{email: email, locale: locale} = user, activities, options)
|
||||
when is_list(activities) do
|
||||
if can_send?(user) do
|
||||
activities = Enum.filter(activities, &can_send_activity?(&1, user))
|
||||
|
||||
if can_send?(user) && length(activities) > 0 do
|
||||
email
|
||||
|> EmailActivity.direct_activity(activities, Keyword.put(options, :locale, locale))
|
||||
|> Mailer.send_email()
|
||||
@@ -37,6 +41,34 @@ defmodule Mobilizon.Service.Notifier.Email do
|
||||
end
|
||||
end
|
||||
|
||||
@spec can_send_activity?(Activity.t(), User.t()) :: boolean()
|
||||
defp can_send_activity?(%Activity{} = activity, %User{} = user) do
|
||||
Filter.can_send_activity?(activity, "email", user, &default_activity_behavior/1)
|
||||
end
|
||||
|
||||
@spec default_activity_behavior(String.t()) :: boolean()
|
||||
defp default_activity_behavior(activity_setting) do
|
||||
case activity_setting do
|
||||
"participation_event_updated" -> true
|
||||
"participation_event_comment" -> true
|
||||
"event_new_pending_participation" -> true
|
||||
"event_new_participation" -> false
|
||||
"event_created" -> false
|
||||
"event_updated" -> false
|
||||
"discussion_updated" -> false
|
||||
"post_published" -> false
|
||||
"post_updated" -> false
|
||||
"resource_updated" -> false
|
||||
"member_request" -> true
|
||||
"member_updated" -> false
|
||||
"user_email_password_updated" -> true
|
||||
"event_comment_mention" -> true
|
||||
"discussion_mention" -> true
|
||||
"event_new_comment" -> true
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
@type notification_type ::
|
||||
:group_notifications
|
||||
| :notification_pending_participation
|
||||
|
||||
60
lib/service/notifier/filter.ex
Normal file
60
lib/service/notifier/filter.ex
Normal file
@@ -0,0 +1,60 @@
|
||||
defmodule Mobilizon.Service.Notifier.Filter do
|
||||
alias Mobilizon.Users
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.Users.{ActivitySetting, User}
|
||||
|
||||
@type method :: String.t()
|
||||
|
||||
@spec can_send_activity?(Activity.t(), method(), User.t(), function()) :: boolean()
|
||||
def can_send_activity?(%Activity{} = activity, method, %User{} = user, get_default) do
|
||||
case map_activity_to_activity_setting(activity) do
|
||||
false -> false
|
||||
key -> user |> Users.activity_setting(key, method) |> enabled?(key, get_default)
|
||||
end
|
||||
end
|
||||
|
||||
@spec enabled?(ActivitySetting.t() | nil, String.t(), function()) :: boolean()
|
||||
defp enabled?(nil, activity_setting, get_default), do: get_default.(activity_setting)
|
||||
defp enabled?(%ActivitySetting{enabled: enabled}, _activity_setting, _get_default), do: enabled
|
||||
|
||||
# Comment mention
|
||||
defp map_activity_to_activity_setting(%Activity{subject: :event_comment_mention}),
|
||||
do: "event_comment_mention"
|
||||
|
||||
# Participation
|
||||
@spec map_activity_to_activity_setting(Activity.t()) :: String.t() | false
|
||||
defp map_activity_to_activity_setting(%Activity{subject: :participation_event_updated}),
|
||||
do: "participation_event_updated"
|
||||
|
||||
defp map_activity_to_activity_setting(%Activity{subject: :participation_event_comment}),
|
||||
do: "participation_event_comment"
|
||||
|
||||
# Organizers
|
||||
defp map_activity_to_activity_setting(%Activity{subject: :event_new_pending_participation}),
|
||||
do: "event_new_pending_participation"
|
||||
|
||||
defp map_activity_to_activity_setting(%Activity{subject: :event_new_participation}),
|
||||
do: "event_new_participation"
|
||||
|
||||
# Event
|
||||
defp map_activity_to_activity_setting(%Activity{subject: :event_created}), do: "event_created"
|
||||
defp map_activity_to_activity_setting(%Activity{type: :event}), do: "event_updated"
|
||||
|
||||
# Post
|
||||
defp map_activity_to_activity_setting(%Activity{subject: :post_created}), do: "post_published"
|
||||
defp map_activity_to_activity_setting(%Activity{type: :post}), do: "post_updated"
|
||||
|
||||
# Discussion
|
||||
defp map_activity_to_activity_setting(%Activity{type: :discussion}), do: "discussion_updated"
|
||||
|
||||
# Resource
|
||||
defp map_activity_to_activity_setting(%Activity{type: :resource}), do: "resource_updated"
|
||||
|
||||
# Member
|
||||
defp map_activity_to_activity_setting(%Activity{subject: :member_request}),
|
||||
do: "member_request"
|
||||
|
||||
defp map_activity_to_activity_setting(%Activity{type: :member}), do: "member"
|
||||
|
||||
defp map_activity_to_activity_setting(_), do: false
|
||||
end
|
||||
@@ -6,7 +6,7 @@ defmodule Mobilizon.Service.Notifier.Push do
|
||||
alias Mobilizon.{Config, Users}
|
||||
alias Mobilizon.Service.Activity.{Renderer, Utils}
|
||||
alias Mobilizon.Service.Notifier
|
||||
alias Mobilizon.Service.Notifier.Push
|
||||
alias Mobilizon.Service.Notifier.{Filter, Push}
|
||||
alias Mobilizon.Storage.Page
|
||||
alias Mobilizon.Users.{PushSubscription, User}
|
||||
|
||||
@@ -20,11 +20,16 @@ defmodule Mobilizon.Service.Notifier.Push do
|
||||
@impl Notifier
|
||||
def send(user, activity, options \\ [])
|
||||
|
||||
def send(%User{id: user_id, locale: locale} = _user, %Activity{} = activity, options) do
|
||||
options = Keyword.put_new(options, :locale, locale)
|
||||
def send(%User{id: user_id, locale: locale} = user, %Activity{} = activity, options) do
|
||||
if can_send_activity?(activity, user) do
|
||||
options = Keyword.put_new(options, :locale, locale)
|
||||
|
||||
%Page{elements: subscriptions} = Users.list_user_push_subscriptions(user_id, 1, 100)
|
||||
Enum.map(subscriptions, &send_subscription(activity, convert_subscription(&1), options))
|
||||
%Page{elements: subscriptions} = Users.list_user_push_subscriptions(user_id, 1, 100)
|
||||
Enum.each(subscriptions, &send_subscription(activity, convert_subscription(&1), options))
|
||||
{:ok, :sent}
|
||||
else
|
||||
{:ok, :skipped}
|
||||
end
|
||||
end
|
||||
|
||||
@impl Notifier
|
||||
@@ -32,6 +37,34 @@ defmodule Mobilizon.Service.Notifier.Push do
|
||||
Enum.map(activities, &Push.send(user, &1, options))
|
||||
end
|
||||
|
||||
@spec can_send_activity?(Activity.t(), User.t()) :: boolean()
|
||||
defp can_send_activity?(%Activity{} = activity, %User{} = user) do
|
||||
Filter.can_send_activity?(activity, "push", user, &default_activity_behavior/1)
|
||||
end
|
||||
|
||||
@spec default_activity_behavior(String.t()) :: boolean()
|
||||
defp default_activity_behavior(activity_setting) do
|
||||
case activity_setting do
|
||||
"participation_event_updated" -> true
|
||||
"participation_event_comment" -> true
|
||||
"event_new_pending_participation" -> true
|
||||
"event_new_participation" -> false
|
||||
"event_created" -> false
|
||||
"event_updated" -> false
|
||||
"discussion_updated" -> false
|
||||
"post_published" -> false
|
||||
"post_updated" -> false
|
||||
"resource_updated" -> false
|
||||
"member_request" -> true
|
||||
"member_updated" -> false
|
||||
"user_email_password_updated" -> false
|
||||
"event_comment_mention" -> true
|
||||
"discussion_mention" -> false
|
||||
"event_new_comment" -> false
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
defp send_subscription(activity, subscription, options) do
|
||||
activity
|
||||
|> payload(options)
|
||||
|
||||
71
lib/service/workers/legacy_notifier_builder.ex
Normal file
71
lib/service/workers/legacy_notifier_builder.ex
Normal file
@@ -0,0 +1,71 @@
|
||||
defmodule Mobilizon.Service.Workers.LegacyNotifierBuilder do
|
||||
@moduledoc """
|
||||
Worker to push legacy notifications
|
||||
"""
|
||||
|
||||
alias Mobilizon.{Actors, Events, Users}
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.Service.Notifier
|
||||
|
||||
use Mobilizon.Service.Workers.Helper, queue: "activity"
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(%Job{args: args}) do
|
||||
with {"legacy_notify", args} <- Map.pop(args, "op") do
|
||||
activity = build_activity(args)
|
||||
|
||||
args
|
||||
|> users_to_notify(args["author_id"])
|
||||
|> Enum.each(&Notifier.notify(&1, activity, single_activity: true))
|
||||
end
|
||||
end
|
||||
|
||||
def build_activity(args) do
|
||||
author = Actors.get_actor(args["author_id"])
|
||||
|
||||
%Activity{
|
||||
type: String.to_existing_atom(args["type"]),
|
||||
subject: String.to_existing_atom(args["subject"]),
|
||||
subject_params: args["subject_params"],
|
||||
inserted_at: DateTime.utc_now(),
|
||||
object_type: String.to_existing_atom(args["object_type"]),
|
||||
object_id: args["object_id"],
|
||||
group: nil,
|
||||
author: author
|
||||
}
|
||||
end
|
||||
|
||||
@spec users_to_notify(map(), integer() | String.t()) :: list(Users.t())
|
||||
defp users_to_notify(
|
||||
%{"subject" => "event_comment_mention", "mentions" => mentionned_actor_ids},
|
||||
author_id
|
||||
) do
|
||||
users_from_actor_ids(mentionned_actor_ids, author_id)
|
||||
end
|
||||
|
||||
defp users_to_notify(
|
||||
%{
|
||||
"subject" => "participation_event_comment",
|
||||
"subject_params" => subject_params
|
||||
},
|
||||
author_id
|
||||
) do
|
||||
subject_params
|
||||
|> Map.get("event_id")
|
||||
|> Events.list_actors_participants_for_event()
|
||||
|> Enum.map(& &1.id)
|
||||
|> users_from_actor_ids(author_id)
|
||||
end
|
||||
|
||||
@spec users_from_actor_ids(list(), integer() | String.t()) :: list(Users.t())
|
||||
defp users_from_actor_ids(actor_ids, author_id) do
|
||||
actor_ids
|
||||
|> Enum.filter(&(&1 != author_id))
|
||||
|> Enum.map(&Actors.get_actor/1)
|
||||
|> Enum.filter(& &1)
|
||||
|> Enum.map(& &1.user_id)
|
||||
|> Enum.filter(& &1)
|
||||
|> Enum.uniq()
|
||||
|> Enum.map(&Users.get_user_with_settings!/1)
|
||||
end
|
||||
end
|
||||
@@ -43,8 +43,15 @@ defmodule Mobilizon.Web.Email.Activity do
|
||||
@spec chunk_activities(list()) :: map()
|
||||
defp chunk_activities(activities) do
|
||||
activities
|
||||
|> Enum.reduce(%{}, fn %Activity{group: %Actor{id: group_id}} = activity, acc ->
|
||||
Map.update(acc, group_id, [activity], fn activities -> activities ++ [activity] end)
|
||||
|> Enum.reduce(%{}, fn activity, acc ->
|
||||
case activity do
|
||||
%Activity{group: %Actor{id: group_id}} ->
|
||||
Map.update(acc, group_id, [activity], fn activities -> activities ++ [activity] end)
|
||||
|
||||
# Not a group activity
|
||||
%Activity{} ->
|
||||
Map.update(acc, nil, [activity], fn activities -> activities ++ [activity] end)
|
||||
end
|
||||
end)
|
||||
|> Enum.map(fn {key, value} ->
|
||||
{key, Enum.sort(value, &(&1.inserted_at <= &2.inserted_at))}
|
||||
@@ -57,20 +64,34 @@ defmodule Mobilizon.Web.Email.Activity do
|
||||
# so it will probably not catch much things
|
||||
@spec filter_duplicates(list()) :: list()
|
||||
defp filter_duplicates(activities) do
|
||||
Enum.uniq_by(activities, fn %Activity{
|
||||
author: %Actor{id: author_id},
|
||||
group: %Actor{id: group_id},
|
||||
type: type,
|
||||
subject: subject,
|
||||
subject_params: subject_params
|
||||
} ->
|
||||
%{
|
||||
author_id: author_id,
|
||||
group_id: group_id,
|
||||
type: type,
|
||||
subject: subject,
|
||||
subject_params: subject_params
|
||||
}
|
||||
Enum.uniq_by(activities, fn activity ->
|
||||
case activity do
|
||||
%Activity{
|
||||
author: %Actor{id: author_id},
|
||||
group: %Actor{id: group_id},
|
||||
type: type,
|
||||
subject: subject,
|
||||
subject_params: subject_params
|
||||
} ->
|
||||
%{
|
||||
author_id: author_id,
|
||||
group_id: group_id,
|
||||
type: type,
|
||||
subject: subject,
|
||||
subject_params: subject_params
|
||||
}
|
||||
|
||||
%Activity{
|
||||
type: type,
|
||||
subject: subject,
|
||||
subject_params: subject_params
|
||||
} ->
|
||||
%{
|
||||
type: type,
|
||||
subject: subject,
|
||||
subject_params: subject_params
|
||||
}
|
||||
end
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -67,4 +67,35 @@
|
||||
discussion: "<b>#{@activity.subject_params["discussion_title"]}</b>"
|
||||
}
|
||||
) |> raw %>
|
||||
|
||||
<% :event_comment_mention -> %>
|
||||
<%=
|
||||
dgettext("activity", "%{profile} mentionned you in a comment under event %{event}.",
|
||||
%{
|
||||
profile: "<b>#{Mobilizon.Actors.Actor.display_name_and_username(@activity.author)}</b>",
|
||||
event: "<a href=\"#{
|
||||
page_url(
|
||||
Mobilizon.Web.Endpoint,
|
||||
:event,
|
||||
@activity.subject_params["event_uuid"]
|
||||
) |> URI.decode()}\">
|
||||
#{@activity.subject_params["event_title"]}
|
||||
</a>"
|
||||
}
|
||||
) |> raw %>
|
||||
<% :participation_event_comment -> %>
|
||||
<%=
|
||||
dgettext("activity", "%{profile} has posted an announcement under event %{event}.",
|
||||
%{
|
||||
profile: "<b>#{Mobilizon.Actors.Actor.display_name_and_username(@activity.author)}</b>",
|
||||
event: "<a href=\"#{
|
||||
page_url(
|
||||
Mobilizon.Web.Endpoint,
|
||||
:event,
|
||||
@activity.subject_params["event_uuid"]
|
||||
) |> URI.decode()}\">
|
||||
#{@activity.subject_params["event_title"]}
|
||||
</a>"
|
||||
}
|
||||
) |> raw %>
|
||||
<% end %>
|
||||
@@ -27,4 +27,17 @@
|
||||
profile: Mobilizon.Actors.Actor.display_name_and_username(@activity.author),
|
||||
discussion: @activity.subject_params["discussion_title"]
|
||||
}
|
||||
) %><% end %>
|
||||
) %>
|
||||
<%= page_url(Mobilizon.Web.Endpoint, :discussion, Mobilizon.Actors.Actor.preferred_username_and_domain(@activity.group), @activity.subject_params["discussion_slug"]) |> URI.decode() %><% :event_comment_mention -> %><%= dgettext("activity", "%{profile} mentionned you in a comment under %{event}.",
|
||||
%{
|
||||
profile: Mobilizon.Actors.Actor.display_name_and_username(@activity.author),
|
||||
event: @activity.subject_params["event_title"]
|
||||
}
|
||||
) %>
|
||||
<%= page_url(Mobilizon.Web.Endpoint, :event, @activity.subject_params["event_uuid"]) |> URI.decode() %><% :participation_event_comment -> %><%= dgettext("activity", "%{profile} has posted an announcement under event %{event}.",
|
||||
%{
|
||||
profile: Mobilizon.Actors.Actor.display_name_and_username(@activity.author),
|
||||
event: @activity.subject_params["event_title"]
|
||||
}
|
||||
) %>
|
||||
<%= page_url(Mobilizon.Web.Endpoint, :event, @activity.subject_params["event_uuid"]) |> URI.decode() %><% end %>
|
||||
@@ -47,43 +47,45 @@
|
||||
<ul style="margin: 0 auto; padding-left: 15px;">
|
||||
<%= for {_, group_activities} <- @activities do %>
|
||||
<li style="list-style: none;border-bottom: solid 2px #d7d6de;padding: 10px 0;">
|
||||
<table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
|
||||
<tr>
|
||||
<td align="left">
|
||||
<table align="left">
|
||||
<tr>
|
||||
<%= if hd(group_activities).group.avatar do %>
|
||||
<td width="85">
|
||||
<a href="<%= page_url(Mobilizon.Web.Endpoint, :actor, Mobilizon.Actors.Actor.preferred_username_and_domain(hd(group_activities).group)) |> URI.decode() %>" target="_blank" style="text-decoration: none;">
|
||||
<img width="80" src="<%= hd(group_activities).group.avatar.url %>" style="width: 80px;max-height: 100px;" style="margin:0; padding:0; border:none; display:block;" border="0" alt="" />
|
||||
</a>
|
||||
</td>
|
||||
<% end %>
|
||||
<td width="400">
|
||||
<table width="" cellpadding="0" cellspacing="0" border="0" style="max-width: 400px;width: 100%;" align="left">
|
||||
<tr>
|
||||
<td align="left">
|
||||
<a href="<%= page_url(Mobilizon.Web.Endpoint, :actor, Mobilizon.Actors.Actor.preferred_username_and_domain(hd(group_activities).group)) |> URI.decode() %>" target="_blank" style="text-decoration: none;color: #474467;font-family: 'Roboto', Helvetica, Arial, sans-serif;font-size: 18px;font-weight: bold;line-height: 25px;">
|
||||
<%= hd(group_activities).group.name || "@#{Mobilizon.Actors.Actor.preferred_username_and_domain(hd(group_activities).group)}" %>
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
<%= if hd(group_activities).group.name do %>
|
||||
<%= if hd(group_activities).group do %>
|
||||
<table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
|
||||
<tr>
|
||||
<td align="left">
|
||||
<table align="left">
|
||||
<tr>
|
||||
<%= if hd(group_activities).group.avatar do %>
|
||||
<td width="85">
|
||||
<a href="<%= page_url(Mobilizon.Web.Endpoint, :actor, Mobilizon.Actors.Actor.preferred_username_and_domain(hd(group_activities).group)) |> URI.decode() %>" target="_blank" style="text-decoration: none;">
|
||||
<img width="80" src="<%= hd(group_activities).group.avatar.url %>" style="width: 80px;max-height: 100px;" style="margin:0; padding:0; border:none; display:block;" border="0" alt="" />
|
||||
</a>
|
||||
</td>
|
||||
<% end %>
|
||||
<td width="400">
|
||||
<table width="" cellpadding="0" cellspacing="0" border="0" style="max-width: 400px;width: 100%;" align="left">
|
||||
<tr>
|
||||
<td align="left">
|
||||
<a href="<%= page_url(Mobilizon.Web.Endpoint, :actor, Mobilizon.Actors.Actor.preferred_username_and_domain(hd(group_activities).group)) |> URI.decode() %>" target="_blank" style="text-decoration: none;display: block;color: #7a7a7a;font-family: 'Roboto', Helvetica, Arial, sans-serif;font-size: 16px;font-weight: 400;line-height: 25px;">
|
||||
@<%= Mobilizon.Actors.Actor.preferred_username_and_domain(hd(group_activities).group) %>
|
||||
<a href="<%= page_url(Mobilizon.Web.Endpoint, :actor, Mobilizon.Actors.Actor.preferred_username_and_domain(hd(group_activities).group)) |> URI.decode() %>" target="_blank" style="text-decoration: none;color: #474467;font-family: 'Roboto', Helvetica, Arial, sans-serif;font-size: 18px;font-weight: bold;line-height: 25px;">
|
||||
<%= hd(group_activities).group.name || "@#{Mobilizon.Actors.Actor.preferred_username_and_domain(hd(group_activities).group)}" %>
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<%= if hd(group_activities).group.name do %>
|
||||
<tr>
|
||||
<td align="left">
|
||||
<a href="<%= page_url(Mobilizon.Web.Endpoint, :actor, Mobilizon.Actors.Actor.preferred_username_and_domain(hd(group_activities).group)) |> URI.decode() %>" target="_blank" style="text-decoration: none;display: block;color: #7a7a7a;font-family: 'Roboto', Helvetica, Arial, sans-serif;font-size: 16px;font-weight: 400;line-height: 25px;">
|
||||
@<%= Mobilizon.Actors.Actor.preferred_username_and_domain(hd(group_activities).group) %>
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<% end %>
|
||||
<ul style="padding-left: 25px;margin-top: 10px;">
|
||||
<%= for activity <- Enum.take(group_activities, 5) do %>
|
||||
<li style="margin-bottom: 7px;">
|
||||
|
||||
@@ -6,7 +6,9 @@
|
||||
<%= for {_, group_activities} <- @activities do %>
|
||||
|
||||
==
|
||||
<%= if hd(group_activities).group do %>
|
||||
<%= hd(group_activities).group.name || "@#{Mobilizon.Actors.Actor.preferred_username_and_domain(hd(group_activities).group)}" %>
|
||||
<% end %>
|
||||
|
||||
<%= for activity <- Enum.take(group_activities, 5) do %>
|
||||
* <%= case activity.type do %><% :discussion -> %><%= render("activity/_discussion_activity_item.text", activity: activity) %><% :event -> %><%= render("activity/_event_activity_item.text", activity: activity) %><% :group -> %><%= render("activity/_group_activity_item.text", activity: activity) %>
|
||||
|
||||
Reference in New Issue
Block a user