DirectMessageController.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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. abort_if($user->created_at->gt(now()->subHours(72)), 400, 'You need to wait a bit before you can DM another account');
  298. $profile = $user->profile;
  299. $recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id'));
  300. abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403);
  301. $msg = $request->input('message');
  302. if ((! $recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) {
  303. if ($recipient->follows($profile) == true) {
  304. $hidden = false;
  305. } else {
  306. $hidden = true;
  307. }
  308. } else {
  309. $hidden = false;
  310. }
  311. $status = new Status;
  312. $status->profile_id = $profile->id;
  313. $status->caption = $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 ($query) use ($pid, $uid) {
  397. $query->where('from_id', $pid)->where('to_id', $uid);
  398. })->orWhere(function ($query) use ($pid, $uid) {
  399. $query->where('from_id', $uid)->where('to_id', $pid);
  400. })
  401. ->orderBy('id', 'asc')
  402. ->take(8)
  403. ->get()
  404. ->reverse();
  405. } elseif ($max_id) {
  406. $res = DirectMessage::select('*')
  407. ->where('id', '<', $max_id)
  408. ->where(function ($query) use ($pid, $uid) {
  409. $query->where('from_id', $pid)->where('to_id', $uid);
  410. })->orWhere(function ($query) use ($pid, $uid) {
  411. $query->where('from_id', $uid)->where('to_id', $pid);
  412. })
  413. ->orderBy('id', 'desc')
  414. ->take(8)
  415. ->get();
  416. } else {
  417. $res = DirectMessage::where(function ($query) use ($pid, $uid) {
  418. $query->where('from_id', $pid)->where('to_id', $uid);
  419. })->orWhere(function ($query) use ($pid, $uid) {
  420. $query->where('from_id', $uid)->where('to_id', $pid);
  421. })
  422. ->orderBy('id', 'desc')
  423. ->take(8)
  424. ->get();
  425. }
  426. $res = $res->filter(function ($s) {
  427. return $s && $s->status;
  428. })
  429. ->map(function ($s) use ($uid) {
  430. return [
  431. 'id' => (string) $s->id,
  432. 'hidden' => (bool) $s->is_hidden,
  433. 'isAuthor' => $uid == $s->from_id,
  434. 'type' => $s->type,
  435. 'text' => $s->status->caption,
  436. 'media' => $s->status->firstMedia() ? $s->status->firstMedia()->url() : null,
  437. 'carousel' => MediaService::get($s->status_id),
  438. 'created_at' => $s->created_at->format('c'),
  439. 'timeAgo' => $s->created_at->diffForHumans(null, null, true),
  440. 'seen' => $s->read_at != null,
  441. 'reportId' => (string) $s->status_id,
  442. 'meta' => json_decode($s->meta, true),
  443. ];
  444. })
  445. ->values();
  446. $filters = UserFilterService::mutes($uid);
  447. $w = [
  448. 'id' => (string) $r->id,
  449. 'name' => $r->name,
  450. 'username' => $r->username,
  451. 'avatar' => $r->avatarUrl(),
  452. 'url' => $r->url(),
  453. 'muted' => in_array($r->id, $filters),
  454. 'isLocal' => (bool) ! $r->domain,
  455. 'domain' => $r->domain,
  456. 'created_at' => $r->created_at->format('c'),
  457. 'updated_at' => $r->updated_at->format('c'),
  458. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  459. 'lastMessage' => '',
  460. 'messages' => $res,
  461. ];
  462. return response()->json($w, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  463. }
  464. public function delete(Request $request)
  465. {
  466. $this->validate($request, [
  467. 'id' => 'required',
  468. ]);
  469. $sid = $request->input('id');
  470. $pid = $request->user()->profile_id;
  471. $dm = DirectMessage::whereFromId($pid)
  472. ->whereStatusId($sid)
  473. ->firstOrFail();
  474. $status = Status::whereProfileId($pid)
  475. ->findOrFail($dm->status_id);
  476. $recipient = AccountService::get($dm->to_id);
  477. if (! $recipient) {
  478. return response('', 422);
  479. }
  480. if ($recipient['local'] == false) {
  481. $dmc = $dm;
  482. $this->remoteDelete($dmc);
  483. } else {
  484. StatusDelete::dispatch($status)->onQueue('high');
  485. }
  486. if (Conversation::whereStatusId($sid)->count()) {
  487. $latest = DirectMessage::where(['from_id' => $dm->from_id, 'to_id' => $dm->to_id])
  488. ->orWhere(['to_id' => $dm->from_id, 'from_id' => $dm->to_id])
  489. ->latest()
  490. ->first();
  491. if ($latest->status_id == $sid) {
  492. Conversation::where(['to_id' => $dm->from_id, 'from_id' => $dm->to_id])
  493. ->update([
  494. 'updated_at' => $latest->updated_at,
  495. 'status_id' => $latest->status_id,
  496. 'type' => $latest->type,
  497. 'is_hidden' => false,
  498. ]);
  499. Conversation::where(['to_id' => $dm->to_id, 'from_id' => $dm->from_id])
  500. ->update([
  501. 'updated_at' => $latest->updated_at,
  502. 'status_id' => $latest->status_id,
  503. 'type' => $latest->type,
  504. 'is_hidden' => false,
  505. ]);
  506. } else {
  507. Conversation::where([
  508. 'status_id' => $sid,
  509. 'to_id' => $dm->from_id,
  510. 'from_id' => $dm->to_id,
  511. ])->delete();
  512. Conversation::where([
  513. 'status_id' => $sid,
  514. 'from_id' => $dm->from_id,
  515. 'to_id' => $dm->to_id,
  516. ])->delete();
  517. }
  518. }
  519. StatusService::del($status->id, true);
  520. $status->forceDeleteQuietly();
  521. return [200];
  522. }
  523. public function get(Request $request, $id)
  524. {
  525. $user = $request->user();
  526. abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
  527. $pid = $request->user()->profile_id;
  528. $dm = DirectMessage::whereStatusId($id)->firstOrFail();
  529. abort_if($pid !== $dm->to_id && $pid !== $dm->from_id, 404);
  530. return response()->json($dm, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
  531. }
  532. public function mediaUpload(Request $request)
  533. {
  534. $this->validate($request, [
  535. 'file' => function () {
  536. return [
  537. 'required',
  538. 'mimetypes:'.config_cache('pixelfed.media_types'),
  539. 'max:'.config_cache('pixelfed.max_photo_size'),
  540. ];
  541. },
  542. 'to_id' => 'required',
  543. ]);
  544. $user = $request->user();
  545. abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
  546. $profile = $user->profile;
  547. $recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id'));
  548. abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403);
  549. if ((! $recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) {
  550. if ($recipient->follows($profile) == true) {
  551. $hidden = false;
  552. } else {
  553. $hidden = true;
  554. }
  555. } else {
  556. $hidden = false;
  557. }
  558. $accountSize = UserStorageService::get($user->id);
  559. abort_if($accountSize === -1, 403, 'Invalid request.');
  560. $photo = $request->file('file');
  561. $fileSize = $photo->getSize();
  562. $sizeInKbs = (int) ceil($fileSize / 1000);
  563. $updatedAccountSize = (int) $accountSize + (int) $sizeInKbs;
  564. if ((bool) config_cache('pixelfed.enforce_account_limit') == true) {
  565. $limit = (int) config_cache('pixelfed.max_account_size');
  566. if ($updatedAccountSize >= $limit) {
  567. abort(403, 'Account size limit reached.');
  568. }
  569. }
  570. $mimes = explode(',', config_cache('pixelfed.media_types'));
  571. if (in_array($photo->getMimeType(), $mimes) == false) {
  572. abort(403, 'Invalid or unsupported mime type.');
  573. }
  574. $storagePath = MediaPathService::get($user, 2).Str::random(8);
  575. $path = $photo->storePublicly($storagePath);
  576. $hash = \hash_file('sha256', $photo);
  577. abort_if(MediaBlocklistService::exists($hash) == true, 451);
  578. $status = new Status;
  579. $status->profile_id = $profile->id;
  580. $status->caption = null;
  581. $status->visibility = 'direct';
  582. $status->scope = 'direct';
  583. $status->in_reply_to_profile_id = $recipient->id;
  584. $status->save();
  585. $media = new Media;
  586. $media->status_id = $status->id;
  587. $media->profile_id = $profile->id;
  588. $media->user_id = $user->id;
  589. $media->media_path = $path;
  590. $media->original_sha256 = $hash;
  591. $media->size = $photo->getSize();
  592. $media->mime = $photo->getMimeType();
  593. $media->caption = null;
  594. $media->filter_class = null;
  595. $media->filter_name = null;
  596. $media->save();
  597. $dm = new DirectMessage;
  598. $dm->to_id = $recipient->id;
  599. $dm->from_id = $profile->id;
  600. $dm->status_id = $status->id;
  601. $dm->type = array_first(explode('/', $media->mime)) == 'video' ? 'video' : 'photo';
  602. $dm->is_hidden = $hidden;
  603. $dm->save();
  604. Conversation::updateOrInsert(
  605. [
  606. 'to_id' => $recipient->id,
  607. 'from_id' => $profile->id,
  608. ],
  609. [
  610. 'type' => $dm->type,
  611. 'status_id' => $status->id,
  612. 'dm_id' => $dm->id,
  613. 'is_hidden' => $hidden,
  614. ]
  615. );
  616. $user->storage_used = (int) $updatedAccountSize;
  617. $user->storage_used_updated_at = now();
  618. $user->save();
  619. if ($recipient->domain) {
  620. $this->remoteDeliver($dm);
  621. }
  622. return [
  623. 'id' => $dm->id,
  624. 'reportId' => (string) $dm->status_id,
  625. 'type' => $dm->type,
  626. 'url' => $media->url(),
  627. ];
  628. }
  629. public function composeLookup(Request $request)
  630. {
  631. $this->validate($request, [
  632. 'q' => 'required|string|min:2|max:50',
  633. 'remote' => 'nullable',
  634. ]);
  635. $user = $request->user();
  636. if ($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id)) {
  637. return [];
  638. }
  639. $q = $request->input('q');
  640. $r = $request->input('remote', false);
  641. if ($r && ! Str::of($q)->contains('.')) {
  642. return [];
  643. }
  644. if ($r && Helpers::validateUrl($q)) {
  645. Helpers::profileFetch($q);
  646. }
  647. if (Str::of($q)->startsWith('@')) {
  648. if (strlen($q) < 3) {
  649. return [];
  650. }
  651. if (substr_count($q, '@') == 2) {
  652. WebfingerService::lookup($q);
  653. }
  654. $q = mb_substr($q, 1);
  655. }
  656. $blocked = UserFilter::whereFilterableType('App\Profile')
  657. ->whereFilterType('block')
  658. ->whereFilterableId($request->user()->profile_id)
  659. ->pluck('user_id');
  660. $blocked->push($request->user()->profile_id);
  661. $results = Profile::select('id', 'domain', 'username')
  662. ->whereNotIn('id', $blocked)
  663. ->where('username', 'like', '%'.$q.'%')
  664. ->orderBy('domain')
  665. ->limit(8)
  666. ->get()
  667. ->map(function ($r) {
  668. $acct = AccountService::get($r->id);
  669. return [
  670. 'local' => (bool) ! $r->domain,
  671. 'id' => (string) $r->id,
  672. 'name' => $r->username,
  673. 'privacy' => true,
  674. 'avatar' => $r->avatarUrl(),
  675. 'account' => $acct,
  676. ];
  677. });
  678. return $results;
  679. }
  680. public function read(Request $request)
  681. {
  682. $this->validate($request, [
  683. 'pid' => 'required',
  684. 'sid' => 'required',
  685. ]);
  686. $pid = $request->input('pid');
  687. $sid = $request->input('sid');
  688. $user = $request->user();
  689. abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
  690. $dms = DirectMessage::whereToId($request->user()->profile_id)
  691. ->whereFromId($pid)
  692. ->where('status_id', '>=', $sid)
  693. ->get();
  694. $now = now();
  695. foreach ($dms as $dm) {
  696. $dm->read_at = $now;
  697. $dm->save();
  698. }
  699. return response()->json($dms->pluck('id'));
  700. }
  701. public function mute(Request $request)
  702. {
  703. $this->validate($request, [
  704. 'id' => 'required',
  705. ]);
  706. $user = $request->user();
  707. abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
  708. $fid = $request->input('id');
  709. $pid = $request->user()->profile_id;
  710. UserFilter::firstOrCreate(
  711. [
  712. 'user_id' => $pid,
  713. 'filterable_id' => $fid,
  714. 'filterable_type' => 'App\Profile',
  715. 'filter_type' => 'dm.mute',
  716. ]
  717. );
  718. return [200];
  719. }
  720. public function unmute(Request $request)
  721. {
  722. $this->validate($request, [
  723. 'id' => 'required',
  724. ]);
  725. $user = $request->user();
  726. abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
  727. $fid = $request->input('id');
  728. $pid = $request->user()->profile_id;
  729. $f = UserFilter::whereUserId($pid)
  730. ->whereFilterableId($fid)
  731. ->whereFilterableType('App\Profile')
  732. ->whereFilterType('dm.mute')
  733. ->firstOrFail();
  734. $f->delete();
  735. return [200];
  736. }
  737. public function remoteDeliver($dm)
  738. {
  739. $profile = $dm->author;
  740. $url = $dm->recipient->sharedInbox ?? $dm->recipient->inbox_url;
  741. $status = $dm->status;
  742. if (! $status) {
  743. return;
  744. }
  745. $tags = [
  746. [
  747. 'type' => 'Mention',
  748. 'href' => $dm->recipient->permalink(),
  749. 'name' => $dm->recipient->emailUrl(),
  750. ],
  751. ];
  752. $content = $status->caption ? Autolink::create()->autolink($status->caption) : null;
  753. $body = [
  754. '@context' => [
  755. 'https://w3id.org/security/v1',
  756. 'https://www.w3.org/ns/activitystreams',
  757. ],
  758. 'id' => $dm->status->permalink(),
  759. 'type' => 'Create',
  760. 'actor' => $dm->status->profile->permalink(),
  761. 'published' => $dm->status->created_at->toAtomString(),
  762. 'to' => [$dm->recipient->permalink()],
  763. 'cc' => [],
  764. 'object' => [
  765. 'id' => $dm->status->url(),
  766. 'type' => 'Note',
  767. 'summary' => null,
  768. 'content' => $content,
  769. 'inReplyTo' => null,
  770. 'published' => $dm->status->created_at->toAtomString(),
  771. 'url' => $dm->status->url(),
  772. 'attributedTo' => $dm->status->profile->permalink(),
  773. 'to' => [$dm->recipient->permalink()],
  774. 'cc' => [],
  775. 'sensitive' => (bool) $dm->status->is_nsfw,
  776. 'attachment' => $dm->status->media()->orderBy('order')->get()->map(function ($media) {
  777. return [
  778. 'type' => $media->activityVerb(),
  779. 'mediaType' => $media->mime,
  780. 'url' => $media->url(),
  781. 'name' => $media->caption,
  782. ];
  783. })->toArray(),
  784. 'tag' => $tags,
  785. ],
  786. ];
  787. DirectDeliverPipeline::dispatch($profile, $url, $body)->onQueue('high');
  788. }
  789. public function remoteDelete($dm)
  790. {
  791. $profile = $dm->author;
  792. $url = $dm->recipient->sharedInbox ?? $dm->recipient->inbox_url;
  793. $body = [
  794. '@context' => [
  795. 'https://www.w3.org/ns/activitystreams',
  796. ],
  797. 'id' => $dm->status->permalink('#delete'),
  798. 'to' => [
  799. 'https://www.w3.org/ns/activitystreams#Public',
  800. ],
  801. 'type' => 'Delete',
  802. 'actor' => $dm->status->profile->permalink(),
  803. 'object' => [
  804. 'id' => $dm->status->url(),
  805. 'type' => 'Tombstone',
  806. ],
  807. ];
  808. DirectDeletePipeline::dispatch($profile, $url, $body)->onQueue('high');
  809. }
  810. }