DirectMessageController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Auth, Cache;
  4. use Illuminate\Http\Request;
  5. use App\{
  6. DirectMessage,
  7. Media,
  8. Notification,
  9. Profile,
  10. Status,
  11. User,
  12. UserFilter,
  13. UserSetting
  14. };
  15. use App\Services\MediaPathService;
  16. use App\Services\MediaBlocklistService;
  17. use App\Jobs\StatusPipeline\NewStatusPipeline;
  18. use Illuminate\Support\Str;
  19. use App\Util\ActivityPub\Helpers;
  20. use App\Services\WebfingerService;
  21. class DirectMessageController extends Controller
  22. {
  23. public function __construct()
  24. {
  25. $this->middleware('auth');
  26. }
  27. public function browse(Request $request)
  28. {
  29. $this->validate($request, [
  30. 'a' => 'nullable|string|in:inbox,sent,filtered',
  31. 'page' => 'nullable|integer|min:1|max:99'
  32. ]);
  33. $profile = $request->user()->profile_id;
  34. $action = $request->input('a', 'inbox');
  35. $page = $request->input('page');
  36. if(config('database.default') == 'pgsql') {
  37. if($action == 'inbox') {
  38. $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
  39. ->whereToId($profile)
  40. ->with(['author','status'])
  41. ->whereIsHidden(false)
  42. ->when($page, function($q, $page) {
  43. if($page > 1) {
  44. return $q->offset($page * 8 - 8);
  45. }
  46. })
  47. ->latest()
  48. ->get()
  49. ->unique('from_id')
  50. ->take(8)
  51. ->map(function($r) use($profile) {
  52. return $r->from_id !== $profile ? [
  53. 'id' => (string) $r->from_id,
  54. 'name' => $r->author->name,
  55. 'username' => $r->author->username,
  56. 'avatar' => $r->author->avatarUrl(),
  57. 'url' => $r->author->url(),
  58. 'isLocal' => (bool) !$r->author->domain,
  59. 'domain' => $r->author->domain,
  60. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  61. 'lastMessage' => $r->status->caption,
  62. 'messages' => []
  63. ] : [
  64. 'id' => (string) $r->to_id,
  65. 'name' => $r->recipient->name,
  66. 'username' => $r->recipient->username,
  67. 'avatar' => $r->recipient->avatarUrl(),
  68. 'url' => $r->recipient->url(),
  69. 'isLocal' => (bool) !$r->recipient->domain,
  70. 'domain' => $r->recipient->domain,
  71. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  72. 'lastMessage' => $r->status->caption,
  73. 'messages' => []
  74. ];
  75. })->values();
  76. }
  77. if($action == 'sent') {
  78. $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
  79. ->whereFromId($profile)
  80. ->with(['author','status'])
  81. ->orderBy('id', 'desc')
  82. ->when($page, function($q, $page) {
  83. if($page > 1) {
  84. return $q->offset($page * 8 - 8);
  85. }
  86. })
  87. ->get()
  88. ->unique('to_id')
  89. ->take(8)
  90. ->map(function($r) use($profile) {
  91. return $r->from_id !== $profile ? [
  92. 'id' => (string) $r->from_id,
  93. 'name' => $r->author->name,
  94. 'username' => $r->author->username,
  95. 'avatar' => $r->author->avatarUrl(),
  96. 'url' => $r->author->url(),
  97. 'isLocal' => (bool) !$r->author->domain,
  98. 'domain' => $r->author->domain,
  99. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  100. 'lastMessage' => $r->status->caption,
  101. 'messages' => []
  102. ] : [
  103. 'id' => (string) $r->to_id,
  104. 'name' => $r->recipient->name,
  105. 'username' => $r->recipient->username,
  106. 'avatar' => $r->recipient->avatarUrl(),
  107. 'url' => $r->recipient->url(),
  108. 'isLocal' => (bool) !$r->recipient->domain,
  109. 'domain' => $r->recipient->domain,
  110. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  111. 'lastMessage' => $r->status->caption,
  112. 'messages' => []
  113. ];
  114. });
  115. }
  116. if($action == 'filtered') {
  117. $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
  118. ->whereToId($profile)
  119. ->with(['author','status'])
  120. ->whereIsHidden(true)
  121. ->orderBy('id', 'desc')
  122. ->when($page, function($q, $page) {
  123. if($page > 1) {
  124. return $q->offset($page * 8 - 8);
  125. }
  126. })
  127. ->get()
  128. ->unique('from_id')
  129. ->take(8)
  130. ->map(function($r) use($profile) {
  131. return $r->from_id !== $profile ? [
  132. 'id' => (string) $r->from_id,
  133. 'name' => $r->author->name,
  134. 'username' => $r->author->username,
  135. 'avatar' => $r->author->avatarUrl(),
  136. 'url' => $r->author->url(),
  137. 'isLocal' => (bool) !$r->author->domain,
  138. 'domain' => $r->author->domain,
  139. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  140. 'lastMessage' => $r->status->caption,
  141. 'messages' => []
  142. ] : [
  143. 'id' => (string) $r->to_id,
  144. 'name' => $r->recipient->name,
  145. 'username' => $r->recipient->username,
  146. 'avatar' => $r->recipient->avatarUrl(),
  147. 'url' => $r->recipient->url(),
  148. 'isLocal' => (bool) !$r->recipient->domain,
  149. 'domain' => $r->recipient->domain,
  150. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  151. 'lastMessage' => $r->status->caption,
  152. 'messages' => []
  153. ];
  154. });
  155. }
  156. } elseif(config('database.default') == 'mysql') {
  157. if($action == 'inbox') {
  158. $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
  159. ->whereToId($profile)
  160. ->with(['author','status'])
  161. ->whereIsHidden(false)
  162. ->groupBy('from_id')
  163. ->latest()
  164. ->when($page, function($q, $page) {
  165. if($page > 1) {
  166. return $q->offset($page * 8 - 8);
  167. }
  168. })
  169. ->limit(8)
  170. ->get()
  171. ->map(function($r) use($profile) {
  172. return $r->from_id !== $profile ? [
  173. 'id' => (string) $r->from_id,
  174. 'name' => $r->author->name,
  175. 'username' => $r->author->username,
  176. 'avatar' => $r->author->avatarUrl(),
  177. 'url' => $r->author->url(),
  178. 'isLocal' => (bool) !$r->author->domain,
  179. 'domain' => $r->author->domain,
  180. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  181. 'lastMessage' => $r->status->caption,
  182. 'messages' => []
  183. ] : [
  184. 'id' => (string) $r->to_id,
  185. 'name' => $r->recipient->name,
  186. 'username' => $r->recipient->username,
  187. 'avatar' => $r->recipient->avatarUrl(),
  188. 'url' => $r->recipient->url(),
  189. 'isLocal' => (bool) !$r->recipient->domain,
  190. 'domain' => $r->recipient->domain,
  191. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  192. 'lastMessage' => $r->status->caption,
  193. 'messages' => []
  194. ];
  195. });
  196. }
  197. if($action == 'sent') {
  198. $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
  199. ->whereFromId($profile)
  200. ->with(['author','status'])
  201. ->groupBy('to_id')
  202. ->orderBy('createdAt', 'desc')
  203. ->when($page, function($q, $page) {
  204. if($page > 1) {
  205. return $q->offset($page * 8 - 8);
  206. }
  207. })
  208. ->limit(8)
  209. ->get()
  210. ->map(function($r) use($profile) {
  211. return $r->from_id !== $profile ? [
  212. 'id' => (string) $r->from_id,
  213. 'name' => $r->author->name,
  214. 'username' => $r->author->username,
  215. 'avatar' => $r->author->avatarUrl(),
  216. 'url' => $r->author->url(),
  217. 'isLocal' => (bool) !$r->author->domain,
  218. 'domain' => $r->author->domain,
  219. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  220. 'lastMessage' => $r->status->caption,
  221. 'messages' => []
  222. ] : [
  223. 'id' => (string) $r->to_id,
  224. 'name' => $r->recipient->name,
  225. 'username' => $r->recipient->username,
  226. 'avatar' => $r->recipient->avatarUrl(),
  227. 'url' => $r->recipient->url(),
  228. 'isLocal' => (bool) !$r->recipient->domain,
  229. 'domain' => $r->recipient->domain,
  230. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  231. 'lastMessage' => $r->status->caption,
  232. 'messages' => []
  233. ];
  234. });
  235. }
  236. if($action == 'filtered') {
  237. $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
  238. ->whereToId($profile)
  239. ->with(['author','status'])
  240. ->whereIsHidden(true)
  241. ->groupBy('from_id')
  242. ->orderBy('createdAt', 'desc')
  243. ->when($page, function($q, $page) {
  244. if($page > 1) {
  245. return $q->offset($page * 8 - 8);
  246. }
  247. })
  248. ->limit(8)
  249. ->get()
  250. ->map(function($r) use($profile) {
  251. return $r->from_id !== $profile ? [
  252. 'id' => (string) $r->from_id,
  253. 'name' => $r->author->name,
  254. 'username' => $r->author->username,
  255. 'avatar' => $r->author->avatarUrl(),
  256. 'url' => $r->author->url(),
  257. 'isLocal' => (bool) !$r->author->domain,
  258. 'domain' => $r->author->domain,
  259. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  260. 'lastMessage' => $r->status->caption,
  261. 'messages' => []
  262. ] : [
  263. 'id' => (string) $r->to_id,
  264. 'name' => $r->recipient->name,
  265. 'username' => $r->recipient->username,
  266. 'avatar' => $r->recipient->avatarUrl(),
  267. 'url' => $r->recipient->url(),
  268. 'isLocal' => (bool) !$r->recipient->domain,
  269. 'domain' => $r->recipient->domain,
  270. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  271. 'lastMessage' => $r->status->caption,
  272. 'messages' => []
  273. ];
  274. });
  275. }
  276. }
  277. return response()->json($dms->all());
  278. }
  279. public function create(Request $request)
  280. {
  281. $this->validate($request, [
  282. 'to_id' => 'required',
  283. 'message' => 'required|string|min:1|max:500',
  284. 'type' => 'required|in:text,emoji'
  285. ]);
  286. $profile = $request->user()->profile;
  287. $recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id'));
  288. abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403);
  289. $msg = $request->input('message');
  290. if((!$recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) {
  291. if($recipient->follows($profile) == true) {
  292. $hidden = false;
  293. } else {
  294. $hidden = true;
  295. }
  296. } else {
  297. $hidden = false;
  298. }
  299. $status = new Status;
  300. $status->profile_id = $profile->id;
  301. $status->caption = $msg;
  302. $status->rendered = $msg;
  303. $status->visibility = 'direct';
  304. $status->scope = 'direct';
  305. $status->in_reply_to_profile_id = $recipient->id;
  306. $status->save();
  307. $dm = new DirectMessage;
  308. $dm->to_id = $recipient->id;
  309. $dm->from_id = $profile->id;
  310. $dm->status_id = $status->id;
  311. $dm->is_hidden = $hidden;
  312. $dm->type = $request->input('type');
  313. $dm->save();
  314. if(filter_var($msg, FILTER_VALIDATE_URL)) {
  315. if(Helpers::validateUrl($msg)) {
  316. $dm->type = 'link';
  317. $dm->meta = [
  318. 'domain' => parse_url($msg, PHP_URL_HOST),
  319. 'local' => parse_url($msg, PHP_URL_HOST) ==
  320. parse_url(config('app.url'), PHP_URL_HOST)
  321. ];
  322. $dm->save();
  323. }
  324. }
  325. $nf = UserFilter::whereUserId($recipient->id)
  326. ->whereFilterableId($profile->id)
  327. ->whereFilterableType('App\Profile')
  328. ->whereFilterType('dm.mute')
  329. ->exists();
  330. if($recipient->domain == null && $hidden == false && !$nf) {
  331. $notification = new Notification();
  332. $notification->profile_id = $recipient->id;
  333. $notification->actor_id = $profile->id;
  334. $notification->action = 'dm';
  335. $notification->message = $dm->toText();
  336. $notification->rendered = $dm->toHtml();
  337. $notification->item_id = $dm->id;
  338. $notification->item_type = "App\DirectMessage";
  339. $notification->save();
  340. }
  341. if($recipient->domain) {
  342. $this->remoteDeliver($dm);
  343. }
  344. $res = [
  345. 'id' => (string) $dm->id,
  346. 'isAuthor' => $profile->id == $dm->from_id,
  347. 'reportId' => (string) $dm->status_id,
  348. 'hidden' => (bool) $dm->is_hidden,
  349. 'type' => $dm->type,
  350. 'text' => $dm->status->caption,
  351. 'media' => null,
  352. 'timeAgo' => $dm->created_at->diffForHumans(null,null,true),
  353. 'seen' => $dm->read_at != null,
  354. 'meta' => $dm->meta
  355. ];
  356. return response()->json($res);
  357. }
  358. public function thread(Request $request)
  359. {
  360. $this->validate($request, [
  361. 'pid' => 'required'
  362. ]);
  363. $uid = $request->user()->profile_id;
  364. $pid = $request->input('pid');
  365. $max_id = $request->input('max_id');
  366. $min_id = $request->input('min_id');
  367. $r = Profile::findOrFail($pid);
  368. if($min_id) {
  369. $res = DirectMessage::select('*')
  370. ->where('id', '>', $min_id)
  371. ->where(function($q) use($pid,$uid) {
  372. return $q->where([['from_id',$pid],['to_id',$uid]
  373. ])->orWhere([['from_id',$uid],['to_id',$pid]]);
  374. })
  375. ->latest()
  376. ->take(8)
  377. ->get();
  378. } else if ($max_id) {
  379. $res = DirectMessage::select('*')
  380. ->where('id', '<', $max_id)
  381. ->where(function($q) use($pid,$uid) {
  382. return $q->where([['from_id',$pid],['to_id',$uid]
  383. ])->orWhere([['from_id',$uid],['to_id',$pid]]);
  384. })
  385. ->latest()
  386. ->take(8)
  387. ->get();
  388. } else {
  389. $res = DirectMessage::where(function($q) use($pid,$uid) {
  390. return $q->where([['from_id',$pid],['to_id',$uid]
  391. ])->orWhere([['from_id',$uid],['to_id',$pid]]);
  392. })
  393. ->latest()
  394. ->take(8)
  395. ->get();
  396. }
  397. $res = $res->map(function($s) use ($uid){
  398. return [
  399. 'id' => (string) $s->id,
  400. 'hidden' => (bool) $s->is_hidden,
  401. 'isAuthor' => $uid == $s->from_id,
  402. 'type' => $s->type,
  403. 'text' => $s->status->caption,
  404. 'media' => $s->status->firstMedia() ? $s->status->firstMedia()->url() : null,
  405. 'timeAgo' => $s->created_at->diffForHumans(null,null,true),
  406. 'seen' => $s->read_at != null,
  407. 'reportId' => (string) $s->status_id,
  408. 'meta' => json_decode($s->meta,true)
  409. ];
  410. });
  411. $w = [
  412. 'id' => (string) $r->id,
  413. 'name' => $r->name,
  414. 'username' => $r->username,
  415. 'avatar' => $r->avatarUrl(),
  416. 'url' => $r->url(),
  417. 'muted' => UserFilter::whereUserId($uid)
  418. ->whereFilterableId($r->id)
  419. ->whereFilterableType('App\Profile')
  420. ->whereFilterType('dm.mute')
  421. ->first() ? true : false,
  422. 'isLocal' => (bool) !$r->domain,
  423. 'domain' => $r->domain,
  424. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  425. 'lastMessage' => '',
  426. 'messages' => $res
  427. ];
  428. return response()->json($w, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  429. }
  430. public function delete(Request $request)
  431. {
  432. $this->validate($request, [
  433. 'id' => 'required'
  434. ]);
  435. $sid = $request->input('id');
  436. $pid = $request->user()->profile_id;
  437. $dm = DirectMessage::whereFromId($pid)
  438. ->whereStatusId($sid)
  439. ->firstOrFail();
  440. $status = Status::whereProfileId($pid)
  441. ->findOrFail($dm->status_id);
  442. if($dm->recipient->domain) {
  443. $dmc = $dm;
  444. $this->remoteDelete($dmc);
  445. }
  446. $status->delete();
  447. $dm->delete();
  448. return [200];
  449. }
  450. public function get(Request $request, $id)
  451. {
  452. $pid = $request->user()->profile_id;
  453. $dm = DirectMessage::whereStatusId($id)->firstOrFail();
  454. abort_if($pid !== $dm->to_id && $pid !== $dm->from_id, 404);
  455. return response()->json($dm, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  456. }
  457. public function mediaUpload(Request $request)
  458. {
  459. $this->validate($request, [
  460. 'file' => function() {
  461. return [
  462. 'required',
  463. 'mimetypes:' . config_cache('pixelfed.media_types'),
  464. 'max:' . config_cache('pixelfed.max_photo_size'),
  465. ];
  466. },
  467. 'to_id' => 'required'
  468. ]);
  469. $user = $request->user();
  470. $profile = $user->profile;
  471. $recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id'));
  472. abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403);
  473. if((!$recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) {
  474. if($recipient->follows($profile) == true) {
  475. $hidden = false;
  476. } else {
  477. $hidden = true;
  478. }
  479. } else {
  480. $hidden = false;
  481. }
  482. if(config_cache('pixelfed.enforce_account_limit') == true) {
  483. $size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) {
  484. return Media::whereUserId($user->id)->sum('size') / 1000;
  485. });
  486. $limit = (int) config_cache('pixelfed.max_account_size');
  487. if ($size >= $limit) {
  488. abort(403, 'Account size limit reached.');
  489. }
  490. }
  491. $photo = $request->file('file');
  492. $mimes = explode(',', config_cache('pixelfed.media_types'));
  493. if(in_array($photo->getMimeType(), $mimes) == false) {
  494. abort(403, 'Invalid or unsupported mime type.');
  495. }
  496. $storagePath = MediaPathService::get($user, 2) . Str::random(8);
  497. $path = $photo->store($storagePath);
  498. $hash = \hash_file('sha256', $photo);
  499. abort_if(MediaBlocklistService::exists($hash) == true, 451);
  500. $status = new Status;
  501. $status->profile_id = $profile->id;
  502. $status->caption = null;
  503. $status->rendered = null;
  504. $status->visibility = 'direct';
  505. $status->scope = 'direct';
  506. $status->in_reply_to_profile_id = $recipient->id;
  507. $status->save();
  508. $media = new Media();
  509. $media->status_id = $status->id;
  510. $media->profile_id = $profile->id;
  511. $media->user_id = $user->id;
  512. $media->media_path = $path;
  513. $media->original_sha256 = $hash;
  514. $media->size = $photo->getSize();
  515. $media->mime = $photo->getMimeType();
  516. $media->caption = null;
  517. $media->filter_class = null;
  518. $media->filter_name = null;
  519. $media->save();
  520. $dm = new DirectMessage;
  521. $dm->to_id = $recipient->id;
  522. $dm->from_id = $profile->id;
  523. $dm->status_id = $status->id;
  524. $dm->type = array_first(explode('/', $media->mime)) == 'video' ? 'video' : 'photo';
  525. $dm->is_hidden = $hidden;
  526. $dm->save();
  527. if($recipient->domain) {
  528. $this->remoteDeliver($dm);
  529. }
  530. return [
  531. 'id' => $dm->id,
  532. 'reportId' => (string) $dm->status_id,
  533. 'type' => $dm->type,
  534. 'url' => $media->url()
  535. ];
  536. }
  537. public function composeLookup(Request $request)
  538. {
  539. $this->validate($request, [
  540. 'q' => 'required|string|min:2|max:50',
  541. 'remote' => 'nullable',
  542. ]);
  543. $q = $request->input('q');
  544. $r = $request->input('remote', false);
  545. if($r && !Str::of($q)->contains('.')) {
  546. return [];
  547. }
  548. if($r && Helpers::validateUrl($q)) {
  549. Helpers::profileFetch($q);
  550. }
  551. if(Str::of($q)->startsWith('@')) {
  552. if(strlen($q) < 3) {
  553. return [];
  554. }
  555. if(substr_count($q, '@') == 2) {
  556. WebfingerService::lookup($q);
  557. }
  558. $q = mb_substr($q, 1);
  559. }
  560. $blocked = UserFilter::whereFilterableType('App\Profile')
  561. ->whereFilterType('block')
  562. ->whereFilterableId($request->user()->profile_id)
  563. ->pluck('user_id');
  564. $blocked->push($request->user()->profile_id);
  565. $results = Profile::select('id','domain','username')
  566. ->whereNotIn('id', $blocked)
  567. ->where('username','like','%'.$q.'%')
  568. ->orderBy('domain')
  569. ->limit(8)
  570. ->get()
  571. ->map(function($r) {
  572. return [
  573. 'local' => (bool) !$r->domain,
  574. 'id' => (string) $r->id,
  575. 'name' => $r->username,
  576. 'privacy' => true,
  577. 'avatar' => $r->avatarUrl()
  578. ];
  579. });
  580. return $results;
  581. }
  582. public function read(Request $request)
  583. {
  584. $this->validate($request, [
  585. 'pid' => 'required',
  586. 'sid' => 'required'
  587. ]);
  588. $pid = $request->input('pid');
  589. $sid = $request->input('sid');
  590. $dms = DirectMessage::whereToId($request->user()->profile_id)
  591. ->whereFromId($pid)
  592. ->where('status_id', '>=', $sid)
  593. ->get();
  594. $now = now();
  595. foreach($dms as $dm) {
  596. $dm->read_at = $now;
  597. $dm->save();
  598. }
  599. return response()->json($dms->pluck('id'));
  600. }
  601. public function mute(Request $request)
  602. {
  603. $this->validate($request, [
  604. 'id' => 'required'
  605. ]);
  606. $fid = $request->input('id');
  607. $pid = $request->user()->profile_id;
  608. UserFilter::firstOrCreate(
  609. [
  610. 'user_id' => $pid,
  611. 'filterable_id' => $fid,
  612. 'filterable_type' => 'App\Profile',
  613. 'filter_type' => 'dm.mute'
  614. ]
  615. );
  616. return [200];
  617. }
  618. public function unmute(Request $request)
  619. {
  620. $this->validate($request, [
  621. 'id' => 'required'
  622. ]);
  623. $fid = $request->input('id');
  624. $pid = $request->user()->profile_id;
  625. $f = UserFilter::whereUserId($pid)
  626. ->whereFilterableId($fid)
  627. ->whereFilterableType('App\Profile')
  628. ->whereFilterType('dm.mute')
  629. ->firstOrFail();
  630. $f->delete();
  631. return [200];
  632. }
  633. public function remoteDeliver($dm)
  634. {
  635. $profile = $dm->author;
  636. $url = $dm->recipient->sharedInbox ?? $dm->recipient->inbox_url;
  637. $tags = [
  638. [
  639. 'type' => 'Mention',
  640. 'href' => $dm->recipient->permalink(),
  641. 'name' => $dm->recipient->emailUrl(),
  642. ]
  643. ];
  644. $body = [
  645. '@context' => [
  646. 'https://www.w3.org/ns/activitystreams',
  647. 'https://w3id.org/security/v1',
  648. [
  649. 'sc' => 'http://schema.org#',
  650. 'Hashtag' => 'as:Hashtag',
  651. 'sensitive' => 'as:sensitive',
  652. ]
  653. ],
  654. 'id' => $dm->status->permalink(),
  655. 'type' => 'Create',
  656. 'actor' => $dm->status->profile->permalink(),
  657. 'published' => $dm->status->created_at->toAtomString(),
  658. 'to' => [$dm->recipient->permalink()],
  659. 'cc' => [],
  660. 'object' => [
  661. 'id' => $dm->status->url(),
  662. 'type' => 'Note',
  663. 'summary' => null,
  664. 'content' => $dm->status->rendered ?? $dm->status->caption,
  665. 'inReplyTo' => null,
  666. 'published' => $dm->status->created_at->toAtomString(),
  667. 'url' => $dm->status->url(),
  668. 'attributedTo' => $dm->status->profile->permalink(),
  669. 'to' => [$dm->recipient->permalink()],
  670. 'cc' => [],
  671. 'sensitive' => (bool) $dm->status->is_nsfw,
  672. 'attachment' => $dm->status->media()->orderBy('order')->get()->map(function ($media) {
  673. return [
  674. 'type' => $media->activityVerb(),
  675. 'mediaType' => $media->mime,
  676. 'url' => $media->url(),
  677. 'name' => $media->caption,
  678. ];
  679. })->toArray(),
  680. 'tag' => $tags,
  681. ]
  682. ];
  683. Helpers::sendSignedObject($profile, $url, $body);
  684. }
  685. public function remoteDelete($dm)
  686. {
  687. $profile = $dm->author;
  688. $url = $dm->recipient->sharedInbox ?? $dm->recipient->inbox_url;
  689. $body = [
  690. '@context' => [
  691. 'https://www.w3.org/ns/activitystreams',
  692. 'https://w3id.org/security/v1',
  693. [
  694. 'sc' => 'http://schema.org#',
  695. 'Hashtag' => 'as:Hashtag',
  696. 'sensitive' => 'as:sensitive',
  697. ]
  698. ],
  699. 'id' => $dm->status->permalink('#delete'),
  700. 'to' => [
  701. 'https://www.w3.org/ns/activitystreams#Public'
  702. ],
  703. 'type' => 'Delete',
  704. 'actor' => $dm->status->profile->permalink(),
  705. 'object' => [
  706. 'id' => $dm->status->url(),
  707. 'type' => 'Tombstone'
  708. ]
  709. ];
  710. Helpers::sendSignedObject($profile, $url, $body);
  711. }
  712. }