Module: Diaspora::Mentionable

Defined in:
lib/diaspora/mentionable.rb

Defined Under Namespace

Modules: MentionsInternal

Constant Summary collapse

REGEX =

regex for finding mention markup in plain text ex.

"message @{User Name; [email protected]} text"
will yield "User Name" and "[email protected]"
/(@\{([^\}]+)\})/
PERSON_HREF_CLASS =

class attribute that will be added to all mention html links

"mention hovercardable"

Class Method Summary collapse

Class Method Details

.filter_for_aspects(msg_text, user, *aspects) ⇒ String

takes a message text and converts mentions for people that are not in the given aspects to simple markdown links, leaving only mentions for people who will actually be able to receive notifications for being mentioned.

Parameters:

  • message (String)

    text

  • aspect (User)

    owner

  • array (Mixed)

    containing aspect ids or “all”

Returns:

  • (String)

    message text with filtered mentions


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/diaspora/mentionable.rb', line 65

def self.filter_for_aspects(msg_text, user, *aspects)
  aspect_ids = MentionsInternal.get_aspect_ids(user, *aspects)

  mentioned_ppl = people_from_string(msg_text)
  aspects_ppl = AspectMembership.where(aspect_id: aspect_ids)
                                .includes(:contact => :person)
                                .map(&:person)

  msg_text.to_s.gsub(REGEX) {|match_str|
    name, handle = mention_attrs(match_str)
    person = mentioned_ppl.find {|p| p.diaspora_handle == handle }
    mention = MentionsInternal.profile_link(person, name) unless aspects_ppl.include?(person)

    mention || match_str
  }
end

.format(msg_text, people, opts = {}) ⇒ String

takes a message text and returns the text with mentions in (html escaped) plain text or formatted with html markup linking to user profiles. default is html output.

Parameters:

  • text (String)

    containing mentions

  • list (Array<Person>)

    of mentioned people

  • formatting (Hash)

    options

Returns:

  • (String)

    formatted message


31
32
33
34
35
36
37
38
39
40
# File 'lib/diaspora/mentionable.rb', line 31

def self.format(msg_text, people, opts={})
  people = [*people]

  msg_text.to_s.gsub(REGEX) {|match_str|
    name, handle = mention_attrs(match_str)
    person = people.find {|p| p.diaspora_handle == handle }

    ERB::Util.h(MentionsInternal.mention_link(person, name, opts))
  }
end

.mention_attrs(mention_str) ⇒ Object


13
14
15
16
17
18
19
20
21
# File 'lib/diaspora/mentionable.rb', line 13

def self.mention_attrs(mention_str)
  mention = mention_str.match(REGEX)[2]
  del_pos = mention.rindex(/;/)

  name   = mention[0..(del_pos-1)].strip
  handle = mention[(del_pos+1)..-1].strip

  [name, handle]
end

.people_from_string(msg_text) ⇒ Array<Person>

takes a message text and returns an array of people constructed from the contained mentions

Parameters:

  • text (String)

    containing mentions

Returns:

  • (Array<Person>)

    array of people


47
48
49
50
51
52
53
54
55
# File 'lib/diaspora/mentionable.rb', line 47

def self.people_from_string(msg_text)
  identifiers = msg_text.to_s.scan(REGEX).map do |match_str|
    _, handle = mention_attrs(match_str.first)
    handle
  end

  return [] if identifiers.empty?
  Person.where(diaspora_handle: identifiers)
end