1
0

DirectMessageController.php 34 KB

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