DirectMessageController.php 23 KB

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