@@ -78,34 +78,56 @@ function ($val) {
78
78
);
79
79
}
80
80
81
- public function extract (?string $ val , $ type = self ::ALL ): ?array
81
+ /**
82
+ * Try to extract mentions from the body (eg. @[email protected] ).
83
+ *
84
+ * @param val Body input string
85
+ * @param type Type of mentions to extract (ALL, LOCAL only or REMOTE only)
86
+ *
87
+ * @return string[]
88
+ */
89
+ public function extract (?string $ body , $ type = self ::ALL ): ?array
82
90
{
83
- if (!$ val ) {
91
+ if (!$ body ) {
84
92
return null ;
85
93
}
86
94
87
95
$ result = match ($ type ) {
88
- self ::ALL => array_merge ($ this ->byApPrefix ($ val ), $ this ->byPrefix ($ val )),
89
- self ::LOCAL => $ this ->byPrefix ($ val ),
90
- self ::REMOTE => $ this ->byApPrefix ($ val ),
96
+ self ::ALL => array_merge ($ this ->byApPrefix ($ body ), $ this ->byPrefix ($ body )),
97
+ self ::LOCAL => $ this ->byPrefix ($ body ),
98
+ self ::REMOTE => $ this ->byApPrefix ($ body ),
91
99
};
92
100
93
101
$ result = array_map (fn ($ val ) => trim ($ val ), $ result );
94
102
95
103
return \count ($ result ) ? array_unique ($ result ) : null ;
96
104
}
97
105
106
+ /**
107
+ * Remote activitypub prefix, like @[email protected] .
108
+ *
109
+ * @param value Input string
110
+ *
111
+ * @return string[]
112
+ */
98
113
private function byApPrefix (string $ value ): array
99
114
{
100
115
preg_match_all (
101
- '/(?<!\/)\B@(\w{1,30} )(@)(([\pL\pN\pS\pM\-\_]++\.)+[\pL\pN\pM]++|[a-z0-9\-\_]++)/u ' ,
116
+ '/(?<!\/)\B@([a-zA-Z0-9._-]+@? )(@)(([\pL\pN\pS\pM\-\_]++\.)+[\pL\pN\pM]++|[a-z0-9\-\_]++)/u ' ,
102
117
$ value ,
103
118
$ matches
104
119
);
105
120
106
121
return \count ($ matches [0 ]) ? array_unique (array_values ($ matches [0 ])) : [];
107
122
}
108
123
124
+ /**
125
+ * Local username prefix, like @username.
126
+ *
127
+ * @param value Input string
128
+ *
129
+ * @return string[]
130
+ */
109
131
private function byPrefix (string $ value ): array
110
132
{
111
133
preg_match_all ('/(?<!\/)\B@([a-zA-Z0-9_-]{1,30}@?)/u ' , $ value , $ matches );
0 commit comments