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:

"message @{[email protected]} text"

it can also contain a name, which gets used as the link text:

"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_people(msg_text, allowed_people) ⇒ String

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

Parameters:

  • message (String)

    text

  • allowed_people (Array)

    ids of people that are allowed to stay

Returns:

  • (String)

    message text with filtered mentions


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/diaspora/mentionable.rb', line 59

def self.filter_people(msg_text, allowed_people)
  mentioned_ppl = people_from_string(msg_text)

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

    if person && allowed_people.include?(person.id)
      match_str
    else
      "@#{MentionsInternal.profile_link(person, name, diaspora_id)}"
    end
  }
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


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

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

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

    "@#{ERB::Util.h(MentionsInternal.mention_link(person, name, diaspora_id, opts))}"
  }
end

.mention_attrs(mention_str) ⇒ Object


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

def self.mention_attrs(mention_str)
  name, diaspora_id = mention_str.match(REGEX).captures

  [name.try(:strip).presence, diaspora_id.strip]
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


46
47
48
49
50
# File 'lib/diaspora/mentionable.rb', line 46

def self.people_from_string(msg_text)
  identifiers = msg_text.to_s.scan(REGEX).map {|match_str| match_str.second.strip }

  identifiers.compact.uniq.map {|identifier| find_or_fetch_person_by_identifier(identifier) }.compact
end