You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

253 lines
7.5 KiB

9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. <?php
  2. use Movim\Widget\Base;
  3. include_once WIDGETS_PATH.'Post/Post.php';
  4. use Illuminate\Database\Capsule\Manager as DB;
  5. class Menu extends Base
  6. {
  7. private $_paging = 10;
  8. public function load()
  9. {
  10. $this->registerEvent('post', 'onPost');
  11. $this->registerEvent('post_retract', 'onRetract', 'news');
  12. $this->registerEvent('pubsub_postdelete', 'onRetract', 'news');
  13. $this->registerEvent('pubsub_getitem_handle', 'onItem', 'news');
  14. $this->addjs('menu.js');
  15. $this->addcss('menu.css');
  16. }
  17. public function onItem($packet)
  18. {
  19. $post = $packet->content;
  20. if ($post && $post->isComment()) {
  21. $post = $post->getParent();
  22. }
  23. if ($post) {
  24. $this->rpc('MovimTpl.fill', '#menu_widget #'.cleanupId($post->nodeid), $this->preparePost($post));
  25. }
  26. }
  27. public function onRetract($packet)
  28. {
  29. $this->ajaxHttpGetAll();
  30. }
  31. public function onStream($count)
  32. {
  33. $view = $this->tpl();
  34. $view->assign('count', $count);
  35. $this->rpc('MovimTpl.fill', '#menu_refresh', $view->draw('_menu_refresh'));
  36. }
  37. public function onPost($packet)
  38. {
  39. $since = \App\Cache::c('since');
  40. if ($since) {
  41. $count = \App\Post::whereIn('id', function ($query) {
  42. $filters = DB::table('posts')->where('id', -1);
  43. $filters = \App\Post::withMineScope($filters);
  44. $filters = \App\Post::withContactsScope($filters);
  45. $filters = \App\Post::withSubscriptionsScope($filters);
  46. $query->select('id')->from(
  47. $filters,
  48. 'posts'
  49. );
  50. })->withoutComments()->where('published', '>', $since)->count();
  51. } else {
  52. $count = 0;
  53. }
  54. $post = $packet->content;
  55. if (!is_object($post)) {
  56. return;
  57. }
  58. $post = \App\Post::where('server', $post->server)
  59. ->where('node', $post->node)
  60. ->where('nodeid', $post->nodeid)
  61. ->first();
  62. if ($post === null) {
  63. return;
  64. }
  65. if ($post->isComment()
  66. && !$post->isMine()) {
  67. $contact = \App\Contact::firstOrNew(['id' => $post->aid]);
  68. $parent = $post->parent;
  69. if ($parent) {
  70. Notification::append(
  71. 'comments',
  72. '📝 ' . $parent->title,
  73. ($post->isLike()) ? '❤️ ' .$contact->truename : $post->title,
  74. $contact->getPhoto(),
  75. 2
  76. );
  77. }
  78. } elseif ($count > 0
  79. && (strtotime($post->published) > strtotime($since))) {
  80. if ($post->isMicroblog()) {
  81. $contact = \App\Contact::firstOrNew(['id' => $post->server]);
  82. $title = ($post->title == null)
  83. ? __('post.default_title')
  84. : $post->title;
  85. if (!$post->isMine()) {
  86. Notification::append(
  87. 'news',
  88. '📝 ' . $title,
  89. $contact->truename,
  90. $contact->getPhoto(),
  91. 2,
  92. $this->route('post', [$post->server, $post->node, $post->nodeid]),
  93. $this->route('contact', $post->server)
  94. );
  95. }
  96. } else {
  97. $info = \App\Info::where('server', $post->server)
  98. ->where('node', $post->node)
  99. ->first();
  100. $logo = null;
  101. $title = $post->node;
  102. if ($info) {
  103. if ($info->name) {
  104. $title = $info->name;
  105. }
  106. $logo = $info->getPhoto('l');
  107. }
  108. Notification::append(
  109. 'news',
  110. $title,
  111. $post->title,
  112. $logo,
  113. 2,
  114. $this->route('post', [$post->server, $post->node, $post->nodeid]),
  115. $this->route('community', [$post->server, $post->node])
  116. );
  117. }
  118. $this->onStream($count);
  119. }
  120. }
  121. public function ajaxHttpGetAll($page = 0)
  122. {
  123. $this->ajaxGet('all', null, null, $page);
  124. }
  125. public function ajaxHttpGetNews($page = 0)
  126. {
  127. $this->ajaxGet('news', null, null, $page);
  128. }
  129. public function ajaxHttpGetFeed($page = 0)
  130. {
  131. $this->ajaxGet('feed', null, null, $page);
  132. }
  133. public function ajaxGet($type = 'all', $server = null, $node = null, $page = 0)
  134. {
  135. $html = $this->prepareList($type, $server, $node, $page);
  136. $this->rpc('MovimTpl.fill', '#menu_widget', $html);
  137. $this->rpc('MovimUtils.enhanceArticlesContent');
  138. }
  139. public function prepareList($type = 'all', $server = null, $node = null, $page = 0)
  140. {
  141. $view = $this->tpl();
  142. $posts = \App\Post::whereIn('id', function ($query) {
  143. $filters = DB::table('posts')->where('id', -1);
  144. $filters = \App\Post::withMineScope($filters);
  145. $filters = \App\Post::withContactsScope($filters);
  146. $filters = \App\Post::withSubscriptionsScope($filters);
  147. $query->select('id')->from(
  148. $filters,
  149. 'posts'
  150. );
  151. });
  152. $since = \App\Cache::c('since');
  153. $count = ($since)
  154. ? $posts->where('published', '>', $since)->count()
  155. : 0;
  156. // getting newer, not older
  157. if ($page == 0) {
  158. $count = 0;
  159. $last = $posts->orderBy('published', 'desc')->first();
  160. \App\Cache::c('since', ($last) ? $last->published : date(MOVIM_SQL_DATE));
  161. }
  162. $items = \App\Post::skip($page * $this->_paging + $count)->withoutComments();
  163. $items->whereIn('id', function ($query) use ($type) {
  164. $filters = DB::table('posts')->where('id', -1);
  165. if (in_array($type, ['all', 'feed'])) {
  166. $filters = \App\Post::withContactsScope($filters);
  167. $filters = \App\Post::withMineScope($filters);
  168. }
  169. if (in_array($type, ['all', 'news'])) {
  170. $filters = \App\Post::withSubscriptionsScope($filters);
  171. }
  172. $query->select('id')->from(
  173. $filters,
  174. 'posts'
  175. );
  176. });
  177. $view->assign('previous', $this->route('news', $page-1));
  178. $view->assign('next', $this->route('news', $page+1));
  179. if ($type == 'news') {
  180. $view->assign('previous', $this->route('news', $page-1, [], 'communities'));
  181. $view->assign('next', $this->route('news', $page+1, [], 'communities'));
  182. } elseif ($type == 'feed') {
  183. $view->assign('previous', $this->route('news', $page-1, [], 'contacts'));
  184. $view->assign('next', $this->route('news', $page+1, [], 'contacts'));
  185. }
  186. $items = $items
  187. ->orderBy('published', 'desc')
  188. ->take($this->_paging)->get();
  189. if ($items->isNotEmpty()) {
  190. $items = resolveInfos($items);
  191. }
  192. $view->assign('items', $items);
  193. $view->assign('type', $type);
  194. $view->assign('page', $page);
  195. $view->assign('paging', $this->_paging);
  196. return $view->draw('_menu_list');
  197. }
  198. public function preparePost($post)
  199. {
  200. return (new \Post)->preparePost($post, false, true);
  201. }
  202. }