def delete(self): try: user_id = self.get_argument('user_id') room_id = self.get_argument('room_id') except MissingArgumentError: return self.error(MISSING_PARAMS) user = User.get(user_id) if not user: return self.error(USER_NOT_FOUND) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) if int(room.creator_id) == int(user_id): return self.error(CREATOR_COULD_NOT_EXIT_ROOM) try: RoomUser.delete_room_user(room_id, user_id) except Exception as e: logger.error(u'用户退出房间失败。User:[%s], Room:[%s], Error:[%s]' % (user_id, room_id, e)) return self.render({ 'status': 0, })
def post(self): try: room_id = self.get_argument('room_id') user_id = self.get_argument('user_id') except MissingArgumentError: return self.error(MISSING_PARAMS) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) if int(room.creator_id) == int(user_id): return self.error(CREATOR_NOT_NEED_JOIN_ROOM) if RoomUser.room_exists_user(room_id, user_id): return self.error(ROOM_EXISTS_USER) if RoomBlackUser.is_room_black_user(room_id, user_id): return self.error(USER_IN_ROOM_BLACK_LIST) if room.private_not_join: return self.error(ROOM_NOT_JOIN) try: RoomUser.add(room_id, user_id, ROOM_USER_NORMAL) return self.render({ "status": 0, "data": room.jsonify(), }) except Exception as e: logger.error(u'用户加入房间失败。User:[%s] Room:[%s] Error:[%s]' % (user_id, room_id, e)) return self.error(SYSTEM_ERROR)
def post(self): try: user_id = int(self.get_argument('user_id')) room_id = self.get_argument('room_id') admin_id = int(self.get_argument('admin_id')) except MissingArgumentError: return self.error(MISSING_PARAMS) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) if int(room.creator_id) != user_id or user_id == admin_id: return self.error(ACCESS_NOT_ALLOWED) if not RoomUser.room_exists_user(room_id, admin_id): return self.error(ACCESS_NOT_ALLOWED) room_admin_count = RoomUser.get_room_admin_count(room_id) if room_admin_count >= ROOM_ADMIN_USER_MAX_AMOUNT: return self.error(ROOM_ADMIN_USER_OVER_LIMIT) try: RoomUser.set_room_admin(room_id, admin_id) except Exception as e: logger.error(u'添加房间管理员失败。Error:[%s]' % e) return self.error(SYSTEM_ERROR) return self.render({ 'status': 0, })
def receive_trade_payment(self): db = DbManager().db trades = self.trades now = datetime.now() all_trades_complete = True for trade in trades: if trade.status != STATUS_COMPLETE: return if all_trades_complete: try: db.execute('begin;') for trade in trades: if trade.pay_method == PAYMETHOD_ACCOUNT: sql = 'update {table} set balance=balance-%s where user_id=%s'.format( table=Account.table) db.execute(sql, float(trade.amount), self.creator_id) sql = 'update {table} set balance=balance+%s where room_id=%s'.format( table=StarFund.table) db.execute(sql, float(self.amount), self.room_id) sql = 'update {table} set status=%s, pay_time=%s where id=%s'.format( table=self.table) db.execute(sql, STATUS_COMPLETE, now, self.id) sql = 'insert into {table} (user_id, source, order_type, order_id, amount, create_time) values ' \ '(%s, %s, %s, %s, %s, %s)'.format(table=WalletRecord.table) db.execute(sql, self.creator_id, WALLET_RECORD_ROOM_RENT, ORDER_ROOM, self.id, -self.amount, now) db.execute('commit;') except: db.execute('rollback;') raise RoomUser.add(self.room_id, self.creator_id) self.flush_fund_cache()
def post(self): try: user_id = self.get_argument('user_id') room_id = self.get_argument('room_id') except MissingArgumentError: return self.error(MISSING_PARAMS) user = User.get(user_id) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) if int(room.creator_id) == int(user_id): return self.error(CREATOR_NOT_NEED_JOIN_ROOM) if RoomUser.room_exists_user(room_id, user_id): return self.error(ROOM_EXISTS_USER) if RoomBlackUser.is_room_black_user(room_id, user_id): return self.error(USER_IN_ROOM_BLACK_LIST) # 房主自身也属于房间成员之一 if room.limit_user_number: room_user_amount = RoomUser.get_user_amount_by_room(room_id) if room_user_amount == room.limit_user_number: return self.error(ROOM_USER_FULL) if room.private_not_join: return self.error(ROOM_NOT_JOIN) if room.private_need_verify: room_question = RoomQuestion.get_question_by_room(room_id) return self.render({ 'status': 0, 'data': room_question.jsonify() if room_question else None }) else: try: RoomUser.add(room_id, user_id, ROOM_USER_NORMAL) # handle_join_room_integral.delay(user_id, room_id) # user_amount = room.user_amount # if user_amount == 500: # handle_room_user_over_500_integral.delay(room.creator_id, room.id) return self.render({ "status": 0, "data": room.jsonify(user), }) except Exception as e: logger.error(u'用户加入房间失败。User:[%s] Room:[%s] Error:[%s]' % (user_id, room_id, e)) return self.error(SYSTEM_ERROR)
def add(cls, creator_id, name, intro, tag_ids, avatar, limit_user_number, create_time, status=ROOM_PUBLIC, question_info=None): from starmachine.model.room_tag import RoomTag from starmachine.model.room_user import RoomUser db = DbManager().db cache = CacheManager().cache db.execute('begin;') try: sql = 'insert into {table} (creator_id, name, intro, avatar, limit_user_number, status, create_time) ' \ 'values (%s, %s, %s, %s, %s, %s, %s)'.format(table=cls.table) room_id = db.execute(sql, creator_id, name, intro, avatar, limit_user_number, status, create_time) if int(status) == ROOM_PRIVATE_NEED_VERIFY and question_info: question_name = question_info.get('question_name') options = question_info.get('options') sql = 'insert into {table} (room_id, name, create_time) values (%s, %s, %s)'.format( table=RoomQuestion.table) room_question_id = db.execute(sql, room_id, question_name, create_time) for option in options: text = option.get('text') is_right_answer = option.get('is_right_answer') sql = 'insert into {table} (question_id, text, is_right_answer) values (%s, %s, %s)'.format( table=RoomQuestionOption.table) db.execute(sql, room_question_id, text, is_right_answer) for tag_id in tag_ids: sql = 'insert into {table} (room_id, tag_id) values (%s, %s)'.format( table=RoomTag.table) db.execute(sql, room_id, tag_id) sql = 'insert into {table} (room_id, balance) values (%s, %s)'.format( table=StarFund.table) db.execute(sql, room_id, '0.00') db.execute('commit;') except: db.execute('rollback;') raise cache_key = USER_CREATED_ROOM_KEY % creator_id cache.sadd(cache_key, room_id) RoomUser.add(room_id, creator_id, ROOM_USER_CREATOR) return room_id
def has_room_create_welfare_access(self, room): from starmachine.model.room_user import RoomUser if int(room.creator_id) == int(self.id) or RoomUser.is_room_admin( room.id, self.id): return True return False
def post(self): user_id = self.get_argument('user_id') text = self.get_argument('text', '') room_id = self.get_argument('room_id') video = self.get_argument('video', '') images = self.get_argument('images', '[]') if not room_id: return self.error(MISSING_PARAMS) if filter.check_sensitive(text): return self.error(SENSITIVE_WORD_EXISTS) user = User.get(user_id) if not user: return self.error(USER_NOT_FOUND) room_user = RoomUser.get_by_room_and_user(room_id, user_id) if not user.super_user: if not room_user: return self.error(ACCESS_NOT_ALLOWED) if int(room_user.status) == ROOM_USER_SILENT: return self.error(USER_IN_ROOM_SILENT_LIST) try: posts = Posts.add(user_id, room_id, text, images, video, room_user.status) except Exception as e: logger.error(u'添加帖子内容失败。Error:[%s]' % e) return self.error(SYSTEM_ERROR) handle_daily_create_content.delay(user_id, posts.id) return self.render({'status': 0, 'data': posts.jsonify()})
def post(self): try: room_id = self.get_argument('room_id') user_id = self.get_argument('user_id') silent_user_id = self.get_argument('silent_user_id') except MissingArgumentError: return self.error(MISSING_PARAMS) set_time = datetime.now() operator = User.get(user_id) silent_user = User.get(silent_user_id) if not silent_user: return self.error(USER_NOT_FOUND) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) room_operator = RoomUser.get_by_room_and_user(room_id, user_id) room_silent_user = RoomUser.get_by_room_and_user( room_id, silent_user_id) operator_status = room_operator.status if not room_operator.has_room_user_handle_access(room_silent_user): return self.error(ACCESS_NOT_ALLOWED) try: RoomUser.set_user_silent(room_id, silent_user_id, set_time) except Exception as e: logger.error(u'用户房间禁言失败。User:[%s], Room:[%s], Error:[%s]' % (silent_user_id, room_id, e)) return self.error(SYSTEM_ERROR) self.render({ 'status': 0, }) room_silent_user_notify.delay(operator, operator_status, room, silent_user) # scheduler = TornadoScheduler() # scheduler.add_jobstore('redis', jobs_key='room_user:silent:jobs', run_times_key='room_user:remove_silent') run_date = set_time + timedelta(days=1) tornado_scheduler.add_job( remove_room_silent_user, 'date', run_date=run_date, args=[room_id, user_id, room_silent_user.status]) # scheduler.start() return
def get(self): try: user_id = self.get_argument('user_id', '') start = int(self.get_argument('start', 0)) count = int(self.get_argument('count', 10)) except MissingArgumentError: return self.error(MISSING_PARAMS) user = User.get(user_id) if not user: return self.error(USER_NOT_FOUND) has_joined_rooms = RoomUser.get_rooms_by_user(user_id, 0, 1) if has_joined_rooms: join_rooms = RoomUser.get_rooms_by_user_order_by_update_time(user_id, start, count) return self.render({ 'status': 0, 'data': { 'recommend': 'joined', 'data': [room.jsonify(user) for room in join_rooms], } }) user_tags = UserTag.get_tags_by_user(user_id) if user_tags: rooms = RoomTag.get_rooms_by_tags(user_tags) if rooms: rooms = sorted(rooms, key=lambda room: room.user_amount, reverse=True) end = start + count - 1 rooms = rooms[start: end] return self.render({ 'status': 0, 'data': { 'recommend': 'tags', 'data': [room.jsonify(user) for room in rooms], } }) rooms = Room.get_rooms_by_user_amount(start, count) return self.render({ 'status': 0, 'data': { 'recommend': 'hot', 'data': [room.jsonify(user) for room in rooms if int(room.id) not in [100043, 100053]], } })
def jsonify(self, user=None): from starmachine.model.content.content_like import ContentLiked options = VoteOption.gets_option_by_vote(self.id) data = { 'id': self.id, 'creator': { 'id': self.creator.id, 'name': self.creator.user_name, 'avatar': self.creator.avatar_url, }, 'name': self.name, 'deadline': self.deadline.strftime('%Y-%m-%d %H:%M:%S'), 'create_time': self.create_time if isinstance(self.create_time, basestring) else self.create_time.strftime('%Y-%m-%d %H:%M:%S'), 'options': [option.jsonify() for option in options], 'content_type': VOTE_TYPE, 'reward_amount': self.reward_amount, 'like_amount': self.like_amount, 'comment_amount': self.comment_amount, 'reward_user_amount': self.reward_user_amount, 'voted_amount': self.voted_amount, 'expired': self.expired, 'robed_user_amount': 0, } if user: data.update({ 'has_liked': bool(ContentLiked.has_liked(self.id, user.id)), 'has_voted': bool(VoteResult.has_voted(self.id, user.id)), 'has_robed': False, 'has_delivery': False, }) room_user = RoomUser.get_by_room_and_user(self.room_id, user.id) if room_user: data.update({ 'room_user_status': room_user.status, }) return data
def get(self): user_id = self.get_argument('user_id') target_user_id = self.get_argument('target_user_id') start = int(self.get_argument('start', 0)) count = int(self.get_argument('count', 10)) user = User.get(user_id) rooms = RoomUser.get_rooms_by_user(target_user_id, start, count) return self.render({ 'status': 0, 'data': [room.jsonify(user) for room in rooms] })
def post(self): try: user_id = self.get_argument('user_id') room_id = self.get_argument('room_id') welfare_type = self.get_argument('welfare_type', WELFARE_TYPE_SPECIAL) price = float(self.get_argument('price')) count = int(self.get_argument('count')) name = self.get_argument('name') images = self.get_argument('images') start_time = self.get_argument('start_time') deadline = self.get_argument('deadline') except MissingArgumentError: return self.error(MISSING_PARAMS) user = User.get(user_id) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) star_fund = StarFund.get_by_room(room_id) amount = price * count + count * WELFARE_POSTAGE if float(star_fund.balance) < amount: return self.error(ROOM_STAR_FUND_NOT_ENOUGH) room_user = RoomUser.get_by_room_and_user(room_id, user_id) if not room_user or not int( room_user.status) in (ROOM_USER_ADMIN, ROOM_USER_CREATOR): return self.error(ACCESS_NOT_ALLOWED) try: welfare = Welfare.add(user_id, room_id, welfare_type, price, count, name, start_time, deadline, images, room_user.status, amount) except Exception as e: logger.error(u'创建福利失败。Error:[%s]' % e) return self.error(SYSTEM_ERROR) self.render({ 'status': 0, 'data': welfare.jsonify(), }) handle_daily_create_welfare.delay(user_id, welfare.id) welfare_created_notify.delay(room, welfare) # scheduler = TornadoScheduler() # scheduler.add_jobstore('redis', jobs_key='welfare:jobs', run_times_key='welfare:deadline') # tornado_scheduler.add_jobstore('redis', jobs_key='welfare:jobs', run_times_key='welfare:deadline') tornado_scheduler.add_job(welfare_end_notify, 'date', run_date=welfare.deadline, args=[welfare]) return
def post(self): try: user_id = self.get_argument('user_id') room_id = self.get_argument('room_id') question_id = self.get_argument('question_id') option_id = self.get_argument('option_id') except MissingArgumentError: return self.error(MISSING_PARAMS) user = User.get(user_id) room = Room.get(room_id) question = RoomQuestion.get(question_id) option = RoomQuestionOption.get(option_id) if not question: return self.error(ROOM_QUESTION_NOT_FOUND) if int(question.room_id) != int(room_id) or option.question_id != int(question_id): return self.error(ACCESS_NOT_ALLOWED) user_room_question_24_hours_limit = 'user:%s:room:%s:question:24:limit' LIMIT_SECONDS = 24 * 60 * 60 redis_key = user_room_question_24_hours_limit % (user_id, room_id) cache = CacheManager().cache if cache.exists(redis_key): return self.error(ROOM_QUESTION_ANSWER_LIMIT) cache.set(redis_key, 'true') cache.expire(redis_key, LIMIT_SECONDS) if option.is_right_answer: try: RoomUser.add(room_id, user_id, ROOM_USER_NORMAL) return self.render({ "status": 0, "data": room.jsonify(user), }) except Exception as e: logger.error(u'用户加入房间失败。User:[%s] Room:[%s] Error:[%s]' % (user_id, room_id, e)) return self.error(SYSTEM_ERROR) else: return self.error(ROOM_QUESTION_ANSWER_WRONG)
def has_room_access(self, room_id): from starmachine.model.room_user import RoomUser from starmachine.model.room import Room room = Room.get(room_id) if self.super_user: return True if int(room.creator_id) == int(self.id): return True if RoomUser.room_exists_user(room_id, self.id): return True return False
def post(self): try: user_id = self.get_argument('user_id') room_id = self.get_argument('room_id') name = self.get_argument('name') deadline = self.get_argument('deadline') options = self.get_argument('options') options = json.loads(options) except (MissingArgumentError, TypeError): return self.error(MISSING_PARAMS) if filter.check_sensitive(name): return self.error(SENSITIVE_WORD_EXISTS) for op in options: if filter.check_sensitive(op): return self.error(SENSITIVE_WORD_EXISTS) if len(op) > 24: return self.error(OPTION_TEXT_TOO_LONG) user = User.get(user_id) if not user: return self.error(USER_NOT_FOUND) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) room_user = RoomUser.get_by_room_and_user(room_id, user_id) if not user.super_user: if not room_user: return self.error(ACCESS_NOT_ALLOWED) if int(room_user.status) == ROOM_USER_SILENT: return self.error(USER_IN_ROOM_SILENT_LIST) try: vote = Vote.add(user_id, room_id, name, deadline, options, room_user.status) except Exception as e: logger.error(u'添加投票失败。Error:[%s]' % e) return self.error(SYSTEM_ERROR) handle_daily_create_content.delay(user_id, vote.id) return self.render({ 'status': 0, 'data': vote.jsonify(), })
def post(self): try: room_id = self.get_argument('room_id') user_id = self.get_argument('user_id') remove_user_id = self.get_argument('remove_user_id') except MissingArgumentError: return self.error(MISSING_PARAMS) operator = User.get(user_id) remove_user = User.get(remove_user_id) if not remove_user: return self.error(USER_NOT_FOUND) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) room_operator = RoomUser.get_by_room_and_user(room_id, user_id) room_remove_user = RoomUser.get_by_room_and_user( room_id, remove_user_id) if not room_operator.has_room_user_handle_access(room_remove_user): return self.error(ACCESS_NOT_ALLOWED) try: RoomUser.remove_and_black_user(room_id, remove_user_id) except Exception as e: logger.error(u'房间删除拉黑用户失败。User:[%s], Room:[%s], Error:[%s]' % (remove_user_id, room_id, e)) return self.error(SYSTEM_ERROR) self.render({ 'status': 0, }) room_remove_user_notify(operator, room_operator.status, room, remove_user) return
def delete(self): try: user_id = int(self.get_argument('user_id')) room_id = self.get_argument('room_id') admin_id = self.get_argument('admin_id') except MissingArgumentError: return self.error(MISSING_PARAMS) room = Room.get(room_id) if not room: return self.error(ROOM_NOT_FOUND) if int(room.creator_id) != user_id: return self.error(ACCESS_NOT_ALLOWED) try: RoomUser.delete_room_admin(room_id, admin_id) except Exception as e: logger.error(u'删除管理员失败。Error:[%s]' % e) return self.error(SYSTEM_ERROR) return self.render({ 'status': 0, })
def welfare_created_notify(room, welfare): user_ids = RoomUser.get_user_ids_by_room(room.id) welfare_name = welfare.name if not isinstance(welfare_name, unicode): welfare_name = welfare_name.decode('utf-8') if len(welfare_name) > 10: welfare_name = welfare_name[:10] + u'……' room_name = room.name if not isinstance(room_name, unicode): room_name = room_name.decode('utf-8') text = u'%s房间发福利啦:"%s"' % (room_name, welfare_name) for user_id in user_ids: Notify.add(welfare.creator_id, user_id, NOTIFY_ACTION_WELFARE_CREATED, welfare.id, extra_info=text)
def get(self): try: user_id = self.get_argument('user_id') room_id = self.get_argument('room_id') start = int(self.get_argument('start', 0)) count = int(self.get_argument('count', 10)) except MissingArgumentError: return self.error(MISSING_PARAMS) user = User.get(user_id) # if not user.has_room_access(room_id): # return self.error(ACCESS_NOT_ALLOWED) room_users = RoomUser.get_ordered_room_users_by_room( room_id, start, count) data = [] for room_user in room_users: user_id = room_user.user_id user = User.get(user_id) status = room_user.status data.append({ 'id': user.id, 'name': user.user_name, 'avatar': user.avatar_url, 'status': status, 'join_time': room_user.join_time.strftime('%Y-%m-%d %H:%M:%S'), }) return self.render({ 'status': 0, 'data': data, })
def remove_room_silent_user(room_id, user_id, status): RoomUser.remove_user_silent(room_id, user_id, status)
def jsonify(self, user=None): from starmachine.model.room_user import RoomUser last_content_updated = self.last_content_updated if isinstance(last_content_updated, basestring): if last_content_updated == 'None': last_content_updated = '' else: if last_content_updated: last_content_updated = last_content_updated.strftime( '%Y-%m-%d %H:%M:%S') else: last_content_updated = '' data = { 'id': self.id, 'creator': { 'id': self.creator.id, 'name': self.creator.user_name, 'avatar': self.creator.avatar_url }, 'name': self.name, 'intro': self.intro, 'avatar_url': self.avatar_url, 'limit_user_number': int(self.limit_user_number), 'status': self.status, 'user_amount': self.user_amount, 'balance': self.star_fund, 'create_time': self.create_time if isinstance(self.create_time, basestring) else self.create_time.strftime('%Y-%m-%d %H:%M:%S'), 'tags': [tag.jsonify() for tag in self.tags if tag], 'last_content_updated': last_content_updated, 'admin_users': RoomUser.get_admin_users_by_room(self.id), } room_users = RoomUser.get_ordered_room_users_by_room(self.id) room_users_data = [] for room_user in room_users: user_obj = User.get(room_user.user_id) room_users_data.append({ 'id': user_obj.id, 'name': user_obj.user_name, 'avatar': user_obj.avatar_url, 'status': user_obj.status, }) data.update({ 'users': room_users_data, }) if int(self.status) == ROOM_PRIVATE_NEED_VERIFY: question_info = RoomQuestion.get_question_by_room(self.id) if question_info: data.update({'question_info': question_info.jsonify()}) if user: from starmachine.model.room_user import RoomUser, RoomBlackUser data.update({ 'owner': bool(int(self.creator_id) == int(user.id)), 'joined': bool(RoomUser.room_exists_user(self.id, user.id)), 'is_black': bool(RoomBlackUser.is_room_black_user(self.id, user.id)), }) return data
def job_delete_room_user(room_id, user_id): from starmachine.model.room_user import RoomUser RoomUser.delete_room_user_from_sql(room_id, user_id)
def add_room_user(room_id, user_id, join_time, user_status): from starmachine.model.room_user import RoomUser RoomUser.add_to_mysql(room_id, user_id, join_time, user_status)
# coding: utf-8 from starmachine.model.content import Content from starmachine.model.room import Room from starmachine.model.room_user import RoomUser contents = Content.gets_all() for content in contents: room = Room.get(content.room_id) room_id = content.room_id user_id = content.creator_id room_user = RoomUser.get_by_room_and_user(room_id, user_id) if not room_user: content.update(room_user_status=1) else: content.update(room_user_status=room_user.status) room.update(last_content_updated=content.create_time)
# coding: utf-8 from starmachine.model.room_user import RoomUser from starmachine.model.user import User from starmachine.model.room import Room room_users = RoomUser.gets_all() try: for room_user in room_users: room_id = room_user.room_id user_id = room_user.user_id room = Room.get(room_id) if room: if int(room.creator_id) == int(user_id): room_user.update(status=1) except Exception as e: print e
def has_room_speak_access(self, room_id): from starmachine.model.room_user import RoomUser if RoomUser.is_room_silent_user(room_id, self.id): return False return True