public static void post(Long convID, String msg) { if (!Security.isConnected()) return; Conversation conv = Conversation.findById(convID); if (conv == null) { conv = new Conversation(); conv.id = convID; conv.save(); } Message mess = new Message(); mess.conversation = conv; mess.user = Security.loggedUser(); mess.contents = msg; mess.timeSent = new Date(); mess.save(); }
@Security.Authenticated(Secured.class) public static Result viewMyConversations() { return ok( viewConversations.render( Student.find.byId(request().username()), Conversation.findConversationsOfStudent(Student.find.byId(request().username())))); }
@Security.Authenticated(Secured.class) public static Result viewMyConversation(Long id) { Student s = Student.find.byId(request().username()); Conversation c = Conversation.find.byId(id.toString()); if (!c.getParticipants().contains(s)) { // unauthorized access! return redirect(routes.Application.viewMyConversations()); } else { // mark all read for (Message m : c.messages) { if (!m.sender.email.equals(s.email)) { m.setRead(); m.save(); } } return ok(viewConversation.render(s, c, form(MessageForm.class))); } }
public static long getConversationID(String stud1, String stud2) { List<Conversation> convs = Conversation.findConversationsOfStudent(Student.find.byId(stud1)); long id = -1; // default value, new conv needs to be created if (stud1.equals(stud2)) { return -2; } for (Conversation c : convs) { if ((c.participants.get(0).email.equals(stud1) && c.participants.get(1).email.equals(stud2)) || (c.participants.get(0).email.equals(stud2) && c.participants.get(1).email.equals(stud1))) { id = c.id; } } if (id == -1) { Conversation c = new Conversation(Student.find.byId(stud1), Student.find.byId(stud2)); c.save(); id = c.id; } return id; }
@Security.Authenticated(Secured.class) public static Result addNewMessageForm(Long id) { Form<MessageForm> mesForm = Form.form(MessageForm.class).bindFromRequest(); if (mesForm.hasErrors()) { return badRequest(postNewMessage.render(mesForm, id)); } else { Message m = new Message( mesForm.field("text").value().toString(), Student.find.byId(request().username())); Conversation c = null; c = Conversation.find.byId(id.toString()); c.messages.add(m); c.save(); return redirect(routes.Application.viewMyConversation(id)); } }
public static int numberOfUnreadMessagesOf(Student student) { int numberUnread = 0; List<Conversation> conversations = Conversation.findConversationsOfStudent(student); for (Conversation c : conversations) { for (Message m : c.messages) { if (!m.sender.email.equals(student.email) && !m.read) { numberUnread++; } } } return numberUnread; }