The lawsuit gave Twitter a chance to assume a favorite role as a defender of free speech, offering a respite for a company that has struggled recently to expand its audience, excite investors or attract new revenue streams.