DirectMessageController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. // $r = Profile::whereNull('domain')->findOrFail($pid);
  369. if($min_id) {
  370. $res = DirectMessage::select('*')
  371. ->where('id', '>', $min_id)
  372. ->where(function($q) use($pid,$uid) {
  373. return $q->where([['from_id',$pid],['to_id',$uid]
  374. ])->orWhere([['from_id',$uid],['to_id',$pid]]);
  375. })
  376. ->latest()
  377. ->take(8)
  378. ->get();
  379. } else if ($max_id) {
  380. $res = DirectMessage::select('*')
  381. ->where('id', '<', $max_id)
  382. ->where(function($q) use($pid,$uid) {
  383. return $q->where([['from_id',$pid],['to_id',$uid]
  384. ])->orWhere([['from_id',$uid],['to_id',$pid]]);
  385. })
  386. ->latest()
  387. ->take(8)
  388. ->get();
  389. } else {
  390. $res = DirectMessage::where(function($q) use($pid,$uid) {
  391. return $q->where([['from_id',$pid],['to_id',$uid]
  392. ])->orWhere([['from_id',$uid],['to_id',$pid]]);
  393. })
  394. ->latest()
  395. ->take(8)
  396. ->get();
  397. }
  398. $res = $res->map(function($s) use ($uid){
  399. return [
  400. 'id' => (string) $s->id,
  401. 'hidden' => (bool) $s->is_hidden,
  402. 'isAuthor' => $uid == $s->from_id,
  403. 'type' => $s->type,
  404. 'text' => $s->status->caption,
  405. 'media' => $s->status->firstMedia() ? $s->status->firstMedia()->url() : null,
  406. 'timeAgo' => $s->created_at->diffForHumans(null,null,true),
  407. 'seen' => $s->read_at != null,
  408. 'reportId' => (string) $s->status_id,
  409. 'meta' => json_decode($s->meta,true)
  410. ];
  411. });
  412. $w = [
  413. 'id' => (string) $r->id,
  414. 'name' => $r->name,
  415. 'username' => $r->username,
  416. 'avatar' => $r->avatarUrl(),
  417. 'url' => $r->url(),
  418. 'muted' => UserFilter::whereUserId($uid)
  419. ->whereFilterableId($r->id)
  420. ->whereFilterableType('App\Profile')
  421. ->whereFilterType('dm.mute')
  422. ->first() ? true : false,
  423. 'isLocal' => (bool) !$r->domain,
  424. 'domain' => $r->domain,
  425. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  426. 'lastMessage' => '',
  427. 'messages' => $res
  428. ];
  429. return response()->json($w, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  430. }
  431. public function delete(Request $request)
  432. {
  433. $this->validate($request, [
  434. 'id' => 'required'
  435. ]);
  436. $sid = $request->input('id');
  437. $pid = $request->user()->profile_id;
  438. $dm = DirectMessage::whereFromId($pid)
  439. ->whereStatusId($sid)
  440. ->firstOrFail();
  441. $status = Status::whereProfileId($pid)
  442. ->findOrFail($dm->status_id);
  443. if($dm->recipient->domain) {
  444. $dmc = $dm;
  445. $this->remoteDelete($dmc);
  446. }
  447. $status->delete();
  448. $dm->delete();
  449. return [200];
  450. }
  451. public function get(Request $request, $id)
  452. {
  453. $pid = $request->user()->profile_id;
  454. $dm = DirectMessage::whereStatusId($id)->firstOrFail();
  455. abort_if($pid !== $dm->to_id && $pid !== $dm->from_id, 404);
  456. return response()->json($dm, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  457. }
  458. public function mediaUpload(Request $request)
  459. {
  460. $this->validate($request, [
  461. 'file' => function() {
  462. return [
  463. 'required',
  464. 'mimes:' . config_cache('pixelfed.media_types'),
  465. 'max:' . config_cache('pixelfed.max_photo_size'),
  466. ];
  467. },
  468. 'to_id' => 'required'
  469. ]);
  470. $user = $request->user();
  471. $profile = $user->profile;
  472. $recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id'));
  473. abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403);
  474. if((!$recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) {
  475. if($recipient->follows($profile) == true) {
  476. $hidden = false;
  477. } else {
  478. $hidden = true;
  479. }
  480. } else {
  481. $hidden = false;
  482. }
  483. if(config('pixelfed.enforce_account_limit') == true) {
  484. $size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) {
  485. return Media::whereUserId($user->id)->sum('size') / 1000;
  486. });
  487. $limit = (int) config('pixelfed.max_account_size');
  488. if ($size >= $limit) {
  489. abort(403, 'Account size limit reached.');
  490. }
  491. }
  492. $photo = $request->file('file');
  493. $mimes = explode(',', config_cache('pixelfed.media_types'));
  494. if(in_array($photo->getMimeType(), $mimes) == false) {
  495. abort(403, 'Invalid or unsupported mime type.');
  496. }
  497. $storagePath = MediaPathService::get($user, 2) . Str::random(8);
  498. $path = $photo->store($storagePath);
  499. $hash = \hash_file('sha256', $photo);
  500. abort_if(MediaBlocklistService::exists($hash) == true, 451);
  501. $status = new Status;
  502. $status->profile_id = $profile->id;
  503. $status->caption = null;
  504. $status->rendered = null;
  505. $status->visibility = 'direct';
  506. $status->scope = 'direct';
  507. $status->in_reply_to_profile_id = $recipient->id;
  508. $status->save();
  509. $media = new Media();
  510. $media->status_id = $status->id;
  511. $media->profile_id = $profile->id;
  512. $media->user_id = $user->id;
  513. $media->media_path = $path;
  514. $media->original_sha256 = $hash;
  515. $media->size = $photo->getSize();
  516. $media->mime = $photo->getMimeType();
  517. $media->caption = null;
  518. $media->filter_class = null;
  519. $media->filter_name = null;
  520. $media->save();
  521. $dm = new DirectMessage;
  522. $dm->to_id = $recipient->id;
  523. $dm->from_id = $profile->id;
  524. $dm->status_id = $status->id;
  525. $dm->type = array_first(explode('/', $media->mime)) == 'video' ? 'video' : 'photo';
  526. $dm->is_hidden = $hidden;
  527. $dm->save();
  528. if($recipient->domain) {
  529. $this->remoteDeliver($dm);
  530. }
  531. return [
  532. 'id' => $dm->id,
  533. 'reportId' => (string) $dm->status_id,
  534. 'type' => $dm->type,
  535. 'url' => $media->url()
  536. ];
  537. }
  538. public function composeLookup(Request $request)
  539. {
  540. $this->validate($request, [
  541. 'q' => 'required|string|min:2|max:50',
  542. 'remote' => 'nullable|boolean',
  543. ]);
  544. $q = $request->input('q');
  545. $r = $request->input('remote');
  546. if($r && Helpers::validateUrl($q)) {
  547. Helpers::profileFetch($q);
  548. }
  549. if(Str::of($q)->startsWith('@')) {
  550. if(strlen($q) < 3) {
  551. return [];
  552. }
  553. if(substr_count($q, '@') == 2) {
  554. WebfingerService::lookup($q);
  555. }
  556. $q = mb_substr($q, 1);
  557. }
  558. $blocked = UserFilter::whereFilterableType('App\Profile')
  559. ->whereFilterType('block')
  560. ->whereFilterableId($request->user()->profile_id)
  561. ->pluck('user_id');
  562. $blocked->push($request->user()->profile_id);
  563. $results = Profile::select('id','domain','username')
  564. ->whereNotIn('id', $blocked)
  565. ->where('username','like','%'.$q.'%')
  566. ->orderBy('domain')
  567. ->limit(8)
  568. ->get()
  569. ->map(function($r) {
  570. return [
  571. 'local' => (bool) !$r->domain,
  572. 'id' => (string) $r->id,
  573. 'name' => $r->username,
  574. 'privacy' => true,
  575. 'avatar' => $r->avatarUrl()
  576. ];
  577. });
  578. return $results;
  579. }
  580. public function read(Request $request)
  581. {
  582. $this->validate($request, [
  583. 'pid' => 'required',
  584. 'sid' => 'required'
  585. ]);
  586. $pid = $request->input('pid');
  587. $sid = $request->input('sid');
  588. $dms = DirectMessage::whereToId($request->user()->profile_id)
  589. ->whereFromId($pid)
  590. ->where('status_id', '>=', $sid)
  591. ->get();
  592. $now = now();
  593. foreach($dms as $dm) {
  594. $dm->read_at = $now;
  595. $dm->save();
  596. }
  597. return response()->json($dms->pluck('id'));
  598. }
  599. public function mute(Request $request)
  600. {
  601. $this->validate($request, [
  602. 'id' => 'required'
  603. ]);
  604. $fid = $request->input('id');
  605. $pid = $request->user()->profile_id;
  606. UserFilter::firstOrCreate(
  607. [
  608. 'user_id' => $pid,
  609. 'filterable_id' => $fid,
  610. 'filterable_type' => 'App\Profile',
  611. 'filter_type' => 'dm.mute'
  612. ]
  613. );
  614. return [200];
  615. }
  616. public function unmute(Request $request)
  617. {
  618. $this->validate($request, [
  619. 'id' => 'required'
  620. ]);
  621. $fid = $request->input('id');
  622. $pid = $request->user()->profile_id;
  623. $f = UserFilter::whereUserId($pid)
  624. ->whereFilterableId($fid)
  625. ->whereFilterableType('App\Profile')
  626. ->whereFilterType('dm.mute')
  627. ->firstOrFail();
  628. $f->delete();
  629. return [200];
  630. }
  631. public function remoteDeliver($dm)
  632. {
  633. $profile = $dm->author;
  634. $url = $dm->recipient->sharedInbox ?? $dm->recipient->inbox_url;
  635. $tags = [
  636. [
  637. 'type' => 'Mention',
  638. 'href' => $dm->recipient->permalink(),
  639. 'name' => $dm->recipient->emailUrl(),
  640. ]
  641. ];
  642. $body = [
  643. '@context' => [
  644. 'https://www.w3.org/ns/activitystreams',
  645. 'https://w3id.org/security/v1',
  646. [
  647. 'sc' => 'http://schema.org#',
  648. 'Hashtag' => 'as:Hashtag',
  649. 'sensitive' => 'as:sensitive',
  650. ]
  651. ],
  652. 'id' => $dm->status->permalink(),
  653. 'type' => 'Create',
  654. 'actor' => $dm->status->profile->permalink(),
  655. 'published' => $dm->status->created_at->toAtomString(),
  656. 'to' => [$dm->recipient->permalink()],
  657. 'cc' => [],
  658. 'object' => [
  659. 'id' => $dm->status->url(),
  660. 'type' => 'Note',
  661. 'summary' => null,
  662. 'content' => $dm->status->rendered ?? $dm->status->caption,
  663. 'inReplyTo' => null,
  664. 'published' => $dm->status->created_at->toAtomString(),
  665. 'url' => $dm->status->url(),
  666. 'attributedTo' => $dm->status->profile->permalink(),
  667. 'to' => [$dm->recipient->permalink()],
  668. 'cc' => [],
  669. 'sensitive' => (bool) $dm->status->is_nsfw,
  670. 'attachment' => $dm->status->media()->orderBy('order')->get()->map(function ($media) {
  671. return [
  672. 'type' => $media->activityVerb(),
  673. 'mediaType' => $media->mime,
  674. 'url' => $media->url(),
  675. 'name' => $media->caption,
  676. ];
  677. })->toArray(),
  678. 'tag' => $tags,
  679. ]
  680. ];
  681. Helpers::sendSignedObject($profile, $url, $body);
  682. }
  683. public function remoteDelete($dm)
  684. {
  685. $profile = $dm->author;
  686. $url = $dm->recipient->sharedInbox ?? $dm->recipient->inbox_url;
  687. $body = [
  688. '@context' => [
  689. 'https://www.w3.org/ns/activitystreams',
  690. 'https://w3id.org/security/v1',
  691. [
  692. 'sc' => 'http://schema.org#',
  693. 'Hashtag' => 'as:Hashtag',
  694. 'sensitive' => 'as:sensitive',
  695. ]
  696. ],
  697. 'id' => $dm->status->permalink('#delete'),
  698. 'to' => [
  699. 'https://www.w3.org/ns/activitystreams#Public'
  700. ],
  701. 'type' => 'Delete',
  702. 'actor' => $dm->status->profile->permalink(),
  703. 'object' => [
  704. 'id' => $dm->status->url(),
  705. 'type' => 'Tombstone'
  706. ]
  707. ];
  708. Helpers::sendSignedObject($profile, $url, $body);
  709. }
  710. }