DirectMessageController.php 32 KB

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