DirectMessageController.php 34 KB

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